2 * Copyright (c) 2001-2008, 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>
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctp_asconf.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_cc_functions.h>
52 #include <netinet/udp.h>
62 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
64 /* Initialize and modify the sysctled variables */
66 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 SCTP_BASE_SYSCTL(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_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
74 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76 * Now for the recv window, should we take the same amount? or
77 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 * now I will just copy.
80 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
82 SCTP_BASE_VAR(first_time) = 0;
83 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
85 #if defined(SCTP_PACKET_LOGGING)
86 SCTP_BASE_VAR(packet_log_writers) = 0;
87 SCTP_BASE_VAR(packet_log_end) = 0;
88 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104 struct sctp_tcb *stcb,
105 struct sctp_nets *net,
108 struct sctp_tmit_chunk *chk;
110 /* Adjust that too */
111 stcb->asoc.smallest_mtu = nxtsz;
112 /* now off to subtract IP_DF flag if needed */
113 #ifdef SCTP_PRINT_FOR_B_AND_M
114 SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
115 inp, stcb, net, nxtsz);
117 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
118 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
119 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
122 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
123 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
125 * For this guy we also mark for immediate resend
126 * since we sent to big of chunk
128 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
129 if (chk->sent != SCTP_DATAGRAM_RESEND) {
130 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
132 chk->sent = SCTP_DATAGRAM_RESEND;
133 chk->rec.data.doing_fast_retransmit = 0;
134 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
135 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
136 chk->whoTo->flight_size,
138 (uintptr_t) chk->whoTo,
139 chk->rec.data.TSN_seq);
141 /* Clear any time so NO RTT is being done */
143 sctp_flight_size_decrease(chk);
144 sctp_total_flight_decrease(stcb, chk);
150 sctp_notify_mbuf(struct sctp_inpcb *inp,
151 struct sctp_tcb *stcb,
152 struct sctp_nets *net,
157 int totsz, tmr_stopped = 0;
161 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
162 (ip == NULL) || (sh == NULL)) {
164 SCTP_TCB_UNLOCK(stcb);
168 /* First job is to verify the vtag matches what I would send */
169 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
170 SCTP_TCB_UNLOCK(stcb);
173 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
175 if (icmph->icmp_type != ICMP_UNREACH) {
176 /* We only care about unreachable */
177 SCTP_TCB_UNLOCK(stcb);
180 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
181 /* not a unreachable message due to frag. */
182 SCTP_TCB_UNLOCK(stcb);
187 nxtsz = ntohs(icmph->icmp_nextmtu);
190 * old type router that does not tell us what the next size
191 * mtu is. Rats we will have to guess (in a educated fashion
194 nxtsz = find_next_best_mtu(totsz);
196 /* Stop any PMTU timer */
197 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
199 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
200 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
202 /* Adjust destination size limit */
203 if (net->mtu > nxtsz) {
206 net->mtu -= sizeof(struct udphdr);
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_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(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;
520 inp = (struct sctp_inpcb *)so->so_pcb;
522 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
525 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
526 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
531 error = sctp_inpcb_alloc(so, vrf_id);
535 inp = (struct sctp_inpcb *)so->so_pcb;
537 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
538 ip_inp = &inp->ip_inp.inp;
539 ip_inp->inp_vflag |= INP_IPV4;
540 ip_inp->inp_ip_ttl = MODULE_GLOBAL(MOD_INET, ip_defttl);
542 error = ipsec_init_policy(so, &ip_inp->inp_sp);
543 #ifdef SCTP_LOG_CLOSING
544 sctp_log_closing(inp, NULL, 17);
547 flags = inp->sctp_flags;
548 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
549 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
550 #ifdef SCTP_LOG_CLOSING
551 sctp_log_closing(inp, NULL, 15);
553 SCTP_INP_WUNLOCK(inp);
554 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
555 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
557 SCTP_INP_WUNLOCK(inp);
562 SCTP_INP_WUNLOCK(inp);
567 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
569 struct sctp_inpcb *inp = NULL;
573 if (addr && addr->sa_family != AF_INET) {
574 /* must be a v4 address! */
575 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
579 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
580 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
583 inp = (struct sctp_inpcb *)so->so_pcb;
585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
588 error = sctp_inpcb_bind(so, addr, NULL, p);
593 sctp_close(struct socket *so)
595 struct sctp_inpcb *inp;
598 inp = (struct sctp_inpcb *)so->so_pcb;
603 * Inform all the lower layer assoc that we are done.
606 flags = inp->sctp_flags;
607 #ifdef SCTP_LOG_CLOSING
608 sctp_log_closing(inp, NULL, 17);
610 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
611 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
612 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
613 (so->so_rcv.sb_cc > 0)) {
614 #ifdef SCTP_LOG_CLOSING
615 sctp_log_closing(inp, NULL, 13);
617 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
618 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
620 #ifdef SCTP_LOG_CLOSING
621 sctp_log_closing(inp, NULL, 14);
623 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
624 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
627 * The socket is now detached, no matter what the state of
628 * the SCTP association.
631 SCTP_SB_CLEAR(so->so_snd);
633 * same for the rcv ones, they are only here for the
636 SCTP_SB_CLEAR(so->so_rcv);
638 /* Now null out the reference, we are completely detached. */
642 flags = inp->sctp_flags;
643 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
644 goto sctp_must_try_again;
652 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
653 struct mbuf *control, struct thread *p);
657 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
658 struct mbuf *control, struct thread *p)
660 struct sctp_inpcb *inp;
663 inp = (struct sctp_inpcb *)so->so_pcb;
666 sctp_m_freem(control);
669 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
673 /* Got to have an to address if we are NOT a connected socket */
674 if ((addr == NULL) &&
675 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
676 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
679 } else if (addr == NULL) {
680 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
681 error = EDESTADDRREQ;
684 sctp_m_freem(control);
690 if (addr->sa_family != AF_INET) {
691 /* must be a v4 address! */
692 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
695 sctp_m_freem(control);
698 error = EDESTADDRREQ;
703 /* now what about control */
706 SCTP_PRINTF("huh? control set?\n");
707 sctp_m_freem(inp->control);
710 inp->control = control;
714 SCTP_BUF_NEXT(inp->pkt_last) = m;
717 inp->pkt_last = inp->pkt = m;
720 /* FreeBSD uses a flag passed */
721 ((flags & PRUS_MORETOCOME) == 0)
724 * note with the current version this code will only be used
725 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
726 * re-defining sosend to use the sctp_sosend. One can
727 * optionally switch back to this code (by changing back the
728 * definitions) but this is not advisable. This code is used
729 * by FreeBSD when sending a file with sendfile() though.
733 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
743 sctp_disconnect(struct socket *so)
745 struct sctp_inpcb *inp;
747 inp = (struct sctp_inpcb *)so->so_pcb;
749 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
753 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
754 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
755 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
757 SCTP_INP_RUNLOCK(inp);
760 struct sctp_association *asoc;
761 struct sctp_tcb *stcb;
763 stcb = LIST_FIRST(&inp->sctp_asoc_list);
765 SCTP_INP_RUNLOCK(inp);
766 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
771 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
772 /* We are about to be freed, out of here */
773 SCTP_TCB_UNLOCK(stcb);
774 SCTP_INP_RUNLOCK(inp);
777 if (((so->so_options & SO_LINGER) &&
778 (so->so_linger == 0)) ||
779 (so->so_rcv.sb_cc > 0)) {
780 if (SCTP_GET_STATE(asoc) !=
781 SCTP_STATE_COOKIE_WAIT) {
782 /* Left with Data unread */
785 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
791 struct sctp_paramhdr *ph;
793 ph = mtod(err, struct sctp_paramhdr *);
794 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
795 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
796 ph->param_length = htons(SCTP_BUF_LEN(err));
798 #if defined(SCTP_PANIC_ON_ABORT)
799 panic("disconnect does an abort");
801 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
802 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
804 SCTP_INP_RUNLOCK(inp);
805 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
806 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
807 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
809 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
810 /* No unlock tcb assoc is gone */
813 if (TAILQ_EMPTY(&asoc->send_queue) &&
814 TAILQ_EMPTY(&asoc->sent_queue) &&
815 (asoc->stream_queue_cnt == 0)) {
816 /* there is nothing queued to send, so done */
817 if (asoc->locked_on_sending) {
820 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
821 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
822 /* only send SHUTDOWN 1st time thru */
823 sctp_stop_timers_for_shutdown(stcb);
824 sctp_send_shutdown(stcb,
825 stcb->asoc.primary_destination);
826 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
827 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
828 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
829 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
831 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
832 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
833 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
835 asoc->primary_destination);
836 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
838 asoc->primary_destination);
842 * we still got (or just got) data to send,
843 * so set SHUTDOWN_PENDING
846 * XXX sockets draft says that SCTP_EOF
847 * should be sent with no data. currently,
848 * we will allow user data to be sent first
849 * and move to SHUTDOWN-PENDING
851 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
852 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
853 asoc->primary_destination);
854 if (asoc->locked_on_sending) {
855 /* Locked to send out the data */
856 struct sctp_stream_queue_pending *sp;
858 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
860 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
861 asoc->locked_on_sending->stream_no);
863 if ((sp->length == 0) && (sp->msg_is_complete == 0))
864 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
867 if (TAILQ_EMPTY(&asoc->send_queue) &&
868 TAILQ_EMPTY(&asoc->sent_queue) &&
869 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
873 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
874 0, M_DONTWAIT, 1, MT_DATA);
880 struct sctp_paramhdr *ph;
883 SCTP_BUF_LEN(op_err) =
884 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
886 struct sctp_paramhdr *);
887 ph->param_type = htons(
888 SCTP_CAUSE_USER_INITIATED_ABT);
889 ph->param_length = htons(SCTP_BUF_LEN(op_err));
890 ippp = (uint32_t *) (ph + 1);
891 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
893 #if defined(SCTP_PANIC_ON_ABORT)
894 panic("disconnect does an abort");
897 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
898 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
899 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
900 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
901 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
902 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
904 SCTP_INP_RUNLOCK(inp);
905 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
908 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
911 SCTP_TCB_UNLOCK(stcb);
912 SCTP_INP_RUNLOCK(inp);
917 /* UDP model does not support this */
918 SCTP_INP_RUNLOCK(inp);
919 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
925 sctp_flush(struct socket *so, int how)
928 * We will just clear out the values and let subsequent close clear
929 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
930 * they will not be able to read the data, the socket will block
931 * that from happening.
933 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
935 * First make sure the sb will be happy, we don't use these
936 * except maybe the count
938 so->so_rcv.sb_cc = 0;
939 so->so_rcv.sb_mbcnt = 0;
940 so->so_rcv.sb_mb = NULL;
942 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
944 * First make sure the sb will be happy, we don't use these
945 * except maybe the count
947 so->so_snd.sb_cc = 0;
948 so->so_snd.sb_mbcnt = 0;
949 so->so_snd.sb_mb = NULL;
956 sctp_shutdown(struct socket *so)
958 struct sctp_inpcb *inp;
960 inp = (struct sctp_inpcb *)so->so_pcb;
962 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
966 /* For UDP model this is a invalid call */
967 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
968 /* Restore the flags that the soshutdown took away. */
969 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
970 /* This proc will wakeup for read and do nothing (I hope) */
971 SCTP_INP_RUNLOCK(inp);
972 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
976 * Ok if we reach here its the TCP model and it is either a SHUT_WR
977 * or SHUT_RDWR. This means we put the shutdown flag against it.
980 struct sctp_tcb *stcb;
981 struct sctp_association *asoc;
985 stcb = LIST_FIRST(&inp->sctp_asoc_list);
988 * Ok we hit the case that the shutdown call was
989 * made after an abort or something. Nothing to do
992 SCTP_INP_RUNLOCK(inp);
997 if (TAILQ_EMPTY(&asoc->send_queue) &&
998 TAILQ_EMPTY(&asoc->sent_queue) &&
999 (asoc->stream_queue_cnt == 0)) {
1000 if (asoc->locked_on_sending) {
1003 /* there is nothing queued to send, so I'm done... */
1004 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1005 /* only send SHUTDOWN the first time through */
1006 sctp_stop_timers_for_shutdown(stcb);
1007 sctp_send_shutdown(stcb,
1008 stcb->asoc.primary_destination);
1009 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1010 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1011 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1012 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1014 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1015 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1016 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1017 stcb->sctp_ep, stcb,
1018 asoc->primary_destination);
1019 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1020 stcb->sctp_ep, stcb,
1021 asoc->primary_destination);
1025 * we still got (or just got) data to send, so set
1028 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1029 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1030 asoc->primary_destination);
1032 if (asoc->locked_on_sending) {
1033 /* Locked to send out the data */
1034 struct sctp_stream_queue_pending *sp;
1036 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1038 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1039 asoc->locked_on_sending->stream_no);
1041 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1042 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1046 if (TAILQ_EMPTY(&asoc->send_queue) &&
1047 TAILQ_EMPTY(&asoc->sent_queue) &&
1048 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1049 struct mbuf *op_err;
1052 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1053 0, M_DONTWAIT, 1, MT_DATA);
1055 /* Fill in the user initiated abort */
1056 struct sctp_paramhdr *ph;
1059 SCTP_BUF_LEN(op_err) =
1060 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1062 struct sctp_paramhdr *);
1063 ph->param_type = htons(
1064 SCTP_CAUSE_USER_INITIATED_ABT);
1065 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1066 ippp = (uint32_t *) (ph + 1);
1067 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1069 #if defined(SCTP_PANIC_ON_ABORT)
1070 panic("shutdown does an abort");
1072 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1073 sctp_abort_an_association(stcb->sctp_ep, stcb,
1074 SCTP_RESPONSE_TO_USER_REQ,
1075 op_err, SCTP_SO_LOCKED);
1078 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1081 SCTP_TCB_UNLOCK(stcb);
1084 SCTP_INP_RUNLOCK(inp);
1089 * copies a "user" presentable address and removes embedded scope, etc.
1090 * returns 0 on success, 1 on error
1093 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1096 struct sockaddr_in6 lsa6;
1098 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1101 memcpy(ss, sa, sa->sa_len);
1108 * NOTE: assumes addr lock is held
1111 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1112 struct sctp_tcb *stcb,
1114 struct sockaddr_storage *sas,
1117 struct sctp_ifn *sctp_ifn;
1118 struct sctp_ifa *sctp_ifa;
1119 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1121 int ipv4_addr_legal, ipv6_addr_legal;
1122 struct sctp_vrf *vrf;
1129 /* Turn on all the appropriate scope */
1130 loopback_scope = stcb->asoc.loopback_scope;
1131 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1132 local_scope = stcb->asoc.local_scope;
1133 site_scope = stcb->asoc.site_scope;
1135 /* Turn on ALL scope, since we look at the EP */
1136 loopback_scope = ipv4_local_scope = local_scope =
1139 ipv4_addr_legal = ipv6_addr_legal = 0;
1140 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1141 ipv6_addr_legal = 1;
1142 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1143 ipv4_addr_legal = 1;
1146 ipv4_addr_legal = 1;
1148 vrf = sctp_find_vrf(vrf_id);
1152 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1153 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1154 if ((loopback_scope == 0) &&
1155 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1156 /* Skip loopback if loopback_scope not set */
1159 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1162 * For the BOUND-ALL case, the list
1163 * associated with a TCB is Always
1164 * considered a reverse list.. i.e.
1165 * it lists addresses that are NOT
1166 * part of the association. If this
1167 * is one of those we must skip it.
1169 if (sctp_is_addr_restricted(stcb,
1174 switch (sctp_ifa->address.sa.sa_family) {
1176 if (ipv4_addr_legal) {
1177 struct sockaddr_in *sin;
1179 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1180 if (sin->sin_addr.s_addr == 0) {
1188 if ((ipv4_local_scope == 0) &&
1189 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1193 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1194 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1195 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1196 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1197 actual += sizeof(struct sockaddr_in6);
1200 memcpy(sas, sin, sizeof(*sin));
1201 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1202 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1203 actual += sizeof(*sin);
1207 if (actual >= limit) {
1216 if (ipv6_addr_legal) {
1217 struct sockaddr_in6 *sin6;
1219 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1220 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1228 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1229 if (local_scope == 0)
1231 if (sin6->sin6_scope_id == 0) {
1232 if (sa6_recoverscope(sin6) != 0)
1250 if ((site_scope == 0) &&
1251 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1254 memcpy(sas, sin6, sizeof(*sin6));
1255 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1256 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1257 actual += sizeof(*sin6);
1258 if (actual >= limit) {
1273 struct sctp_laddr *laddr;
1275 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1277 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1281 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1284 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1285 sas = (struct sockaddr_storage *)((caddr_t)sas +
1286 laddr->ifa->address.sa.sa_len);
1287 actual += laddr->ifa->address.sa.sa_len;
1288 if (actual >= limit) {
1297 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1298 struct sctp_tcb *stcb,
1300 struct sockaddr_storage *sas)
1304 SCTP_IPI_ADDR_RLOCK();
1305 /* fill up addresses for the endpoint's default vrf */
1306 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1308 SCTP_IPI_ADDR_RUNLOCK();
1313 * NOTE: assumes addr lock is held
1316 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1319 struct sctp_vrf *vrf = NULL;
1322 * In both sub-set bound an bound_all cases we return the MAXIMUM
1323 * number of addresses that you COULD get. In reality the sub-set
1324 * bound may have an exclusion list for a given TCB OR in the
1325 * bound-all case a TCB may NOT include the loopback or other
1326 * addresses as well.
1328 vrf = sctp_find_vrf(vrf_id);
1332 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1333 struct sctp_ifn *sctp_ifn;
1334 struct sctp_ifa *sctp_ifa;
1336 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1337 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1338 /* Count them if they are the right type */
1339 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1340 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1341 cnt += sizeof(struct sockaddr_in6);
1343 cnt += sizeof(struct sockaddr_in);
1345 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1346 cnt += sizeof(struct sockaddr_in6);
1350 struct sctp_laddr *laddr;
1352 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1353 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1354 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1355 cnt += sizeof(struct sockaddr_in6);
1357 cnt += sizeof(struct sockaddr_in);
1359 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1360 cnt += sizeof(struct sockaddr_in6);
1367 sctp_count_max_addresses(struct sctp_inpcb *inp)
1371 SCTP_IPI_ADDR_RLOCK();
1372 /* count addresses for the endpoint's default VRF */
1373 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1374 SCTP_IPI_ADDR_RUNLOCK();
1379 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1380 size_t optsize, void *p, int delay)
1383 int creat_lock_on = 0;
1384 struct sctp_tcb *stcb = NULL;
1385 struct sockaddr *sa;
1386 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1389 int bad_addresses = 0;
1392 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1394 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1395 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1396 /* We are already connected AND the TCP model */
1397 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1398 return (EADDRINUSE);
1400 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1401 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1402 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1405 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1406 SCTP_INP_RLOCK(inp);
1407 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1408 SCTP_INP_RUNLOCK(inp);
1411 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1414 SCTP_INP_INCR_REF(inp);
1415 SCTP_ASOC_CREATE_LOCK(inp);
1417 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1418 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1419 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1423 totaddrp = (int *)optval;
1424 totaddr = *totaddrp;
1425 sa = (struct sockaddr *)(totaddrp + 1);
1426 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1427 if ((stcb != NULL) || bad_addresses) {
1428 /* Already have or am bring up an association */
1429 SCTP_ASOC_CREATE_UNLOCK(inp);
1432 SCTP_TCB_UNLOCK(stcb);
1433 if (bad_addresses == 0) {
1434 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1440 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1445 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1447 struct in6pcb *inp6;
1449 inp6 = (struct in6pcb *)inp;
1450 if (SCTP_IPV6_V6ONLY(inp6)) {
1452 * if IPV6_V6ONLY flag, ignore connections destined
1453 * to a v4 addr or v4-mapped addr
1455 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1461 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1462 SCTP_PCB_FLAGS_UNBOUND) {
1463 /* Bind a ephemeral port */
1464 error = sctp_inpcb_bind(so, NULL, NULL, p);
1469 /* FIX ME: do we want to pass in a vrf on the connect call? */
1470 vrf_id = inp->def_vrf_id;
1473 /* We are GOOD to go */
1474 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1478 /* Gak! no memory */
1481 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1482 /* move to second address */
1483 if (sa->sa_family == AF_INET)
1484 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1486 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1489 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1490 /* Fill in the return id */
1492 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1495 a_id = (sctp_assoc_t *) optval;
1496 *a_id = sctp_get_associd(stcb);
1498 /* initialize authentication parameters for the assoc */
1499 sctp_initialize_auth_params(inp, stcb);
1502 /* doing delayed connection */
1503 stcb->asoc.delayed_connection = 1;
1504 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1506 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1507 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1509 SCTP_TCB_UNLOCK(stcb);
1510 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1511 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1512 /* Set the connected flag so we can queue data */
1516 if (creat_lock_on) {
1517 SCTP_ASOC_CREATE_UNLOCK(inp);
1519 SCTP_INP_DECR_REF(inp);
1523 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1524 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1525 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1526 SCTP_INP_RLOCK(inp); \
1527 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1529 SCTP_TCB_LOCK(stcb); \
1531 SCTP_INP_RUNLOCK(inp); \
1532 } else if (assoc_id != 0) { \
1533 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1534 if (stcb == NULL) { \
1535 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1545 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1546 if (size < sizeof(type)) { \
1547 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1551 destp = (type *)srcp; \
1556 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1559 struct sctp_inpcb *inp = NULL;
1561 struct sctp_tcb *stcb = NULL;
1563 if (optval == NULL) {
1564 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1567 inp = (struct sctp_inpcb *)so->so_pcb;
1569 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1576 case SCTP_AUTOCLOSE:
1577 case SCTP_EXPLICIT_EOR:
1578 case SCTP_AUTO_ASCONF:
1579 case SCTP_DISABLE_FRAGMENTS:
1580 case SCTP_I_WANT_MAPPED_V4_ADDR:
1581 case SCTP_USE_EXT_RCVINFO:
1582 SCTP_INP_RLOCK(inp);
1584 case SCTP_DISABLE_FRAGMENTS:
1585 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1587 case SCTP_I_WANT_MAPPED_V4_ADDR:
1588 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1590 case SCTP_AUTO_ASCONF:
1591 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1592 /* only valid for bound all sockets */
1593 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1595 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1600 case SCTP_EXPLICIT_EOR:
1601 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1604 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1606 case SCTP_USE_EXT_RCVINFO:
1607 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1609 case SCTP_AUTOCLOSE:
1610 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1611 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1617 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1618 error = ENOPROTOOPT;
1619 } /* end switch (sopt->sopt_name) */
1620 if (optname != SCTP_AUTOCLOSE) {
1621 /* make it an "on/off" value */
1624 if (*optsize < sizeof(val)) {
1625 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1629 SCTP_INP_RUNLOCK(inp);
1631 /* return the option value */
1632 *(int *)optval = val;
1633 *optsize = sizeof(val);
1636 case SCTP_GET_PACKET_LOG:
1638 #ifdef SCTP_PACKET_LOGGING
1642 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1643 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1646 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1651 case SCTP_REUSE_PORT:
1655 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1656 /* Can't do this for a 1-m socket */
1660 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1661 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1662 *optsize = sizeof(uint32_t);
1665 case SCTP_PARTIAL_DELIVERY_POINT:
1669 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1670 *value = inp->partial_delivery_point;
1671 *optsize = sizeof(uint32_t);
1674 case SCTP_FRAGMENT_INTERLEAVE:
1678 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1679 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1680 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1681 *value = SCTP_FRAG_LEVEL_2;
1683 *value = SCTP_FRAG_LEVEL_1;
1686 *value = SCTP_FRAG_LEVEL_0;
1688 *optsize = sizeof(uint32_t);
1691 case SCTP_CMT_ON_OFF:
1693 struct sctp_assoc_value *av;
1695 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1696 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1697 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1699 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1700 SCTP_TCB_UNLOCK(stcb);
1703 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1707 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1708 error = ENOPROTOOPT;
1710 *optsize = sizeof(*av);
1713 /* EY - set socket option for nr_sacks */
1714 case SCTP_NR_SACK_ON_OFF:
1716 struct sctp_assoc_value *av;
1718 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1719 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
1720 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1722 av->assoc_value = stcb->asoc.sctp_nr_sack_on_off;
1723 SCTP_TCB_UNLOCK(stcb);
1726 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1731 error = ENOPROTOOPT;
1733 *optsize = sizeof(*av);
1736 /* JRS - Get socket option for pluggable congestion control */
1737 case SCTP_PLUGGABLE_CC:
1739 struct sctp_assoc_value *av;
1741 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1742 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1744 av->assoc_value = stcb->asoc.congestion_control_module;
1745 SCTP_TCB_UNLOCK(stcb);
1747 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1749 *optsize = sizeof(*av);
1752 case SCTP_GET_ADDR_LEN:
1754 struct sctp_assoc_value *av;
1756 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1759 if (av->assoc_value == AF_INET) {
1760 av->assoc_value = sizeof(struct sockaddr_in);
1765 if (av->assoc_value == AF_INET6) {
1766 av->assoc_value = sizeof(struct sockaddr_in6);
1771 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1773 *optsize = sizeof(*av);
1776 case SCTP_GET_ASSOC_NUMBER:
1778 uint32_t *value, cnt;
1780 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1782 SCTP_INP_RLOCK(inp);
1783 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1786 SCTP_INP_RUNLOCK(inp);
1788 *optsize = sizeof(uint32_t);
1792 case SCTP_GET_ASSOC_ID_LIST:
1794 struct sctp_assoc_ids *ids;
1795 unsigned int at, limit;
1797 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1799 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1800 SCTP_INP_RLOCK(inp);
1801 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1803 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1806 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1810 SCTP_INP_RUNLOCK(inp);
1811 ids->gaids_number_of_ids = at;
1812 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1817 struct sctp_assoc_value *av;
1819 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1820 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1823 av->assoc_value = stcb->asoc.context;
1824 SCTP_TCB_UNLOCK(stcb);
1826 SCTP_INP_RLOCK(inp);
1827 av->assoc_value = inp->sctp_context;
1828 SCTP_INP_RUNLOCK(inp);
1830 *optsize = sizeof(*av);
1835 uint32_t *default_vrfid;
1837 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1838 *default_vrfid = inp->def_vrf_id;
1841 case SCTP_GET_ASOC_VRF:
1843 struct sctp_assoc_value *id;
1845 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1846 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1849 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1852 id->assoc_value = stcb->asoc.vrf_id;
1855 case SCTP_GET_VRF_IDS:
1857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1861 case SCTP_GET_NONCE_VALUES:
1863 struct sctp_get_nonce_values *gnv;
1865 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1866 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1869 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1870 gnv->gn_local_tag = stcb->asoc.my_vtag;
1871 SCTP_TCB_UNLOCK(stcb);
1873 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1876 *optsize = sizeof(*gnv);
1879 case SCTP_DELAYED_SACK:
1881 struct sctp_sack_info *sack;
1883 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1884 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1886 sack->sack_delay = stcb->asoc.delayed_ack;
1887 sack->sack_freq = stcb->asoc.sack_freq;
1888 SCTP_TCB_UNLOCK(stcb);
1890 SCTP_INP_RLOCK(inp);
1891 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1892 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1893 SCTP_INP_RUNLOCK(inp);
1895 *optsize = sizeof(*sack);
1899 case SCTP_GET_SNDBUF_USE:
1901 struct sctp_sockstat *ss;
1903 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1904 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1907 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1908 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1909 stcb->asoc.size_on_all_streams);
1910 SCTP_TCB_UNLOCK(stcb);
1912 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1915 *optsize = sizeof(struct sctp_sockstat);
1918 case SCTP_MAX_BURST:
1922 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1924 SCTP_INP_RLOCK(inp);
1925 *value = inp->sctp_ep.max_burst;
1926 SCTP_INP_RUNLOCK(inp);
1927 *optsize = sizeof(uint8_t);
1932 struct sctp_assoc_value *av;
1935 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1936 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1939 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1940 SCTP_TCB_UNLOCK(stcb);
1942 SCTP_INP_RLOCK(inp);
1943 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1944 ovh = SCTP_MED_OVERHEAD;
1946 ovh = SCTP_MED_V4_OVERHEAD;
1948 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1949 av->assoc_value = 0;
1951 av->assoc_value = inp->sctp_frag_point - ovh;
1952 SCTP_INP_RUNLOCK(inp);
1954 *optsize = sizeof(struct sctp_assoc_value);
1957 case SCTP_GET_STAT_LOG:
1958 error = sctp_fill_stat_log(optval, optsize);
1962 struct sctp_event_subscribe *events;
1964 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1965 memset(events, 0, sizeof(*events));
1966 SCTP_INP_RLOCK(inp);
1967 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1968 events->sctp_data_io_event = 1;
1970 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1971 events->sctp_association_event = 1;
1973 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1974 events->sctp_address_event = 1;
1976 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1977 events->sctp_send_failure_event = 1;
1979 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1980 events->sctp_peer_error_event = 1;
1982 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1983 events->sctp_shutdown_event = 1;
1985 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1986 events->sctp_partial_delivery_event = 1;
1988 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1989 events->sctp_adaptation_layer_event = 1;
1991 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1992 events->sctp_authentication_event = 1;
1994 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
1995 events->sctp_sender_dry_event = 1;
1997 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1998 events->sctp_stream_reset_events = 1;
1999 SCTP_INP_RUNLOCK(inp);
2000 *optsize = sizeof(struct sctp_event_subscribe);
2004 case SCTP_ADAPTATION_LAYER:
2008 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2010 SCTP_INP_RLOCK(inp);
2011 *value = inp->sctp_ep.adaptation_layer_indicator;
2012 SCTP_INP_RUNLOCK(inp);
2013 *optsize = sizeof(uint32_t);
2016 case SCTP_SET_INITIAL_DBG_SEQ:
2020 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2021 SCTP_INP_RLOCK(inp);
2022 *value = inp->sctp_ep.initial_sequence_debug;
2023 SCTP_INP_RUNLOCK(inp);
2024 *optsize = sizeof(uint32_t);
2027 case SCTP_GET_LOCAL_ADDR_SIZE:
2031 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2032 SCTP_INP_RLOCK(inp);
2033 *value = sctp_count_max_addresses(inp);
2034 SCTP_INP_RUNLOCK(inp);
2035 *optsize = sizeof(uint32_t);
2038 case SCTP_GET_REMOTE_ADDR_SIZE:
2042 struct sctp_nets *net;
2044 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2045 /* FIXME MT: change to sctp_assoc_value? */
2046 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2050 /* Count the sizes */
2051 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2052 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2053 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2054 size += sizeof(struct sockaddr_in6);
2055 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2056 size += sizeof(struct sockaddr_in);
2062 SCTP_TCB_UNLOCK(stcb);
2063 *value = (uint32_t) size;
2065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2068 *optsize = sizeof(uint32_t);
2071 case SCTP_GET_PEER_ADDRESSES:
2073 * Get the address information, an array is passed in to
2074 * fill up we pack it.
2078 struct sockaddr_storage *sas;
2079 struct sctp_nets *net;
2080 struct sctp_getaddresses *saddr;
2082 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2083 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2086 left = (*optsize) - sizeof(struct sctp_getaddresses);
2087 *optsize = sizeof(struct sctp_getaddresses);
2088 sas = (struct sockaddr_storage *)&saddr->addr[0];
2090 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2091 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2092 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2093 cpsz = sizeof(struct sockaddr_in6);
2094 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2095 cpsz = sizeof(struct sockaddr_in);
2101 /* not enough room. */
2105 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2106 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2107 /* Must map the address */
2108 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2109 (struct sockaddr_in6 *)sas);
2112 memcpy(sas, &net->ro._l_addr, cpsz);
2116 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2118 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2122 SCTP_TCB_UNLOCK(stcb);
2124 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2129 case SCTP_GET_LOCAL_ADDRESSES:
2131 size_t limit, actual;
2132 struct sockaddr_storage *sas;
2133 struct sctp_getaddresses *saddr;
2135 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2136 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2138 sas = (struct sockaddr_storage *)&saddr->addr[0];
2139 limit = *optsize - sizeof(sctp_assoc_t);
2140 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2142 SCTP_TCB_UNLOCK(stcb);
2144 *optsize = sizeof(struct sockaddr_storage) + actual;
2147 case SCTP_PEER_ADDR_PARAMS:
2149 struct sctp_paddrparams *paddrp;
2150 struct sctp_nets *net;
2152 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2153 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2157 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2160 * We increment here since
2161 * sctp_findassociation_ep_addr() wil do a
2162 * decrement if it finds the stcb as long as
2163 * the locked tcb (last argument) is NOT a
2166 SCTP_INP_INCR_REF(inp);
2167 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2169 SCTP_INP_DECR_REF(inp);
2172 if (stcb && (net == NULL)) {
2173 struct sockaddr *sa;
2175 sa = (struct sockaddr *)&paddrp->spp_address;
2176 if (sa->sa_family == AF_INET) {
2177 struct sockaddr_in *sin;
2179 sin = (struct sockaddr_in *)sa;
2180 if (sin->sin_addr.s_addr) {
2182 SCTP_TCB_UNLOCK(stcb);
2183 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2186 } else if (sa->sa_family == AF_INET6) {
2187 struct sockaddr_in6 *sin6;
2189 sin6 = (struct sockaddr_in6 *)sa;
2190 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2192 SCTP_TCB_UNLOCK(stcb);
2193 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2197 error = EAFNOSUPPORT;
2198 SCTP_TCB_UNLOCK(stcb);
2199 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2204 /* Applys to the specific association */
2205 paddrp->spp_flags = 0;
2209 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2210 ovh = SCTP_MED_OVERHEAD;
2212 ovh = SCTP_MED_V4_OVERHEAD;
2216 paddrp->spp_pathmaxrxt = net->failure_threshold;
2217 paddrp->spp_pathmtu = net->mtu - ovh;
2218 /* get flags for HB */
2219 if (net->dest_state & SCTP_ADDR_NOHB)
2220 paddrp->spp_flags |= SPP_HB_DISABLE;
2222 paddrp->spp_flags |= SPP_HB_ENABLE;
2223 /* get flags for PMTU */
2224 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2225 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2227 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2230 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2231 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2232 paddrp->spp_flags |= SPP_IPV4_TOS;
2236 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2237 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2238 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2243 * No destination so return default
2248 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2249 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2251 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2252 paddrp->spp_flags |= SPP_IPV4_TOS;
2255 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2256 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2258 /* default settings should be these */
2259 if (stcb->asoc.hb_is_disabled == 0) {
2260 paddrp->spp_flags |= SPP_HB_ENABLE;
2262 paddrp->spp_flags |= SPP_HB_DISABLE;
2264 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2265 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2270 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2273 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2274 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2275 SCTP_TCB_UNLOCK(stcb);
2277 /* Use endpoint defaults */
2278 SCTP_INP_RLOCK(inp);
2279 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2280 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2281 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2282 /* get inp's default */
2284 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2285 paddrp->spp_flags |= SPP_IPV4_TOS;
2288 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2289 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2290 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2293 /* can't return this */
2294 paddrp->spp_pathmtu = 0;
2296 /* default behavior, no stcb */
2297 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2299 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2300 paddrp->spp_flags |= SPP_HB_ENABLE;
2302 paddrp->spp_flags |= SPP_HB_DISABLE;
2304 SCTP_INP_RUNLOCK(inp);
2306 *optsize = sizeof(struct sctp_paddrparams);
2309 case SCTP_GET_PEER_ADDR_INFO:
2311 struct sctp_paddrinfo *paddri;
2312 struct sctp_nets *net;
2314 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2315 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2319 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2322 * We increment here since
2323 * sctp_findassociation_ep_addr() wil do a
2324 * decrement if it finds the stcb as long as
2325 * the locked tcb (last argument) is NOT a
2328 SCTP_INP_INCR_REF(inp);
2329 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2331 SCTP_INP_DECR_REF(inp);
2335 if ((stcb) && (net)) {
2336 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2337 paddri->spinfo_cwnd = net->cwnd;
2338 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2339 paddri->spinfo_rto = net->RTO;
2340 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2341 SCTP_TCB_UNLOCK(stcb);
2344 SCTP_TCB_UNLOCK(stcb);
2346 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2349 *optsize = sizeof(struct sctp_paddrinfo);
2352 case SCTP_PCB_STATUS:
2354 struct sctp_pcbinfo *spcb;
2356 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2357 sctp_fill_pcbinfo(spcb);
2358 *optsize = sizeof(struct sctp_pcbinfo);
2364 struct sctp_nets *net;
2365 struct sctp_status *sstat;
2367 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2368 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2371 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2376 * I think passing the state is fine since
2377 * sctp_constants.h will be available to the user
2380 sstat->sstat_state = stcb->asoc.state;
2381 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2382 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2383 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2385 * We can't include chunks that have been passed to
2386 * the socket layer. Only things in queue.
2388 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2389 stcb->asoc.cnt_on_all_streams);
2392 sstat->sstat_instrms = stcb->asoc.streamincnt;
2393 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2394 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2395 memcpy(&sstat->sstat_primary.spinfo_address,
2396 &stcb->asoc.primary_destination->ro._l_addr,
2397 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2398 net = stcb->asoc.primary_destination;
2399 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2401 * Again the user can get info from sctp_constants.h
2402 * for what the state of the network is.
2404 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2405 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2406 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2407 sstat->sstat_primary.spinfo_rto = net->RTO;
2408 sstat->sstat_primary.spinfo_mtu = net->mtu;
2409 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2410 SCTP_TCB_UNLOCK(stcb);
2411 *optsize = sizeof(*sstat);
2416 struct sctp_rtoinfo *srto;
2418 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2419 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2422 srto->srto_initial = stcb->asoc.initial_rto;
2423 srto->srto_max = stcb->asoc.maxrto;
2424 srto->srto_min = stcb->asoc.minrto;
2425 SCTP_TCB_UNLOCK(stcb);
2427 SCTP_INP_RLOCK(inp);
2428 srto->srto_initial = inp->sctp_ep.initial_rto;
2429 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2430 srto->srto_min = inp->sctp_ep.sctp_minrto;
2431 SCTP_INP_RUNLOCK(inp);
2433 *optsize = sizeof(*srto);
2436 case SCTP_ASSOCINFO:
2438 struct sctp_assocparams *sasoc;
2441 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2442 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2445 oldval = sasoc->sasoc_cookie_life;
2446 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2447 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2448 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2449 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2450 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2451 SCTP_TCB_UNLOCK(stcb);
2453 SCTP_INP_RLOCK(inp);
2454 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2455 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2456 sasoc->sasoc_number_peer_destinations = 0;
2457 sasoc->sasoc_peer_rwnd = 0;
2458 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2459 SCTP_INP_RUNLOCK(inp);
2461 *optsize = sizeof(*sasoc);
2464 case SCTP_DEFAULT_SEND_PARAM:
2466 struct sctp_sndrcvinfo *s_info;
2468 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2469 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2472 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2473 SCTP_TCB_UNLOCK(stcb);
2475 SCTP_INP_RLOCK(inp);
2476 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2477 SCTP_INP_RUNLOCK(inp);
2479 *optsize = sizeof(*s_info);
2484 struct sctp_initmsg *sinit;
2486 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2487 SCTP_INP_RLOCK(inp);
2488 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2489 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2490 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2491 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2492 SCTP_INP_RUNLOCK(inp);
2493 *optsize = sizeof(*sinit);
2496 case SCTP_PRIMARY_ADDR:
2497 /* we allow a "get" operation on this */
2499 struct sctp_setprim *ssp;
2501 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2502 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2505 /* simply copy out the sockaddr_storage... */
2509 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2510 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2512 memcpy(&ssp->ssp_addr,
2513 &stcb->asoc.primary_destination->ro._l_addr,
2515 SCTP_TCB_UNLOCK(stcb);
2517 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2520 *optsize = sizeof(*ssp);
2524 case SCTP_HMAC_IDENT:
2526 struct sctp_hmacalgo *shmac;
2527 sctp_hmaclist_t *hmaclist;
2531 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2533 SCTP_INP_RLOCK(inp);
2534 hmaclist = inp->sctp_ep.local_hmacs;
2535 if (hmaclist == NULL) {
2536 /* no HMACs to return */
2537 *optsize = sizeof(*shmac);
2538 SCTP_INP_RUNLOCK(inp);
2541 /* is there room for all of the hmac ids? */
2542 size = sizeof(*shmac) + (hmaclist->num_algo *
2543 sizeof(shmac->shmac_idents[0]));
2544 if ((size_t)(*optsize) < size) {
2545 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2547 SCTP_INP_RUNLOCK(inp);
2550 /* copy in the list */
2551 shmac->shmac_number_of_idents = hmaclist->num_algo;
2552 for (i = 0; i < hmaclist->num_algo; i++) {
2553 shmac->shmac_idents[i] = hmaclist->hmac[i];
2555 SCTP_INP_RUNLOCK(inp);
2559 case SCTP_AUTH_ACTIVE_KEY:
2561 struct sctp_authkeyid *scact;
2563 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2564 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2567 /* get the active key on the assoc */
2568 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2569 SCTP_TCB_UNLOCK(stcb);
2571 /* get the endpoint active key */
2572 SCTP_INP_RLOCK(inp);
2573 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2574 SCTP_INP_RUNLOCK(inp);
2576 *optsize = sizeof(*scact);
2579 case SCTP_LOCAL_AUTH_CHUNKS:
2581 struct sctp_authchunks *sac;
2582 sctp_auth_chklist_t *chklist = NULL;
2585 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2586 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2589 /* get off the assoc */
2590 chklist = stcb->asoc.local_auth_chunks;
2591 /* is there enough space? */
2592 size = sctp_auth_get_chklist_size(chklist);
2593 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2595 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2597 /* copy in the chunks */
2598 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2600 SCTP_TCB_UNLOCK(stcb);
2602 /* get off the endpoint */
2603 SCTP_INP_RLOCK(inp);
2604 chklist = inp->sctp_ep.local_auth_chunks;
2605 /* is there enough space? */
2606 size = sctp_auth_get_chklist_size(chklist);
2607 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2609 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2611 /* copy in the chunks */
2612 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2614 SCTP_INP_RUNLOCK(inp);
2616 *optsize = sizeof(struct sctp_authchunks) + size;
2619 case SCTP_PEER_AUTH_CHUNKS:
2621 struct sctp_authchunks *sac;
2622 sctp_auth_chklist_t *chklist = NULL;
2625 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2626 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2629 /* get off the assoc */
2630 chklist = stcb->asoc.peer_auth_chunks;
2631 /* is there enough space? */
2632 size = sctp_auth_get_chklist_size(chklist);
2633 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2635 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2637 /* copy in the chunks */
2638 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2640 SCTP_TCB_UNLOCK(stcb);
2642 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2645 *optsize = sizeof(struct sctp_authchunks) + size;
2651 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2652 error = ENOPROTOOPT;
2655 } /* end switch (sopt->sopt_name) */
2660 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2665 struct sctp_tcb *stcb = NULL;
2666 struct sctp_inpcb *inp = NULL;
2669 if (optval == NULL) {
2670 SCTP_PRINTF("optval is NULL\n");
2671 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2674 inp = (struct sctp_inpcb *)so->so_pcb;
2676 SCTP_PRINTF("inp is NULL?\n");
2677 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2680 vrf_id = inp->def_vrf_id;
2685 case SCTP_AUTOCLOSE:
2686 case SCTP_AUTO_ASCONF:
2687 case SCTP_EXPLICIT_EOR:
2688 case SCTP_DISABLE_FRAGMENTS:
2689 case SCTP_USE_EXT_RCVINFO:
2690 case SCTP_I_WANT_MAPPED_V4_ADDR:
2691 /* copy in the option value */
2692 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2697 case SCTP_DISABLE_FRAGMENTS:
2698 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2700 case SCTP_AUTO_ASCONF:
2702 * NOTE: we don't really support this flag
2704 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2705 /* only valid for bound all sockets */
2706 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2708 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2712 case SCTP_EXPLICIT_EOR:
2713 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2715 case SCTP_USE_EXT_RCVINFO:
2716 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2718 case SCTP_I_WANT_MAPPED_V4_ADDR:
2719 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2720 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2722 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2727 set_opt = SCTP_PCB_FLAGS_NODELAY;
2729 case SCTP_AUTOCLOSE:
2730 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2731 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2732 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2735 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2737 * The value is in ticks. Note this does not effect
2738 * old associations, only new ones.
2740 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2743 SCTP_INP_WLOCK(inp);
2745 sctp_feature_on(inp, set_opt);
2747 sctp_feature_off(inp, set_opt);
2749 SCTP_INP_WUNLOCK(inp);
2751 case SCTP_REUSE_PORT:
2753 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2754 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2755 /* Can't set it after we are bound */
2759 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2760 /* Can't do this for a 1-m socket */
2765 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2767 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2770 case SCTP_PARTIAL_DELIVERY_POINT:
2774 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2775 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2780 inp->partial_delivery_point = *value;
2783 case SCTP_FRAGMENT_INTERLEAVE:
2784 /* not yet until we re-write sctp_recvmsg() */
2788 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2789 if (*level == SCTP_FRAG_LEVEL_2) {
2790 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2791 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2792 } else if (*level == SCTP_FRAG_LEVEL_1) {
2793 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2794 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2795 } else if (*level == SCTP_FRAG_LEVEL_0) {
2796 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2797 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2800 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2805 case SCTP_CMT_ON_OFF:
2807 struct sctp_assoc_value *av;
2809 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2810 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2811 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2813 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2814 SCTP_TCB_UNLOCK(stcb);
2816 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2820 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2821 error = ENOPROTOOPT;
2825 /* EY nr_sack_on_off socket option */
2826 case SCTP_NR_SACK_ON_OFF:
2828 struct sctp_assoc_value *av;
2830 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2831 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
2832 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2834 stcb->asoc.sctp_nr_sack_on_off = (uint8_t) av->assoc_value;
2835 SCTP_TCB_UNLOCK(stcb);
2837 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2842 error = ENOPROTOOPT;
2846 /* JRS - Set socket option for pluggable congestion control */
2847 case SCTP_PLUGGABLE_CC:
2849 struct sctp_assoc_value *av;
2851 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2852 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2854 switch (av->assoc_value) {
2856 * JRS - Standard TCP congestion
2859 case SCTP_CC_RFC2581:
2861 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2862 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2863 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2864 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2865 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2866 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2867 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2868 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2869 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2870 SCTP_TCB_UNLOCK(stcb);
2874 * JRS - High Speed TCP congestion
2879 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2880 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2881 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2882 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2883 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2884 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2885 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2886 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2887 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2888 SCTP_TCB_UNLOCK(stcb);
2891 /* JRS - HTCP congestion control */
2894 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2895 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2896 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2897 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2898 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2899 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2900 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2901 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2902 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2903 SCTP_TCB_UNLOCK(stcb);
2907 * JRS - All other values are
2912 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2914 SCTP_TCB_UNLOCK(stcb);
2919 switch (av->assoc_value) {
2920 case SCTP_CC_RFC2581:
2923 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2926 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2933 case SCTP_CLR_STAT_LOG:
2934 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2939 struct sctp_assoc_value *av;
2941 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2942 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2945 stcb->asoc.context = av->assoc_value;
2946 SCTP_TCB_UNLOCK(stcb);
2948 SCTP_INP_WLOCK(inp);
2949 inp->sctp_context = av->assoc_value;
2950 SCTP_INP_WUNLOCK(inp);
2956 uint32_t *default_vrfid;
2958 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2959 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2960 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2964 inp->def_vrf_id = *default_vrfid;
2967 case SCTP_DEL_VRF_ID:
2969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2973 case SCTP_ADD_VRF_ID:
2975 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2979 case SCTP_DELAYED_SACK:
2981 struct sctp_sack_info *sack;
2983 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2984 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2985 if (sack->sack_delay) {
2986 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2987 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2990 if (sack->sack_delay) {
2991 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2992 sack->sack_delay = TICKS_TO_MSEC(1);
2994 stcb->asoc.delayed_ack = sack->sack_delay;
2996 if (sack->sack_freq) {
2997 stcb->asoc.sack_freq = sack->sack_freq;
2999 SCTP_TCB_UNLOCK(stcb);
3001 SCTP_INP_WLOCK(inp);
3002 if (sack->sack_delay) {
3003 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3004 sack->sack_delay = TICKS_TO_MSEC(1);
3006 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3008 if (sack->sack_freq) {
3009 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3011 SCTP_INP_WUNLOCK(inp);
3015 case SCTP_AUTH_CHUNK:
3017 struct sctp_authchunk *sauth;
3019 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3021 SCTP_INP_WLOCK(inp);
3022 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3023 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3026 SCTP_INP_WUNLOCK(inp);
3031 struct sctp_authkey *sca;
3032 struct sctp_keyhead *shared_keys;
3033 sctp_sharedkey_t *shared_key;
3034 sctp_key_t *key = NULL;
3037 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3038 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3039 size = optsize - sizeof(*sca);
3042 /* set it on the assoc */
3043 shared_keys = &stcb->asoc.shared_keys;
3044 /* clear the cached keys for this key id */
3045 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3047 * create the new shared key and
3051 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3053 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3055 SCTP_TCB_UNLOCK(stcb);
3059 shared_key = sctp_alloc_sharedkey();
3060 if (shared_key == NULL) {
3062 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3064 SCTP_TCB_UNLOCK(stcb);
3067 shared_key->key = key;
3068 shared_key->keyid = sca->sca_keynumber;
3069 error = sctp_insert_sharedkey(shared_keys, shared_key);
3070 SCTP_TCB_UNLOCK(stcb);
3072 /* set it on the endpoint */
3073 SCTP_INP_WLOCK(inp);
3074 shared_keys = &inp->sctp_ep.shared_keys;
3076 * clear the cached keys on all assocs for
3079 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3081 * create the new shared key and
3085 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3087 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3089 SCTP_INP_WUNLOCK(inp);
3093 shared_key = sctp_alloc_sharedkey();
3094 if (shared_key == NULL) {
3096 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3098 SCTP_INP_WUNLOCK(inp);
3101 shared_key->key = key;
3102 shared_key->keyid = sca->sca_keynumber;
3103 error = sctp_insert_sharedkey(shared_keys, shared_key);
3104 SCTP_INP_WUNLOCK(inp);
3108 case SCTP_HMAC_IDENT:
3110 struct sctp_hmacalgo *shmac;
3111 sctp_hmaclist_t *hmaclist;
3117 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3118 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3119 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3123 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3124 if (hmaclist == NULL) {
3125 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3129 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3130 hmacid = shmac->shmac_idents[i];
3131 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3132 /* invalid HMACs were found */ ;
3133 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3135 sctp_free_hmaclist(hmaclist);
3136 goto sctp_set_hmac_done;
3140 for (i = 0; i < hmaclist->num_algo; i++) {
3141 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3142 /* already in list */
3147 sctp_free_hmaclist(hmaclist);
3148 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3152 /* set it on the endpoint */
3153 SCTP_INP_WLOCK(inp);
3154 if (inp->sctp_ep.local_hmacs)
3155 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3156 inp->sctp_ep.local_hmacs = hmaclist;
3157 SCTP_INP_WUNLOCK(inp);
3161 case SCTP_AUTH_ACTIVE_KEY:
3163 struct sctp_authkeyid *scact;
3165 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3167 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3169 /* set the active key on the right place */
3171 /* set the active key on the assoc */
3172 if (sctp_auth_setactivekey(stcb,
3173 scact->scact_keynumber)) {
3174 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3175 SCTP_FROM_SCTP_USRREQ,
3179 SCTP_TCB_UNLOCK(stcb);
3181 /* set the active key on the endpoint */
3182 SCTP_INP_WLOCK(inp);
3183 if (sctp_auth_setactivekey_ep(inp,
3184 scact->scact_keynumber)) {
3185 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3186 SCTP_FROM_SCTP_USRREQ,
3190 SCTP_INP_WUNLOCK(inp);
3194 case SCTP_AUTH_DELETE_KEY:
3196 struct sctp_authkeyid *scdel;
3198 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3200 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3202 /* delete the key from the right place */
3204 if (sctp_delete_sharedkey(stcb,
3205 scdel->scact_keynumber)) {
3206 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3207 SCTP_FROM_SCTP_USRREQ,
3211 SCTP_TCB_UNLOCK(stcb);
3213 SCTP_INP_WLOCK(inp);
3214 if (sctp_delete_sharedkey_ep(inp,
3215 scdel->scact_keynumber)) {
3216 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3217 SCTP_FROM_SCTP_USRREQ,
3221 SCTP_INP_WUNLOCK(inp);
3225 case SCTP_AUTH_DEACTIVATE_KEY:
3227 struct sctp_authkeyid *keyid;
3229 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3231 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3233 /* deactivate the key from the right place */
3235 if (sctp_deact_sharedkey(stcb,
3236 keyid->scact_keynumber)) {
3237 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3238 SCTP_FROM_SCTP_USRREQ,
3242 SCTP_TCB_UNLOCK(stcb);
3244 SCTP_INP_WLOCK(inp);
3245 if (sctp_deact_sharedkey_ep(inp,
3246 keyid->scact_keynumber)) {
3247 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3248 SCTP_FROM_SCTP_USRREQ,
3252 SCTP_INP_WUNLOCK(inp);
3257 case SCTP_RESET_STREAMS:
3259 struct sctp_stream_reset *strrst;
3260 uint8_t send_in = 0, send_tsn = 0, send_out = 0;
3263 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3264 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3267 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3271 if (stcb->asoc.peer_supports_strreset == 0) {
3273 * Peer does not support it, we return
3274 * protocol not supported since this is true
3275 * for this feature and this peer, not the
3276 * socket request in general.
3278 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3279 error = EPROTONOSUPPORT;
3280 SCTP_TCB_UNLOCK(stcb);
3283 if (stcb->asoc.stream_reset_outstanding) {
3284 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3286 SCTP_TCB_UNLOCK(stcb);
3289 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3291 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3293 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3296 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3299 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3301 SCTP_TCB_UNLOCK(stcb);
3304 for (i = 0; i < strrst->strrst_num_streams; i++) {
3307 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3308 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3313 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3314 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3321 SCTP_TCB_UNLOCK(stcb);
3324 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3325 strrst->strrst_list,
3326 send_out, (stcb->asoc.str_reset_seq_in - 3),
3329 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3330 SCTP_TCB_UNLOCK(stcb);
3334 case SCTP_CONNECT_X:
3335 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3336 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3340 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3343 case SCTP_CONNECT_X_DELAYED:
3344 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3345 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3349 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3352 case SCTP_CONNECT_X_COMPLETE:
3354 struct sockaddr *sa;
3355 struct sctp_nets *net;
3357 /* FIXME MT: check correct? */
3358 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3361 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3362 SCTP_INP_RLOCK(inp);
3363 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3365 SCTP_TCB_LOCK(stcb);
3366 net = sctp_findnet(stcb, sa);
3368 SCTP_INP_RUNLOCK(inp);
3371 * We increment here since
3372 * sctp_findassociation_ep_addr() wil do a
3373 * decrement if it finds the stcb as long as
3374 * the locked tcb (last argument) is NOT a
3377 SCTP_INP_INCR_REF(inp);
3378 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3380 SCTP_INP_DECR_REF(inp);
3385 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3389 if (stcb->asoc.delayed_connection == 1) {
3390 stcb->asoc.delayed_connection = 0;
3391 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3392 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3393 stcb->asoc.primary_destination,
3394 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3395 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3398 * already expired or did not use delayed
3401 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3404 SCTP_TCB_UNLOCK(stcb);
3407 case SCTP_MAX_BURST:
3411 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3413 SCTP_INP_WLOCK(inp);
3415 inp->sctp_ep.max_burst = *burst;
3417 SCTP_INP_WUNLOCK(inp);
3422 struct sctp_assoc_value *av;
3425 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3426 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3428 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3429 ovh = SCTP_MED_OVERHEAD;
3431 ovh = SCTP_MED_V4_OVERHEAD;
3434 if (av->assoc_value) {
3435 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3437 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3439 SCTP_TCB_UNLOCK(stcb);
3441 SCTP_INP_WLOCK(inp);
3443 * FIXME MT: I think this is not in tune
3446 if (av->assoc_value) {
3447 inp->sctp_frag_point = (av->assoc_value + ovh);
3449 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3451 SCTP_INP_WUNLOCK(inp);
3457 struct sctp_event_subscribe *events;
3459 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3461 SCTP_INP_WLOCK(inp);
3462 if (events->sctp_data_io_event) {
3463 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3465 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3468 if (events->sctp_association_event) {
3469 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3471 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3474 if (events->sctp_address_event) {
3475 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3477 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3480 if (events->sctp_send_failure_event) {
3481 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3483 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3486 if (events->sctp_peer_error_event) {
3487 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3489 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3492 if (events->sctp_shutdown_event) {
3493 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3495 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3498 if (events->sctp_partial_delivery_event) {
3499 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3501 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3504 if (events->sctp_adaptation_layer_event) {
3505 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3507 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3510 if (events->sctp_authentication_event) {
3511 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3513 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3516 if (events->sctp_sender_dry_event) {
3517 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3519 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3522 if (events->sctp_stream_reset_events) {
3523 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3525 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3527 SCTP_INP_WUNLOCK(inp);
3531 case SCTP_ADAPTATION_LAYER:
3533 struct sctp_setadaptation *adap_bits;
3535 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3536 SCTP_INP_WLOCK(inp);
3537 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3538 SCTP_INP_WUNLOCK(inp);
3542 case SCTP_SET_INITIAL_DBG_SEQ:
3546 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3547 SCTP_INP_WLOCK(inp);
3548 inp->sctp_ep.initial_sequence_debug = *vvv;
3549 SCTP_INP_WUNLOCK(inp);
3553 case SCTP_DEFAULT_SEND_PARAM:
3555 struct sctp_sndrcvinfo *s_info;
3557 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3558 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3561 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3562 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3564 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3567 SCTP_TCB_UNLOCK(stcb);
3569 SCTP_INP_WLOCK(inp);
3570 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3571 SCTP_INP_WUNLOCK(inp);
3575 case SCTP_PEER_ADDR_PARAMS:
3576 /* Applys to the specific association */
3578 struct sctp_paddrparams *paddrp;
3579 struct sctp_nets *net;
3581 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3582 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3585 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3588 * We increment here since
3589 * sctp_findassociation_ep_addr() wil do a
3590 * decrement if it finds the stcb as long as
3591 * the locked tcb (last argument) is NOT a
3594 SCTP_INP_INCR_REF(inp);
3595 stcb = sctp_findassociation_ep_addr(&inp,
3596 (struct sockaddr *)&paddrp->spp_address,
3599 SCTP_INP_DECR_REF(inp);
3602 if (stcb && (net == NULL)) {
3603 struct sockaddr *sa;
3605 sa = (struct sockaddr *)&paddrp->spp_address;
3606 if (sa->sa_family == AF_INET) {
3607 struct sockaddr_in *sin;
3609 sin = (struct sockaddr_in *)sa;
3610 if (sin->sin_addr.s_addr) {
3611 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3612 SCTP_TCB_UNLOCK(stcb);
3616 } else if (sa->sa_family == AF_INET6) {
3617 struct sockaddr_in6 *sin6;
3619 sin6 = (struct sockaddr_in6 *)sa;
3620 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3621 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3622 SCTP_TCB_UNLOCK(stcb);
3627 error = EAFNOSUPPORT;
3628 SCTP_TCB_UNLOCK(stcb);
3629 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3634 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3636 SCTP_TCB_UNLOCK(stcb);
3637 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3640 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3642 SCTP_TCB_UNLOCK(stcb);
3643 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3647 /************************TCB SPECIFIC SET ******************/
3649 * do we change the timer for HB, we run
3654 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3655 ovh = SCTP_MED_OVERHEAD;
3657 ovh = SCTP_MED_V4_OVERHEAD;
3660 if (paddrp->spp_hbinterval)
3661 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3662 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3663 stcb->asoc.heart_beat_delay = 0;
3665 /* network sets ? */
3667 /************************NET SPECIFIC SET ******************/
3668 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3670 if (sctp_send_hb(stcb, 1, net) < 0) {
3671 /* asoc destroyed */
3672 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3677 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3678 net->dest_state |= SCTP_ADDR_NOHB;
3680 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3681 net->dest_state &= ~SCTP_ADDR_NOHB;
3683 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3684 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3685 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3686 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3688 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3689 net->mtu = paddrp->spp_pathmtu + ovh;
3690 if (net->mtu < stcb->asoc.smallest_mtu) {
3691 #ifdef SCTP_PRINT_FOR_B_AND_M
3692 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3695 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3699 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3700 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3701 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3704 if (paddrp->spp_pathmaxrxt)
3705 net->failure_threshold = paddrp->spp_pathmaxrxt;
3707 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3708 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3709 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3714 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3715 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3716 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3721 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3722 if (paddrp->spp_pathmaxrxt)
3723 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3725 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3726 /* Turn back on the timer */
3727 stcb->asoc.hb_is_disabled = 0;
3728 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3730 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3731 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3732 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3733 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3734 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3736 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3737 net->mtu = paddrp->spp_pathmtu + ovh;
3738 if (net->mtu < stcb->asoc.smallest_mtu) {
3739 #ifdef SCTP_PRINT_FOR_B_AND_M
3740 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3743 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3748 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3749 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3750 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3751 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3755 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3756 int cnt_of_unconf = 0;
3757 struct sctp_nets *lnet;
3759 stcb->asoc.hb_is_disabled = 1;
3760 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3761 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3766 * stop the timer ONLY if we
3767 * have no unconfirmed
3770 if (cnt_of_unconf == 0) {
3771 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3772 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3773 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3777 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3778 /* start up the timer. */
3779 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3780 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3784 if (paddrp->spp_flags & SPP_IPV4_TOS)
3785 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3788 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3789 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3793 SCTP_TCB_UNLOCK(stcb);
3795 /************************NO TCB, SET TO default stuff ******************/
3796 SCTP_INP_WLOCK(inp);
3798 * For the TOS/FLOWLABEL stuff you set it
3799 * with the options on the socket
3801 if (paddrp->spp_pathmaxrxt) {
3802 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3804 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3805 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3806 else if (paddrp->spp_hbinterval) {
3807 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3808 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3809 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3811 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3812 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3814 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3815 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3817 SCTP_INP_WUNLOCK(inp);
3823 struct sctp_rtoinfo *srto;
3824 uint32_t new_init, new_min, new_max;
3826 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3827 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3830 if (srto->srto_initial)
3831 new_init = srto->srto_initial;
3833 new_init = stcb->asoc.initial_rto;
3835 new_max = srto->srto_max;
3837 new_max = stcb->asoc.maxrto;
3839 new_min = srto->srto_min;
3841 new_min = stcb->asoc.minrto;
3842 if ((new_min <= new_init) && (new_init <= new_max)) {
3843 stcb->asoc.initial_rto = new_init;
3844 stcb->asoc.maxrto = new_max;
3845 stcb->asoc.minrto = new_min;
3847 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3850 SCTP_TCB_UNLOCK(stcb);
3852 SCTP_INP_WLOCK(inp);
3853 if (srto->srto_initial)
3854 new_init = srto->srto_initial;
3856 new_init = inp->sctp_ep.initial_rto;
3858 new_max = srto->srto_max;
3860 new_max = inp->sctp_ep.sctp_maxrto;
3862 new_min = srto->srto_min;
3864 new_min = inp->sctp_ep.sctp_minrto;
3865 if ((new_min <= new_init) && (new_init <= new_max)) {
3866 inp->sctp_ep.initial_rto = new_init;
3867 inp->sctp_ep.sctp_maxrto = new_max;
3868 inp->sctp_ep.sctp_minrto = new_min;
3870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3873 SCTP_INP_WUNLOCK(inp);
3877 case SCTP_ASSOCINFO:
3879 struct sctp_assocparams *sasoc;
3881 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3882 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3883 if (sasoc->sasoc_cookie_life) {
3884 /* boundary check the cookie life */
3885 if (sasoc->sasoc_cookie_life < 1000)
3886 sasoc->sasoc_cookie_life = 1000;
3887 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3888 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3892 if (sasoc->sasoc_asocmaxrxt)
3893 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3894 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3895 sasoc->sasoc_peer_rwnd = 0;
3896 sasoc->sasoc_local_rwnd = 0;
3897 if (sasoc->sasoc_cookie_life) {
3898 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3900 SCTP_TCB_UNLOCK(stcb);
3902 SCTP_INP_WLOCK(inp);
3903 if (sasoc->sasoc_asocmaxrxt)
3904 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3905 sasoc->sasoc_number_peer_destinations = 0;
3906 sasoc->sasoc_peer_rwnd = 0;
3907 sasoc->sasoc_local_rwnd = 0;
3908 if (sasoc->sasoc_cookie_life) {
3909 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3911 SCTP_INP_WUNLOCK(inp);
3917 struct sctp_initmsg *sinit;
3919 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3920 SCTP_INP_WLOCK(inp);
3921 if (sinit->sinit_num_ostreams)
3922 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3924 if (sinit->sinit_max_instreams)
3925 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3927 if (sinit->sinit_max_attempts)
3928 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3930 if (sinit->sinit_max_init_timeo)
3931 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3932 SCTP_INP_WUNLOCK(inp);
3935 case SCTP_PRIMARY_ADDR:
3937 struct sctp_setprim *spa;
3938 struct sctp_nets *net, *lnet;
3940 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3941 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3945 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3948 * We increment here since
3949 * sctp_findassociation_ep_addr() wil do a
3950 * decrement if it finds the stcb as long as
3951 * the locked tcb (last argument) is NOT a
3954 SCTP_INP_INCR_REF(inp);
3955 stcb = sctp_findassociation_ep_addr(&inp,
3956 (struct sockaddr *)&spa->ssp_addr,
3959 SCTP_INP_DECR_REF(inp);
3963 if ((stcb) && (net)) {
3964 if ((net != stcb->asoc.primary_destination) &&
3965 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3966 /* Ok we need to set it */
3967 lnet = stcb->asoc.primary_destination;
3968 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3969 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3970 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3972 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3976 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3980 SCTP_TCB_UNLOCK(stcb);
3984 case SCTP_SET_DYNAMIC_PRIMARY:
3986 union sctp_sockstore *ss;
3988 error = priv_check(curthread,
3989 PRIV_NETINET_RESERVEDPORT);
3993 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3994 /* SUPER USER CHECK? */
3995 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3998 case SCTP_SET_PEER_PRIMARY_ADDR:
4000 struct sctp_setpeerprim *sspp;
4002 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4003 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4005 struct sctp_ifa *ifa;
4007 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4008 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4010 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4014 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4016 * Must validate the ifa found is in
4019 struct sctp_laddr *laddr;
4022 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4023 if (laddr->ifa == NULL) {
4024 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4028 if (laddr->ifa == ifa) {
4034 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4039 if (sctp_set_primary_ip_address_sa(stcb,
4040 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4041 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4045 SCTP_TCB_UNLOCK(stcb);
4047 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4053 case SCTP_BINDX_ADD_ADDR:
4055 struct sctp_getaddresses *addrs;
4059 td = (struct thread *)p;
4060 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4062 if (addrs->addr->sa_family == AF_INET) {
4063 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4069 if (td != NULL && prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr))) {
4070 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
4071 error = EADDRNOTAVAIL;
4075 } else if (addrs->addr->sa_family == AF_INET6) {
4076 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4082 if (td != NULL && prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4083 (SCTP_IPV6_V6ONLY(inp) != 0)) != 0) {
4084 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
4085 error = EADDRNOTAVAIL;
4090 error = EAFNOSUPPORT;
4093 sctp_bindx_add_address(so, inp, addrs->addr,
4094 addrs->sget_assoc_id, vrf_id,
4098 case SCTP_BINDX_REM_ADDR:
4100 struct sctp_getaddresses *addrs;
4104 td = (struct thread *)p;
4106 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4107 if (addrs->addr->sa_family == AF_INET) {
4108 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4110 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4114 if (td != NULL && prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr))) {
4115 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
4116 error = EADDRNOTAVAIL;
4120 } else if (addrs->addr->sa_family == AF_INET6) {
4121 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4123 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4127 if (td != NULL && prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4128 (SCTP_IPV6_V6ONLY(inp) != 0)) != 0) {
4129 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRNOTAVAIL);
4130 error = EADDRNOTAVAIL;
4135 error = EAFNOSUPPORT;
4138 sctp_bindx_delete_address(so, inp, addrs->addr,
4139 addrs->sget_assoc_id, vrf_id,
4144 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4145 error = ENOPROTOOPT;
4147 } /* end switch (opt) */
4152 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4154 void *optval = NULL;
4156 struct sctp_inpcb *inp;
4160 inp = (struct sctp_inpcb *)so->so_pcb;
4162 /* I made the same as TCP since we are not setup? */
4163 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4164 return (ECONNRESET);
4166 if (sopt->sopt_level != IPPROTO_SCTP) {
4167 /* wrong proto level... send back up to IP */
4169 if (INP_CHECK_SOCKAF(so, AF_INET6))
4170 error = ip6_ctloutput(so, sopt);
4173 error = ip_ctloutput(so, sopt);
4176 optsize = sopt->sopt_valsize;
4178 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4179 if (optval == NULL) {
4180 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4183 error = sooptcopyin(sopt, optval, optsize, optsize);
4185 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4189 p = (void *)sopt->sopt_td;
4190 if (sopt->sopt_dir == SOPT_SET) {
4191 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4192 } else if (sopt->sopt_dir == SOPT_GET) {
4193 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4195 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4198 if ((error == 0) && (optval != NULL)) {
4199 error = sooptcopyout(sopt, optval, optsize);
4200 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4201 } else if (optval != NULL) {
4202 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4210 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4213 int create_lock_on = 0;
4215 struct sctp_inpcb *inp;
4216 struct sctp_tcb *stcb = NULL;
4218 inp = (struct sctp_inpcb *)so->so_pcb;
4220 /* I made the same as TCP since we are not setup? */
4221 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4222 return (ECONNRESET);
4225 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4229 if (addr->sa_family == AF_INET6) {
4230 struct sockaddr_in6 *sin6p;
4232 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4233 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4236 sin6p = (struct sockaddr_in6 *)addr;
4237 if (p != NULL && prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr) != 0) {
4238 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4243 if (addr->sa_family == AF_INET) {
4244 struct sockaddr_in *sinp;
4246 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4247 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4250 sinp = (struct sockaddr_in *)addr;
4251 if (p != NULL && prison_remote_ip4(p->td_ucred, &sinp->sin_addr) != 0) {
4252 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4256 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4257 return (EAFNOSUPPORT);
4259 SCTP_INP_INCR_REF(inp);
4260 SCTP_ASOC_CREATE_LOCK(inp);
4263 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4264 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4265 /* Should I really unlock ? */
4266 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4271 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4272 (addr->sa_family == AF_INET6)) {
4273 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4278 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4279 SCTP_PCB_FLAGS_UNBOUND) {
4280 /* Bind a ephemeral port */
4281 error = sctp_inpcb_bind(so, NULL, NULL, p);
4286 /* Now do we connect? */
4287 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4288 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4289 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4293 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4294 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4295 /* We are already connected AND the TCP model */
4296 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4300 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4301 SCTP_INP_RLOCK(inp);
4302 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4303 SCTP_INP_RUNLOCK(inp);
4306 * We increment here since sctp_findassociation_ep_addr()
4307 * will do a decrement if it finds the stcb as long as the
4308 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4310 SCTP_INP_INCR_REF(inp);
4311 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4313 SCTP_INP_DECR_REF(inp);
4315 SCTP_TCB_UNLOCK(stcb);
4319 /* Already have or am bring up an association */
4320 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4324 vrf_id = inp->def_vrf_id;
4325 /* We are GOOD to go */
4326 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4328 /* Gak! no memory */
4331 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4332 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4333 /* Set the connected flag so we can queue data */
4336 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4337 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4339 /* initialize authentication parameters for the assoc */
4340 sctp_initialize_auth_params(inp, stcb);
4342 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4343 SCTP_TCB_UNLOCK(stcb);
4345 if (create_lock_on) {
4346 SCTP_ASOC_CREATE_UNLOCK(inp);
4348 SCTP_INP_DECR_REF(inp);
4353 sctp_listen(struct socket *so, int backlog, struct thread *p)
4356 * Note this module depends on the protocol processing being called
4357 * AFTER any socket level flags and backlog are applied to the
4358 * socket. The traditional way that the socket flags are applied is
4359 * AFTER protocol processing. We have made a change to the
4360 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4361 * place if the socket API for SCTP is to work properly.
4365 struct sctp_inpcb *inp;
4367 inp = (struct sctp_inpcb *)so->so_pcb;
4369 /* I made the same as TCP since we are not setup? */
4370 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4371 return (ECONNRESET);
4373 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4374 /* See if we have a listener */
4375 struct sctp_inpcb *tinp;
4376 union sctp_sockstore store, *sp;
4379 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4381 struct sctp_laddr *laddr;
4383 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4384 memcpy(&store, &laddr->ifa->address, sizeof(store));
4385 sp->sin.sin_port = inp->sctp_lport;
4386 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4387 if (tinp && (tinp != inp) &&
4388 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4389 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4390 (tinp->sctp_socket->so_qlimit)) {
4392 * we have a listener already and
4395 SCTP_INP_DECR_REF(tinp);
4396 return (EADDRINUSE);
4398 SCTP_INP_DECR_REF(tinp);
4402 /* Setup a local addr bound all */
4403 memset(&store, 0, sizeof(store));
4404 store.sin.sin_port = inp->sctp_lport;
4406 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4407 store.sa.sa_family = AF_INET6;
4408 store.sa.sa_len = sizeof(struct sockaddr_in6);
4411 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4412 store.sa.sa_family = AF_INET;
4413 store.sa.sa_len = sizeof(struct sockaddr_in);
4415 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4416 if (tinp && (tinp != inp) &&
4417 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4418 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4419 (tinp->sctp_socket->so_qlimit)) {
4421 * we have a listener already and its not
4424 SCTP_INP_DECR_REF(tinp);
4425 return (EADDRINUSE);
4427 SCTP_INP_DECR_REF(inp);
4431 SCTP_INP_RLOCK(inp);
4432 #ifdef SCTP_LOCK_LOGGING
4433 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4434 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4438 error = solisten_proto_check(so);
4441 SCTP_INP_RUNLOCK(inp);
4444 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4445 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4447 * The unlucky case - We are in the tcp pool with this guy.
4448 * - Someone else is in the main inp slot. - We must move
4449 * this guy (the listener) to the main slot - We must then
4450 * move the guy that was listener to the TCP Pool.
4452 if (sctp_swap_inpcb_for_listen(inp)) {
4456 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4457 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4458 /* We are already connected AND the TCP model */
4460 SCTP_INP_RUNLOCK(inp);
4462 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4463 return (EADDRINUSE);
4465 SCTP_INP_RUNLOCK(inp);
4466 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4467 /* We must do a bind. */
4469 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4470 /* bind error, probably perm */
4475 /* It appears for 7.0 and on, we must always call this. */
4476 solisten_proto(so, backlog);
4477 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4478 /* remove the ACCEPTCONN flag for one-to-many sockets */
4479 so->so_options &= ~SO_ACCEPTCONN;
4482 /* turning off listen */
4483 so->so_options &= ~SO_ACCEPTCONN;
4489 static int sctp_defered_wakeup_cnt = 0;
4492 sctp_accept(struct socket *so, struct sockaddr **addr)
4494 struct sctp_tcb *stcb;
4495 struct sctp_inpcb *inp;
4496 union sctp_sockstore store;
4502 inp = (struct sctp_inpcb *)so->so_pcb;
4505 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4506 return (ECONNRESET);
4508 SCTP_INP_RLOCK(inp);
4509 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4510 SCTP_INP_RUNLOCK(inp);
4511 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4512 return (EOPNOTSUPP);
4514 if (so->so_state & SS_ISDISCONNECTED) {
4515 SCTP_INP_RUNLOCK(inp);
4516 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4517 return (ECONNABORTED);
4519 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4521 SCTP_INP_RUNLOCK(inp);
4522 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4523 return (ECONNRESET);
4525 SCTP_TCB_LOCK(stcb);
4526 SCTP_INP_RUNLOCK(inp);
4527 store = stcb->asoc.primary_destination->ro._l_addr;
4528 SCTP_TCB_UNLOCK(stcb);
4529 switch (store.sa.sa_family) {
4532 struct sockaddr_in *sin;
4534 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4535 sin->sin_family = AF_INET;
4536 sin->sin_len = sizeof(*sin);
4537 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4538 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4539 *addr = (struct sockaddr *)sin;
4545 struct sockaddr_in6 *sin6;
4547 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4548 sin6->sin6_family = AF_INET6;
4549 sin6->sin6_len = sizeof(*sin6);
4550 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4552 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4553 if ((error = sa6_recoverscope(sin6)) != 0) {
4554 SCTP_FREE_SONAME(sin6);
4557 *addr = (struct sockaddr *)sin6;
4565 /* Wake any delayed sleep action */
4566 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4567 SCTP_INP_WLOCK(inp);
4568 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4569 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4570 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4571 SCTP_INP_WUNLOCK(inp);
4572 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4573 if (sowriteable(inp->sctp_socket)) {
4574 sowwakeup_locked(inp->sctp_socket);
4576 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4578 SCTP_INP_WLOCK(inp);
4580 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4581 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4582 SCTP_INP_WUNLOCK(inp);
4583 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4584 if (soreadable(inp->sctp_socket)) {
4585 sctp_defered_wakeup_cnt++;
4586 sorwakeup_locked(inp->sctp_socket);
4588 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4590 SCTP_INP_WLOCK(inp);
4592 SCTP_INP_WUNLOCK(inp);
4598 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4600 struct sockaddr_in *sin;
4602 struct sctp_inpcb *inp;
4603 struct sctp_ifa *sctp_ifa;
4606 * Do the malloc first in case it blocks.
4608 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4609 sin->sin_family = AF_INET;
4610 sin->sin_len = sizeof(*sin);
4611 inp = (struct sctp_inpcb *)so->so_pcb;
4613 SCTP_FREE_SONAME(sin);
4614 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4617 SCTP_INP_RLOCK(inp);
4618 sin->sin_port = inp->sctp_lport;
4619 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4620 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4621 struct sctp_tcb *stcb;
4622 struct sockaddr_in *sin_a;
4623 struct sctp_nets *net;
4626 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4632 SCTP_TCB_LOCK(stcb);
4633 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4634 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4636 /* this will make coverity happy */
4639 if (sin_a->sin_family == AF_INET) {
4644 if ((!fnd) || (sin_a == NULL)) {
4646 SCTP_TCB_UNLOCK(stcb);
4649 vrf_id = inp->def_vrf_id;
4650 sctp_ifa = sctp_source_address_selection(inp,
4652 (sctp_route_t *) & net->ro,
4655 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4656 sctp_free_ifa(sctp_ifa);
4658 SCTP_TCB_UNLOCK(stcb);
4660 /* For the bound all case you get back 0 */
4662 sin->sin_addr.s_addr = 0;
4666 /* Take the first IPv4 address in the list */
4667 struct sctp_laddr *laddr;
4670 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4671 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4672 struct sockaddr_in *sin_a;
4674 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4675 sin->sin_addr = sin_a->sin_addr;
4681 SCTP_FREE_SONAME(sin);
4682 SCTP_INP_RUNLOCK(inp);
4683 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4687 SCTP_INP_RUNLOCK(inp);
4688 (*addr) = (struct sockaddr *)sin;
4693 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4695 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4697 struct sockaddr_in *sin_a;
4698 struct sctp_inpcb *inp;
4699 struct sctp_tcb *stcb;
4700 struct sctp_nets *net;
4702 /* Do the malloc first in case it blocks. */
4703 inp = (struct sctp_inpcb *)so->so_pcb;
4704 if ((inp == NULL) ||
4705 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4706 /* UDP type and listeners will drop out here */
4707 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4710 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4711 sin->sin_family = AF_INET;
4712 sin->sin_len = sizeof(*sin);
4714 /* We must recapture incase we blocked */
4715 inp = (struct sctp_inpcb *)so->so_pcb;
4717 SCTP_FREE_SONAME(sin);
4718 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4721 SCTP_INP_RLOCK(inp);
4722 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4724 SCTP_TCB_LOCK(stcb);
4726 SCTP_INP_RUNLOCK(inp);
4728 SCTP_FREE_SONAME(sin);
4729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4733 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4734 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4735 if (sin_a->sin_family == AF_INET) {
4737 sin->sin_port = stcb->rport;
4738 sin->sin_addr = sin_a->sin_addr;
4742 SCTP_TCB_UNLOCK(stcb);
4744 /* No IPv4 address */
4745 SCTP_FREE_SONAME(sin);
4746 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4749 (*addr) = (struct sockaddr *)sin;
4753 struct pr_usrreqs sctp_usrreqs = {
4754 .pru_abort = sctp_abort,
4755 .pru_accept = sctp_accept,
4756 .pru_attach = sctp_attach,
4757 .pru_bind = sctp_bind,
4758 .pru_connect = sctp_connect,
4759 .pru_control = in_control,
4760 .pru_close = sctp_close,
4761 .pru_detach = sctp_close,
4762 .pru_sopoll = sopoll_generic,
4763 .pru_flush = sctp_flush,
4764 .pru_disconnect = sctp_disconnect,
4765 .pru_listen = sctp_listen,
4766 .pru_peeraddr = sctp_peeraddr,
4767 .pru_send = sctp_sendm,
4768 .pru_shutdown = sctp_shutdown,
4769 .pru_sockaddr = sctp_ingetaddr,
4770 .pru_sosend = sctp_sosend,
4771 .pru_soreceive = sctp_soreceive