2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
41 #include <netinet6/sctp6_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_bsd_addr.h>
52 #include <netinet/sctp_cc_functions.h>
60 /* Init the SCTP pcb in sctp_pcb.c */
66 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 sctp_max_chunks_on_queue = (nmbclusters / 8);
69 * Allow a user to take no more than 1/2 the number of clusters or
70 * the SB_MAX whichever is smaller for the send window.
72 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
74 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76 * Now for the recv window, should we take the same amount? or
77 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 * now I will just copy.
80 sctp_recvspace = sctp_sendspace;
87 * cleanup of the sctppcbinfo structure.
88 * Assumes that the sctppcbinfo lock is held.
91 sctp_pcbinfo_cleanup(void)
93 /* free the hash tables */
94 if (sctppcbinfo.sctp_asochash != NULL)
95 SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
96 if (sctppcbinfo.sctp_ephash != NULL)
97 SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
98 if (sctppcbinfo.sctp_tcpephash != NULL)
99 SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
100 if (sctppcbinfo.sctp_restarthash != NULL)
101 SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
106 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
107 struct sctp_tcb *stcb,
108 struct sctp_nets *net,
111 struct sctp_tmit_chunk *chk;
113 /* Adjust that too */
114 stcb->asoc.smallest_mtu = nxtsz;
115 /* now off to subtract IP_DF flag if needed */
116 #ifdef SCTP_PRINT_FOR_B_AND_M
117 SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
118 inp, stcb, net, nxtsz);
120 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
121 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
122 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
125 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
126 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
128 * For this guy we also mark for immediate resend
129 * since we sent to big of chunk
131 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
132 if (chk->sent != SCTP_DATAGRAM_RESEND) {
133 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
135 chk->sent = SCTP_DATAGRAM_RESEND;
136 chk->rec.data.doing_fast_retransmit = 0;
137 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
138 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
139 chk->whoTo->flight_size,
141 (uintptr_t) chk->whoTo,
142 chk->rec.data.TSN_seq);
144 /* Clear any time so NO RTT is being done */
146 sctp_flight_size_decrease(chk);
147 sctp_total_flight_decrease(stcb, chk);
153 sctp_notify_mbuf(struct sctp_inpcb *inp,
154 struct sctp_tcb *stcb,
155 struct sctp_nets *net,
160 int totsz, tmr_stopped = 0;
164 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165 (ip == NULL) || (sh == NULL)) {
167 SCTP_TCB_UNLOCK(stcb);
171 /* First job is to verify the vtag matches what I would send */
172 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173 SCTP_TCB_UNLOCK(stcb);
176 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
178 if (icmph->icmp_type != ICMP_UNREACH) {
179 /* We only care about unreachable */
180 SCTP_TCB_UNLOCK(stcb);
183 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184 /* not a unreachable message due to frag. */
185 SCTP_TCB_UNLOCK(stcb);
190 nxtsz = ntohs(icmph->icmp_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_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
831 asoc->primary_destination);
832 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
834 asoc->primary_destination);
838 * we still got (or just got) data to send,
839 * so set SHUTDOWN_PENDING
842 * XXX sockets draft says that SCTP_EOF
843 * should be sent with no data. currently,
844 * we will allow user data to be sent first
845 * and move to SHUTDOWN-PENDING
847 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
848 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
849 asoc->primary_destination);
850 if (asoc->locked_on_sending) {
851 /* Locked to send out the data */
852 struct sctp_stream_queue_pending *sp;
854 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
856 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
857 asoc->locked_on_sending->stream_no);
859 if ((sp->length == 0) && (sp->msg_is_complete == 0))
860 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
863 if (TAILQ_EMPTY(&asoc->send_queue) &&
864 TAILQ_EMPTY(&asoc->sent_queue) &&
865 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
869 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
870 0, M_DONTWAIT, 1, MT_DATA);
876 struct sctp_paramhdr *ph;
879 SCTP_BUF_LEN(op_err) =
880 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
882 struct sctp_paramhdr *);
883 ph->param_type = htons(
884 SCTP_CAUSE_USER_INITIATED_ABT);
885 ph->param_length = htons(SCTP_BUF_LEN(op_err));
886 ippp = (uint32_t *) (ph + 1);
887 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
889 #if defined(SCTP_PANIC_ON_ABORT)
890 panic("disconnect does an abort");
893 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
894 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
895 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
896 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
897 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
898 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
900 SCTP_INP_RUNLOCK(inp);
901 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
904 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
907 SCTP_TCB_UNLOCK(stcb);
908 SCTP_INP_RUNLOCK(inp);
913 /* UDP model does not support this */
914 SCTP_INP_RUNLOCK(inp);
915 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
921 sctp_shutdown(struct socket *so)
923 struct sctp_inpcb *inp;
925 inp = (struct sctp_inpcb *)so->so_pcb;
927 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
931 /* For UDP model this is a invalid call */
932 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
933 /* Restore the flags that the soshutdown took away. */
934 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
935 /* This proc will wakeup for read and do nothing (I hope) */
936 SCTP_INP_RUNLOCK(inp);
937 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
941 * Ok if we reach here its the TCP model and it is either a SHUT_WR
942 * or SHUT_RDWR. This means we put the shutdown flag against it.
945 struct sctp_tcb *stcb;
946 struct sctp_association *asoc;
950 stcb = LIST_FIRST(&inp->sctp_asoc_list);
953 * Ok we hit the case that the shutdown call was
954 * made after an abort or something. Nothing to do
957 SCTP_INP_RUNLOCK(inp);
962 if (TAILQ_EMPTY(&asoc->send_queue) &&
963 TAILQ_EMPTY(&asoc->sent_queue) &&
964 (asoc->stream_queue_cnt == 0)) {
965 if (asoc->locked_on_sending) {
968 /* there is nothing queued to send, so I'm done... */
969 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
970 /* only send SHUTDOWN the first time through */
971 sctp_stop_timers_for_shutdown(stcb);
972 sctp_send_shutdown(stcb,
973 stcb->asoc.primary_destination);
974 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
975 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
976 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
977 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
979 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
980 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
982 asoc->primary_destination);
983 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
985 asoc->primary_destination);
989 * we still got (or just got) data to send, so set
992 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
993 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
994 asoc->primary_destination);
996 if (asoc->locked_on_sending) {
997 /* Locked to send out the data */
998 struct sctp_stream_queue_pending *sp;
1000 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1002 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1003 asoc->locked_on_sending->stream_no);
1005 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1006 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1010 if (TAILQ_EMPTY(&asoc->send_queue) &&
1011 TAILQ_EMPTY(&asoc->sent_queue) &&
1012 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1013 struct mbuf *op_err;
1016 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1017 0, M_DONTWAIT, 1, MT_DATA);
1019 /* Fill in the user initiated abort */
1020 struct sctp_paramhdr *ph;
1023 SCTP_BUF_LEN(op_err) =
1024 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1026 struct sctp_paramhdr *);
1027 ph->param_type = htons(
1028 SCTP_CAUSE_USER_INITIATED_ABT);
1029 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1030 ippp = (uint32_t *) (ph + 1);
1031 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1033 #if defined(SCTP_PANIC_ON_ABORT)
1034 panic("shutdown does an abort");
1036 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1037 sctp_abort_an_association(stcb->sctp_ep, stcb,
1038 SCTP_RESPONSE_TO_USER_REQ,
1039 op_err, SCTP_SO_LOCKED);
1042 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1045 SCTP_TCB_UNLOCK(stcb);
1048 SCTP_INP_RUNLOCK(inp);
1053 * copies a "user" presentable address and removes embedded scope, etc.
1054 * returns 0 on success, 1 on error
1057 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1059 struct sockaddr_in6 lsa6;
1061 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1063 memcpy(ss, sa, sa->sa_len);
1070 * NOTE: assumes addr lock is held
1073 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1074 struct sctp_tcb *stcb,
1076 struct sockaddr_storage *sas,
1079 struct sctp_ifn *sctp_ifn;
1080 struct sctp_ifa *sctp_ifa;
1081 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1083 int ipv4_addr_legal, ipv6_addr_legal;
1084 struct sctp_vrf *vrf;
1091 /* Turn on all the appropriate scope */
1092 loopback_scope = stcb->asoc.loopback_scope;
1093 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1094 local_scope = stcb->asoc.local_scope;
1095 site_scope = stcb->asoc.site_scope;
1097 /* Turn on ALL scope, since we look at the EP */
1098 loopback_scope = ipv4_local_scope = local_scope =
1101 ipv4_addr_legal = ipv6_addr_legal = 0;
1102 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1103 ipv6_addr_legal = 1;
1104 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1105 ipv4_addr_legal = 1;
1108 ipv4_addr_legal = 1;
1110 vrf = sctp_find_vrf(vrf_id);
1114 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1115 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1116 if ((loopback_scope == 0) &&
1117 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1118 /* Skip loopback if loopback_scope not set */
1121 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1124 * For the BOUND-ALL case, the list
1125 * associated with a TCB is Always
1126 * considered a reverse list.. i.e.
1127 * it lists addresses that are NOT
1128 * part of the association. If this
1129 * is one of those we must skip it.
1131 if (sctp_is_addr_restricted(stcb,
1136 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1137 (ipv4_addr_legal)) {
1138 struct sockaddr_in *sin;
1140 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1141 if (sin->sin_addr.s_addr == 0) {
1143 * we skip unspecifed
1148 if ((ipv4_local_scope == 0) &&
1149 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1152 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1153 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1154 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1155 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1156 actual += sizeof(struct sockaddr_in6);
1158 memcpy(sas, sin, sizeof(*sin));
1159 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1160 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1161 actual += sizeof(*sin);
1163 if (actual >= limit) {
1166 } else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1167 (ipv6_addr_legal)) {
1168 struct sockaddr_in6 *sin6;
1170 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1171 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1173 * we skip unspecifed
1178 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1179 if (local_scope == 0)
1181 if (sin6->sin6_scope_id == 0) {
1182 if (sa6_recoverscope(sin6) != 0)
1191 if ((site_scope == 0) &&
1192 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1195 memcpy(sas, sin6, sizeof(*sin6));
1196 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1197 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1198 actual += sizeof(*sin6);
1199 if (actual >= limit) {
1206 struct sctp_laddr *laddr;
1208 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1210 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1214 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1217 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1218 sas = (struct sockaddr_storage *)((caddr_t)sas +
1219 laddr->ifa->address.sa.sa_len);
1220 actual += laddr->ifa->address.sa.sa_len;
1221 if (actual >= limit) {
1230 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1231 struct sctp_tcb *stcb,
1233 struct sockaddr_storage *sas)
1237 SCTP_IPI_ADDR_RLOCK();
1238 /* fill up addresses for the endpoint's default vrf */
1239 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1241 SCTP_IPI_ADDR_RUNLOCK();
1246 * NOTE: assumes addr lock is held
1249 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1252 struct sctp_vrf *vrf = NULL;
1255 * In both sub-set bound an bound_all cases we return the MAXIMUM
1256 * number of addresses that you COULD get. In reality the sub-set
1257 * bound may have an exclusion list for a given TCB OR in the
1258 * bound-all case a TCB may NOT include the loopback or other
1259 * addresses as well.
1261 vrf = sctp_find_vrf(vrf_id);
1265 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1266 struct sctp_ifn *sctp_ifn;
1267 struct sctp_ifa *sctp_ifa;
1269 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1270 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1271 /* Count them if they are the right type */
1272 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1273 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1274 cnt += sizeof(struct sockaddr_in6);
1276 cnt += sizeof(struct sockaddr_in);
1278 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1279 cnt += sizeof(struct sockaddr_in6);
1283 struct sctp_laddr *laddr;
1285 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1286 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1287 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1288 cnt += sizeof(struct sockaddr_in6);
1290 cnt += sizeof(struct sockaddr_in);
1292 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1293 cnt += sizeof(struct sockaddr_in6);
1300 sctp_count_max_addresses(struct sctp_inpcb *inp)
1304 SCTP_IPI_ADDR_RLOCK();
1305 /* count addresses for the endpoint's default VRF */
1306 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1307 SCTP_IPI_ADDR_RUNLOCK();
1312 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1313 size_t optsize, void *p, int delay)
1316 int creat_lock_on = 0;
1317 struct sctp_tcb *stcb = NULL;
1318 struct sockaddr *sa;
1319 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1322 int bad_addresses = 0;
1325 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1327 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1328 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1329 /* We are already connected AND the TCP model */
1330 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1331 return (EADDRINUSE);
1333 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1334 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1337 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1338 SCTP_INP_RLOCK(inp);
1339 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1340 SCTP_INP_RUNLOCK(inp);
1343 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1346 SCTP_INP_INCR_REF(inp);
1347 SCTP_ASOC_CREATE_LOCK(inp);
1349 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1350 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1351 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1355 totaddrp = (int *)optval;
1356 totaddr = *totaddrp;
1357 sa = (struct sockaddr *)(totaddrp + 1);
1358 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1359 if ((stcb != NULL) || bad_addresses) {
1360 /* Already have or am bring up an association */
1361 SCTP_ASOC_CREATE_UNLOCK(inp);
1364 SCTP_TCB_UNLOCK(stcb);
1365 if (bad_addresses == 0) {
1366 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1372 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1377 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1379 struct in6pcb *inp6;
1381 inp6 = (struct in6pcb *)inp;
1382 if (SCTP_IPV6_V6ONLY(inp6)) {
1384 * if IPV6_V6ONLY flag, ignore connections destined
1385 * to a v4 addr or v4-mapped addr
1387 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1393 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1394 SCTP_PCB_FLAGS_UNBOUND) {
1395 /* Bind a ephemeral port */
1396 error = sctp_inpcb_bind(so, NULL, NULL, p);
1401 /* FIX ME: do we want to pass in a vrf on the connect call? */
1402 vrf_id = inp->def_vrf_id;
1404 /* We are GOOD to go */
1405 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1409 /* Gak! no memory */
1412 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1413 /* move to second address */
1414 if (sa->sa_family == AF_INET)
1415 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1417 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1420 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1421 /* Fill in the return id */
1423 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1426 a_id = (sctp_assoc_t *) optval;
1427 *a_id = sctp_get_associd(stcb);
1429 /* initialize authentication parameters for the assoc */
1430 sctp_initialize_auth_params(inp, stcb);
1433 /* doing delayed connection */
1434 stcb->asoc.delayed_connection = 1;
1435 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1437 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1438 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1440 SCTP_TCB_UNLOCK(stcb);
1441 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1442 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1443 /* Set the connected flag so we can queue data */
1447 if (creat_lock_on) {
1448 SCTP_ASOC_CREATE_UNLOCK(inp);
1450 SCTP_INP_DECR_REF(inp);
1454 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1455 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1456 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1457 SCTP_INP_RLOCK(inp); \
1458 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1460 SCTP_TCB_LOCK(stcb); \
1462 SCTP_INP_RUNLOCK(inp); \
1463 } else if (assoc_id != 0) { \
1464 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1465 if (stcb == NULL) { \
1466 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1476 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1477 if (size < sizeof(type)) { \
1478 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1482 destp = (type *)srcp; \
1487 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1490 struct sctp_inpcb *inp = NULL;
1492 struct sctp_tcb *stcb = NULL;
1494 if (optval == NULL) {
1495 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1498 inp = (struct sctp_inpcb *)so->so_pcb;
1500 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1507 case SCTP_AUTOCLOSE:
1508 case SCTP_EXPLICIT_EOR:
1509 case SCTP_AUTO_ASCONF:
1510 case SCTP_DISABLE_FRAGMENTS:
1511 case SCTP_I_WANT_MAPPED_V4_ADDR:
1512 case SCTP_USE_EXT_RCVINFO:
1513 SCTP_INP_RLOCK(inp);
1515 case SCTP_DISABLE_FRAGMENTS:
1516 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1518 case SCTP_I_WANT_MAPPED_V4_ADDR:
1519 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1521 case SCTP_AUTO_ASCONF:
1522 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1523 /* only valid for bound all sockets */
1524 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1526 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1531 case SCTP_EXPLICIT_EOR:
1532 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1535 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1537 case SCTP_USE_EXT_RCVINFO:
1538 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1540 case SCTP_AUTOCLOSE:
1541 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1542 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1548 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1549 error = ENOPROTOOPT;
1550 } /* end switch (sopt->sopt_name) */
1551 if (optname != SCTP_AUTOCLOSE) {
1552 /* make it an "on/off" value */
1555 if (*optsize < sizeof(val)) {
1556 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1560 SCTP_INP_RUNLOCK(inp);
1562 /* return the option value */
1563 *(int *)optval = val;
1564 *optsize = sizeof(val);
1567 case SCTP_GET_PACKET_LOG:
1569 #ifdef SCTP_PACKET_LOGGING
1573 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1574 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1577 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1582 case SCTP_PARTIAL_DELIVERY_POINT:
1586 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1587 *value = inp->partial_delivery_point;
1588 *optsize = sizeof(uint32_t);
1591 case SCTP_FRAGMENT_INTERLEAVE:
1595 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1596 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1597 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1598 *value = SCTP_FRAG_LEVEL_2;
1600 *value = SCTP_FRAG_LEVEL_1;
1603 *value = SCTP_FRAG_LEVEL_0;
1605 *optsize = sizeof(uint32_t);
1608 case SCTP_CMT_ON_OFF:
1610 struct sctp_assoc_value *av;
1612 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1613 if (sctp_cmt_on_off) {
1614 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1616 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1617 SCTP_TCB_UNLOCK(stcb);
1620 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1624 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1625 error = ENOPROTOOPT;
1627 *optsize = sizeof(*av);
1630 /* JRS - Get socket option for pluggable congestion control */
1631 case SCTP_PLUGGABLE_CC:
1633 struct sctp_assoc_value *av;
1635 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1636 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1638 av->assoc_value = stcb->asoc.congestion_control_module;
1639 SCTP_TCB_UNLOCK(stcb);
1641 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1643 *optsize = sizeof(*av);
1646 case SCTP_GET_ADDR_LEN:
1648 struct sctp_assoc_value *av;
1650 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1653 if (av->assoc_value == AF_INET) {
1654 av->assoc_value = sizeof(struct sockaddr_in);
1659 if (av->assoc_value == AF_INET6) {
1660 av->assoc_value = sizeof(struct sockaddr_in6);
1665 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1667 *optsize = sizeof(*av);
1670 case SCTP_GET_ASSOC_NUMBER:
1672 uint32_t *value, cnt;
1674 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1676 SCTP_INP_RLOCK(inp);
1677 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1680 SCTP_INP_RUNLOCK(inp);
1682 *optsize = sizeof(uint32_t);
1686 case SCTP_GET_ASSOC_ID_LIST:
1688 struct sctp_assoc_ids *ids;
1689 unsigned int at, limit;
1691 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1693 limit = *optsize / sizeof(sctp_assoc_t);
1694 SCTP_INP_RLOCK(inp);
1695 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1697 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1700 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1704 SCTP_INP_RUNLOCK(inp);
1705 *optsize = at * sizeof(sctp_assoc_t);
1710 struct sctp_assoc_value *av;
1712 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1713 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1716 av->assoc_value = stcb->asoc.context;
1717 SCTP_TCB_UNLOCK(stcb);
1719 SCTP_INP_RLOCK(inp);
1720 av->assoc_value = inp->sctp_context;
1721 SCTP_INP_RUNLOCK(inp);
1723 *optsize = sizeof(*av);
1728 uint32_t *default_vrfid;
1730 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1731 *default_vrfid = inp->def_vrf_id;
1734 case SCTP_GET_ASOC_VRF:
1736 struct sctp_assoc_value *id;
1738 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1739 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1745 id->assoc_value = stcb->asoc.vrf_id;
1748 case SCTP_GET_VRF_IDS:
1750 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1754 case SCTP_GET_NONCE_VALUES:
1756 struct sctp_get_nonce_values *gnv;
1758 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1759 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1762 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1763 gnv->gn_local_tag = stcb->asoc.my_vtag;
1764 SCTP_TCB_UNLOCK(stcb);
1766 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1769 *optsize = sizeof(*gnv);
1772 case SCTP_DELAYED_SACK:
1774 struct sctp_sack_info *sack;
1776 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1777 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1779 sack->sack_delay = stcb->asoc.delayed_ack;
1780 sack->sack_freq = stcb->asoc.sack_freq;
1781 SCTP_TCB_UNLOCK(stcb);
1783 SCTP_INP_RLOCK(inp);
1784 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1785 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1786 SCTP_INP_RUNLOCK(inp);
1788 *optsize = sizeof(*sack);
1792 case SCTP_GET_SNDBUF_USE:
1794 struct sctp_sockstat *ss;
1796 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1797 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1800 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1801 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1802 stcb->asoc.size_on_all_streams);
1803 SCTP_TCB_UNLOCK(stcb);
1805 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1808 *optsize = sizeof(struct sctp_sockstat);
1811 case SCTP_MAX_BURST:
1815 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1817 SCTP_INP_RLOCK(inp);
1818 *value = inp->sctp_ep.max_burst;
1819 SCTP_INP_RUNLOCK(inp);
1820 *optsize = sizeof(uint8_t);
1825 struct sctp_assoc_value *av;
1828 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1829 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1832 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1833 SCTP_TCB_UNLOCK(stcb);
1835 SCTP_INP_RLOCK(inp);
1836 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1837 ovh = SCTP_MED_OVERHEAD;
1839 ovh = SCTP_MED_V4_OVERHEAD;
1841 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1842 av->assoc_value = 0;
1844 av->assoc_value = inp->sctp_frag_point - ovh;
1845 SCTP_INP_RUNLOCK(inp);
1847 *optsize = sizeof(struct sctp_assoc_value);
1850 case SCTP_GET_STAT_LOG:
1851 error = sctp_fill_stat_log(optval, optsize);
1855 struct sctp_event_subscribe *events;
1857 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1858 memset(events, 0, sizeof(*events));
1859 SCTP_INP_RLOCK(inp);
1860 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1861 events->sctp_data_io_event = 1;
1863 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1864 events->sctp_association_event = 1;
1866 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1867 events->sctp_address_event = 1;
1869 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1870 events->sctp_send_failure_event = 1;
1872 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1873 events->sctp_peer_error_event = 1;
1875 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1876 events->sctp_shutdown_event = 1;
1878 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1879 events->sctp_partial_delivery_event = 1;
1881 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1882 events->sctp_adaptation_layer_event = 1;
1884 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1885 events->sctp_authentication_event = 1;
1887 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1888 events->sctp_stream_reset_events = 1;
1889 SCTP_INP_RUNLOCK(inp);
1890 *optsize = sizeof(struct sctp_event_subscribe);
1894 case SCTP_ADAPTATION_LAYER:
1898 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1900 SCTP_INP_RLOCK(inp);
1901 *value = inp->sctp_ep.adaptation_layer_indicator;
1902 SCTP_INP_RUNLOCK(inp);
1903 *optsize = sizeof(uint32_t);
1906 case SCTP_SET_INITIAL_DBG_SEQ:
1910 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1911 SCTP_INP_RLOCK(inp);
1912 *value = inp->sctp_ep.initial_sequence_debug;
1913 SCTP_INP_RUNLOCK(inp);
1914 *optsize = sizeof(uint32_t);
1917 case SCTP_GET_LOCAL_ADDR_SIZE:
1921 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1922 SCTP_INP_RLOCK(inp);
1923 *value = sctp_count_max_addresses(inp);
1924 SCTP_INP_RUNLOCK(inp);
1925 *optsize = sizeof(uint32_t);
1928 case SCTP_GET_REMOTE_ADDR_SIZE:
1932 struct sctp_nets *net;
1934 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1935 /* FIXME MT: change to sctp_assoc_value? */
1936 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1940 /* Count the sizes */
1941 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1942 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1943 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1944 size += sizeof(struct sockaddr_in6);
1945 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1946 size += sizeof(struct sockaddr_in);
1952 SCTP_TCB_UNLOCK(stcb);
1953 *value = (uint32_t) size;
1955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1958 *optsize = sizeof(uint32_t);
1961 case SCTP_GET_PEER_ADDRESSES:
1963 * Get the address information, an array is passed in to
1964 * fill up we pack it.
1968 struct sockaddr_storage *sas;
1969 struct sctp_nets *net;
1970 struct sctp_getaddresses *saddr;
1972 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1973 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1976 left = (*optsize) - sizeof(struct sctp_getaddresses);
1977 *optsize = sizeof(struct sctp_getaddresses);
1978 sas = (struct sockaddr_storage *)&saddr->addr[0];
1980 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1981 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1982 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1983 cpsz = sizeof(struct sockaddr_in6);
1984 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1985 cpsz = sizeof(struct sockaddr_in);
1991 /* not enough room. */
1994 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1995 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1996 /* Must map the address */
1997 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1998 (struct sockaddr_in6 *)sas);
2000 memcpy(sas, &net->ro._l_addr, cpsz);
2002 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2004 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2008 SCTP_TCB_UNLOCK(stcb);
2010 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2015 case SCTP_GET_LOCAL_ADDRESSES:
2017 size_t limit, actual;
2018 struct sockaddr_storage *sas;
2019 struct sctp_getaddresses *saddr;
2021 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2022 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2024 sas = (struct sockaddr_storage *)&saddr->addr[0];
2025 limit = *optsize - sizeof(sctp_assoc_t);
2026 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2028 SCTP_TCB_UNLOCK(stcb);
2030 *optsize = sizeof(struct sockaddr_storage) + actual;
2033 case SCTP_PEER_ADDR_PARAMS:
2035 struct sctp_paddrparams *paddrp;
2036 struct sctp_nets *net;
2038 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2039 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2043 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2046 * We increment here since
2047 * sctp_findassociation_ep_addr() wil do a
2048 * decrement if it finds the stcb as long as
2049 * the locked tcb (last argument) is NOT a
2052 SCTP_INP_INCR_REF(inp);
2053 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2055 SCTP_INP_DECR_REF(inp);
2058 if (stcb && (net == NULL)) {
2059 struct sockaddr *sa;
2061 sa = (struct sockaddr *)&paddrp->spp_address;
2062 if (sa->sa_family == AF_INET) {
2063 struct sockaddr_in *sin;
2065 sin = (struct sockaddr_in *)sa;
2066 if (sin->sin_addr.s_addr) {
2068 SCTP_TCB_UNLOCK(stcb);
2069 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2072 } else if (sa->sa_family == AF_INET6) {
2073 struct sockaddr_in6 *sin6;
2075 sin6 = (struct sockaddr_in6 *)sa;
2076 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2078 SCTP_TCB_UNLOCK(stcb);
2079 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2083 error = EAFNOSUPPORT;
2084 SCTP_TCB_UNLOCK(stcb);
2085 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2090 /* Applys to the specific association */
2091 paddrp->spp_flags = 0;
2095 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2096 ovh = SCTP_MED_OVERHEAD;
2098 ovh = SCTP_MED_V4_OVERHEAD;
2102 paddrp->spp_pathmaxrxt = net->failure_threshold;
2103 paddrp->spp_pathmtu = net->mtu - ovh;
2104 /* get flags for HB */
2105 if (net->dest_state & SCTP_ADDR_NOHB)
2106 paddrp->spp_flags |= SPP_HB_DISABLE;
2108 paddrp->spp_flags |= SPP_HB_ENABLE;
2109 /* get flags for PMTU */
2110 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2111 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2113 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2116 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2117 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2118 paddrp->spp_flags |= SPP_IPV4_TOS;
2122 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2123 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2124 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2129 * No destination so return default
2134 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2135 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2137 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2138 paddrp->spp_flags |= SPP_IPV4_TOS;
2141 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2142 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2144 /* default settings should be these */
2145 if (stcb->asoc.hb_is_disabled == 0) {
2146 paddrp->spp_flags |= SPP_HB_ENABLE;
2148 paddrp->spp_flags |= SPP_HB_DISABLE;
2150 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2151 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2156 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2159 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2160 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2161 SCTP_TCB_UNLOCK(stcb);
2163 /* Use endpoint defaults */
2164 SCTP_INP_RLOCK(inp);
2165 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2166 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2167 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2168 /* get inp's default */
2170 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2171 paddrp->spp_flags |= SPP_IPV4_TOS;
2174 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2175 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2176 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2179 /* can't return this */
2180 paddrp->spp_pathmtu = 0;
2182 /* default behavior, no stcb */
2183 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2185 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2186 paddrp->spp_flags |= SPP_HB_ENABLE;
2188 paddrp->spp_flags |= SPP_HB_DISABLE;
2190 SCTP_INP_RUNLOCK(inp);
2192 *optsize = sizeof(struct sctp_paddrparams);
2195 case SCTP_GET_PEER_ADDR_INFO:
2197 struct sctp_paddrinfo *paddri;
2198 struct sctp_nets *net;
2200 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2201 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2205 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2208 * We increment here since
2209 * sctp_findassociation_ep_addr() wil do a
2210 * decrement if it finds the stcb as long as
2211 * the locked tcb (last argument) is NOT a
2214 SCTP_INP_INCR_REF(inp);
2215 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2217 SCTP_INP_DECR_REF(inp);
2221 if ((stcb) && (net)) {
2222 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2223 paddri->spinfo_cwnd = net->cwnd;
2224 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2225 paddri->spinfo_rto = net->RTO;
2226 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2227 SCTP_TCB_UNLOCK(stcb);
2230 SCTP_TCB_UNLOCK(stcb);
2232 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2235 *optsize = sizeof(struct sctp_paddrinfo);
2238 case SCTP_PCB_STATUS:
2240 struct sctp_pcbinfo *spcb;
2242 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2243 sctp_fill_pcbinfo(spcb);
2244 *optsize = sizeof(struct sctp_pcbinfo);
2250 struct sctp_nets *net;
2251 struct sctp_status *sstat;
2253 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2254 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2257 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2262 * I think passing the state is fine since
2263 * sctp_constants.h will be available to the user
2266 sstat->sstat_state = stcb->asoc.state;
2267 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2268 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2270 * We can't include chunks that have been passed to
2271 * the socket layer. Only things in queue.
2273 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2274 stcb->asoc.cnt_on_all_streams);
2277 sstat->sstat_instrms = stcb->asoc.streamincnt;
2278 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2279 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2280 memcpy(&sstat->sstat_primary.spinfo_address,
2281 &stcb->asoc.primary_destination->ro._l_addr,
2282 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2283 net = stcb->asoc.primary_destination;
2284 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2286 * Again the user can get info from sctp_constants.h
2287 * for what the state of the network is.
2289 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2290 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2291 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2292 sstat->sstat_primary.spinfo_rto = net->RTO;
2293 sstat->sstat_primary.spinfo_mtu = net->mtu;
2294 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2295 SCTP_TCB_UNLOCK(stcb);
2296 *optsize = sizeof(*sstat);
2301 struct sctp_rtoinfo *srto;
2303 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2304 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2307 srto->srto_initial = stcb->asoc.initial_rto;
2308 srto->srto_max = stcb->asoc.maxrto;
2309 srto->srto_min = stcb->asoc.minrto;
2310 SCTP_TCB_UNLOCK(stcb);
2312 SCTP_INP_RLOCK(inp);
2313 srto->srto_initial = inp->sctp_ep.initial_rto;
2314 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2315 srto->srto_min = inp->sctp_ep.sctp_minrto;
2316 SCTP_INP_RUNLOCK(inp);
2318 *optsize = sizeof(*srto);
2321 case SCTP_ASSOCINFO:
2323 struct sctp_assocparams *sasoc;
2326 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2327 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2330 oldval = sasoc->sasoc_cookie_life;
2331 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2332 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2333 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2334 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2335 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2336 SCTP_TCB_UNLOCK(stcb);
2338 SCTP_INP_RLOCK(inp);
2339 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2340 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2341 sasoc->sasoc_number_peer_destinations = 0;
2342 sasoc->sasoc_peer_rwnd = 0;
2343 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2344 SCTP_INP_RUNLOCK(inp);
2346 *optsize = sizeof(*sasoc);
2349 case SCTP_DEFAULT_SEND_PARAM:
2351 struct sctp_sndrcvinfo *s_info;
2353 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2354 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2357 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2358 SCTP_TCB_UNLOCK(stcb);
2360 SCTP_INP_RLOCK(inp);
2361 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2362 SCTP_INP_RUNLOCK(inp);
2364 *optsize = sizeof(*s_info);
2369 struct sctp_initmsg *sinit;
2371 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2372 SCTP_INP_RLOCK(inp);
2373 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2374 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2375 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2376 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2377 SCTP_INP_RUNLOCK(inp);
2378 *optsize = sizeof(*sinit);
2381 case SCTP_PRIMARY_ADDR:
2382 /* we allow a "get" operation on this */
2384 struct sctp_setprim *ssp;
2386 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2387 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2390 /* simply copy out the sockaddr_storage... */
2394 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2395 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2397 memcpy(&ssp->ssp_addr,
2398 &stcb->asoc.primary_destination->ro._l_addr,
2400 SCTP_TCB_UNLOCK(stcb);
2402 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2405 *optsize = sizeof(*ssp);
2409 case SCTP_HMAC_IDENT:
2411 struct sctp_hmacalgo *shmac;
2412 sctp_hmaclist_t *hmaclist;
2416 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2418 SCTP_INP_RLOCK(inp);
2419 hmaclist = inp->sctp_ep.local_hmacs;
2420 if (hmaclist == NULL) {
2421 /* no HMACs to return */
2422 *optsize = sizeof(*shmac);
2423 SCTP_INP_RUNLOCK(inp);
2426 /* is there room for all of the hmac ids? */
2427 size = sizeof(*shmac) + (hmaclist->num_algo *
2428 sizeof(shmac->shmac_idents[0]));
2429 if ((size_t)(*optsize) < size) {
2430 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2432 SCTP_INP_RUNLOCK(inp);
2435 /* copy in the list */
2436 for (i = 0; i < hmaclist->num_algo; i++)
2437 shmac->shmac_idents[i] = hmaclist->hmac[i];
2438 SCTP_INP_RUNLOCK(inp);
2442 case SCTP_AUTH_ACTIVE_KEY:
2444 struct sctp_authkeyid *scact;
2446 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2447 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2450 /* get the active key on the assoc */
2451 scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2452 SCTP_TCB_UNLOCK(stcb);
2454 /* get the endpoint active key */
2455 SCTP_INP_RLOCK(inp);
2456 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2457 SCTP_INP_RUNLOCK(inp);
2459 *optsize = sizeof(*scact);
2462 case SCTP_LOCAL_AUTH_CHUNKS:
2464 struct sctp_authchunks *sac;
2465 sctp_auth_chklist_t *chklist = NULL;
2468 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2469 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2472 /* get off the assoc */
2473 chklist = stcb->asoc.local_auth_chunks;
2474 /* is there enough space? */
2475 size = sctp_auth_get_chklist_size(chklist);
2476 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2478 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2480 /* copy in the chunks */
2481 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2483 SCTP_TCB_UNLOCK(stcb);
2485 /* get off the endpoint */
2486 SCTP_INP_RLOCK(inp);
2487 chklist = inp->sctp_ep.local_auth_chunks;
2488 /* is there enough space? */
2489 size = sctp_auth_get_chklist_size(chklist);
2490 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2492 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2494 /* copy in the chunks */
2495 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2497 SCTP_INP_RUNLOCK(inp);
2499 *optsize = sizeof(struct sctp_authchunks) + size;
2502 case SCTP_PEER_AUTH_CHUNKS:
2504 struct sctp_authchunks *sac;
2505 sctp_auth_chklist_t *chklist = NULL;
2508 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2509 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2512 /* get off the assoc */
2513 chklist = stcb->asoc.peer_auth_chunks;
2514 /* is there enough space? */
2515 size = sctp_auth_get_chklist_size(chklist);
2516 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2518 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2520 /* copy in the chunks */
2521 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2523 SCTP_TCB_UNLOCK(stcb);
2525 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2528 *optsize = sizeof(struct sctp_authchunks) + size;
2534 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2535 error = ENOPROTOOPT;
2538 } /* end switch (sopt->sopt_name) */
2543 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2548 struct sctp_tcb *stcb = NULL;
2549 struct sctp_inpcb *inp = NULL;
2552 if (optval == NULL) {
2553 SCTP_PRINTF("optval is NULL\n");
2554 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2557 inp = (struct sctp_inpcb *)so->so_pcb;
2559 SCTP_PRINTF("inp is NULL?\n");
2560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2563 vrf_id = inp->def_vrf_id;
2568 case SCTP_AUTOCLOSE:
2569 case SCTP_AUTO_ASCONF:
2570 case SCTP_EXPLICIT_EOR:
2571 case SCTP_DISABLE_FRAGMENTS:
2572 case SCTP_USE_EXT_RCVINFO:
2573 case SCTP_I_WANT_MAPPED_V4_ADDR:
2574 /* copy in the option value */
2575 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2580 case SCTP_DISABLE_FRAGMENTS:
2581 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2583 case SCTP_AUTO_ASCONF:
2585 * NOTE: we don't really support this flag
2587 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2588 /* only valid for bound all sockets */
2589 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2591 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2595 case SCTP_EXPLICIT_EOR:
2596 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2598 case SCTP_USE_EXT_RCVINFO:
2599 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2601 case SCTP_I_WANT_MAPPED_V4_ADDR:
2602 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2603 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2605 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2610 set_opt = SCTP_PCB_FLAGS_NODELAY;
2612 case SCTP_AUTOCLOSE:
2613 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2614 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2615 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2618 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2620 * The value is in ticks. Note this does not effect
2621 * old associations, only new ones.
2623 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2626 SCTP_INP_WLOCK(inp);
2628 sctp_feature_on(inp, set_opt);
2630 sctp_feature_off(inp, set_opt);
2632 SCTP_INP_WUNLOCK(inp);
2634 case SCTP_PARTIAL_DELIVERY_POINT:
2638 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2639 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2640 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2644 inp->partial_delivery_point = *value;
2647 case SCTP_FRAGMENT_INTERLEAVE:
2648 /* not yet until we re-write sctp_recvmsg() */
2652 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2653 if (*level == SCTP_FRAG_LEVEL_2) {
2654 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2655 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2656 } else if (*level == SCTP_FRAG_LEVEL_1) {
2657 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2658 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2659 } else if (*level == SCTP_FRAG_LEVEL_0) {
2660 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2661 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2664 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2669 case SCTP_CMT_ON_OFF:
2671 struct sctp_assoc_value *av;
2673 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2674 if (sctp_cmt_on_off) {
2675 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2677 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2678 SCTP_TCB_UNLOCK(stcb);
2680 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2684 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2685 error = ENOPROTOOPT;
2689 /* JRS - Set socket option for pluggable congestion control */
2690 case SCTP_PLUGGABLE_CC:
2692 struct sctp_assoc_value *av;
2694 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2695 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2697 switch (av->assoc_value) {
2699 * JRS - Standard TCP congestion
2702 case SCTP_CC_RFC2581:
2704 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2705 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2706 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2707 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2708 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2709 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2710 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2711 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2712 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2713 SCTP_TCB_UNLOCK(stcb);
2717 * JRS - High Speed TCP congestion
2722 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2723 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2724 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2725 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2726 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2727 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2728 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2729 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2730 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2731 SCTP_TCB_UNLOCK(stcb);
2734 /* JRS - HTCP congestion control */
2737 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2738 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2739 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2740 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2741 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2742 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2743 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2744 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2745 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2746 SCTP_TCB_UNLOCK(stcb);
2750 * JRS - All other values are
2755 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2757 SCTP_TCB_UNLOCK(stcb);
2762 switch (av->assoc_value) {
2763 case SCTP_CC_RFC2581:
2766 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2769 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2776 case SCTP_CLR_STAT_LOG:
2777 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2782 struct sctp_assoc_value *av;
2784 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2785 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2788 stcb->asoc.context = av->assoc_value;
2789 SCTP_TCB_UNLOCK(stcb);
2791 SCTP_INP_WLOCK(inp);
2792 inp->sctp_context = av->assoc_value;
2793 SCTP_INP_WUNLOCK(inp);
2799 uint32_t *default_vrfid;
2801 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2802 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2807 inp->def_vrf_id = *default_vrfid;
2810 case SCTP_DEL_VRF_ID:
2812 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2816 case SCTP_ADD_VRF_ID:
2818 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2822 case SCTP_DELAYED_SACK:
2824 struct sctp_sack_info *sack;
2826 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2827 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2828 if (sack->sack_delay) {
2829 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2830 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2833 if (sack->sack_delay) {
2834 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2835 sack->sack_delay = TICKS_TO_MSEC(1);
2837 stcb->asoc.delayed_ack = sack->sack_delay;
2839 if (sack->sack_freq) {
2840 stcb->asoc.sack_freq = sack->sack_freq;
2842 SCTP_TCB_UNLOCK(stcb);
2844 SCTP_INP_WLOCK(inp);
2845 if (sack->sack_delay) {
2846 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2847 sack->sack_delay = TICKS_TO_MSEC(1);
2849 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2851 if (sack->sack_freq) {
2852 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2854 SCTP_INP_WUNLOCK(inp);
2858 case SCTP_AUTH_CHUNK:
2860 struct sctp_authchunk *sauth;
2862 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2864 SCTP_INP_WLOCK(inp);
2865 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
2866 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2869 SCTP_INP_WUNLOCK(inp);
2874 struct sctp_authkey *sca;
2875 struct sctp_keyhead *shared_keys;
2876 sctp_sharedkey_t *shared_key;
2877 sctp_key_t *key = NULL;
2880 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2881 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
2882 size = optsize - sizeof(*sca);
2885 /* set it on the assoc */
2886 shared_keys = &stcb->asoc.shared_keys;
2887 /* clear the cached keys for this key id */
2888 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2890 * create the new shared key and
2894 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2896 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2898 SCTP_TCB_UNLOCK(stcb);
2902 shared_key = sctp_alloc_sharedkey();
2903 if (shared_key == NULL) {
2905 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2907 SCTP_TCB_UNLOCK(stcb);
2910 shared_key->key = key;
2911 shared_key->keyid = sca->sca_keynumber;
2912 sctp_insert_sharedkey(shared_keys, shared_key);
2913 SCTP_TCB_UNLOCK(stcb);
2915 /* set it on the endpoint */
2916 SCTP_INP_WLOCK(inp);
2917 shared_keys = &inp->sctp_ep.shared_keys;
2919 * clear the cached keys on all assocs for
2922 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2924 * create the new shared key and
2928 key = sctp_set_key(sca->sca_key, (uint32_t) size);
2930 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2932 SCTP_INP_WUNLOCK(inp);
2936 shared_key = sctp_alloc_sharedkey();
2937 if (shared_key == NULL) {
2939 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2941 SCTP_INP_WUNLOCK(inp);
2944 shared_key->key = key;
2945 shared_key->keyid = sca->sca_keynumber;
2946 sctp_insert_sharedkey(shared_keys, shared_key);
2947 SCTP_INP_WUNLOCK(inp);
2951 case SCTP_HMAC_IDENT:
2953 struct sctp_hmacalgo *shmac;
2954 sctp_hmaclist_t *hmaclist;
2956 size_t size, i, found;
2958 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2959 size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2960 hmaclist = sctp_alloc_hmaclist(size);
2961 if (hmaclist == NULL) {
2962 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
2966 for (i = 0; i < size; i++) {
2967 hmacid = shmac->shmac_idents[i];
2968 if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2969 /* invalid HMACs were found */ ;
2970 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2972 sctp_free_hmaclist(hmaclist);
2973 goto sctp_set_hmac_done;
2977 for (i = 0; i < hmaclist->num_algo; i++) {
2978 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
2979 /* already in list */
2984 sctp_free_hmaclist(hmaclist);
2985 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2989 /* set it on the endpoint */
2990 SCTP_INP_WLOCK(inp);
2991 if (inp->sctp_ep.local_hmacs)
2992 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2993 inp->sctp_ep.local_hmacs = hmaclist;
2994 SCTP_INP_WUNLOCK(inp);
2998 case SCTP_AUTH_ACTIVE_KEY:
3000 struct sctp_authkeyid *scact;
3002 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
3003 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3005 /* set the active key on the right place */
3007 /* set the active key on the assoc */
3008 if (sctp_auth_setactivekey(stcb, scact->scact_keynumber)) {
3009 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3012 SCTP_TCB_UNLOCK(stcb);
3014 /* set the active key on the endpoint */
3015 SCTP_INP_WLOCK(inp);
3016 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
3017 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3020 SCTP_INP_WUNLOCK(inp);
3024 case SCTP_AUTH_DELETE_KEY:
3026 struct sctp_authkeyid *scdel;
3028 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
3029 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3031 /* delete the key from the right place */
3033 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
3034 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3037 SCTP_TCB_UNLOCK(stcb);
3039 SCTP_INP_WLOCK(inp);
3040 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
3041 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3044 SCTP_INP_WUNLOCK(inp);
3049 case SCTP_RESET_STREAMS:
3051 struct sctp_stream_reset *strrst;
3052 uint8_t send_in = 0, send_tsn = 0, send_out = 0;
3055 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3056 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3059 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3063 if (stcb->asoc.peer_supports_strreset == 0) {
3065 * Peer does not support it, we return
3066 * protocol not supported since this is true
3067 * for this feature and this peer, not the
3068 * socket request in general.
3070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3071 error = EPROTONOSUPPORT;
3072 SCTP_TCB_UNLOCK(stcb);
3075 if (stcb->asoc.stream_reset_outstanding) {
3076 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3078 SCTP_TCB_UNLOCK(stcb);
3081 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3083 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3085 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3088 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3091 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3093 SCTP_TCB_UNLOCK(stcb);
3096 for (i = 0; i < strrst->strrst_num_streams; i++) {
3099 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3105 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3106 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3113 SCTP_TCB_UNLOCK(stcb);
3116 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3117 strrst->strrst_list,
3118 send_out, (stcb->asoc.str_reset_seq_in - 3),
3121 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3122 SCTP_TCB_UNLOCK(stcb);
3126 case SCTP_CONNECT_X:
3127 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3128 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3132 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3135 case SCTP_CONNECT_X_DELAYED:
3136 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3137 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3141 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3144 case SCTP_CONNECT_X_COMPLETE:
3146 struct sockaddr *sa;
3147 struct sctp_nets *net;
3149 /* FIXME MT: check correct? */
3150 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3153 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3154 SCTP_INP_RLOCK(inp);
3155 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3157 SCTP_TCB_LOCK(stcb);
3158 net = sctp_findnet(stcb, sa);
3160 SCTP_INP_RUNLOCK(inp);
3163 * We increment here since
3164 * sctp_findassociation_ep_addr() wil do a
3165 * decrement if it finds the stcb as long as
3166 * the locked tcb (last argument) is NOT a
3169 SCTP_INP_INCR_REF(inp);
3170 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3172 SCTP_INP_DECR_REF(inp);
3177 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3181 if (stcb->asoc.delayed_connection == 1) {
3182 stcb->asoc.delayed_connection = 0;
3183 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3184 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3185 stcb->asoc.primary_destination,
3186 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3187 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3190 * already expired or did not use delayed
3193 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3196 SCTP_TCB_UNLOCK(stcb);
3199 case SCTP_MAX_BURST:
3203 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3205 SCTP_INP_WLOCK(inp);
3207 inp->sctp_ep.max_burst = *burst;
3209 SCTP_INP_WUNLOCK(inp);
3214 struct sctp_assoc_value *av;
3217 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3218 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3220 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3221 ovh = SCTP_MED_OVERHEAD;
3223 ovh = SCTP_MED_V4_OVERHEAD;
3226 if (av->assoc_value) {
3227 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3229 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3231 SCTP_TCB_UNLOCK(stcb);
3233 SCTP_INP_WLOCK(inp);
3235 * FIXME MT: I think this is not in tune
3238 if (av->assoc_value) {
3239 inp->sctp_frag_point = (av->assoc_value + ovh);
3241 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3243 SCTP_INP_WUNLOCK(inp);
3249 struct sctp_event_subscribe *events;
3251 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3253 SCTP_INP_WLOCK(inp);
3254 if (events->sctp_data_io_event) {
3255 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3257 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3260 if (events->sctp_association_event) {
3261 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3263 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3266 if (events->sctp_address_event) {
3267 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3269 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3272 if (events->sctp_send_failure_event) {
3273 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3275 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3278 if (events->sctp_peer_error_event) {
3279 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3281 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3284 if (events->sctp_shutdown_event) {
3285 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3287 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3290 if (events->sctp_partial_delivery_event) {
3291 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3293 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3296 if (events->sctp_adaptation_layer_event) {
3297 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3299 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3302 if (events->sctp_authentication_event) {
3303 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3305 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3308 if (events->sctp_stream_reset_events) {
3309 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3311 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3313 SCTP_INP_WUNLOCK(inp);
3317 case SCTP_ADAPTATION_LAYER:
3319 struct sctp_setadaptation *adap_bits;
3321 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3322 SCTP_INP_WLOCK(inp);
3323 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3324 SCTP_INP_WUNLOCK(inp);
3328 case SCTP_SET_INITIAL_DBG_SEQ:
3332 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3333 SCTP_INP_WLOCK(inp);
3334 inp->sctp_ep.initial_sequence_debug = *vvv;
3335 SCTP_INP_WUNLOCK(inp);
3339 case SCTP_DEFAULT_SEND_PARAM:
3341 struct sctp_sndrcvinfo *s_info;
3343 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3344 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3347 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3348 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3350 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3353 SCTP_TCB_UNLOCK(stcb);
3355 SCTP_INP_WLOCK(inp);
3356 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3357 SCTP_INP_WUNLOCK(inp);
3361 case SCTP_PEER_ADDR_PARAMS:
3362 /* Applys to the specific association */
3364 struct sctp_paddrparams *paddrp;
3365 struct sctp_nets *net;
3367 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3368 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3371 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3374 * We increment here since
3375 * sctp_findassociation_ep_addr() wil do a
3376 * decrement if it finds the stcb as long as
3377 * the locked tcb (last argument) is NOT a
3380 SCTP_INP_INCR_REF(inp);
3381 stcb = sctp_findassociation_ep_addr(&inp,
3382 (struct sockaddr *)&paddrp->spp_address,
3385 SCTP_INP_DECR_REF(inp);
3388 if (stcb && (net == NULL)) {
3389 struct sockaddr *sa;
3391 sa = (struct sockaddr *)&paddrp->spp_address;
3392 if (sa->sa_family == AF_INET) {
3393 struct sockaddr_in *sin;
3395 sin = (struct sockaddr_in *)sa;
3396 if (sin->sin_addr.s_addr) {
3397 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3398 SCTP_TCB_UNLOCK(stcb);
3402 } else if (sa->sa_family == AF_INET6) {
3403 struct sockaddr_in6 *sin6;
3405 sin6 = (struct sockaddr_in6 *)sa;
3406 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3407 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3408 SCTP_TCB_UNLOCK(stcb);
3413 error = EAFNOSUPPORT;
3414 SCTP_TCB_UNLOCK(stcb);
3415 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3420 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3422 SCTP_TCB_UNLOCK(stcb);
3423 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3426 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3428 SCTP_TCB_UNLOCK(stcb);
3429 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3433 /************************TCB SPECIFIC SET ******************/
3435 * do we change the timer for HB, we run
3440 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3441 ovh = SCTP_MED_OVERHEAD;
3443 ovh = SCTP_MED_V4_OVERHEAD;
3446 if (paddrp->spp_hbinterval)
3447 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3448 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3449 stcb->asoc.heart_beat_delay = 0;
3451 /* network sets ? */
3453 /************************NET SPECIFIC SET ******************/
3454 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3456 if (sctp_send_hb(stcb, 1, net) < 0) {
3457 /* asoc destroyed */
3458 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3463 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3464 net->dest_state |= SCTP_ADDR_NOHB;
3466 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3467 net->dest_state &= ~SCTP_ADDR_NOHB;
3469 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3470 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3471 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3472 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3474 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3475 net->mtu = paddrp->spp_pathmtu + ovh;
3476 if (net->mtu < stcb->asoc.smallest_mtu) {
3477 #ifdef SCTP_PRINT_FOR_B_AND_M
3478 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3481 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3485 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3486 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3487 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3490 if (paddrp->spp_pathmaxrxt)
3491 net->failure_threshold = paddrp->spp_pathmaxrxt;
3493 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3494 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3495 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3500 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3501 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3502 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3507 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3508 if (paddrp->spp_pathmaxrxt)
3509 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3511 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3512 /* Turn back on the timer */
3513 stcb->asoc.hb_is_disabled = 0;
3514 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3516 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3517 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3518 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3519 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3520 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3522 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3523 net->mtu = paddrp->spp_pathmtu + ovh;
3524 if (net->mtu < stcb->asoc.smallest_mtu) {
3525 #ifdef SCTP_PRINT_FOR_B_AND_M
3526 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3529 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3534 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3535 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3536 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3537 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3541 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3542 int cnt_of_unconf = 0;
3543 struct sctp_nets *lnet;
3545 stcb->asoc.hb_is_disabled = 1;
3546 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3547 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3552 * stop the timer ONLY if we
3553 * have no unconfirmed
3556 if (cnt_of_unconf == 0) {
3557 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3558 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3559 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3563 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3564 /* start up the timer. */
3565 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3566 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3570 if (paddrp->spp_flags & SPP_IPV4_TOS)
3571 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3574 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3575 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3579 SCTP_TCB_UNLOCK(stcb);
3581 /************************NO TCB, SET TO default stuff ******************/
3582 SCTP_INP_WLOCK(inp);
3584 * For the TOS/FLOWLABEL stuff you set it
3585 * with the options on the socket
3587 if (paddrp->spp_pathmaxrxt) {
3588 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3590 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3591 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3592 else if (paddrp->spp_hbinterval) {
3593 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3594 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3595 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3597 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3598 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3600 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3601 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3603 SCTP_INP_WUNLOCK(inp);
3609 struct sctp_rtoinfo *srto;
3610 uint32_t new_init, new_min, new_max;
3612 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3613 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3616 if (srto->srto_initial)
3617 new_init = srto->srto_initial;
3619 new_init = stcb->asoc.initial_rto;
3621 new_max = srto->srto_max;
3623 new_max = stcb->asoc.maxrto;
3625 new_min = srto->srto_min;
3627 new_min = stcb->asoc.minrto;
3628 if ((new_min <= new_init) && (new_init <= new_max)) {
3629 stcb->asoc.initial_rto = new_init;
3630 stcb->asoc.maxrto = new_max;
3631 stcb->asoc.minrto = new_min;
3633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDOM);
3636 SCTP_TCB_UNLOCK(stcb);
3638 SCTP_INP_WLOCK(inp);
3639 if (srto->srto_initial)
3640 new_init = srto->srto_initial;
3642 new_init = inp->sctp_ep.initial_rto;
3644 new_max = srto->srto_max;
3646 new_max = inp->sctp_ep.sctp_maxrto;
3648 new_min = srto->srto_min;
3650 new_min = inp->sctp_ep.sctp_minrto;
3651 if ((new_min <= new_init) && (new_init <= new_max)) {
3652 inp->sctp_ep.initial_rto = new_init;
3653 inp->sctp_ep.sctp_maxrto = new_max;
3654 inp->sctp_ep.sctp_minrto = new_min;
3656 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDOM);
3659 SCTP_INP_WUNLOCK(inp);
3663 case SCTP_ASSOCINFO:
3665 struct sctp_assocparams *sasoc;
3667 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3668 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3669 if (sasoc->sasoc_cookie_life) {
3670 /* boundary check the cookie life */
3671 if (sasoc->sasoc_cookie_life < 1000)
3672 sasoc->sasoc_cookie_life = 1000;
3673 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3674 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3678 if (sasoc->sasoc_asocmaxrxt)
3679 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3680 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3681 sasoc->sasoc_peer_rwnd = 0;
3682 sasoc->sasoc_local_rwnd = 0;
3683 if (sasoc->sasoc_cookie_life) {
3684 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3686 SCTP_TCB_UNLOCK(stcb);
3688 SCTP_INP_WLOCK(inp);
3689 if (sasoc->sasoc_asocmaxrxt)
3690 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3691 sasoc->sasoc_number_peer_destinations = 0;
3692 sasoc->sasoc_peer_rwnd = 0;
3693 sasoc->sasoc_local_rwnd = 0;
3694 if (sasoc->sasoc_cookie_life) {
3695 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3697 SCTP_INP_WUNLOCK(inp);
3703 struct sctp_initmsg *sinit;
3705 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3706 SCTP_INP_WLOCK(inp);
3707 if (sinit->sinit_num_ostreams)
3708 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3710 if (sinit->sinit_max_instreams)
3711 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3713 if (sinit->sinit_max_attempts)
3714 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3716 if (sinit->sinit_max_init_timeo)
3717 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3718 SCTP_INP_WUNLOCK(inp);
3721 case SCTP_PRIMARY_ADDR:
3723 struct sctp_setprim *spa;
3724 struct sctp_nets *net, *lnet;
3726 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3727 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3731 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3734 * We increment here since
3735 * sctp_findassociation_ep_addr() wil do a
3736 * decrement if it finds the stcb as long as
3737 * the locked tcb (last argument) is NOT a
3740 SCTP_INP_INCR_REF(inp);
3741 stcb = sctp_findassociation_ep_addr(&inp,
3742 (struct sockaddr *)&spa->ssp_addr,
3745 SCTP_INP_DECR_REF(inp);
3749 if ((stcb) && (net)) {
3750 if ((net != stcb->asoc.primary_destination) &&
3751 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3752 /* Ok we need to set it */
3753 lnet = stcb->asoc.primary_destination;
3754 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3755 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3756 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3758 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3762 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3766 SCTP_TCB_UNLOCK(stcb);
3770 case SCTP_SET_DYNAMIC_PRIMARY:
3772 union sctp_sockstore *ss;
3774 error = priv_check(curthread,
3775 PRIV_NETINET_RESERVEDPORT);
3779 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3780 /* SUPER USER CHECK? */
3781 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3784 case SCTP_SET_PEER_PRIMARY_ADDR:
3786 struct sctp_setpeerprim *sspp;
3788 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3789 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3791 struct sctp_ifa *ifa;
3793 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
3794 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
3796 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3800 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3802 * Must validate the ifa found is in
3805 struct sctp_laddr *laddr;
3808 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3809 if (laddr->ifa == NULL) {
3810 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
3814 if (laddr->ifa == ifa) {
3820 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3825 if (sctp_set_primary_ip_address_sa(stcb,
3826 (struct sockaddr *)&sspp->sspp_addr) != 0) {
3827 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3831 SCTP_TCB_UNLOCK(stcb);
3833 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3839 case SCTP_BINDX_ADD_ADDR:
3841 struct sctp_getaddresses *addrs;
3846 td = (struct thread *)p;
3847 if (jailed(td->td_ucred)) {
3850 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
3852 if (addrs->addr->sa_family == AF_INET) {
3853 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3855 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3859 if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3860 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3861 error = EADDRNOTAVAIL;
3863 } else if (addrs->addr->sa_family == AF_INET6) {
3864 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3866 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3870 /* JAIL XXXX Add else here for V6 */
3872 sctp_bindx_add_address(so, inp, addrs->addr,
3873 addrs->sget_assoc_id, vrf_id,
3877 case SCTP_BINDX_REM_ADDR:
3879 struct sctp_getaddresses *addrs;
3884 td = (struct thread *)p;
3885 if (jailed(td->td_ucred)) {
3888 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3889 if (addrs->addr->sa_family == AF_INET) {
3890 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
3892 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3896 if (prison && prison_ip(td->td_ucred, 0, &(((struct sockaddr_in *)(addrs->addr))->sin_addr.s_addr))) {
3897 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
3898 error = EADDRNOTAVAIL;
3900 } else if (addrs->addr->sa_family == AF_INET6) {
3901 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
3903 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3907 /* JAIL XXXX Add else here for V6 */
3909 sctp_bindx_delete_address(so, inp, addrs->addr,
3910 addrs->sget_assoc_id, vrf_id,
3915 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3916 error = ENOPROTOOPT;
3918 } /* end switch (opt) */
3924 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3926 void *optval = NULL;
3928 struct sctp_inpcb *inp;
3932 inp = (struct sctp_inpcb *)so->so_pcb;
3934 /* I made the same as TCP since we are not setup? */
3935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3936 return (ECONNRESET);
3938 if (sopt->sopt_level != IPPROTO_SCTP) {
3939 /* wrong proto level... send back up to IP */
3941 if (INP_CHECK_SOCKAF(so, AF_INET6))
3942 error = ip6_ctloutput(so, sopt);
3945 error = ip_ctloutput(so, sopt);
3948 optsize = sopt->sopt_valsize;
3950 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
3951 if (optval == NULL) {
3952 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
3955 error = sooptcopyin(sopt, optval, optsize, optsize);
3957 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3961 p = (void *)sopt->sopt_td;
3962 if (sopt->sopt_dir == SOPT_SET) {
3963 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3964 } else if (sopt->sopt_dir == SOPT_GET) {
3965 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3967 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3970 if ((error == 0) && (optval != NULL)) {
3971 error = sooptcopyout(sopt, optval, optsize);
3972 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3973 } else if (optval != NULL) {
3974 SCTP_FREE(optval, SCTP_M_SOCKOPT);
3982 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3985 int create_lock_on = 0;
3987 struct sctp_inpcb *inp;
3988 struct sctp_tcb *stcb = NULL;
3990 inp = (struct sctp_inpcb *)so->so_pcb;
3992 /* I made the same as TCP since we are not setup? */
3993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3994 return (ECONNRESET);
3997 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4000 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
4001 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4004 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
4005 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4008 SCTP_ASOC_CREATE_LOCK(inp);
4011 SCTP_INP_INCR_REF(inp);
4012 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4013 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4014 /* Should I really unlock ? */
4015 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4020 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4021 (addr->sa_family == AF_INET6)) {
4022 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4027 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4028 SCTP_PCB_FLAGS_UNBOUND) {
4029 /* Bind a ephemeral port */
4030 error = sctp_inpcb_bind(so, NULL, NULL, p);
4035 /* Now do we connect? */
4036 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
4037 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4041 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4042 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4043 /* We are already connected AND the TCP model */
4044 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4048 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4049 SCTP_INP_RLOCK(inp);
4050 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4051 SCTP_INP_RUNLOCK(inp);
4054 * We increment here since sctp_findassociation_ep_addr()
4055 * wil do a decrement if it finds the stcb as long as the
4056 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4058 SCTP_INP_INCR_REF(inp);
4059 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4061 SCTP_INP_DECR_REF(inp);
4063 SCTP_TCB_LOCK(stcb);
4067 /* Already have or am bring up an association */
4068 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4072 vrf_id = inp->def_vrf_id;
4073 /* We are GOOD to go */
4074 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4076 /* Gak! no memory */
4079 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4080 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4081 /* Set the connected flag so we can queue data */
4084 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4085 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4087 /* initialize authentication parameters for the assoc */
4088 sctp_initialize_auth_params(inp, stcb);
4090 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4091 SCTP_TCB_UNLOCK(stcb);
4093 if (create_lock_on) {
4094 SCTP_ASOC_CREATE_UNLOCK(inp);
4096 SCTP_INP_DECR_REF(inp);
4101 sctp_listen(struct socket *so, int backlog, struct thread *p)
4104 * Note this module depends on the protocol processing being called
4105 * AFTER any socket level flags and backlog are applied to the
4106 * socket. The traditional way that the socket flags are applied is
4107 * AFTER protocol processing. We have made a change to the
4108 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4109 * place if the socket API for SCTP is to work properly.
4113 struct sctp_inpcb *inp;
4115 inp = (struct sctp_inpcb *)so->so_pcb;
4117 /* I made the same as TCP since we are not setup? */
4118 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4119 return (ECONNRESET);
4121 SCTP_INP_RLOCK(inp);
4122 #ifdef SCTP_LOCK_LOGGING
4123 if (sctp_logging_level & SCTP_LOCK_LOGGING_ENABLE) {
4124 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4128 error = solisten_proto_check(so);
4131 SCTP_INP_RUNLOCK(inp);
4134 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4135 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4136 /* We are already connected AND the TCP model */
4137 SCTP_INP_RUNLOCK(inp);
4139 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4140 return (EADDRINUSE);
4142 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4143 /* We must do a bind. */
4145 SCTP_INP_RUNLOCK(inp);
4146 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4147 /* bind error, probably perm */
4152 SCTP_INP_RUNLOCK(inp);
4154 /* It appears for 7.0 and on, we must always call this. */
4155 solisten_proto(so, backlog);
4156 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4157 /* remove the ACCEPTCONN flag for one-to-many sockets */
4158 so->so_options &= ~SO_ACCEPTCONN;
4161 /* turning off listen */
4162 so->so_options &= ~SO_ACCEPTCONN;
4168 static int sctp_defered_wakeup_cnt = 0;
4171 sctp_accept(struct socket *so, struct sockaddr **addr)
4173 struct sctp_tcb *stcb;
4174 struct sctp_inpcb *inp;
4175 union sctp_sockstore store;
4179 inp = (struct sctp_inpcb *)so->so_pcb;
4182 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4183 return (ECONNRESET);
4185 SCTP_INP_RLOCK(inp);
4186 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4187 SCTP_INP_RUNLOCK(inp);
4188 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4189 return (EOPNOTSUPP);
4191 if (so->so_state & SS_ISDISCONNECTED) {
4192 SCTP_INP_RUNLOCK(inp);
4193 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4194 return (ECONNABORTED);
4196 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4198 SCTP_INP_RUNLOCK(inp);
4199 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4200 return (ECONNRESET);
4202 SCTP_TCB_LOCK(stcb);
4203 SCTP_INP_RUNLOCK(inp);
4204 store = stcb->asoc.primary_destination->ro._l_addr;
4205 SCTP_TCB_UNLOCK(stcb);
4206 if (store.sa.sa_family == AF_INET) {
4207 struct sockaddr_in *sin;
4209 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4210 sin->sin_family = AF_INET;
4211 sin->sin_len = sizeof(*sin);
4212 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4213 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4214 *addr = (struct sockaddr *)sin;
4216 struct sockaddr_in6 *sin6;
4218 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4219 sin6->sin6_family = AF_INET6;
4220 sin6->sin6_len = sizeof(*sin6);
4221 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4223 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4224 if ((error = sa6_recoverscope(sin6)) != 0) {
4225 SCTP_FREE_SONAME(sin6);
4228 *addr = (struct sockaddr *)sin6;
4230 /* Wake any delayed sleep action */
4231 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4232 SCTP_INP_WLOCK(inp);
4233 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4234 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4235 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4236 SCTP_INP_WUNLOCK(inp);
4237 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4238 if (sowriteable(inp->sctp_socket)) {
4239 sowwakeup_locked(inp->sctp_socket);
4241 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4243 SCTP_INP_WLOCK(inp);
4245 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4246 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4247 SCTP_INP_WUNLOCK(inp);
4248 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4249 if (soreadable(inp->sctp_socket)) {
4250 sctp_defered_wakeup_cnt++;
4251 sorwakeup_locked(inp->sctp_socket);
4253 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4255 SCTP_INP_WLOCK(inp);
4257 SCTP_INP_WUNLOCK(inp);
4263 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4265 struct sockaddr_in *sin;
4267 struct sctp_inpcb *inp;
4268 struct sctp_ifa *sctp_ifa;
4271 * Do the malloc first in case it blocks.
4273 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4274 sin->sin_family = AF_INET;
4275 sin->sin_len = sizeof(*sin);
4276 inp = (struct sctp_inpcb *)so->so_pcb;
4278 SCTP_FREE_SONAME(sin);
4279 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4282 SCTP_INP_RLOCK(inp);
4283 sin->sin_port = inp->sctp_lport;
4284 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4285 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4286 struct sctp_tcb *stcb;
4287 struct sockaddr_in *sin_a;
4288 struct sctp_nets *net;
4291 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4297 SCTP_TCB_LOCK(stcb);
4298 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4299 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4301 /* this will make coverity happy */
4304 if (sin_a->sin_family == AF_INET) {
4309 if ((!fnd) || (sin_a == NULL)) {
4311 SCTP_TCB_UNLOCK(stcb);
4314 vrf_id = inp->def_vrf_id;
4315 sctp_ifa = sctp_source_address_selection(inp,
4317 (sctp_route_t *) & net->ro,
4320 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4321 sctp_free_ifa(sctp_ifa);
4323 SCTP_TCB_UNLOCK(stcb);
4325 /* For the bound all case you get back 0 */
4327 sin->sin_addr.s_addr = 0;
4331 /* Take the first IPv4 address in the list */
4332 struct sctp_laddr *laddr;
4335 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4336 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4337 struct sockaddr_in *sin_a;
4339 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4340 sin->sin_addr = sin_a->sin_addr;
4346 SCTP_FREE_SONAME(sin);
4347 SCTP_INP_RUNLOCK(inp);
4348 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4352 SCTP_INP_RUNLOCK(inp);
4353 (*addr) = (struct sockaddr *)sin;
4358 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4360 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4362 struct sockaddr_in *sin_a;
4363 struct sctp_inpcb *inp;
4364 struct sctp_tcb *stcb;
4365 struct sctp_nets *net;
4367 /* Do the malloc first in case it blocks. */
4368 inp = (struct sctp_inpcb *)so->so_pcb;
4369 if ((inp == NULL) ||
4370 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4371 /* UDP type and listeners will drop out here */
4372 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4375 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4376 sin->sin_family = AF_INET;
4377 sin->sin_len = sizeof(*sin);
4379 /* We must recapture incase we blocked */
4380 inp = (struct sctp_inpcb *)so->so_pcb;
4382 SCTP_FREE_SONAME(sin);
4383 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4386 SCTP_INP_RLOCK(inp);
4387 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4389 SCTP_TCB_LOCK(stcb);
4391 SCTP_INP_RUNLOCK(inp);
4393 SCTP_FREE_SONAME(sin);
4394 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4398 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4399 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4400 if (sin_a->sin_family == AF_INET) {
4402 sin->sin_port = stcb->rport;
4403 sin->sin_addr = sin_a->sin_addr;
4407 SCTP_TCB_UNLOCK(stcb);
4409 /* No IPv4 address */
4410 SCTP_FREE_SONAME(sin);
4411 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4414 (*addr) = (struct sockaddr *)sin;
4418 struct pr_usrreqs sctp_usrreqs = {
4419 .pru_abort = sctp_abort,
4420 .pru_accept = sctp_accept,
4421 .pru_attach = sctp_attach,
4422 .pru_bind = sctp_bind,
4423 .pru_connect = sctp_connect,
4424 .pru_control = in_control,
4425 .pru_close = sctp_close,
4426 .pru_detach = sctp_close,
4427 .pru_sopoll = sopoll_generic,
4428 .pru_disconnect = sctp_disconnect,
4429 .pru_listen = sctp_listen,
4430 .pru_peeraddr = sctp_peeraddr,
4431 .pru_send = sctp_sendm,
4432 .pru_shutdown = sctp_shutdown,
4433 .pru_sockaddr = sctp_ingetaddr,
4434 .pru_sosend = sctp_sosend,
4435 .pru_soreceive = sctp_soreceive