2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctp_asconf.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_cc_functions.h>
52 #include <netinet/udp.h>
62 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
64 /* Initialize and modify the sysctled variables */
66 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
69 * Allow a user to take no more than 1/2 the number of clusters or
70 * the SB_MAX whichever is smaller for the send window.
72 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
74 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76 * Now for the recv window, should we take the same amount? or
77 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 * now I will just copy.
80 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
82 SCTP_BASE_VAR(first_time) = 0;
83 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
85 #if defined(SCTP_PACKET_LOGGING)
86 SCTP_BASE_VAR(packet_log_writers) = 0;
87 SCTP_BASE_VAR(packet_log_end) = 0;
88 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104 struct sctp_tcb *stcb,
105 struct sctp_nets *net,
108 struct sctp_tmit_chunk *chk;
110 /* Adjust that too */
111 stcb->asoc.smallest_mtu = nxtsz;
112 /* now off to subtract IP_DF flag if needed */
113 #ifdef SCTP_PRINT_FOR_B_AND_M
114 SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
115 inp, stcb, net, nxtsz);
117 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
118 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
119 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
122 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
123 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
125 * For this guy we also mark for immediate resend
126 * since we sent to big of chunk
128 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
129 if (chk->sent < SCTP_DATAGRAM_RESEND) {
130 sctp_flight_size_decrease(chk);
131 sctp_total_flight_decrease(stcb, chk);
133 if (chk->sent != SCTP_DATAGRAM_RESEND) {
134 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
136 chk->sent = SCTP_DATAGRAM_RESEND;
137 chk->rec.data.doing_fast_retransmit = 0;
138 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
139 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
140 chk->whoTo->flight_size,
142 (uintptr_t) chk->whoTo,
143 chk->rec.data.TSN_seq);
145 /* Clear any time so NO RTT is being done */
152 sctp_notify_mbuf(struct sctp_inpcb *inp,
153 struct sctp_tcb *stcb,
154 struct sctp_nets *net,
159 int totsz, tmr_stopped = 0;
163 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
164 (ip == NULL) || (sh == NULL)) {
166 SCTP_TCB_UNLOCK(stcb);
170 /* First job is to verify the vtag matches what I would send */
171 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
172 SCTP_TCB_UNLOCK(stcb);
175 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
177 if (icmph->icmp_type != ICMP_UNREACH) {
178 /* We only care about unreachable */
179 SCTP_TCB_UNLOCK(stcb);
182 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
183 /* not a unreachable message due to frag. */
184 SCTP_TCB_UNLOCK(stcb);
189 nxtsz = ntohs(icmph->icmp_nextmtu);
192 * old type router that does not tell us what the next size
193 * mtu is. Rats we will have to guess (in a educated fashion
196 nxtsz = find_next_best_mtu(totsz);
198 /* Stop any PMTU timer */
199 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
201 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
202 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
204 /* Adjust destination size limit */
205 if (net->mtu > nxtsz) {
208 net->mtu -= sizeof(struct udphdr);
211 /* now what about the ep? */
212 if (stcb->asoc.smallest_mtu > nxtsz) {
213 #ifdef SCTP_PRINT_FOR_B_AND_M
214 SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
217 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
220 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
222 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 (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
299 net->dest_state &= ~SCTP_ADDR_PF;
300 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
303 net->error_count = net->failure_threshold + 1;
304 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
305 stcb, SCTP_FAILED_THRESHOLD,
306 (void *)net, SCTP_SO_NOT_LOCKED);
308 SCTP_TCB_UNLOCK(stcb);
309 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
310 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
312 * Here the peer is either playing tricks on us, including
313 * an address that belongs to someone who does not support
314 * SCTP OR was a userland implementation that shutdown and
315 * now is dead. In either case treat it like a OOTB abort
318 reason = SCTP_PEER_FAULTY;
319 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
320 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
321 so = SCTP_INP_SO(inp);
322 atomic_add_int(&stcb->asoc.refcnt, 1);
323 SCTP_TCB_UNLOCK(stcb);
324 SCTP_SOCKET_LOCK(so, 1);
326 atomic_subtract_int(&stcb->asoc.refcnt, 1);
328 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
329 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
330 SCTP_SOCKET_UNLOCK(so, 1);
331 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
333 /* no need to unlock here, since the TCB is gone */
335 SCTP_TCB_UNLOCK(stcb);
340 sctp_ctlinput(cmd, sa, vip)
349 /* FIX, for non-bsd is this right? */
350 vrf_id = SCTP_DEFAULT_VRFID;
351 if (sa->sa_family != AF_INET ||
352 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
355 if (PRC_IS_REDIRECT(cmd)) {
357 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
361 struct sctp_inpcb *inp = NULL;
362 struct sctp_tcb *stcb = NULL;
363 struct sctp_nets *net = NULL;
364 struct sockaddr_in to, from;
366 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
367 bzero(&to, sizeof(to));
368 bzero(&from, sizeof(from));
369 from.sin_family = to.sin_family = AF_INET;
370 from.sin_len = to.sin_len = sizeof(to);
371 from.sin_port = sh->src_port;
372 from.sin_addr = ip->ip_src;
373 to.sin_port = sh->dest_port;
374 to.sin_addr = ip->ip_dst;
377 * 'to' holds the dest of the packet that failed to be sent.
378 * 'from' holds our local endpoint address. Thus we reverse
379 * the to and the from in the lookup.
381 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
382 (struct sockaddr *)&to,
383 &inp, &net, 1, vrf_id);
384 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
385 if (cmd != PRC_MSGSIZE) {
386 sctp_notify(inp, ip, sh,
387 (struct sockaddr *)&to, stcb,
390 /* handle possible ICMP size messages */
391 sctp_notify_mbuf(inp, stcb, net, ip, sh);
394 if ((stcb == NULL) && (inp != NULL)) {
395 /* reduce ref-count */
397 SCTP_INP_DECR_REF(inp);
398 SCTP_INP_WUNLOCK(inp);
406 sctp_getcred(SYSCTL_HANDLER_ARGS)
409 struct sockaddr_in addrs[2];
410 struct sctp_inpcb *inp;
411 struct sctp_nets *net;
412 struct sctp_tcb *stcb;
416 /* FIX, for non-bsd is this right? */
417 vrf_id = SCTP_DEFAULT_VRFID;
419 error = priv_check(req->td, PRIV_NETINET_GETCRED);
424 error = SYSCTL_IN(req, addrs, sizeof(addrs));
428 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
430 &inp, &net, 1, vrf_id);
431 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
432 if ((inp != NULL) && (stcb == NULL)) {
433 /* reduce ref-count */
435 SCTP_INP_DECR_REF(inp);
438 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
442 SCTP_TCB_UNLOCK(stcb);
444 * We use the write lock here, only since in the error leg we need
445 * it. If we used RLOCK, then we would have to
446 * wlock/decr/unlock/rlock. Which in theory could create a hole.
447 * Better to use higher wlock.
451 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
453 SCTP_INP_WUNLOCK(inp);
456 cru2x(inp->sctp_socket->so_cred, &xuc);
457 SCTP_INP_WUNLOCK(inp);
458 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
463 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
464 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
468 sctp_abort(struct socket *so)
470 struct sctp_inpcb *inp;
473 inp = (struct sctp_inpcb *)so->so_pcb;
478 flags = inp->sctp_flags;
479 #ifdef SCTP_LOG_CLOSING
480 sctp_log_closing(inp, NULL, 17);
482 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
483 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
484 #ifdef SCTP_LOG_CLOSING
485 sctp_log_closing(inp, NULL, 16);
487 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
488 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
490 SCTP_SB_CLEAR(so->so_snd);
492 * same for the rcv ones, they are only here for the
495 SCTP_SB_CLEAR(so->so_rcv);
497 /* Now null out the reference, we are completely detached. */
501 flags = inp->sctp_flags;
502 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
503 goto sctp_must_try_again;
510 sctp_attach(struct socket *so, int proto, struct thread *p)
512 struct sctp_inpcb *inp;
513 struct inpcb *ip_inp;
515 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
522 inp = (struct sctp_inpcb *)so->so_pcb;
524 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
527 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
528 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
533 error = sctp_inpcb_alloc(so, vrf_id);
537 inp = (struct sctp_inpcb *)so->so_pcb;
539 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
540 ip_inp = &inp->ip_inp.inp;
541 ip_inp->inp_vflag |= INP_IPV4;
542 ip_inp->inp_ip_ttl = MODULE_GLOBAL(MOD_INET, ip_defttl);
544 error = ipsec_init_policy(so, &ip_inp->inp_sp);
545 #ifdef SCTP_LOG_CLOSING
546 sctp_log_closing(inp, NULL, 17);
549 flags = inp->sctp_flags;
550 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
551 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
552 #ifdef SCTP_LOG_CLOSING
553 sctp_log_closing(inp, NULL, 15);
555 SCTP_INP_WUNLOCK(inp);
556 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
557 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
559 SCTP_INP_WUNLOCK(inp);
564 SCTP_INP_WUNLOCK(inp);
569 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
571 struct sctp_inpcb *inp = NULL;
575 if (addr && addr->sa_family != AF_INET) {
576 /* must be a v4 address! */
577 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
581 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
582 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
585 inp = (struct sctp_inpcb *)so->so_pcb;
587 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
590 error = sctp_inpcb_bind(so, addr, NULL, p);
595 sctp_close(struct socket *so)
597 struct sctp_inpcb *inp;
600 inp = (struct sctp_inpcb *)so->so_pcb;
605 * Inform all the lower layer assoc that we are done.
608 flags = inp->sctp_flags;
609 #ifdef SCTP_LOG_CLOSING
610 sctp_log_closing(inp, NULL, 17);
612 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
613 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
614 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
615 (so->so_rcv.sb_cc > 0)) {
616 #ifdef SCTP_LOG_CLOSING
617 sctp_log_closing(inp, NULL, 13);
619 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
620 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
622 #ifdef SCTP_LOG_CLOSING
623 sctp_log_closing(inp, NULL, 14);
625 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
626 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
629 * The socket is now detached, no matter what the state of
630 * the SCTP association.
633 SCTP_SB_CLEAR(so->so_snd);
635 * same for the rcv ones, they are only here for the
638 SCTP_SB_CLEAR(so->so_rcv);
640 /* Now null out the reference, we are completely detached. */
644 flags = inp->sctp_flags;
645 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
646 goto sctp_must_try_again;
654 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
655 struct mbuf *control, struct thread *p);
659 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
660 struct mbuf *control, struct thread *p)
662 struct sctp_inpcb *inp;
665 inp = (struct sctp_inpcb *)so->so_pcb;
668 sctp_m_freem(control);
671 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
675 /* Got to have an to address if we are NOT a connected socket */
676 if ((addr == NULL) &&
677 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
678 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
681 } else if (addr == NULL) {
682 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
683 error = EDESTADDRREQ;
686 sctp_m_freem(control);
692 if (addr->sa_family != AF_INET) {
693 /* must be a v4 address! */
694 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
697 sctp_m_freem(control);
700 error = EDESTADDRREQ;
705 /* now what about control */
708 SCTP_PRINTF("huh? control set?\n");
709 sctp_m_freem(inp->control);
712 inp->control = control;
716 SCTP_BUF_NEXT(inp->pkt_last) = m;
719 inp->pkt_last = inp->pkt = m;
722 /* FreeBSD uses a flag passed */
723 ((flags & PRUS_MORETOCOME) == 0)
726 * note with the current version this code will only be used
727 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
728 * re-defining sosend to use the sctp_sosend. One can
729 * optionally switch back to this code (by changing back the
730 * definitions) but this is not advisable. This code is used
731 * by FreeBSD when sending a file with sendfile() though.
735 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
745 sctp_disconnect(struct socket *so)
747 struct sctp_inpcb *inp;
749 inp = (struct sctp_inpcb *)so->so_pcb;
751 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
755 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
756 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
757 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
759 SCTP_INP_RUNLOCK(inp);
762 struct sctp_association *asoc;
763 struct sctp_tcb *stcb;
765 stcb = LIST_FIRST(&inp->sctp_asoc_list);
767 SCTP_INP_RUNLOCK(inp);
768 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
773 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
774 /* We are about to be freed, out of here */
775 SCTP_TCB_UNLOCK(stcb);
776 SCTP_INP_RUNLOCK(inp);
779 if (((so->so_options & SO_LINGER) &&
780 (so->so_linger == 0)) ||
781 (so->so_rcv.sb_cc > 0)) {
782 if (SCTP_GET_STATE(asoc) !=
783 SCTP_STATE_COOKIE_WAIT) {
784 /* Left with Data unread */
787 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
793 struct sctp_paramhdr *ph;
795 ph = mtod(err, struct sctp_paramhdr *);
796 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
797 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
798 ph->param_length = htons(SCTP_BUF_LEN(err));
800 #if defined(SCTP_PANIC_ON_ABORT)
801 panic("disconnect does an abort");
803 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
804 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
806 SCTP_INP_RUNLOCK(inp);
807 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
808 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
809 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
811 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
812 /* No unlock tcb assoc is gone */
815 if (TAILQ_EMPTY(&asoc->send_queue) &&
816 TAILQ_EMPTY(&asoc->sent_queue) &&
817 (asoc->stream_queue_cnt == 0)) {
818 /* there is nothing queued to send, so done */
819 if (asoc->locked_on_sending) {
822 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
823 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
824 /* only send SHUTDOWN 1st time thru */
825 sctp_stop_timers_for_shutdown(stcb);
826 sctp_send_shutdown(stcb,
827 stcb->asoc.primary_destination);
828 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
829 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
830 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
831 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
833 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
834 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
835 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
837 asoc->primary_destination);
838 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
840 asoc->primary_destination);
844 * we still got (or just got) data to send,
845 * so set SHUTDOWN_PENDING
848 * XXX sockets draft says that SCTP_EOF
849 * should be sent with no data. currently,
850 * we will allow user data to be sent first
851 * and move to SHUTDOWN-PENDING
853 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
854 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
855 asoc->primary_destination);
856 if (asoc->locked_on_sending) {
857 /* Locked to send out the data */
858 struct sctp_stream_queue_pending *sp;
860 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
862 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
863 asoc->locked_on_sending->stream_no);
865 if ((sp->length == 0) && (sp->msg_is_complete == 0))
866 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
869 if (TAILQ_EMPTY(&asoc->send_queue) &&
870 TAILQ_EMPTY(&asoc->sent_queue) &&
871 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
875 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
876 0, M_DONTWAIT, 1, MT_DATA);
882 struct sctp_paramhdr *ph;
885 SCTP_BUF_LEN(op_err) =
886 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
888 struct sctp_paramhdr *);
889 ph->param_type = htons(
890 SCTP_CAUSE_USER_INITIATED_ABT);
891 ph->param_length = htons(SCTP_BUF_LEN(op_err));
892 ippp = (uint32_t *) (ph + 1);
893 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
895 #if defined(SCTP_PANIC_ON_ABORT)
896 panic("disconnect does an abort");
899 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
900 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
901 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
902 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
903 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
904 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
906 SCTP_INP_RUNLOCK(inp);
907 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
910 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
913 soisdisconnecting(so);
914 SCTP_TCB_UNLOCK(stcb);
915 SCTP_INP_RUNLOCK(inp);
920 /* UDP model does not support this */
921 SCTP_INP_RUNLOCK(inp);
922 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
928 sctp_flush(struct socket *so, int how)
931 * We will just clear out the values and let subsequent close clear
932 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
933 * they will not be able to read the data, the socket will block
934 * that from happening.
936 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
938 * First make sure the sb will be happy, we don't use these
939 * except maybe the count
941 so->so_rcv.sb_cc = 0;
942 so->so_rcv.sb_mbcnt = 0;
943 so->so_rcv.sb_mb = NULL;
945 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
947 * First make sure the sb will be happy, we don't use these
948 * except maybe the count
950 so->so_snd.sb_cc = 0;
951 so->so_snd.sb_mbcnt = 0;
952 so->so_snd.sb_mb = NULL;
959 sctp_shutdown(struct socket *so)
961 struct sctp_inpcb *inp;
963 inp = (struct sctp_inpcb *)so->so_pcb;
965 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
969 /* For UDP model this is a invalid call */
970 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
971 /* Restore the flags that the soshutdown took away. */
972 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
973 /* This proc will wakeup for read and do nothing (I hope) */
974 SCTP_INP_RUNLOCK(inp);
975 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
979 * Ok if we reach here its the TCP model and it is either a SHUT_WR
980 * or SHUT_RDWR. This means we put the shutdown flag against it.
983 struct sctp_tcb *stcb;
984 struct sctp_association *asoc;
987 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
988 SCTP_INP_RUNLOCK(inp);
993 stcb = LIST_FIRST(&inp->sctp_asoc_list);
996 * Ok we hit the case that the shutdown call was
997 * made after an abort or something. Nothing to do
1000 SCTP_INP_RUNLOCK(inp);
1003 SCTP_TCB_LOCK(stcb);
1005 if (TAILQ_EMPTY(&asoc->send_queue) &&
1006 TAILQ_EMPTY(&asoc->sent_queue) &&
1007 (asoc->stream_queue_cnt == 0)) {
1008 if (asoc->locked_on_sending) {
1011 /* there is nothing queued to send, so I'm done... */
1012 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1013 /* only send SHUTDOWN the first time through */
1014 sctp_stop_timers_for_shutdown(stcb);
1015 sctp_send_shutdown(stcb,
1016 stcb->asoc.primary_destination);
1017 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1018 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1019 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1020 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1022 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1023 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1024 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1025 stcb->sctp_ep, stcb,
1026 asoc->primary_destination);
1027 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1028 stcb->sctp_ep, stcb,
1029 asoc->primary_destination);
1033 * we still got (or just got) data to send, so set
1036 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1037 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1038 asoc->primary_destination);
1040 if (asoc->locked_on_sending) {
1041 /* Locked to send out the data */
1042 struct sctp_stream_queue_pending *sp;
1044 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1046 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1047 asoc->locked_on_sending->stream_no);
1049 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1050 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1054 if (TAILQ_EMPTY(&asoc->send_queue) &&
1055 TAILQ_EMPTY(&asoc->sent_queue) &&
1056 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1057 struct mbuf *op_err;
1060 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1061 0, M_DONTWAIT, 1, MT_DATA);
1063 /* Fill in the user initiated abort */
1064 struct sctp_paramhdr *ph;
1067 SCTP_BUF_LEN(op_err) =
1068 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1070 struct sctp_paramhdr *);
1071 ph->param_type = htons(
1072 SCTP_CAUSE_USER_INITIATED_ABT);
1073 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1074 ippp = (uint32_t *) (ph + 1);
1075 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1077 #if defined(SCTP_PANIC_ON_ABORT)
1078 panic("shutdown does an abort");
1080 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1081 sctp_abort_an_association(stcb->sctp_ep, stcb,
1082 SCTP_RESPONSE_TO_USER_REQ,
1083 op_err, SCTP_SO_LOCKED);
1086 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1089 SCTP_TCB_UNLOCK(stcb);
1092 SCTP_INP_RUNLOCK(inp);
1097 * copies a "user" presentable address and removes embedded scope, etc.
1098 * returns 0 on success, 1 on error
1101 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1104 struct sockaddr_in6 lsa6;
1106 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1109 memcpy(ss, sa, sa->sa_len);
1116 * NOTE: assumes addr lock is held
1119 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1120 struct sctp_tcb *stcb,
1122 struct sockaddr_storage *sas,
1125 struct sctp_ifn *sctp_ifn;
1126 struct sctp_ifa *sctp_ifa;
1127 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1129 int ipv4_addr_legal, ipv6_addr_legal;
1130 struct sctp_vrf *vrf;
1137 /* Turn on all the appropriate scope */
1138 loopback_scope = stcb->asoc.loopback_scope;
1139 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1140 local_scope = stcb->asoc.local_scope;
1141 site_scope = stcb->asoc.site_scope;
1143 /* Turn on ALL scope, since we look at the EP */
1144 loopback_scope = ipv4_local_scope = local_scope =
1147 ipv4_addr_legal = ipv6_addr_legal = 0;
1148 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1149 ipv6_addr_legal = 1;
1150 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1151 ipv4_addr_legal = 1;
1154 ipv4_addr_legal = 1;
1156 vrf = sctp_find_vrf(vrf_id);
1160 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1161 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1162 if ((loopback_scope == 0) &&
1163 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1164 /* Skip loopback if loopback_scope not set */
1167 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1170 * For the BOUND-ALL case, the list
1171 * associated with a TCB is Always
1172 * considered a reverse list.. i.e.
1173 * it lists addresses that are NOT
1174 * part of the association. If this
1175 * is one of those we must skip it.
1177 if (sctp_is_addr_restricted(stcb,
1182 switch (sctp_ifa->address.sa.sa_family) {
1184 if (ipv4_addr_legal) {
1185 struct sockaddr_in *sin;
1187 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1188 if (sin->sin_addr.s_addr == 0) {
1196 if ((ipv4_local_scope == 0) &&
1197 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1201 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1202 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1203 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1204 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1205 actual += sizeof(struct sockaddr_in6);
1208 memcpy(sas, sin, sizeof(*sin));
1209 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1210 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1211 actual += sizeof(*sin);
1215 if (actual >= limit) {
1224 if (ipv6_addr_legal) {
1225 struct sockaddr_in6 *sin6;
1227 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1228 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1236 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1237 if (local_scope == 0)
1239 if (sin6->sin6_scope_id == 0) {
1240 if (sa6_recoverscope(sin6) != 0)
1258 if ((site_scope == 0) &&
1259 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1262 memcpy(sas, sin6, sizeof(*sin6));
1263 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1264 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1265 actual += sizeof(*sin6);
1266 if (actual >= limit) {
1281 struct sctp_laddr *laddr;
1283 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1285 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1289 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1292 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1293 sas = (struct sockaddr_storage *)((caddr_t)sas +
1294 laddr->ifa->address.sa.sa_len);
1295 actual += laddr->ifa->address.sa.sa_len;
1296 if (actual >= limit) {
1305 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1306 struct sctp_tcb *stcb,
1308 struct sockaddr_storage *sas)
1312 SCTP_IPI_ADDR_RLOCK();
1313 /* fill up addresses for the endpoint's default vrf */
1314 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1316 SCTP_IPI_ADDR_RUNLOCK();
1321 * NOTE: assumes addr lock is held
1324 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1327 struct sctp_vrf *vrf = NULL;
1330 * In both sub-set bound an bound_all cases we return the MAXIMUM
1331 * number of addresses that you COULD get. In reality the sub-set
1332 * bound may have an exclusion list for a given TCB OR in the
1333 * bound-all case a TCB may NOT include the loopback or other
1334 * addresses as well.
1336 vrf = sctp_find_vrf(vrf_id);
1340 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1341 struct sctp_ifn *sctp_ifn;
1342 struct sctp_ifa *sctp_ifa;
1344 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1345 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1346 /* Count them if they are the right type */
1347 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1348 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1349 cnt += sizeof(struct sockaddr_in6);
1351 cnt += sizeof(struct sockaddr_in);
1353 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1354 cnt += sizeof(struct sockaddr_in6);
1358 struct sctp_laddr *laddr;
1360 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1361 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1362 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1363 cnt += sizeof(struct sockaddr_in6);
1365 cnt += sizeof(struct sockaddr_in);
1367 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1368 cnt += sizeof(struct sockaddr_in6);
1375 sctp_count_max_addresses(struct sctp_inpcb *inp)
1379 SCTP_IPI_ADDR_RLOCK();
1380 /* count addresses for the endpoint's default VRF */
1381 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1382 SCTP_IPI_ADDR_RUNLOCK();
1387 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1388 size_t optsize, void *p, int delay)
1391 int creat_lock_on = 0;
1392 struct sctp_tcb *stcb = NULL;
1393 struct sockaddr *sa;
1394 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1397 int bad_addresses = 0;
1400 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1402 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1403 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1404 /* We are already connected AND the TCP model */
1405 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1406 return (EADDRINUSE);
1408 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1409 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1410 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1413 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1414 SCTP_INP_RLOCK(inp);
1415 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1416 SCTP_INP_RUNLOCK(inp);
1419 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1422 SCTP_INP_INCR_REF(inp);
1423 SCTP_ASOC_CREATE_LOCK(inp);
1425 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1426 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1427 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1431 totaddrp = (int *)optval;
1432 totaddr = *totaddrp;
1433 sa = (struct sockaddr *)(totaddrp + 1);
1434 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1435 if ((stcb != NULL) || bad_addresses) {
1436 /* Already have or am bring up an association */
1437 SCTP_ASOC_CREATE_UNLOCK(inp);
1440 SCTP_TCB_UNLOCK(stcb);
1441 if (bad_addresses == 0) {
1442 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1448 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1453 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1455 struct in6pcb *inp6;
1457 inp6 = (struct in6pcb *)inp;
1458 if (SCTP_IPV6_V6ONLY(inp6)) {
1460 * if IPV6_V6ONLY flag, ignore connections destined
1461 * to a v4 addr or v4-mapped addr
1463 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1469 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1470 SCTP_PCB_FLAGS_UNBOUND) {
1471 /* Bind a ephemeral port */
1472 error = sctp_inpcb_bind(so, NULL, NULL, p);
1477 /* FIX ME: do we want to pass in a vrf on the connect call? */
1478 vrf_id = inp->def_vrf_id;
1481 /* We are GOOD to go */
1482 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1486 /* Gak! no memory */
1489 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1490 /* move to second address */
1491 if (sa->sa_family == AF_INET)
1492 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1494 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1497 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1498 /* Fill in the return id */
1500 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1503 a_id = (sctp_assoc_t *) optval;
1504 *a_id = sctp_get_associd(stcb);
1506 /* initialize authentication parameters for the assoc */
1507 sctp_initialize_auth_params(inp, stcb);
1510 /* doing delayed connection */
1511 stcb->asoc.delayed_connection = 1;
1512 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1514 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1515 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1517 SCTP_TCB_UNLOCK(stcb);
1518 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1519 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1520 /* Set the connected flag so we can queue data */
1524 if (creat_lock_on) {
1525 SCTP_ASOC_CREATE_UNLOCK(inp);
1527 SCTP_INP_DECR_REF(inp);
1531 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1532 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1533 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1534 SCTP_INP_RLOCK(inp); \
1535 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1537 SCTP_TCB_LOCK(stcb); \
1539 SCTP_INP_RUNLOCK(inp); \
1540 } else if (assoc_id != 0) { \
1541 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1542 if (stcb == NULL) { \
1543 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1553 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1554 if (size < sizeof(type)) { \
1555 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1559 destp = (type *)srcp; \
1564 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1567 struct sctp_inpcb *inp = NULL;
1569 struct sctp_tcb *stcb = NULL;
1571 if (optval == NULL) {
1572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1575 inp = (struct sctp_inpcb *)so->so_pcb;
1577 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1584 case SCTP_AUTOCLOSE:
1585 case SCTP_EXPLICIT_EOR:
1586 case SCTP_AUTO_ASCONF:
1587 case SCTP_DISABLE_FRAGMENTS:
1588 case SCTP_I_WANT_MAPPED_V4_ADDR:
1589 case SCTP_USE_EXT_RCVINFO:
1590 SCTP_INP_RLOCK(inp);
1592 case SCTP_DISABLE_FRAGMENTS:
1593 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1595 case SCTP_I_WANT_MAPPED_V4_ADDR:
1596 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1598 case SCTP_AUTO_ASCONF:
1599 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1600 /* only valid for bound all sockets */
1601 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1603 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1608 case SCTP_EXPLICIT_EOR:
1609 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1612 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1614 case SCTP_USE_EXT_RCVINFO:
1615 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1617 case SCTP_AUTOCLOSE:
1618 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1619 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1625 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1626 error = ENOPROTOOPT;
1627 } /* end switch (sopt->sopt_name) */
1628 if (optname != SCTP_AUTOCLOSE) {
1629 /* make it an "on/off" value */
1632 if (*optsize < sizeof(val)) {
1633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1637 SCTP_INP_RUNLOCK(inp);
1639 /* return the option value */
1640 *(int *)optval = val;
1641 *optsize = sizeof(val);
1644 case SCTP_GET_PACKET_LOG:
1646 #ifdef SCTP_PACKET_LOGGING
1650 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1651 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1654 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1659 case SCTP_REUSE_PORT:
1663 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1664 /* Can't do this for a 1-m socket */
1668 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1669 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1670 *optsize = sizeof(uint32_t);
1673 case SCTP_PARTIAL_DELIVERY_POINT:
1677 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1678 *value = inp->partial_delivery_point;
1679 *optsize = sizeof(uint32_t);
1682 case SCTP_FRAGMENT_INTERLEAVE:
1686 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1687 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1688 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1689 *value = SCTP_FRAG_LEVEL_2;
1691 *value = SCTP_FRAG_LEVEL_1;
1694 *value = SCTP_FRAG_LEVEL_0;
1696 *optsize = sizeof(uint32_t);
1699 case SCTP_CMT_ON_OFF:
1701 struct sctp_assoc_value *av;
1703 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1704 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1705 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1707 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1708 SCTP_TCB_UNLOCK(stcb);
1711 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1715 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1716 error = ENOPROTOOPT;
1718 *optsize = sizeof(*av);
1721 /* EY - set socket option for nr_sacks */
1722 case SCTP_NR_SACK_ON_OFF:
1724 struct sctp_assoc_value *av;
1726 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1727 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
1728 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1730 av->assoc_value = stcb->asoc.sctp_nr_sack_on_off;
1731 SCTP_TCB_UNLOCK(stcb);
1734 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1738 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1739 error = ENOPROTOOPT;
1741 *optsize = sizeof(*av);
1744 /* JRS - Get socket option for pluggable congestion control */
1745 case SCTP_PLUGGABLE_CC:
1747 struct sctp_assoc_value *av;
1749 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1750 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1752 av->assoc_value = stcb->asoc.congestion_control_module;
1753 SCTP_TCB_UNLOCK(stcb);
1755 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1757 *optsize = sizeof(*av);
1760 case SCTP_GET_ADDR_LEN:
1762 struct sctp_assoc_value *av;
1764 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1767 if (av->assoc_value == AF_INET) {
1768 av->assoc_value = sizeof(struct sockaddr_in);
1773 if (av->assoc_value == AF_INET6) {
1774 av->assoc_value = sizeof(struct sockaddr_in6);
1779 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1781 *optsize = sizeof(*av);
1784 case SCTP_GET_ASSOC_NUMBER:
1786 uint32_t *value, cnt;
1788 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1790 SCTP_INP_RLOCK(inp);
1791 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1794 SCTP_INP_RUNLOCK(inp);
1796 *optsize = sizeof(uint32_t);
1800 case SCTP_GET_ASSOC_ID_LIST:
1802 struct sctp_assoc_ids *ids;
1803 unsigned int at, limit;
1805 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1807 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1808 SCTP_INP_RLOCK(inp);
1809 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1811 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1814 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1818 SCTP_INP_RUNLOCK(inp);
1819 ids->gaids_number_of_ids = at;
1820 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1825 struct sctp_assoc_value *av;
1827 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1828 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1831 av->assoc_value = stcb->asoc.context;
1832 SCTP_TCB_UNLOCK(stcb);
1834 SCTP_INP_RLOCK(inp);
1835 av->assoc_value = inp->sctp_context;
1836 SCTP_INP_RUNLOCK(inp);
1838 *optsize = sizeof(*av);
1843 uint32_t *default_vrfid;
1845 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1846 *default_vrfid = inp->def_vrf_id;
1849 case SCTP_GET_ASOC_VRF:
1851 struct sctp_assoc_value *id;
1853 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1854 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1860 id->assoc_value = stcb->asoc.vrf_id;
1863 case SCTP_GET_VRF_IDS:
1865 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1869 case SCTP_GET_NONCE_VALUES:
1871 struct sctp_get_nonce_values *gnv;
1873 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1874 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1877 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1878 gnv->gn_local_tag = stcb->asoc.my_vtag;
1879 SCTP_TCB_UNLOCK(stcb);
1881 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1884 *optsize = sizeof(*gnv);
1887 case SCTP_DELAYED_SACK:
1889 struct sctp_sack_info *sack;
1891 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1892 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1894 sack->sack_delay = stcb->asoc.delayed_ack;
1895 sack->sack_freq = stcb->asoc.sack_freq;
1896 SCTP_TCB_UNLOCK(stcb);
1898 SCTP_INP_RLOCK(inp);
1899 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1900 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1901 SCTP_INP_RUNLOCK(inp);
1903 *optsize = sizeof(*sack);
1907 case SCTP_GET_SNDBUF_USE:
1909 struct sctp_sockstat *ss;
1911 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1912 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1915 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1916 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1917 stcb->asoc.size_on_all_streams);
1918 SCTP_TCB_UNLOCK(stcb);
1920 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1923 *optsize = sizeof(struct sctp_sockstat);
1926 case SCTP_MAX_BURST:
1930 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1932 SCTP_INP_RLOCK(inp);
1933 *value = inp->sctp_ep.max_burst;
1934 SCTP_INP_RUNLOCK(inp);
1935 *optsize = sizeof(uint8_t);
1940 struct sctp_assoc_value *av;
1943 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1944 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1947 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1948 SCTP_TCB_UNLOCK(stcb);
1950 SCTP_INP_RLOCK(inp);
1951 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1952 ovh = SCTP_MED_OVERHEAD;
1954 ovh = SCTP_MED_V4_OVERHEAD;
1956 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1957 av->assoc_value = 0;
1959 av->assoc_value = inp->sctp_frag_point - ovh;
1960 SCTP_INP_RUNLOCK(inp);
1962 *optsize = sizeof(struct sctp_assoc_value);
1965 case SCTP_GET_STAT_LOG:
1966 error = sctp_fill_stat_log(optval, optsize);
1970 struct sctp_event_subscribe *events;
1972 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1973 memset(events, 0, sizeof(*events));
1974 SCTP_INP_RLOCK(inp);
1975 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1976 events->sctp_data_io_event = 1;
1978 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1979 events->sctp_association_event = 1;
1981 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1982 events->sctp_address_event = 1;
1984 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1985 events->sctp_send_failure_event = 1;
1987 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1988 events->sctp_peer_error_event = 1;
1990 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1991 events->sctp_shutdown_event = 1;
1993 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1994 events->sctp_partial_delivery_event = 1;
1996 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1997 events->sctp_adaptation_layer_event = 1;
1999 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2000 events->sctp_authentication_event = 1;
2002 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2003 events->sctp_sender_dry_event = 1;
2005 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2006 events->sctp_stream_reset_events = 1;
2007 SCTP_INP_RUNLOCK(inp);
2008 *optsize = sizeof(struct sctp_event_subscribe);
2012 case SCTP_ADAPTATION_LAYER:
2016 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2018 SCTP_INP_RLOCK(inp);
2019 *value = inp->sctp_ep.adaptation_layer_indicator;
2020 SCTP_INP_RUNLOCK(inp);
2021 *optsize = sizeof(uint32_t);
2024 case SCTP_SET_INITIAL_DBG_SEQ:
2028 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2029 SCTP_INP_RLOCK(inp);
2030 *value = inp->sctp_ep.initial_sequence_debug;
2031 SCTP_INP_RUNLOCK(inp);
2032 *optsize = sizeof(uint32_t);
2035 case SCTP_GET_LOCAL_ADDR_SIZE:
2039 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2040 SCTP_INP_RLOCK(inp);
2041 *value = sctp_count_max_addresses(inp);
2042 SCTP_INP_RUNLOCK(inp);
2043 *optsize = sizeof(uint32_t);
2046 case SCTP_GET_REMOTE_ADDR_SIZE:
2050 struct sctp_nets *net;
2052 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2053 /* FIXME MT: change to sctp_assoc_value? */
2054 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2058 /* Count the sizes */
2059 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2060 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2061 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2062 size += sizeof(struct sockaddr_in6);
2063 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2064 size += sizeof(struct sockaddr_in);
2070 SCTP_TCB_UNLOCK(stcb);
2071 *value = (uint32_t) size;
2073 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2076 *optsize = sizeof(uint32_t);
2079 case SCTP_GET_PEER_ADDRESSES:
2081 * Get the address information, an array is passed in to
2082 * fill up we pack it.
2086 struct sockaddr_storage *sas;
2087 struct sctp_nets *net;
2088 struct sctp_getaddresses *saddr;
2090 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2091 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2094 left = (*optsize) - sizeof(struct sctp_getaddresses);
2095 *optsize = sizeof(struct sctp_getaddresses);
2096 sas = (struct sockaddr_storage *)&saddr->addr[0];
2098 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2099 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2100 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2101 cpsz = sizeof(struct sockaddr_in6);
2102 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2103 cpsz = sizeof(struct sockaddr_in);
2109 /* not enough room. */
2113 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2114 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2115 /* Must map the address */
2116 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2117 (struct sockaddr_in6 *)sas);
2120 memcpy(sas, &net->ro._l_addr, cpsz);
2124 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2126 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2130 SCTP_TCB_UNLOCK(stcb);
2132 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2137 case SCTP_GET_LOCAL_ADDRESSES:
2139 size_t limit, actual;
2140 struct sockaddr_storage *sas;
2141 struct sctp_getaddresses *saddr;
2143 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2144 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2146 sas = (struct sockaddr_storage *)&saddr->addr[0];
2147 limit = *optsize - sizeof(sctp_assoc_t);
2148 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2150 SCTP_TCB_UNLOCK(stcb);
2152 *optsize = sizeof(struct sockaddr_storage) + actual;
2155 case SCTP_PEER_ADDR_PARAMS:
2157 struct sctp_paddrparams *paddrp;
2158 struct sctp_nets *net;
2160 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2161 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2165 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2168 * We increment here since
2169 * sctp_findassociation_ep_addr() wil do a
2170 * decrement if it finds the stcb as long as
2171 * the locked tcb (last argument) is NOT a
2174 SCTP_INP_INCR_REF(inp);
2175 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2177 SCTP_INP_DECR_REF(inp);
2180 if (stcb && (net == NULL)) {
2181 struct sockaddr *sa;
2183 sa = (struct sockaddr *)&paddrp->spp_address;
2184 if (sa->sa_family == AF_INET) {
2185 struct sockaddr_in *sin;
2187 sin = (struct sockaddr_in *)sa;
2188 if (sin->sin_addr.s_addr) {
2190 SCTP_TCB_UNLOCK(stcb);
2191 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2194 } else if (sa->sa_family == AF_INET6) {
2195 struct sockaddr_in6 *sin6;
2197 sin6 = (struct sockaddr_in6 *)sa;
2198 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2200 SCTP_TCB_UNLOCK(stcb);
2201 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2205 error = EAFNOSUPPORT;
2206 SCTP_TCB_UNLOCK(stcb);
2207 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2212 /* Applys to the specific association */
2213 paddrp->spp_flags = 0;
2217 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2218 ovh = SCTP_MED_OVERHEAD;
2220 ovh = SCTP_MED_V4_OVERHEAD;
2224 paddrp->spp_pathmaxrxt = net->failure_threshold;
2225 paddrp->spp_pathmtu = net->mtu - ovh;
2226 /* get flags for HB */
2227 if (net->dest_state & SCTP_ADDR_NOHB)
2228 paddrp->spp_flags |= SPP_HB_DISABLE;
2230 paddrp->spp_flags |= SPP_HB_ENABLE;
2231 /* get flags for PMTU */
2232 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2233 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2235 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2238 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2239 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2240 paddrp->spp_flags |= SPP_IPV4_TOS;
2244 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2245 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2246 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2251 * No destination so return default
2256 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2257 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2259 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2260 paddrp->spp_flags |= SPP_IPV4_TOS;
2263 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2264 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2266 /* default settings should be these */
2267 if (stcb->asoc.hb_is_disabled == 0) {
2268 paddrp->spp_flags |= SPP_HB_ENABLE;
2270 paddrp->spp_flags |= SPP_HB_DISABLE;
2272 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2273 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2278 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2281 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2282 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2283 SCTP_TCB_UNLOCK(stcb);
2285 /* Use endpoint defaults */
2286 SCTP_INP_RLOCK(inp);
2287 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2288 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2289 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2290 /* get inp's default */
2292 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2293 paddrp->spp_flags |= SPP_IPV4_TOS;
2296 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2297 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2298 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2301 /* can't return this */
2302 paddrp->spp_pathmtu = 0;
2304 /* default behavior, no stcb */
2305 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2307 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2308 paddrp->spp_flags |= SPP_HB_ENABLE;
2310 paddrp->spp_flags |= SPP_HB_DISABLE;
2312 SCTP_INP_RUNLOCK(inp);
2314 *optsize = sizeof(struct sctp_paddrparams);
2317 case SCTP_GET_PEER_ADDR_INFO:
2319 struct sctp_paddrinfo *paddri;
2320 struct sctp_nets *net;
2322 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2323 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2327 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2330 * We increment here since
2331 * sctp_findassociation_ep_addr() wil do a
2332 * decrement if it finds the stcb as long as
2333 * the locked tcb (last argument) is NOT a
2336 SCTP_INP_INCR_REF(inp);
2337 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2339 SCTP_INP_DECR_REF(inp);
2343 if ((stcb) && (net)) {
2344 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2345 paddri->spinfo_cwnd = net->cwnd;
2346 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2347 paddri->spinfo_rto = net->RTO;
2348 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2349 SCTP_TCB_UNLOCK(stcb);
2352 SCTP_TCB_UNLOCK(stcb);
2354 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2357 *optsize = sizeof(struct sctp_paddrinfo);
2360 case SCTP_PCB_STATUS:
2362 struct sctp_pcbinfo *spcb;
2364 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2365 sctp_fill_pcbinfo(spcb);
2366 *optsize = sizeof(struct sctp_pcbinfo);
2372 struct sctp_nets *net;
2373 struct sctp_status *sstat;
2375 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2376 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2379 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2384 * I think passing the state is fine since
2385 * sctp_constants.h will be available to the user
2388 sstat->sstat_state = stcb->asoc.state;
2389 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2390 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2391 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2393 * We can't include chunks that have been passed to
2394 * the socket layer. Only things in queue.
2396 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2397 stcb->asoc.cnt_on_all_streams);
2400 sstat->sstat_instrms = stcb->asoc.streamincnt;
2401 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2402 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2403 memcpy(&sstat->sstat_primary.spinfo_address,
2404 &stcb->asoc.primary_destination->ro._l_addr,
2405 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2406 net = stcb->asoc.primary_destination;
2407 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2409 * Again the user can get info from sctp_constants.h
2410 * for what the state of the network is.
2412 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2413 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2414 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2415 sstat->sstat_primary.spinfo_rto = net->RTO;
2416 sstat->sstat_primary.spinfo_mtu = net->mtu;
2417 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2418 SCTP_TCB_UNLOCK(stcb);
2419 *optsize = sizeof(*sstat);
2424 struct sctp_rtoinfo *srto;
2426 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2427 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2430 srto->srto_initial = stcb->asoc.initial_rto;
2431 srto->srto_max = stcb->asoc.maxrto;
2432 srto->srto_min = stcb->asoc.minrto;
2433 SCTP_TCB_UNLOCK(stcb);
2435 SCTP_INP_RLOCK(inp);
2436 srto->srto_initial = inp->sctp_ep.initial_rto;
2437 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2438 srto->srto_min = inp->sctp_ep.sctp_minrto;
2439 SCTP_INP_RUNLOCK(inp);
2441 *optsize = sizeof(*srto);
2444 case SCTP_ASSOCINFO:
2446 struct sctp_assocparams *sasoc;
2449 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2450 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2453 oldval = sasoc->sasoc_cookie_life;
2454 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2455 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2456 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2457 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2458 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2459 SCTP_TCB_UNLOCK(stcb);
2461 SCTP_INP_RLOCK(inp);
2462 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2463 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2464 sasoc->sasoc_number_peer_destinations = 0;
2465 sasoc->sasoc_peer_rwnd = 0;
2466 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2467 SCTP_INP_RUNLOCK(inp);
2469 *optsize = sizeof(*sasoc);
2472 case SCTP_DEFAULT_SEND_PARAM:
2474 struct sctp_sndrcvinfo *s_info;
2476 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2477 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2480 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2481 SCTP_TCB_UNLOCK(stcb);
2483 SCTP_INP_RLOCK(inp);
2484 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2485 SCTP_INP_RUNLOCK(inp);
2487 *optsize = sizeof(*s_info);
2492 struct sctp_initmsg *sinit;
2494 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2495 SCTP_INP_RLOCK(inp);
2496 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2497 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2498 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2499 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2500 SCTP_INP_RUNLOCK(inp);
2501 *optsize = sizeof(*sinit);
2504 case SCTP_PRIMARY_ADDR:
2505 /* we allow a "get" operation on this */
2507 struct sctp_setprim *ssp;
2509 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2510 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2513 /* simply copy out the sockaddr_storage... */
2517 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2518 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2520 memcpy(&ssp->ssp_addr,
2521 &stcb->asoc.primary_destination->ro._l_addr,
2523 SCTP_TCB_UNLOCK(stcb);
2525 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2528 *optsize = sizeof(*ssp);
2532 case SCTP_HMAC_IDENT:
2534 struct sctp_hmacalgo *shmac;
2535 sctp_hmaclist_t *hmaclist;
2539 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2541 SCTP_INP_RLOCK(inp);
2542 hmaclist = inp->sctp_ep.local_hmacs;
2543 if (hmaclist == NULL) {
2544 /* no HMACs to return */
2545 *optsize = sizeof(*shmac);
2546 SCTP_INP_RUNLOCK(inp);
2549 /* is there room for all of the hmac ids? */
2550 size = sizeof(*shmac) + (hmaclist->num_algo *
2551 sizeof(shmac->shmac_idents[0]));
2552 if ((size_t)(*optsize) < size) {
2553 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2555 SCTP_INP_RUNLOCK(inp);
2558 /* copy in the list */
2559 shmac->shmac_number_of_idents = hmaclist->num_algo;
2560 for (i = 0; i < hmaclist->num_algo; i++) {
2561 shmac->shmac_idents[i] = hmaclist->hmac[i];
2563 SCTP_INP_RUNLOCK(inp);
2567 case SCTP_AUTH_ACTIVE_KEY:
2569 struct sctp_authkeyid *scact;
2571 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2572 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2575 /* get the active key on the assoc */
2576 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2577 SCTP_TCB_UNLOCK(stcb);
2579 /* get the endpoint active key */
2580 SCTP_INP_RLOCK(inp);
2581 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2582 SCTP_INP_RUNLOCK(inp);
2584 *optsize = sizeof(*scact);
2587 case SCTP_LOCAL_AUTH_CHUNKS:
2589 struct sctp_authchunks *sac;
2590 sctp_auth_chklist_t *chklist = NULL;
2593 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2594 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2597 /* get off the assoc */
2598 chklist = stcb->asoc.local_auth_chunks;
2599 /* is there enough space? */
2600 size = sctp_auth_get_chklist_size(chklist);
2601 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2603 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2605 /* copy in the chunks */
2606 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2608 SCTP_TCB_UNLOCK(stcb);
2610 /* get off the endpoint */
2611 SCTP_INP_RLOCK(inp);
2612 chklist = inp->sctp_ep.local_auth_chunks;
2613 /* is there enough space? */
2614 size = sctp_auth_get_chklist_size(chklist);
2615 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2617 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2619 /* copy in the chunks */
2620 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2622 SCTP_INP_RUNLOCK(inp);
2624 *optsize = sizeof(struct sctp_authchunks) + size;
2627 case SCTP_PEER_AUTH_CHUNKS:
2629 struct sctp_authchunks *sac;
2630 sctp_auth_chklist_t *chklist = NULL;
2633 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2634 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2637 /* get off the assoc */
2638 chklist = stcb->asoc.peer_auth_chunks;
2639 /* is there enough space? */
2640 size = sctp_auth_get_chklist_size(chklist);
2641 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2643 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2645 /* copy in the chunks */
2646 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2648 SCTP_TCB_UNLOCK(stcb);
2650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2653 *optsize = sizeof(struct sctp_authchunks) + size;
2659 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2660 error = ENOPROTOOPT;
2663 } /* end switch (sopt->sopt_name) */
2668 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2673 struct sctp_tcb *stcb = NULL;
2674 struct sctp_inpcb *inp = NULL;
2677 if (optval == NULL) {
2678 SCTP_PRINTF("optval is NULL\n");
2679 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2682 inp = (struct sctp_inpcb *)so->so_pcb;
2684 SCTP_PRINTF("inp is NULL?\n");
2685 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2688 vrf_id = inp->def_vrf_id;
2693 case SCTP_AUTOCLOSE:
2694 case SCTP_AUTO_ASCONF:
2695 case SCTP_EXPLICIT_EOR:
2696 case SCTP_DISABLE_FRAGMENTS:
2697 case SCTP_USE_EXT_RCVINFO:
2698 case SCTP_I_WANT_MAPPED_V4_ADDR:
2699 /* copy in the option value */
2700 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2705 case SCTP_DISABLE_FRAGMENTS:
2706 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2708 case SCTP_AUTO_ASCONF:
2710 * NOTE: we don't really support this flag
2712 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2713 /* only valid for bound all sockets */
2714 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2716 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2720 case SCTP_EXPLICIT_EOR:
2721 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2723 case SCTP_USE_EXT_RCVINFO:
2724 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2726 case SCTP_I_WANT_MAPPED_V4_ADDR:
2727 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2728 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2735 set_opt = SCTP_PCB_FLAGS_NODELAY;
2737 case SCTP_AUTOCLOSE:
2738 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2739 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2740 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2743 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2745 * The value is in ticks. Note this does not effect
2746 * old associations, only new ones.
2748 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2751 SCTP_INP_WLOCK(inp);
2753 sctp_feature_on(inp, set_opt);
2755 sctp_feature_off(inp, set_opt);
2757 SCTP_INP_WUNLOCK(inp);
2759 case SCTP_REUSE_PORT:
2761 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2762 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2763 /* Can't set it after we are bound */
2767 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2768 /* Can't do this for a 1-m socket */
2773 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2775 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2778 case SCTP_PARTIAL_DELIVERY_POINT:
2782 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2783 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2784 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2788 inp->partial_delivery_point = *value;
2791 case SCTP_FRAGMENT_INTERLEAVE:
2792 /* not yet until we re-write sctp_recvmsg() */
2796 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2797 if (*level == SCTP_FRAG_LEVEL_2) {
2798 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2799 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2800 } else if (*level == SCTP_FRAG_LEVEL_1) {
2801 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2802 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2803 } else if (*level == SCTP_FRAG_LEVEL_0) {
2804 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2805 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2808 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2813 case SCTP_CMT_ON_OFF:
2815 struct sctp_assoc_value *av;
2817 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2818 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2819 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2821 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2822 SCTP_TCB_UNLOCK(stcb);
2824 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2828 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2829 error = ENOPROTOOPT;
2833 /* EY nr_sack_on_off socket option */
2834 case SCTP_NR_SACK_ON_OFF:
2836 struct sctp_assoc_value *av;
2838 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2839 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
2840 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2842 stcb->asoc.sctp_nr_sack_on_off = (uint8_t) av->assoc_value;
2843 SCTP_TCB_UNLOCK(stcb);
2845 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2849 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2850 error = ENOPROTOOPT;
2854 /* JRS - Set socket option for pluggable congestion control */
2855 case SCTP_PLUGGABLE_CC:
2857 struct sctp_assoc_value *av;
2859 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2860 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2862 switch (av->assoc_value) {
2864 * JRS - Standard TCP congestion
2867 case SCTP_CC_RFC2581:
2869 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2870 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2871 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2872 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2873 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2874 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2875 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2876 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2877 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2878 SCTP_TCB_UNLOCK(stcb);
2882 * JRS - High Speed TCP congestion
2887 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2888 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2889 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2890 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2891 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2892 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2893 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2894 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2895 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2896 SCTP_TCB_UNLOCK(stcb);
2899 /* JRS - HTCP congestion control */
2902 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2903 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2904 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2905 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2906 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2907 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2908 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2909 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2910 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2911 SCTP_TCB_UNLOCK(stcb);
2915 * JRS - All other values are
2920 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2922 SCTP_TCB_UNLOCK(stcb);
2927 switch (av->assoc_value) {
2928 case SCTP_CC_RFC2581:
2931 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2934 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2941 case SCTP_CLR_STAT_LOG:
2942 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2947 struct sctp_assoc_value *av;
2949 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2950 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2953 stcb->asoc.context = av->assoc_value;
2954 SCTP_TCB_UNLOCK(stcb);
2956 SCTP_INP_WLOCK(inp);
2957 inp->sctp_context = av->assoc_value;
2958 SCTP_INP_WUNLOCK(inp);
2964 uint32_t *default_vrfid;
2966 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2967 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2968 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2972 inp->def_vrf_id = *default_vrfid;
2975 case SCTP_DEL_VRF_ID:
2977 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2981 case SCTP_ADD_VRF_ID:
2983 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2987 case SCTP_DELAYED_SACK:
2989 struct sctp_sack_info *sack;
2991 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2992 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2993 if (sack->sack_delay) {
2994 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2995 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2998 if (sack->sack_delay) {
2999 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3000 sack->sack_delay = TICKS_TO_MSEC(1);
3002 stcb->asoc.delayed_ack = sack->sack_delay;
3004 if (sack->sack_freq) {
3005 stcb->asoc.sack_freq = sack->sack_freq;
3007 SCTP_TCB_UNLOCK(stcb);
3009 SCTP_INP_WLOCK(inp);
3010 if (sack->sack_delay) {
3011 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3012 sack->sack_delay = TICKS_TO_MSEC(1);
3014 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3016 if (sack->sack_freq) {
3017 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3019 SCTP_INP_WUNLOCK(inp);
3023 case SCTP_AUTH_CHUNK:
3025 struct sctp_authchunk *sauth;
3027 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3029 SCTP_INP_WLOCK(inp);
3030 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3031 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3034 SCTP_INP_WUNLOCK(inp);
3039 struct sctp_authkey *sca;
3040 struct sctp_keyhead *shared_keys;
3041 sctp_sharedkey_t *shared_key;
3042 sctp_key_t *key = NULL;
3045 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3046 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3047 size = optsize - sizeof(*sca);
3050 /* set it on the assoc */
3051 shared_keys = &stcb->asoc.shared_keys;
3052 /* clear the cached keys for this key id */
3053 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3055 * create the new shared key and
3059 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3061 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3063 SCTP_TCB_UNLOCK(stcb);
3067 shared_key = sctp_alloc_sharedkey();
3068 if (shared_key == NULL) {
3070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3072 SCTP_TCB_UNLOCK(stcb);
3075 shared_key->key = key;
3076 shared_key->keyid = sca->sca_keynumber;
3077 error = sctp_insert_sharedkey(shared_keys, shared_key);
3078 SCTP_TCB_UNLOCK(stcb);
3080 /* set it on the endpoint */
3081 SCTP_INP_WLOCK(inp);
3082 shared_keys = &inp->sctp_ep.shared_keys;
3084 * clear the cached keys on all assocs for
3087 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3089 * create the new shared key and
3093 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3095 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3097 SCTP_INP_WUNLOCK(inp);
3101 shared_key = sctp_alloc_sharedkey();
3102 if (shared_key == NULL) {
3104 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3106 SCTP_INP_WUNLOCK(inp);
3109 shared_key->key = key;
3110 shared_key->keyid = sca->sca_keynumber;
3111 error = sctp_insert_sharedkey(shared_keys, shared_key);
3112 SCTP_INP_WUNLOCK(inp);
3116 case SCTP_HMAC_IDENT:
3118 struct sctp_hmacalgo *shmac;
3119 sctp_hmaclist_t *hmaclist;
3125 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3126 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3127 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3131 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3132 if (hmaclist == NULL) {
3133 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3137 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3138 hmacid = shmac->shmac_idents[i];
3139 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3140 /* invalid HMACs were found */ ;
3141 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3143 sctp_free_hmaclist(hmaclist);
3144 goto sctp_set_hmac_done;
3148 for (i = 0; i < hmaclist->num_algo; i++) {
3149 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3150 /* already in list */
3155 sctp_free_hmaclist(hmaclist);
3156 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3160 /* set it on the endpoint */
3161 SCTP_INP_WLOCK(inp);
3162 if (inp->sctp_ep.local_hmacs)
3163 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3164 inp->sctp_ep.local_hmacs = hmaclist;
3165 SCTP_INP_WUNLOCK(inp);
3169 case SCTP_AUTH_ACTIVE_KEY:
3171 struct sctp_authkeyid *scact;
3173 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3175 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3177 /* set the active key on the right place */
3179 /* set the active key on the assoc */
3180 if (sctp_auth_setactivekey(stcb,
3181 scact->scact_keynumber)) {
3182 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3183 SCTP_FROM_SCTP_USRREQ,
3187 SCTP_TCB_UNLOCK(stcb);
3189 /* set the active key on the endpoint */
3190 SCTP_INP_WLOCK(inp);
3191 if (sctp_auth_setactivekey_ep(inp,
3192 scact->scact_keynumber)) {
3193 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3194 SCTP_FROM_SCTP_USRREQ,
3198 SCTP_INP_WUNLOCK(inp);
3202 case SCTP_AUTH_DELETE_KEY:
3204 struct sctp_authkeyid *scdel;
3206 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3208 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3210 /* delete the key from the right place */
3212 if (sctp_delete_sharedkey(stcb,
3213 scdel->scact_keynumber)) {
3214 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3215 SCTP_FROM_SCTP_USRREQ,
3219 SCTP_TCB_UNLOCK(stcb);
3221 SCTP_INP_WLOCK(inp);
3222 if (sctp_delete_sharedkey_ep(inp,
3223 scdel->scact_keynumber)) {
3224 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3225 SCTP_FROM_SCTP_USRREQ,
3229 SCTP_INP_WUNLOCK(inp);
3233 case SCTP_AUTH_DEACTIVATE_KEY:
3235 struct sctp_authkeyid *keyid;
3237 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3239 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3241 /* deactivate the key from the right place */
3243 if (sctp_deact_sharedkey(stcb,
3244 keyid->scact_keynumber)) {
3245 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3246 SCTP_FROM_SCTP_USRREQ,
3250 SCTP_TCB_UNLOCK(stcb);
3252 SCTP_INP_WLOCK(inp);
3253 if (sctp_deact_sharedkey_ep(inp,
3254 keyid->scact_keynumber)) {
3255 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3256 SCTP_FROM_SCTP_USRREQ,
3260 SCTP_INP_WUNLOCK(inp);
3265 case SCTP_RESET_STREAMS:
3267 struct sctp_stream_reset *strrst;
3268 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3270 uint16_t addstrmcnt = 0;
3273 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3274 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3277 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3281 if (stcb->asoc.peer_supports_strreset == 0) {
3283 * Peer does not support it, we return
3284 * protocol not supported since this is true
3285 * for this feature and this peer, not the
3286 * socket request in general.
3288 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3289 error = EPROTONOSUPPORT;
3290 SCTP_TCB_UNLOCK(stcb);
3293 if (stcb->asoc.stream_reset_outstanding) {
3294 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3296 SCTP_TCB_UNLOCK(stcb);
3299 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3301 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3303 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3306 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3308 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3312 /* We can't do that and add streams */
3316 if (stcb->asoc.stream_reset_outstanding) {
3321 /* We allocate here */
3322 addstrmcnt = strrst->strrst_num_streams;
3323 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3324 /* You can't have more than 64k */
3328 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3329 /* Need to allocate more */
3330 struct sctp_stream_out *oldstream;
3331 struct sctp_stream_queue_pending *sp;
3334 oldstream = stcb->asoc.strmout;
3336 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3337 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3339 if (stcb->asoc.strmout == NULL) {
3340 stcb->asoc.strmout = oldstream;
3345 * Ok now we proceed with copying
3346 * the old out stuff and
3347 * initializing the new stuff.
3349 SCTP_TCB_SEND_LOCK(stcb);
3350 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3351 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3352 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3353 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3354 stcb->asoc.strmout[i].stream_no = i;
3355 if (oldstream[i].next_spoke.tqe_next) {
3356 sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3357 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3358 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3361 /* not on out wheel */
3362 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3363 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3367 * now anything on those
3370 while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
3371 sp = TAILQ_FIRST(&oldstream[i].outqueue);
3372 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3373 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3375 /* Did we disrupt the wheel? */
3377 sctp_insert_on_wheel(stcb,
3379 &stcb->asoc.strmout[i],
3383 * Now move assoc pointers
3386 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3387 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3389 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3390 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3393 /* now the new streams */
3394 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3395 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3396 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3397 stcb->asoc.strmout[i].stream_no = i;
3398 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3399 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3400 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3402 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3403 SCTP_FREE(oldstream, SCTP_M_STRMO);
3405 SCTP_TCB_SEND_UNLOCK(stcb);
3408 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3410 SCTP_TCB_UNLOCK(stcb);
3413 for (i = 0; i < strrst->strrst_num_streams; i++) {
3416 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3417 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3422 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3423 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3431 SCTP_TCB_UNLOCK(stcb);
3434 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3435 strrst->strrst_list,
3436 send_out, (stcb->asoc.str_reset_seq_in - 3),
3437 send_in, send_tsn, addstream, addstrmcnt);
3439 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3440 SCTP_TCB_UNLOCK(stcb);
3444 case SCTP_CONNECT_X:
3445 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3446 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3450 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3453 case SCTP_CONNECT_X_DELAYED:
3454 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3455 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3459 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3462 case SCTP_CONNECT_X_COMPLETE:
3464 struct sockaddr *sa;
3465 struct sctp_nets *net;
3467 /* FIXME MT: check correct? */
3468 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3471 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3472 SCTP_INP_RLOCK(inp);
3473 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3475 SCTP_TCB_LOCK(stcb);
3476 net = sctp_findnet(stcb, sa);
3478 SCTP_INP_RUNLOCK(inp);
3481 * We increment here since
3482 * sctp_findassociation_ep_addr() wil do a
3483 * decrement if it finds the stcb as long as
3484 * the locked tcb (last argument) is NOT a
3487 SCTP_INP_INCR_REF(inp);
3488 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3490 SCTP_INP_DECR_REF(inp);
3495 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3499 if (stcb->asoc.delayed_connection == 1) {
3500 stcb->asoc.delayed_connection = 0;
3501 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3502 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3503 stcb->asoc.primary_destination,
3504 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3505 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3508 * already expired or did not use delayed
3511 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3514 SCTP_TCB_UNLOCK(stcb);
3517 case SCTP_MAX_BURST:
3521 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3523 SCTP_INP_WLOCK(inp);
3525 inp->sctp_ep.max_burst = *burst;
3527 SCTP_INP_WUNLOCK(inp);
3532 struct sctp_assoc_value *av;
3535 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3536 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3538 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3539 ovh = SCTP_MED_OVERHEAD;
3541 ovh = SCTP_MED_V4_OVERHEAD;
3544 if (av->assoc_value) {
3545 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3547 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3549 SCTP_TCB_UNLOCK(stcb);
3551 SCTP_INP_WLOCK(inp);
3553 * FIXME MT: I think this is not in tune
3556 if (av->assoc_value) {
3557 inp->sctp_frag_point = (av->assoc_value + ovh);
3559 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3561 SCTP_INP_WUNLOCK(inp);
3567 struct sctp_event_subscribe *events;
3569 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3571 SCTP_INP_WLOCK(inp);
3572 if (events->sctp_data_io_event) {
3573 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3575 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3578 if (events->sctp_association_event) {
3579 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3581 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3584 if (events->sctp_address_event) {
3585 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3587 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3590 if (events->sctp_send_failure_event) {
3591 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3593 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3596 if (events->sctp_peer_error_event) {
3597 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3599 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3602 if (events->sctp_shutdown_event) {
3603 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3605 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3608 if (events->sctp_partial_delivery_event) {
3609 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3611 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3614 if (events->sctp_adaptation_layer_event) {
3615 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3617 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3620 if (events->sctp_authentication_event) {
3621 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3623 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3626 if (events->sctp_sender_dry_event) {
3627 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3628 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3629 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3630 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3632 SCTP_TCB_LOCK(stcb);
3635 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3636 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3637 (stcb->asoc.stream_queue_cnt == 0)) {
3638 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3641 SCTP_TCB_UNLOCK(stcb);
3645 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3648 if (events->sctp_stream_reset_events) {
3649 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3651 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3653 SCTP_INP_WUNLOCK(inp);
3657 case SCTP_ADAPTATION_LAYER:
3659 struct sctp_setadaptation *adap_bits;
3661 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3662 SCTP_INP_WLOCK(inp);
3663 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3664 SCTP_INP_WUNLOCK(inp);
3668 case SCTP_SET_INITIAL_DBG_SEQ:
3672 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3673 SCTP_INP_WLOCK(inp);
3674 inp->sctp_ep.initial_sequence_debug = *vvv;
3675 SCTP_INP_WUNLOCK(inp);
3679 case SCTP_DEFAULT_SEND_PARAM:
3681 struct sctp_sndrcvinfo *s_info;
3683 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3684 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3687 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3688 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3690 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3693 SCTP_TCB_UNLOCK(stcb);
3695 SCTP_INP_WLOCK(inp);
3696 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3697 SCTP_INP_WUNLOCK(inp);
3701 case SCTP_PEER_ADDR_PARAMS:
3702 /* Applys to the specific association */
3704 struct sctp_paddrparams *paddrp;
3705 struct sctp_nets *net;
3707 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3708 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3711 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3714 * We increment here since
3715 * sctp_findassociation_ep_addr() wil do a
3716 * decrement if it finds the stcb as long as
3717 * the locked tcb (last argument) is NOT a
3720 SCTP_INP_INCR_REF(inp);
3721 stcb = sctp_findassociation_ep_addr(&inp,
3722 (struct sockaddr *)&paddrp->spp_address,
3725 SCTP_INP_DECR_REF(inp);
3728 if (stcb && (net == NULL)) {
3729 struct sockaddr *sa;
3731 sa = (struct sockaddr *)&paddrp->spp_address;
3732 if (sa->sa_family == AF_INET) {
3733 struct sockaddr_in *sin;
3735 sin = (struct sockaddr_in *)sa;
3736 if (sin->sin_addr.s_addr) {
3737 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3738 SCTP_TCB_UNLOCK(stcb);
3742 } else if (sa->sa_family == AF_INET6) {
3743 struct sockaddr_in6 *sin6;
3745 sin6 = (struct sockaddr_in6 *)sa;
3746 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3747 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3748 SCTP_TCB_UNLOCK(stcb);
3753 error = EAFNOSUPPORT;
3754 SCTP_TCB_UNLOCK(stcb);
3755 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3760 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3762 SCTP_TCB_UNLOCK(stcb);
3763 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3766 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3768 SCTP_TCB_UNLOCK(stcb);
3769 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3773 /************************TCB SPECIFIC SET ******************/
3775 * do we change the timer for HB, we run
3780 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3781 ovh = SCTP_MED_OVERHEAD;
3783 ovh = SCTP_MED_V4_OVERHEAD;
3786 if (paddrp->spp_hbinterval)
3787 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3788 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3789 stcb->asoc.heart_beat_delay = 0;
3791 /* network sets ? */
3793 /************************NET SPECIFIC SET ******************/
3794 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3796 if (sctp_send_hb(stcb, 1, net) < 0) {
3797 /* asoc destroyed */
3798 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3803 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3804 net->dest_state |= SCTP_ADDR_NOHB;
3806 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3807 net->dest_state &= ~SCTP_ADDR_NOHB;
3809 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3810 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3811 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3812 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3814 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3815 net->mtu = paddrp->spp_pathmtu + ovh;
3816 if (net->mtu < stcb->asoc.smallest_mtu) {
3817 #ifdef SCTP_PRINT_FOR_B_AND_M
3818 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3821 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3825 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3826 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3827 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3830 if (paddrp->spp_pathmaxrxt)
3831 net->failure_threshold = paddrp->spp_pathmaxrxt;
3833 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3834 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3835 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3840 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3841 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3842 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3847 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3848 if (paddrp->spp_pathmaxrxt)
3849 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3851 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3852 /* Turn back on the timer */
3853 stcb->asoc.hb_is_disabled = 0;
3854 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3856 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3857 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3858 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3859 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3860 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3862 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3863 net->mtu = paddrp->spp_pathmtu + ovh;
3864 if (net->mtu < stcb->asoc.smallest_mtu) {
3865 #ifdef SCTP_PRINT_FOR_B_AND_M
3866 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3869 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3874 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3875 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3876 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3877 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3881 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3882 int cnt_of_unconf = 0;
3883 struct sctp_nets *lnet;
3885 stcb->asoc.hb_is_disabled = 1;
3886 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3887 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3892 * stop the timer ONLY if we
3893 * have no unconfirmed
3896 if (cnt_of_unconf == 0) {
3897 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3898 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3899 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3903 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3904 /* start up the timer. */
3905 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3906 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3910 if (paddrp->spp_flags & SPP_IPV4_TOS)
3911 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3914 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3915 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3919 SCTP_TCB_UNLOCK(stcb);
3921 /************************NO TCB, SET TO default stuff ******************/
3922 SCTP_INP_WLOCK(inp);
3924 * For the TOS/FLOWLABEL stuff you set it
3925 * with the options on the socket
3927 if (paddrp->spp_pathmaxrxt) {
3928 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3930 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3931 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3932 else if (paddrp->spp_hbinterval) {
3933 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3934 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3935 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3937 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3938 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3940 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3941 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3943 SCTP_INP_WUNLOCK(inp);
3949 struct sctp_rtoinfo *srto;
3950 uint32_t new_init, new_min, new_max;
3952 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3953 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3956 if (srto->srto_initial)
3957 new_init = srto->srto_initial;
3959 new_init = stcb->asoc.initial_rto;
3961 new_max = srto->srto_max;
3963 new_max = stcb->asoc.maxrto;
3965 new_min = srto->srto_min;
3967 new_min = stcb->asoc.minrto;
3968 if ((new_min <= new_init) && (new_init <= new_max)) {
3969 stcb->asoc.initial_rto = new_init;
3970 stcb->asoc.maxrto = new_max;
3971 stcb->asoc.minrto = new_min;
3973 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3976 SCTP_TCB_UNLOCK(stcb);
3978 SCTP_INP_WLOCK(inp);
3979 if (srto->srto_initial)
3980 new_init = srto->srto_initial;
3982 new_init = inp->sctp_ep.initial_rto;
3984 new_max = srto->srto_max;
3986 new_max = inp->sctp_ep.sctp_maxrto;
3988 new_min = srto->srto_min;
3990 new_min = inp->sctp_ep.sctp_minrto;
3991 if ((new_min <= new_init) && (new_init <= new_max)) {
3992 inp->sctp_ep.initial_rto = new_init;
3993 inp->sctp_ep.sctp_maxrto = new_max;
3994 inp->sctp_ep.sctp_minrto = new_min;
3996 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3999 SCTP_INP_WUNLOCK(inp);
4003 case SCTP_ASSOCINFO:
4005 struct sctp_assocparams *sasoc;
4007 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4008 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4009 if (sasoc->sasoc_cookie_life) {
4010 /* boundary check the cookie life */
4011 if (sasoc->sasoc_cookie_life < 1000)
4012 sasoc->sasoc_cookie_life = 1000;
4013 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4014 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4018 if (sasoc->sasoc_asocmaxrxt)
4019 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4020 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4021 sasoc->sasoc_peer_rwnd = 0;
4022 sasoc->sasoc_local_rwnd = 0;
4023 if (sasoc->sasoc_cookie_life) {
4024 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4026 SCTP_TCB_UNLOCK(stcb);
4028 SCTP_INP_WLOCK(inp);
4029 if (sasoc->sasoc_asocmaxrxt)
4030 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4031 sasoc->sasoc_number_peer_destinations = 0;
4032 sasoc->sasoc_peer_rwnd = 0;
4033 sasoc->sasoc_local_rwnd = 0;
4034 if (sasoc->sasoc_cookie_life) {
4035 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4037 SCTP_INP_WUNLOCK(inp);
4043 struct sctp_initmsg *sinit;
4045 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4046 SCTP_INP_WLOCK(inp);
4047 if (sinit->sinit_num_ostreams)
4048 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4050 if (sinit->sinit_max_instreams)
4051 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4053 if (sinit->sinit_max_attempts)
4054 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4056 if (sinit->sinit_max_init_timeo)
4057 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4058 SCTP_INP_WUNLOCK(inp);
4061 case SCTP_PRIMARY_ADDR:
4063 struct sctp_setprim *spa;
4064 struct sctp_nets *net, *lnet;
4066 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4067 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4071 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4074 * We increment here since
4075 * sctp_findassociation_ep_addr() wil do a
4076 * decrement if it finds the stcb as long as
4077 * the locked tcb (last argument) is NOT a
4080 SCTP_INP_INCR_REF(inp);
4081 stcb = sctp_findassociation_ep_addr(&inp,
4082 (struct sockaddr *)&spa->ssp_addr,
4085 SCTP_INP_DECR_REF(inp);
4089 if ((stcb) && (net)) {
4090 if ((net != stcb->asoc.primary_destination) &&
4091 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4092 /* Ok we need to set it */
4093 lnet = stcb->asoc.primary_destination;
4094 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4095 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4096 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4098 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4102 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4106 SCTP_TCB_UNLOCK(stcb);
4110 case SCTP_SET_DYNAMIC_PRIMARY:
4112 union sctp_sockstore *ss;
4114 error = priv_check(curthread,
4115 PRIV_NETINET_RESERVEDPORT);
4119 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4120 /* SUPER USER CHECK? */
4121 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4124 case SCTP_SET_PEER_PRIMARY_ADDR:
4126 struct sctp_setpeerprim *sspp;
4128 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4129 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4131 struct sctp_ifa *ifa;
4133 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4134 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4136 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4140 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4142 * Must validate the ifa found is in
4145 struct sctp_laddr *laddr;
4148 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4149 if (laddr->ifa == NULL) {
4150 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4154 if (laddr->ifa == ifa) {
4160 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4165 if (sctp_set_primary_ip_address_sa(stcb,
4166 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4167 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4171 SCTP_TCB_UNLOCK(stcb);
4173 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4179 case SCTP_BINDX_ADD_ADDR:
4181 struct sctp_getaddresses *addrs;
4185 td = (struct thread *)p;
4186 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4188 if (addrs->addr->sa_family == AF_INET) {
4189 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4191 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4195 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4196 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4200 } else if (addrs->addr->sa_family == AF_INET6) {
4201 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4203 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4207 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4208 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4209 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4214 error = EAFNOSUPPORT;
4217 sctp_bindx_add_address(so, inp, addrs->addr,
4218 addrs->sget_assoc_id, vrf_id,
4222 case SCTP_BINDX_REM_ADDR:
4224 struct sctp_getaddresses *addrs;
4228 td = (struct thread *)p;
4230 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4231 if (addrs->addr->sa_family == AF_INET) {
4232 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4234 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4238 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4239 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4243 } else if (addrs->addr->sa_family == AF_INET6) {
4244 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4246 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4250 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4251 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4252 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4257 error = EAFNOSUPPORT;
4260 sctp_bindx_delete_address(so, inp, addrs->addr,
4261 addrs->sget_assoc_id, vrf_id,
4266 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4267 error = ENOPROTOOPT;
4269 } /* end switch (opt) */
4274 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4276 void *optval = NULL;
4278 struct sctp_inpcb *inp;
4282 inp = (struct sctp_inpcb *)so->so_pcb;
4284 /* I made the same as TCP since we are not setup? */
4285 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4286 return (ECONNRESET);
4288 if (sopt->sopt_level != IPPROTO_SCTP) {
4289 /* wrong proto level... send back up to IP */
4291 if (INP_CHECK_SOCKAF(so, AF_INET6))
4292 error = ip6_ctloutput(so, sopt);
4295 error = ip_ctloutput(so, sopt);
4298 optsize = sopt->sopt_valsize;
4300 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4301 if (optval == NULL) {
4302 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4305 error = sooptcopyin(sopt, optval, optsize, optsize);
4307 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4311 p = (void *)sopt->sopt_td;
4312 if (sopt->sopt_dir == SOPT_SET) {
4313 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4314 } else if (sopt->sopt_dir == SOPT_GET) {
4315 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4317 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4320 if ((error == 0) && (optval != NULL)) {
4321 error = sooptcopyout(sopt, optval, optsize);
4322 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4323 } else if (optval != NULL) {
4324 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4332 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4335 int create_lock_on = 0;
4337 struct sctp_inpcb *inp;
4338 struct sctp_tcb *stcb = NULL;
4340 inp = (struct sctp_inpcb *)so->so_pcb;
4342 /* I made the same as TCP since we are not setup? */
4343 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4344 return (ECONNRESET);
4347 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4351 if (addr->sa_family == AF_INET6) {
4352 struct sockaddr_in6 *sin6p;
4354 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4355 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4358 sin6p = (struct sockaddr_in6 *)addr;
4359 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4360 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4365 if (addr->sa_family == AF_INET) {
4366 struct sockaddr_in *sinp;
4368 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4369 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4372 sinp = (struct sockaddr_in *)addr;
4373 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4374 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4378 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4379 return (EAFNOSUPPORT);
4381 SCTP_INP_INCR_REF(inp);
4382 SCTP_ASOC_CREATE_LOCK(inp);
4386 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4387 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4388 /* Should I really unlock ? */
4389 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4394 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4395 (addr->sa_family == AF_INET6)) {
4396 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4401 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4402 SCTP_PCB_FLAGS_UNBOUND) {
4403 /* Bind a ephemeral port */
4404 error = sctp_inpcb_bind(so, NULL, NULL, p);
4409 /* Now do we connect? */
4410 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4411 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4412 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4416 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4417 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4418 /* We are already connected AND the TCP model */
4419 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4423 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4424 SCTP_INP_RLOCK(inp);
4425 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4426 SCTP_INP_RUNLOCK(inp);
4429 * We increment here since sctp_findassociation_ep_addr()
4430 * will do a decrement if it finds the stcb as long as the
4431 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4433 SCTP_INP_INCR_REF(inp);
4434 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4436 SCTP_INP_DECR_REF(inp);
4438 SCTP_TCB_UNLOCK(stcb);
4442 /* Already have or am bring up an association */
4443 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4447 vrf_id = inp->def_vrf_id;
4448 /* We are GOOD to go */
4449 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4451 /* Gak! no memory */
4454 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4455 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4456 /* Set the connected flag so we can queue data */
4459 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4460 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4462 /* initialize authentication parameters for the assoc */
4463 sctp_initialize_auth_params(inp, stcb);
4465 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4466 SCTP_TCB_UNLOCK(stcb);
4468 if (create_lock_on) {
4469 SCTP_ASOC_CREATE_UNLOCK(inp);
4471 SCTP_INP_DECR_REF(inp);
4476 sctp_listen(struct socket *so, int backlog, struct thread *p)
4479 * Note this module depends on the protocol processing being called
4480 * AFTER any socket level flags and backlog are applied to the
4481 * socket. The traditional way that the socket flags are applied is
4482 * AFTER protocol processing. We have made a change to the
4483 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4484 * place if the socket API for SCTP is to work properly.
4488 struct sctp_inpcb *inp;
4490 inp = (struct sctp_inpcb *)so->so_pcb;
4492 /* I made the same as TCP since we are not setup? */
4493 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4494 return (ECONNRESET);
4496 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4497 /* See if we have a listener */
4498 struct sctp_inpcb *tinp;
4499 union sctp_sockstore store, *sp;
4502 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4504 struct sctp_laddr *laddr;
4506 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4507 memcpy(&store, &laddr->ifa->address, sizeof(store));
4508 sp->sin.sin_port = inp->sctp_lport;
4509 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4510 if (tinp && (tinp != inp) &&
4511 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4512 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4513 (tinp->sctp_socket->so_qlimit)) {
4515 * we have a listener already and
4518 SCTP_INP_DECR_REF(tinp);
4519 return (EADDRINUSE);
4521 SCTP_INP_DECR_REF(tinp);
4525 /* Setup a local addr bound all */
4526 memset(&store, 0, sizeof(store));
4527 store.sin.sin_port = inp->sctp_lport;
4529 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4530 store.sa.sa_family = AF_INET6;
4531 store.sa.sa_len = sizeof(struct sockaddr_in6);
4534 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4535 store.sa.sa_family = AF_INET;
4536 store.sa.sa_len = sizeof(struct sockaddr_in);
4538 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4539 if (tinp && (tinp != inp) &&
4540 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4541 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4542 (tinp->sctp_socket->so_qlimit)) {
4544 * we have a listener already and its not
4547 SCTP_INP_DECR_REF(tinp);
4548 return (EADDRINUSE);
4550 SCTP_INP_DECR_REF(inp);
4554 SCTP_INP_RLOCK(inp);
4555 #ifdef SCTP_LOCK_LOGGING
4556 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4557 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4561 error = solisten_proto_check(so);
4564 SCTP_INP_RUNLOCK(inp);
4567 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4568 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4570 * The unlucky case - We are in the tcp pool with this guy.
4571 * - Someone else is in the main inp slot. - We must move
4572 * this guy (the listener) to the main slot - We must then
4573 * move the guy that was listener to the TCP Pool.
4575 if (sctp_swap_inpcb_for_listen(inp)) {
4579 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4580 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4581 /* We are already connected AND the TCP model */
4583 SCTP_INP_RUNLOCK(inp);
4585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4586 return (EADDRINUSE);
4588 SCTP_INP_RUNLOCK(inp);
4589 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4590 /* We must do a bind. */
4592 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4593 /* bind error, probably perm */
4598 /* It appears for 7.0 and on, we must always call this. */
4599 solisten_proto(so, backlog);
4600 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4601 /* remove the ACCEPTCONN flag for one-to-many sockets */
4602 so->so_options &= ~SO_ACCEPTCONN;
4605 /* turning off listen */
4606 so->so_options &= ~SO_ACCEPTCONN;
4612 static int sctp_defered_wakeup_cnt = 0;
4615 sctp_accept(struct socket *so, struct sockaddr **addr)
4617 struct sctp_tcb *stcb;
4618 struct sctp_inpcb *inp;
4619 union sctp_sockstore store;
4625 inp = (struct sctp_inpcb *)so->so_pcb;
4628 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4629 return (ECONNRESET);
4631 SCTP_INP_RLOCK(inp);
4632 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4633 SCTP_INP_RUNLOCK(inp);
4634 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4635 return (EOPNOTSUPP);
4637 if (so->so_state & SS_ISDISCONNECTED) {
4638 SCTP_INP_RUNLOCK(inp);
4639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4640 return (ECONNABORTED);
4642 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4644 SCTP_INP_RUNLOCK(inp);
4645 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4646 return (ECONNRESET);
4648 SCTP_TCB_LOCK(stcb);
4649 SCTP_INP_RUNLOCK(inp);
4650 store = stcb->asoc.primary_destination->ro._l_addr;
4651 SCTP_TCB_UNLOCK(stcb);
4652 switch (store.sa.sa_family) {
4655 struct sockaddr_in *sin;
4657 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4658 sin->sin_family = AF_INET;
4659 sin->sin_len = sizeof(*sin);
4660 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4661 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4662 *addr = (struct sockaddr *)sin;
4668 struct sockaddr_in6 *sin6;
4670 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4671 sin6->sin6_family = AF_INET6;
4672 sin6->sin6_len = sizeof(*sin6);
4673 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4675 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4676 if ((error = sa6_recoverscope(sin6)) != 0) {
4677 SCTP_FREE_SONAME(sin6);
4680 *addr = (struct sockaddr *)sin6;
4688 /* Wake any delayed sleep action */
4689 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4690 SCTP_INP_WLOCK(inp);
4691 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4692 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4693 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4694 SCTP_INP_WUNLOCK(inp);
4695 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4696 if (sowriteable(inp->sctp_socket)) {
4697 sowwakeup_locked(inp->sctp_socket);
4699 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4701 SCTP_INP_WLOCK(inp);
4703 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4704 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4705 SCTP_INP_WUNLOCK(inp);
4706 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4707 if (soreadable(inp->sctp_socket)) {
4708 sctp_defered_wakeup_cnt++;
4709 sorwakeup_locked(inp->sctp_socket);
4711 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4713 SCTP_INP_WLOCK(inp);
4715 SCTP_INP_WUNLOCK(inp);
4721 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4723 struct sockaddr_in *sin;
4725 struct sctp_inpcb *inp;
4726 struct sctp_ifa *sctp_ifa;
4729 * Do the malloc first in case it blocks.
4731 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4732 sin->sin_family = AF_INET;
4733 sin->sin_len = sizeof(*sin);
4734 inp = (struct sctp_inpcb *)so->so_pcb;
4736 SCTP_FREE_SONAME(sin);
4737 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4740 SCTP_INP_RLOCK(inp);
4741 sin->sin_port = inp->sctp_lport;
4742 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4743 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4744 struct sctp_tcb *stcb;
4745 struct sockaddr_in *sin_a;
4746 struct sctp_nets *net;
4749 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4755 SCTP_TCB_LOCK(stcb);
4756 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4757 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4759 /* this will make coverity happy */
4762 if (sin_a->sin_family == AF_INET) {
4767 if ((!fnd) || (sin_a == NULL)) {
4769 SCTP_TCB_UNLOCK(stcb);
4772 vrf_id = inp->def_vrf_id;
4773 sctp_ifa = sctp_source_address_selection(inp,
4775 (sctp_route_t *) & net->ro,
4778 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4779 sctp_free_ifa(sctp_ifa);
4781 SCTP_TCB_UNLOCK(stcb);
4783 /* For the bound all case you get back 0 */
4785 sin->sin_addr.s_addr = 0;
4789 /* Take the first IPv4 address in the list */
4790 struct sctp_laddr *laddr;
4793 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4794 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4795 struct sockaddr_in *sin_a;
4797 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4798 sin->sin_addr = sin_a->sin_addr;
4804 SCTP_FREE_SONAME(sin);
4805 SCTP_INP_RUNLOCK(inp);
4806 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4810 SCTP_INP_RUNLOCK(inp);
4811 (*addr) = (struct sockaddr *)sin;
4816 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4818 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4820 struct sockaddr_in *sin_a;
4821 struct sctp_inpcb *inp;
4822 struct sctp_tcb *stcb;
4823 struct sctp_nets *net;
4825 /* Do the malloc first in case it blocks. */
4826 inp = (struct sctp_inpcb *)so->so_pcb;
4827 if ((inp == NULL) ||
4828 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4829 /* UDP type and listeners will drop out here */
4830 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4833 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4834 sin->sin_family = AF_INET;
4835 sin->sin_len = sizeof(*sin);
4837 /* We must recapture incase we blocked */
4838 inp = (struct sctp_inpcb *)so->so_pcb;
4840 SCTP_FREE_SONAME(sin);
4841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4844 SCTP_INP_RLOCK(inp);
4845 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4847 SCTP_TCB_LOCK(stcb);
4849 SCTP_INP_RUNLOCK(inp);
4851 SCTP_FREE_SONAME(sin);
4852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4856 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4857 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4858 if (sin_a->sin_family == AF_INET) {
4860 sin->sin_port = stcb->rport;
4861 sin->sin_addr = sin_a->sin_addr;
4865 SCTP_TCB_UNLOCK(stcb);
4867 /* No IPv4 address */
4868 SCTP_FREE_SONAME(sin);
4869 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4872 (*addr) = (struct sockaddr *)sin;
4876 struct pr_usrreqs sctp_usrreqs = {
4877 .pru_abort = sctp_abort,
4878 .pru_accept = sctp_accept,
4879 .pru_attach = sctp_attach,
4880 .pru_bind = sctp_bind,
4881 .pru_connect = sctp_connect,
4882 .pru_control = in_control,
4883 .pru_close = sctp_close,
4884 .pru_detach = sctp_close,
4885 .pru_sopoll = sopoll_generic,
4886 .pru_flush = sctp_flush,
4887 .pru_disconnect = sctp_disconnect,
4888 .pru_listen = sctp_listen,
4889 .pru_peeraddr = sctp_peeraddr,
4890 .pru_send = sctp_sendm,
4891 .pru_shutdown = sctp_shutdown,
4892 .pru_sockaddr = sctp_ingetaddr,
4893 .pru_sosend = sctp_sosend,
4894 .pru_soreceive = sctp_soreceive