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>
52 #include <netinet/sctp_cc_functions.h>
60 /* Init the SCTP pcb in sctp_pcb.c */
66 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 sctp_max_chunks_on_queue = (nmbclusters / 8);
69 * Allow a user to take no more than 1/2 the number of clusters or
70 * the SB_MAX whichever is smaller for the send window.
72 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
74 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76 * Now for the recv window, should we take the same amount? or
77 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 * now I will just copy.
80 sctp_recvspace = sctp_sendspace;
87 * cleanup of the sctppcbinfo structure.
88 * Assumes that the sctppcbinfo lock is held.
91 sctp_pcbinfo_cleanup(void)
93 /* free the hash tables */
94 if (sctppcbinfo.sctp_asochash != NULL)
95 SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
96 if (sctppcbinfo.sctp_ephash != NULL)
97 SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
98 if (sctppcbinfo.sctp_tcpephash != NULL)
99 SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
100 if (sctppcbinfo.sctp_restarthash != NULL)
101 SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
106 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
107 struct sctp_tcb *stcb,
108 struct sctp_nets *net,
111 struct sctp_tmit_chunk *chk;
113 /* Adjust that too */
114 stcb->asoc.smallest_mtu = nxtsz;
115 /* now off to subtract IP_DF flag if needed */
116 #ifdef SCTP_PRINT_FOR_B_AND_M
117 SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
118 inp, stcb, net, nxtsz);
120 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
121 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
125 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
126 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
128 * For this guy we also mark for immediate resend
129 * since we sent to big of chunk
131 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
132 if (chk->sent != SCTP_DATAGRAM_RESEND) {
133 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
135 chk->sent = SCTP_DATAGRAM_RESEND;
136 chk->rec.data.doing_fast_retransmit = 0;
137 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
138 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
139 chk->whoTo->flight_size,
141 (uintptr_t) chk->whoTo,
142 chk->rec.data.TSN_seq);
144 /* Clear any time so NO RTT is being done */
146 sctp_flight_size_decrease(chk);
147 sctp_total_flight_decrease(stcb, chk);
153 sctp_notify_mbuf(struct sctp_inpcb *inp,
154 struct sctp_tcb *stcb,
155 struct sctp_nets *net,
160 int totsz, tmr_stopped = 0;
164 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165 (ip == NULL) || (sh == NULL)) {
167 SCTP_TCB_UNLOCK(stcb);
171 /* First job is to verify the vtag matches what I would send */
172 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173 SCTP_TCB_UNLOCK(stcb);
176 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
178 if (icmph->icmp_type != ICMP_UNREACH) {
179 /* We only care about unreachable */
180 SCTP_TCB_UNLOCK(stcb);
183 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184 /* not a unreachable message due to frag. */
185 SCTP_TCB_UNLOCK(stcb);
190 nxtsz = ntohs(icmph->icmp_seq);
193 * old type router that does not tell us what the next size
194 * mtu is. Rats we will have to guess (in a educated fashion
197 nxtsz = find_next_best_mtu(totsz);
199 /* Stop any PMTU timer */
200 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
202 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
203 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
205 /* Adjust destination size limit */
206 if (net->mtu > nxtsz) {
209 /* now what about the ep? */
210 if (stcb->asoc.smallest_mtu > nxtsz) {
211 #ifdef SCTP_PRINT_FOR_B_AND_M
212 SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
215 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
218 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
220 SCTP_TCB_UNLOCK(stcb);
225 sctp_notify(struct sctp_inpcb *inp,
229 struct sctp_tcb *stcb,
230 struct sctp_nets *net)
233 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
234 (sh == NULL) || (to == NULL)) {
237 /* First job is to verify the vtag matches what I would send */
238 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
241 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
243 if ((error == EHOSTUNREACH) || /* Host is not reachable */
244 (error == EHOSTDOWN) || /* Host is down */
245 (error == ECONNREFUSED) || /* Host refused the connection, (not
247 (error == ENOPROTOOPT) /* SCTP is not present on host */
250 * Hmm reachablity problems we must examine closely. If its
251 * not reachable, we may have lost a network. Or if there is
252 * NO protocol at the other end named SCTP. well we consider
255 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
256 if (net->dest_state & SCTP_ADDR_REACHABLE) {
257 /* Ok that destination is NOT reachable */
258 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
260 net->failure_threshold,
263 net->dest_state &= ~SCTP_ADDR_REACHABLE;
264 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
266 * JRS 5/14/07 - If a destination is
267 * unreachable, the PF bit is turned off.
268 * This allows an unambiguous use of the PF
269 * bit for destinations that are reachable
270 * but potentially failed. If the
271 * destination is set to the unreachable
272 * state, also set the destination to the PF
276 * Add debug message here if destination is
279 /* Stop any running T3 timers here? */
280 if (sctp_cmt_on_off && sctp_cmt_pf) {
281 net->dest_state &= ~SCTP_ADDR_PF;
282 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
285 net->error_count = net->failure_threshold + 1;
286 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
287 stcb, SCTP_FAILED_THRESHOLD,
291 SCTP_TCB_UNLOCK(stcb);
295 * Here the peer is either playing tricks on us,
296 * including an address that belongs to someone who
297 * does not support SCTP OR was a userland
298 * implementation that shutdown and now is dead. In
299 * either case treat it like a OOTB abort with no
302 sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
303 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
304 /* no need to unlock here, since the TCB is gone */
307 /* Send all others to the app */
309 SCTP_TCB_UNLOCK(stcb);
311 if (inp->sctp_socket) {
312 #ifdef SCTP_LOCK_LOGGING
313 if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
314 sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
317 SOCK_LOCK(inp->sctp_socket);
318 inp->sctp_socket->so_error = error;
319 sctp_sowwakeup(inp, inp->sctp_socket);
320 SOCK_UNLOCK(inp->sctp_socket);
326 sctp_ctlinput(cmd, sa, vip)
335 /* FIX, for non-bsd is this right? */
336 vrf_id = SCTP_DEFAULT_VRFID;
337 if (sa->sa_family != AF_INET ||
338 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
341 if (PRC_IS_REDIRECT(cmd)) {
343 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
347 struct sctp_inpcb *inp = NULL;
348 struct sctp_tcb *stcb = NULL;
349 struct sctp_nets *net = NULL;
350 struct sockaddr_in to, from;
352 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
353 bzero(&to, sizeof(to));
354 bzero(&from, sizeof(from));
355 from.sin_family = to.sin_family = AF_INET;
356 from.sin_len = to.sin_len = sizeof(to);
357 from.sin_port = sh->src_port;
358 from.sin_addr = ip->ip_src;
359 to.sin_port = sh->dest_port;
360 to.sin_addr = ip->ip_dst;
363 * 'to' holds the dest of the packet that failed to be sent.
364 * 'from' holds our local endpoint address. Thus we reverse
365 * the to and the from in the lookup.
367 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
368 (struct sockaddr *)&to,
369 &inp, &net, 1, vrf_id);
370 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
371 if (cmd != PRC_MSGSIZE) {
374 if (cmd == PRC_HOSTDEAD) {
377 cm = inetctlerrmap[cmd];
379 sctp_notify(inp, cm, sh,
380 (struct sockaddr *)&to, stcb,
383 /* handle possible ICMP size messages */
384 sctp_notify_mbuf(inp, stcb, net, ip, sh);
387 if ((stcb == NULL) && (inp != NULL)) {
388 /* reduce ref-count */
390 SCTP_INP_DECR_REF(inp);
391 SCTP_INP_WUNLOCK(inp);
399 sctp_getcred(SYSCTL_HANDLER_ARGS)
402 struct sockaddr_in addrs[2];
403 struct sctp_inpcb *inp;
404 struct sctp_nets *net;
405 struct sctp_tcb *stcb;
409 /* FIX, for non-bsd is this right? */
410 vrf_id = SCTP_DEFAULT_VRFID;
412 error = priv_check(req->td, PRIV_NETINET_GETCRED);
417 error = SYSCTL_IN(req, addrs, sizeof(addrs));
421 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
423 &inp, &net, 1, vrf_id);
424 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
425 if ((inp != NULL) && (stcb == NULL)) {
426 /* reduce ref-count */
428 SCTP_INP_DECR_REF(inp);
434 SCTP_TCB_UNLOCK(stcb);
436 * We use the write lock here, only since in the error leg we need
437 * it. If we used RLOCK, then we would have to
438 * wlock/decr/unlock/rlock. Which in theory could create a hole.
439 * Better to use higher wlock.
443 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
445 SCTP_INP_WUNLOCK(inp);
448 cru2x(inp->sctp_socket->so_cred, &xuc);
449 SCTP_INP_WUNLOCK(inp);
450 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
455 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
456 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
460 sctp_abort(struct socket *so)
462 struct sctp_inpcb *inp;
465 inp = (struct sctp_inpcb *)so->so_pcb;
470 flags = inp->sctp_flags;
471 #ifdef SCTP_LOG_CLOSING
472 sctp_log_closing(inp, NULL, 17);
474 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
475 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
476 #ifdef SCTP_LOG_CLOSING
477 sctp_log_closing(inp, NULL, 16);
479 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
480 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
482 SCTP_SB_CLEAR(so->so_snd);
484 * same for the rcv ones, they are only here for the
487 SCTP_SB_CLEAR(so->so_rcv);
489 /* Now null out the reference, we are completely detached. */
493 flags = inp->sctp_flags;
494 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
495 goto sctp_must_try_again;
502 sctp_attach(struct socket *so, int proto, struct thread *p)
504 struct sctp_inpcb *inp;
505 struct inpcb *ip_inp;
507 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
513 inp = (struct sctp_inpcb *)so->so_pcb;
517 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
521 error = sctp_inpcb_alloc(so, vrf_id);
525 inp = (struct sctp_inpcb *)so->so_pcb;
528 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
529 ip_inp = &inp->ip_inp.inp;
530 ip_inp->inp_vflag |= INP_IPV4;
531 ip_inp->inp_ip_ttl = ip_defttl;
534 error = ipsec_init_policy(so, &ip_inp->inp_sp);
535 #ifdef SCTP_LOG_CLOSING
536 sctp_log_closing(inp, NULL, 17);
539 flags = inp->sctp_flags;
540 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
541 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
542 #ifdef SCTP_LOG_CLOSING
543 sctp_log_closing(inp, NULL, 15);
545 SCTP_INP_WUNLOCK(inp);
546 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
547 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
549 SCTP_INP_WUNLOCK(inp);
554 SCTP_INP_WUNLOCK(inp);
559 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
561 struct sctp_inpcb *inp;
565 if (addr && addr->sa_family != AF_INET)
566 /* must be a v4 address! */
569 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
572 inp = (struct sctp_inpcb *)so->so_pcb;
576 error = sctp_inpcb_bind(so, addr, NULL, p);
581 sctp_close(struct socket *so)
583 struct sctp_inpcb *inp;
586 inp = (struct sctp_inpcb *)so->so_pcb;
591 * Inform all the lower layer assoc that we are done.
594 flags = inp->sctp_flags;
595 #ifdef SCTP_LOG_CLOSING
596 sctp_log_closing(inp, NULL, 17);
598 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
599 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
600 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
601 (so->so_rcv.sb_cc > 0)) {
602 #ifdef SCTP_LOG_CLOSING
603 sctp_log_closing(inp, NULL, 13);
605 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
606 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
608 #ifdef SCTP_LOG_CLOSING
609 sctp_log_closing(inp, NULL, 14);
611 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
612 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
615 * The socket is now detached, no matter what the state of
616 * the SCTP association.
619 SCTP_SB_CLEAR(so->so_snd);
621 * same for the rcv ones, they are only here for the
624 SCTP_SB_CLEAR(so->so_rcv);
626 /* Now null out the reference, we are completely detached. */
630 flags = inp->sctp_flags;
631 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
632 goto sctp_must_try_again;
640 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
641 struct mbuf *control, struct thread *p);
645 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
646 struct mbuf *control, struct thread *p)
648 struct sctp_inpcb *inp;
651 inp = (struct sctp_inpcb *)so->so_pcb;
654 sctp_m_freem(control);
660 /* Got to have an to address if we are NOT a connected socket */
661 if ((addr == NULL) &&
662 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
663 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
666 } else if (addr == NULL) {
667 error = EDESTADDRREQ;
670 sctp_m_freem(control);
676 if (addr->sa_family != AF_INET) {
677 /* must be a v4 address! */
680 sctp_m_freem(control);
683 error = EDESTADDRREQ;
688 /* now what about control */
691 SCTP_PRINTF("huh? control set?\n");
692 sctp_m_freem(inp->control);
695 inp->control = control;
699 SCTP_BUF_NEXT(inp->pkt_last) = m;
702 inp->pkt_last = inp->pkt = m;
705 /* FreeBSD uses a flag passed */
706 ((flags & PRUS_MORETOCOME) == 0)
709 * note with the current version this code will only be used
710 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
711 * re-defining sosend to use the sctp_sosend. One can
712 * optionally switch back to this code (by changing back the
713 * definitions) but this is not advisable. This code is used
714 * by FreeBSD when sending a file with sendfile() though.
718 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
728 sctp_disconnect(struct socket *so)
730 struct sctp_inpcb *inp;
732 inp = (struct sctp_inpcb *)so->so_pcb;
737 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
738 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
739 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
741 SCTP_INP_RUNLOCK(inp);
744 struct sctp_association *asoc;
745 struct sctp_tcb *stcb;
747 stcb = LIST_FIRST(&inp->sctp_asoc_list);
749 SCTP_INP_RUNLOCK(inp);
754 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
755 /* We are about to be freed, out of here */
756 SCTP_TCB_UNLOCK(stcb);
757 SCTP_INP_RUNLOCK(inp);
760 if (((so->so_options & SO_LINGER) &&
761 (so->so_linger == 0)) ||
762 (so->so_rcv.sb_cc > 0)) {
763 if (SCTP_GET_STATE(asoc) !=
764 SCTP_STATE_COOKIE_WAIT) {
765 /* Left with Data unread */
768 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
774 struct sctp_paramhdr *ph;
776 ph = mtod(err, struct sctp_paramhdr *);
777 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
778 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
779 ph->param_length = htons(SCTP_BUF_LEN(err));
781 sctp_send_abort_tcb(stcb, err);
782 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
784 SCTP_INP_RUNLOCK(inp);
785 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
786 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
787 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
789 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
790 /* No unlock tcb assoc is gone */
793 if (TAILQ_EMPTY(&asoc->send_queue) &&
794 TAILQ_EMPTY(&asoc->sent_queue) &&
795 (asoc->stream_queue_cnt == 0)) {
796 /* there is nothing queued to send, so done */
797 if (asoc->locked_on_sending) {
800 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
801 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
802 /* only send SHUTDOWN 1st time thru */
803 sctp_stop_timers_for_shutdown(stcb);
804 sctp_send_shutdown(stcb,
805 stcb->asoc.primary_destination);
806 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
807 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
808 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
809 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
811 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
812 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
814 asoc->primary_destination);
815 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
817 asoc->primary_destination);
821 * we still got (or just got) data to send,
822 * so set SHUTDOWN_PENDING
825 * XXX sockets draft says that SCTP_EOF
826 * should be sent with no data. currently,
827 * we will allow user data to be sent first
828 * and move to SHUTDOWN-PENDING
830 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
831 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
832 asoc->primary_destination);
833 if (asoc->locked_on_sending) {
834 /* Locked to send out the data */
835 struct sctp_stream_queue_pending *sp;
837 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
839 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
840 asoc->locked_on_sending->stream_no);
842 if ((sp->length == 0) && (sp->msg_is_complete == 0))
843 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
846 if (TAILQ_EMPTY(&asoc->send_queue) &&
847 TAILQ_EMPTY(&asoc->sent_queue) &&
848 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
852 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
853 0, M_DONTWAIT, 1, MT_DATA);
859 struct sctp_paramhdr *ph;
862 SCTP_BUF_LEN(op_err) =
863 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
865 struct sctp_paramhdr *);
866 ph->param_type = htons(
867 SCTP_CAUSE_USER_INITIATED_ABT);
868 ph->param_length = htons(SCTP_BUF_LEN(op_err));
869 ippp = (uint32_t *) (ph + 1);
870 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
872 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
873 sctp_send_abort_tcb(stcb, op_err);
874 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
875 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
876 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
877 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
879 SCTP_INP_RUNLOCK(inp);
880 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
884 SCTP_TCB_UNLOCK(stcb);
885 SCTP_INP_RUNLOCK(inp);
890 /* UDP model does not support this */
891 SCTP_INP_RUNLOCK(inp);
897 sctp_shutdown(struct socket *so)
899 struct sctp_inpcb *inp;
901 inp = (struct sctp_inpcb *)so->so_pcb;
906 /* For UDP model this is a invalid call */
907 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
908 /* Restore the flags that the soshutdown took away. */
909 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
910 /* This proc will wakeup for read and do nothing (I hope) */
911 SCTP_INP_RUNLOCK(inp);
915 * Ok if we reach here its the TCP model and it is either a SHUT_WR
916 * or SHUT_RDWR. This means we put the shutdown flag against it.
919 struct sctp_tcb *stcb;
920 struct sctp_association *asoc;
924 stcb = LIST_FIRST(&inp->sctp_asoc_list);
927 * Ok we hit the case that the shutdown call was
928 * made after an abort or something. Nothing to do
931 SCTP_INP_RUNLOCK(inp);
936 if (TAILQ_EMPTY(&asoc->send_queue) &&
937 TAILQ_EMPTY(&asoc->sent_queue) &&
938 (asoc->stream_queue_cnt == 0)) {
939 if (asoc->locked_on_sending) {
942 /* there is nothing queued to send, so I'm done... */
943 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
944 /* only send SHUTDOWN the first time through */
945 sctp_stop_timers_for_shutdown(stcb);
946 sctp_send_shutdown(stcb,
947 stcb->asoc.primary_destination);
948 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
949 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
950 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
951 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
953 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
954 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
956 asoc->primary_destination);
957 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
959 asoc->primary_destination);
963 * we still got (or just got) data to send, so set
966 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
967 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
968 asoc->primary_destination);
970 if (asoc->locked_on_sending) {
971 /* Locked to send out the data */
972 struct sctp_stream_queue_pending *sp;
974 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
976 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
977 asoc->locked_on_sending->stream_no);
979 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
980 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
984 if (TAILQ_EMPTY(&asoc->send_queue) &&
985 TAILQ_EMPTY(&asoc->sent_queue) &&
986 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
990 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
991 0, M_DONTWAIT, 1, MT_DATA);
993 /* Fill in the user initiated abort */
994 struct sctp_paramhdr *ph;
997 SCTP_BUF_LEN(op_err) =
998 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1000 struct sctp_paramhdr *);
1001 ph->param_type = htons(
1002 SCTP_CAUSE_USER_INITIATED_ABT);
1003 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1004 ippp = (uint32_t *) (ph + 1);
1005 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1007 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1008 sctp_abort_an_association(stcb->sctp_ep, stcb,
1009 SCTP_RESPONSE_TO_USER_REQ,
1014 SCTP_TCB_UNLOCK(stcb);
1017 SCTP_INP_RUNLOCK(inp);
1022 * copies a "user" presentable address and removes embedded scope, etc.
1023 * returns 0 on success, 1 on error
1026 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1028 struct sockaddr_in6 lsa6;
1030 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1032 memcpy(ss, sa, sa->sa_len);
1039 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1040 struct sctp_tcb *stcb,
1042 struct sockaddr_storage *sas,
1045 struct sctp_ifn *sctp_ifn;
1046 struct sctp_ifa *sctp_ifa;
1047 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1049 int ipv4_addr_legal, ipv6_addr_legal;
1050 struct sctp_vrf *vrf;
1057 /* Turn on all the appropriate scope */
1058 loopback_scope = stcb->asoc.loopback_scope;
1059 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1060 local_scope = stcb->asoc.local_scope;
1061 site_scope = stcb->asoc.site_scope;
1063 /* Turn on ALL scope, since we look at the EP */
1064 loopback_scope = ipv4_local_scope = local_scope =
1067 ipv4_addr_legal = ipv6_addr_legal = 0;
1068 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1069 ipv6_addr_legal = 1;
1070 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1071 ipv4_addr_legal = 1;
1074 ipv4_addr_legal = 1;
1076 vrf = sctp_find_vrf(vrf_id);
1080 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1081 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1082 if ((loopback_scope == 0) &&
1083 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1084 /* Skip loopback if loopback_scope not set */
1087 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1090 * For the BOUND-ALL case, the list
1091 * associated with a TCB is Always
1092 * considered a reverse list.. i.e.
1093 * it lists addresses that are NOT
1094 * part of the association. If this
1095 * is one of those we must skip it.
1097 if (sctp_is_addr_restricted(stcb,
1102 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1103 (ipv4_addr_legal)) {
1104 struct sockaddr_in *sin;
1106 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1107 if (sin->sin_addr.s_addr == 0) {
1109 * we skip unspecifed
1114 if ((ipv4_local_scope == 0) &&
1115 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1118 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1119 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1120 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1121 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1122 actual += sizeof(sizeof(struct sockaddr_in6));
1124 memcpy(sas, sin, sizeof(*sin));
1125 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1126 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1127 actual += sizeof(*sin);
1129 if (actual >= limit) {
1132 } else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1133 (ipv6_addr_legal)) {
1134 struct sockaddr_in6 *sin6;
1136 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1137 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1139 * we skip unspecifed
1144 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1145 if (local_scope == 0)
1147 if (sin6->sin6_scope_id == 0) {
1148 if (sa6_recoverscope(sin6) != 0)
1157 if ((site_scope == 0) &&
1158 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1161 memcpy(sas, sin6, sizeof(*sin6));
1162 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1163 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1164 actual += sizeof(*sin6);
1165 if (actual >= limit) {
1172 struct sctp_laddr *laddr;
1174 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1176 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1180 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1183 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1184 sas = (struct sockaddr_storage *)((caddr_t)sas +
1185 laddr->ifa->address.sa.sa_len);
1186 actual += laddr->ifa->address.sa.sa_len;
1187 if (actual >= limit) {
1196 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1197 struct sctp_tcb *stcb,
1199 struct sockaddr_storage *sas)
1203 /* fill up addresses for the endpoint's default vrf */
1204 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1210 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1213 struct sctp_vrf *vrf = NULL;
1216 * In both sub-set bound an bound_all cases we return the MAXIMUM
1217 * number of addresses that you COULD get. In reality the sub-set
1218 * bound may have an exclusion list for a given TCB OR in the
1219 * bound-all case a TCB may NOT include the loopback or other
1220 * addresses as well.
1222 vrf = sctp_find_vrf(vrf_id);
1226 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1227 struct sctp_ifn *sctp_ifn;
1228 struct sctp_ifa *sctp_ifa;
1230 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1231 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1232 /* Count them if they are the right type */
1233 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1234 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1235 cnt += sizeof(struct sockaddr_in6);
1237 cnt += sizeof(struct sockaddr_in);
1239 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1240 cnt += sizeof(struct sockaddr_in6);
1244 struct sctp_laddr *laddr;
1246 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1247 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1248 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1249 cnt += sizeof(struct sockaddr_in6);
1251 cnt += sizeof(struct sockaddr_in);
1253 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1254 cnt += sizeof(struct sockaddr_in6);
1261 sctp_count_max_addresses(struct sctp_inpcb *inp)
1265 /* count addresses for the endpoint's default VRF */
1266 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1271 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1272 size_t optsize, void *p, int delay)
1275 int creat_lock_on = 0;
1276 struct sctp_tcb *stcb = NULL;
1277 struct sockaddr *sa;
1278 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1281 int bad_addresses = 0;
1284 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1286 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1287 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1288 /* We are already connected AND the TCP model */
1289 return (EADDRINUSE);
1291 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1294 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1295 SCTP_INP_RLOCK(inp);
1296 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1297 SCTP_INP_RUNLOCK(inp);
1302 SCTP_INP_INCR_REF(inp);
1303 SCTP_ASOC_CREATE_LOCK(inp);
1305 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1306 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1310 totaddrp = (int *)optval;
1311 totaddr = *totaddrp;
1312 sa = (struct sockaddr *)(totaddrp + 1);
1313 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1314 if ((stcb != NULL) || bad_addresses) {
1315 /* Already have or am bring up an association */
1316 SCTP_ASOC_CREATE_UNLOCK(inp);
1319 SCTP_TCB_UNLOCK(stcb);
1320 if (bad_addresses == 0)
1325 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1330 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1332 struct in6pcb *inp6;
1334 inp6 = (struct in6pcb *)inp;
1335 if (SCTP_IPV6_V6ONLY(inp6)) {
1337 * if IPV6_V6ONLY flag, ignore connections destined
1338 * to a v4 addr or v4-mapped addr
1345 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1346 SCTP_PCB_FLAGS_UNBOUND) {
1347 /* Bind a ephemeral port */
1348 error = sctp_inpcb_bind(so, NULL, NULL, p);
1353 /* FIX ME: do we want to pass in a vrf on the connect call? */
1354 vrf_id = inp->def_vrf_id;
1356 /* We are GOOD to go */
1357 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1361 /* Gak! no memory */
1364 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1365 /* move to second address */
1366 if (sa->sa_family == AF_INET)
1367 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1369 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1372 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1373 /* Fill in the return id */
1375 sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1378 a_id = (sctp_assoc_t *) optval;
1379 *a_id = sctp_get_associd(stcb);
1381 /* initialize authentication parameters for the assoc */
1382 sctp_initialize_auth_params(inp, stcb);
1385 /* doing delayed connection */
1386 stcb->asoc.delayed_connection = 1;
1387 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1389 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1390 sctp_send_initiate(inp, stcb);
1392 SCTP_TCB_UNLOCK(stcb);
1393 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1394 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1395 /* Set the connected flag so we can queue data */
1399 if (creat_lock_on) {
1400 SCTP_ASOC_CREATE_UNLOCK(inp);
1402 SCTP_INP_DECR_REF(inp);
1406 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1407 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1408 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1409 SCTP_INP_RLOCK(inp); \
1410 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1412 SCTP_TCB_LOCK(stcb); \
1414 SCTP_INP_RUNLOCK(inp); \
1415 } else if (assoc_id != 0) { \
1416 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1417 if (stcb == NULL) { \
1427 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1428 if (size < sizeof(type)) { \
1432 destp = (type *)srcp; \
1437 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1440 struct sctp_inpcb *inp;
1442 struct sctp_tcb *stcb = NULL;
1444 if (optval == NULL) {
1447 inp = (struct sctp_inpcb *)so->so_pcb;
1454 case SCTP_AUTOCLOSE:
1455 case SCTP_EXPLICIT_EOR:
1456 case SCTP_AUTO_ASCONF:
1457 case SCTP_DISABLE_FRAGMENTS:
1458 case SCTP_I_WANT_MAPPED_V4_ADDR:
1459 case SCTP_USE_EXT_RCVINFO:
1460 SCTP_INP_RLOCK(inp);
1462 case SCTP_DISABLE_FRAGMENTS:
1463 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1465 case SCTP_I_WANT_MAPPED_V4_ADDR:
1466 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1468 case SCTP_AUTO_ASCONF:
1469 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1471 case SCTP_EXPLICIT_EOR:
1472 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1475 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1477 case SCTP_USE_EXT_RCVINFO:
1478 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1480 case SCTP_AUTOCLOSE:
1481 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1482 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1488 error = ENOPROTOOPT;
1489 } /* end switch (sopt->sopt_name) */
1490 if (optname != SCTP_AUTOCLOSE) {
1491 /* make it an "on/off" value */
1494 if (*optsize < sizeof(val)) {
1497 SCTP_INP_RUNLOCK(inp);
1499 /* return the option value */
1500 *(int *)optval = val;
1501 *optsize = sizeof(val);
1504 case SCTP_GET_PACKET_LOG:
1506 #ifdef SCTP_PACKET_LOGGING
1510 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1511 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1518 case SCTP_PARTIAL_DELIVERY_POINT:
1522 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1523 *value = inp->partial_delivery_point;
1524 *optsize = sizeof(uint32_t);
1527 case SCTP_FRAGMENT_INTERLEAVE:
1531 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1532 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1533 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1534 *value = SCTP_FRAG_LEVEL_2;
1536 *value = SCTP_FRAG_LEVEL_1;
1539 *value = SCTP_FRAG_LEVEL_0;
1541 *optsize = sizeof(uint32_t);
1544 case SCTP_CMT_ON_OFF:
1546 struct sctp_assoc_value *av;
1548 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1549 if (sctp_cmt_on_off) {
1550 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1552 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1553 SCTP_TCB_UNLOCK(stcb);
1559 error = ENOPROTOOPT;
1561 *optsize = sizeof(*av);
1564 /* JRS - Get socket option for pluggable congestion control */
1565 case SCTP_PLUGGABLE_CC:
1567 struct sctp_assoc_value *av;
1569 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1570 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1572 av->assoc_value = stcb->asoc.congestion_control_module;
1573 SCTP_TCB_UNLOCK(stcb);
1575 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1577 *optsize = sizeof(*av);
1580 case SCTP_GET_ADDR_LEN:
1582 struct sctp_assoc_value *av;
1584 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1587 if (av->assoc_value == AF_INET) {
1588 av->assoc_value = sizeof(struct sockaddr_in);
1593 if (av->assoc_value == AF_INET6) {
1594 av->assoc_value = sizeof(struct sockaddr_in6);
1598 *optsize = sizeof(*av);
1601 case SCTP_GET_ASSOC_NUMBER:
1603 uint32_t *value, cnt;
1605 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1607 SCTP_INP_RLOCK(inp);
1608 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1611 SCTP_INP_RUNLOCK(inp);
1613 *optsize = sizeof(uint32_t);
1617 case SCTP_GET_ASSOC_ID_LIST:
1619 struct sctp_assoc_ids *ids;
1620 unsigned int at, limit;
1622 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1624 limit = *optsize / sizeof(sctp_assoc_t);
1625 SCTP_INP_RLOCK(inp);
1626 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1628 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1634 SCTP_INP_RUNLOCK(inp);
1635 *optsize = at * sizeof(sctp_assoc_t);
1640 struct sctp_assoc_value *av;
1642 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1643 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1646 av->assoc_value = stcb->asoc.context;
1647 SCTP_TCB_UNLOCK(stcb);
1649 SCTP_INP_RLOCK(inp);
1650 av->assoc_value = inp->sctp_context;
1651 SCTP_INP_RUNLOCK(inp);
1653 *optsize = sizeof(*av);
1658 uint32_t *default_vrfid;
1660 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1661 *default_vrfid = inp->def_vrf_id;
1664 case SCTP_GET_ASOC_VRF:
1666 struct sctp_assoc_value *id;
1668 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1669 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1674 id->assoc_value = stcb->asoc.vrf_id;
1677 case SCTP_GET_VRF_IDS:
1682 case SCTP_GET_NONCE_VALUES:
1684 struct sctp_get_nonce_values *gnv;
1686 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1687 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1690 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1691 gnv->gn_local_tag = stcb->asoc.my_vtag;
1692 SCTP_TCB_UNLOCK(stcb);
1696 *optsize = sizeof(*gnv);
1699 case SCTP_DELAYED_SACK:
1701 struct sctp_sack_info *sack;
1703 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1704 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1706 sack->sack_delay = stcb->asoc.delayed_ack;
1707 sack->sack_freq = stcb->asoc.sack_freq;
1708 SCTP_TCB_UNLOCK(stcb);
1710 SCTP_INP_RLOCK(inp);
1711 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1712 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1713 SCTP_INP_RUNLOCK(inp);
1715 *optsize = sizeof(*sack);
1719 case SCTP_GET_SNDBUF_USE:
1721 struct sctp_sockstat *ss;
1723 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1724 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1727 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1728 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1729 stcb->asoc.size_on_all_streams);
1730 SCTP_TCB_UNLOCK(stcb);
1734 *optsize = sizeof(struct sctp_sockstat);
1737 case SCTP_MAX_BURST:
1741 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1743 SCTP_INP_RLOCK(inp);
1744 *value = inp->sctp_ep.max_burst;
1745 SCTP_INP_RUNLOCK(inp);
1746 *optsize = sizeof(uint8_t);
1751 struct sctp_assoc_value *av;
1754 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1755 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1758 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1759 SCTP_TCB_UNLOCK(stcb);
1761 SCTP_INP_RLOCK(inp);
1762 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1763 ovh = SCTP_MED_OVERHEAD;
1765 ovh = SCTP_MED_V4_OVERHEAD;
1767 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1768 av->assoc_value = 0;
1770 av->assoc_value = inp->sctp_frag_point - ovh;
1771 SCTP_INP_RUNLOCK(inp);
1773 *optsize = sizeof(struct sctp_assoc_value);
1776 case SCTP_GET_STAT_LOG:
1777 error = sctp_fill_stat_log(optval, optsize);
1781 struct sctp_event_subscribe *events;
1783 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1784 memset(events, 0, sizeof(*events));
1785 SCTP_INP_RLOCK(inp);
1786 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1787 events->sctp_data_io_event = 1;
1789 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1790 events->sctp_association_event = 1;
1792 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1793 events->sctp_address_event = 1;
1795 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1796 events->sctp_send_failure_event = 1;
1798 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1799 events->sctp_peer_error_event = 1;
1801 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1802 events->sctp_shutdown_event = 1;
1804 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1805 events->sctp_partial_delivery_event = 1;
1807 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1808 events->sctp_adaptation_layer_event = 1;
1810 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1811 events->sctp_authentication_event = 1;
1813 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1814 events->sctp_stream_reset_events = 1;
1815 SCTP_INP_RUNLOCK(inp);
1816 *optsize = sizeof(struct sctp_event_subscribe);
1820 case SCTP_ADAPTATION_LAYER:
1824 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1826 SCTP_INP_RLOCK(inp);
1827 *value = inp->sctp_ep.adaptation_layer_indicator;
1828 SCTP_INP_RUNLOCK(inp);
1829 *optsize = sizeof(uint32_t);
1832 case SCTP_SET_INITIAL_DBG_SEQ:
1836 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1837 SCTP_INP_RLOCK(inp);
1838 *value = inp->sctp_ep.initial_sequence_debug;
1839 SCTP_INP_RUNLOCK(inp);
1840 *optsize = sizeof(uint32_t);
1843 case SCTP_GET_LOCAL_ADDR_SIZE:
1847 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1848 SCTP_INP_RLOCK(inp);
1849 *value = sctp_count_max_addresses(inp);
1850 SCTP_INP_RUNLOCK(inp);
1851 *optsize = sizeof(uint32_t);
1854 case SCTP_GET_REMOTE_ADDR_SIZE:
1858 struct sctp_nets *net;
1860 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1861 /* FIXME MT: change to sctp_assoc_value? */
1862 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1866 /* Count the sizes */
1867 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1868 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1869 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1870 size += sizeof(struct sockaddr_in6);
1871 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1872 size += sizeof(struct sockaddr_in);
1878 SCTP_TCB_UNLOCK(stcb);
1879 *value = (uint32_t) size;
1883 *optsize = sizeof(uint32_t);
1886 case SCTP_GET_PEER_ADDRESSES:
1888 * Get the address information, an array is passed in to
1889 * fill up we pack it.
1893 struct sockaddr_storage *sas;
1894 struct sctp_nets *net;
1895 struct sctp_getaddresses *saddr;
1897 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1898 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1901 left = (*optsize) - sizeof(struct sctp_getaddresses);
1902 *optsize = sizeof(struct sctp_getaddresses);
1903 sas = (struct sockaddr_storage *)&saddr->addr[0];
1905 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1906 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1907 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1908 cpsz = sizeof(struct sockaddr_in6);
1909 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1910 cpsz = sizeof(struct sockaddr_in);
1916 /* not enough room. */
1919 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1920 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1921 /* Must map the address */
1922 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1923 (struct sockaddr_in6 *)sas);
1925 memcpy(sas, &net->ro._l_addr, cpsz);
1927 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1929 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1933 SCTP_TCB_UNLOCK(stcb);
1939 case SCTP_GET_LOCAL_ADDRESSES:
1941 size_t limit, actual;
1942 struct sockaddr_storage *sas;
1943 struct sctp_getaddresses *saddr;
1945 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1946 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1948 sas = (struct sockaddr_storage *)&saddr->addr[0];
1949 limit = *optsize - sizeof(sctp_assoc_t);
1950 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1952 SCTP_TCB_UNLOCK(stcb);
1954 *optsize = sizeof(struct sockaddr_storage) + actual;
1957 case SCTP_PEER_ADDR_PARAMS:
1959 struct sctp_paddrparams *paddrp;
1960 struct sctp_nets *net;
1962 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1963 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1967 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1970 * We increment here since
1971 * sctp_findassociation_ep_addr() wil do a
1972 * decrement if it finds the stcb as long as
1973 * the locked tcb (last argument) is NOT a
1976 SCTP_INP_INCR_REF(inp);
1977 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1979 SCTP_INP_DECR_REF(inp);
1984 /* Applys to the specific association */
1985 paddrp->spp_flags = 0;
1989 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1990 ovh = SCTP_MED_OVERHEAD;
1992 ovh = SCTP_MED_V4_OVERHEAD;
1996 paddrp->spp_pathmaxrxt = net->failure_threshold;
1997 paddrp->spp_pathmtu = net->mtu - ovh;
1998 /* get flags for HB */
1999 if (net->dest_state & SCTP_ADDR_NOHB)
2000 paddrp->spp_flags |= SPP_HB_DISABLE;
2002 paddrp->spp_flags |= SPP_HB_ENABLE;
2003 /* get flags for PMTU */
2004 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2005 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2007 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2010 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2011 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2012 paddrp->spp_flags |= SPP_IPV4_TOS;
2016 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2017 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2018 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2023 * No destination so return default
2028 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2029 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2031 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2032 paddrp->spp_flags |= SPP_IPV4_TOS;
2035 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2036 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2038 /* default settings should be these */
2039 if (stcb->asoc.hb_is_disabled == 0) {
2040 paddrp->spp_flags |= SPP_HB_ENABLE;
2042 paddrp->spp_flags |= SPP_HB_DISABLE;
2044 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2045 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2050 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2053 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2054 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2055 SCTP_TCB_UNLOCK(stcb);
2057 /* Use endpoint defaults */
2058 SCTP_INP_RLOCK(inp);
2059 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2060 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2061 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2062 /* get inp's default */
2064 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2065 paddrp->spp_flags |= SPP_IPV4_TOS;
2068 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2069 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2070 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2073 /* can't return this */
2074 paddrp->spp_pathmtu = 0;
2076 /* default behavior, no stcb */
2077 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2079 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2080 paddrp->spp_flags |= SPP_HB_ENABLE;
2082 paddrp->spp_flags |= SPP_HB_DISABLE;
2084 SCTP_INP_RUNLOCK(inp);
2086 *optsize = sizeof(struct sctp_paddrparams);
2089 case SCTP_GET_PEER_ADDR_INFO:
2091 struct sctp_paddrinfo *paddri;
2092 struct sctp_nets *net;
2094 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2095 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2099 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2102 * We increment here since
2103 * sctp_findassociation_ep_addr() wil do a
2104 * decrement if it finds the stcb as long as
2105 * the locked tcb (last argument) is NOT a
2108 SCTP_INP_INCR_REF(inp);
2109 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2111 SCTP_INP_DECR_REF(inp);
2115 if ((stcb) && (net)) {
2116 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2117 paddri->spinfo_cwnd = net->cwnd;
2118 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2119 paddri->spinfo_rto = net->RTO;
2120 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2121 SCTP_TCB_UNLOCK(stcb);
2124 SCTP_TCB_UNLOCK(stcb);
2128 *optsize = sizeof(struct sctp_paddrinfo);
2131 case SCTP_PCB_STATUS:
2133 struct sctp_pcbinfo *spcb;
2135 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2136 sctp_fill_pcbinfo(spcb);
2137 *optsize = sizeof(struct sctp_pcbinfo);
2143 struct sctp_nets *net;
2144 struct sctp_status *sstat;
2146 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2147 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2154 * I think passing the state is fine since
2155 * sctp_constants.h will be available to the user
2158 sstat->sstat_state = stcb->asoc.state;
2159 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2160 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2162 * We can't include chunks that have been passed to
2163 * the socket layer. Only things in queue.
2165 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2166 stcb->asoc.cnt_on_all_streams);
2169 sstat->sstat_instrms = stcb->asoc.streamincnt;
2170 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2171 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2172 memcpy(&sstat->sstat_primary.spinfo_address,
2173 &stcb->asoc.primary_destination->ro._l_addr,
2174 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2175 net = stcb->asoc.primary_destination;
2176 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2178 * Again the user can get info from sctp_constants.h
2179 * for what the state of the network is.
2181 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2182 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2183 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2184 sstat->sstat_primary.spinfo_rto = net->RTO;
2185 sstat->sstat_primary.spinfo_mtu = net->mtu;
2186 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2187 SCTP_TCB_UNLOCK(stcb);
2188 *optsize = sizeof(*sstat);
2193 struct sctp_rtoinfo *srto;
2195 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2196 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2199 srto->srto_initial = stcb->asoc.initial_rto;
2200 srto->srto_max = stcb->asoc.maxrto;
2201 srto->srto_min = stcb->asoc.minrto;
2202 SCTP_TCB_UNLOCK(stcb);
2204 SCTP_INP_RLOCK(inp);
2205 srto->srto_initial = inp->sctp_ep.initial_rto;
2206 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2207 srto->srto_min = inp->sctp_ep.sctp_minrto;
2208 SCTP_INP_RUNLOCK(inp);
2210 *optsize = sizeof(*srto);
2213 case SCTP_ASSOCINFO:
2215 struct sctp_assocparams *sasoc;
2218 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2219 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2222 oldval = sasoc->sasoc_cookie_life;
2223 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2224 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2225 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2226 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2227 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2228 SCTP_TCB_UNLOCK(stcb);
2230 SCTP_INP_RLOCK(inp);
2231 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2232 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2233 sasoc->sasoc_number_peer_destinations = 0;
2234 sasoc->sasoc_peer_rwnd = 0;
2235 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2236 SCTP_INP_RUNLOCK(inp);
2238 *optsize = sizeof(*sasoc);
2241 case SCTP_DEFAULT_SEND_PARAM:
2243 struct sctp_sndrcvinfo *s_info;
2245 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2246 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2249 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2250 SCTP_TCB_UNLOCK(stcb);
2252 SCTP_INP_RLOCK(inp);
2253 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2254 SCTP_INP_RUNLOCK(inp);
2256 *optsize = sizeof(*s_info);
2261 struct sctp_initmsg *sinit;
2263 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2264 SCTP_INP_RLOCK(inp);
2265 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2266 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2267 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2268 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2269 SCTP_INP_RUNLOCK(inp);
2270 *optsize = sizeof(*sinit);
2273 case SCTP_PRIMARY_ADDR:
2274 /* we allow a "get" operation on this */
2276 struct sctp_setprim *ssp;
2278 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2279 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2282 /* simply copy out the sockaddr_storage... */
2286 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2287 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2289 memcpy(&ssp->ssp_addr,
2290 &stcb->asoc.primary_destination->ro._l_addr,
2292 SCTP_TCB_UNLOCK(stcb);
2296 *optsize = sizeof(*ssp);
2300 case SCTP_HMAC_IDENT:
2302 struct sctp_hmacalgo *shmac;
2303 sctp_hmaclist_t *hmaclist;
2307 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2309 SCTP_INP_RLOCK(inp);
2310 hmaclist = inp->sctp_ep.local_hmacs;
2311 if (hmaclist == NULL) {
2312 /* no HMACs to return */
2313 *optsize = sizeof(*shmac);
2314 SCTP_INP_RUNLOCK(inp);
2317 /* is there room for all of the hmac ids? */
2318 size = sizeof(*shmac) + (hmaclist->num_algo *
2319 sizeof(shmac->shmac_idents[0]));
2320 if ((size_t)(*optsize) < size) {
2322 SCTP_INP_RUNLOCK(inp);
2325 /* copy in the list */
2326 for (i = 0; i < hmaclist->num_algo; i++)
2327 shmac->shmac_idents[i] = hmaclist->hmac[i];
2328 SCTP_INP_RUNLOCK(inp);
2332 case SCTP_AUTH_ACTIVE_KEY:
2334 struct sctp_authkeyid *scact;
2336 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2337 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2340 /* get the active key on the assoc */
2341 scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2342 SCTP_TCB_UNLOCK(stcb);
2344 /* get the endpoint active key */
2345 SCTP_INP_RLOCK(inp);
2346 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2347 SCTP_INP_RUNLOCK(inp);
2349 *optsize = sizeof(*scact);
2352 case SCTP_LOCAL_AUTH_CHUNKS:
2354 struct sctp_authchunks *sac;
2355 sctp_auth_chklist_t *chklist = NULL;
2358 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2359 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2362 /* get off the assoc */
2363 chklist = stcb->asoc.local_auth_chunks;
2364 /* is there enough space? */
2365 size = sctp_auth_get_chklist_size(chklist);
2366 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2369 /* copy in the chunks */
2370 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2372 SCTP_TCB_UNLOCK(stcb);
2374 /* get off the endpoint */
2375 SCTP_INP_RLOCK(inp);
2376 chklist = inp->sctp_ep.local_auth_chunks;
2377 /* is there enough space? */
2378 size = sctp_auth_get_chklist_size(chklist);
2379 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2382 /* copy in the chunks */
2383 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2385 SCTP_INP_RUNLOCK(inp);
2387 *optsize = sizeof(struct sctp_authchunks) + size;
2390 case SCTP_PEER_AUTH_CHUNKS:
2392 struct sctp_authchunks *sac;
2393 sctp_auth_chklist_t *chklist = NULL;
2396 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2397 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2400 /* get off the assoc */
2401 chklist = stcb->asoc.peer_auth_chunks;
2402 /* is there enough space? */
2403 size = sctp_auth_get_chklist_size(chklist);
2404 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2407 /* copy in the chunks */
2408 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2410 SCTP_TCB_UNLOCK(stcb);
2414 *optsize = sizeof(struct sctp_authchunks) + size;
2420 error = ENOPROTOOPT;
2423 } /* end switch (sopt->sopt_name) */
2428 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2433 struct sctp_tcb *stcb = NULL;
2434 struct sctp_inpcb *inp;
2437 if (optval == NULL) {
2438 SCTP_PRINTF("optval is NULL\n");
2441 inp = (struct sctp_inpcb *)so->so_pcb;
2443 SCTP_PRINTF("inp is NULL?\n");
2446 vrf_id = inp->def_vrf_id;
2451 case SCTP_AUTOCLOSE:
2452 case SCTP_AUTO_ASCONF:
2453 case SCTP_EXPLICIT_EOR:
2454 case SCTP_DISABLE_FRAGMENTS:
2455 case SCTP_USE_EXT_RCVINFO:
2456 case SCTP_I_WANT_MAPPED_V4_ADDR:
2457 /* copy in the option value */
2458 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2463 case SCTP_DISABLE_FRAGMENTS:
2464 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2466 case SCTP_AUTO_ASCONF:
2467 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2469 case SCTP_EXPLICIT_EOR:
2470 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2472 case SCTP_USE_EXT_RCVINFO:
2473 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2475 case SCTP_I_WANT_MAPPED_V4_ADDR:
2476 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2477 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2483 set_opt = SCTP_PCB_FLAGS_NODELAY;
2485 case SCTP_AUTOCLOSE:
2486 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2487 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2490 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2492 * The value is in ticks. Note this does not effect
2493 * old associations, only new ones.
2495 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2498 SCTP_INP_WLOCK(inp);
2500 sctp_feature_on(inp, set_opt);
2502 sctp_feature_off(inp, set_opt);
2504 SCTP_INP_WUNLOCK(inp);
2506 case SCTP_PARTIAL_DELIVERY_POINT:
2510 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2511 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2515 inp->partial_delivery_point = *value;
2518 case SCTP_FRAGMENT_INTERLEAVE:
2519 /* not yet until we re-write sctp_recvmsg() */
2523 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2524 if (*level == SCTP_FRAG_LEVEL_2) {
2525 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2526 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2527 } else if (*level == SCTP_FRAG_LEVEL_1) {
2528 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2529 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2530 } else if (*level == SCTP_FRAG_LEVEL_0) {
2531 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2532 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2539 case SCTP_CMT_ON_OFF:
2541 struct sctp_assoc_value *av;
2543 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2544 if (sctp_cmt_on_off) {
2545 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2547 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2548 SCTP_TCB_UNLOCK(stcb);
2553 error = ENOPROTOOPT;
2557 /* JRS - Set socket option for pluggable congestion control */
2558 case SCTP_PLUGGABLE_CC:
2560 struct sctp_assoc_value *av;
2562 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2563 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2565 switch (av->assoc_value) {
2567 * JRS - Standard TCP congestion
2570 case SCTP_CC_RFC2581:
2572 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2573 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2574 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2575 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2576 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2577 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2578 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2579 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2580 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2581 SCTP_TCB_UNLOCK(stcb);
2585 * JRS - High Speed TCP congestion
2590 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2591 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2592 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2593 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2594 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2595 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2596 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2597 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2598 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2599 SCTP_TCB_UNLOCK(stcb);
2602 /* JRS - HTCP congestion control */
2605 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2606 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2607 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2608 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2609 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2610 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2611 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2612 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2613 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2614 SCTP_TCB_UNLOCK(stcb);
2618 * JRS - All other values are
2624 SCTP_TCB_UNLOCK(stcb);
2629 switch (av->assoc_value) {
2630 case SCTP_CC_RFC2581:
2633 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2642 case SCTP_CLR_STAT_LOG:
2647 struct sctp_assoc_value *av;
2649 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2650 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2653 stcb->asoc.context = av->assoc_value;
2654 SCTP_TCB_UNLOCK(stcb);
2656 SCTP_INP_WLOCK(inp);
2657 inp->sctp_context = av->assoc_value;
2658 SCTP_INP_WUNLOCK(inp);
2664 uint32_t *default_vrfid;
2666 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2667 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2671 inp->def_vrf_id = *default_vrfid;
2674 case SCTP_DEL_VRF_ID:
2679 case SCTP_ADD_VRF_ID:
2684 case SCTP_DELAYED_SACK:
2686 struct sctp_sack_info *sack;
2688 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2689 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2690 if (sack->sack_delay) {
2691 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2692 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2695 if (sack->sack_delay) {
2696 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2697 sack->sack_delay = TICKS_TO_MSEC(1);
2699 stcb->asoc.delayed_ack = sack->sack_delay;
2701 if (sack->sack_freq) {
2702 stcb->asoc.sack_freq = sack->sack_freq;
2704 SCTP_TCB_UNLOCK(stcb);
2706 SCTP_INP_WLOCK(inp);
2707 if (sack->sack_delay) {
2708 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2709 sack->sack_delay = TICKS_TO_MSEC(1);
2711 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2713 if (sack->sack_freq) {
2714 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2716 SCTP_INP_WUNLOCK(inp);
2720 case SCTP_AUTH_CHUNK:
2722 struct sctp_authchunk *sauth;
2724 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2726 SCTP_INP_WLOCK(inp);
2727 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2729 SCTP_INP_WUNLOCK(inp);
2734 struct sctp_authkey *sca;
2735 struct sctp_keyhead *shared_keys;
2736 sctp_sharedkey_t *shared_key;
2737 sctp_key_t *key = NULL;
2740 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2741 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2742 size = optsize - sizeof(*sca);
2745 /* set it on the assoc */
2746 shared_keys = &stcb->asoc.shared_keys;
2747 /* clear the cached keys for this key id */
2748 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2750 * create the new shared key and
2754 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2757 SCTP_TCB_UNLOCK(stcb);
2761 shared_key = sctp_alloc_sharedkey();
2762 if (shared_key == NULL) {
2765 SCTP_TCB_UNLOCK(stcb);
2768 shared_key->key = key;
2769 shared_key->keyid = sca->sca_keynumber;
2770 sctp_insert_sharedkey(shared_keys, shared_key);
2771 SCTP_TCB_UNLOCK(stcb);
2773 /* set it on the endpoint */
2774 SCTP_INP_WLOCK(inp);
2775 shared_keys = &inp->sctp_ep.shared_keys;
2777 * clear the cached keys on all assocs for
2780 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2782 * create the new shared key and
2786 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2789 SCTP_INP_WUNLOCK(inp);
2793 shared_key = sctp_alloc_sharedkey();
2794 if (shared_key == NULL) {
2797 SCTP_INP_WUNLOCK(inp);
2800 shared_key->key = key;
2801 shared_key->keyid = sca->sca_keynumber;
2802 sctp_insert_sharedkey(shared_keys, shared_key);
2803 SCTP_INP_WUNLOCK(inp);
2807 case SCTP_HMAC_IDENT:
2809 struct sctp_hmacalgo *shmac;
2810 sctp_hmaclist_t *hmaclist;
2812 size_t size, i, found;
2814 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2815 size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2816 hmaclist = sctp_alloc_hmaclist(size);
2817 if (hmaclist == NULL) {
2821 for (i = 0; i < size; i++) {
2822 hmacid = shmac->shmac_idents[i];
2823 if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2824 /* invalid HMACs were found */ ;
2826 sctp_free_hmaclist(hmaclist);
2827 goto sctp_set_hmac_done;
2831 for (i = 0; i < hmaclist->num_algo; i++) {
2832 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
2833 /* already in list */
2838 sctp_free_hmaclist(hmaclist);
2842 /* set it on the endpoint */
2843 SCTP_INP_WLOCK(inp);
2844 if (inp->sctp_ep.local_hmacs)
2845 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2846 inp->sctp_ep.local_hmacs = hmaclist;
2847 SCTP_INP_WUNLOCK(inp);
2851 case SCTP_AUTH_ACTIVE_KEY:
2853 struct sctp_authkeyid *scact;
2855 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2856 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2858 /* set the active key on the right place */
2860 /* set the active key on the assoc */
2861 if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2863 SCTP_TCB_UNLOCK(stcb);
2865 /* set the active key on the endpoint */
2866 SCTP_INP_WLOCK(inp);
2867 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2869 SCTP_INP_WUNLOCK(inp);
2873 case SCTP_AUTH_DELETE_KEY:
2875 struct sctp_authkeyid *scdel;
2877 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2878 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2880 /* delete the key from the right place */
2882 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2884 SCTP_TCB_UNLOCK(stcb);
2886 SCTP_INP_WLOCK(inp);
2887 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2889 SCTP_INP_WUNLOCK(inp);
2894 case SCTP_RESET_STREAMS:
2896 struct sctp_stream_reset *strrst;
2897 uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2900 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2901 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2907 if (stcb->asoc.peer_supports_strreset == 0) {
2909 * Peer does not support it, we return
2910 * protocol not supported since this is true
2911 * for this feature and this peer, not the
2912 * socket request in general.
2914 error = EPROTONOSUPPORT;
2915 SCTP_TCB_UNLOCK(stcb);
2918 if (stcb->asoc.stream_reset_outstanding) {
2920 SCTP_TCB_UNLOCK(stcb);
2923 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2925 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2927 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2930 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2934 SCTP_TCB_UNLOCK(stcb);
2937 for (i = 0; i < strrst->strrst_num_streams; i++) {
2940 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2945 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2952 SCTP_TCB_UNLOCK(stcb);
2955 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2956 strrst->strrst_list,
2957 send_out, (stcb->asoc.str_reset_seq_in - 3),
2960 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2961 SCTP_TCB_UNLOCK(stcb);
2965 case SCTP_CONNECT_X:
2966 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2970 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2973 case SCTP_CONNECT_X_DELAYED:
2974 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2978 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2981 case SCTP_CONNECT_X_COMPLETE:
2983 struct sockaddr *sa;
2984 struct sctp_nets *net;
2986 /* FIXME MT: check correct? */
2987 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2990 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2991 SCTP_INP_RLOCK(inp);
2992 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2994 SCTP_TCB_LOCK(stcb);
2995 net = sctp_findnet(stcb, sa);
2997 SCTP_INP_RUNLOCK(inp);
3000 * We increment here since
3001 * sctp_findassociation_ep_addr() wil do a
3002 * decrement if it finds the stcb as long as
3003 * the locked tcb (last argument) is NOT a
3006 SCTP_INP_INCR_REF(inp);
3007 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3009 SCTP_INP_DECR_REF(inp);
3017 if (stcb->asoc.delayed_connection == 1) {
3018 stcb->asoc.delayed_connection = 0;
3019 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3020 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3021 stcb->asoc.primary_destination,
3022 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3023 sctp_send_initiate(inp, stcb);
3026 * already expired or did not use delayed
3031 SCTP_TCB_UNLOCK(stcb);
3034 case SCTP_MAX_BURST:
3038 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3040 SCTP_INP_WLOCK(inp);
3042 inp->sctp_ep.max_burst = *burst;
3044 SCTP_INP_WUNLOCK(inp);
3049 struct sctp_assoc_value *av;
3052 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3053 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3055 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3056 ovh = SCTP_MED_OVERHEAD;
3058 ovh = SCTP_MED_V4_OVERHEAD;
3061 if (av->assoc_value) {
3062 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3064 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3066 SCTP_TCB_UNLOCK(stcb);
3068 SCTP_INP_WLOCK(inp);
3070 * FIXME MT: I think this is not in tune
3073 if (av->assoc_value) {
3074 inp->sctp_frag_point = (av->assoc_value + ovh);
3076 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3078 SCTP_INP_WUNLOCK(inp);
3084 struct sctp_event_subscribe *events;
3086 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3088 SCTP_INP_WLOCK(inp);
3089 if (events->sctp_data_io_event) {
3090 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3092 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3095 if (events->sctp_association_event) {
3096 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3098 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3101 if (events->sctp_address_event) {
3102 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3104 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3107 if (events->sctp_send_failure_event) {
3108 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3110 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3113 if (events->sctp_peer_error_event) {
3114 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3116 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3119 if (events->sctp_shutdown_event) {
3120 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3122 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3125 if (events->sctp_partial_delivery_event) {
3126 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3128 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3131 if (events->sctp_adaptation_layer_event) {
3132 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3134 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3137 if (events->sctp_authentication_event) {
3138 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3140 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3143 if (events->sctp_stream_reset_events) {
3144 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3146 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3148 SCTP_INP_WUNLOCK(inp);
3152 case SCTP_ADAPTATION_LAYER:
3154 struct sctp_setadaptation *adap_bits;
3156 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3157 SCTP_INP_WLOCK(inp);
3158 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3159 SCTP_INP_WUNLOCK(inp);
3163 case SCTP_SET_INITIAL_DBG_SEQ:
3167 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3168 SCTP_INP_WLOCK(inp);
3169 inp->sctp_ep.initial_sequence_debug = *vvv;
3170 SCTP_INP_WUNLOCK(inp);
3174 case SCTP_DEFAULT_SEND_PARAM:
3176 struct sctp_sndrcvinfo *s_info;
3178 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3179 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3182 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3183 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3187 SCTP_TCB_UNLOCK(stcb);
3189 SCTP_INP_WLOCK(inp);
3190 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3191 SCTP_INP_WUNLOCK(inp);
3195 case SCTP_PEER_ADDR_PARAMS:
3196 /* Applys to the specific association */
3198 struct sctp_paddrparams *paddrp;
3199 struct sctp_nets *net;
3201 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3202 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3205 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3208 * We increment here since
3209 * sctp_findassociation_ep_addr() wil do a
3210 * decrement if it finds the stcb as long as
3211 * the locked tcb (last argument) is NOT a
3214 SCTP_INP_INCR_REF(inp);
3215 stcb = sctp_findassociation_ep_addr(&inp,
3216 (struct sockaddr *)&paddrp->spp_address,
3219 SCTP_INP_DECR_REF(inp);
3223 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3225 SCTP_TCB_UNLOCK(stcb);
3228 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3230 SCTP_TCB_UNLOCK(stcb);
3234 /************************TCB SPECIFIC SET ******************/
3236 * do we change the timer for HB, we run
3241 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3242 ovh = SCTP_MED_OVERHEAD;
3244 ovh = SCTP_MED_V4_OVERHEAD;
3247 if (paddrp->spp_hbinterval)
3248 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3249 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3250 stcb->asoc.heart_beat_delay = 0;
3252 /* network sets ? */
3254 /************************NET SPECIFIC SET ******************/
3255 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3257 if (sctp_send_hb(stcb, 1, net) < 0) {
3258 /* asoc destroyed */
3263 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3264 net->dest_state |= SCTP_ADDR_NOHB;
3266 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3267 net->dest_state &= ~SCTP_ADDR_NOHB;
3269 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3270 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3271 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3272 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3274 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3275 net->mtu = paddrp->spp_pathmtu + ovh;
3276 if (net->mtu < stcb->asoc.smallest_mtu) {
3277 #ifdef SCTP_PRINT_FOR_B_AND_M
3278 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3281 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3285 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3286 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3287 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3290 if (paddrp->spp_pathmaxrxt)
3291 net->failure_threshold = paddrp->spp_pathmaxrxt;
3293 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3294 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3295 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3300 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3301 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3302 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3307 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3308 if (paddrp->spp_pathmaxrxt)
3309 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3311 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3312 /* Turn back on the timer */
3313 stcb->asoc.hb_is_disabled = 0;
3314 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3316 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3317 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3318 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3319 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3320 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3322 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3323 net->mtu = paddrp->spp_pathmtu + ovh;
3324 if (net->mtu < stcb->asoc.smallest_mtu) {
3325 #ifdef SCTP_PRINT_FOR_B_AND_M
3326 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3329 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3334 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3335 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3336 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3337 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3341 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3342 int cnt_of_unconf = 0;
3343 struct sctp_nets *lnet;
3345 stcb->asoc.hb_is_disabled = 1;
3346 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3347 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3352 * stop the timer ONLY if we
3353 * have no unconfirmed
3356 if (cnt_of_unconf == 0) {
3357 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3358 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3359 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3363 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3364 /* start up the timer. */
3365 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3366 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3370 if (paddrp->spp_flags & SPP_IPV4_TOS)
3371 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3374 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3375 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3379 SCTP_TCB_UNLOCK(stcb);
3381 /************************NO TCB, SET TO default stuff ******************/
3382 SCTP_INP_WLOCK(inp);
3384 * For the TOS/FLOWLABEL stuff you set it
3385 * with the options on the socket
3387 if (paddrp->spp_pathmaxrxt) {
3388 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3390 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3391 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3392 else if (paddrp->spp_hbinterval) {
3393 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3394 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3395 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3397 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3398 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3400 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3401 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3403 SCTP_INP_WUNLOCK(inp);
3409 struct sctp_rtoinfo *srto;
3410 uint32_t new_init, new_min, new_max;
3412 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3413 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3416 if (srto->srto_initial)
3417 new_init = srto->srto_initial;
3419 new_init = stcb->asoc.initial_rto;
3421 new_max = srto->srto_max;
3423 new_max = stcb->asoc.maxrto;
3425 new_min = srto->srto_min;
3427 new_min = stcb->asoc.minrto;
3428 if ((new_min <= new_init) && (new_init <= new_max)) {
3429 stcb->asoc.initial_rto = new_init;
3430 stcb->asoc.maxrto = new_max;
3431 stcb->asoc.minrto = new_min;
3435 SCTP_TCB_UNLOCK(stcb);
3437 SCTP_INP_WLOCK(inp);
3438 if (srto->srto_initial)
3439 new_init = srto->srto_initial;
3441 new_init = inp->sctp_ep.initial_rto;
3443 new_max = srto->srto_max;
3445 new_max = inp->sctp_ep.sctp_maxrto;
3447 new_min = srto->srto_min;
3449 new_min = inp->sctp_ep.sctp_minrto;
3450 if ((new_min <= new_init) && (new_init <= new_max)) {
3451 inp->sctp_ep.initial_rto = new_init;
3452 inp->sctp_ep.sctp_maxrto = new_max;
3453 inp->sctp_ep.sctp_minrto = new_min;
3457 SCTP_INP_WUNLOCK(inp);
3461 case SCTP_ASSOCINFO:
3463 struct sctp_assocparams *sasoc;
3465 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3466 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3467 if (sasoc->sasoc_cookie_life) {
3468 /* boundary check the cookie life */
3469 if (sasoc->sasoc_cookie_life < 1000)
3470 sasoc->sasoc_cookie_life = 1000;
3471 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3472 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3476 if (sasoc->sasoc_asocmaxrxt)
3477 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3478 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3479 sasoc->sasoc_peer_rwnd = 0;
3480 sasoc->sasoc_local_rwnd = 0;
3481 if (sasoc->sasoc_cookie_life) {
3482 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3485 SCTP_TCB_UNLOCK(stcb);
3487 SCTP_INP_WLOCK(inp);
3488 if (sasoc->sasoc_asocmaxrxt)
3489 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3490 sasoc->sasoc_number_peer_destinations = 0;
3491 sasoc->sasoc_peer_rwnd = 0;
3492 sasoc->sasoc_local_rwnd = 0;
3493 if (sasoc->sasoc_cookie_life) {
3494 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3496 SCTP_INP_WUNLOCK(inp);
3502 struct sctp_initmsg *sinit;
3504 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3505 SCTP_INP_WLOCK(inp);
3506 if (sinit->sinit_num_ostreams)
3507 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3509 if (sinit->sinit_max_instreams)
3510 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3512 if (sinit->sinit_max_attempts)
3513 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3515 if (sinit->sinit_max_init_timeo)
3516 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3517 SCTP_INP_WUNLOCK(inp);
3520 case SCTP_PRIMARY_ADDR:
3522 struct sctp_setprim *spa;
3523 struct sctp_nets *net, *lnet;
3525 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3526 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3530 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3533 * We increment here since
3534 * sctp_findassociation_ep_addr() wil do a
3535 * decrement if it finds the stcb as long as
3536 * the locked tcb (last argument) is NOT a
3539 SCTP_INP_INCR_REF(inp);
3540 stcb = sctp_findassociation_ep_addr(&inp,
3541 (struct sockaddr *)&spa->ssp_addr,
3544 SCTP_INP_DECR_REF(inp);
3548 if ((stcb) && (net)) {
3549 if ((net != stcb->asoc.primary_destination) &&
3550 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3551 /* Ok we need to set it */
3552 lnet = stcb->asoc.primary_destination;
3553 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3554 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3555 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3557 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3564 SCTP_TCB_UNLOCK(stcb);
3568 case SCTP_SET_DYNAMIC_PRIMARY:
3570 union sctp_sockstore *ss;
3572 error = priv_check(curthread,
3573 PRIV_NETINET_RESERVEDPORT);
3577 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3578 /* SUPER USER CHECK? */
3579 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3582 case SCTP_SET_PEER_PRIMARY_ADDR:
3584 struct sctp_setpeerprim *sspp;
3586 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3587 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3589 struct sctp_ifa *ifa;
3591 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3592 stcb->asoc.vrf_id, 0);
3597 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3599 * Must validate the ifa found is in
3602 struct sctp_laddr *laddr;
3605 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3606 if (laddr->ifa == NULL) {
3607 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3611 if (laddr->ifa == ifa) {
3621 if (sctp_set_primary_ip_address_sa(stcb,
3622 (struct sockaddr *)&sspp->sspp_addr) != 0) {
3626 SCTP_TCB_UNLOCK(stcb);
3633 case SCTP_BINDX_ADD_ADDR:
3635 struct sctp_getaddresses *addrs;
3640 td = (struct thread *)p;
3641 if (jailed(td->td_ucred)) {
3644 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3646 if (addrs->addr->sa_family == AF_INET) {
3647 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3652 if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3653 error = EADDRNOTAVAIL;
3655 } else if (addrs->addr->sa_family == AF_INET6) {
3656 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3661 /* JAIL XXXX Add else here for V6 */
3663 sctp_bindx_add_address(so, inp, addrs->addr,
3664 addrs->sget_assoc_id, vrf_id,
3668 case SCTP_BINDX_REM_ADDR:
3670 struct sctp_getaddresses *addrs;
3675 td = (struct thread *)p;
3676 if (jailed(td->td_ucred)) {
3679 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3680 if (addrs->addr->sa_family == AF_INET) {
3681 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3686 if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3687 error = EADDRNOTAVAIL;
3689 } else if (addrs->addr->sa_family == AF_INET6) {
3690 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3695 /* JAIL XXXX Add else here for V6 */
3697 sctp_bindx_delete_address(so, inp, addrs->addr,
3698 addrs->sget_assoc_id, vrf_id,
3703 error = ENOPROTOOPT;
3705 } /* end switch (opt) */
3711 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3713 void *optval = NULL;
3715 struct sctp_inpcb *inp;
3719 inp = (struct sctp_inpcb *)so->so_pcb;
3721 /* I made the same as TCP since we are not setup? */
3722 return (ECONNRESET);
3724 if (sopt->sopt_level != IPPROTO_SCTP) {
3725 /* wrong proto level... send back up to IP */
3727 if (INP_CHECK_SOCKAF(so, AF_INET6))
3728 error = ip6_ctloutput(so, sopt);
3731 error = ip_ctloutput(so, sopt);
3734 optsize = sopt->sopt_valsize;
3736 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
3737 if (optval == NULL) {
3740 error = sooptcopyin(sopt, optval, optsize, optsize);
3742 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3746 p = (void *)sopt->sopt_td;
3747 if (sopt->sopt_dir == SOPT_SET) {
3748 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3749 } else if (sopt->sopt_dir == SOPT_GET) {
3750 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3754 if ((error == 0) && (optval != NULL)) {
3755 error = sooptcopyout(sopt, optval, optsize);
3756 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3757 } else if (optval != NULL) {
3758 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3766 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3769 int create_lock_on = 0;
3771 struct sctp_inpcb *inp;
3772 struct sctp_tcb *stcb = NULL;
3774 inp = (struct sctp_inpcb *)so->so_pcb;
3776 /* I made the same as TCP since we are not setup? */
3777 return (ECONNRESET);
3782 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
3785 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
3788 SCTP_ASOC_CREATE_LOCK(inp);
3791 SCTP_INP_INCR_REF(inp);
3792 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3793 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3794 /* Should I really unlock ? */
3799 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3800 (addr->sa_family == AF_INET6)) {
3805 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3806 SCTP_PCB_FLAGS_UNBOUND) {
3807 /* Bind a ephemeral port */
3808 error = sctp_inpcb_bind(so, NULL, NULL, p);
3813 /* Now do we connect? */
3814 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3818 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3819 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3820 /* We are already connected AND the TCP model */
3824 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3825 SCTP_INP_RLOCK(inp);
3826 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3827 SCTP_INP_RUNLOCK(inp);
3830 * We increment here since sctp_findassociation_ep_addr()
3831 * wil do a decrement if it finds the stcb as long as the
3832 * locked tcb (last argument) is NOT a TCB.. aka NULL.
3834 SCTP_INP_INCR_REF(inp);
3835 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3837 SCTP_INP_DECR_REF(inp);
3839 SCTP_TCB_LOCK(stcb);
3843 /* Already have or am bring up an association */
3847 vrf_id = inp->def_vrf_id;
3848 /* We are GOOD to go */
3849 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
3851 /* Gak! no memory */
3854 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3855 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3856 /* Set the connected flag so we can queue data */
3859 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3860 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3862 /* initialize authentication parameters for the assoc */
3863 sctp_initialize_auth_params(inp, stcb);
3865 sctp_send_initiate(inp, stcb);
3866 SCTP_TCB_UNLOCK(stcb);
3868 if (create_lock_on) {
3869 SCTP_ASOC_CREATE_UNLOCK(inp);
3871 SCTP_INP_DECR_REF(inp);
3876 sctp_listen(struct socket *so, int backlog, struct thread *p)
3879 * Note this module depends on the protocol processing being called
3880 * AFTER any socket level flags and backlog are applied to the
3881 * socket. The traditional way that the socket flags are applied is
3882 * AFTER protocol processing. We have made a change to the
3883 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3884 * place if the socket API for SCTP is to work properly.
3888 struct sctp_inpcb *inp;
3890 inp = (struct sctp_inpcb *)so->so_pcb;
3892 /* I made the same as TCP since we are not setup? */
3893 return (ECONNRESET);
3895 SCTP_INP_RLOCK(inp);
3896 #ifdef SCTP_LOCK_LOGGING
3897 if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
3898 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3902 error = solisten_proto_check(so);
3905 SCTP_INP_RUNLOCK(inp);
3908 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3909 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3910 /* We are already connected AND the TCP model */
3911 SCTP_INP_RUNLOCK(inp);
3913 return (EADDRINUSE);
3915 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3916 /* We must do a bind. */
3918 SCTP_INP_RUNLOCK(inp);
3919 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
3920 /* bind error, probably perm */
3925 SCTP_INP_RUNLOCK(inp);
3927 /* It appears for 7.0 and on, we must always call this. */
3928 solisten_proto(so, backlog);
3929 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3930 /* remove the ACCEPTCONN flag for one-to-many sockets */
3931 so->so_options &= ~SO_ACCEPTCONN;
3934 /* turning off listen */
3935 so->so_options &= ~SO_ACCEPTCONN;
3941 static int sctp_defered_wakeup_cnt = 0;
3944 sctp_accept(struct socket *so, struct sockaddr **addr)
3946 struct sctp_tcb *stcb;
3947 struct sctp_inpcb *inp;
3948 union sctp_sockstore store;
3952 inp = (struct sctp_inpcb *)so->so_pcb;
3955 return (ECONNRESET);
3957 SCTP_INP_RLOCK(inp);
3958 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3959 SCTP_INP_RUNLOCK(inp);
3962 if (so->so_state & SS_ISDISCONNECTED) {
3963 SCTP_INP_RUNLOCK(inp);
3964 return (ECONNABORTED);
3966 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3968 SCTP_INP_RUNLOCK(inp);
3969 return (ECONNRESET);
3971 SCTP_TCB_LOCK(stcb);
3972 SCTP_INP_RUNLOCK(inp);
3973 store = stcb->asoc.primary_destination->ro._l_addr;
3974 SCTP_TCB_UNLOCK(stcb);
3975 if (store.sa.sa_family == AF_INET) {
3976 struct sockaddr_in *sin;
3978 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3979 sin->sin_family = AF_INET;
3980 sin->sin_len = sizeof(*sin);
3981 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3982 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3983 *addr = (struct sockaddr *)sin;
3985 struct sockaddr_in6 *sin6;
3987 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3988 sin6->sin6_family = AF_INET6;
3989 sin6->sin6_len = sizeof(*sin6);
3990 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3992 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3993 if ((error = sa6_recoverscope(sin6)) != 0) {
3994 SCTP_FREE_SONAME(sin6);
3997 *addr = (struct sockaddr *)sin6;
3999 /* Wake any delayed sleep action */
4000 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4001 SCTP_INP_WLOCK(inp);
4002 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4003 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4004 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4005 SCTP_INP_WUNLOCK(inp);
4006 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4007 if (sowriteable(inp->sctp_socket)) {
4008 sowwakeup_locked(inp->sctp_socket);
4010 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4012 SCTP_INP_WLOCK(inp);
4014 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4015 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4016 SCTP_INP_WUNLOCK(inp);
4017 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4018 if (soreadable(inp->sctp_socket)) {
4019 sctp_defered_wakeup_cnt++;
4020 sorwakeup_locked(inp->sctp_socket);
4022 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4024 SCTP_INP_WLOCK(inp);
4026 SCTP_INP_WUNLOCK(inp);
4032 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4034 struct sockaddr_in *sin;
4036 struct sctp_inpcb *inp;
4037 struct sctp_ifa *sctp_ifa;
4040 * Do the malloc first in case it blocks.
4042 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4043 sin->sin_family = AF_INET;
4044 sin->sin_len = sizeof(*sin);
4045 inp = (struct sctp_inpcb *)so->so_pcb;
4047 SCTP_FREE_SONAME(sin);
4050 SCTP_INP_RLOCK(inp);
4051 sin->sin_port = inp->sctp_lport;
4052 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4053 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4054 struct sctp_tcb *stcb;
4055 struct sockaddr_in *sin_a;
4056 struct sctp_nets *net;
4059 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4065 SCTP_TCB_LOCK(stcb);
4066 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4067 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4069 /* this will make coverity happy */
4072 if (sin_a->sin_family == AF_INET) {
4077 if ((!fnd) || (sin_a == NULL)) {
4079 SCTP_TCB_UNLOCK(stcb);
4082 vrf_id = inp->def_vrf_id;
4083 sctp_ifa = sctp_source_address_selection(inp,
4085 (sctp_route_t *) & net->ro,
4088 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4089 sctp_free_ifa(sctp_ifa);
4091 SCTP_TCB_UNLOCK(stcb);
4093 /* For the bound all case you get back 0 */
4095 sin->sin_addr.s_addr = 0;
4099 /* Take the first IPv4 address in the list */
4100 struct sctp_laddr *laddr;
4103 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4104 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4105 struct sockaddr_in *sin_a;
4107 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4108 sin->sin_addr = sin_a->sin_addr;
4114 SCTP_FREE_SONAME(sin);
4115 SCTP_INP_RUNLOCK(inp);
4119 SCTP_INP_RUNLOCK(inp);
4120 (*addr) = (struct sockaddr *)sin;
4125 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4127 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4129 struct sockaddr_in *sin_a;
4130 struct sctp_inpcb *inp;
4131 struct sctp_tcb *stcb;
4132 struct sctp_nets *net;
4134 /* Do the malloc first in case it blocks. */
4135 inp = (struct sctp_inpcb *)so->so_pcb;
4136 if ((inp == NULL) ||
4137 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4138 /* UDP type and listeners will drop out here */
4141 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4142 sin->sin_family = AF_INET;
4143 sin->sin_len = sizeof(*sin);
4145 /* We must recapture incase we blocked */
4146 inp = (struct sctp_inpcb *)so->so_pcb;
4148 SCTP_FREE_SONAME(sin);
4151 SCTP_INP_RLOCK(inp);
4152 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4154 SCTP_TCB_LOCK(stcb);
4156 SCTP_INP_RUNLOCK(inp);
4158 SCTP_FREE_SONAME(sin);
4162 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4163 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4164 if (sin_a->sin_family == AF_INET) {
4166 sin->sin_port = stcb->rport;
4167 sin->sin_addr = sin_a->sin_addr;
4171 SCTP_TCB_UNLOCK(stcb);
4173 /* No IPv4 address */
4174 SCTP_FREE_SONAME(sin);
4177 (*addr) = (struct sockaddr *)sin;
4181 struct pr_usrreqs sctp_usrreqs = {
4182 .pru_abort = sctp_abort,
4183 .pru_accept = sctp_accept,
4184 .pru_attach = sctp_attach,
4185 .pru_bind = sctp_bind,
4186 .pru_connect = sctp_connect,
4187 .pru_control = in_control,
4188 .pru_close = sctp_close,
4189 .pru_detach = sctp_close,
4190 .pru_sopoll = sopoll_generic,
4191 .pru_disconnect = sctp_disconnect,
4192 .pru_listen = sctp_listen,
4193 .pru_peeraddr = sctp_peeraddr,
4194 .pru_send = sctp_sendm,
4195 .pru_shutdown = sctp_shutdown,
4196 .pru_sockaddr = sctp_ingetaddr,
4197 .pru_sosend = sctp_sosend,
4198 .pru_soreceive = sctp_soreceive