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/udp.h>
55 extern struct sctp_cc_functions sctp_cc_functions[];
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;
111 /* Adjust that too */
112 stcb->asoc.smallest_mtu = nxtsz;
113 /* now off to subtract IP_DF flag if needed */
114 overhead = IP_HDR_SIZE;
115 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
116 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
118 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
119 if ((chk->send_size + overhead) > nxtsz) {
120 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
123 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
124 if ((chk->send_size + overhead) > nxtsz) {
126 * For this guy we also mark for immediate resend
127 * since we sent to big of chunk
129 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
130 if (chk->sent < SCTP_DATAGRAM_RESEND) {
131 sctp_flight_size_decrease(chk);
132 sctp_total_flight_decrease(stcb, chk);
134 if (chk->sent != SCTP_DATAGRAM_RESEND) {
135 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
137 chk->sent = SCTP_DATAGRAM_RESEND;
138 chk->rec.data.doing_fast_retransmit = 0;
139 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
140 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
141 chk->whoTo->flight_size,
143 (uintptr_t) chk->whoTo,
144 chk->rec.data.TSN_seq);
146 /* Clear any time so NO RTT is being done */
153 sctp_notify_mbuf(struct sctp_inpcb *inp,
154 struct sctp_tcb *stcb,
155 struct sctp_nets *net,
160 int totsz, tmr_stopped = 0;
164 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
165 (ip == NULL) || (sh == NULL)) {
167 SCTP_TCB_UNLOCK(stcb);
171 /* First job is to verify the vtag matches what I would send */
172 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
173 SCTP_TCB_UNLOCK(stcb);
176 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
178 if (icmph->icmp_type != ICMP_UNREACH) {
179 /* We only care about unreachable */
180 SCTP_TCB_UNLOCK(stcb);
183 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
184 /* not a unreachable message due to frag. */
185 SCTP_TCB_UNLOCK(stcb);
190 nxtsz = ntohs(icmph->icmp_nextmtu);
193 * old type router that does not tell us what the next size
194 * mtu is. Rats we will have to guess (in a educated fashion
197 nxtsz = sctp_get_prev_mtu(totsz);
199 /* Stop any PMTU timer */
200 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
202 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
203 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
205 /* Adjust destination size limit */
206 if (net->mtu > nxtsz) {
209 net->mtu -= sizeof(struct udphdr);
212 /* now what about the ep? */
213 if (stcb->asoc.smallest_mtu > nxtsz) {
214 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
217 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
219 SCTP_TCB_UNLOCK(stcb);
224 sctp_notify(struct sctp_inpcb *inp,
228 struct sctp_tcb *stcb,
229 struct sctp_nets *net)
231 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
240 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
241 (sh == NULL) || (to == NULL)) {
243 SCTP_TCB_UNLOCK(stcb);
246 /* First job is to verify the vtag matches what I would send */
247 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
248 SCTP_TCB_UNLOCK(stcb);
251 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
253 if (icmph->icmp_type != ICMP_UNREACH) {
254 /* We only care about unreachable */
255 SCTP_TCB_UNLOCK(stcb);
258 if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
259 (icmph->icmp_code == ICMP_UNREACH_HOST) ||
260 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
261 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
262 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
263 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
264 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
265 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
268 * Hmm reachablity problems we must examine closely. If its
269 * not reachable, we may have lost a network. Or if there is
270 * NO protocol at the other end named SCTP. well we consider
273 if (net->dest_state & SCTP_ADDR_REACHABLE) {
274 /* Ok that destination is NOT reachable */
275 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
277 net->failure_threshold,
280 net->dest_state &= ~SCTP_ADDR_REACHABLE;
281 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
283 * JRS 5/14/07 - If a destination is unreachable,
284 * the PF bit is turned off. This allows an
285 * unambiguous use of the PF bit for destinations
286 * that are reachable but potentially failed. If the
287 * destination is set to the unreachable state, also
288 * set the destination to the PF state.
291 * Add debug message here if destination is not in
294 /* Stop any running T3 timers here? */
295 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
296 (stcb->asoc.sctp_cmt_pf > 0)) {
297 net->dest_state &= ~SCTP_ADDR_PF;
298 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
301 net->error_count = net->failure_threshold + 1;
302 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
303 stcb, SCTP_FAILED_THRESHOLD,
304 (void *)net, SCTP_SO_NOT_LOCKED);
306 SCTP_TCB_UNLOCK(stcb);
307 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
308 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
310 * Here the peer is either playing tricks on us, including
311 * an address that belongs to someone who does not support
312 * SCTP OR was a userland implementation that shutdown and
313 * now is dead. In either case treat it like a OOTB abort
316 reason = SCTP_PEER_FAULTY;
317 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
318 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
319 so = SCTP_INP_SO(inp);
320 atomic_add_int(&stcb->asoc.refcnt, 1);
321 SCTP_TCB_UNLOCK(stcb);
322 SCTP_SOCKET_LOCK(so, 1);
324 atomic_subtract_int(&stcb->asoc.refcnt, 1);
326 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
327 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
328 SCTP_SOCKET_UNLOCK(so, 1);
329 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
331 /* no need to unlock here, since the TCB is gone */
333 SCTP_TCB_UNLOCK(stcb);
338 sctp_ctlinput(cmd, sa, vip)
347 /* FIX, for non-bsd is this right? */
348 vrf_id = SCTP_DEFAULT_VRFID;
349 if (sa->sa_family != AF_INET ||
350 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
353 if (PRC_IS_REDIRECT(cmd)) {
355 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
359 struct sctp_inpcb *inp = NULL;
360 struct sctp_tcb *stcb = NULL;
361 struct sctp_nets *net = NULL;
362 struct sockaddr_in to, from;
364 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
365 bzero(&to, sizeof(to));
366 bzero(&from, sizeof(from));
367 from.sin_family = to.sin_family = AF_INET;
368 from.sin_len = to.sin_len = sizeof(to);
369 from.sin_port = sh->src_port;
370 from.sin_addr = ip->ip_src;
371 to.sin_port = sh->dest_port;
372 to.sin_addr = ip->ip_dst;
375 * 'to' holds the dest of the packet that failed to be sent.
376 * 'from' holds our local endpoint address. Thus we reverse
377 * the to and the from in the lookup.
379 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
380 (struct sockaddr *)&to,
381 &inp, &net, 1, vrf_id);
382 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
383 if (cmd != PRC_MSGSIZE) {
384 sctp_notify(inp, ip, sh,
385 (struct sockaddr *)&to, stcb,
388 /* handle possible ICMP size messages */
389 sctp_notify_mbuf(inp, stcb, net, ip, sh);
392 if ((stcb == NULL) && (inp != NULL)) {
393 /* reduce ref-count */
395 SCTP_INP_DECR_REF(inp);
396 SCTP_INP_WUNLOCK(inp);
399 SCTP_TCB_UNLOCK(stcb);
407 sctp_getcred(SYSCTL_HANDLER_ARGS)
410 struct sockaddr_in addrs[2];
411 struct sctp_inpcb *inp;
412 struct sctp_nets *net;
413 struct sctp_tcb *stcb;
417 /* FIX, for non-bsd is this right? */
418 vrf_id = SCTP_DEFAULT_VRFID;
420 error = priv_check(req->td, PRIV_NETINET_GETCRED);
425 error = SYSCTL_IN(req, addrs, sizeof(addrs));
429 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
431 &inp, &net, 1, vrf_id);
432 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
433 if ((inp != NULL) && (stcb == NULL)) {
434 /* reduce ref-count */
436 SCTP_INP_DECR_REF(inp);
439 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
443 SCTP_TCB_UNLOCK(stcb);
445 * We use the write lock here, only since in the error leg we need
446 * it. If we used RLOCK, then we would have to
447 * wlock/decr/unlock/rlock. Which in theory could create a hole.
448 * Better to use higher wlock.
452 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
454 SCTP_INP_WUNLOCK(inp);
457 cru2x(inp->sctp_socket->so_cred, &xuc);
458 SCTP_INP_WUNLOCK(inp);
459 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
464 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
465 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
469 sctp_abort(struct socket *so)
471 struct sctp_inpcb *inp;
474 inp = (struct sctp_inpcb *)so->so_pcb;
479 flags = inp->sctp_flags;
480 #ifdef SCTP_LOG_CLOSING
481 sctp_log_closing(inp, NULL, 17);
483 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
484 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
485 #ifdef SCTP_LOG_CLOSING
486 sctp_log_closing(inp, NULL, 16);
488 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
489 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
491 SCTP_SB_CLEAR(so->so_snd);
493 * same for the rcv ones, they are only here for the
496 SCTP_SB_CLEAR(so->so_rcv);
498 /* Now null out the reference, we are completely detached. */
502 flags = inp->sctp_flags;
503 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
504 goto sctp_must_try_again;
511 sctp_attach(struct socket *so, int proto, struct thread *p)
513 struct sctp_inpcb *inp;
514 struct inpcb *ip_inp;
516 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
523 inp = (struct sctp_inpcb *)so->so_pcb;
525 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
528 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
529 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
534 error = sctp_inpcb_alloc(so, vrf_id);
538 inp = (struct sctp_inpcb *)so->so_pcb;
540 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
541 ip_inp = &inp->ip_inp.inp;
542 ip_inp->inp_vflag |= INP_IPV4;
543 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
545 error = ipsec_init_policy(so, &ip_inp->inp_sp);
546 #ifdef SCTP_LOG_CLOSING
547 sctp_log_closing(inp, NULL, 17);
551 flags = inp->sctp_flags;
552 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
553 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
554 #ifdef SCTP_LOG_CLOSING
555 sctp_log_closing(inp, NULL, 15);
557 SCTP_INP_WUNLOCK(inp);
558 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
559 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
561 flags = inp->sctp_flags;
562 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
565 SCTP_INP_WUNLOCK(inp);
571 SCTP_INP_WUNLOCK(inp);
576 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
578 struct sctp_inpcb *inp = NULL;
582 if (addr && addr->sa_family != AF_INET) {
583 /* must be a v4 address! */
584 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
588 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
589 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
592 inp = (struct sctp_inpcb *)so->so_pcb;
594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
597 error = sctp_inpcb_bind(so, addr, NULL, p);
602 sctp_close(struct socket *so)
604 struct sctp_inpcb *inp;
607 inp = (struct sctp_inpcb *)so->so_pcb;
612 * Inform all the lower layer assoc that we are done.
615 flags = inp->sctp_flags;
616 #ifdef SCTP_LOG_CLOSING
617 sctp_log_closing(inp, NULL, 17);
619 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
620 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
621 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
622 (so->so_rcv.sb_cc > 0)) {
623 #ifdef SCTP_LOG_CLOSING
624 sctp_log_closing(inp, NULL, 13);
626 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
627 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
629 #ifdef SCTP_LOG_CLOSING
630 sctp_log_closing(inp, NULL, 14);
632 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
633 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
636 * The socket is now detached, no matter what the state of
637 * the SCTP association.
640 SCTP_SB_CLEAR(so->so_snd);
642 * same for the rcv ones, they are only here for the
645 SCTP_SB_CLEAR(so->so_rcv);
647 /* Now null out the reference, we are completely detached. */
651 flags = inp->sctp_flags;
652 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
653 goto sctp_must_try_again;
661 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
662 struct mbuf *control, struct thread *p);
666 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
667 struct mbuf *control, struct thread *p)
669 struct sctp_inpcb *inp;
672 inp = (struct sctp_inpcb *)so->so_pcb;
675 sctp_m_freem(control);
678 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
682 /* Got to have an to address if we are NOT a connected socket */
683 if ((addr == NULL) &&
684 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
685 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
688 } else if (addr == NULL) {
689 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
690 error = EDESTADDRREQ;
693 sctp_m_freem(control);
699 if (addr->sa_family != AF_INET) {
700 /* must be a v4 address! */
701 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
704 sctp_m_freem(control);
707 error = EDESTADDRREQ;
712 /* now what about control */
715 SCTP_PRINTF("huh? control set?\n");
716 sctp_m_freem(inp->control);
719 inp->control = control;
723 SCTP_BUF_NEXT(inp->pkt_last) = m;
726 inp->pkt_last = inp->pkt = m;
729 /* FreeBSD uses a flag passed */
730 ((flags & PRUS_MORETOCOME) == 0)
733 * note with the current version this code will only be used
734 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
735 * re-defining sosend to use the sctp_sosend. One can
736 * optionally switch back to this code (by changing back the
737 * definitions) but this is not advisable. This code is used
738 * by FreeBSD when sending a file with sendfile() though.
742 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
752 sctp_disconnect(struct socket *so)
754 struct sctp_inpcb *inp;
756 inp = (struct sctp_inpcb *)so->so_pcb;
758 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
762 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
763 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
764 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
766 SCTP_INP_RUNLOCK(inp);
769 struct sctp_association *asoc;
770 struct sctp_tcb *stcb;
772 stcb = LIST_FIRST(&inp->sctp_asoc_list);
774 SCTP_INP_RUNLOCK(inp);
775 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
780 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
781 /* We are about to be freed, out of here */
782 SCTP_TCB_UNLOCK(stcb);
783 SCTP_INP_RUNLOCK(inp);
786 if (((so->so_options & SO_LINGER) &&
787 (so->so_linger == 0)) ||
788 (so->so_rcv.sb_cc > 0)) {
789 if (SCTP_GET_STATE(asoc) !=
790 SCTP_STATE_COOKIE_WAIT) {
791 /* Left with Data unread */
794 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
800 struct sctp_paramhdr *ph;
802 ph = mtod(err, struct sctp_paramhdr *);
803 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
804 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
805 ph->param_length = htons(SCTP_BUF_LEN(err));
807 #if defined(SCTP_PANIC_ON_ABORT)
808 panic("disconnect does an abort");
810 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
811 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
813 SCTP_INP_RUNLOCK(inp);
814 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
815 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
816 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
818 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
819 /* No unlock tcb assoc is gone */
822 if (TAILQ_EMPTY(&asoc->send_queue) &&
823 TAILQ_EMPTY(&asoc->sent_queue) &&
824 (asoc->stream_queue_cnt == 0)) {
825 /* there is nothing queued to send, so done */
826 if (asoc->locked_on_sending) {
829 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
830 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
831 /* only send SHUTDOWN 1st time thru */
832 sctp_stop_timers_for_shutdown(stcb);
833 sctp_send_shutdown(stcb,
834 stcb->asoc.primary_destination);
835 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
836 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
837 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
838 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
840 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
841 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
842 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
844 asoc->primary_destination);
845 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
847 asoc->primary_destination);
851 * we still got (or just got) data to send,
852 * so set SHUTDOWN_PENDING
855 * XXX sockets draft says that SCTP_EOF
856 * should be sent with no data. currently,
857 * we will allow user data to be sent first
858 * and move to SHUTDOWN-PENDING
860 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
861 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
862 asoc->primary_destination);
863 if (asoc->locked_on_sending) {
864 /* Locked to send out the data */
865 struct sctp_stream_queue_pending *sp;
867 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
869 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
870 asoc->locked_on_sending->stream_no);
872 if ((sp->length == 0) && (sp->msg_is_complete == 0))
873 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
876 if (TAILQ_EMPTY(&asoc->send_queue) &&
877 TAILQ_EMPTY(&asoc->sent_queue) &&
878 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
882 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
883 0, M_DONTWAIT, 1, MT_DATA);
889 struct sctp_paramhdr *ph;
892 SCTP_BUF_LEN(op_err) =
893 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
895 struct sctp_paramhdr *);
896 ph->param_type = htons(
897 SCTP_CAUSE_USER_INITIATED_ABT);
898 ph->param_length = htons(SCTP_BUF_LEN(op_err));
899 ippp = (uint32_t *) (ph + 1);
900 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
902 #if defined(SCTP_PANIC_ON_ABORT)
903 panic("disconnect does an abort");
906 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
907 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
908 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
909 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
910 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
911 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
913 SCTP_INP_RUNLOCK(inp);
914 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
917 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
920 soisdisconnecting(so);
921 SCTP_TCB_UNLOCK(stcb);
922 SCTP_INP_RUNLOCK(inp);
927 /* UDP model does not support this */
928 SCTP_INP_RUNLOCK(inp);
929 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
935 sctp_flush(struct socket *so, int how)
938 * We will just clear out the values and let subsequent close clear
939 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
940 * they will not be able to read the data, the socket will block
941 * that from happening.
943 struct sctp_inpcb *inp;
945 inp = (struct sctp_inpcb *)so->so_pcb;
947 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
951 /* For the 1 to many model this does nothing */
952 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
953 SCTP_INP_RUNLOCK(inp);
956 SCTP_INP_RUNLOCK(inp);
957 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
959 * First make sure the sb will be happy, we don't use these
960 * except maybe the count
963 SCTP_INP_READ_LOCK(inp);
964 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
965 SCTP_INP_READ_UNLOCK(inp);
966 SCTP_INP_WUNLOCK(inp);
967 so->so_rcv.sb_cc = 0;
968 so->so_rcv.sb_mbcnt = 0;
969 so->so_rcv.sb_mb = NULL;
971 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
973 * First make sure the sb will be happy, we don't use these
974 * except maybe the count
976 so->so_snd.sb_cc = 0;
977 so->so_snd.sb_mbcnt = 0;
978 so->so_snd.sb_mb = NULL;
985 sctp_shutdown(struct socket *so)
987 struct sctp_inpcb *inp;
989 inp = (struct sctp_inpcb *)so->so_pcb;
991 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
995 /* For UDP model this is a invalid call */
996 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
997 /* Restore the flags that the soshutdown took away. */
998 SOCKBUF_LOCK(&so->so_rcv);
999 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1000 SOCKBUF_UNLOCK(&so->so_rcv);
1001 /* This proc will wakeup for read and do nothing (I hope) */
1002 SCTP_INP_RUNLOCK(inp);
1003 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1004 return (EOPNOTSUPP);
1007 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1008 * or SHUT_RDWR. This means we put the shutdown flag against it.
1011 struct sctp_tcb *stcb;
1012 struct sctp_association *asoc;
1015 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1016 SCTP_INP_RUNLOCK(inp);
1021 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1024 * Ok we hit the case that the shutdown call was
1025 * made after an abort or something. Nothing to do
1028 SCTP_INP_RUNLOCK(inp);
1031 SCTP_TCB_LOCK(stcb);
1033 if (TAILQ_EMPTY(&asoc->send_queue) &&
1034 TAILQ_EMPTY(&asoc->sent_queue) &&
1035 (asoc->stream_queue_cnt == 0)) {
1036 if (asoc->locked_on_sending) {
1039 /* there is nothing queued to send, so I'm done... */
1040 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1041 /* only send SHUTDOWN the first time through */
1042 sctp_stop_timers_for_shutdown(stcb);
1043 sctp_send_shutdown(stcb,
1044 stcb->asoc.primary_destination);
1045 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1046 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1047 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1048 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1050 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1051 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1052 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1053 stcb->sctp_ep, stcb,
1054 asoc->primary_destination);
1055 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1056 stcb->sctp_ep, stcb,
1057 asoc->primary_destination);
1061 * we still got (or just got) data to send, so set
1064 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1065 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1066 asoc->primary_destination);
1068 if (asoc->locked_on_sending) {
1069 /* Locked to send out the data */
1070 struct sctp_stream_queue_pending *sp;
1072 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1074 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1075 asoc->locked_on_sending->stream_no);
1077 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1078 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1082 if (TAILQ_EMPTY(&asoc->send_queue) &&
1083 TAILQ_EMPTY(&asoc->sent_queue) &&
1084 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1085 struct mbuf *op_err;
1088 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1089 0, M_DONTWAIT, 1, MT_DATA);
1091 /* Fill in the user initiated abort */
1092 struct sctp_paramhdr *ph;
1095 SCTP_BUF_LEN(op_err) =
1096 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1098 struct sctp_paramhdr *);
1099 ph->param_type = htons(
1100 SCTP_CAUSE_USER_INITIATED_ABT);
1101 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1102 ippp = (uint32_t *) (ph + 1);
1103 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1105 #if defined(SCTP_PANIC_ON_ABORT)
1106 panic("shutdown does an abort");
1108 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1109 sctp_abort_an_association(stcb->sctp_ep, stcb,
1110 SCTP_RESPONSE_TO_USER_REQ,
1111 op_err, SCTP_SO_LOCKED);
1114 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1117 SCTP_TCB_UNLOCK(stcb);
1120 SCTP_INP_RUNLOCK(inp);
1125 * copies a "user" presentable address and removes embedded scope, etc.
1126 * returns 0 on success, 1 on error
1129 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1132 struct sockaddr_in6 lsa6;
1134 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1137 memcpy(ss, sa, sa->sa_len);
1144 * NOTE: assumes addr lock is held
1147 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1148 struct sctp_tcb *stcb,
1150 struct sockaddr_storage *sas,
1153 struct sctp_ifn *sctp_ifn;
1154 struct sctp_ifa *sctp_ifa;
1155 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1157 int ipv4_addr_legal, ipv6_addr_legal;
1158 struct sctp_vrf *vrf;
1165 /* Turn on all the appropriate scope */
1166 loopback_scope = stcb->asoc.loopback_scope;
1167 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1168 local_scope = stcb->asoc.local_scope;
1169 site_scope = stcb->asoc.site_scope;
1171 /* Turn on ALL scope, since we look at the EP */
1172 loopback_scope = ipv4_local_scope = local_scope =
1175 ipv4_addr_legal = ipv6_addr_legal = 0;
1176 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1177 ipv6_addr_legal = 1;
1178 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1179 ipv4_addr_legal = 1;
1182 ipv4_addr_legal = 1;
1184 vrf = sctp_find_vrf(vrf_id);
1188 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1189 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1190 if ((loopback_scope == 0) &&
1191 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1192 /* Skip loopback if loopback_scope not set */
1195 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1198 * For the BOUND-ALL case, the list
1199 * associated with a TCB is Always
1200 * considered a reverse list.. i.e.
1201 * it lists addresses that are NOT
1202 * part of the association. If this
1203 * is one of those we must skip it.
1205 if (sctp_is_addr_restricted(stcb,
1210 switch (sctp_ifa->address.sa.sa_family) {
1212 if (ipv4_addr_legal) {
1213 struct sockaddr_in *sin;
1215 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1216 if (sin->sin_addr.s_addr == 0) {
1224 if ((ipv4_local_scope == 0) &&
1225 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1229 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1230 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1231 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1232 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1233 actual += sizeof(struct sockaddr_in6);
1236 memcpy(sas, sin, sizeof(*sin));
1237 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1238 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1239 actual += sizeof(*sin);
1243 if (actual >= limit) {
1252 if (ipv6_addr_legal) {
1253 struct sockaddr_in6 *sin6;
1255 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1256 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1264 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1265 if (local_scope == 0)
1267 if (sin6->sin6_scope_id == 0) {
1268 if (sa6_recoverscope(sin6) != 0)
1286 if ((site_scope == 0) &&
1287 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1290 memcpy(sas, sin6, sizeof(*sin6));
1291 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1292 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1293 actual += sizeof(*sin6);
1294 if (actual >= limit) {
1309 struct sctp_laddr *laddr;
1311 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1313 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1317 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1320 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1321 sas = (struct sockaddr_storage *)((caddr_t)sas +
1322 laddr->ifa->address.sa.sa_len);
1323 actual += laddr->ifa->address.sa.sa_len;
1324 if (actual >= limit) {
1333 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1334 struct sctp_tcb *stcb,
1336 struct sockaddr_storage *sas)
1340 SCTP_IPI_ADDR_RLOCK();
1341 /* fill up addresses for the endpoint's default vrf */
1342 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1344 SCTP_IPI_ADDR_RUNLOCK();
1349 * NOTE: assumes addr lock is held
1352 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1355 struct sctp_vrf *vrf = NULL;
1358 * In both sub-set bound an bound_all cases we return the MAXIMUM
1359 * number of addresses that you COULD get. In reality the sub-set
1360 * bound may have an exclusion list for a given TCB OR in the
1361 * bound-all case a TCB may NOT include the loopback or other
1362 * addresses as well.
1364 vrf = sctp_find_vrf(vrf_id);
1368 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1369 struct sctp_ifn *sctp_ifn;
1370 struct sctp_ifa *sctp_ifa;
1372 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1373 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1374 /* Count them if they are the right type */
1375 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1376 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1377 cnt += sizeof(struct sockaddr_in6);
1379 cnt += sizeof(struct sockaddr_in);
1381 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1382 cnt += sizeof(struct sockaddr_in6);
1386 struct sctp_laddr *laddr;
1388 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1389 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1390 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1391 cnt += sizeof(struct sockaddr_in6);
1393 cnt += sizeof(struct sockaddr_in);
1395 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1396 cnt += sizeof(struct sockaddr_in6);
1403 sctp_count_max_addresses(struct sctp_inpcb *inp)
1407 SCTP_IPI_ADDR_RLOCK();
1408 /* count addresses for the endpoint's default VRF */
1409 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1410 SCTP_IPI_ADDR_RUNLOCK();
1415 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1416 size_t optsize, void *p, int delay)
1419 int creat_lock_on = 0;
1420 struct sctp_tcb *stcb = NULL;
1421 struct sockaddr *sa;
1422 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1425 int bad_addresses = 0;
1428 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1430 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1431 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1432 /* We are already connected AND the TCP model */
1433 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1434 return (EADDRINUSE);
1436 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1437 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1438 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1441 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1442 SCTP_INP_RLOCK(inp);
1443 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1444 SCTP_INP_RUNLOCK(inp);
1447 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1450 SCTP_INP_INCR_REF(inp);
1451 SCTP_ASOC_CREATE_LOCK(inp);
1453 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1454 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1455 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1459 totaddrp = (int *)optval;
1460 totaddr = *totaddrp;
1461 sa = (struct sockaddr *)(totaddrp + 1);
1462 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1463 if ((stcb != NULL) || bad_addresses) {
1464 /* Already have or am bring up an association */
1465 SCTP_ASOC_CREATE_UNLOCK(inp);
1468 SCTP_TCB_UNLOCK(stcb);
1469 if (bad_addresses == 0) {
1470 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1476 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1481 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1483 struct in6pcb *inp6;
1485 inp6 = (struct in6pcb *)inp;
1486 if (SCTP_IPV6_V6ONLY(inp6)) {
1488 * if IPV6_V6ONLY flag, ignore connections destined
1489 * to a v4 addr or v4-mapped addr
1491 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1497 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1498 SCTP_PCB_FLAGS_UNBOUND) {
1499 /* Bind a ephemeral port */
1500 error = sctp_inpcb_bind(so, NULL, NULL, p);
1505 /* FIX ME: do we want to pass in a vrf on the connect call? */
1506 vrf_id = inp->def_vrf_id;
1509 /* We are GOOD to go */
1510 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1514 /* Gak! no memory */
1517 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1518 /* move to second address */
1519 if (sa->sa_family == AF_INET)
1520 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1522 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1525 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1526 /* Fill in the return id */
1528 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1531 a_id = (sctp_assoc_t *) optval;
1532 *a_id = sctp_get_associd(stcb);
1534 /* initialize authentication parameters for the assoc */
1535 sctp_initialize_auth_params(inp, stcb);
1538 /* doing delayed connection */
1539 stcb->asoc.delayed_connection = 1;
1540 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1542 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1543 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1545 SCTP_TCB_UNLOCK(stcb);
1546 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1547 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1548 /* Set the connected flag so we can queue data */
1552 if (creat_lock_on) {
1553 SCTP_ASOC_CREATE_UNLOCK(inp);
1555 SCTP_INP_DECR_REF(inp);
1559 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1560 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1561 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1562 SCTP_INP_RLOCK(inp); \
1563 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1565 SCTP_TCB_LOCK(stcb); \
1567 SCTP_INP_RUNLOCK(inp); \
1568 } else if (assoc_id != 0) { \
1569 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1570 if (stcb == NULL) { \
1571 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1581 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1582 if (size < sizeof(type)) { \
1583 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1587 destp = (type *)srcp; \
1592 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1595 struct sctp_inpcb *inp = NULL;
1597 struct sctp_tcb *stcb = NULL;
1599 if (optval == NULL) {
1600 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1603 inp = (struct sctp_inpcb *)so->so_pcb;
1605 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1612 case SCTP_AUTOCLOSE:
1613 case SCTP_EXPLICIT_EOR:
1614 case SCTP_AUTO_ASCONF:
1615 case SCTP_DISABLE_FRAGMENTS:
1616 case SCTP_I_WANT_MAPPED_V4_ADDR:
1617 case SCTP_USE_EXT_RCVINFO:
1618 SCTP_INP_RLOCK(inp);
1620 case SCTP_DISABLE_FRAGMENTS:
1621 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1623 case SCTP_I_WANT_MAPPED_V4_ADDR:
1624 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1626 case SCTP_AUTO_ASCONF:
1627 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1628 /* only valid for bound all sockets */
1629 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1631 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1636 case SCTP_EXPLICIT_EOR:
1637 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1640 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1642 case SCTP_USE_EXT_RCVINFO:
1643 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1645 case SCTP_AUTOCLOSE:
1646 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1647 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1653 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1654 error = ENOPROTOOPT;
1655 } /* end switch (sopt->sopt_name) */
1656 if (optname != SCTP_AUTOCLOSE) {
1657 /* make it an "on/off" value */
1660 if (*optsize < sizeof(val)) {
1661 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1665 SCTP_INP_RUNLOCK(inp);
1667 /* return the option value */
1668 *(int *)optval = val;
1669 *optsize = sizeof(val);
1672 case SCTP_GET_PACKET_LOG:
1674 #ifdef SCTP_PACKET_LOGGING
1678 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1679 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1682 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1687 case SCTP_REUSE_PORT:
1691 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1692 /* Can't do this for a 1-m socket */
1696 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1697 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1698 *optsize = sizeof(uint32_t);
1701 case SCTP_PARTIAL_DELIVERY_POINT:
1705 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1706 *value = inp->partial_delivery_point;
1707 *optsize = sizeof(uint32_t);
1710 case SCTP_FRAGMENT_INTERLEAVE:
1714 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1715 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1716 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1717 *value = SCTP_FRAG_LEVEL_2;
1719 *value = SCTP_FRAG_LEVEL_1;
1722 *value = SCTP_FRAG_LEVEL_0;
1724 *optsize = sizeof(uint32_t);
1727 case SCTP_CMT_ON_OFF:
1729 struct sctp_assoc_value *av;
1731 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1732 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1734 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1735 SCTP_TCB_UNLOCK(stcb);
1737 SCTP_INP_RLOCK(inp);
1738 av->assoc_value = inp->sctp_cmt_on_off;
1739 SCTP_INP_RUNLOCK(inp);
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_event = 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);
2446 struct sctp_timeouts *stimo;
2448 SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2449 SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2452 stimo->stimo_init = stcb->asoc.timoinit;
2453 stimo->stimo_data = stcb->asoc.timodata;
2454 stimo->stimo_sack = stcb->asoc.timosack;
2455 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2456 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2457 stimo->stimo_cookie = stcb->asoc.timocookie;
2458 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2459 SCTP_TCB_UNLOCK(stcb);
2461 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2464 *optsize = sizeof(*stimo);
2467 case SCTP_ASSOCINFO:
2469 struct sctp_assocparams *sasoc;
2472 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2473 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2476 oldval = sasoc->sasoc_cookie_life;
2477 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2478 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2479 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2480 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2481 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2482 SCTP_TCB_UNLOCK(stcb);
2484 SCTP_INP_RLOCK(inp);
2485 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2486 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2487 sasoc->sasoc_number_peer_destinations = 0;
2488 sasoc->sasoc_peer_rwnd = 0;
2489 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2490 SCTP_INP_RUNLOCK(inp);
2492 *optsize = sizeof(*sasoc);
2495 case SCTP_DEFAULT_SEND_PARAM:
2497 struct sctp_sndrcvinfo *s_info;
2499 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2500 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2503 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2504 SCTP_TCB_UNLOCK(stcb);
2506 SCTP_INP_RLOCK(inp);
2507 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2508 SCTP_INP_RUNLOCK(inp);
2510 *optsize = sizeof(*s_info);
2515 struct sctp_initmsg *sinit;
2517 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2518 SCTP_INP_RLOCK(inp);
2519 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2520 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2521 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2522 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2523 SCTP_INP_RUNLOCK(inp);
2524 *optsize = sizeof(*sinit);
2527 case SCTP_PRIMARY_ADDR:
2528 /* we allow a "get" operation on this */
2530 struct sctp_setprim *ssp;
2532 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2533 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2536 /* simply copy out the sockaddr_storage... */
2540 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2541 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2543 memcpy(&ssp->ssp_addr,
2544 &stcb->asoc.primary_destination->ro._l_addr,
2546 SCTP_TCB_UNLOCK(stcb);
2548 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2551 *optsize = sizeof(*ssp);
2555 case SCTP_HMAC_IDENT:
2557 struct sctp_hmacalgo *shmac;
2558 sctp_hmaclist_t *hmaclist;
2562 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2564 SCTP_INP_RLOCK(inp);
2565 hmaclist = inp->sctp_ep.local_hmacs;
2566 if (hmaclist == NULL) {
2567 /* no HMACs to return */
2568 *optsize = sizeof(*shmac);
2569 SCTP_INP_RUNLOCK(inp);
2572 /* is there room for all of the hmac ids? */
2573 size = sizeof(*shmac) + (hmaclist->num_algo *
2574 sizeof(shmac->shmac_idents[0]));
2575 if ((size_t)(*optsize) < size) {
2576 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2578 SCTP_INP_RUNLOCK(inp);
2581 /* copy in the list */
2582 shmac->shmac_number_of_idents = hmaclist->num_algo;
2583 for (i = 0; i < hmaclist->num_algo; i++) {
2584 shmac->shmac_idents[i] = hmaclist->hmac[i];
2586 SCTP_INP_RUNLOCK(inp);
2590 case SCTP_AUTH_ACTIVE_KEY:
2592 struct sctp_authkeyid *scact;
2594 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2595 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2598 /* get the active key on the assoc */
2599 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2600 SCTP_TCB_UNLOCK(stcb);
2602 /* get the endpoint active key */
2603 SCTP_INP_RLOCK(inp);
2604 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2605 SCTP_INP_RUNLOCK(inp);
2607 *optsize = sizeof(*scact);
2610 case SCTP_LOCAL_AUTH_CHUNKS:
2612 struct sctp_authchunks *sac;
2613 sctp_auth_chklist_t *chklist = NULL;
2616 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2617 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2620 /* get off the assoc */
2621 chklist = stcb->asoc.local_auth_chunks;
2622 /* is there enough space? */
2623 size = sctp_auth_get_chklist_size(chklist);
2624 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2626 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2628 /* copy in the chunks */
2629 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2631 SCTP_TCB_UNLOCK(stcb);
2633 /* get off the endpoint */
2634 SCTP_INP_RLOCK(inp);
2635 chklist = inp->sctp_ep.local_auth_chunks;
2636 /* is there enough space? */
2637 size = sctp_auth_get_chklist_size(chklist);
2638 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2640 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2642 /* copy in the chunks */
2643 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2645 SCTP_INP_RUNLOCK(inp);
2647 *optsize = sizeof(struct sctp_authchunks) + size;
2650 case SCTP_PEER_AUTH_CHUNKS:
2652 struct sctp_authchunks *sac;
2653 sctp_auth_chklist_t *chklist = NULL;
2656 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2657 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2660 /* get off the assoc */
2661 chklist = stcb->asoc.peer_auth_chunks;
2662 /* is there enough space? */
2663 size = sctp_auth_get_chklist_size(chklist);
2664 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2666 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2668 /* copy in the chunks */
2669 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2671 SCTP_TCB_UNLOCK(stcb);
2673 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2676 *optsize = sizeof(struct sctp_authchunks) + size;
2682 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2683 error = ENOPROTOOPT;
2686 } /* end switch (sopt->sopt_name) */
2691 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2696 struct sctp_tcb *stcb = NULL;
2697 struct sctp_inpcb *inp = NULL;
2700 if (optval == NULL) {
2701 SCTP_PRINTF("optval is NULL\n");
2702 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2705 inp = (struct sctp_inpcb *)so->so_pcb;
2707 SCTP_PRINTF("inp is NULL?\n");
2708 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2711 vrf_id = inp->def_vrf_id;
2716 case SCTP_AUTOCLOSE:
2717 case SCTP_AUTO_ASCONF:
2718 case SCTP_EXPLICIT_EOR:
2719 case SCTP_DISABLE_FRAGMENTS:
2720 case SCTP_USE_EXT_RCVINFO:
2721 case SCTP_I_WANT_MAPPED_V4_ADDR:
2722 /* copy in the option value */
2723 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2728 case SCTP_DISABLE_FRAGMENTS:
2729 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2731 case SCTP_AUTO_ASCONF:
2733 * NOTE: we don't really support this flag
2735 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2736 /* only valid for bound all sockets */
2737 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2739 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2743 case SCTP_EXPLICIT_EOR:
2744 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2746 case SCTP_USE_EXT_RCVINFO:
2747 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2749 case SCTP_I_WANT_MAPPED_V4_ADDR:
2750 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2751 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2753 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2758 set_opt = SCTP_PCB_FLAGS_NODELAY;
2760 case SCTP_AUTOCLOSE:
2761 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2762 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2763 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2766 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2768 * The value is in ticks. Note this does not effect
2769 * old associations, only new ones.
2771 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2774 SCTP_INP_WLOCK(inp);
2776 sctp_feature_on(inp, set_opt);
2778 sctp_feature_off(inp, set_opt);
2780 SCTP_INP_WUNLOCK(inp);
2782 case SCTP_REUSE_PORT:
2784 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2785 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2786 /* Can't set it after we are bound */
2790 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2791 /* Can't do this for a 1-m socket */
2796 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2798 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2801 case SCTP_PARTIAL_DELIVERY_POINT:
2805 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2806 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2807 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2811 inp->partial_delivery_point = *value;
2814 case SCTP_FRAGMENT_INTERLEAVE:
2815 /* not yet until we re-write sctp_recvmsg() */
2819 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2820 if (*level == SCTP_FRAG_LEVEL_2) {
2821 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2822 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2823 } else if (*level == SCTP_FRAG_LEVEL_1) {
2824 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2825 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2826 } else if (*level == SCTP_FRAG_LEVEL_0) {
2827 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2828 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2831 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2836 case SCTP_CMT_ON_OFF:
2837 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2838 struct sctp_assoc_value *av;
2840 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2841 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2843 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
2844 if (stcb->asoc.sctp_cmt_on_off > 2) {
2845 stcb->asoc.sctp_cmt_on_off = 2;
2847 SCTP_TCB_UNLOCK(stcb);
2849 SCTP_INP_WLOCK(inp);
2850 inp->sctp_cmt_on_off = av->assoc_value;
2851 if (inp->sctp_cmt_on_off > 2) {
2852 inp->sctp_cmt_on_off = 2;
2854 SCTP_INP_WUNLOCK(inp);
2857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2858 error = ENOPROTOOPT;
2861 /* JRS - Set socket option for pluggable congestion control */
2862 case SCTP_PLUGGABLE_CC:
2864 struct sctp_assoc_value *av;
2866 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2867 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2869 switch (av->assoc_value) {
2870 case SCTP_CC_RFC2581:
2873 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
2874 stcb->asoc.congestion_control_module = av->assoc_value;
2877 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2881 SCTP_TCB_UNLOCK(stcb);
2883 switch (av->assoc_value) {
2884 case SCTP_CC_RFC2581:
2887 SCTP_INP_WLOCK(inp);
2888 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2889 SCTP_INP_WUNLOCK(inp);
2892 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2899 case SCTP_CLR_STAT_LOG:
2900 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2905 struct sctp_assoc_value *av;
2907 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2908 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2911 stcb->asoc.context = av->assoc_value;
2912 SCTP_TCB_UNLOCK(stcb);
2914 SCTP_INP_WLOCK(inp);
2915 inp->sctp_context = av->assoc_value;
2916 SCTP_INP_WUNLOCK(inp);
2922 uint32_t *default_vrfid;
2924 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2925 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2926 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2930 inp->def_vrf_id = *default_vrfid;
2933 case SCTP_DEL_VRF_ID:
2935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2939 case SCTP_ADD_VRF_ID:
2941 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2945 case SCTP_DELAYED_SACK:
2947 struct sctp_sack_info *sack;
2949 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2950 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2951 if (sack->sack_delay) {
2952 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2953 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2956 if (sack->sack_delay) {
2957 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2958 sack->sack_delay = TICKS_TO_MSEC(1);
2960 stcb->asoc.delayed_ack = sack->sack_delay;
2962 if (sack->sack_freq) {
2963 stcb->asoc.sack_freq = sack->sack_freq;
2965 SCTP_TCB_UNLOCK(stcb);
2967 SCTP_INP_WLOCK(inp);
2968 if (sack->sack_delay) {
2969 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2970 sack->sack_delay = TICKS_TO_MSEC(1);
2972 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2974 if (sack->sack_freq) {
2975 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2977 SCTP_INP_WUNLOCK(inp);
2981 case SCTP_AUTH_CHUNK:
2983 struct sctp_authchunk *sauth;
2985 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2987 SCTP_INP_WLOCK(inp);
2988 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
2989 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2992 SCTP_INP_WUNLOCK(inp);
2997 struct sctp_authkey *sca;
2998 struct sctp_keyhead *shared_keys;
2999 sctp_sharedkey_t *shared_key;
3000 sctp_key_t *key = NULL;
3003 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3004 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3005 size = optsize - sizeof(*sca);
3008 /* set it on the assoc */
3009 shared_keys = &stcb->asoc.shared_keys;
3010 /* clear the cached keys for this key id */
3011 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3013 * create the new shared key and
3017 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3021 SCTP_TCB_UNLOCK(stcb);
3025 shared_key = sctp_alloc_sharedkey();
3026 if (shared_key == NULL) {
3028 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3030 SCTP_TCB_UNLOCK(stcb);
3033 shared_key->key = key;
3034 shared_key->keyid = sca->sca_keynumber;
3035 error = sctp_insert_sharedkey(shared_keys, shared_key);
3036 SCTP_TCB_UNLOCK(stcb);
3038 /* set it on the endpoint */
3039 SCTP_INP_WLOCK(inp);
3040 shared_keys = &inp->sctp_ep.shared_keys;
3042 * clear the cached keys on all assocs for
3045 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3047 * create the new shared key and
3051 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3053 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3055 SCTP_INP_WUNLOCK(inp);
3059 shared_key = sctp_alloc_sharedkey();
3060 if (shared_key == NULL) {
3062 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3064 SCTP_INP_WUNLOCK(inp);
3067 shared_key->key = key;
3068 shared_key->keyid = sca->sca_keynumber;
3069 error = sctp_insert_sharedkey(shared_keys, shared_key);
3070 SCTP_INP_WUNLOCK(inp);
3074 case SCTP_HMAC_IDENT:
3076 struct sctp_hmacalgo *shmac;
3077 sctp_hmaclist_t *hmaclist;
3083 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3084 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3085 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3089 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3090 if (hmaclist == NULL) {
3091 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3095 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3096 hmacid = shmac->shmac_idents[i];
3097 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3098 /* invalid HMACs were found */ ;
3099 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3101 sctp_free_hmaclist(hmaclist);
3102 goto sctp_set_hmac_done;
3106 for (i = 0; i < hmaclist->num_algo; i++) {
3107 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3108 /* already in list */
3113 sctp_free_hmaclist(hmaclist);
3114 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3118 /* set it on the endpoint */
3119 SCTP_INP_WLOCK(inp);
3120 if (inp->sctp_ep.local_hmacs)
3121 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3122 inp->sctp_ep.local_hmacs = hmaclist;
3123 SCTP_INP_WUNLOCK(inp);
3127 case SCTP_AUTH_ACTIVE_KEY:
3129 struct sctp_authkeyid *scact;
3131 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3133 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3135 /* set the active key on the right place */
3137 /* set the active key on the assoc */
3138 if (sctp_auth_setactivekey(stcb,
3139 scact->scact_keynumber)) {
3140 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3141 SCTP_FROM_SCTP_USRREQ,
3145 SCTP_TCB_UNLOCK(stcb);
3147 /* set the active key on the endpoint */
3148 SCTP_INP_WLOCK(inp);
3149 if (sctp_auth_setactivekey_ep(inp,
3150 scact->scact_keynumber)) {
3151 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3152 SCTP_FROM_SCTP_USRREQ,
3156 SCTP_INP_WUNLOCK(inp);
3160 case SCTP_AUTH_DELETE_KEY:
3162 struct sctp_authkeyid *scdel;
3164 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3166 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3168 /* delete the key from the right place */
3170 if (sctp_delete_sharedkey(stcb,
3171 scdel->scact_keynumber)) {
3172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3173 SCTP_FROM_SCTP_USRREQ,
3177 SCTP_TCB_UNLOCK(stcb);
3179 SCTP_INP_WLOCK(inp);
3180 if (sctp_delete_sharedkey_ep(inp,
3181 scdel->scact_keynumber)) {
3182 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3183 SCTP_FROM_SCTP_USRREQ,
3187 SCTP_INP_WUNLOCK(inp);
3191 case SCTP_AUTH_DEACTIVATE_KEY:
3193 struct sctp_authkeyid *keyid;
3195 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3197 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3199 /* deactivate the key from the right place */
3201 if (sctp_deact_sharedkey(stcb,
3202 keyid->scact_keynumber)) {
3203 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3204 SCTP_FROM_SCTP_USRREQ,
3208 SCTP_TCB_UNLOCK(stcb);
3210 SCTP_INP_WLOCK(inp);
3211 if (sctp_deact_sharedkey_ep(inp,
3212 keyid->scact_keynumber)) {
3213 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3214 SCTP_FROM_SCTP_USRREQ,
3218 SCTP_INP_WUNLOCK(inp);
3223 case SCTP_RESET_STREAMS:
3225 struct sctp_stream_reset *strrst;
3226 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3228 uint16_t addstrmcnt = 0;
3231 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3232 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3235 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3239 if (stcb->asoc.peer_supports_strreset == 0) {
3241 * Peer does not support it, we return
3242 * protocol not supported since this is true
3243 * for this feature and this peer, not the
3244 * socket request in general.
3246 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3247 error = EPROTONOSUPPORT;
3248 SCTP_TCB_UNLOCK(stcb);
3251 if (stcb->asoc.stream_reset_outstanding) {
3252 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3254 SCTP_TCB_UNLOCK(stcb);
3257 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3259 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3261 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3264 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3266 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3270 /* We can't do that and add streams */
3274 if (stcb->asoc.stream_reset_outstanding) {
3279 /* We allocate here */
3280 addstrmcnt = strrst->strrst_num_streams;
3281 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3282 /* You can't have more than 64k */
3286 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3287 /* Need to allocate more */
3288 struct sctp_stream_out *oldstream;
3289 struct sctp_stream_queue_pending *sp,
3293 oldstream = stcb->asoc.strmout;
3295 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3296 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3298 if (stcb->asoc.strmout == NULL) {
3299 stcb->asoc.strmout = oldstream;
3304 * Ok now we proceed with copying
3305 * the old out stuff and
3306 * initializing the new stuff.
3308 SCTP_TCB_SEND_LOCK(stcb);
3309 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3310 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3311 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3312 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3313 stcb->asoc.strmout[i].stream_no = i;
3314 if (oldstream[i].next_spoke.tqe_next) {
3315 sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3316 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3317 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3320 /* not on out wheel */
3321 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3322 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3326 * now anything on those
3329 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
3330 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3331 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3333 /* Did we disrupt the wheel? */
3335 sctp_insert_on_wheel(stcb,
3337 &stcb->asoc.strmout[i],
3341 * Now move assoc pointers
3344 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3345 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3347 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3348 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3351 /* now the new streams */
3352 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3353 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3354 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3355 stcb->asoc.strmout[i].stream_no = i;
3356 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3357 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3358 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3360 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3361 SCTP_FREE(oldstream, SCTP_M_STRMO);
3363 SCTP_TCB_SEND_UNLOCK(stcb);
3366 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3368 SCTP_TCB_UNLOCK(stcb);
3371 for (i = 0; i < strrst->strrst_num_streams; i++) {
3374 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3375 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3380 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3381 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3389 SCTP_TCB_UNLOCK(stcb);
3392 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3393 strrst->strrst_list,
3394 send_out, (stcb->asoc.str_reset_seq_in - 3),
3395 send_in, send_tsn, addstream, addstrmcnt);
3397 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3398 SCTP_TCB_UNLOCK(stcb);
3402 case SCTP_CONNECT_X:
3403 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3404 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3408 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3411 case SCTP_CONNECT_X_DELAYED:
3412 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3413 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3417 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3420 case SCTP_CONNECT_X_COMPLETE:
3422 struct sockaddr *sa;
3423 struct sctp_nets *net;
3425 /* FIXME MT: check correct? */
3426 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3429 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3430 SCTP_INP_RLOCK(inp);
3431 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3433 SCTP_TCB_LOCK(stcb);
3434 net = sctp_findnet(stcb, sa);
3436 SCTP_INP_RUNLOCK(inp);
3439 * We increment here since
3440 * sctp_findassociation_ep_addr() wil do a
3441 * decrement if it finds the stcb as long as
3442 * the locked tcb (last argument) is NOT a
3445 SCTP_INP_INCR_REF(inp);
3446 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3448 SCTP_INP_DECR_REF(inp);
3453 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3457 if (stcb->asoc.delayed_connection == 1) {
3458 stcb->asoc.delayed_connection = 0;
3459 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3460 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3461 stcb->asoc.primary_destination,
3462 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3463 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3466 * already expired or did not use delayed
3469 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3472 SCTP_TCB_UNLOCK(stcb);
3475 case SCTP_MAX_BURST:
3479 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3481 SCTP_INP_WLOCK(inp);
3483 inp->sctp_ep.max_burst = *burst;
3485 SCTP_INP_WUNLOCK(inp);
3490 struct sctp_assoc_value *av;
3493 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3494 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3496 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3497 ovh = SCTP_MED_OVERHEAD;
3499 ovh = SCTP_MED_V4_OVERHEAD;
3502 if (av->assoc_value) {
3503 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3505 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3507 SCTP_TCB_UNLOCK(stcb);
3509 SCTP_INP_WLOCK(inp);
3511 * FIXME MT: I think this is not in tune
3514 if (av->assoc_value) {
3515 inp->sctp_frag_point = (av->assoc_value + ovh);
3517 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3519 SCTP_INP_WUNLOCK(inp);
3525 struct sctp_event_subscribe *events;
3527 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3529 SCTP_INP_WLOCK(inp);
3530 if (events->sctp_data_io_event) {
3531 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3533 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3536 if (events->sctp_association_event) {
3537 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3539 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3542 if (events->sctp_address_event) {
3543 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3545 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3548 if (events->sctp_send_failure_event) {
3549 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3551 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3554 if (events->sctp_peer_error_event) {
3555 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3557 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3560 if (events->sctp_shutdown_event) {
3561 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3563 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3566 if (events->sctp_partial_delivery_event) {
3567 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3569 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3572 if (events->sctp_adaptation_layer_event) {
3573 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3575 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3578 if (events->sctp_authentication_event) {
3579 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3581 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3584 if (events->sctp_sender_dry_event) {
3585 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3586 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3587 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3588 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3590 SCTP_TCB_LOCK(stcb);
3593 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3594 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3595 (stcb->asoc.stream_queue_cnt == 0)) {
3596 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3599 SCTP_TCB_UNLOCK(stcb);
3603 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3606 if (events->sctp_stream_reset_event) {
3607 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3609 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3611 SCTP_INP_WUNLOCK(inp);
3615 case SCTP_ADAPTATION_LAYER:
3617 struct sctp_setadaptation *adap_bits;
3619 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3620 SCTP_INP_WLOCK(inp);
3621 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3622 SCTP_INP_WUNLOCK(inp);
3626 case SCTP_SET_INITIAL_DBG_SEQ:
3630 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3631 SCTP_INP_WLOCK(inp);
3632 inp->sctp_ep.initial_sequence_debug = *vvv;
3633 SCTP_INP_WUNLOCK(inp);
3637 case SCTP_DEFAULT_SEND_PARAM:
3639 struct sctp_sndrcvinfo *s_info;
3641 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3642 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3645 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3646 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3651 SCTP_TCB_UNLOCK(stcb);
3653 SCTP_INP_WLOCK(inp);
3654 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3655 SCTP_INP_WUNLOCK(inp);
3659 case SCTP_PEER_ADDR_PARAMS:
3660 /* Applys to the specific association */
3662 struct sctp_paddrparams *paddrp;
3663 struct sctp_nets *net;
3665 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3666 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3669 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3672 * We increment here since
3673 * sctp_findassociation_ep_addr() wil do a
3674 * decrement if it finds the stcb as long as
3675 * the locked tcb (last argument) is NOT a
3678 SCTP_INP_INCR_REF(inp);
3679 stcb = sctp_findassociation_ep_addr(&inp,
3680 (struct sockaddr *)&paddrp->spp_address,
3683 SCTP_INP_DECR_REF(inp);
3686 if (stcb && (net == NULL)) {
3687 struct sockaddr *sa;
3689 sa = (struct sockaddr *)&paddrp->spp_address;
3690 if (sa->sa_family == AF_INET) {
3691 struct sockaddr_in *sin;
3693 sin = (struct sockaddr_in *)sa;
3694 if (sin->sin_addr.s_addr) {
3695 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3696 SCTP_TCB_UNLOCK(stcb);
3700 } else if (sa->sa_family == AF_INET6) {
3701 struct sockaddr_in6 *sin6;
3703 sin6 = (struct sockaddr_in6 *)sa;
3704 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3705 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3706 SCTP_TCB_UNLOCK(stcb);
3711 error = EAFNOSUPPORT;
3712 SCTP_TCB_UNLOCK(stcb);
3713 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3718 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3720 SCTP_TCB_UNLOCK(stcb);
3721 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3724 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3726 SCTP_TCB_UNLOCK(stcb);
3727 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3731 /************************TCB SPECIFIC SET ******************/
3733 * do we change the timer for HB, we run
3738 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3739 ovh = SCTP_MED_OVERHEAD;
3741 ovh = SCTP_MED_V4_OVERHEAD;
3744 if (paddrp->spp_hbinterval)
3745 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3746 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3747 stcb->asoc.heart_beat_delay = 0;
3749 /* network sets ? */
3751 /************************NET SPECIFIC SET ******************/
3752 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3754 if (sctp_send_hb(stcb, 1, net) < 0) {
3755 /* asoc destroyed */
3756 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3761 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3762 net->dest_state |= SCTP_ADDR_NOHB;
3764 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3765 net->dest_state &= ~SCTP_ADDR_NOHB;
3767 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3768 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3769 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3770 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3772 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3773 net->mtu = paddrp->spp_pathmtu + ovh;
3774 if (net->mtu < stcb->asoc.smallest_mtu) {
3775 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3779 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3780 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3781 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3784 if (paddrp->spp_pathmaxrxt)
3785 net->failure_threshold = paddrp->spp_pathmaxrxt;
3787 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3788 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3789 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3794 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3795 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3796 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3801 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3802 if (paddrp->spp_pathmaxrxt)
3803 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3805 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3806 /* Turn back on the timer */
3807 stcb->asoc.hb_is_disabled = 0;
3808 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3810 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3811 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3812 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3813 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3814 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3816 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3817 net->mtu = paddrp->spp_pathmtu + ovh;
3818 if (net->mtu < stcb->asoc.smallest_mtu) {
3819 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3824 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3825 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3826 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3827 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3831 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3832 int cnt_of_unconf = 0;
3833 struct sctp_nets *lnet;
3835 stcb->asoc.hb_is_disabled = 1;
3836 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3837 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3842 * stop the timer ONLY if we
3843 * have no unconfirmed
3846 if (cnt_of_unconf == 0) {
3847 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3848 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3849 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3853 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3854 /* start up the timer. */
3855 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3856 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3860 if (paddrp->spp_flags & SPP_IPV4_TOS)
3861 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3864 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3865 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3869 SCTP_TCB_UNLOCK(stcb);
3871 /************************NO TCB, SET TO default stuff ******************/
3872 SCTP_INP_WLOCK(inp);
3874 * For the TOS/FLOWLABEL stuff you set it
3875 * with the options on the socket
3877 if (paddrp->spp_pathmaxrxt) {
3878 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3880 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3881 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3882 else if (paddrp->spp_hbinterval) {
3883 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3884 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3885 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3887 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3888 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3890 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3891 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3893 SCTP_INP_WUNLOCK(inp);
3899 struct sctp_rtoinfo *srto;
3900 uint32_t new_init, new_min, new_max;
3902 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3903 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3906 if (srto->srto_initial)
3907 new_init = srto->srto_initial;
3909 new_init = stcb->asoc.initial_rto;
3911 new_max = srto->srto_max;
3913 new_max = stcb->asoc.maxrto;
3915 new_min = srto->srto_min;
3917 new_min = stcb->asoc.minrto;
3918 if ((new_min <= new_init) && (new_init <= new_max)) {
3919 stcb->asoc.initial_rto = new_init;
3920 stcb->asoc.maxrto = new_max;
3921 stcb->asoc.minrto = new_min;
3923 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3926 SCTP_TCB_UNLOCK(stcb);
3928 SCTP_INP_WLOCK(inp);
3929 if (srto->srto_initial)
3930 new_init = srto->srto_initial;
3932 new_init = inp->sctp_ep.initial_rto;
3934 new_max = srto->srto_max;
3936 new_max = inp->sctp_ep.sctp_maxrto;
3938 new_min = srto->srto_min;
3940 new_min = inp->sctp_ep.sctp_minrto;
3941 if ((new_min <= new_init) && (new_init <= new_max)) {
3942 inp->sctp_ep.initial_rto = new_init;
3943 inp->sctp_ep.sctp_maxrto = new_max;
3944 inp->sctp_ep.sctp_minrto = new_min;
3946 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3949 SCTP_INP_WUNLOCK(inp);
3953 case SCTP_ASSOCINFO:
3955 struct sctp_assocparams *sasoc;
3957 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3958 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3959 if (sasoc->sasoc_cookie_life) {
3960 /* boundary check the cookie life */
3961 if (sasoc->sasoc_cookie_life < 1000)
3962 sasoc->sasoc_cookie_life = 1000;
3963 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3964 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3968 if (sasoc->sasoc_asocmaxrxt)
3969 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3970 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3971 sasoc->sasoc_peer_rwnd = 0;
3972 sasoc->sasoc_local_rwnd = 0;
3973 if (sasoc->sasoc_cookie_life) {
3974 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3976 SCTP_TCB_UNLOCK(stcb);
3978 SCTP_INP_WLOCK(inp);
3979 if (sasoc->sasoc_asocmaxrxt)
3980 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3981 sasoc->sasoc_number_peer_destinations = 0;
3982 sasoc->sasoc_peer_rwnd = 0;
3983 sasoc->sasoc_local_rwnd = 0;
3984 if (sasoc->sasoc_cookie_life) {
3985 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3987 SCTP_INP_WUNLOCK(inp);
3993 struct sctp_initmsg *sinit;
3995 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3996 SCTP_INP_WLOCK(inp);
3997 if (sinit->sinit_num_ostreams)
3998 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4000 if (sinit->sinit_max_instreams)
4001 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4003 if (sinit->sinit_max_attempts)
4004 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4006 if (sinit->sinit_max_init_timeo)
4007 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4008 SCTP_INP_WUNLOCK(inp);
4011 case SCTP_PRIMARY_ADDR:
4013 struct sctp_setprim *spa;
4014 struct sctp_nets *net, *lnet;
4016 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4017 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4021 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4024 * We increment here since
4025 * sctp_findassociation_ep_addr() wil do a
4026 * decrement if it finds the stcb as long as
4027 * the locked tcb (last argument) is NOT a
4030 SCTP_INP_INCR_REF(inp);
4031 stcb = sctp_findassociation_ep_addr(&inp,
4032 (struct sockaddr *)&spa->ssp_addr,
4035 SCTP_INP_DECR_REF(inp);
4039 if ((stcb) && (net)) {
4040 if ((net != stcb->asoc.primary_destination) &&
4041 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4042 /* Ok we need to set it */
4043 lnet = stcb->asoc.primary_destination;
4044 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4045 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4046 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4048 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4052 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4056 SCTP_TCB_UNLOCK(stcb);
4060 case SCTP_SET_DYNAMIC_PRIMARY:
4062 union sctp_sockstore *ss;
4064 error = priv_check(curthread,
4065 PRIV_NETINET_RESERVEDPORT);
4069 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4070 /* SUPER USER CHECK? */
4071 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4074 case SCTP_SET_PEER_PRIMARY_ADDR:
4076 struct sctp_setpeerprim *sspp;
4078 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4079 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4081 struct sctp_ifa *ifa;
4083 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4084 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4086 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4090 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4092 * Must validate the ifa found is in
4095 struct sctp_laddr *laddr;
4098 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4099 if (laddr->ifa == NULL) {
4100 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4104 if (laddr->ifa == ifa) {
4110 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4115 if (sctp_set_primary_ip_address_sa(stcb,
4116 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4117 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4121 SCTP_TCB_UNLOCK(stcb);
4123 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4129 case SCTP_BINDX_ADD_ADDR:
4131 struct sctp_getaddresses *addrs;
4135 td = (struct thread *)p;
4136 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4138 if (addrs->addr->sa_family == AF_INET) {
4139 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4141 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4145 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4146 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4150 } else if (addrs->addr->sa_family == AF_INET6) {
4151 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4153 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4157 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4158 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4159 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4164 error = EAFNOSUPPORT;
4167 sctp_bindx_add_address(so, inp, addrs->addr,
4168 addrs->sget_assoc_id, vrf_id,
4172 case SCTP_BINDX_REM_ADDR:
4174 struct sctp_getaddresses *addrs;
4178 td = (struct thread *)p;
4180 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4181 if (addrs->addr->sa_family == AF_INET) {
4182 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4184 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4188 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4189 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4193 } else if (addrs->addr->sa_family == AF_INET6) {
4194 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4196 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4200 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4201 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4202 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4207 error = EAFNOSUPPORT;
4210 sctp_bindx_delete_address(so, inp, addrs->addr,
4211 addrs->sget_assoc_id, vrf_id,
4216 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4217 error = ENOPROTOOPT;
4219 } /* end switch (opt) */
4224 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4226 void *optval = NULL;
4228 struct sctp_inpcb *inp;
4232 inp = (struct sctp_inpcb *)so->so_pcb;
4234 /* I made the same as TCP since we are not setup? */
4235 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4236 return (ECONNRESET);
4238 if (sopt->sopt_level != IPPROTO_SCTP) {
4239 /* wrong proto level... send back up to IP */
4241 if (INP_CHECK_SOCKAF(so, AF_INET6))
4242 error = ip6_ctloutput(so, sopt);
4245 error = ip_ctloutput(so, sopt);
4248 optsize = sopt->sopt_valsize;
4250 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4251 if (optval == NULL) {
4252 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4255 error = sooptcopyin(sopt, optval, optsize, optsize);
4257 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4261 p = (void *)sopt->sopt_td;
4262 if (sopt->sopt_dir == SOPT_SET) {
4263 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4264 } else if (sopt->sopt_dir == SOPT_GET) {
4265 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4267 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4270 if ((error == 0) && (optval != NULL)) {
4271 error = sooptcopyout(sopt, optval, optsize);
4272 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4273 } else if (optval != NULL) {
4274 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4282 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4285 int create_lock_on = 0;
4287 struct sctp_inpcb *inp;
4288 struct sctp_tcb *stcb = NULL;
4290 inp = (struct sctp_inpcb *)so->so_pcb;
4292 /* I made the same as TCP since we are not setup? */
4293 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4294 return (ECONNRESET);
4297 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4301 if (addr->sa_family == AF_INET6) {
4302 struct sockaddr_in6 *sin6p;
4304 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4305 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4308 sin6p = (struct sockaddr_in6 *)addr;
4309 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4310 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4315 if (addr->sa_family == AF_INET) {
4316 struct sockaddr_in *sinp;
4318 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4319 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4322 sinp = (struct sockaddr_in *)addr;
4323 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4324 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4328 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4329 return (EAFNOSUPPORT);
4331 SCTP_INP_INCR_REF(inp);
4332 SCTP_ASOC_CREATE_LOCK(inp);
4336 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4337 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4338 /* Should I really unlock ? */
4339 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4344 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4345 (addr->sa_family == AF_INET6)) {
4346 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4351 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4352 SCTP_PCB_FLAGS_UNBOUND) {
4353 /* Bind a ephemeral port */
4354 error = sctp_inpcb_bind(so, NULL, NULL, p);
4359 /* Now do we connect? */
4360 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4361 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4362 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4366 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4367 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4368 /* We are already connected AND the TCP model */
4369 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4373 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4374 SCTP_INP_RLOCK(inp);
4375 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4376 SCTP_INP_RUNLOCK(inp);
4379 * We increment here since sctp_findassociation_ep_addr()
4380 * will do a decrement if it finds the stcb as long as the
4381 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4383 SCTP_INP_INCR_REF(inp);
4384 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4386 SCTP_INP_DECR_REF(inp);
4388 SCTP_TCB_UNLOCK(stcb);
4392 /* Already have or am bring up an association */
4393 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4397 vrf_id = inp->def_vrf_id;
4398 /* We are GOOD to go */
4399 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4401 /* Gak! no memory */
4404 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4405 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4406 /* Set the connected flag so we can queue data */
4407 SOCKBUF_LOCK(&so->so_rcv);
4408 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4409 SOCKBUF_UNLOCK(&so->so_rcv);
4410 SOCKBUF_LOCK(&so->so_snd);
4411 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4412 SOCKBUF_UNLOCK(&so->so_snd);
4414 so->so_state &= ~SS_ISDISCONNECTING;
4418 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4419 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4421 /* initialize authentication parameters for the assoc */
4422 sctp_initialize_auth_params(inp, stcb);
4424 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4425 SCTP_TCB_UNLOCK(stcb);
4427 if (create_lock_on) {
4428 SCTP_ASOC_CREATE_UNLOCK(inp);
4430 SCTP_INP_DECR_REF(inp);
4435 sctp_listen(struct socket *so, int backlog, struct thread *p)
4438 * Note this module depends on the protocol processing being called
4439 * AFTER any socket level flags and backlog are applied to the
4440 * socket. The traditional way that the socket flags are applied is
4441 * AFTER protocol processing. We have made a change to the
4442 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4443 * place if the socket API for SCTP is to work properly.
4447 struct sctp_inpcb *inp;
4449 inp = (struct sctp_inpcb *)so->so_pcb;
4451 /* I made the same as TCP since we are not setup? */
4452 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4453 return (ECONNRESET);
4455 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4456 /* See if we have a listener */
4457 struct sctp_inpcb *tinp;
4458 union sctp_sockstore store, *sp;
4461 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4463 struct sctp_laddr *laddr;
4465 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4466 memcpy(&store, &laddr->ifa->address, sizeof(store));
4467 sp->sin.sin_port = inp->sctp_lport;
4468 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4469 if (tinp && (tinp != inp) &&
4470 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4471 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4472 (tinp->sctp_socket->so_qlimit)) {
4474 * we have a listener already and
4477 SCTP_INP_DECR_REF(tinp);
4478 return (EADDRINUSE);
4480 SCTP_INP_DECR_REF(tinp);
4484 /* Setup a local addr bound all */
4485 memset(&store, 0, sizeof(store));
4486 store.sin.sin_port = inp->sctp_lport;
4488 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4489 store.sa.sa_family = AF_INET6;
4490 store.sa.sa_len = sizeof(struct sockaddr_in6);
4493 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4494 store.sa.sa_family = AF_INET;
4495 store.sa.sa_len = sizeof(struct sockaddr_in);
4497 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4498 if (tinp && (tinp != inp) &&
4499 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4500 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4501 (tinp->sctp_socket->so_qlimit)) {
4503 * we have a listener already and its not
4506 SCTP_INP_DECR_REF(tinp);
4507 return (EADDRINUSE);
4509 SCTP_INP_DECR_REF(inp);
4513 SCTP_INP_RLOCK(inp);
4514 #ifdef SCTP_LOCK_LOGGING
4515 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4516 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4520 error = solisten_proto_check(so);
4523 SCTP_INP_RUNLOCK(inp);
4526 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4527 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4529 * The unlucky case - We are in the tcp pool with this guy.
4530 * - Someone else is in the main inp slot. - We must move
4531 * this guy (the listener) to the main slot - We must then
4532 * move the guy that was listener to the TCP Pool.
4534 if (sctp_swap_inpcb_for_listen(inp)) {
4538 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4539 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4540 /* We are already connected AND the TCP model */
4542 SCTP_INP_RUNLOCK(inp);
4544 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4545 return (EADDRINUSE);
4547 SCTP_INP_RUNLOCK(inp);
4548 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4549 /* We must do a bind. */
4551 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4552 /* bind error, probably perm */
4557 /* It appears for 7.0 and on, we must always call this. */
4558 solisten_proto(so, backlog);
4559 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4560 /* remove the ACCEPTCONN flag for one-to-many sockets */
4561 so->so_options &= ~SO_ACCEPTCONN;
4564 /* turning off listen */
4565 so->so_options &= ~SO_ACCEPTCONN;
4571 static int sctp_defered_wakeup_cnt = 0;
4574 sctp_accept(struct socket *so, struct sockaddr **addr)
4576 struct sctp_tcb *stcb;
4577 struct sctp_inpcb *inp;
4578 union sctp_sockstore store;
4584 inp = (struct sctp_inpcb *)so->so_pcb;
4587 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4588 return (ECONNRESET);
4590 SCTP_INP_RLOCK(inp);
4591 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4592 SCTP_INP_RUNLOCK(inp);
4593 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4594 return (EOPNOTSUPP);
4596 if (so->so_state & SS_ISDISCONNECTED) {
4597 SCTP_INP_RUNLOCK(inp);
4598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4599 return (ECONNABORTED);
4601 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4603 SCTP_INP_RUNLOCK(inp);
4604 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4605 return (ECONNRESET);
4607 SCTP_TCB_LOCK(stcb);
4608 SCTP_INP_RUNLOCK(inp);
4609 store = stcb->asoc.primary_destination->ro._l_addr;
4610 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4611 SCTP_TCB_UNLOCK(stcb);
4612 switch (store.sa.sa_family) {
4615 struct sockaddr_in *sin;
4617 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4620 sin->sin_family = AF_INET;
4621 sin->sin_len = sizeof(*sin);
4622 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4623 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4624 *addr = (struct sockaddr *)sin;
4630 struct sockaddr_in6 *sin6;
4632 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4635 sin6->sin6_family = AF_INET6;
4636 sin6->sin6_len = sizeof(*sin6);
4637 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4639 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4640 if ((error = sa6_recoverscope(sin6)) != 0) {
4641 SCTP_FREE_SONAME(sin6);
4644 *addr = (struct sockaddr *)sin6;
4652 /* Wake any delayed sleep action */
4653 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4654 SCTP_INP_WLOCK(inp);
4655 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4656 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4657 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4658 SCTP_INP_WUNLOCK(inp);
4659 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4660 if (sowriteable(inp->sctp_socket)) {
4661 sowwakeup_locked(inp->sctp_socket);
4663 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4665 SCTP_INP_WLOCK(inp);
4667 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4668 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4669 SCTP_INP_WUNLOCK(inp);
4670 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4671 if (soreadable(inp->sctp_socket)) {
4672 sctp_defered_wakeup_cnt++;
4673 sorwakeup_locked(inp->sctp_socket);
4675 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4677 SCTP_INP_WLOCK(inp);
4679 SCTP_INP_WUNLOCK(inp);
4681 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4682 SCTP_TCB_LOCK(stcb);
4683 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4689 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4691 struct sockaddr_in *sin;
4693 struct sctp_inpcb *inp;
4694 struct sctp_ifa *sctp_ifa;
4697 * Do the malloc first in case it blocks.
4699 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4702 sin->sin_family = AF_INET;
4703 sin->sin_len = sizeof(*sin);
4704 inp = (struct sctp_inpcb *)so->so_pcb;
4706 SCTP_FREE_SONAME(sin);
4707 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4710 SCTP_INP_RLOCK(inp);
4711 sin->sin_port = inp->sctp_lport;
4712 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4713 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4714 struct sctp_tcb *stcb;
4715 struct sockaddr_in *sin_a;
4716 struct sctp_nets *net;
4719 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4725 SCTP_TCB_LOCK(stcb);
4726 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4727 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4729 /* this will make coverity happy */
4732 if (sin_a->sin_family == AF_INET) {
4737 if ((!fnd) || (sin_a == NULL)) {
4739 SCTP_TCB_UNLOCK(stcb);
4742 vrf_id = inp->def_vrf_id;
4743 sctp_ifa = sctp_source_address_selection(inp,
4745 (sctp_route_t *) & net->ro,
4748 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4749 sctp_free_ifa(sctp_ifa);
4751 SCTP_TCB_UNLOCK(stcb);
4753 /* For the bound all case you get back 0 */
4755 sin->sin_addr.s_addr = 0;
4759 /* Take the first IPv4 address in the list */
4760 struct sctp_laddr *laddr;
4763 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4764 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4765 struct sockaddr_in *sin_a;
4767 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4768 sin->sin_addr = sin_a->sin_addr;
4774 SCTP_FREE_SONAME(sin);
4775 SCTP_INP_RUNLOCK(inp);
4776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4780 SCTP_INP_RUNLOCK(inp);
4781 (*addr) = (struct sockaddr *)sin;
4786 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4788 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4790 struct sockaddr_in *sin_a;
4791 struct sctp_inpcb *inp;
4792 struct sctp_tcb *stcb;
4793 struct sctp_nets *net;
4795 /* Do the malloc first in case it blocks. */
4796 inp = (struct sctp_inpcb *)so->so_pcb;
4797 if ((inp == NULL) ||
4798 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4799 /* UDP type and listeners will drop out here */
4800 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4803 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4806 sin->sin_family = AF_INET;
4807 sin->sin_len = sizeof(*sin);
4809 /* We must recapture incase we blocked */
4810 inp = (struct sctp_inpcb *)so->so_pcb;
4812 SCTP_FREE_SONAME(sin);
4813 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4816 SCTP_INP_RLOCK(inp);
4817 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4819 SCTP_TCB_LOCK(stcb);
4821 SCTP_INP_RUNLOCK(inp);
4823 SCTP_FREE_SONAME(sin);
4824 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4828 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4829 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4830 if (sin_a->sin_family == AF_INET) {
4832 sin->sin_port = stcb->rport;
4833 sin->sin_addr = sin_a->sin_addr;
4837 SCTP_TCB_UNLOCK(stcb);
4839 /* No IPv4 address */
4840 SCTP_FREE_SONAME(sin);
4841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4844 (*addr) = (struct sockaddr *)sin;
4848 struct pr_usrreqs sctp_usrreqs = {
4849 .pru_abort = sctp_abort,
4850 .pru_accept = sctp_accept,
4851 .pru_attach = sctp_attach,
4852 .pru_bind = sctp_bind,
4853 .pru_connect = sctp_connect,
4854 .pru_control = in_control,
4855 .pru_close = sctp_close,
4856 .pru_detach = sctp_close,
4857 .pru_sopoll = sopoll_generic,
4858 .pru_flush = sctp_flush,
4859 .pru_disconnect = sctp_disconnect,
4860 .pru_listen = sctp_listen,
4861 .pru_peeraddr = sctp_peeraddr,
4862 .pru_send = sctp_sendm,
4863 .pru_shutdown = sctp_shutdown,
4864 .pru_sockaddr = sctp_ingetaddr,
4865 .pru_sosend = sctp_sosend,
4866 .pru_soreceive = sctp_soreceive