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(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_nextmtu);
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)
232 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
241 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
242 (sh == NULL) || (to == NULL)) {
244 SCTP_TCB_UNLOCK(stcb);
247 /* First job is to verify the vtag matches what I would send */
248 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
249 SCTP_TCB_UNLOCK(stcb);
252 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
254 if (icmph->icmp_type != ICMP_UNREACH) {
255 /* We only care about unreachable */
256 SCTP_TCB_UNLOCK(stcb);
259 if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
260 (icmph->icmp_code == ICMP_UNREACH_HOST) ||
261 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
262 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
263 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
264 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
265 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
266 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
269 * Hmm reachablity problems we must examine closely. If its
270 * not reachable, we may have lost a network. Or if there is
271 * NO protocol at the other end named SCTP. well we consider
274 if (net->dest_state & SCTP_ADDR_REACHABLE) {
275 /* Ok that destination is NOT reachable */
276 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
278 net->failure_threshold,
281 net->dest_state &= ~SCTP_ADDR_REACHABLE;
282 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
284 * JRS 5/14/07 - If a destination is unreachable,
285 * the PF bit is turned off. This allows an
286 * unambiguous use of the PF bit for destinations
287 * that are reachable but potentially failed. If the
288 * destination is set to the unreachable state, also
289 * set the destination to the PF state.
292 * Add debug message here if destination is not in
295 /* Stop any running T3 timers here? */
296 if (sctp_cmt_on_off && sctp_cmt_pf) {
297 net->dest_state &= ~SCTP_ADDR_PF;
298 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
301 net->error_count = net->failure_threshold + 1;
302 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
303 stcb, SCTP_FAILED_THRESHOLD,
304 (void *)net, SCTP_SO_NOT_LOCKED);
306 SCTP_TCB_UNLOCK(stcb);
307 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
308 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
310 * Here the peer is either playing tricks on us, including
311 * an address that belongs to someone who does not support
312 * SCTP OR was a userland implementation that shutdown and
313 * now is dead. In either case treat it like a OOTB abort
316 reason = SCTP_PEER_FAULTY;
317 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
318 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
319 so = SCTP_INP_SO(inp);
320 atomic_add_int(&stcb->asoc.refcnt, 1);
321 SCTP_TCB_UNLOCK(stcb);
322 SCTP_SOCKET_LOCK(so, 1);
324 atomic_subtract_int(&stcb->asoc.refcnt, 1);
326 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
327 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
328 SCTP_SOCKET_UNLOCK(so, 1);
329 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
331 /* no need to unlock here, since the TCB is gone */
333 SCTP_TCB_UNLOCK(stcb);
338 sctp_ctlinput(cmd, sa, vip)
347 /* FIX, for non-bsd is this right? */
348 vrf_id = SCTP_DEFAULT_VRFID;
349 if (sa->sa_family != AF_INET ||
350 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
353 if (PRC_IS_REDIRECT(cmd)) {
355 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
359 struct sctp_inpcb *inp = NULL;
360 struct sctp_tcb *stcb = NULL;
361 struct sctp_nets *net = NULL;
362 struct sockaddr_in to, from;
364 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
365 bzero(&to, sizeof(to));
366 bzero(&from, sizeof(from));
367 from.sin_family = to.sin_family = AF_INET;
368 from.sin_len = to.sin_len = sizeof(to);
369 from.sin_port = sh->src_port;
370 from.sin_addr = ip->ip_src;
371 to.sin_port = sh->dest_port;
372 to.sin_addr = ip->ip_dst;
375 * 'to' holds the dest of the packet that failed to be sent.
376 * 'from' holds our local endpoint address. Thus we reverse
377 * the to and the from in the lookup.
379 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
380 (struct sockaddr *)&to,
381 &inp, &net, 1, vrf_id);
382 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
383 if (cmd != PRC_MSGSIZE) {
384 sctp_notify(inp, ip, sh,
385 (struct sockaddr *)&to, stcb,
388 /* handle possible ICMP size messages */
389 sctp_notify_mbuf(inp, stcb, net, ip, sh);
392 if ((stcb == NULL) && (inp != NULL)) {
393 /* reduce ref-count */
395 SCTP_INP_DECR_REF(inp);
396 SCTP_INP_WUNLOCK(inp);
404 sctp_getcred(SYSCTL_HANDLER_ARGS)
407 struct sockaddr_in addrs[2];
408 struct sctp_inpcb *inp;
409 struct sctp_nets *net;
410 struct sctp_tcb *stcb;
414 /* FIX, for non-bsd is this right? */
415 vrf_id = SCTP_DEFAULT_VRFID;
417 error = priv_check(req->td, PRIV_NETINET_GETCRED);
422 error = SYSCTL_IN(req, addrs, sizeof(addrs));
426 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
428 &inp, &net, 1, vrf_id);
429 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
430 if ((inp != NULL) && (stcb == NULL)) {
431 /* reduce ref-count */
433 SCTP_INP_DECR_REF(inp);
436 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
440 SCTP_TCB_UNLOCK(stcb);
442 * We use the write lock here, only since in the error leg we need
443 * it. If we used RLOCK, then we would have to
444 * wlock/decr/unlock/rlock. Which in theory could create a hole.
445 * Better to use higher wlock.
449 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
451 SCTP_INP_WUNLOCK(inp);
454 cru2x(inp->sctp_socket->so_cred, &xuc);
455 SCTP_INP_WUNLOCK(inp);
456 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
461 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
462 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
466 sctp_abort(struct socket *so)
468 struct sctp_inpcb *inp;
471 inp = (struct sctp_inpcb *)so->so_pcb;
476 flags = inp->sctp_flags;
477 #ifdef SCTP_LOG_CLOSING
478 sctp_log_closing(inp, NULL, 17);
480 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
481 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
482 #ifdef SCTP_LOG_CLOSING
483 sctp_log_closing(inp, NULL, 16);
485 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
486 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
488 SCTP_SB_CLEAR(so->so_snd);
490 * same for the rcv ones, they are only here for the
493 SCTP_SB_CLEAR(so->so_rcv);
495 /* Now null out the reference, we are completely detached. */
499 flags = inp->sctp_flags;
500 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
501 goto sctp_must_try_again;
508 sctp_attach(struct socket *so, int proto, struct thread *p)
510 struct sctp_inpcb *inp;
511 struct inpcb *ip_inp;
513 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
519 inp = (struct sctp_inpcb *)so->so_pcb;
521 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
524 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
528 error = sctp_inpcb_alloc(so, vrf_id);
532 inp = (struct sctp_inpcb *)so->so_pcb;
534 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
535 ip_inp = &inp->ip_inp.inp;
536 ip_inp->inp_vflag |= INP_IPV4;
537 ip_inp->inp_ip_ttl = ip_defttl;
539 error = ipsec_init_policy(so, &ip_inp->inp_sp);
540 #ifdef SCTP_LOG_CLOSING
541 sctp_log_closing(inp, NULL, 17);
544 flags = inp->sctp_flags;
545 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
546 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
547 #ifdef SCTP_LOG_CLOSING
548 sctp_log_closing(inp, NULL, 15);
550 SCTP_INP_WUNLOCK(inp);
551 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
552 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
554 SCTP_INP_WUNLOCK(inp);
559 SCTP_INP_WUNLOCK(inp);
564 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
566 struct sctp_inpcb *inp = NULL;
570 if (addr && addr->sa_family != AF_INET) {
571 /* must be a v4 address! */
572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
576 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
577 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
580 inp = (struct sctp_inpcb *)so->so_pcb;
582 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
585 error = sctp_inpcb_bind(so, addr, NULL, p);
590 sctp_close(struct socket *so)
592 struct sctp_inpcb *inp;
595 inp = (struct sctp_inpcb *)so->so_pcb;
600 * Inform all the lower layer assoc that we are done.
603 flags = inp->sctp_flags;
604 #ifdef SCTP_LOG_CLOSING
605 sctp_log_closing(inp, NULL, 17);
607 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
608 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
609 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
610 (so->so_rcv.sb_cc > 0)) {
611 #ifdef SCTP_LOG_CLOSING
612 sctp_log_closing(inp, NULL, 13);
614 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
615 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
617 #ifdef SCTP_LOG_CLOSING
618 sctp_log_closing(inp, NULL, 14);
620 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
621 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
624 * The socket is now detached, no matter what the state of
625 * the SCTP association.
628 SCTP_SB_CLEAR(so->so_snd);
630 * same for the rcv ones, they are only here for the
633 SCTP_SB_CLEAR(so->so_rcv);
635 /* Now null out the reference, we are completely detached. */
639 flags = inp->sctp_flags;
640 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
641 goto sctp_must_try_again;
649 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
650 struct mbuf *control, struct thread *p);
654 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
655 struct mbuf *control, struct thread *p)
657 struct sctp_inpcb *inp;
660 inp = (struct sctp_inpcb *)so->so_pcb;
663 sctp_m_freem(control);
666 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
670 /* Got to have an to address if we are NOT a connected socket */
671 if ((addr == NULL) &&
672 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
673 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
676 } else if (addr == NULL) {
677 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
678 error = EDESTADDRREQ;
681 sctp_m_freem(control);
687 if (addr->sa_family != AF_INET) {
688 /* must be a v4 address! */
689 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
692 sctp_m_freem(control);
695 error = EDESTADDRREQ;
700 /* now what about control */
703 SCTP_PRINTF("huh? control set?\n");
704 sctp_m_freem(inp->control);
707 inp->control = control;
711 SCTP_BUF_NEXT(inp->pkt_last) = m;
714 inp->pkt_last = inp->pkt = m;
717 /* FreeBSD uses a flag passed */
718 ((flags & PRUS_MORETOCOME) == 0)
721 * note with the current version this code will only be used
722 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
723 * re-defining sosend to use the sctp_sosend. One can
724 * optionally switch back to this code (by changing back the
725 * definitions) but this is not advisable. This code is used
726 * by FreeBSD when sending a file with sendfile() though.
730 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
740 sctp_disconnect(struct socket *so)
742 struct sctp_inpcb *inp;
744 inp = (struct sctp_inpcb *)so->so_pcb;
746 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
750 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
751 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
752 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
754 SCTP_INP_RUNLOCK(inp);
757 struct sctp_association *asoc;
758 struct sctp_tcb *stcb;
760 stcb = LIST_FIRST(&inp->sctp_asoc_list);
762 SCTP_INP_RUNLOCK(inp);
763 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
768 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
769 /* We are about to be freed, out of here */
770 SCTP_TCB_UNLOCK(stcb);
771 SCTP_INP_RUNLOCK(inp);
774 if (((so->so_options & SO_LINGER) &&
775 (so->so_linger == 0)) ||
776 (so->so_rcv.sb_cc > 0)) {
777 if (SCTP_GET_STATE(asoc) !=
778 SCTP_STATE_COOKIE_WAIT) {
779 /* Left with Data unread */
782 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
788 struct sctp_paramhdr *ph;
790 ph = mtod(err, struct sctp_paramhdr *);
791 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
792 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
793 ph->param_length = htons(SCTP_BUF_LEN(err));
795 #if defined(SCTP_PANIC_ON_ABORT)
796 panic("disconnect does an abort");
798 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
799 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
801 SCTP_INP_RUNLOCK(inp);
802 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
803 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
804 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
806 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
807 /* No unlock tcb assoc is gone */
810 if (TAILQ_EMPTY(&asoc->send_queue) &&
811 TAILQ_EMPTY(&asoc->sent_queue) &&
812 (asoc->stream_queue_cnt == 0)) {
813 /* there is nothing queued to send, so done */
814 if (asoc->locked_on_sending) {
817 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
818 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
819 /* only send SHUTDOWN 1st time thru */
820 sctp_stop_timers_for_shutdown(stcb);
821 sctp_send_shutdown(stcb,
822 stcb->asoc.primary_destination);
823 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
824 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
825 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
826 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
828 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
829 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
830 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
832 asoc->primary_destination);
833 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
835 asoc->primary_destination);
839 * we still got (or just got) data to send,
840 * so set SHUTDOWN_PENDING
843 * XXX sockets draft says that SCTP_EOF
844 * should be sent with no data. currently,
845 * we will allow user data to be sent first
846 * and move to SHUTDOWN-PENDING
848 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
849 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
850 asoc->primary_destination);
851 if (asoc->locked_on_sending) {
852 /* Locked to send out the data */
853 struct sctp_stream_queue_pending *sp;
855 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
857 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
858 asoc->locked_on_sending->stream_no);
860 if ((sp->length == 0) && (sp->msg_is_complete == 0))
861 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
864 if (TAILQ_EMPTY(&asoc->send_queue) &&
865 TAILQ_EMPTY(&asoc->sent_queue) &&
866 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
870 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
871 0, M_DONTWAIT, 1, MT_DATA);
877 struct sctp_paramhdr *ph;
880 SCTP_BUF_LEN(op_err) =
881 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
883 struct sctp_paramhdr *);
884 ph->param_type = htons(
885 SCTP_CAUSE_USER_INITIATED_ABT);
886 ph->param_length = htons(SCTP_BUF_LEN(op_err));
887 ippp = (uint32_t *) (ph + 1);
888 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
890 #if defined(SCTP_PANIC_ON_ABORT)
891 panic("disconnect does an abort");
894 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
895 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
896 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
897 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
898 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
899 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
901 SCTP_INP_RUNLOCK(inp);
902 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
905 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
908 SCTP_TCB_UNLOCK(stcb);
909 SCTP_INP_RUNLOCK(inp);
914 /* UDP model does not support this */
915 SCTP_INP_RUNLOCK(inp);
916 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
922 sctp_flush(struct socket *so, int how)
925 * We will just clear out the values and let subsequent close clear
926 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
927 * they will not be able to read the data, the socket will block
928 * that from happening.
930 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
932 * First make sure the sb will be happy, we don't use these
933 * except maybe the count
935 so->so_rcv.sb_cc = 0;
936 so->so_rcv.sb_mbcnt = 0;
937 so->so_rcv.sb_mb = NULL;
939 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
941 * First make sure the sb will be happy, we don't use these
942 * except maybe the count
944 so->so_snd.sb_cc = 0;
945 so->so_snd.sb_mbcnt = 0;
946 so->so_snd.sb_mb = NULL;
953 sctp_shutdown(struct socket *so)
955 struct sctp_inpcb *inp;
957 inp = (struct sctp_inpcb *)so->so_pcb;
959 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
963 /* For UDP model this is a invalid call */
964 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
965 /* Restore the flags that the soshutdown took away. */
966 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
967 /* This proc will wakeup for read and do nothing (I hope) */
968 SCTP_INP_RUNLOCK(inp);
969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
973 * Ok if we reach here its the TCP model and it is either a SHUT_WR
974 * or SHUT_RDWR. This means we put the shutdown flag against it.
977 struct sctp_tcb *stcb;
978 struct sctp_association *asoc;
982 stcb = LIST_FIRST(&inp->sctp_asoc_list);
985 * Ok we hit the case that the shutdown call was
986 * made after an abort or something. Nothing to do
989 SCTP_INP_RUNLOCK(inp);
994 if (TAILQ_EMPTY(&asoc->send_queue) &&
995 TAILQ_EMPTY(&asoc->sent_queue) &&
996 (asoc->stream_queue_cnt == 0)) {
997 if (asoc->locked_on_sending) {
1000 /* there is nothing queued to send, so I'm done... */
1001 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1002 /* only send SHUTDOWN the first time through */
1003 sctp_stop_timers_for_shutdown(stcb);
1004 sctp_send_shutdown(stcb,
1005 stcb->asoc.primary_destination);
1006 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1007 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1008 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1009 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1011 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1012 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1013 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1014 stcb->sctp_ep, stcb,
1015 asoc->primary_destination);
1016 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1017 stcb->sctp_ep, stcb,
1018 asoc->primary_destination);
1022 * we still got (or just got) data to send, so set
1025 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1026 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1027 asoc->primary_destination);
1029 if (asoc->locked_on_sending) {
1030 /* Locked to send out the data */
1031 struct sctp_stream_queue_pending *sp;
1033 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1035 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1036 asoc->locked_on_sending->stream_no);
1038 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1039 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1043 if (TAILQ_EMPTY(&asoc->send_queue) &&
1044 TAILQ_EMPTY(&asoc->sent_queue) &&
1045 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1046 struct mbuf *op_err;
1049 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1050 0, M_DONTWAIT, 1, MT_DATA);
1052 /* Fill in the user initiated abort */
1053 struct sctp_paramhdr *ph;
1056 SCTP_BUF_LEN(op_err) =
1057 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1059 struct sctp_paramhdr *);
1060 ph->param_type = htons(
1061 SCTP_CAUSE_USER_INITIATED_ABT);
1062 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1063 ippp = (uint32_t *) (ph + 1);
1064 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1066 #if defined(SCTP_PANIC_ON_ABORT)
1067 panic("shutdown does an abort");
1069 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1070 sctp_abort_an_association(stcb->sctp_ep, stcb,
1071 SCTP_RESPONSE_TO_USER_REQ,
1072 op_err, SCTP_SO_LOCKED);
1075 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1078 SCTP_TCB_UNLOCK(stcb);
1081 SCTP_INP_RUNLOCK(inp);
1086 * copies a "user" presentable address and removes embedded scope, etc.
1087 * returns 0 on success, 1 on error
1090 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1093 struct sockaddr_in6 lsa6;
1095 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1098 memcpy(ss, sa, sa->sa_len);
1105 * NOTE: assumes addr lock is held
1108 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1109 struct sctp_tcb *stcb,
1111 struct sockaddr_storage *sas,
1114 struct sctp_ifn *sctp_ifn;
1115 struct sctp_ifa *sctp_ifa;
1116 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1118 int ipv4_addr_legal, ipv6_addr_legal;
1119 struct sctp_vrf *vrf;
1126 /* Turn on all the appropriate scope */
1127 loopback_scope = stcb->asoc.loopback_scope;
1128 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1129 local_scope = stcb->asoc.local_scope;
1130 site_scope = stcb->asoc.site_scope;
1132 /* Turn on ALL scope, since we look at the EP */
1133 loopback_scope = ipv4_local_scope = local_scope =
1136 ipv4_addr_legal = ipv6_addr_legal = 0;
1137 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1138 ipv6_addr_legal = 1;
1139 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1140 ipv4_addr_legal = 1;
1143 ipv4_addr_legal = 1;
1145 vrf = sctp_find_vrf(vrf_id);
1149 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1150 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1151 if ((loopback_scope == 0) &&
1152 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1153 /* Skip loopback if loopback_scope not set */
1156 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1159 * For the BOUND-ALL case, the list
1160 * associated with a TCB is Always
1161 * considered a reverse list.. i.e.
1162 * it lists addresses that are NOT
1163 * part of the association. If this
1164 * is one of those we must skip it.
1166 if (sctp_is_addr_restricted(stcb,
1171 switch (sctp_ifa->address.sa.sa_family) {
1173 if (ipv4_addr_legal) {
1174 struct sockaddr_in *sin;
1176 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1177 if (sin->sin_addr.s_addr == 0) {
1185 if ((ipv4_local_scope == 0) &&
1186 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1190 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1191 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1192 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1193 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1194 actual += sizeof(struct sockaddr_in6);
1197 memcpy(sas, sin, sizeof(*sin));
1198 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1199 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1200 actual += sizeof(*sin);
1204 if (actual >= limit) {
1213 if (ipv6_addr_legal) {
1214 struct sockaddr_in6 *sin6;
1216 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1217 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1225 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1226 if (local_scope == 0)
1228 if (sin6->sin6_scope_id == 0) {
1229 if (sa6_recoverscope(sin6) != 0)
1247 if ((site_scope == 0) &&
1248 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1251 memcpy(sas, sin6, sizeof(*sin6));
1252 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1253 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1254 actual += sizeof(*sin6);
1255 if (actual >= limit) {
1270 struct sctp_laddr *laddr;
1272 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1274 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1278 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1281 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1282 sas = (struct sockaddr_storage *)((caddr_t)sas +
1283 laddr->ifa->address.sa.sa_len);
1284 actual += laddr->ifa->address.sa.sa_len;
1285 if (actual >= limit) {
1294 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1295 struct sctp_tcb *stcb,
1297 struct sockaddr_storage *sas)
1301 SCTP_IPI_ADDR_RLOCK();
1302 /* fill up addresses for the endpoint's default vrf */
1303 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1305 SCTP_IPI_ADDR_RUNLOCK();
1310 * NOTE: assumes addr lock is held
1313 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1316 struct sctp_vrf *vrf = NULL;
1319 * In both sub-set bound an bound_all cases we return the MAXIMUM
1320 * number of addresses that you COULD get. In reality the sub-set
1321 * bound may have an exclusion list for a given TCB OR in the
1322 * bound-all case a TCB may NOT include the loopback or other
1323 * addresses as well.
1325 vrf = sctp_find_vrf(vrf_id);
1329 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1330 struct sctp_ifn *sctp_ifn;
1331 struct sctp_ifa *sctp_ifa;
1333 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1334 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1335 /* Count them if they are the right type */
1336 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1337 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1338 cnt += sizeof(struct sockaddr_in6);
1340 cnt += sizeof(struct sockaddr_in);
1342 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1343 cnt += sizeof(struct sockaddr_in6);
1347 struct sctp_laddr *laddr;
1349 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1350 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1351 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1352 cnt += sizeof(struct sockaddr_in6);
1354 cnt += sizeof(struct sockaddr_in);
1356 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1357 cnt += sizeof(struct sockaddr_in6);
1364 sctp_count_max_addresses(struct sctp_inpcb *inp)
1368 SCTP_IPI_ADDR_RLOCK();
1369 /* count addresses for the endpoint's default VRF */
1370 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1371 SCTP_IPI_ADDR_RUNLOCK();
1376 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1377 size_t optsize, void *p, int delay)
1380 int creat_lock_on = 0;
1381 struct sctp_tcb *stcb = NULL;
1382 struct sockaddr *sa;
1383 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1386 int bad_addresses = 0;
1389 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1391 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1392 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1393 /* We are already connected AND the TCP model */
1394 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1395 return (EADDRINUSE);
1397 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1398 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1401 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1402 SCTP_INP_RLOCK(inp);
1403 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1404 SCTP_INP_RUNLOCK(inp);
1407 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1410 SCTP_INP_INCR_REF(inp);
1411 SCTP_ASOC_CREATE_LOCK(inp);
1413 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1414 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1415 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1419 totaddrp = (int *)optval;
1420 totaddr = *totaddrp;
1421 sa = (struct sockaddr *)(totaddrp + 1);
1422 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1423 if ((stcb != NULL) || bad_addresses) {
1424 /* Already have or am bring up an association */
1425 SCTP_ASOC_CREATE_UNLOCK(inp);
1428 SCTP_TCB_UNLOCK(stcb);
1429 if (bad_addresses == 0) {
1430 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1436 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1441 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1443 struct in6pcb *inp6;
1445 inp6 = (struct in6pcb *)inp;
1446 if (SCTP_IPV6_V6ONLY(inp6)) {
1448 * if IPV6_V6ONLY flag, ignore connections destined
1449 * to a v4 addr or v4-mapped addr
1451 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1457 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1458 SCTP_PCB_FLAGS_UNBOUND) {
1459 /* Bind a ephemeral port */
1460 error = sctp_inpcb_bind(so, NULL, NULL, p);
1465 /* FIX ME: do we want to pass in a vrf on the connect call? */
1466 vrf_id = inp->def_vrf_id;
1468 /* We are GOOD to go */
1469 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1473 /* Gak! no memory */
1476 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1477 /* move to second address */
1478 if (sa->sa_family == AF_INET)
1479 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1481 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1484 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1485 /* Fill in the return id */
1487 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1490 a_id = (sctp_assoc_t *) optval;
1491 *a_id = sctp_get_associd(stcb);
1493 /* initialize authentication parameters for the assoc */
1494 sctp_initialize_auth_params(inp, stcb);
1497 /* doing delayed connection */
1498 stcb->asoc.delayed_connection = 1;
1499 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1501 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1502 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1504 SCTP_TCB_UNLOCK(stcb);
1505 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1506 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1507 /* Set the connected flag so we can queue data */
1511 if (creat_lock_on) {
1512 SCTP_ASOC_CREATE_UNLOCK(inp);
1514 SCTP_INP_DECR_REF(inp);
1518 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1519 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1520 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1521 SCTP_INP_RLOCK(inp); \
1522 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1524 SCTP_TCB_LOCK(stcb); \
1526 SCTP_INP_RUNLOCK(inp); \
1527 } else if (assoc_id != 0) { \
1528 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1529 if (stcb == NULL) { \
1530 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1540 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1541 if (size < sizeof(type)) { \
1542 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1546 destp = (type *)srcp; \
1551 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1554 struct sctp_inpcb *inp = NULL;
1556 struct sctp_tcb *stcb = NULL;
1558 if (optval == NULL) {
1559 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1562 inp = (struct sctp_inpcb *)so->so_pcb;
1564 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1571 case SCTP_AUTOCLOSE:
1572 case SCTP_EXPLICIT_EOR:
1573 case SCTP_AUTO_ASCONF:
1574 case SCTP_DISABLE_FRAGMENTS:
1575 case SCTP_I_WANT_MAPPED_V4_ADDR:
1576 case SCTP_USE_EXT_RCVINFO:
1577 SCTP_INP_RLOCK(inp);
1579 case SCTP_DISABLE_FRAGMENTS:
1580 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1582 case SCTP_I_WANT_MAPPED_V4_ADDR:
1583 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1585 case SCTP_AUTO_ASCONF:
1586 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1587 /* only valid for bound all sockets */
1588 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1590 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1595 case SCTP_EXPLICIT_EOR:
1596 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1599 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1601 case SCTP_USE_EXT_RCVINFO:
1602 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1604 case SCTP_AUTOCLOSE:
1605 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1606 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1612 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1613 error = ENOPROTOOPT;
1614 } /* end switch (sopt->sopt_name) */
1615 if (optname != SCTP_AUTOCLOSE) {
1616 /* make it an "on/off" value */
1619 if (*optsize < sizeof(val)) {
1620 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1624 SCTP_INP_RUNLOCK(inp);
1626 /* return the option value */
1627 *(int *)optval = val;
1628 *optsize = sizeof(val);
1631 case SCTP_GET_PACKET_LOG:
1633 #ifdef SCTP_PACKET_LOGGING
1637 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1638 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1641 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1646 case SCTP_PARTIAL_DELIVERY_POINT:
1650 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1651 *value = inp->partial_delivery_point;
1652 *optsize = sizeof(uint32_t);
1655 case SCTP_FRAGMENT_INTERLEAVE:
1659 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1660 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1661 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1662 *value = SCTP_FRAG_LEVEL_2;
1664 *value = SCTP_FRAG_LEVEL_1;
1667 *value = SCTP_FRAG_LEVEL_0;
1669 *optsize = sizeof(uint32_t);
1672 case SCTP_CMT_ON_OFF:
1674 struct sctp_assoc_value *av;
1676 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1677 if (sctp_cmt_on_off) {
1678 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1680 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1681 SCTP_TCB_UNLOCK(stcb);
1684 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1688 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1689 error = ENOPROTOOPT;
1691 *optsize = sizeof(*av);
1694 /* JRS - Get socket option for pluggable congestion control */
1695 case SCTP_PLUGGABLE_CC:
1697 struct sctp_assoc_value *av;
1699 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1700 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1702 av->assoc_value = stcb->asoc.congestion_control_module;
1703 SCTP_TCB_UNLOCK(stcb);
1705 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1707 *optsize = sizeof(*av);
1710 case SCTP_GET_ADDR_LEN:
1712 struct sctp_assoc_value *av;
1714 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1717 if (av->assoc_value == AF_INET) {
1718 av->assoc_value = sizeof(struct sockaddr_in);
1723 if (av->assoc_value == AF_INET6) {
1724 av->assoc_value = sizeof(struct sockaddr_in6);
1729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1731 *optsize = sizeof(*av);
1734 case SCTP_GET_ASSOC_NUMBER:
1736 uint32_t *value, cnt;
1738 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1740 SCTP_INP_RLOCK(inp);
1741 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1744 SCTP_INP_RUNLOCK(inp);
1746 *optsize = sizeof(uint32_t);
1750 case SCTP_GET_ASSOC_ID_LIST:
1752 struct sctp_assoc_ids *ids;
1753 unsigned int at, limit;
1755 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1757 limit = *optsize / sizeof(sctp_assoc_t);
1758 SCTP_INP_RLOCK(inp);
1759 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1761 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1764 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1768 SCTP_INP_RUNLOCK(inp);
1769 *optsize = at * sizeof(sctp_assoc_t);
1774 struct sctp_assoc_value *av;
1776 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1777 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1780 av->assoc_value = stcb->asoc.context;
1781 SCTP_TCB_UNLOCK(stcb);
1783 SCTP_INP_RLOCK(inp);
1784 av->assoc_value = inp->sctp_context;
1785 SCTP_INP_RUNLOCK(inp);
1787 *optsize = sizeof(*av);
1792 uint32_t *default_vrfid;
1794 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1795 *default_vrfid = inp->def_vrf_id;
1798 case SCTP_GET_ASOC_VRF:
1800 struct sctp_assoc_value *id;
1802 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1803 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1806 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1809 id->assoc_value = stcb->asoc.vrf_id;
1812 case SCTP_GET_VRF_IDS:
1814 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1818 case SCTP_GET_NONCE_VALUES:
1820 struct sctp_get_nonce_values *gnv;
1822 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1823 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1826 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1827 gnv->gn_local_tag = stcb->asoc.my_vtag;
1828 SCTP_TCB_UNLOCK(stcb);
1830 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1833 *optsize = sizeof(*gnv);
1836 case SCTP_DELAYED_SACK:
1838 struct sctp_sack_info *sack;
1840 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1841 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1843 sack->sack_delay = stcb->asoc.delayed_ack;
1844 sack->sack_freq = stcb->asoc.sack_freq;
1845 SCTP_TCB_UNLOCK(stcb);
1847 SCTP_INP_RLOCK(inp);
1848 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1849 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1850 SCTP_INP_RUNLOCK(inp);
1852 *optsize = sizeof(*sack);
1856 case SCTP_GET_SNDBUF_USE:
1858 struct sctp_sockstat *ss;
1860 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1861 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1864 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1865 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1866 stcb->asoc.size_on_all_streams);
1867 SCTP_TCB_UNLOCK(stcb);
1869 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1872 *optsize = sizeof(struct sctp_sockstat);
1875 case SCTP_MAX_BURST:
1879 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1881 SCTP_INP_RLOCK(inp);
1882 *value = inp->sctp_ep.max_burst;
1883 SCTP_INP_RUNLOCK(inp);
1884 *optsize = sizeof(uint8_t);
1889 struct sctp_assoc_value *av;
1892 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1893 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1896 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1897 SCTP_TCB_UNLOCK(stcb);
1899 SCTP_INP_RLOCK(inp);
1900 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1901 ovh = SCTP_MED_OVERHEAD;
1903 ovh = SCTP_MED_V4_OVERHEAD;
1905 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1906 av->assoc_value = 0;
1908 av->assoc_value = inp->sctp_frag_point - ovh;
1909 SCTP_INP_RUNLOCK(inp);
1911 *optsize = sizeof(struct sctp_assoc_value);
1914 case SCTP_GET_STAT_LOG:
1915 error = sctp_fill_stat_log(optval, optsize);
1919 struct sctp_event_subscribe *events;
1921 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1922 memset(events, 0, sizeof(*events));
1923 SCTP_INP_RLOCK(inp);
1924 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1925 events->sctp_data_io_event = 1;
1927 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1928 events->sctp_association_event = 1;
1930 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1931 events->sctp_address_event = 1;
1933 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1934 events->sctp_send_failure_event = 1;
1936 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1937 events->sctp_peer_error_event = 1;
1939 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1940 events->sctp_shutdown_event = 1;
1942 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1943 events->sctp_partial_delivery_event = 1;
1945 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1946 events->sctp_adaptation_layer_event = 1;
1948 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1949 events->sctp_authentication_event = 1;
1951 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1952 events->sctp_stream_reset_events = 1;
1953 SCTP_INP_RUNLOCK(inp);
1954 *optsize = sizeof(struct sctp_event_subscribe);
1958 case SCTP_ADAPTATION_LAYER:
1962 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1964 SCTP_INP_RLOCK(inp);
1965 *value = inp->sctp_ep.adaptation_layer_indicator;
1966 SCTP_INP_RUNLOCK(inp);
1967 *optsize = sizeof(uint32_t);
1970 case SCTP_SET_INITIAL_DBG_SEQ:
1974 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1975 SCTP_INP_RLOCK(inp);
1976 *value = inp->sctp_ep.initial_sequence_debug;
1977 SCTP_INP_RUNLOCK(inp);
1978 *optsize = sizeof(uint32_t);
1981 case SCTP_GET_LOCAL_ADDR_SIZE:
1985 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1986 SCTP_INP_RLOCK(inp);
1987 *value = sctp_count_max_addresses(inp);
1988 SCTP_INP_RUNLOCK(inp);
1989 *optsize = sizeof(uint32_t);
1992 case SCTP_GET_REMOTE_ADDR_SIZE:
1996 struct sctp_nets *net;
1998 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1999 /* FIXME MT: change to sctp_assoc_value? */
2000 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2004 /* Count the sizes */
2005 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2006 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2007 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2008 size += sizeof(struct sockaddr_in6);
2009 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2010 size += sizeof(struct sockaddr_in);
2016 SCTP_TCB_UNLOCK(stcb);
2017 *value = (uint32_t) size;
2019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2022 *optsize = sizeof(uint32_t);
2025 case SCTP_GET_PEER_ADDRESSES:
2027 * Get the address information, an array is passed in to
2028 * fill up we pack it.
2032 struct sockaddr_storage *sas;
2033 struct sctp_nets *net;
2034 struct sctp_getaddresses *saddr;
2036 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2037 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2040 left = (*optsize) - sizeof(struct sctp_getaddresses);
2041 *optsize = sizeof(struct sctp_getaddresses);
2042 sas = (struct sockaddr_storage *)&saddr->addr[0];
2044 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2045 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2046 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2047 cpsz = sizeof(struct sockaddr_in6);
2048 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2049 cpsz = sizeof(struct sockaddr_in);
2055 /* not enough room. */
2059 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2060 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2061 /* Must map the address */
2062 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2063 (struct sockaddr_in6 *)sas);
2066 memcpy(sas, &net->ro._l_addr, cpsz);
2070 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2072 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2076 SCTP_TCB_UNLOCK(stcb);
2078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2083 case SCTP_GET_LOCAL_ADDRESSES:
2085 size_t limit, actual;
2086 struct sockaddr_storage *sas;
2087 struct sctp_getaddresses *saddr;
2089 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2090 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2092 sas = (struct sockaddr_storage *)&saddr->addr[0];
2093 limit = *optsize - sizeof(sctp_assoc_t);
2094 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2096 SCTP_TCB_UNLOCK(stcb);
2098 *optsize = sizeof(struct sockaddr_storage) + actual;
2101 case SCTP_PEER_ADDR_PARAMS:
2103 struct sctp_paddrparams *paddrp;
2104 struct sctp_nets *net;
2106 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2107 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2111 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2114 * We increment here since
2115 * sctp_findassociation_ep_addr() wil do a
2116 * decrement if it finds the stcb as long as
2117 * the locked tcb (last argument) is NOT a
2120 SCTP_INP_INCR_REF(inp);
2121 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2123 SCTP_INP_DECR_REF(inp);
2126 if (stcb && (net == NULL)) {
2127 struct sockaddr *sa;
2129 sa = (struct sockaddr *)&paddrp->spp_address;
2130 if (sa->sa_family == AF_INET) {
2131 struct sockaddr_in *sin;
2133 sin = (struct sockaddr_in *)sa;
2134 if (sin->sin_addr.s_addr) {
2136 SCTP_TCB_UNLOCK(stcb);
2137 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2140 } else if (sa->sa_family == AF_INET6) {
2141 struct sockaddr_in6 *sin6;
2143 sin6 = (struct sockaddr_in6 *)sa;
2144 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2146 SCTP_TCB_UNLOCK(stcb);
2147 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2151 error = EAFNOSUPPORT;
2152 SCTP_TCB_UNLOCK(stcb);
2153 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2158 /* Applys to the specific association */
2159 paddrp->spp_flags = 0;
2163 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2164 ovh = SCTP_MED_OVERHEAD;
2166 ovh = SCTP_MED_V4_OVERHEAD;
2170 paddrp->spp_pathmaxrxt = net->failure_threshold;
2171 paddrp->spp_pathmtu = net->mtu - ovh;
2172 /* get flags for HB */
2173 if (net->dest_state & SCTP_ADDR_NOHB)
2174 paddrp->spp_flags |= SPP_HB_DISABLE;
2176 paddrp->spp_flags |= SPP_HB_ENABLE;
2177 /* get flags for PMTU */
2178 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2179 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2181 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2184 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2185 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2186 paddrp->spp_flags |= SPP_IPV4_TOS;
2190 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2191 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2192 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2197 * No destination so return default
2202 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2203 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2205 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2206 paddrp->spp_flags |= SPP_IPV4_TOS;
2209 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2210 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2212 /* default settings should be these */
2213 if (stcb->asoc.hb_is_disabled == 0) {
2214 paddrp->spp_flags |= SPP_HB_ENABLE;
2216 paddrp->spp_flags |= SPP_HB_DISABLE;
2218 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2219 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2224 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2227 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2228 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2229 SCTP_TCB_UNLOCK(stcb);
2231 /* Use endpoint defaults */
2232 SCTP_INP_RLOCK(inp);
2233 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2234 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2235 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2236 /* get inp's default */
2238 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2239 paddrp->spp_flags |= SPP_IPV4_TOS;
2242 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2243 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2244 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2247 /* can't return this */
2248 paddrp->spp_pathmtu = 0;
2250 /* default behavior, no stcb */
2251 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2253 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2254 paddrp->spp_flags |= SPP_HB_ENABLE;
2256 paddrp->spp_flags |= SPP_HB_DISABLE;
2258 SCTP_INP_RUNLOCK(inp);
2260 *optsize = sizeof(struct sctp_paddrparams);
2263 case SCTP_GET_PEER_ADDR_INFO:
2265 struct sctp_paddrinfo *paddri;
2266 struct sctp_nets *net;
2268 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2269 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2273 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2276 * We increment here since
2277 * sctp_findassociation_ep_addr() wil do a
2278 * decrement if it finds the stcb as long as
2279 * the locked tcb (last argument) is NOT a
2282 SCTP_INP_INCR_REF(inp);
2283 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2285 SCTP_INP_DECR_REF(inp);
2289 if ((stcb) && (net)) {
2290 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2291 paddri->spinfo_cwnd = net->cwnd;
2292 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2293 paddri->spinfo_rto = net->RTO;
2294 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2295 SCTP_TCB_UNLOCK(stcb);
2298 SCTP_TCB_UNLOCK(stcb);
2300 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2303 *optsize = sizeof(struct sctp_paddrinfo);
2306 case SCTP_PCB_STATUS:
2308 struct sctp_pcbinfo *spcb;
2310 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2311 sctp_fill_pcbinfo(spcb);
2312 *optsize = sizeof(struct sctp_pcbinfo);
2318 struct sctp_nets *net;
2319 struct sctp_status *sstat;
2321 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2322 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2325 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2330 * I think passing the state is fine since
2331 * sctp_constants.h will be available to the user
2334 sstat->sstat_state = stcb->asoc.state;
2335 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2336 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2337 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2339 * We can't include chunks that have been passed to
2340 * the socket layer. Only things in queue.
2342 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2343 stcb->asoc.cnt_on_all_streams);
2346 sstat->sstat_instrms = stcb->asoc.streamincnt;
2347 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2348 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2349 memcpy(&sstat->sstat_primary.spinfo_address,
2350 &stcb->asoc.primary_destination->ro._l_addr,
2351 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2352 net = stcb->asoc.primary_destination;
2353 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2355 * Again the user can get info from sctp_constants.h
2356 * for what the state of the network is.
2358 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2359 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2360 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2361 sstat->sstat_primary.spinfo_rto = net->RTO;
2362 sstat->sstat_primary.spinfo_mtu = net->mtu;
2363 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2364 SCTP_TCB_UNLOCK(stcb);
2365 *optsize = sizeof(*sstat);
2370 struct sctp_rtoinfo *srto;
2372 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2373 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2376 srto->srto_initial = stcb->asoc.initial_rto;
2377 srto->srto_max = stcb->asoc.maxrto;
2378 srto->srto_min = stcb->asoc.minrto;
2379 SCTP_TCB_UNLOCK(stcb);
2381 SCTP_INP_RLOCK(inp);
2382 srto->srto_initial = inp->sctp_ep.initial_rto;
2383 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2384 srto->srto_min = inp->sctp_ep.sctp_minrto;
2385 SCTP_INP_RUNLOCK(inp);
2387 *optsize = sizeof(*srto);
2390 case SCTP_ASSOCINFO:
2392 struct sctp_assocparams *sasoc;
2395 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2396 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2399 oldval = sasoc->sasoc_cookie_life;
2400 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2401 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2402 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2403 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2404 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2405 SCTP_TCB_UNLOCK(stcb);
2407 SCTP_INP_RLOCK(inp);
2408 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2409 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2410 sasoc->sasoc_number_peer_destinations = 0;
2411 sasoc->sasoc_peer_rwnd = 0;
2412 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2413 SCTP_INP_RUNLOCK(inp);
2415 *optsize = sizeof(*sasoc);
2418 case SCTP_DEFAULT_SEND_PARAM:
2420 struct sctp_sndrcvinfo *s_info;
2422 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2423 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2426 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2427 SCTP_TCB_UNLOCK(stcb);
2429 SCTP_INP_RLOCK(inp);
2430 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2431 SCTP_INP_RUNLOCK(inp);
2433 *optsize = sizeof(*s_info);
2438 struct sctp_initmsg *sinit;
2440 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2441 SCTP_INP_RLOCK(inp);
2442 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2443 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2444 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2445 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2446 SCTP_INP_RUNLOCK(inp);
2447 *optsize = sizeof(*sinit);
2450 case SCTP_PRIMARY_ADDR:
2451 /* we allow a "get" operation on this */
2453 struct sctp_setprim *ssp;
2455 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2456 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2459 /* simply copy out the sockaddr_storage... */
2463 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2464 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2466 memcpy(&ssp->ssp_addr,
2467 &stcb->asoc.primary_destination->ro._l_addr,
2469 SCTP_TCB_UNLOCK(stcb);
2471 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2474 *optsize = sizeof(*ssp);
2478 case SCTP_HMAC_IDENT:
2480 struct sctp_hmacalgo *shmac;
2481 sctp_hmaclist_t *hmaclist;
2485 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2487 SCTP_INP_RLOCK(inp);
2488 hmaclist = inp->sctp_ep.local_hmacs;
2489 if (hmaclist == NULL) {
2490 /* no HMACs to return */
2491 *optsize = sizeof(*shmac);
2492 SCTP_INP_RUNLOCK(inp);
2495 /* is there room for all of the hmac ids? */
2496 size = sizeof(*shmac) + (hmaclist->num_algo *
2497 sizeof(shmac->shmac_idents[0]));
2498 if ((size_t)(*optsize) < size) {
2499 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2501 SCTP_INP_RUNLOCK(inp);
2504 /* copy in the list */
2505 for (i = 0; i < hmaclist->num_algo; i++)
2506 shmac->shmac_idents[i] = hmaclist->hmac[i];
2507 SCTP_INP_RUNLOCK(inp);
2511 case SCTP_AUTH_ACTIVE_KEY:
2513 struct sctp_authkeyid *scact;
2515 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2516 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2519 /* get the active key on the assoc */
2520 scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2521 SCTP_TCB_UNLOCK(stcb);
2523 /* get the endpoint active key */
2524 SCTP_INP_RLOCK(inp);
2525 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2526 SCTP_INP_RUNLOCK(inp);
2528 *optsize = sizeof(*scact);
2531 case SCTP_LOCAL_AUTH_CHUNKS:
2533 struct sctp_authchunks *sac;
2534 sctp_auth_chklist_t *chklist = NULL;
2537 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2538 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2541 /* get off the assoc */
2542 chklist = stcb->asoc.local_auth_chunks;
2543 /* is there enough space? */
2544 size = sctp_auth_get_chklist_size(chklist);
2545 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2547 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2549 /* copy in the chunks */
2550 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2552 SCTP_TCB_UNLOCK(stcb);
2554 /* get off the endpoint */
2555 SCTP_INP_RLOCK(inp);
2556 chklist = inp->sctp_ep.local_auth_chunks;
2557 /* is there enough space? */
2558 size = sctp_auth_get_chklist_size(chklist);
2559 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2561 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2563 /* copy in the chunks */
2564 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2566 SCTP_INP_RUNLOCK(inp);
2568 *optsize = sizeof(struct sctp_authchunks) + size;
2571 case SCTP_PEER_AUTH_CHUNKS:
2573 struct sctp_authchunks *sac;
2574 sctp_auth_chklist_t *chklist = NULL;
2577 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2578 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2581 /* get off the assoc */
2582 chklist = stcb->asoc.peer_auth_chunks;
2583 /* is there enough space? */
2584 size = sctp_auth_get_chklist_size(chklist);
2585 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2587 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2589 /* copy in the chunks */
2590 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2592 SCTP_TCB_UNLOCK(stcb);
2594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2597 *optsize = sizeof(struct sctp_authchunks) + size;
2603 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2604 error = ENOPROTOOPT;
2607 } /* end switch (sopt->sopt_name) */
2612 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2617 struct sctp_tcb *stcb = NULL;
2618 struct sctp_inpcb *inp = NULL;
2621 if (optval == NULL) {
2622 SCTP_PRINTF("optval is NULL\n");
2623 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2626 inp = (struct sctp_inpcb *)so->so_pcb;
2628 SCTP_PRINTF("inp is NULL?\n");
2629 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2632 vrf_id = inp->def_vrf_id;
2637 case SCTP_AUTOCLOSE:
2638 case SCTP_AUTO_ASCONF:
2639 case SCTP_EXPLICIT_EOR:
2640 case SCTP_DISABLE_FRAGMENTS:
2641 case SCTP_USE_EXT_RCVINFO:
2642 case SCTP_I_WANT_MAPPED_V4_ADDR:
2643 /* copy in the option value */
2644 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2649 case SCTP_DISABLE_FRAGMENTS:
2650 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2652 case SCTP_AUTO_ASCONF:
2654 * NOTE: we don't really support this flag
2656 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2657 /* only valid for bound all sockets */
2658 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2660 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2664 case SCTP_EXPLICIT_EOR:
2665 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2667 case SCTP_USE_EXT_RCVINFO:
2668 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2670 case SCTP_I_WANT_MAPPED_V4_ADDR:
2671 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2672 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2674 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2679 set_opt = SCTP_PCB_FLAGS_NODELAY;
2681 case SCTP_AUTOCLOSE:
2682 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2683 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2684 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2687 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2689 * The value is in ticks. Note this does not effect
2690 * old associations, only new ones.
2692 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2695 SCTP_INP_WLOCK(inp);
2697 sctp_feature_on(inp, set_opt);
2699 sctp_feature_off(inp, set_opt);
2701 SCTP_INP_WUNLOCK(inp);
2703 case SCTP_PARTIAL_DELIVERY_POINT:
2707 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2708 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2709 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2713 inp->partial_delivery_point = *value;
2716 case SCTP_FRAGMENT_INTERLEAVE:
2717 /* not yet until we re-write sctp_recvmsg() */
2721 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2722 if (*level == SCTP_FRAG_LEVEL_2) {
2723 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2724 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2725 } else if (*level == SCTP_FRAG_LEVEL_1) {
2726 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2727 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2728 } else if (*level == SCTP_FRAG_LEVEL_0) {
2729 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2730 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2733 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2738 case SCTP_CMT_ON_OFF:
2740 struct sctp_assoc_value *av;
2742 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2743 if (sctp_cmt_on_off) {
2744 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2746 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2747 SCTP_TCB_UNLOCK(stcb);
2749 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2753 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2754 error = ENOPROTOOPT;
2758 /* JRS - Set socket option for pluggable congestion control */
2759 case SCTP_PLUGGABLE_CC:
2761 struct sctp_assoc_value *av;
2763 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2764 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2766 switch (av->assoc_value) {
2768 * JRS - Standard TCP congestion
2771 case SCTP_CC_RFC2581:
2773 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2774 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2775 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2776 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2777 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2778 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2779 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2780 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2781 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2782 SCTP_TCB_UNLOCK(stcb);
2786 * JRS - High Speed TCP congestion
2791 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2792 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2793 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2794 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2795 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2796 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2797 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2798 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2799 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2800 SCTP_TCB_UNLOCK(stcb);
2803 /* JRS - HTCP congestion control */
2806 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2807 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2808 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2809 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2810 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2811 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2812 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2813 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2814 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2815 SCTP_TCB_UNLOCK(stcb);
2819 * JRS - All other values are
2824 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2826 SCTP_TCB_UNLOCK(stcb);
2831 switch (av->assoc_value) {
2832 case SCTP_CC_RFC2581:
2835 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2838 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2845 case SCTP_CLR_STAT_LOG:
2846 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2851 struct sctp_assoc_value *av;
2853 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2854 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2857 stcb->asoc.context = av->assoc_value;
2858 SCTP_TCB_UNLOCK(stcb);
2860 SCTP_INP_WLOCK(inp);
2861 inp->sctp_context = av->assoc_value;
2862 SCTP_INP_WUNLOCK(inp);
2868 uint32_t *default_vrfid;
2870 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2871 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2872 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2876 inp->def_vrf_id = *default_vrfid;
2879 case SCTP_DEL_VRF_ID:
2881 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2885 case SCTP_ADD_VRF_ID:
2887 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2891 case SCTP_DELAYED_SACK:
2893 struct sctp_sack_info *sack;
2895 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2896 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2897 if (sack->sack_delay) {
2898 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2899 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2902 if (sack->sack_delay) {
2903 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2904 sack->sack_delay = TICKS_TO_MSEC(1);
2906 stcb->asoc.delayed_ack = sack->sack_delay;
2908 if (sack->sack_freq) {
2909 stcb->asoc.sack_freq = sack->sack_freq;
2911 SCTP_TCB_UNLOCK(stcb);
2913 SCTP_INP_WLOCK(inp);
2914 if (sack->sack_delay) {
2915 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2916 sack->sack_delay = TICKS_TO_MSEC(1);
2918 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2920 if (sack->sack_freq) {
2921 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2923 SCTP_INP_WUNLOCK(inp);
2927 case SCTP_AUTH_CHUNK:
2929 struct sctp_authchunk *sauth;
2931 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2933 SCTP_INP_WLOCK(inp);
2934 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
2935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2938 SCTP_INP_WUNLOCK(inp);
2943 struct sctp_authkey *sca;
2944 struct sctp_keyhead *shared_keys;
2945 sctp_sharedkey_t *shared_key;
2946 sctp_key_t *key = NULL;
2949 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2950 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2951 size = optsize - sizeof(*sca);
2954 /* set it on the assoc */
2955 shared_keys = &stcb->asoc.shared_keys;
2956 /* clear the cached keys for this key id */
2957 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2959 * create the new shared key and
2963 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2965 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2967 SCTP_TCB_UNLOCK(stcb);
2971 shared_key = sctp_alloc_sharedkey();
2972 if (shared_key == NULL) {
2974 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2976 SCTP_TCB_UNLOCK(stcb);
2979 shared_key->key = key;
2980 shared_key->keyid = sca->sca_keynumber;
2981 sctp_insert_sharedkey(shared_keys, shared_key);
2982 SCTP_TCB_UNLOCK(stcb);
2984 /* set it on the endpoint */
2985 SCTP_INP_WLOCK(inp);
2986 shared_keys = &inp->sctp_ep.shared_keys;
2988 * clear the cached keys on all assocs for
2991 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2993 * create the new shared key and
2997 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2999 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3001 SCTP_INP_WUNLOCK(inp);
3005 shared_key = sctp_alloc_sharedkey();
3006 if (shared_key == NULL) {
3008 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3010 SCTP_INP_WUNLOCK(inp);
3013 shared_key->key = key;
3014 shared_key->keyid = sca->sca_keynumber;
3015 sctp_insert_sharedkey(shared_keys, shared_key);
3016 SCTP_INP_WUNLOCK(inp);
3020 case SCTP_HMAC_IDENT:
3022 struct sctp_hmacalgo *shmac;
3023 sctp_hmaclist_t *hmaclist;
3025 size_t size, i, found;
3027 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3028 size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
3029 hmaclist = sctp_alloc_hmaclist(size);
3030 if (hmaclist == NULL) {
3031 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3035 for (i = 0; i < size; i++) {
3036 hmacid = shmac->shmac_idents[i];
3037 if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
3038 /* invalid HMACs were found */ ;
3039 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3041 sctp_free_hmaclist(hmaclist);
3042 goto sctp_set_hmac_done;
3046 for (i = 0; i < hmaclist->num_algo; i++) {
3047 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3048 /* already in list */
3053 sctp_free_hmaclist(hmaclist);
3054 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3058 /* set it on the endpoint */
3059 SCTP_INP_WLOCK(inp);
3060 if (inp->sctp_ep.local_hmacs)
3061 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3062 inp->sctp_ep.local_hmacs = hmaclist;
3063 SCTP_INP_WUNLOCK(inp);
3067 case SCTP_AUTH_ACTIVE_KEY:
3069 struct sctp_authkeyid *scact;
3071 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
3072 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3074 /* set the active key on the right place */
3076 /* set the active key on the assoc */
3077 if (sctp_auth_setactivekey(stcb, scact->scact_keynumber)) {
3078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3081 SCTP_TCB_UNLOCK(stcb);
3083 /* set the active key on the endpoint */
3084 SCTP_INP_WLOCK(inp);
3085 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
3086 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3089 SCTP_INP_WUNLOCK(inp);
3093 case SCTP_AUTH_DELETE_KEY:
3095 struct sctp_authkeyid *scdel;
3097 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
3098 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3100 /* delete the key from the right place */
3102 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
3103 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3106 SCTP_TCB_UNLOCK(stcb);
3108 SCTP_INP_WLOCK(inp);
3109 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
3110 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3113 SCTP_INP_WUNLOCK(inp);
3118 case SCTP_RESET_STREAMS:
3120 struct sctp_stream_reset *strrst;
3121 uint8_t send_in = 0, send_tsn = 0, send_out = 0;
3124 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3125 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3128 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3132 if (stcb->asoc.peer_supports_strreset == 0) {
3134 * Peer does not support it, we return
3135 * protocol not supported since this is true
3136 * for this feature and this peer, not the
3137 * socket request in general.
3139 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3140 error = EPROTONOSUPPORT;
3141 SCTP_TCB_UNLOCK(stcb);
3144 if (stcb->asoc.stream_reset_outstanding) {
3145 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3147 SCTP_TCB_UNLOCK(stcb);
3150 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3152 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3154 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3157 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3160 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3162 SCTP_TCB_UNLOCK(stcb);
3165 for (i = 0; i < strrst->strrst_num_streams; i++) {
3168 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3169 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3174 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3175 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3182 SCTP_TCB_UNLOCK(stcb);
3185 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3186 strrst->strrst_list,
3187 send_out, (stcb->asoc.str_reset_seq_in - 3),
3190 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3191 SCTP_TCB_UNLOCK(stcb);
3195 case SCTP_CONNECT_X:
3196 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3197 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3201 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3204 case SCTP_CONNECT_X_DELAYED:
3205 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3206 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3210 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3213 case SCTP_CONNECT_X_COMPLETE:
3215 struct sockaddr *sa;
3216 struct sctp_nets *net;
3218 /* FIXME MT: check correct? */
3219 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3222 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3223 SCTP_INP_RLOCK(inp);
3224 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3226 SCTP_TCB_LOCK(stcb);
3227 net = sctp_findnet(stcb, sa);
3229 SCTP_INP_RUNLOCK(inp);
3232 * We increment here since
3233 * sctp_findassociation_ep_addr() wil do a
3234 * decrement if it finds the stcb as long as
3235 * the locked tcb (last argument) is NOT a
3238 SCTP_INP_INCR_REF(inp);
3239 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3241 SCTP_INP_DECR_REF(inp);
3246 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3250 if (stcb->asoc.delayed_connection == 1) {
3251 stcb->asoc.delayed_connection = 0;
3252 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3253 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3254 stcb->asoc.primary_destination,
3255 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3256 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3259 * already expired or did not use delayed
3262 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3265 SCTP_TCB_UNLOCK(stcb);
3268 case SCTP_MAX_BURST:
3272 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3274 SCTP_INP_WLOCK(inp);
3276 inp->sctp_ep.max_burst = *burst;
3278 SCTP_INP_WUNLOCK(inp);
3283 struct sctp_assoc_value *av;
3286 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3287 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3289 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3290 ovh = SCTP_MED_OVERHEAD;
3292 ovh = SCTP_MED_V4_OVERHEAD;
3295 if (av->assoc_value) {
3296 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3298 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3300 SCTP_TCB_UNLOCK(stcb);
3302 SCTP_INP_WLOCK(inp);
3304 * FIXME MT: I think this is not in tune
3307 if (av->assoc_value) {
3308 inp->sctp_frag_point = (av->assoc_value + ovh);
3310 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3312 SCTP_INP_WUNLOCK(inp);
3318 struct sctp_event_subscribe *events;
3320 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3322 SCTP_INP_WLOCK(inp);
3323 if (events->sctp_data_io_event) {
3324 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3326 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3329 if (events->sctp_association_event) {
3330 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3332 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3335 if (events->sctp_address_event) {
3336 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3338 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3341 if (events->sctp_send_failure_event) {
3342 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3344 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3347 if (events->sctp_peer_error_event) {
3348 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3350 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3353 if (events->sctp_shutdown_event) {
3354 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3356 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3359 if (events->sctp_partial_delivery_event) {
3360 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3362 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3365 if (events->sctp_adaptation_layer_event) {
3366 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3368 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3371 if (events->sctp_authentication_event) {
3372 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3374 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3377 if (events->sctp_stream_reset_events) {
3378 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3380 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3382 SCTP_INP_WUNLOCK(inp);
3386 case SCTP_ADAPTATION_LAYER:
3388 struct sctp_setadaptation *adap_bits;
3390 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3391 SCTP_INP_WLOCK(inp);
3392 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3393 SCTP_INP_WUNLOCK(inp);
3397 case SCTP_SET_INITIAL_DBG_SEQ:
3401 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3402 SCTP_INP_WLOCK(inp);
3403 inp->sctp_ep.initial_sequence_debug = *vvv;
3404 SCTP_INP_WUNLOCK(inp);
3408 case SCTP_DEFAULT_SEND_PARAM:
3410 struct sctp_sndrcvinfo *s_info;
3412 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3413 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3416 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3417 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3419 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3422 SCTP_TCB_UNLOCK(stcb);
3424 SCTP_INP_WLOCK(inp);
3425 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3426 SCTP_INP_WUNLOCK(inp);
3430 case SCTP_PEER_ADDR_PARAMS:
3431 /* Applys to the specific association */
3433 struct sctp_paddrparams *paddrp;
3434 struct sctp_nets *net;
3436 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3437 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3440 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3443 * We increment here since
3444 * sctp_findassociation_ep_addr() wil do a
3445 * decrement if it finds the stcb as long as
3446 * the locked tcb (last argument) is NOT a
3449 SCTP_INP_INCR_REF(inp);
3450 stcb = sctp_findassociation_ep_addr(&inp,
3451 (struct sockaddr *)&paddrp->spp_address,
3454 SCTP_INP_DECR_REF(inp);
3457 if (stcb && (net == NULL)) {
3458 struct sockaddr *sa;
3460 sa = (struct sockaddr *)&paddrp->spp_address;
3461 if (sa->sa_family == AF_INET) {
3462 struct sockaddr_in *sin;
3464 sin = (struct sockaddr_in *)sa;
3465 if (sin->sin_addr.s_addr) {
3466 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3467 SCTP_TCB_UNLOCK(stcb);
3471 } else if (sa->sa_family == AF_INET6) {
3472 struct sockaddr_in6 *sin6;
3474 sin6 = (struct sockaddr_in6 *)sa;
3475 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3476 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3477 SCTP_TCB_UNLOCK(stcb);
3482 error = EAFNOSUPPORT;
3483 SCTP_TCB_UNLOCK(stcb);
3484 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3489 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3491 SCTP_TCB_UNLOCK(stcb);
3492 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3495 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3497 SCTP_TCB_UNLOCK(stcb);
3498 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3502 /************************TCB SPECIFIC SET ******************/
3504 * do we change the timer for HB, we run
3509 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3510 ovh = SCTP_MED_OVERHEAD;
3512 ovh = SCTP_MED_V4_OVERHEAD;
3515 if (paddrp->spp_hbinterval)
3516 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3517 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3518 stcb->asoc.heart_beat_delay = 0;
3520 /* network sets ? */
3522 /************************NET SPECIFIC SET ******************/
3523 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3525 if (sctp_send_hb(stcb, 1, net) < 0) {
3526 /* asoc destroyed */
3527 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3532 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3533 net->dest_state |= SCTP_ADDR_NOHB;
3535 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3536 net->dest_state &= ~SCTP_ADDR_NOHB;
3538 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3539 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3540 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3541 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3543 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3544 net->mtu = paddrp->spp_pathmtu + ovh;
3545 if (net->mtu < stcb->asoc.smallest_mtu) {
3546 #ifdef SCTP_PRINT_FOR_B_AND_M
3547 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3550 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3554 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3555 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3556 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3559 if (paddrp->spp_pathmaxrxt)
3560 net->failure_threshold = paddrp->spp_pathmaxrxt;
3562 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3563 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3564 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3569 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3570 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3571 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3576 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3577 if (paddrp->spp_pathmaxrxt)
3578 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3580 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3581 /* Turn back on the timer */
3582 stcb->asoc.hb_is_disabled = 0;
3583 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3585 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3586 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3587 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3588 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3589 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3591 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3592 net->mtu = paddrp->spp_pathmtu + ovh;
3593 if (net->mtu < stcb->asoc.smallest_mtu) {
3594 #ifdef SCTP_PRINT_FOR_B_AND_M
3595 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3598 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3603 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3604 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3605 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3606 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3610 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3611 int cnt_of_unconf = 0;
3612 struct sctp_nets *lnet;
3614 stcb->asoc.hb_is_disabled = 1;
3615 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3616 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3621 * stop the timer ONLY if we
3622 * have no unconfirmed
3625 if (cnt_of_unconf == 0) {
3626 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3627 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3628 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3632 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3633 /* start up the timer. */
3634 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3635 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3639 if (paddrp->spp_flags & SPP_IPV4_TOS)
3640 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3643 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3644 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3648 SCTP_TCB_UNLOCK(stcb);
3650 /************************NO TCB, SET TO default stuff ******************/
3651 SCTP_INP_WLOCK(inp);
3653 * For the TOS/FLOWLABEL stuff you set it
3654 * with the options on the socket
3656 if (paddrp->spp_pathmaxrxt) {
3657 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3659 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3660 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3661 else if (paddrp->spp_hbinterval) {
3662 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3663 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3664 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3666 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3667 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3669 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3670 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3672 SCTP_INP_WUNLOCK(inp);
3678 struct sctp_rtoinfo *srto;
3679 uint32_t new_init, new_min, new_max;
3681 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3682 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3685 if (srto->srto_initial)
3686 new_init = srto->srto_initial;
3688 new_init = stcb->asoc.initial_rto;
3690 new_max = srto->srto_max;
3692 new_max = stcb->asoc.maxrto;
3694 new_min = srto->srto_min;
3696 new_min = stcb->asoc.minrto;
3697 if ((new_min <= new_init) && (new_init <= new_max)) {
3698 stcb->asoc.initial_rto = new_init;
3699 stcb->asoc.maxrto = new_max;
3700 stcb->asoc.minrto = new_min;
3702 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDOM);
3705 SCTP_TCB_UNLOCK(stcb);
3707 SCTP_INP_WLOCK(inp);
3708 if (srto->srto_initial)
3709 new_init = srto->srto_initial;
3711 new_init = inp->sctp_ep.initial_rto;
3713 new_max = srto->srto_max;
3715 new_max = inp->sctp_ep.sctp_maxrto;
3717 new_min = srto->srto_min;
3719 new_min = inp->sctp_ep.sctp_minrto;
3720 if ((new_min <= new_init) && (new_init <= new_max)) {
3721 inp->sctp_ep.initial_rto = new_init;
3722 inp->sctp_ep.sctp_maxrto = new_max;
3723 inp->sctp_ep.sctp_minrto = new_min;
3725 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDOM);
3728 SCTP_INP_WUNLOCK(inp);
3732 case SCTP_ASSOCINFO:
3734 struct sctp_assocparams *sasoc;
3736 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3737 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3738 if (sasoc->sasoc_cookie_life) {
3739 /* boundary check the cookie life */
3740 if (sasoc->sasoc_cookie_life < 1000)
3741 sasoc->sasoc_cookie_life = 1000;
3742 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3743 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3747 if (sasoc->sasoc_asocmaxrxt)
3748 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3749 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3750 sasoc->sasoc_peer_rwnd = 0;
3751 sasoc->sasoc_local_rwnd = 0;
3752 if (sasoc->sasoc_cookie_life) {
3753 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3755 SCTP_TCB_UNLOCK(stcb);
3757 SCTP_INP_WLOCK(inp);
3758 if (sasoc->sasoc_asocmaxrxt)
3759 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3760 sasoc->sasoc_number_peer_destinations = 0;
3761 sasoc->sasoc_peer_rwnd = 0;
3762 sasoc->sasoc_local_rwnd = 0;
3763 if (sasoc->sasoc_cookie_life) {
3764 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3766 SCTP_INP_WUNLOCK(inp);
3772 struct sctp_initmsg *sinit;
3774 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3775 SCTP_INP_WLOCK(inp);
3776 if (sinit->sinit_num_ostreams)
3777 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3779 if (sinit->sinit_max_instreams)
3780 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3782 if (sinit->sinit_max_attempts)
3783 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3785 if (sinit->sinit_max_init_timeo)
3786 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3787 SCTP_INP_WUNLOCK(inp);
3790 case SCTP_PRIMARY_ADDR:
3792 struct sctp_setprim *spa;
3793 struct sctp_nets *net, *lnet;
3795 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3796 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3800 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3803 * We increment here since
3804 * sctp_findassociation_ep_addr() wil do a
3805 * decrement if it finds the stcb as long as
3806 * the locked tcb (last argument) is NOT a
3809 SCTP_INP_INCR_REF(inp);
3810 stcb = sctp_findassociation_ep_addr(&inp,
3811 (struct sockaddr *)&spa->ssp_addr,
3814 SCTP_INP_DECR_REF(inp);
3818 if ((stcb) && (net)) {
3819 if ((net != stcb->asoc.primary_destination) &&
3820 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3821 /* Ok we need to set it */
3822 lnet = stcb->asoc.primary_destination;
3823 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3824 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3825 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3827 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3831 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3835 SCTP_TCB_UNLOCK(stcb);
3839 case SCTP_SET_DYNAMIC_PRIMARY:
3841 union sctp_sockstore *ss;
3843 error = priv_check(curthread,
3844 PRIV_NETINET_RESERVEDPORT);
3848 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3849 /* SUPER USER CHECK? */
3850 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3853 case SCTP_SET_PEER_PRIMARY_ADDR:
3855 struct sctp_setpeerprim *sspp;
3857 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3858 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3860 struct sctp_ifa *ifa;
3862 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3863 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
3865 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3869 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3871 * Must validate the ifa found is in
3874 struct sctp_laddr *laddr;
3877 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3878 if (laddr->ifa == NULL) {
3879 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3883 if (laddr->ifa == ifa) {
3889 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3894 if (sctp_set_primary_ip_address_sa(stcb,
3895 (struct sockaddr *)&sspp->sspp_addr) != 0) {
3896 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3900 SCTP_TCB_UNLOCK(stcb);
3902 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3908 case SCTP_BINDX_ADD_ADDR:
3910 struct sctp_getaddresses *addrs;
3915 td = (struct thread *)p;
3916 if (jailed(td->td_ucred)) {
3919 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3921 if (addrs->addr->sa_family == AF_INET) {
3922 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3924 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3928 if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3929 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3930 error = EADDRNOTAVAIL;
3932 } else if (addrs->addr->sa_family == AF_INET6) {
3933 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3939 /* JAIL XXXX Add else here for V6 */
3941 sctp_bindx_add_address(so, inp, addrs->addr,
3942 addrs->sget_assoc_id, vrf_id,
3946 case SCTP_BINDX_REM_ADDR:
3948 struct sctp_getaddresses *addrs;
3953 td = (struct thread *)p;
3954 if (jailed(td->td_ucred)) {
3957 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3958 if (addrs->addr->sa_family == AF_INET) {
3959 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3961 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3965 if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3966 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3967 error = EADDRNOTAVAIL;
3969 } else if (addrs->addr->sa_family == AF_INET6) {
3970 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3972 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3976 /* JAIL XXXX Add else here for V6 */
3978 sctp_bindx_delete_address(so, inp, addrs->addr,
3979 addrs->sget_assoc_id, vrf_id,
3984 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3985 error = ENOPROTOOPT;
3987 } /* end switch (opt) */
3993 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3995 void *optval = NULL;
3997 struct sctp_inpcb *inp;
4001 inp = (struct sctp_inpcb *)so->so_pcb;
4003 /* I made the same as TCP since we are not setup? */
4004 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4005 return (ECONNRESET);
4007 if (sopt->sopt_level != IPPROTO_SCTP) {
4008 /* wrong proto level... send back up to IP */
4010 if (INP_CHECK_SOCKAF(so, AF_INET6))
4011 error = ip6_ctloutput(so, sopt);
4014 error = ip_ctloutput(so, sopt);
4017 optsize = sopt->sopt_valsize;
4019 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4020 if (optval == NULL) {
4021 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4024 error = sooptcopyin(sopt, optval, optsize, optsize);
4026 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4030 p = (void *)sopt->sopt_td;
4031 if (sopt->sopt_dir == SOPT_SET) {
4032 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4033 } else if (sopt->sopt_dir == SOPT_GET) {
4034 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4036 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4039 if ((error == 0) && (optval != NULL)) {
4040 error = sooptcopyout(sopt, optval, optsize);
4041 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4042 } else if (optval != NULL) {
4043 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4051 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4054 int create_lock_on = 0;
4056 struct sctp_inpcb *inp;
4057 struct sctp_tcb *stcb = NULL;
4059 inp = (struct sctp_inpcb *)so->so_pcb;
4061 /* I made the same as TCP since we are not setup? */
4062 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4063 return (ECONNRESET);
4066 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4069 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
4070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4073 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
4074 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4077 SCTP_INP_INCR_REF(inp);
4078 SCTP_ASOC_CREATE_LOCK(inp);
4082 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4083 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4084 /* Should I really unlock ? */
4085 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4090 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4091 (addr->sa_family == AF_INET6)) {
4092 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4097 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4098 SCTP_PCB_FLAGS_UNBOUND) {
4099 /* Bind a ephemeral port */
4100 error = sctp_inpcb_bind(so, NULL, NULL, p);
4105 /* Now do we connect? */
4106 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
4107 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4111 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4112 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4113 /* We are already connected AND the TCP model */
4114 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4118 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4119 SCTP_INP_RLOCK(inp);
4120 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4121 SCTP_INP_RUNLOCK(inp);
4124 * We increment here since sctp_findassociation_ep_addr()
4125 * wil do a decrement if it finds the stcb as long as the
4126 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4128 SCTP_INP_INCR_REF(inp);
4129 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4131 SCTP_INP_DECR_REF(inp);
4133 SCTP_TCB_UNLOCK(stcb);
4137 /* Already have or am bring up an association */
4138 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4142 vrf_id = inp->def_vrf_id;
4143 /* We are GOOD to go */
4144 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4146 /* Gak! no memory */
4149 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4150 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4151 /* Set the connected flag so we can queue data */
4154 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4155 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4157 /* initialize authentication parameters for the assoc */
4158 sctp_initialize_auth_params(inp, stcb);
4160 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4161 SCTP_TCB_UNLOCK(stcb);
4163 if (create_lock_on) {
4164 SCTP_ASOC_CREATE_UNLOCK(inp);
4166 SCTP_INP_DECR_REF(inp);
4171 sctp_listen(struct socket *so, int backlog, struct thread *p)
4174 * Note this module depends on the protocol processing being called
4175 * AFTER any socket level flags and backlog are applied to the
4176 * socket. The traditional way that the socket flags are applied is
4177 * AFTER protocol processing. We have made a change to the
4178 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4179 * place if the socket API for SCTP is to work properly.
4183 struct sctp_inpcb *inp;
4185 inp = (struct sctp_inpcb *)so->so_pcb;
4187 /* I made the same as TCP since we are not setup? */
4188 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4189 return (ECONNRESET);
4191 SCTP_INP_RLOCK(inp);
4192 #ifdef SCTP_LOCK_LOGGING
4193 if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
4194 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4198 error = solisten_proto_check(so);
4201 SCTP_INP_RUNLOCK(inp);
4204 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4205 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4206 /* We are already connected AND the TCP model */
4207 SCTP_INP_RUNLOCK(inp);
4209 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4210 return (EADDRINUSE);
4212 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4213 /* We must do a bind. */
4215 SCTP_INP_RUNLOCK(inp);
4216 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4217 /* bind error, probably perm */
4223 inp->sctp_flags |= SCTP_PCB_FLAGS_LISTENING;
4225 inp->sctp_flags &= ~SCTP_PCB_FLAGS_LISTENING;
4227 SCTP_INP_RUNLOCK(inp);
4229 /* It appears for 7.0 and on, we must always call this. */
4230 solisten_proto(so, backlog);
4231 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4232 /* remove the ACCEPTCONN flag for one-to-many sockets */
4233 so->so_options &= ~SO_ACCEPTCONN;
4236 /* turning off listen */
4237 so->so_options &= ~SO_ACCEPTCONN;
4243 static int sctp_defered_wakeup_cnt = 0;
4246 sctp_accept(struct socket *so, struct sockaddr **addr)
4248 struct sctp_tcb *stcb;
4249 struct sctp_inpcb *inp;
4250 union sctp_sockstore store;
4256 inp = (struct sctp_inpcb *)so->so_pcb;
4259 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4260 return (ECONNRESET);
4262 SCTP_INP_RLOCK(inp);
4263 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4264 SCTP_INP_RUNLOCK(inp);
4265 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4266 return (EOPNOTSUPP);
4268 if (so->so_state & SS_ISDISCONNECTED) {
4269 SCTP_INP_RUNLOCK(inp);
4270 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4271 return (ECONNABORTED);
4273 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4275 SCTP_INP_RUNLOCK(inp);
4276 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4277 return (ECONNRESET);
4279 SCTP_TCB_LOCK(stcb);
4280 SCTP_INP_RUNLOCK(inp);
4281 store = stcb->asoc.primary_destination->ro._l_addr;
4282 SCTP_TCB_UNLOCK(stcb);
4283 switch (store.sa.sa_family) {
4286 struct sockaddr_in *sin;
4288 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4289 sin->sin_family = AF_INET;
4290 sin->sin_len = sizeof(*sin);
4291 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4292 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4293 *addr = (struct sockaddr *)sin;
4299 struct sockaddr_in6 *sin6;
4301 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4302 sin6->sin6_family = AF_INET6;
4303 sin6->sin6_len = sizeof(*sin6);
4304 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4306 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4307 if ((error = sa6_recoverscope(sin6)) != 0) {
4308 SCTP_FREE_SONAME(sin6);
4311 *addr = (struct sockaddr *)sin6;
4319 /* Wake any delayed sleep action */
4320 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4321 SCTP_INP_WLOCK(inp);
4322 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4323 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4324 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4325 SCTP_INP_WUNLOCK(inp);
4326 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4327 if (sowriteable(inp->sctp_socket)) {
4328 sowwakeup_locked(inp->sctp_socket);
4330 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4332 SCTP_INP_WLOCK(inp);
4334 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4335 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4336 SCTP_INP_WUNLOCK(inp);
4337 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4338 if (soreadable(inp->sctp_socket)) {
4339 sctp_defered_wakeup_cnt++;
4340 sorwakeup_locked(inp->sctp_socket);
4342 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4344 SCTP_INP_WLOCK(inp);
4346 SCTP_INP_WUNLOCK(inp);
4352 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4354 struct sockaddr_in *sin;
4356 struct sctp_inpcb *inp;
4357 struct sctp_ifa *sctp_ifa;
4360 * Do the malloc first in case it blocks.
4362 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4363 sin->sin_family = AF_INET;
4364 sin->sin_len = sizeof(*sin);
4365 inp = (struct sctp_inpcb *)so->so_pcb;
4367 SCTP_FREE_SONAME(sin);
4368 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4371 SCTP_INP_RLOCK(inp);
4372 sin->sin_port = inp->sctp_lport;
4373 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4374 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4375 struct sctp_tcb *stcb;
4376 struct sockaddr_in *sin_a;
4377 struct sctp_nets *net;
4380 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4386 SCTP_TCB_LOCK(stcb);
4387 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4388 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4390 /* this will make coverity happy */
4393 if (sin_a->sin_family == AF_INET) {
4398 if ((!fnd) || (sin_a == NULL)) {
4400 SCTP_TCB_UNLOCK(stcb);
4403 vrf_id = inp->def_vrf_id;
4404 sctp_ifa = sctp_source_address_selection(inp,
4406 (sctp_route_t *) & net->ro,
4409 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4410 sctp_free_ifa(sctp_ifa);
4412 SCTP_TCB_UNLOCK(stcb);
4414 /* For the bound all case you get back 0 */
4416 sin->sin_addr.s_addr = 0;
4420 /* Take the first IPv4 address in the list */
4421 struct sctp_laddr *laddr;
4424 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4425 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4426 struct sockaddr_in *sin_a;
4428 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4429 sin->sin_addr = sin_a->sin_addr;
4435 SCTP_FREE_SONAME(sin);
4436 SCTP_INP_RUNLOCK(inp);
4437 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4441 SCTP_INP_RUNLOCK(inp);
4442 (*addr) = (struct sockaddr *)sin;
4447 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4449 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4451 struct sockaddr_in *sin_a;
4452 struct sctp_inpcb *inp;
4453 struct sctp_tcb *stcb;
4454 struct sctp_nets *net;
4456 /* Do the malloc first in case it blocks. */
4457 inp = (struct sctp_inpcb *)so->so_pcb;
4458 if ((inp == NULL) ||
4459 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4460 /* UDP type and listeners will drop out here */
4461 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4464 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4465 sin->sin_family = AF_INET;
4466 sin->sin_len = sizeof(*sin);
4468 /* We must recapture incase we blocked */
4469 inp = (struct sctp_inpcb *)so->so_pcb;
4471 SCTP_FREE_SONAME(sin);
4472 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4475 SCTP_INP_RLOCK(inp);
4476 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4478 SCTP_TCB_LOCK(stcb);
4480 SCTP_INP_RUNLOCK(inp);
4482 SCTP_FREE_SONAME(sin);
4483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4487 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4488 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4489 if (sin_a->sin_family == AF_INET) {
4491 sin->sin_port = stcb->rport;
4492 sin->sin_addr = sin_a->sin_addr;
4496 SCTP_TCB_UNLOCK(stcb);
4498 /* No IPv4 address */
4499 SCTP_FREE_SONAME(sin);
4500 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4503 (*addr) = (struct sockaddr *)sin;
4507 struct pr_usrreqs sctp_usrreqs = {
4508 .pru_abort = sctp_abort,
4509 .pru_accept = sctp_accept,
4510 .pru_attach = sctp_attach,
4511 .pru_bind = sctp_bind,
4512 .pru_connect = sctp_connect,
4513 .pru_control = in_control,
4514 .pru_close = sctp_close,
4515 .pru_detach = sctp_close,
4516 .pru_sopoll = sopoll_generic,
4517 .pru_flush = sctp_flush,
4518 .pru_disconnect = sctp_disconnect,
4519 .pru_listen = sctp_listen,
4520 .pru_peeraddr = sctp_peeraddr,
4521 .pru_send = sctp_sendm,
4522 .pru_shutdown = sctp_shutdown,
4523 .pru_sockaddr = sctp_ingetaddr,
4524 .pru_sosend = sctp_sosend,
4525 .pru_soreceive = sctp_soreceive