2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_var.h>
44 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/udp.h>
57 extern struct sctp_cc_functions sctp_cc_functions[];
58 extern struct sctp_ss_functions sctp_ss_functions[];
65 /* Initialize and modify the sysctled variables */
67 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
68 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
70 * Allow a user to take no more than 1/2 the number of clusters or
71 * the SB_MAX whichever is smaller for the send window.
73 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
74 SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
75 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
77 * Now for the recv window, should we take the same amount? or
78 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
79 * now I will just copy.
81 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
83 SCTP_BASE_VAR(first_time) = 0;
84 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
86 #if defined(SCTP_PACKET_LOGGING)
87 SCTP_BASE_VAR(packet_log_writers) = 0;
88 SCTP_BASE_VAR(packet_log_end) = 0;
89 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
104 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
105 struct sctp_tcb *stcb,
106 struct sctp_nets *net,
109 struct sctp_tmit_chunk *chk;
112 /* Adjust that too */
113 stcb->asoc.smallest_mtu = nxtsz;
114 /* now off to subtract IP_DF flag if needed */
115 overhead = IP_HDR_SIZE;
116 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
117 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
119 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
120 if ((chk->send_size + overhead) > nxtsz) {
121 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
124 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
125 if ((chk->send_size + overhead) > nxtsz) {
127 * For this guy we also mark for immediate resend
128 * since we sent to big of chunk
130 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
131 if (chk->sent < SCTP_DATAGRAM_RESEND) {
132 sctp_flight_size_decrease(chk);
133 sctp_total_flight_decrease(stcb, chk);
135 if (chk->sent != SCTP_DATAGRAM_RESEND) {
136 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
138 chk->sent = SCTP_DATAGRAM_RESEND;
139 chk->rec.data.doing_fast_retransmit = 0;
140 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
141 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
142 chk->whoTo->flight_size,
144 (uintptr_t) chk->whoTo,
145 chk->rec.data.TSN_seq);
147 /* Clear any time so NO RTT is being done */
155 sctp_notify_mbuf(struct sctp_inpcb *inp,
156 struct sctp_tcb *stcb,
157 struct sctp_nets *net,
162 int totsz, tmr_stopped = 0;
166 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
167 (ip == NULL) || (sh == NULL)) {
169 SCTP_TCB_UNLOCK(stcb);
173 /* First job is to verify the vtag matches what I would send */
174 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
175 SCTP_TCB_UNLOCK(stcb);
178 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
180 if (icmph->icmp_type != ICMP_UNREACH) {
181 /* We only care about unreachable */
182 SCTP_TCB_UNLOCK(stcb);
185 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
186 /* not a unreachable message due to frag. */
187 SCTP_TCB_UNLOCK(stcb);
192 nxtsz = ntohs(icmph->icmp_nextmtu);
195 * old type router that does not tell us what the next size
196 * mtu is. Rats we will have to guess (in a educated fashion
199 nxtsz = sctp_get_prev_mtu(totsz);
201 /* Stop any PMTU timer */
202 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
204 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
205 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
207 /* Adjust destination size limit */
208 if (net->mtu > nxtsz) {
211 net->mtu -= sizeof(struct udphdr);
214 /* now what about the ep? */
215 if (stcb->asoc.smallest_mtu > nxtsz) {
216 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
219 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
221 SCTP_TCB_UNLOCK(stcb);
227 sctp_notify(struct sctp_inpcb *inp,
231 struct sctp_tcb *stcb,
232 struct sctp_nets *net)
234 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
243 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
244 (sh == NULL) || (to == NULL)) {
246 SCTP_TCB_UNLOCK(stcb);
249 /* First job is to verify the vtag matches what I would send */
250 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
251 SCTP_TCB_UNLOCK(stcb);
254 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
256 if (icmph->icmp_type != ICMP_UNREACH) {
257 /* We only care about unreachable */
258 SCTP_TCB_UNLOCK(stcb);
261 if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
262 (icmph->icmp_code == ICMP_UNREACH_HOST) ||
263 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
264 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
265 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
266 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
267 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
268 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
271 * Hmm reachablity problems we must examine closely. If its
272 * not reachable, we may have lost a network. Or if there is
273 * NO protocol at the other end named SCTP. well we consider
276 if (net->dest_state & SCTP_ADDR_REACHABLE) {
277 /* Ok that destination is NOT reachable */
278 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
280 net->failure_threshold,
283 net->dest_state &= ~SCTP_ADDR_REACHABLE;
284 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
286 * JRS 5/14/07 - If a destination is unreachable,
287 * the PF bit is turned off. This allows an
288 * unambiguous use of the PF bit for destinations
289 * that are reachable but potentially failed. If the
290 * destination is set to the unreachable state, also
291 * set the destination to the PF state.
294 * Add debug message here if destination is not in
297 /* Stop any running T3 timers here? */
298 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
299 (stcb->asoc.sctp_cmt_pf > 0)) {
300 net->dest_state &= ~SCTP_ADDR_PF;
301 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
304 net->error_count = net->failure_threshold + 1;
305 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
306 stcb, SCTP_FAILED_THRESHOLD,
307 (void *)net, SCTP_SO_NOT_LOCKED);
309 SCTP_TCB_UNLOCK(stcb);
310 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
311 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
313 * Here the peer is either playing tricks on us, including
314 * an address that belongs to someone who does not support
315 * SCTP OR was a userland implementation that shutdown and
316 * now is dead. In either case treat it like a OOTB abort
319 reason = SCTP_PEER_FAULTY;
320 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
321 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
322 so = SCTP_INP_SO(inp);
323 atomic_add_int(&stcb->asoc.refcnt, 1);
324 SCTP_TCB_UNLOCK(stcb);
325 SCTP_SOCKET_LOCK(so, 1);
327 atomic_subtract_int(&stcb->asoc.refcnt, 1);
329 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
330 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
331 SCTP_SOCKET_UNLOCK(so, 1);
332 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
334 /* no need to unlock here, since the TCB is gone */
336 SCTP_TCB_UNLOCK(stcb);
342 sctp_ctlinput(cmd, sa, vip)
351 /* FIX, for non-bsd is this right? */
352 vrf_id = SCTP_DEFAULT_VRFID;
353 if (sa->sa_family != AF_INET ||
354 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
357 if (PRC_IS_REDIRECT(cmd)) {
359 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
363 struct sctp_inpcb *inp = NULL;
364 struct sctp_tcb *stcb = NULL;
365 struct sctp_nets *net = NULL;
366 struct sockaddr_in to, from;
368 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
369 bzero(&to, sizeof(to));
370 bzero(&from, sizeof(from));
371 from.sin_family = to.sin_family = AF_INET;
372 from.sin_len = to.sin_len = sizeof(to);
373 from.sin_port = sh->src_port;
374 from.sin_addr = ip->ip_src;
375 to.sin_port = sh->dest_port;
376 to.sin_addr = ip->ip_dst;
379 * 'to' holds the dest of the packet that failed to be sent.
380 * 'from' holds our local endpoint address. Thus we reverse
381 * the to and the from in the lookup.
383 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
384 (struct sockaddr *)&to,
385 &inp, &net, 1, vrf_id);
386 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
387 if (cmd != PRC_MSGSIZE) {
388 sctp_notify(inp, ip, sh,
389 (struct sockaddr *)&to, stcb,
392 /* handle possible ICMP size messages */
393 sctp_notify_mbuf(inp, stcb, net, ip, sh);
396 if ((stcb == NULL) && (inp != NULL)) {
397 /* reduce ref-count */
399 SCTP_INP_DECR_REF(inp);
400 SCTP_INP_WUNLOCK(inp);
403 SCTP_TCB_UNLOCK(stcb);
413 sctp_getcred(SYSCTL_HANDLER_ARGS)
416 struct sockaddr_in addrs[2];
417 struct sctp_inpcb *inp;
418 struct sctp_nets *net;
419 struct sctp_tcb *stcb;
423 /* FIX, for non-bsd is this right? */
424 vrf_id = SCTP_DEFAULT_VRFID;
426 error = priv_check(req->td, PRIV_NETINET_GETCRED);
431 error = SYSCTL_IN(req, addrs, sizeof(addrs));
435 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
437 &inp, &net, 1, vrf_id);
438 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
439 if ((inp != NULL) && (stcb == NULL)) {
440 /* reduce ref-count */
442 SCTP_INP_DECR_REF(inp);
445 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
449 SCTP_TCB_UNLOCK(stcb);
451 * We use the write lock here, only since in the error leg we need
452 * it. If we used RLOCK, then we would have to
453 * wlock/decr/unlock/rlock. Which in theory could create a hole.
454 * Better to use higher wlock.
458 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
460 SCTP_INP_WUNLOCK(inp);
463 cru2x(inp->sctp_socket->so_cred, &xuc);
464 SCTP_INP_WUNLOCK(inp);
465 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
470 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
471 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
476 sctp_abort(struct socket *so)
478 struct sctp_inpcb *inp;
481 inp = (struct sctp_inpcb *)so->so_pcb;
486 flags = inp->sctp_flags;
487 #ifdef SCTP_LOG_CLOSING
488 sctp_log_closing(inp, NULL, 17);
490 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
491 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
492 #ifdef SCTP_LOG_CLOSING
493 sctp_log_closing(inp, NULL, 16);
495 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
496 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
498 SCTP_SB_CLEAR(so->so_snd);
500 * same for the rcv ones, they are only here for the
503 SCTP_SB_CLEAR(so->so_rcv);
505 /* Now null out the reference, we are completely detached. */
509 flags = inp->sctp_flags;
510 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
511 goto sctp_must_try_again;
518 sctp_attach(struct socket *so, int proto, struct thread *p)
520 struct sctp_inpcb *inp;
521 struct inpcb *ip_inp;
523 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
530 inp = (struct sctp_inpcb *)so->so_pcb;
532 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
535 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
536 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
541 error = sctp_inpcb_alloc(so, vrf_id);
545 inp = (struct sctp_inpcb *)so->so_pcb;
547 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
548 ip_inp = &inp->ip_inp.inp;
549 ip_inp->inp_vflag |= INP_IPV4;
550 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
552 error = ipsec_init_policy(so, &ip_inp->inp_sp);
553 #ifdef SCTP_LOG_CLOSING
554 sctp_log_closing(inp, NULL, 17);
558 flags = inp->sctp_flags;
559 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
560 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
561 #ifdef SCTP_LOG_CLOSING
562 sctp_log_closing(inp, NULL, 15);
564 SCTP_INP_WUNLOCK(inp);
565 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
566 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
568 flags = inp->sctp_flags;
569 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
572 SCTP_INP_WUNLOCK(inp);
578 SCTP_INP_WUNLOCK(inp);
583 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
585 struct sctp_inpcb *inp = NULL;
589 if (addr && addr->sa_family != AF_INET) {
590 /* must be a v4 address! */
591 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
595 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
596 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
599 inp = (struct sctp_inpcb *)so->so_pcb;
601 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
604 error = sctp_inpcb_bind(so, addr, NULL, p);
610 sctp_close(struct socket *so)
612 struct sctp_inpcb *inp;
615 inp = (struct sctp_inpcb *)so->so_pcb;
620 * Inform all the lower layer assoc that we are done.
623 flags = inp->sctp_flags;
624 #ifdef SCTP_LOG_CLOSING
625 sctp_log_closing(inp, NULL, 17);
627 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
628 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
629 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
630 (so->so_rcv.sb_cc > 0)) {
631 #ifdef SCTP_LOG_CLOSING
632 sctp_log_closing(inp, NULL, 13);
634 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
635 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
637 #ifdef SCTP_LOG_CLOSING
638 sctp_log_closing(inp, NULL, 14);
640 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
641 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
644 * The socket is now detached, no matter what the state of
645 * the SCTP association.
648 SCTP_SB_CLEAR(so->so_snd);
650 * same for the rcv ones, they are only here for the
653 SCTP_SB_CLEAR(so->so_rcv);
655 /* Now null out the reference, we are completely detached. */
659 flags = inp->sctp_flags;
660 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
661 goto sctp_must_try_again;
669 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
670 struct mbuf *control, struct thread *p);
674 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
675 struct mbuf *control, struct thread *p)
677 struct sctp_inpcb *inp;
680 inp = (struct sctp_inpcb *)so->so_pcb;
683 sctp_m_freem(control);
686 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
690 /* Got to have an to address if we are NOT a connected socket */
691 if ((addr == NULL) &&
692 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
693 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
696 } else if (addr == NULL) {
697 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
698 error = EDESTADDRREQ;
701 sctp_m_freem(control);
707 if (addr->sa_family != AF_INET) {
708 /* must be a v4 address! */
709 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
712 sctp_m_freem(control);
715 error = EDESTADDRREQ;
720 /* now what about control */
723 SCTP_PRINTF("huh? control set?\n");
724 sctp_m_freem(inp->control);
727 inp->control = control;
731 SCTP_BUF_NEXT(inp->pkt_last) = m;
734 inp->pkt_last = inp->pkt = m;
737 /* FreeBSD uses a flag passed */
738 ((flags & PRUS_MORETOCOME) == 0)
741 * note with the current version this code will only be used
742 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
743 * re-defining sosend to use the sctp_sosend. One can
744 * optionally switch back to this code (by changing back the
745 * definitions) but this is not advisable. This code is used
746 * by FreeBSD when sending a file with sendfile() though.
750 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
760 sctp_disconnect(struct socket *so)
762 struct sctp_inpcb *inp;
764 inp = (struct sctp_inpcb *)so->so_pcb;
766 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
770 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
771 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
772 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
774 SCTP_INP_RUNLOCK(inp);
777 struct sctp_association *asoc;
778 struct sctp_tcb *stcb;
780 stcb = LIST_FIRST(&inp->sctp_asoc_list);
782 SCTP_INP_RUNLOCK(inp);
783 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
788 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
789 /* We are about to be freed, out of here */
790 SCTP_TCB_UNLOCK(stcb);
791 SCTP_INP_RUNLOCK(inp);
794 if (((so->so_options & SO_LINGER) &&
795 (so->so_linger == 0)) ||
796 (so->so_rcv.sb_cc > 0)) {
797 if (SCTP_GET_STATE(asoc) !=
798 SCTP_STATE_COOKIE_WAIT) {
799 /* Left with Data unread */
802 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
808 struct sctp_paramhdr *ph;
810 ph = mtod(err, struct sctp_paramhdr *);
811 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
812 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
813 ph->param_length = htons(SCTP_BUF_LEN(err));
815 #if defined(SCTP_PANIC_ON_ABORT)
816 panic("disconnect does an abort");
818 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
819 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
821 SCTP_INP_RUNLOCK(inp);
822 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
823 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
824 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
826 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
827 /* No unlock tcb assoc is gone */
830 if (TAILQ_EMPTY(&asoc->send_queue) &&
831 TAILQ_EMPTY(&asoc->sent_queue) &&
832 (asoc->stream_queue_cnt == 0)) {
833 /* there is nothing queued to send, so done */
834 if (asoc->locked_on_sending) {
837 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
838 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
839 /* only send SHUTDOWN 1st time thru */
840 sctp_stop_timers_for_shutdown(stcb);
841 sctp_send_shutdown(stcb,
842 stcb->asoc.primary_destination);
843 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
844 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
845 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
846 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
848 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
849 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
850 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
852 asoc->primary_destination);
853 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
855 asoc->primary_destination);
859 * we still got (or just got) data to send,
860 * so set SHUTDOWN_PENDING
863 * XXX sockets draft says that SCTP_EOF
864 * should be sent with no data. currently,
865 * we will allow user data to be sent first
866 * and move to SHUTDOWN-PENDING
868 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
869 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
870 asoc->primary_destination);
871 if (asoc->locked_on_sending) {
872 /* Locked to send out the data */
873 struct sctp_stream_queue_pending *sp;
875 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
877 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
878 asoc->locked_on_sending->stream_no);
880 if ((sp->length == 0) && (sp->msg_is_complete == 0))
881 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
884 if (TAILQ_EMPTY(&asoc->send_queue) &&
885 TAILQ_EMPTY(&asoc->sent_queue) &&
886 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
890 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
891 0, M_DONTWAIT, 1, MT_DATA);
897 struct sctp_paramhdr *ph;
900 SCTP_BUF_LEN(op_err) =
901 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
903 struct sctp_paramhdr *);
904 ph->param_type = htons(
905 SCTP_CAUSE_USER_INITIATED_ABT);
906 ph->param_length = htons(SCTP_BUF_LEN(op_err));
907 ippp = (uint32_t *) (ph + 1);
908 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
910 #if defined(SCTP_PANIC_ON_ABORT)
911 panic("disconnect does an abort");
914 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
915 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
916 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
917 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
918 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
919 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
921 SCTP_INP_RUNLOCK(inp);
922 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
925 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
928 soisdisconnecting(so);
929 SCTP_TCB_UNLOCK(stcb);
930 SCTP_INP_RUNLOCK(inp);
935 /* UDP model does not support this */
936 SCTP_INP_RUNLOCK(inp);
937 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
943 sctp_flush(struct socket *so, int how)
946 * We will just clear out the values and let subsequent close clear
947 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
948 * they will not be able to read the data, the socket will block
949 * that from happening.
951 struct sctp_inpcb *inp;
953 inp = (struct sctp_inpcb *)so->so_pcb;
955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
959 /* For the 1 to many model this does nothing */
960 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
961 SCTP_INP_RUNLOCK(inp);
964 SCTP_INP_RUNLOCK(inp);
965 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
967 * First make sure the sb will be happy, we don't use these
968 * except maybe the count
971 SCTP_INP_READ_LOCK(inp);
972 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
973 SCTP_INP_READ_UNLOCK(inp);
974 SCTP_INP_WUNLOCK(inp);
975 so->so_rcv.sb_cc = 0;
976 so->so_rcv.sb_mbcnt = 0;
977 so->so_rcv.sb_mb = NULL;
979 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
981 * First make sure the sb will be happy, we don't use these
982 * except maybe the count
984 so->so_snd.sb_cc = 0;
985 so->so_snd.sb_mbcnt = 0;
986 so->so_snd.sb_mb = NULL;
993 sctp_shutdown(struct socket *so)
995 struct sctp_inpcb *inp;
997 inp = (struct sctp_inpcb *)so->so_pcb;
999 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1002 SCTP_INP_RLOCK(inp);
1003 /* For UDP model this is a invalid call */
1004 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
1005 /* Restore the flags that the soshutdown took away. */
1006 SOCKBUF_LOCK(&so->so_rcv);
1007 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1008 SOCKBUF_UNLOCK(&so->so_rcv);
1009 /* This proc will wakeup for read and do nothing (I hope) */
1010 SCTP_INP_RUNLOCK(inp);
1011 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1012 return (EOPNOTSUPP);
1015 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1016 * or SHUT_RDWR. This means we put the shutdown flag against it.
1019 struct sctp_tcb *stcb;
1020 struct sctp_association *asoc;
1023 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1024 SCTP_INP_RUNLOCK(inp);
1029 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1032 * Ok we hit the case that the shutdown call was
1033 * made after an abort or something. Nothing to do
1036 SCTP_INP_RUNLOCK(inp);
1039 SCTP_TCB_LOCK(stcb);
1041 if (TAILQ_EMPTY(&asoc->send_queue) &&
1042 TAILQ_EMPTY(&asoc->sent_queue) &&
1043 (asoc->stream_queue_cnt == 0)) {
1044 if (asoc->locked_on_sending) {
1047 /* there is nothing queued to send, so I'm done... */
1048 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1049 /* only send SHUTDOWN the first time through */
1050 sctp_stop_timers_for_shutdown(stcb);
1051 sctp_send_shutdown(stcb,
1052 stcb->asoc.primary_destination);
1053 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1054 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1055 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1056 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1058 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1059 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1060 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1061 stcb->sctp_ep, stcb,
1062 asoc->primary_destination);
1063 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1064 stcb->sctp_ep, stcb,
1065 asoc->primary_destination);
1069 * we still got (or just got) data to send, so set
1072 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1073 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1074 asoc->primary_destination);
1076 if (asoc->locked_on_sending) {
1077 /* Locked to send out the data */
1078 struct sctp_stream_queue_pending *sp;
1080 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1082 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1083 asoc->locked_on_sending->stream_no);
1085 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1086 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1090 if (TAILQ_EMPTY(&asoc->send_queue) &&
1091 TAILQ_EMPTY(&asoc->sent_queue) &&
1092 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1093 struct mbuf *op_err;
1096 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1097 0, M_DONTWAIT, 1, MT_DATA);
1099 /* Fill in the user initiated abort */
1100 struct sctp_paramhdr *ph;
1103 SCTP_BUF_LEN(op_err) =
1104 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1106 struct sctp_paramhdr *);
1107 ph->param_type = htons(
1108 SCTP_CAUSE_USER_INITIATED_ABT);
1109 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1110 ippp = (uint32_t *) (ph + 1);
1111 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1113 #if defined(SCTP_PANIC_ON_ABORT)
1114 panic("shutdown does an abort");
1116 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1117 sctp_abort_an_association(stcb->sctp_ep, stcb,
1118 SCTP_RESPONSE_TO_USER_REQ,
1119 op_err, SCTP_SO_LOCKED);
1122 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1125 SCTP_TCB_UNLOCK(stcb);
1128 SCTP_INP_RUNLOCK(inp);
1133 * copies a "user" presentable address and removes embedded scope, etc.
1134 * returns 0 on success, 1 on error
1137 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1140 struct sockaddr_in6 lsa6;
1142 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1145 memcpy(ss, sa, sa->sa_len);
1152 * NOTE: assumes addr lock is held
1155 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1156 struct sctp_tcb *stcb,
1158 struct sockaddr_storage *sas,
1161 struct sctp_ifn *sctp_ifn;
1162 struct sctp_ifa *sctp_ifa;
1163 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1165 int ipv4_addr_legal, ipv6_addr_legal;
1166 struct sctp_vrf *vrf;
1173 /* Turn on all the appropriate scope */
1174 loopback_scope = stcb->asoc.loopback_scope;
1175 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1176 local_scope = stcb->asoc.local_scope;
1177 site_scope = stcb->asoc.site_scope;
1179 /* Turn on ALL scope, since we look at the EP */
1180 loopback_scope = ipv4_local_scope = local_scope =
1183 ipv4_addr_legal = ipv6_addr_legal = 0;
1184 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1185 ipv6_addr_legal = 1;
1186 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1187 ipv4_addr_legal = 1;
1190 ipv4_addr_legal = 1;
1192 vrf = sctp_find_vrf(vrf_id);
1196 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1197 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1198 if ((loopback_scope == 0) &&
1199 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1200 /* Skip loopback if loopback_scope not set */
1203 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1206 * For the BOUND-ALL case, the list
1207 * associated with a TCB is Always
1208 * considered a reverse list.. i.e.
1209 * it lists addresses that are NOT
1210 * part of the association. If this
1211 * is one of those we must skip it.
1213 if (sctp_is_addr_restricted(stcb,
1218 switch (sctp_ifa->address.sa.sa_family) {
1221 if (ipv4_addr_legal) {
1222 struct sockaddr_in *sin;
1224 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1225 if (sin->sin_addr.s_addr == 0) {
1233 if ((ipv4_local_scope == 0) &&
1234 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1238 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1239 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1240 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1241 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1242 actual += sizeof(struct sockaddr_in6);
1245 memcpy(sas, sin, sizeof(*sin));
1246 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1247 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1248 actual += sizeof(*sin);
1252 if (actual >= limit) {
1262 if (ipv6_addr_legal) {
1263 struct sockaddr_in6 *sin6;
1265 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1266 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1274 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1275 if (local_scope == 0)
1277 if (sin6->sin6_scope_id == 0) {
1278 if (sa6_recoverscope(sin6) != 0)
1296 if ((site_scope == 0) &&
1297 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1300 memcpy(sas, sin6, sizeof(*sin6));
1301 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1302 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1303 actual += sizeof(*sin6);
1304 if (actual >= limit) {
1319 struct sctp_laddr *laddr;
1321 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1323 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1327 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1330 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1331 sas = (struct sockaddr_storage *)((caddr_t)sas +
1332 laddr->ifa->address.sa.sa_len);
1333 actual += laddr->ifa->address.sa.sa_len;
1334 if (actual >= limit) {
1343 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1344 struct sctp_tcb *stcb,
1346 struct sockaddr_storage *sas)
1350 SCTP_IPI_ADDR_RLOCK();
1351 /* fill up addresses for the endpoint's default vrf */
1352 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1354 SCTP_IPI_ADDR_RUNLOCK();
1359 * NOTE: assumes addr lock is held
1362 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1365 struct sctp_vrf *vrf = NULL;
1368 * In both sub-set bound an bound_all cases we return the MAXIMUM
1369 * number of addresses that you COULD get. In reality the sub-set
1370 * bound may have an exclusion list for a given TCB OR in the
1371 * bound-all case a TCB may NOT include the loopback or other
1372 * addresses as well.
1374 vrf = sctp_find_vrf(vrf_id);
1378 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1379 struct sctp_ifn *sctp_ifn;
1380 struct sctp_ifa *sctp_ifa;
1382 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1383 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1384 /* Count them if they are the right type */
1385 switch (sctp_ifa->address.sa.sa_family) {
1388 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1389 cnt += sizeof(struct sockaddr_in6);
1391 cnt += sizeof(struct sockaddr_in);
1396 cnt += sizeof(struct sockaddr_in6);
1405 struct sctp_laddr *laddr;
1407 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1408 switch (laddr->ifa->address.sa.sa_family) {
1411 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1412 cnt += sizeof(struct sockaddr_in6);
1414 cnt += sizeof(struct sockaddr_in);
1419 cnt += sizeof(struct sockaddr_in6);
1431 sctp_count_max_addresses(struct sctp_inpcb *inp)
1435 SCTP_IPI_ADDR_RLOCK();
1436 /* count addresses for the endpoint's default VRF */
1437 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1438 SCTP_IPI_ADDR_RUNLOCK();
1443 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1444 size_t optsize, void *p, int delay)
1447 int creat_lock_on = 0;
1448 struct sctp_tcb *stcb = NULL;
1449 struct sockaddr *sa;
1450 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1453 int bad_addresses = 0;
1456 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1458 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1459 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1460 /* We are already connected AND the TCP model */
1461 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1462 return (EADDRINUSE);
1464 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1465 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1466 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1469 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1470 SCTP_INP_RLOCK(inp);
1471 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1472 SCTP_INP_RUNLOCK(inp);
1475 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1478 SCTP_INP_INCR_REF(inp);
1479 SCTP_ASOC_CREATE_LOCK(inp);
1481 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1482 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1483 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1487 totaddrp = (int *)optval;
1488 totaddr = *totaddrp;
1489 sa = (struct sockaddr *)(totaddrp + 1);
1490 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1491 if ((stcb != NULL) || bad_addresses) {
1492 /* Already have or am bring up an association */
1493 SCTP_ASOC_CREATE_UNLOCK(inp);
1496 SCTP_TCB_UNLOCK(stcb);
1497 if (bad_addresses == 0) {
1498 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1504 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1509 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1511 struct in6pcb *inp6;
1513 inp6 = (struct in6pcb *)inp;
1514 if (SCTP_IPV6_V6ONLY(inp6)) {
1516 * if IPV6_V6ONLY flag, ignore connections destined
1517 * to a v4 addr or v4-mapped addr
1519 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1525 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1526 SCTP_PCB_FLAGS_UNBOUND) {
1527 /* Bind a ephemeral port */
1528 error = sctp_inpcb_bind(so, NULL, NULL, p);
1533 /* FIX ME: do we want to pass in a vrf on the connect call? */
1534 vrf_id = inp->def_vrf_id;
1537 /* We are GOOD to go */
1538 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1542 /* Gak! no memory */
1545 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1546 /* move to second address */
1547 switch (sa->sa_family) {
1550 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1555 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1563 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1564 /* Fill in the return id */
1566 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1569 a_id = (sctp_assoc_t *) optval;
1570 *a_id = sctp_get_associd(stcb);
1572 /* initialize authentication parameters for the assoc */
1573 sctp_initialize_auth_params(inp, stcb);
1576 /* doing delayed connection */
1577 stcb->asoc.delayed_connection = 1;
1578 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1580 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1581 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1583 SCTP_TCB_UNLOCK(stcb);
1584 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1585 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1586 /* Set the connected flag so we can queue data */
1590 if (creat_lock_on) {
1591 SCTP_ASOC_CREATE_UNLOCK(inp);
1593 SCTP_INP_DECR_REF(inp);
1597 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1598 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1599 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1600 SCTP_INP_RLOCK(inp); \
1601 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1603 SCTP_TCB_LOCK(stcb); \
1605 SCTP_INP_RUNLOCK(inp); \
1606 } else if (assoc_id != 0) { \
1607 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1608 if (stcb == NULL) { \
1609 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1619 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1620 if (size < sizeof(type)) { \
1621 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1625 destp = (type *)srcp; \
1630 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1633 struct sctp_inpcb *inp = NULL;
1635 struct sctp_tcb *stcb = NULL;
1637 if (optval == NULL) {
1638 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1641 inp = (struct sctp_inpcb *)so->so_pcb;
1643 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1650 case SCTP_AUTOCLOSE:
1651 case SCTP_EXPLICIT_EOR:
1652 case SCTP_AUTO_ASCONF:
1653 case SCTP_DISABLE_FRAGMENTS:
1654 case SCTP_I_WANT_MAPPED_V4_ADDR:
1655 case SCTP_USE_EXT_RCVINFO:
1656 SCTP_INP_RLOCK(inp);
1658 case SCTP_DISABLE_FRAGMENTS:
1659 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1661 case SCTP_I_WANT_MAPPED_V4_ADDR:
1662 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1664 case SCTP_AUTO_ASCONF:
1665 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1666 /* only valid for bound all sockets */
1667 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1669 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1674 case SCTP_EXPLICIT_EOR:
1675 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1678 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1680 case SCTP_USE_EXT_RCVINFO:
1681 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1683 case SCTP_AUTOCLOSE:
1684 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1685 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1691 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1692 error = ENOPROTOOPT;
1693 } /* end switch (sopt->sopt_name) */
1694 if (optname != SCTP_AUTOCLOSE) {
1695 /* make it an "on/off" value */
1698 if (*optsize < sizeof(val)) {
1699 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1703 SCTP_INP_RUNLOCK(inp);
1705 /* return the option value */
1706 *(int *)optval = val;
1707 *optsize = sizeof(val);
1710 case SCTP_GET_PACKET_LOG:
1712 #ifdef SCTP_PACKET_LOGGING
1716 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1717 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1720 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1725 case SCTP_REUSE_PORT:
1729 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1730 /* Can't do this for a 1-m socket */
1734 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1735 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1736 *optsize = sizeof(uint32_t);
1739 case SCTP_PARTIAL_DELIVERY_POINT:
1743 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1744 *value = inp->partial_delivery_point;
1745 *optsize = sizeof(uint32_t);
1748 case SCTP_FRAGMENT_INTERLEAVE:
1752 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1753 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1754 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1755 *value = SCTP_FRAG_LEVEL_2;
1757 *value = SCTP_FRAG_LEVEL_1;
1760 *value = SCTP_FRAG_LEVEL_0;
1762 *optsize = sizeof(uint32_t);
1765 case SCTP_CMT_ON_OFF:
1767 struct sctp_assoc_value *av;
1769 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1770 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1772 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1773 SCTP_TCB_UNLOCK(stcb);
1775 SCTP_INP_RLOCK(inp);
1776 av->assoc_value = inp->sctp_cmt_on_off;
1777 SCTP_INP_RUNLOCK(inp);
1779 *optsize = sizeof(*av);
1782 /* JRS - Get socket option for pluggable congestion control */
1783 case SCTP_PLUGGABLE_CC:
1785 struct sctp_assoc_value *av;
1787 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1788 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1790 av->assoc_value = stcb->asoc.congestion_control_module;
1791 SCTP_TCB_UNLOCK(stcb);
1793 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1795 *optsize = sizeof(*av);
1798 case SCTP_CC_OPTION:
1800 struct sctp_cc_option *cc_opt;
1802 SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1803 SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1807 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1810 error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0,
1812 *optsize = sizeof(*cc_opt);
1814 SCTP_TCB_UNLOCK(stcb);
1818 /* RS - Get socket option for pluggable stream scheduling */
1819 case SCTP_PLUGGABLE_SS:
1821 struct sctp_assoc_value *av;
1823 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1824 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1826 av->assoc_value = stcb->asoc.stream_scheduling_module;
1827 SCTP_TCB_UNLOCK(stcb);
1829 av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1831 *optsize = sizeof(*av);
1836 struct sctp_stream_value *av;
1838 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1839 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1841 if (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1842 &av->stream_value) < 0) {
1843 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1846 *optsize = sizeof(*av);
1848 SCTP_TCB_UNLOCK(stcb);
1851 * Can't get stream value without
1854 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1859 case SCTP_GET_ADDR_LEN:
1861 struct sctp_assoc_value *av;
1863 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1866 if (av->assoc_value == AF_INET) {
1867 av->assoc_value = sizeof(struct sockaddr_in);
1872 if (av->assoc_value == AF_INET6) {
1873 av->assoc_value = sizeof(struct sockaddr_in6);
1878 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1880 *optsize = sizeof(*av);
1883 case SCTP_GET_ASSOC_NUMBER:
1885 uint32_t *value, cnt;
1887 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1889 SCTP_INP_RLOCK(inp);
1890 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1893 SCTP_INP_RUNLOCK(inp);
1895 *optsize = sizeof(uint32_t);
1899 case SCTP_GET_ASSOC_ID_LIST:
1901 struct sctp_assoc_ids *ids;
1902 unsigned int at, limit;
1904 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1906 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1907 SCTP_INP_RLOCK(inp);
1908 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1910 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1913 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1917 SCTP_INP_RUNLOCK(inp);
1918 ids->gaids_number_of_ids = at;
1919 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1924 struct sctp_assoc_value *av;
1926 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1927 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1930 av->assoc_value = stcb->asoc.context;
1931 SCTP_TCB_UNLOCK(stcb);
1933 SCTP_INP_RLOCK(inp);
1934 av->assoc_value = inp->sctp_context;
1935 SCTP_INP_RUNLOCK(inp);
1937 *optsize = sizeof(*av);
1942 uint32_t *default_vrfid;
1944 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1945 *default_vrfid = inp->def_vrf_id;
1948 case SCTP_GET_ASOC_VRF:
1950 struct sctp_assoc_value *id;
1952 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1953 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1956 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1959 id->assoc_value = stcb->asoc.vrf_id;
1962 case SCTP_GET_VRF_IDS:
1964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1968 case SCTP_GET_NONCE_VALUES:
1970 struct sctp_get_nonce_values *gnv;
1972 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1973 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1976 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1977 gnv->gn_local_tag = stcb->asoc.my_vtag;
1978 SCTP_TCB_UNLOCK(stcb);
1980 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1983 *optsize = sizeof(*gnv);
1986 case SCTP_DELAYED_SACK:
1988 struct sctp_sack_info *sack;
1990 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1991 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1993 sack->sack_delay = stcb->asoc.delayed_ack;
1994 sack->sack_freq = stcb->asoc.sack_freq;
1995 SCTP_TCB_UNLOCK(stcb);
1997 SCTP_INP_RLOCK(inp);
1998 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1999 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2000 SCTP_INP_RUNLOCK(inp);
2002 *optsize = sizeof(*sack);
2006 case SCTP_GET_SNDBUF_USE:
2008 struct sctp_sockstat *ss;
2010 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2011 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2014 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2015 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2016 stcb->asoc.size_on_all_streams);
2017 SCTP_TCB_UNLOCK(stcb);
2019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2022 *optsize = sizeof(struct sctp_sockstat);
2025 case SCTP_MAX_BURST:
2029 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
2031 SCTP_INP_RLOCK(inp);
2032 if (inp->sctp_ep.max_burst < 256) {
2033 *value = inp->sctp_ep.max_burst;
2037 SCTP_INP_RUNLOCK(inp);
2038 *optsize = sizeof(uint8_t);
2043 struct sctp_assoc_value *av;
2046 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2047 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2050 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2051 SCTP_TCB_UNLOCK(stcb);
2053 SCTP_INP_RLOCK(inp);
2054 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2055 ovh = SCTP_MED_OVERHEAD;
2057 ovh = SCTP_MED_V4_OVERHEAD;
2059 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2060 av->assoc_value = 0;
2062 av->assoc_value = inp->sctp_frag_point - ovh;
2063 SCTP_INP_RUNLOCK(inp);
2065 *optsize = sizeof(struct sctp_assoc_value);
2068 case SCTP_GET_STAT_LOG:
2069 error = sctp_fill_stat_log(optval, optsize);
2073 struct sctp_event_subscribe *events;
2075 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2076 memset(events, 0, sizeof(*events));
2077 SCTP_INP_RLOCK(inp);
2078 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2079 events->sctp_data_io_event = 1;
2081 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2082 events->sctp_association_event = 1;
2084 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2085 events->sctp_address_event = 1;
2087 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2088 events->sctp_send_failure_event = 1;
2090 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2091 events->sctp_peer_error_event = 1;
2093 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2094 events->sctp_shutdown_event = 1;
2096 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2097 events->sctp_partial_delivery_event = 1;
2099 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2100 events->sctp_adaptation_layer_event = 1;
2102 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2103 events->sctp_authentication_event = 1;
2105 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2106 events->sctp_sender_dry_event = 1;
2108 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2109 events->sctp_stream_reset_event = 1;
2110 SCTP_INP_RUNLOCK(inp);
2111 *optsize = sizeof(struct sctp_event_subscribe);
2115 case SCTP_ADAPTATION_LAYER:
2119 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2121 SCTP_INP_RLOCK(inp);
2122 *value = inp->sctp_ep.adaptation_layer_indicator;
2123 SCTP_INP_RUNLOCK(inp);
2124 *optsize = sizeof(uint32_t);
2127 case SCTP_SET_INITIAL_DBG_SEQ:
2131 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2132 SCTP_INP_RLOCK(inp);
2133 *value = inp->sctp_ep.initial_sequence_debug;
2134 SCTP_INP_RUNLOCK(inp);
2135 *optsize = sizeof(uint32_t);
2138 case SCTP_GET_LOCAL_ADDR_SIZE:
2142 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2143 SCTP_INP_RLOCK(inp);
2144 *value = sctp_count_max_addresses(inp);
2145 SCTP_INP_RUNLOCK(inp);
2146 *optsize = sizeof(uint32_t);
2149 case SCTP_GET_REMOTE_ADDR_SIZE:
2153 struct sctp_nets *net;
2155 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2156 /* FIXME MT: change to sctp_assoc_value? */
2157 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2161 /* Count the sizes */
2162 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2163 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2164 size += sizeof(struct sockaddr_in6);
2166 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2169 size += sizeof(struct sockaddr_in);
2174 size += sizeof(struct sockaddr_in6);
2182 SCTP_TCB_UNLOCK(stcb);
2183 *value = (uint32_t) size;
2185 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2188 *optsize = sizeof(uint32_t);
2191 case SCTP_GET_PEER_ADDRESSES:
2193 * Get the address information, an array is passed in to
2194 * fill up we pack it.
2198 struct sockaddr_storage *sas;
2199 struct sctp_nets *net;
2200 struct sctp_getaddresses *saddr;
2202 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2203 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2206 left = (*optsize) - sizeof(struct sctp_getaddresses);
2207 *optsize = sizeof(struct sctp_getaddresses);
2208 sas = (struct sockaddr_storage *)&saddr->addr[0];
2210 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2211 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2212 cpsz = sizeof(struct sockaddr_in6);
2214 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2217 cpsz = sizeof(struct sockaddr_in);
2222 cpsz = sizeof(struct sockaddr_in6);
2234 /* not enough room. */
2237 #if defined(INET) && defined(INET6)
2238 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2239 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2240 /* Must map the address */
2241 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2242 (struct sockaddr_in6 *)sas);
2245 memcpy(sas, &net->ro._l_addr, cpsz);
2246 #if defined(INET) && defined(INET6)
2249 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2251 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2255 SCTP_TCB_UNLOCK(stcb);
2257 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2262 case SCTP_GET_LOCAL_ADDRESSES:
2264 size_t limit, actual;
2265 struct sockaddr_storage *sas;
2266 struct sctp_getaddresses *saddr;
2268 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2269 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2271 sas = (struct sockaddr_storage *)&saddr->addr[0];
2272 limit = *optsize - sizeof(sctp_assoc_t);
2273 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2275 SCTP_TCB_UNLOCK(stcb);
2277 *optsize = sizeof(struct sockaddr_storage) + actual;
2280 case SCTP_PEER_ADDR_PARAMS:
2282 struct sctp_paddrparams *paddrp;
2283 struct sctp_nets *net;
2285 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2286 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2290 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2293 * We increment here since
2294 * sctp_findassociation_ep_addr() wil do a
2295 * decrement if it finds the stcb as long as
2296 * the locked tcb (last argument) is NOT a
2299 SCTP_INP_INCR_REF(inp);
2300 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2302 SCTP_INP_DECR_REF(inp);
2305 if (stcb && (net == NULL)) {
2306 struct sockaddr *sa;
2308 sa = (struct sockaddr *)&paddrp->spp_address;
2310 if (sa->sa_family == AF_INET) {
2311 struct sockaddr_in *sin;
2313 sin = (struct sockaddr_in *)sa;
2314 if (sin->sin_addr.s_addr) {
2316 SCTP_TCB_UNLOCK(stcb);
2317 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2323 if (sa->sa_family == AF_INET6) {
2324 struct sockaddr_in6 *sin6;
2326 sin6 = (struct sockaddr_in6 *)sa;
2327 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2329 SCTP_TCB_UNLOCK(stcb);
2330 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2336 error = EAFNOSUPPORT;
2337 SCTP_TCB_UNLOCK(stcb);
2338 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2343 /* Applys to the specific association */
2344 paddrp->spp_flags = 0;
2348 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2349 ovh = SCTP_MED_OVERHEAD;
2351 ovh = SCTP_MED_V4_OVERHEAD;
2355 paddrp->spp_pathmaxrxt = net->failure_threshold;
2356 paddrp->spp_pathmtu = net->mtu - ovh;
2357 /* get flags for HB */
2358 if (net->dest_state & SCTP_ADDR_NOHB)
2359 paddrp->spp_flags |= SPP_HB_DISABLE;
2361 paddrp->spp_flags |= SPP_HB_ENABLE;
2362 /* get flags for PMTU */
2363 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2364 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2366 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2369 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2370 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2371 paddrp->spp_flags |= SPP_IPV4_TOS;
2375 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2376 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2377 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2382 * No destination so return default
2387 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2388 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2390 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2391 paddrp->spp_flags |= SPP_IPV4_TOS;
2394 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2395 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2397 /* default settings should be these */
2398 if (stcb->asoc.hb_is_disabled == 0) {
2399 paddrp->spp_flags |= SPP_HB_ENABLE;
2401 paddrp->spp_flags |= SPP_HB_DISABLE;
2403 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2404 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2409 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2412 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2413 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2414 SCTP_TCB_UNLOCK(stcb);
2416 /* Use endpoint defaults */
2417 SCTP_INP_RLOCK(inp);
2418 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2419 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2420 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2421 /* get inp's default */
2423 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2424 paddrp->spp_flags |= SPP_IPV4_TOS;
2427 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2428 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2429 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2432 /* can't return this */
2433 paddrp->spp_pathmtu = 0;
2435 /* default behavior, no stcb */
2436 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2438 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2439 paddrp->spp_flags |= SPP_HB_ENABLE;
2441 paddrp->spp_flags |= SPP_HB_DISABLE;
2443 SCTP_INP_RUNLOCK(inp);
2445 *optsize = sizeof(struct sctp_paddrparams);
2448 case SCTP_GET_PEER_ADDR_INFO:
2450 struct sctp_paddrinfo *paddri;
2451 struct sctp_nets *net;
2453 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2454 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2458 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2461 * We increment here since
2462 * sctp_findassociation_ep_addr() wil do a
2463 * decrement if it finds the stcb as long as
2464 * the locked tcb (last argument) is NOT a
2467 SCTP_INP_INCR_REF(inp);
2468 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2470 SCTP_INP_DECR_REF(inp);
2474 if ((stcb) && (net)) {
2475 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2476 /* It's unconfirmed */
2477 paddri->spinfo_state = SCTP_UNCONFIRMED;
2478 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2480 paddri->spinfo_state = SCTP_ACTIVE;
2483 paddri->spinfo_state = SCTP_INACTIVE;
2485 paddri->spinfo_cwnd = net->cwnd;
2486 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2487 paddri->spinfo_rto = net->RTO;
2488 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2489 SCTP_TCB_UNLOCK(stcb);
2492 SCTP_TCB_UNLOCK(stcb);
2494 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2497 *optsize = sizeof(struct sctp_paddrinfo);
2500 case SCTP_PCB_STATUS:
2502 struct sctp_pcbinfo *spcb;
2504 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2505 sctp_fill_pcbinfo(spcb);
2506 *optsize = sizeof(struct sctp_pcbinfo);
2512 struct sctp_nets *net;
2513 struct sctp_status *sstat;
2515 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2516 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2519 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2524 * I think passing the state is fine since
2525 * sctp_constants.h will be available to the user
2528 sstat->sstat_state = stcb->asoc.state;
2529 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2530 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2531 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2533 * We can't include chunks that have been passed to
2534 * the socket layer. Only things in queue.
2536 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2537 stcb->asoc.cnt_on_all_streams);
2540 sstat->sstat_instrms = stcb->asoc.streamincnt;
2541 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2542 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2543 memcpy(&sstat->sstat_primary.spinfo_address,
2544 &stcb->asoc.primary_destination->ro._l_addr,
2545 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2546 net = stcb->asoc.primary_destination;
2547 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2549 * Again the user can get info from sctp_constants.h
2550 * for what the state of the network is.
2552 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2553 /* It's unconfirmed */
2554 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2555 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2557 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2560 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2562 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2563 sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2564 sstat->sstat_primary.spinfo_rto = net->RTO;
2565 sstat->sstat_primary.spinfo_mtu = net->mtu;
2566 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2567 SCTP_TCB_UNLOCK(stcb);
2568 *optsize = sizeof(*sstat);
2573 struct sctp_rtoinfo *srto;
2575 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2576 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2579 srto->srto_initial = stcb->asoc.initial_rto;
2580 srto->srto_max = stcb->asoc.maxrto;
2581 srto->srto_min = stcb->asoc.minrto;
2582 SCTP_TCB_UNLOCK(stcb);
2584 SCTP_INP_RLOCK(inp);
2585 srto->srto_initial = inp->sctp_ep.initial_rto;
2586 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2587 srto->srto_min = inp->sctp_ep.sctp_minrto;
2588 SCTP_INP_RUNLOCK(inp);
2590 *optsize = sizeof(*srto);
2595 struct sctp_timeouts *stimo;
2597 SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2598 SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2601 stimo->stimo_init = stcb->asoc.timoinit;
2602 stimo->stimo_data = stcb->asoc.timodata;
2603 stimo->stimo_sack = stcb->asoc.timosack;
2604 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2605 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2606 stimo->stimo_cookie = stcb->asoc.timocookie;
2607 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2608 SCTP_TCB_UNLOCK(stcb);
2610 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2613 *optsize = sizeof(*stimo);
2616 case SCTP_ASSOCINFO:
2618 struct sctp_assocparams *sasoc;
2621 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2622 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2625 oldval = sasoc->sasoc_cookie_life;
2626 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2627 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2628 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2629 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2630 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2631 SCTP_TCB_UNLOCK(stcb);
2633 SCTP_INP_RLOCK(inp);
2634 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2635 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2636 sasoc->sasoc_number_peer_destinations = 0;
2637 sasoc->sasoc_peer_rwnd = 0;
2638 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2639 SCTP_INP_RUNLOCK(inp);
2641 *optsize = sizeof(*sasoc);
2644 case SCTP_DEFAULT_SEND_PARAM:
2646 struct sctp_sndrcvinfo *s_info;
2648 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2649 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2652 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2653 SCTP_TCB_UNLOCK(stcb);
2655 SCTP_INP_RLOCK(inp);
2656 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2657 SCTP_INP_RUNLOCK(inp);
2659 *optsize = sizeof(*s_info);
2664 struct sctp_initmsg *sinit;
2666 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2667 SCTP_INP_RLOCK(inp);
2668 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2669 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2670 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2671 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2672 SCTP_INP_RUNLOCK(inp);
2673 *optsize = sizeof(*sinit);
2676 case SCTP_PRIMARY_ADDR:
2677 /* we allow a "get" operation on this */
2679 struct sctp_setprim *ssp;
2681 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2682 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2685 /* simply copy out the sockaddr_storage... */
2689 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2690 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2692 memcpy(&ssp->ssp_addr,
2693 &stcb->asoc.primary_destination->ro._l_addr,
2695 SCTP_TCB_UNLOCK(stcb);
2697 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2700 *optsize = sizeof(*ssp);
2704 case SCTP_HMAC_IDENT:
2706 struct sctp_hmacalgo *shmac;
2707 sctp_hmaclist_t *hmaclist;
2711 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2713 SCTP_INP_RLOCK(inp);
2714 hmaclist = inp->sctp_ep.local_hmacs;
2715 if (hmaclist == NULL) {
2716 /* no HMACs to return */
2717 *optsize = sizeof(*shmac);
2718 SCTP_INP_RUNLOCK(inp);
2721 /* is there room for all of the hmac ids? */
2722 size = sizeof(*shmac) + (hmaclist->num_algo *
2723 sizeof(shmac->shmac_idents[0]));
2724 if ((size_t)(*optsize) < size) {
2725 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2727 SCTP_INP_RUNLOCK(inp);
2730 /* copy in the list */
2731 shmac->shmac_number_of_idents = hmaclist->num_algo;
2732 for (i = 0; i < hmaclist->num_algo; i++) {
2733 shmac->shmac_idents[i] = hmaclist->hmac[i];
2735 SCTP_INP_RUNLOCK(inp);
2739 case SCTP_AUTH_ACTIVE_KEY:
2741 struct sctp_authkeyid *scact;
2743 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2744 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2747 /* get the active key on the assoc */
2748 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2749 SCTP_TCB_UNLOCK(stcb);
2751 /* get the endpoint active key */
2752 SCTP_INP_RLOCK(inp);
2753 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2754 SCTP_INP_RUNLOCK(inp);
2756 *optsize = sizeof(*scact);
2759 case SCTP_LOCAL_AUTH_CHUNKS:
2761 struct sctp_authchunks *sac;
2762 sctp_auth_chklist_t *chklist = NULL;
2765 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2766 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2769 /* get off the assoc */
2770 chklist = stcb->asoc.local_auth_chunks;
2771 /* is there enough space? */
2772 size = sctp_auth_get_chklist_size(chklist);
2773 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2775 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2777 /* copy in the chunks */
2778 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2780 SCTP_TCB_UNLOCK(stcb);
2782 /* get off the endpoint */
2783 SCTP_INP_RLOCK(inp);
2784 chklist = inp->sctp_ep.local_auth_chunks;
2785 /* is there enough space? */
2786 size = sctp_auth_get_chklist_size(chklist);
2787 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2789 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2791 /* copy in the chunks */
2792 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2794 SCTP_INP_RUNLOCK(inp);
2796 *optsize = sizeof(struct sctp_authchunks) + size;
2799 case SCTP_PEER_AUTH_CHUNKS:
2801 struct sctp_authchunks *sac;
2802 sctp_auth_chklist_t *chklist = NULL;
2805 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2806 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2809 /* get off the assoc */
2810 chklist = stcb->asoc.peer_auth_chunks;
2811 /* is there enough space? */
2812 size = sctp_auth_get_chklist_size(chklist);
2813 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2815 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2817 /* copy in the chunks */
2818 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2820 SCTP_TCB_UNLOCK(stcb);
2822 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2825 *optsize = sizeof(struct sctp_authchunks) + size;
2831 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2832 error = ENOPROTOOPT;
2835 } /* end switch (sopt->sopt_name) */
2840 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2845 struct sctp_tcb *stcb = NULL;
2846 struct sctp_inpcb *inp = NULL;
2849 if (optval == NULL) {
2850 SCTP_PRINTF("optval is NULL\n");
2851 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2854 inp = (struct sctp_inpcb *)so->so_pcb;
2856 SCTP_PRINTF("inp is NULL?\n");
2857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2860 vrf_id = inp->def_vrf_id;
2865 case SCTP_AUTOCLOSE:
2866 case SCTP_AUTO_ASCONF:
2867 case SCTP_EXPLICIT_EOR:
2868 case SCTP_DISABLE_FRAGMENTS:
2869 case SCTP_USE_EXT_RCVINFO:
2870 case SCTP_I_WANT_MAPPED_V4_ADDR:
2871 /* copy in the option value */
2872 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2877 case SCTP_DISABLE_FRAGMENTS:
2878 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2880 case SCTP_AUTO_ASCONF:
2882 * NOTE: we don't really support this flag
2884 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2885 /* only valid for bound all sockets */
2886 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2888 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2892 case SCTP_EXPLICIT_EOR:
2893 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2895 case SCTP_USE_EXT_RCVINFO:
2896 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2898 case SCTP_I_WANT_MAPPED_V4_ADDR:
2899 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2900 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2902 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2907 set_opt = SCTP_PCB_FLAGS_NODELAY;
2909 case SCTP_AUTOCLOSE:
2910 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2911 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2912 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2915 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2917 * The value is in ticks. Note this does not effect
2918 * old associations, only new ones.
2920 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2923 SCTP_INP_WLOCK(inp);
2925 sctp_feature_on(inp, set_opt);
2927 sctp_feature_off(inp, set_opt);
2929 SCTP_INP_WUNLOCK(inp);
2931 case SCTP_REUSE_PORT:
2933 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2934 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2935 /* Can't set it after we are bound */
2939 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2940 /* Can't do this for a 1-m socket */
2945 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2947 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2950 case SCTP_PARTIAL_DELIVERY_POINT:
2954 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2955 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2956 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2960 inp->partial_delivery_point = *value;
2963 case SCTP_FRAGMENT_INTERLEAVE:
2964 /* not yet until we re-write sctp_recvmsg() */
2968 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2969 if (*level == SCTP_FRAG_LEVEL_2) {
2970 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2971 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2972 } else if (*level == SCTP_FRAG_LEVEL_1) {
2973 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2974 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2975 } else if (*level == SCTP_FRAG_LEVEL_0) {
2976 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2977 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2980 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2985 case SCTP_CMT_ON_OFF:
2986 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2987 struct sctp_assoc_value *av;
2989 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2990 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2992 if (av->assoc_value > SCTP_CMT_MAX) {
2993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2996 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
2998 SCTP_TCB_UNLOCK(stcb);
3000 if (av->assoc_value > SCTP_CMT_MAX) {
3001 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3004 SCTP_INP_WLOCK(inp);
3005 inp->sctp_cmt_on_off = av->assoc_value;
3006 SCTP_INP_WUNLOCK(inp);
3010 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3011 error = ENOPROTOOPT;
3014 /* JRS - Set socket option for pluggable congestion control */
3015 case SCTP_PLUGGABLE_CC:
3017 struct sctp_assoc_value *av;
3018 struct sctp_nets *net;
3020 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3021 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3023 switch (av->assoc_value) {
3024 case SCTP_CC_RFC2581:
3028 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3029 stcb->asoc.congestion_control_module = av->assoc_value;
3030 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3031 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3032 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3037 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3041 SCTP_TCB_UNLOCK(stcb);
3043 switch (av->assoc_value) {
3044 case SCTP_CC_RFC2581:
3048 SCTP_INP_WLOCK(inp);
3049 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3050 SCTP_INP_WUNLOCK(inp);
3053 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3060 case SCTP_CC_OPTION:
3062 struct sctp_cc_option *cc_opt;
3064 SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
3065 SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
3069 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
3072 error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
3075 SCTP_TCB_UNLOCK(stcb);
3079 /* RS - Set socket option for pluggable stream scheduling */
3080 case SCTP_PLUGGABLE_SS:
3082 struct sctp_assoc_value *av;
3084 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3085 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3087 switch (av->assoc_value) {
3088 case SCTP_SS_DEFAULT:
3089 case SCTP_SS_ROUND_ROBIN:
3090 case SCTP_SS_ROUND_ROBIN_PACKET:
3091 case SCTP_SS_PRIORITY:
3092 case SCTP_SS_FAIR_BANDWITH:
3093 case SCTP_SS_FIRST_COME:
3094 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
3095 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
3096 stcb->asoc.stream_scheduling_module = av->assoc_value;
3097 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3104 SCTP_TCB_UNLOCK(stcb);
3106 switch (av->assoc_value) {
3107 case SCTP_SS_DEFAULT:
3108 case SCTP_SS_ROUND_ROBIN:
3109 case SCTP_SS_ROUND_ROBIN_PACKET:
3110 case SCTP_SS_PRIORITY:
3111 case SCTP_SS_FAIR_BANDWITH:
3112 case SCTP_SS_FIRST_COME:
3113 SCTP_INP_WLOCK(inp);
3114 inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
3115 SCTP_INP_WUNLOCK(inp);
3118 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3127 struct sctp_stream_value *av;
3129 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
3130 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3132 if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
3133 av->stream_value) < 0) {
3134 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3137 SCTP_TCB_UNLOCK(stcb);
3140 * Can't set stream value without
3143 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3148 case SCTP_CLR_STAT_LOG:
3149 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3154 struct sctp_assoc_value *av;
3156 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3157 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3160 stcb->asoc.context = av->assoc_value;
3161 SCTP_TCB_UNLOCK(stcb);
3163 SCTP_INP_WLOCK(inp);
3164 inp->sctp_context = av->assoc_value;
3165 SCTP_INP_WUNLOCK(inp);
3171 uint32_t *default_vrfid;
3173 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
3174 if (*default_vrfid > SCTP_MAX_VRF_ID) {
3175 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3179 inp->def_vrf_id = *default_vrfid;
3182 case SCTP_DEL_VRF_ID:
3184 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3188 case SCTP_ADD_VRF_ID:
3190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3194 case SCTP_DELAYED_SACK:
3196 struct sctp_sack_info *sack;
3198 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
3199 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
3200 if (sack->sack_delay) {
3201 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3202 sack->sack_delay = SCTP_MAX_SACK_DELAY;
3205 if (sack->sack_delay) {
3206 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3207 sack->sack_delay = TICKS_TO_MSEC(1);
3209 stcb->asoc.delayed_ack = sack->sack_delay;
3211 if (sack->sack_freq) {
3212 stcb->asoc.sack_freq = sack->sack_freq;
3214 SCTP_TCB_UNLOCK(stcb);
3216 SCTP_INP_WLOCK(inp);
3217 if (sack->sack_delay) {
3218 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3219 sack->sack_delay = TICKS_TO_MSEC(1);
3221 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3223 if (sack->sack_freq) {
3224 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3226 SCTP_INP_WUNLOCK(inp);
3230 case SCTP_AUTH_CHUNK:
3232 struct sctp_authchunk *sauth;
3234 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3236 SCTP_INP_WLOCK(inp);
3237 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3238 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3241 SCTP_INP_WUNLOCK(inp);
3246 struct sctp_authkey *sca;
3247 struct sctp_keyhead *shared_keys;
3248 sctp_sharedkey_t *shared_key;
3249 sctp_key_t *key = NULL;
3252 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3253 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3254 size = optsize - sizeof(*sca);
3257 /* set it on the assoc */
3258 shared_keys = &stcb->asoc.shared_keys;
3259 /* clear the cached keys for this key id */
3260 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3262 * create the new shared key and
3266 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3268 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3270 SCTP_TCB_UNLOCK(stcb);
3274 shared_key = sctp_alloc_sharedkey();
3275 if (shared_key == NULL) {
3277 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3279 SCTP_TCB_UNLOCK(stcb);
3282 shared_key->key = key;
3283 shared_key->keyid = sca->sca_keynumber;
3284 error = sctp_insert_sharedkey(shared_keys, shared_key);
3285 SCTP_TCB_UNLOCK(stcb);
3287 /* set it on the endpoint */
3288 SCTP_INP_WLOCK(inp);
3289 shared_keys = &inp->sctp_ep.shared_keys;
3291 * clear the cached keys on all assocs for
3294 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3296 * create the new shared key and
3300 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3302 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3304 SCTP_INP_WUNLOCK(inp);
3308 shared_key = sctp_alloc_sharedkey();
3309 if (shared_key == NULL) {
3311 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3313 SCTP_INP_WUNLOCK(inp);
3316 shared_key->key = key;
3317 shared_key->keyid = sca->sca_keynumber;
3318 error = sctp_insert_sharedkey(shared_keys, shared_key);
3319 SCTP_INP_WUNLOCK(inp);
3323 case SCTP_HMAC_IDENT:
3325 struct sctp_hmacalgo *shmac;
3326 sctp_hmaclist_t *hmaclist;
3332 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3333 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3334 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3338 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3339 if (hmaclist == NULL) {
3340 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3344 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3345 hmacid = shmac->shmac_idents[i];
3346 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3347 /* invalid HMACs were found */ ;
3348 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3350 sctp_free_hmaclist(hmaclist);
3351 goto sctp_set_hmac_done;
3355 for (i = 0; i < hmaclist->num_algo; i++) {
3356 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3357 /* already in list */
3362 sctp_free_hmaclist(hmaclist);
3363 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3367 /* set it on the endpoint */
3368 SCTP_INP_WLOCK(inp);
3369 if (inp->sctp_ep.local_hmacs)
3370 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3371 inp->sctp_ep.local_hmacs = hmaclist;
3372 SCTP_INP_WUNLOCK(inp);
3376 case SCTP_AUTH_ACTIVE_KEY:
3378 struct sctp_authkeyid *scact;
3380 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3382 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3384 /* set the active key on the right place */
3386 /* set the active key on the assoc */
3387 if (sctp_auth_setactivekey(stcb,
3388 scact->scact_keynumber)) {
3389 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3390 SCTP_FROM_SCTP_USRREQ,
3394 SCTP_TCB_UNLOCK(stcb);
3396 /* set the active key on the endpoint */
3397 SCTP_INP_WLOCK(inp);
3398 if (sctp_auth_setactivekey_ep(inp,
3399 scact->scact_keynumber)) {
3400 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3401 SCTP_FROM_SCTP_USRREQ,
3405 SCTP_INP_WUNLOCK(inp);
3409 case SCTP_AUTH_DELETE_KEY:
3411 struct sctp_authkeyid *scdel;
3413 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3415 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3417 /* delete the key from the right place */
3419 if (sctp_delete_sharedkey(stcb,
3420 scdel->scact_keynumber)) {
3421 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3422 SCTP_FROM_SCTP_USRREQ,
3426 SCTP_TCB_UNLOCK(stcb);
3428 SCTP_INP_WLOCK(inp);
3429 if (sctp_delete_sharedkey_ep(inp,
3430 scdel->scact_keynumber)) {
3431 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3432 SCTP_FROM_SCTP_USRREQ,
3436 SCTP_INP_WUNLOCK(inp);
3440 case SCTP_AUTH_DEACTIVATE_KEY:
3442 struct sctp_authkeyid *keyid;
3444 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3446 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3448 /* deactivate the key from the right place */
3450 if (sctp_deact_sharedkey(stcb,
3451 keyid->scact_keynumber)) {
3452 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3453 SCTP_FROM_SCTP_USRREQ,
3457 SCTP_TCB_UNLOCK(stcb);
3459 SCTP_INP_WLOCK(inp);
3460 if (sctp_deact_sharedkey_ep(inp,
3461 keyid->scact_keynumber)) {
3462 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3463 SCTP_FROM_SCTP_USRREQ,
3467 SCTP_INP_WUNLOCK(inp);
3472 case SCTP_RESET_STREAMS:
3474 struct sctp_stream_reset *strrst;
3475 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3477 uint16_t addstrmcnt = 0;
3480 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3481 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3484 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3488 if (stcb->asoc.peer_supports_strreset == 0) {
3490 * Peer does not support it, we return
3491 * protocol not supported since this is true
3492 * for this feature and this peer, not the
3493 * socket request in general.
3495 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3496 error = EPROTONOSUPPORT;
3497 SCTP_TCB_UNLOCK(stcb);
3500 if (stcb->asoc.stream_reset_outstanding) {
3501 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3503 SCTP_TCB_UNLOCK(stcb);
3506 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3508 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3510 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3513 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3515 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3519 /* We can't do that and add streams */
3523 if (stcb->asoc.stream_reset_outstanding) {
3528 /* We allocate here */
3529 addstrmcnt = strrst->strrst_num_streams;
3530 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3531 /* You can't have more than 64k */
3535 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3536 /* Need to allocate more */
3537 struct sctp_stream_out *oldstream;
3538 struct sctp_stream_queue_pending *sp,
3541 oldstream = stcb->asoc.strmout;
3543 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3544 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3546 if (stcb->asoc.strmout == NULL) {
3547 stcb->asoc.strmout = oldstream;
3552 * Ok now we proceed with copying
3553 * the old out stuff and
3554 * initializing the new stuff.
3556 SCTP_TCB_SEND_LOCK(stcb);
3557 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
3558 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3559 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3560 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3561 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3562 stcb->asoc.strmout[i].stream_no = i;
3563 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
3565 * now anything on those
3568 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
3569 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3570 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3573 * Now move assoc pointers
3576 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3577 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3579 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3580 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3583 /* now the new streams */
3584 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3585 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3586 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3587 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3588 stcb->asoc.strmout[i].stream_no = i;
3589 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3590 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3592 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3593 SCTP_FREE(oldstream, SCTP_M_STRMO);
3595 SCTP_TCB_SEND_UNLOCK(stcb);
3598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3600 SCTP_TCB_UNLOCK(stcb);
3603 for (i = 0; i < strrst->strrst_num_streams; i++) {
3606 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3607 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3612 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3613 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3621 SCTP_TCB_UNLOCK(stcb);
3624 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3625 strrst->strrst_list,
3626 send_out, (stcb->asoc.str_reset_seq_in - 3),
3627 send_in, send_tsn, addstream, addstrmcnt);
3629 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3630 SCTP_TCB_UNLOCK(stcb);
3634 case SCTP_CONNECT_X:
3635 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3636 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3640 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3643 case SCTP_CONNECT_X_DELAYED:
3644 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3645 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3649 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3652 case SCTP_CONNECT_X_COMPLETE:
3654 struct sockaddr *sa;
3655 struct sctp_nets *net;
3657 /* FIXME MT: check correct? */
3658 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3661 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3662 SCTP_INP_RLOCK(inp);
3663 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3665 SCTP_TCB_LOCK(stcb);
3666 net = sctp_findnet(stcb, sa);
3668 SCTP_INP_RUNLOCK(inp);
3671 * We increment here since
3672 * sctp_findassociation_ep_addr() wil do a
3673 * decrement if it finds the stcb as long as
3674 * the locked tcb (last argument) is NOT a
3677 SCTP_INP_INCR_REF(inp);
3678 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3680 SCTP_INP_DECR_REF(inp);
3685 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3689 if (stcb->asoc.delayed_connection == 1) {
3690 stcb->asoc.delayed_connection = 0;
3691 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3692 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3693 stcb->asoc.primary_destination,
3694 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3695 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3698 * already expired or did not use delayed
3701 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3704 SCTP_TCB_UNLOCK(stcb);
3707 case SCTP_MAX_BURST:
3711 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3713 SCTP_INP_WLOCK(inp);
3714 inp->sctp_ep.max_burst = *burst;
3715 SCTP_INP_WUNLOCK(inp);
3720 struct sctp_assoc_value *av;
3723 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3724 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3726 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3727 ovh = SCTP_MED_OVERHEAD;
3729 ovh = SCTP_MED_V4_OVERHEAD;
3732 if (av->assoc_value) {
3733 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3735 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3737 SCTP_TCB_UNLOCK(stcb);
3739 SCTP_INP_WLOCK(inp);
3741 * FIXME MT: I think this is not in tune
3744 if (av->assoc_value) {
3745 inp->sctp_frag_point = (av->assoc_value + ovh);
3747 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3749 SCTP_INP_WUNLOCK(inp);
3755 struct sctp_event_subscribe *events;
3757 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3759 SCTP_INP_WLOCK(inp);
3760 if (events->sctp_data_io_event) {
3761 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3763 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3766 if (events->sctp_association_event) {
3767 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3769 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3772 if (events->sctp_address_event) {
3773 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3775 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3778 if (events->sctp_send_failure_event) {
3779 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3781 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3784 if (events->sctp_peer_error_event) {
3785 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3787 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3790 if (events->sctp_shutdown_event) {
3791 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3793 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3796 if (events->sctp_partial_delivery_event) {
3797 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3799 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3802 if (events->sctp_adaptation_layer_event) {
3803 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3805 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3808 if (events->sctp_authentication_event) {
3809 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3811 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3814 if (events->sctp_sender_dry_event) {
3815 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3816 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3817 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3818 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3820 SCTP_TCB_LOCK(stcb);
3823 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3824 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3825 (stcb->asoc.stream_queue_cnt == 0)) {
3826 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3829 SCTP_TCB_UNLOCK(stcb);
3833 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3836 if (events->sctp_stream_reset_event) {
3837 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3839 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3841 SCTP_INP_WUNLOCK(inp);
3845 case SCTP_ADAPTATION_LAYER:
3847 struct sctp_setadaptation *adap_bits;
3849 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3850 SCTP_INP_WLOCK(inp);
3851 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3852 SCTP_INP_WUNLOCK(inp);
3856 case SCTP_SET_INITIAL_DBG_SEQ:
3860 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3861 SCTP_INP_WLOCK(inp);
3862 inp->sctp_ep.initial_sequence_debug = *vvv;
3863 SCTP_INP_WUNLOCK(inp);
3867 case SCTP_DEFAULT_SEND_PARAM:
3869 struct sctp_sndrcvinfo *s_info;
3871 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3872 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3875 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3876 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3878 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3881 SCTP_TCB_UNLOCK(stcb);
3883 SCTP_INP_WLOCK(inp);
3884 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3885 SCTP_INP_WUNLOCK(inp);
3889 case SCTP_PEER_ADDR_PARAMS:
3890 /* Applys to the specific association */
3892 struct sctp_paddrparams *paddrp;
3893 struct sctp_nets *net;
3895 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3896 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3899 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3902 * We increment here since
3903 * sctp_findassociation_ep_addr() wil do a
3904 * decrement if it finds the stcb as long as
3905 * the locked tcb (last argument) is NOT a
3908 SCTP_INP_INCR_REF(inp);
3909 stcb = sctp_findassociation_ep_addr(&inp,
3910 (struct sockaddr *)&paddrp->spp_address,
3913 SCTP_INP_DECR_REF(inp);
3916 if (stcb && (net == NULL)) {
3917 struct sockaddr *sa;
3919 sa = (struct sockaddr *)&paddrp->spp_address;
3921 if (sa->sa_family == AF_INET) {
3923 struct sockaddr_in *sin;
3925 sin = (struct sockaddr_in *)sa;
3926 if (sin->sin_addr.s_addr) {
3927 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3928 SCTP_TCB_UNLOCK(stcb);
3935 if (sa->sa_family == AF_INET6) {
3936 struct sockaddr_in6 *sin6;
3938 sin6 = (struct sockaddr_in6 *)sa;
3939 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3940 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3941 SCTP_TCB_UNLOCK(stcb);
3948 error = EAFNOSUPPORT;
3949 SCTP_TCB_UNLOCK(stcb);
3950 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3955 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3957 SCTP_TCB_UNLOCK(stcb);
3958 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3961 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3963 SCTP_TCB_UNLOCK(stcb);
3964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3968 /************************TCB SPECIFIC SET ******************/
3970 * do we change the timer for HB, we run
3975 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3976 ovh = SCTP_MED_OVERHEAD;
3978 ovh = SCTP_MED_V4_OVERHEAD;
3981 if (paddrp->spp_hbinterval)
3982 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3983 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3984 stcb->asoc.heart_beat_delay = 0;
3986 /* network sets ? */
3988 /************************NET SPECIFIC SET ******************/
3989 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3991 if (sctp_send_hb(stcb, 1, net, SCTP_SO_LOCKED) < 0) {
3992 /* asoc destroyed */
3993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3998 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3999 net->dest_state |= SCTP_ADDR_NOHB;
4001 if (paddrp->spp_flags & SPP_HB_ENABLE) {
4002 net->dest_state &= ~SCTP_ADDR_NOHB;
4004 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
4005 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4006 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
4007 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4009 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
4010 net->mtu = paddrp->spp_pathmtu + ovh;
4011 if (net->mtu < stcb->asoc.smallest_mtu) {
4012 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
4016 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
4017 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4018 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
4021 if (paddrp->spp_pathmaxrxt)
4022 net->failure_threshold = paddrp->spp_pathmaxrxt;
4024 if (paddrp->spp_flags & SPP_IPV4_TOS) {
4025 if (net->ro._l_addr.sin.sin_family == AF_INET) {
4026 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
4031 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
4032 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
4033 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
4038 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
4039 if (paddrp->spp_pathmaxrxt)
4040 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
4042 if (paddrp->spp_flags & SPP_HB_ENABLE) {
4043 /* Turn back on the timer */
4044 stcb->asoc.hb_is_disabled = 0;
4045 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4047 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
4048 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4049 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4050 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
4051 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4053 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
4054 net->mtu = paddrp->spp_pathmtu + ovh;
4055 if (net->mtu < stcb->asoc.smallest_mtu) {
4056 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
4061 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
4062 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4063 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4064 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
4068 if (paddrp->spp_flags & SPP_HB_DISABLE) {
4069 int cnt_of_unconf = 0;
4070 struct sctp_nets *lnet;
4072 stcb->asoc.hb_is_disabled = 1;
4073 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
4074 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
4079 * stop the timer ONLY if we
4080 * have no unconfirmed
4083 if (cnt_of_unconf == 0) {
4084 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4085 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4086 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
4090 if (paddrp->spp_flags & SPP_HB_ENABLE) {
4091 /* start up the timer. */
4092 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4093 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4097 if (paddrp->spp_flags & SPP_IPV4_TOS)
4098 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
4101 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
4102 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
4106 SCTP_TCB_UNLOCK(stcb);
4108 /************************NO TCB, SET TO default stuff ******************/
4109 SCTP_INP_WLOCK(inp);
4111 * For the TOS/FLOWLABEL stuff you set it
4112 * with the options on the socket
4114 if (paddrp->spp_pathmaxrxt) {
4115 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
4117 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
4118 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
4119 else if (paddrp->spp_hbinterval) {
4120 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
4121 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
4122 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
4124 if (paddrp->spp_flags & SPP_HB_ENABLE) {
4125 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4127 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
4128 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4130 SCTP_INP_WUNLOCK(inp);
4136 struct sctp_rtoinfo *srto;
4137 uint32_t new_init, new_min, new_max;
4139 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
4140 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
4143 if (srto->srto_initial)
4144 new_init = srto->srto_initial;
4146 new_init = stcb->asoc.initial_rto;
4148 new_max = srto->srto_max;
4150 new_max = stcb->asoc.maxrto;
4152 new_min = srto->srto_min;
4154 new_min = stcb->asoc.minrto;
4155 if ((new_min <= new_init) && (new_init <= new_max)) {
4156 stcb->asoc.initial_rto = new_init;
4157 stcb->asoc.maxrto = new_max;
4158 stcb->asoc.minrto = new_min;
4160 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4163 SCTP_TCB_UNLOCK(stcb);
4165 SCTP_INP_WLOCK(inp);
4166 if (srto->srto_initial)
4167 new_init = srto->srto_initial;
4169 new_init = inp->sctp_ep.initial_rto;
4171 new_max = srto->srto_max;
4173 new_max = inp->sctp_ep.sctp_maxrto;
4175 new_min = srto->srto_min;
4177 new_min = inp->sctp_ep.sctp_minrto;
4178 if ((new_min <= new_init) && (new_init <= new_max)) {
4179 inp->sctp_ep.initial_rto = new_init;
4180 inp->sctp_ep.sctp_maxrto = new_max;
4181 inp->sctp_ep.sctp_minrto = new_min;
4183 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4186 SCTP_INP_WUNLOCK(inp);
4190 case SCTP_ASSOCINFO:
4192 struct sctp_assocparams *sasoc;
4194 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4195 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4196 if (sasoc->sasoc_cookie_life) {
4197 /* boundary check the cookie life */
4198 if (sasoc->sasoc_cookie_life < 1000)
4199 sasoc->sasoc_cookie_life = 1000;
4200 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4201 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4205 if (sasoc->sasoc_asocmaxrxt)
4206 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4207 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4208 sasoc->sasoc_peer_rwnd = 0;
4209 sasoc->sasoc_local_rwnd = 0;
4210 if (sasoc->sasoc_cookie_life) {
4211 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4213 SCTP_TCB_UNLOCK(stcb);
4215 SCTP_INP_WLOCK(inp);
4216 if (sasoc->sasoc_asocmaxrxt)
4217 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4218 sasoc->sasoc_number_peer_destinations = 0;
4219 sasoc->sasoc_peer_rwnd = 0;
4220 sasoc->sasoc_local_rwnd = 0;
4221 if (sasoc->sasoc_cookie_life) {
4222 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4224 SCTP_INP_WUNLOCK(inp);
4230 struct sctp_initmsg *sinit;
4232 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4233 SCTP_INP_WLOCK(inp);
4234 if (sinit->sinit_num_ostreams)
4235 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4237 if (sinit->sinit_max_instreams)
4238 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4240 if (sinit->sinit_max_attempts)
4241 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4243 if (sinit->sinit_max_init_timeo)
4244 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4245 SCTP_INP_WUNLOCK(inp);
4248 case SCTP_PRIMARY_ADDR:
4250 struct sctp_setprim *spa;
4251 struct sctp_nets *net, *lnet;
4253 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4254 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4258 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4261 * We increment here since
4262 * sctp_findassociation_ep_addr() wil do a
4263 * decrement if it finds the stcb as long as
4264 * the locked tcb (last argument) is NOT a
4267 SCTP_INP_INCR_REF(inp);
4268 stcb = sctp_findassociation_ep_addr(&inp,
4269 (struct sockaddr *)&spa->ssp_addr,
4272 SCTP_INP_DECR_REF(inp);
4276 if ((stcb) && (net)) {
4277 if ((net != stcb->asoc.primary_destination) &&
4278 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4279 /* Ok we need to set it */
4280 lnet = stcb->asoc.primary_destination;
4281 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4282 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4283 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4285 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4289 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4293 SCTP_TCB_UNLOCK(stcb);
4297 case SCTP_SET_DYNAMIC_PRIMARY:
4299 union sctp_sockstore *ss;
4301 error = priv_check(curthread,
4302 PRIV_NETINET_RESERVEDPORT);
4306 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4307 /* SUPER USER CHECK? */
4308 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4311 case SCTP_SET_PEER_PRIMARY_ADDR:
4313 struct sctp_setpeerprim *sspp;
4315 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4316 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4318 struct sctp_ifa *ifa;
4320 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4321 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4323 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4327 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4329 * Must validate the ifa found is in
4332 struct sctp_laddr *laddr;
4335 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4336 if (laddr->ifa == NULL) {
4337 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4341 if (laddr->ifa == ifa) {
4347 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4352 if (sctp_set_primary_ip_address_sa(stcb,
4353 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4354 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4358 SCTP_TCB_UNLOCK(stcb);
4360 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4366 case SCTP_BINDX_ADD_ADDR:
4368 struct sctp_getaddresses *addrs;
4372 td = (struct thread *)p;
4373 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4376 if (addrs->addr->sa_family == AF_INET) {
4377 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4379 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4383 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4384 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4390 if (addrs->addr->sa_family == AF_INET6) {
4391 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4393 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4397 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4398 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4399 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4405 error = EAFNOSUPPORT;
4408 sctp_bindx_add_address(so, inp, addrs->addr,
4409 addrs->sget_assoc_id, vrf_id,
4413 case SCTP_BINDX_REM_ADDR:
4415 struct sctp_getaddresses *addrs;
4419 td = (struct thread *)p;
4421 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4423 if (addrs->addr->sa_family == AF_INET) {
4424 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4426 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4430 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4431 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4437 if (addrs->addr->sa_family == AF_INET6) {
4438 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4440 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4444 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4445 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4446 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4452 error = EAFNOSUPPORT;
4455 sctp_bindx_delete_address(so, inp, addrs->addr,
4456 addrs->sget_assoc_id, vrf_id,
4461 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4462 error = ENOPROTOOPT;
4464 } /* end switch (opt) */
4469 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4471 void *optval = NULL;
4473 struct sctp_inpcb *inp;
4477 inp = (struct sctp_inpcb *)so->so_pcb;
4479 /* I made the same as TCP since we are not setup? */
4480 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4481 return (ECONNRESET);
4483 if (sopt->sopt_level != IPPROTO_SCTP) {
4484 /* wrong proto level... send back up to IP */
4486 if (INP_CHECK_SOCKAF(so, AF_INET6))
4487 error = ip6_ctloutput(so, sopt);
4489 #if defined(INET) && defined (INET6)
4493 error = ip_ctloutput(so, sopt);
4497 optsize = sopt->sopt_valsize;
4499 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4500 if (optval == NULL) {
4501 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4504 error = sooptcopyin(sopt, optval, optsize, optsize);
4506 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4510 p = (void *)sopt->sopt_td;
4511 if (sopt->sopt_dir == SOPT_SET) {
4512 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4513 } else if (sopt->sopt_dir == SOPT_GET) {
4514 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4516 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4519 if ((error == 0) && (optval != NULL)) {
4520 error = sooptcopyout(sopt, optval, optsize);
4521 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4522 } else if (optval != NULL) {
4523 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4531 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4534 int create_lock_on = 0;
4536 struct sctp_inpcb *inp;
4537 struct sctp_tcb *stcb = NULL;
4539 inp = (struct sctp_inpcb *)so->so_pcb;
4541 /* I made the same as TCP since we are not setup? */
4542 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4543 return (ECONNRESET);
4546 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4549 switch (addr->sa_family) {
4553 struct sockaddr_in6 *sin6p;
4555 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4556 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4559 sin6p = (struct sockaddr_in6 *)addr;
4560 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4561 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4570 struct sockaddr_in *sinp;
4572 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4573 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4576 sinp = (struct sockaddr_in *)addr;
4577 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4578 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4586 return (EAFNOSUPPORT);
4588 SCTP_INP_INCR_REF(inp);
4589 SCTP_ASOC_CREATE_LOCK(inp);
4593 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4594 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4595 /* Should I really unlock ? */
4596 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4601 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4602 (addr->sa_family == AF_INET6)) {
4603 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4608 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4609 SCTP_PCB_FLAGS_UNBOUND) {
4610 /* Bind a ephemeral port */
4611 error = sctp_inpcb_bind(so, NULL, NULL, p);
4616 /* Now do we connect? */
4617 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4618 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4619 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4623 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4624 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4625 /* We are already connected AND the TCP model */
4626 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4630 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4631 SCTP_INP_RLOCK(inp);
4632 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4633 SCTP_INP_RUNLOCK(inp);
4636 * We increment here since sctp_findassociation_ep_addr()
4637 * will do a decrement if it finds the stcb as long as the
4638 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4640 SCTP_INP_INCR_REF(inp);
4641 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4643 SCTP_INP_DECR_REF(inp);
4645 SCTP_TCB_UNLOCK(stcb);
4649 /* Already have or am bring up an association */
4650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4654 vrf_id = inp->def_vrf_id;
4655 /* We are GOOD to go */
4656 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4658 /* Gak! no memory */
4661 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4662 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4663 /* Set the connected flag so we can queue data */
4664 SOCKBUF_LOCK(&so->so_rcv);
4665 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4666 SOCKBUF_UNLOCK(&so->so_rcv);
4667 SOCKBUF_LOCK(&so->so_snd);
4668 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4669 SOCKBUF_UNLOCK(&so->so_snd);
4671 so->so_state &= ~SS_ISDISCONNECTING;
4675 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4676 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4678 /* initialize authentication parameters for the assoc */
4679 sctp_initialize_auth_params(inp, stcb);
4681 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4682 SCTP_TCB_UNLOCK(stcb);
4684 if (create_lock_on) {
4685 SCTP_ASOC_CREATE_UNLOCK(inp);
4687 SCTP_INP_DECR_REF(inp);
4694 sctp_listen(struct socket *so, int backlog, struct thread *p)
4697 * Note this module depends on the protocol processing being called
4698 * AFTER any socket level flags and backlog are applied to the
4699 * socket. The traditional way that the socket flags are applied is
4700 * AFTER protocol processing. We have made a change to the
4701 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4702 * place if the socket API for SCTP is to work properly.
4706 struct sctp_inpcb *inp;
4708 inp = (struct sctp_inpcb *)so->so_pcb;
4710 /* I made the same as TCP since we are not setup? */
4711 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4712 return (ECONNRESET);
4714 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4715 /* See if we have a listener */
4716 struct sctp_inpcb *tinp;
4717 union sctp_sockstore store, *sp;
4720 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4722 struct sctp_laddr *laddr;
4724 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4725 memcpy(&store, &laddr->ifa->address, sizeof(store));
4726 switch (sp->sa.sa_family) {
4729 sp->sin.sin_port = inp->sctp_lport;
4734 sp->sin6.sin6_port = inp->sctp_lport;
4740 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4741 if (tinp && (tinp != inp) &&
4742 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4743 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4744 (tinp->sctp_socket->so_qlimit)) {
4746 * we have a listener already and
4749 SCTP_INP_DECR_REF(tinp);
4750 return (EADDRINUSE);
4752 SCTP_INP_DECR_REF(tinp);
4756 /* Setup a local addr bound all */
4757 memset(&store, 0, sizeof(store));
4758 switch (sp->sa.sa_family) {
4761 store.sin.sin_port = inp->sctp_lport;
4766 sp->sin6.sin6_port = inp->sctp_lport;
4773 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4774 store.sa.sa_family = AF_INET6;
4775 store.sa.sa_len = sizeof(struct sockaddr_in6);
4779 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4780 store.sa.sa_family = AF_INET;
4781 store.sa.sa_len = sizeof(struct sockaddr_in);
4784 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4785 if (tinp && (tinp != inp) &&
4786 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4787 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4788 (tinp->sctp_socket->so_qlimit)) {
4790 * we have a listener already and its not
4793 SCTP_INP_DECR_REF(tinp);
4794 return (EADDRINUSE);
4796 SCTP_INP_DECR_REF(inp);
4800 SCTP_INP_RLOCK(inp);
4801 #ifdef SCTP_LOCK_LOGGING
4802 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4803 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4807 error = solisten_proto_check(so);
4810 SCTP_INP_RUNLOCK(inp);
4813 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4814 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4816 * The unlucky case - We are in the tcp pool with this guy.
4817 * - Someone else is in the main inp slot. - We must move
4818 * this guy (the listener) to the main slot - We must then
4819 * move the guy that was listener to the TCP Pool.
4821 if (sctp_swap_inpcb_for_listen(inp)) {
4825 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4826 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4827 /* We are already connected AND the TCP model */
4829 SCTP_INP_RUNLOCK(inp);
4831 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4832 return (EADDRINUSE);
4834 SCTP_INP_RUNLOCK(inp);
4835 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4836 /* We must do a bind. */
4838 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4839 /* bind error, probably perm */
4844 /* It appears for 7.0 and on, we must always call this. */
4845 solisten_proto(so, backlog);
4846 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4847 /* remove the ACCEPTCONN flag for one-to-many sockets */
4848 so->so_options &= ~SO_ACCEPTCONN;
4851 /* turning off listen */
4852 so->so_options &= ~SO_ACCEPTCONN;
4858 static int sctp_defered_wakeup_cnt = 0;
4861 sctp_accept(struct socket *so, struct sockaddr **addr)
4863 struct sctp_tcb *stcb;
4864 struct sctp_inpcb *inp;
4865 union sctp_sockstore store;
4871 inp = (struct sctp_inpcb *)so->so_pcb;
4874 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4875 return (ECONNRESET);
4877 SCTP_INP_RLOCK(inp);
4878 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4879 SCTP_INP_RUNLOCK(inp);
4880 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4881 return (EOPNOTSUPP);
4883 if (so->so_state & SS_ISDISCONNECTED) {
4884 SCTP_INP_RUNLOCK(inp);
4885 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4886 return (ECONNABORTED);
4888 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4890 SCTP_INP_RUNLOCK(inp);
4891 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4892 return (ECONNRESET);
4894 SCTP_TCB_LOCK(stcb);
4895 SCTP_INP_RUNLOCK(inp);
4896 store = stcb->asoc.primary_destination->ro._l_addr;
4897 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4898 SCTP_TCB_UNLOCK(stcb);
4899 switch (store.sa.sa_family) {
4903 struct sockaddr_in *sin;
4905 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4908 sin->sin_family = AF_INET;
4909 sin->sin_len = sizeof(*sin);
4910 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4911 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4912 *addr = (struct sockaddr *)sin;
4919 struct sockaddr_in6 *sin6;
4921 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4924 sin6->sin6_family = AF_INET6;
4925 sin6->sin6_len = sizeof(*sin6);
4926 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4928 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4929 if ((error = sa6_recoverscope(sin6)) != 0) {
4930 SCTP_FREE_SONAME(sin6);
4933 *addr = (struct sockaddr *)sin6;
4941 /* Wake any delayed sleep action */
4942 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4943 SCTP_INP_WLOCK(inp);
4944 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4945 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4946 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4947 SCTP_INP_WUNLOCK(inp);
4948 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4949 if (sowriteable(inp->sctp_socket)) {
4950 sowwakeup_locked(inp->sctp_socket);
4952 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4954 SCTP_INP_WLOCK(inp);
4956 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4957 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4958 SCTP_INP_WUNLOCK(inp);
4959 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4960 if (soreadable(inp->sctp_socket)) {
4961 sctp_defered_wakeup_cnt++;
4962 sorwakeup_locked(inp->sctp_socket);
4964 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4966 SCTP_INP_WLOCK(inp);
4968 SCTP_INP_WUNLOCK(inp);
4970 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4971 SCTP_TCB_LOCK(stcb);
4972 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4979 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4981 struct sockaddr_in *sin;
4983 struct sctp_inpcb *inp;
4984 struct sctp_ifa *sctp_ifa;
4987 * Do the malloc first in case it blocks.
4989 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4992 sin->sin_family = AF_INET;
4993 sin->sin_len = sizeof(*sin);
4994 inp = (struct sctp_inpcb *)so->so_pcb;
4996 SCTP_FREE_SONAME(sin);
4997 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5000 SCTP_INP_RLOCK(inp);
5001 sin->sin_port = inp->sctp_lport;
5002 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5003 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5004 struct sctp_tcb *stcb;
5005 struct sockaddr_in *sin_a;
5006 struct sctp_nets *net;
5009 stcb = LIST_FIRST(&inp->sctp_asoc_list);
5015 SCTP_TCB_LOCK(stcb);
5016 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5017 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
5019 /* this will make coverity happy */
5022 if (sin_a->sin_family == AF_INET) {
5027 if ((!fnd) || (sin_a == NULL)) {
5029 SCTP_TCB_UNLOCK(stcb);
5032 vrf_id = inp->def_vrf_id;
5033 sctp_ifa = sctp_source_address_selection(inp,
5035 (sctp_route_t *) & net->ro,
5038 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
5039 sctp_free_ifa(sctp_ifa);
5041 SCTP_TCB_UNLOCK(stcb);
5043 /* For the bound all case you get back 0 */
5045 sin->sin_addr.s_addr = 0;
5049 /* Take the first IPv4 address in the list */
5050 struct sctp_laddr *laddr;
5053 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5054 if (laddr->ifa->address.sa.sa_family == AF_INET) {
5055 struct sockaddr_in *sin_a;
5057 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
5058 sin->sin_addr = sin_a->sin_addr;
5064 SCTP_FREE_SONAME(sin);
5065 SCTP_INP_RUNLOCK(inp);
5066 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
5070 SCTP_INP_RUNLOCK(inp);
5071 (*addr) = (struct sockaddr *)sin;
5076 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
5078 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
5080 struct sockaddr_in *sin_a;
5081 struct sctp_inpcb *inp;
5082 struct sctp_tcb *stcb;
5083 struct sctp_nets *net;
5085 /* Do the malloc first in case it blocks. */
5086 inp = (struct sctp_inpcb *)so->so_pcb;
5087 if ((inp == NULL) ||
5088 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
5089 /* UDP type and listeners will drop out here */
5090 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
5093 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
5096 sin->sin_family = AF_INET;
5097 sin->sin_len = sizeof(*sin);
5099 /* We must recapture incase we blocked */
5100 inp = (struct sctp_inpcb *)so->so_pcb;
5102 SCTP_FREE_SONAME(sin);
5103 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5106 SCTP_INP_RLOCK(inp);
5107 stcb = LIST_FIRST(&inp->sctp_asoc_list);
5109 SCTP_TCB_LOCK(stcb);
5111 SCTP_INP_RUNLOCK(inp);
5113 SCTP_FREE_SONAME(sin);
5114 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5118 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5119 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
5120 if (sin_a->sin_family == AF_INET) {
5122 sin->sin_port = stcb->rport;
5123 sin->sin_addr = sin_a->sin_addr;
5127 SCTP_TCB_UNLOCK(stcb);
5129 /* No IPv4 address */
5130 SCTP_FREE_SONAME(sin);
5131 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
5134 (*addr) = (struct sockaddr *)sin;
5139 struct pr_usrreqs sctp_usrreqs = {
5140 .pru_abort = sctp_abort,
5141 .pru_accept = sctp_accept,
5142 .pru_attach = sctp_attach,
5143 .pru_bind = sctp_bind,
5144 .pru_connect = sctp_connect,
5145 .pru_control = in_control,
5146 .pru_close = sctp_close,
5147 .pru_detach = sctp_close,
5148 .pru_sopoll = sopoll_generic,
5149 .pru_flush = sctp_flush,
5150 .pru_disconnect = sctp_disconnect,
5151 .pru_listen = sctp_listen,
5152 .pru_peeraddr = sctp_peeraddr,
5153 .pru_send = sctp_sendm,
5154 .pru_shutdown = sctp_shutdown,
5155 .pru_sockaddr = sctp_ingetaddr,
5156 .pru_sosend = sctp_sosend,
5157 .pru_soreceive = sctp_soreceive