2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctp_asconf.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_cc_functions.h>
52 #include <netinet/udp.h>
62 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
64 /* Initialize and modify the sysctled variables */
66 if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
67 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
69 * Allow a user to take no more than 1/2 the number of clusters or
70 * the SB_MAX whichever is smaller for the send window.
72 sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
73 SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
74 (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76 * Now for the recv window, should we take the same amount? or
77 * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
78 * now I will just copy.
80 SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
82 SCTP_BASE_VAR(first_time) = 0;
83 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
85 #if defined(SCTP_PACKET_LOGGING)
86 SCTP_BASE_VAR(packet_log_writers) = 0;
87 SCTP_BASE_VAR(packet_log_end) = 0;
88 bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104 struct sctp_tcb *stcb,
105 struct sctp_nets *net,
108 struct sctp_tmit_chunk *chk;
111 /* Adjust that too */
112 stcb->asoc.smallest_mtu = nxtsz;
113 /* now off to subtract IP_DF flag if needed */
114 #ifdef SCTP_PRINT_FOR_B_AND_M
115 SCTP_PRINTF("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
116 inp, stcb, net, nxtsz);
118 overhead = IP_HDR_SIZE;
119 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
120 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
122 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
123 if ((chk->send_size + overhead) > nxtsz) {
124 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
127 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
128 if ((chk->send_size + overhead) > nxtsz) {
130 * For this guy we also mark for immediate resend
131 * since we sent to big of chunk
133 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
134 if (chk->sent < SCTP_DATAGRAM_RESEND) {
135 sctp_flight_size_decrease(chk);
136 sctp_total_flight_decrease(stcb, chk);
138 if (chk->sent != SCTP_DATAGRAM_RESEND) {
139 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
141 chk->sent = SCTP_DATAGRAM_RESEND;
142 chk->rec.data.doing_fast_retransmit = 0;
143 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
144 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
145 chk->whoTo->flight_size,
147 (uintptr_t) chk->whoTo,
148 chk->rec.data.TSN_seq);
150 /* Clear any time so NO RTT is being done */
157 sctp_notify_mbuf(struct sctp_inpcb *inp,
158 struct sctp_tcb *stcb,
159 struct sctp_nets *net,
164 int totsz, tmr_stopped = 0;
168 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
169 (ip == NULL) || (sh == NULL)) {
171 SCTP_TCB_UNLOCK(stcb);
175 /* First job is to verify the vtag matches what I would send */
176 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
177 SCTP_TCB_UNLOCK(stcb);
180 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
182 if (icmph->icmp_type != ICMP_UNREACH) {
183 /* We only care about unreachable */
184 SCTP_TCB_UNLOCK(stcb);
187 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
188 /* not a unreachable message due to frag. */
189 SCTP_TCB_UNLOCK(stcb);
194 nxtsz = ntohs(icmph->icmp_nextmtu);
197 * old type router that does not tell us what the next size
198 * mtu is. Rats we will have to guess (in a educated fashion
201 nxtsz = find_next_best_mtu(totsz);
203 /* Stop any PMTU timer */
204 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
206 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
207 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
209 /* Adjust destination size limit */
210 if (net->mtu > nxtsz) {
213 net->mtu -= sizeof(struct udphdr);
216 /* now what about the ep? */
217 if (stcb->asoc.smallest_mtu > nxtsz) {
218 #ifdef SCTP_PRINT_FOR_B_AND_M
219 SCTP_PRINTF("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
222 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
225 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
227 SCTP_TCB_UNLOCK(stcb);
232 sctp_notify(struct sctp_inpcb *inp,
236 struct sctp_tcb *stcb,
237 struct sctp_nets *net)
239 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
248 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
249 (sh == NULL) || (to == NULL)) {
251 SCTP_TCB_UNLOCK(stcb);
254 /* First job is to verify the vtag matches what I would send */
255 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
256 SCTP_TCB_UNLOCK(stcb);
259 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
261 if (icmph->icmp_type != ICMP_UNREACH) {
262 /* We only care about unreachable */
263 SCTP_TCB_UNLOCK(stcb);
266 if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
267 (icmph->icmp_code == ICMP_UNREACH_HOST) ||
268 (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
269 (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
270 (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
271 (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
272 (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
273 (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
276 * Hmm reachablity problems we must examine closely. If its
277 * not reachable, we may have lost a network. Or if there is
278 * NO protocol at the other end named SCTP. well we consider
281 if (net->dest_state & SCTP_ADDR_REACHABLE) {
282 /* Ok that destination is NOT reachable */
283 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
285 net->failure_threshold,
288 net->dest_state &= ~SCTP_ADDR_REACHABLE;
289 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
291 * JRS 5/14/07 - If a destination is unreachable,
292 * the PF bit is turned off. This allows an
293 * unambiguous use of the PF bit for destinations
294 * that are reachable but potentially failed. If the
295 * destination is set to the unreachable state, also
296 * set the destination to the PF state.
299 * Add debug message here if destination is not in
302 /* Stop any running T3 timers here? */
303 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
304 net->dest_state &= ~SCTP_ADDR_PF;
305 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
308 net->error_count = net->failure_threshold + 1;
309 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
310 stcb, SCTP_FAILED_THRESHOLD,
311 (void *)net, SCTP_SO_NOT_LOCKED);
313 SCTP_TCB_UNLOCK(stcb);
314 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
315 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
317 * Here the peer is either playing tricks on us, including
318 * an address that belongs to someone who does not support
319 * SCTP OR was a userland implementation that shutdown and
320 * now is dead. In either case treat it like a OOTB abort
323 reason = SCTP_PEER_FAULTY;
324 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
325 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
326 so = SCTP_INP_SO(inp);
327 atomic_add_int(&stcb->asoc.refcnt, 1);
328 SCTP_TCB_UNLOCK(stcb);
329 SCTP_SOCKET_LOCK(so, 1);
331 atomic_subtract_int(&stcb->asoc.refcnt, 1);
333 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
334 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
335 SCTP_SOCKET_UNLOCK(so, 1);
336 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
338 /* no need to unlock here, since the TCB is gone */
340 SCTP_TCB_UNLOCK(stcb);
345 sctp_ctlinput(cmd, sa, vip)
354 /* FIX, for non-bsd is this right? */
355 vrf_id = SCTP_DEFAULT_VRFID;
356 if (sa->sa_family != AF_INET ||
357 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
360 if (PRC_IS_REDIRECT(cmd)) {
362 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
366 struct sctp_inpcb *inp = NULL;
367 struct sctp_tcb *stcb = NULL;
368 struct sctp_nets *net = NULL;
369 struct sockaddr_in to, from;
371 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
372 bzero(&to, sizeof(to));
373 bzero(&from, sizeof(from));
374 from.sin_family = to.sin_family = AF_INET;
375 from.sin_len = to.sin_len = sizeof(to);
376 from.sin_port = sh->src_port;
377 from.sin_addr = ip->ip_src;
378 to.sin_port = sh->dest_port;
379 to.sin_addr = ip->ip_dst;
382 * 'to' holds the dest of the packet that failed to be sent.
383 * 'from' holds our local endpoint address. Thus we reverse
384 * the to and the from in the lookup.
386 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
387 (struct sockaddr *)&to,
388 &inp, &net, 1, vrf_id);
389 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
390 if (cmd != PRC_MSGSIZE) {
391 sctp_notify(inp, ip, sh,
392 (struct sockaddr *)&to, stcb,
395 /* handle possible ICMP size messages */
396 sctp_notify_mbuf(inp, stcb, net, ip, sh);
399 if ((stcb == NULL) && (inp != NULL)) {
400 /* reduce ref-count */
402 SCTP_INP_DECR_REF(inp);
403 SCTP_INP_WUNLOCK(inp);
411 sctp_getcred(SYSCTL_HANDLER_ARGS)
414 struct sockaddr_in addrs[2];
415 struct sctp_inpcb *inp;
416 struct sctp_nets *net;
417 struct sctp_tcb *stcb;
421 /* FIX, for non-bsd is this right? */
422 vrf_id = SCTP_DEFAULT_VRFID;
424 error = priv_check(req->td, PRIV_NETINET_GETCRED);
429 error = SYSCTL_IN(req, addrs, sizeof(addrs));
433 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
435 &inp, &net, 1, vrf_id);
436 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
437 if ((inp != NULL) && (stcb == NULL)) {
438 /* reduce ref-count */
440 SCTP_INP_DECR_REF(inp);
443 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
447 SCTP_TCB_UNLOCK(stcb);
449 * We use the write lock here, only since in the error leg we need
450 * it. If we used RLOCK, then we would have to
451 * wlock/decr/unlock/rlock. Which in theory could create a hole.
452 * Better to use higher wlock.
456 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
458 SCTP_INP_WUNLOCK(inp);
461 cru2x(inp->sctp_socket->so_cred, &xuc);
462 SCTP_INP_WUNLOCK(inp);
463 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
468 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
469 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
473 sctp_abort(struct socket *so)
475 struct sctp_inpcb *inp;
478 inp = (struct sctp_inpcb *)so->so_pcb;
483 flags = inp->sctp_flags;
484 #ifdef SCTP_LOG_CLOSING
485 sctp_log_closing(inp, NULL, 17);
487 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
488 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
489 #ifdef SCTP_LOG_CLOSING
490 sctp_log_closing(inp, NULL, 16);
492 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
493 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
495 SCTP_SB_CLEAR(so->so_snd);
497 * same for the rcv ones, they are only here for the
500 SCTP_SB_CLEAR(so->so_rcv);
502 /* Now null out the reference, we are completely detached. */
506 flags = inp->sctp_flags;
507 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
508 goto sctp_must_try_again;
515 sctp_attach(struct socket *so, int proto, struct thread *p)
517 struct sctp_inpcb *inp;
518 struct inpcb *ip_inp;
520 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
527 inp = (struct sctp_inpcb *)so->so_pcb;
529 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
532 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
533 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
538 error = sctp_inpcb_alloc(so, vrf_id);
542 inp = (struct sctp_inpcb *)so->so_pcb;
544 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
545 ip_inp = &inp->ip_inp.inp;
546 ip_inp->inp_vflag |= INP_IPV4;
547 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
549 error = ipsec_init_policy(so, &ip_inp->inp_sp);
550 #ifdef SCTP_LOG_CLOSING
551 sctp_log_closing(inp, NULL, 17);
555 flags = inp->sctp_flags;
556 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
557 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
558 #ifdef SCTP_LOG_CLOSING
559 sctp_log_closing(inp, NULL, 15);
561 SCTP_INP_WUNLOCK(inp);
562 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
563 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
565 flags = inp->sctp_flags;
566 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
569 SCTP_INP_WUNLOCK(inp);
575 SCTP_INP_WUNLOCK(inp);
580 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
582 struct sctp_inpcb *inp = NULL;
586 if (addr && addr->sa_family != AF_INET) {
587 /* must be a v4 address! */
588 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
592 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
593 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
596 inp = (struct sctp_inpcb *)so->so_pcb;
598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
601 error = sctp_inpcb_bind(so, addr, NULL, p);
606 sctp_close(struct socket *so)
608 struct sctp_inpcb *inp;
611 inp = (struct sctp_inpcb *)so->so_pcb;
616 * Inform all the lower layer assoc that we are done.
619 flags = inp->sctp_flags;
620 #ifdef SCTP_LOG_CLOSING
621 sctp_log_closing(inp, NULL, 17);
623 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
624 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
625 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
626 (so->so_rcv.sb_cc > 0)) {
627 #ifdef SCTP_LOG_CLOSING
628 sctp_log_closing(inp, NULL, 13);
630 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
631 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
633 #ifdef SCTP_LOG_CLOSING
634 sctp_log_closing(inp, NULL, 14);
636 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
637 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
640 * The socket is now detached, no matter what the state of
641 * the SCTP association.
644 SCTP_SB_CLEAR(so->so_snd);
646 * same for the rcv ones, they are only here for the
649 SCTP_SB_CLEAR(so->so_rcv);
651 /* Now null out the reference, we are completely detached. */
655 flags = inp->sctp_flags;
656 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
657 goto sctp_must_try_again;
665 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
666 struct mbuf *control, struct thread *p);
670 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
671 struct mbuf *control, struct thread *p)
673 struct sctp_inpcb *inp;
676 inp = (struct sctp_inpcb *)so->so_pcb;
679 sctp_m_freem(control);
682 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
686 /* Got to have an to address if we are NOT a connected socket */
687 if ((addr == NULL) &&
688 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
689 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
692 } else if (addr == NULL) {
693 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
694 error = EDESTADDRREQ;
697 sctp_m_freem(control);
703 if (addr->sa_family != AF_INET) {
704 /* must be a v4 address! */
705 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
708 sctp_m_freem(control);
711 error = EDESTADDRREQ;
716 /* now what about control */
719 SCTP_PRINTF("huh? control set?\n");
720 sctp_m_freem(inp->control);
723 inp->control = control;
727 SCTP_BUF_NEXT(inp->pkt_last) = m;
730 inp->pkt_last = inp->pkt = m;
733 /* FreeBSD uses a flag passed */
734 ((flags & PRUS_MORETOCOME) == 0)
737 * note with the current version this code will only be used
738 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
739 * re-defining sosend to use the sctp_sosend. One can
740 * optionally switch back to this code (by changing back the
741 * definitions) but this is not advisable. This code is used
742 * by FreeBSD when sending a file with sendfile() though.
746 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
756 sctp_disconnect(struct socket *so)
758 struct sctp_inpcb *inp;
760 inp = (struct sctp_inpcb *)so->so_pcb;
762 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
766 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
767 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
768 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
770 SCTP_INP_RUNLOCK(inp);
773 struct sctp_association *asoc;
774 struct sctp_tcb *stcb;
776 stcb = LIST_FIRST(&inp->sctp_asoc_list);
778 SCTP_INP_RUNLOCK(inp);
779 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
784 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
785 /* We are about to be freed, out of here */
786 SCTP_TCB_UNLOCK(stcb);
787 SCTP_INP_RUNLOCK(inp);
790 if (((so->so_options & SO_LINGER) &&
791 (so->so_linger == 0)) ||
792 (so->so_rcv.sb_cc > 0)) {
793 if (SCTP_GET_STATE(asoc) !=
794 SCTP_STATE_COOKIE_WAIT) {
795 /* Left with Data unread */
798 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
804 struct sctp_paramhdr *ph;
806 ph = mtod(err, struct sctp_paramhdr *);
807 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
808 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
809 ph->param_length = htons(SCTP_BUF_LEN(err));
811 #if defined(SCTP_PANIC_ON_ABORT)
812 panic("disconnect does an abort");
814 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
815 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
817 SCTP_INP_RUNLOCK(inp);
818 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
819 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
820 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
822 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
823 /* No unlock tcb assoc is gone */
826 if (TAILQ_EMPTY(&asoc->send_queue) &&
827 TAILQ_EMPTY(&asoc->sent_queue) &&
828 (asoc->stream_queue_cnt == 0)) {
829 /* there is nothing queued to send, so done */
830 if (asoc->locked_on_sending) {
833 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
834 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
835 /* only send SHUTDOWN 1st time thru */
836 sctp_stop_timers_for_shutdown(stcb);
837 sctp_send_shutdown(stcb,
838 stcb->asoc.primary_destination);
839 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
840 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
841 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
842 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
844 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
845 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
846 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
848 asoc->primary_destination);
849 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
851 asoc->primary_destination);
855 * we still got (or just got) data to send,
856 * so set SHUTDOWN_PENDING
859 * XXX sockets draft says that SCTP_EOF
860 * should be sent with no data. currently,
861 * we will allow user data to be sent first
862 * and move to SHUTDOWN-PENDING
864 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
865 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
866 asoc->primary_destination);
867 if (asoc->locked_on_sending) {
868 /* Locked to send out the data */
869 struct sctp_stream_queue_pending *sp;
871 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
873 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
874 asoc->locked_on_sending->stream_no);
876 if ((sp->length == 0) && (sp->msg_is_complete == 0))
877 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
880 if (TAILQ_EMPTY(&asoc->send_queue) &&
881 TAILQ_EMPTY(&asoc->sent_queue) &&
882 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
886 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
887 0, M_DONTWAIT, 1, MT_DATA);
893 struct sctp_paramhdr *ph;
896 SCTP_BUF_LEN(op_err) =
897 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
899 struct sctp_paramhdr *);
900 ph->param_type = htons(
901 SCTP_CAUSE_USER_INITIATED_ABT);
902 ph->param_length = htons(SCTP_BUF_LEN(op_err));
903 ippp = (uint32_t *) (ph + 1);
904 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
906 #if defined(SCTP_PANIC_ON_ABORT)
907 panic("disconnect does an abort");
910 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
911 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
912 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
913 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
914 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
915 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
917 SCTP_INP_RUNLOCK(inp);
918 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
921 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
924 soisdisconnecting(so);
925 SCTP_TCB_UNLOCK(stcb);
926 SCTP_INP_RUNLOCK(inp);
931 /* UDP model does not support this */
932 SCTP_INP_RUNLOCK(inp);
933 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
939 sctp_flush(struct socket *so, int how)
942 * We will just clear out the values and let subsequent close clear
943 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
944 * they will not be able to read the data, the socket will block
945 * that from happening.
947 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
949 * First make sure the sb will be happy, we don't use these
950 * except maybe the count
952 so->so_rcv.sb_cc = 0;
953 so->so_rcv.sb_mbcnt = 0;
954 so->so_rcv.sb_mb = NULL;
956 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
958 * First make sure the sb will be happy, we don't use these
959 * except maybe the count
961 so->so_snd.sb_cc = 0;
962 so->so_snd.sb_mbcnt = 0;
963 so->so_snd.sb_mb = NULL;
970 sctp_shutdown(struct socket *so)
972 struct sctp_inpcb *inp;
974 inp = (struct sctp_inpcb *)so->so_pcb;
976 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
980 /* For UDP model this is a invalid call */
981 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
982 /* Restore the flags that the soshutdown took away. */
983 SOCKBUF_LOCK(&so->so_rcv);
984 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
985 SOCKBUF_UNLOCK(&so->so_rcv);
986 /* This proc will wakeup for read and do nothing (I hope) */
987 SCTP_INP_RUNLOCK(inp);
988 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
992 * Ok if we reach here its the TCP model and it is either a SHUT_WR
993 * or SHUT_RDWR. This means we put the shutdown flag against it.
996 struct sctp_tcb *stcb;
997 struct sctp_association *asoc;
1000 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1001 SCTP_INP_RUNLOCK(inp);
1006 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1009 * Ok we hit the case that the shutdown call was
1010 * made after an abort or something. Nothing to do
1013 SCTP_INP_RUNLOCK(inp);
1016 SCTP_TCB_LOCK(stcb);
1018 if (TAILQ_EMPTY(&asoc->send_queue) &&
1019 TAILQ_EMPTY(&asoc->sent_queue) &&
1020 (asoc->stream_queue_cnt == 0)) {
1021 if (asoc->locked_on_sending) {
1024 /* there is nothing queued to send, so I'm done... */
1025 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1026 /* only send SHUTDOWN the first time through */
1027 sctp_stop_timers_for_shutdown(stcb);
1028 sctp_send_shutdown(stcb,
1029 stcb->asoc.primary_destination);
1030 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1031 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1032 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1033 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1035 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1036 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1037 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1038 stcb->sctp_ep, stcb,
1039 asoc->primary_destination);
1040 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1041 stcb->sctp_ep, stcb,
1042 asoc->primary_destination);
1046 * we still got (or just got) data to send, so set
1049 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1050 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1051 asoc->primary_destination);
1053 if (asoc->locked_on_sending) {
1054 /* Locked to send out the data */
1055 struct sctp_stream_queue_pending *sp;
1057 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1059 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1060 asoc->locked_on_sending->stream_no);
1062 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1063 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1067 if (TAILQ_EMPTY(&asoc->send_queue) &&
1068 TAILQ_EMPTY(&asoc->sent_queue) &&
1069 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1070 struct mbuf *op_err;
1073 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1074 0, M_DONTWAIT, 1, MT_DATA);
1076 /* Fill in the user initiated abort */
1077 struct sctp_paramhdr *ph;
1080 SCTP_BUF_LEN(op_err) =
1081 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1083 struct sctp_paramhdr *);
1084 ph->param_type = htons(
1085 SCTP_CAUSE_USER_INITIATED_ABT);
1086 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1087 ippp = (uint32_t *) (ph + 1);
1088 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1090 #if defined(SCTP_PANIC_ON_ABORT)
1091 panic("shutdown does an abort");
1093 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1094 sctp_abort_an_association(stcb->sctp_ep, stcb,
1095 SCTP_RESPONSE_TO_USER_REQ,
1096 op_err, SCTP_SO_LOCKED);
1099 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1102 SCTP_TCB_UNLOCK(stcb);
1105 SCTP_INP_RUNLOCK(inp);
1110 * copies a "user" presentable address and removes embedded scope, etc.
1111 * returns 0 on success, 1 on error
1114 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1117 struct sockaddr_in6 lsa6;
1119 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1122 memcpy(ss, sa, sa->sa_len);
1129 * NOTE: assumes addr lock is held
1132 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1133 struct sctp_tcb *stcb,
1135 struct sockaddr_storage *sas,
1138 struct sctp_ifn *sctp_ifn;
1139 struct sctp_ifa *sctp_ifa;
1140 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1142 int ipv4_addr_legal, ipv6_addr_legal;
1143 struct sctp_vrf *vrf;
1150 /* Turn on all the appropriate scope */
1151 loopback_scope = stcb->asoc.loopback_scope;
1152 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1153 local_scope = stcb->asoc.local_scope;
1154 site_scope = stcb->asoc.site_scope;
1156 /* Turn on ALL scope, since we look at the EP */
1157 loopback_scope = ipv4_local_scope = local_scope =
1160 ipv4_addr_legal = ipv6_addr_legal = 0;
1161 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1162 ipv6_addr_legal = 1;
1163 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1164 ipv4_addr_legal = 1;
1167 ipv4_addr_legal = 1;
1169 vrf = sctp_find_vrf(vrf_id);
1173 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1174 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1175 if ((loopback_scope == 0) &&
1176 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1177 /* Skip loopback if loopback_scope not set */
1180 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1183 * For the BOUND-ALL case, the list
1184 * associated with a TCB is Always
1185 * considered a reverse list.. i.e.
1186 * it lists addresses that are NOT
1187 * part of the association. If this
1188 * is one of those we must skip it.
1190 if (sctp_is_addr_restricted(stcb,
1195 switch (sctp_ifa->address.sa.sa_family) {
1197 if (ipv4_addr_legal) {
1198 struct sockaddr_in *sin;
1200 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1201 if (sin->sin_addr.s_addr == 0) {
1209 if ((ipv4_local_scope == 0) &&
1210 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1214 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1215 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1216 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1217 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1218 actual += sizeof(struct sockaddr_in6);
1221 memcpy(sas, sin, sizeof(*sin));
1222 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1223 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1224 actual += sizeof(*sin);
1228 if (actual >= limit) {
1237 if (ipv6_addr_legal) {
1238 struct sockaddr_in6 *sin6;
1240 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1241 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1249 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1250 if (local_scope == 0)
1252 if (sin6->sin6_scope_id == 0) {
1253 if (sa6_recoverscope(sin6) != 0)
1271 if ((site_scope == 0) &&
1272 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1275 memcpy(sas, sin6, sizeof(*sin6));
1276 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1277 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1278 actual += sizeof(*sin6);
1279 if (actual >= limit) {
1294 struct sctp_laddr *laddr;
1296 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1298 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1302 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1305 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1306 sas = (struct sockaddr_storage *)((caddr_t)sas +
1307 laddr->ifa->address.sa.sa_len);
1308 actual += laddr->ifa->address.sa.sa_len;
1309 if (actual >= limit) {
1318 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1319 struct sctp_tcb *stcb,
1321 struct sockaddr_storage *sas)
1325 SCTP_IPI_ADDR_RLOCK();
1326 /* fill up addresses for the endpoint's default vrf */
1327 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1329 SCTP_IPI_ADDR_RUNLOCK();
1334 * NOTE: assumes addr lock is held
1337 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1340 struct sctp_vrf *vrf = NULL;
1343 * In both sub-set bound an bound_all cases we return the MAXIMUM
1344 * number of addresses that you COULD get. In reality the sub-set
1345 * bound may have an exclusion list for a given TCB OR in the
1346 * bound-all case a TCB may NOT include the loopback or other
1347 * addresses as well.
1349 vrf = sctp_find_vrf(vrf_id);
1353 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1354 struct sctp_ifn *sctp_ifn;
1355 struct sctp_ifa *sctp_ifa;
1357 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1358 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1359 /* Count them if they are the right type */
1360 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1361 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1362 cnt += sizeof(struct sockaddr_in6);
1364 cnt += sizeof(struct sockaddr_in);
1366 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1367 cnt += sizeof(struct sockaddr_in6);
1371 struct sctp_laddr *laddr;
1373 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1374 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1375 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1376 cnt += sizeof(struct sockaddr_in6);
1378 cnt += sizeof(struct sockaddr_in);
1380 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1381 cnt += sizeof(struct sockaddr_in6);
1388 sctp_count_max_addresses(struct sctp_inpcb *inp)
1392 SCTP_IPI_ADDR_RLOCK();
1393 /* count addresses for the endpoint's default VRF */
1394 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1395 SCTP_IPI_ADDR_RUNLOCK();
1400 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1401 size_t optsize, void *p, int delay)
1404 int creat_lock_on = 0;
1405 struct sctp_tcb *stcb = NULL;
1406 struct sockaddr *sa;
1407 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1410 int bad_addresses = 0;
1413 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1415 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1416 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1417 /* We are already connected AND the TCP model */
1418 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1419 return (EADDRINUSE);
1421 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1422 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1423 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1426 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1427 SCTP_INP_RLOCK(inp);
1428 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1429 SCTP_INP_RUNLOCK(inp);
1432 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1435 SCTP_INP_INCR_REF(inp);
1436 SCTP_ASOC_CREATE_LOCK(inp);
1438 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1439 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1440 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1444 totaddrp = (int *)optval;
1445 totaddr = *totaddrp;
1446 sa = (struct sockaddr *)(totaddrp + 1);
1447 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1448 if ((stcb != NULL) || bad_addresses) {
1449 /* Already have or am bring up an association */
1450 SCTP_ASOC_CREATE_UNLOCK(inp);
1453 SCTP_TCB_UNLOCK(stcb);
1454 if (bad_addresses == 0) {
1455 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1461 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1466 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1468 struct in6pcb *inp6;
1470 inp6 = (struct in6pcb *)inp;
1471 if (SCTP_IPV6_V6ONLY(inp6)) {
1473 * if IPV6_V6ONLY flag, ignore connections destined
1474 * to a v4 addr or v4-mapped addr
1476 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1482 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1483 SCTP_PCB_FLAGS_UNBOUND) {
1484 /* Bind a ephemeral port */
1485 error = sctp_inpcb_bind(so, NULL, NULL, p);
1490 /* FIX ME: do we want to pass in a vrf on the connect call? */
1491 vrf_id = inp->def_vrf_id;
1494 /* We are GOOD to go */
1495 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1499 /* Gak! no memory */
1502 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1503 /* move to second address */
1504 if (sa->sa_family == AF_INET)
1505 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1507 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1510 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1511 /* Fill in the return id */
1513 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1516 a_id = (sctp_assoc_t *) optval;
1517 *a_id = sctp_get_associd(stcb);
1519 /* initialize authentication parameters for the assoc */
1520 sctp_initialize_auth_params(inp, stcb);
1523 /* doing delayed connection */
1524 stcb->asoc.delayed_connection = 1;
1525 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1527 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1528 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1530 SCTP_TCB_UNLOCK(stcb);
1531 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1532 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1533 /* Set the connected flag so we can queue data */
1537 if (creat_lock_on) {
1538 SCTP_ASOC_CREATE_UNLOCK(inp);
1540 SCTP_INP_DECR_REF(inp);
1544 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1545 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1546 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1547 SCTP_INP_RLOCK(inp); \
1548 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1550 SCTP_TCB_LOCK(stcb); \
1552 SCTP_INP_RUNLOCK(inp); \
1553 } else if (assoc_id != 0) { \
1554 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1555 if (stcb == NULL) { \
1556 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1566 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1567 if (size < sizeof(type)) { \
1568 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1572 destp = (type *)srcp; \
1577 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1580 struct sctp_inpcb *inp = NULL;
1582 struct sctp_tcb *stcb = NULL;
1584 if (optval == NULL) {
1585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1588 inp = (struct sctp_inpcb *)so->so_pcb;
1590 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1597 case SCTP_AUTOCLOSE:
1598 case SCTP_EXPLICIT_EOR:
1599 case SCTP_AUTO_ASCONF:
1600 case SCTP_DISABLE_FRAGMENTS:
1601 case SCTP_I_WANT_MAPPED_V4_ADDR:
1602 case SCTP_USE_EXT_RCVINFO:
1603 SCTP_INP_RLOCK(inp);
1605 case SCTP_DISABLE_FRAGMENTS:
1606 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1608 case SCTP_I_WANT_MAPPED_V4_ADDR:
1609 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1611 case SCTP_AUTO_ASCONF:
1612 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1613 /* only valid for bound all sockets */
1614 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1621 case SCTP_EXPLICIT_EOR:
1622 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1625 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1627 case SCTP_USE_EXT_RCVINFO:
1628 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1630 case SCTP_AUTOCLOSE:
1631 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1632 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1638 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1639 error = ENOPROTOOPT;
1640 } /* end switch (sopt->sopt_name) */
1641 if (optname != SCTP_AUTOCLOSE) {
1642 /* make it an "on/off" value */
1645 if (*optsize < sizeof(val)) {
1646 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1650 SCTP_INP_RUNLOCK(inp);
1652 /* return the option value */
1653 *(int *)optval = val;
1654 *optsize = sizeof(val);
1657 case SCTP_GET_PACKET_LOG:
1659 #ifdef SCTP_PACKET_LOGGING
1663 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1664 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1667 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1672 case SCTP_REUSE_PORT:
1676 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1677 /* Can't do this for a 1-m socket */
1681 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1682 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1683 *optsize = sizeof(uint32_t);
1686 case SCTP_PARTIAL_DELIVERY_POINT:
1690 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1691 *value = inp->partial_delivery_point;
1692 *optsize = sizeof(uint32_t);
1695 case SCTP_FRAGMENT_INTERLEAVE:
1699 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1700 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1701 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1702 *value = SCTP_FRAG_LEVEL_2;
1704 *value = SCTP_FRAG_LEVEL_1;
1707 *value = SCTP_FRAG_LEVEL_0;
1709 *optsize = sizeof(uint32_t);
1712 case SCTP_CMT_ON_OFF:
1714 struct sctp_assoc_value *av;
1716 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1717 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1718 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1720 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1721 SCTP_TCB_UNLOCK(stcb);
1724 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1729 error = ENOPROTOOPT;
1731 *optsize = sizeof(*av);
1734 /* EY - set socket option for nr_sacks */
1735 case SCTP_NR_SACK_ON_OFF:
1737 struct sctp_assoc_value *av;
1739 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1740 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
1741 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1743 av->assoc_value = stcb->asoc.sctp_nr_sack_on_off;
1744 SCTP_TCB_UNLOCK(stcb);
1747 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1751 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1752 error = ENOPROTOOPT;
1754 *optsize = sizeof(*av);
1757 /* JRS - Get socket option for pluggable congestion control */
1758 case SCTP_PLUGGABLE_CC:
1760 struct sctp_assoc_value *av;
1762 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1763 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1765 av->assoc_value = stcb->asoc.congestion_control_module;
1766 SCTP_TCB_UNLOCK(stcb);
1768 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1770 *optsize = sizeof(*av);
1773 case SCTP_GET_ADDR_LEN:
1775 struct sctp_assoc_value *av;
1777 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1780 if (av->assoc_value == AF_INET) {
1781 av->assoc_value = sizeof(struct sockaddr_in);
1786 if (av->assoc_value == AF_INET6) {
1787 av->assoc_value = sizeof(struct sockaddr_in6);
1792 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1794 *optsize = sizeof(*av);
1797 case SCTP_GET_ASSOC_NUMBER:
1799 uint32_t *value, cnt;
1801 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1803 SCTP_INP_RLOCK(inp);
1804 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1807 SCTP_INP_RUNLOCK(inp);
1809 *optsize = sizeof(uint32_t);
1813 case SCTP_GET_ASSOC_ID_LIST:
1815 struct sctp_assoc_ids *ids;
1816 unsigned int at, limit;
1818 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1820 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1821 SCTP_INP_RLOCK(inp);
1822 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1824 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1827 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1831 SCTP_INP_RUNLOCK(inp);
1832 ids->gaids_number_of_ids = at;
1833 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1838 struct sctp_assoc_value *av;
1840 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1841 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1844 av->assoc_value = stcb->asoc.context;
1845 SCTP_TCB_UNLOCK(stcb);
1847 SCTP_INP_RLOCK(inp);
1848 av->assoc_value = inp->sctp_context;
1849 SCTP_INP_RUNLOCK(inp);
1851 *optsize = sizeof(*av);
1856 uint32_t *default_vrfid;
1858 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1859 *default_vrfid = inp->def_vrf_id;
1862 case SCTP_GET_ASOC_VRF:
1864 struct sctp_assoc_value *id;
1866 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1867 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1873 id->assoc_value = stcb->asoc.vrf_id;
1876 case SCTP_GET_VRF_IDS:
1878 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1882 case SCTP_GET_NONCE_VALUES:
1884 struct sctp_get_nonce_values *gnv;
1886 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1887 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1890 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1891 gnv->gn_local_tag = stcb->asoc.my_vtag;
1892 SCTP_TCB_UNLOCK(stcb);
1894 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1897 *optsize = sizeof(*gnv);
1900 case SCTP_DELAYED_SACK:
1902 struct sctp_sack_info *sack;
1904 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1905 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1907 sack->sack_delay = stcb->asoc.delayed_ack;
1908 sack->sack_freq = stcb->asoc.sack_freq;
1909 SCTP_TCB_UNLOCK(stcb);
1911 SCTP_INP_RLOCK(inp);
1912 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1913 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1914 SCTP_INP_RUNLOCK(inp);
1916 *optsize = sizeof(*sack);
1920 case SCTP_GET_SNDBUF_USE:
1922 struct sctp_sockstat *ss;
1924 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1925 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1928 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1929 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1930 stcb->asoc.size_on_all_streams);
1931 SCTP_TCB_UNLOCK(stcb);
1933 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1936 *optsize = sizeof(struct sctp_sockstat);
1939 case SCTP_MAX_BURST:
1943 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1945 SCTP_INP_RLOCK(inp);
1946 *value = inp->sctp_ep.max_burst;
1947 SCTP_INP_RUNLOCK(inp);
1948 *optsize = sizeof(uint8_t);
1953 struct sctp_assoc_value *av;
1956 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1957 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1960 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1961 SCTP_TCB_UNLOCK(stcb);
1963 SCTP_INP_RLOCK(inp);
1964 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1965 ovh = SCTP_MED_OVERHEAD;
1967 ovh = SCTP_MED_V4_OVERHEAD;
1969 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1970 av->assoc_value = 0;
1972 av->assoc_value = inp->sctp_frag_point - ovh;
1973 SCTP_INP_RUNLOCK(inp);
1975 *optsize = sizeof(struct sctp_assoc_value);
1978 case SCTP_GET_STAT_LOG:
1979 error = sctp_fill_stat_log(optval, optsize);
1983 struct sctp_event_subscribe *events;
1985 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1986 memset(events, 0, sizeof(*events));
1987 SCTP_INP_RLOCK(inp);
1988 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1989 events->sctp_data_io_event = 1;
1991 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1992 events->sctp_association_event = 1;
1994 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1995 events->sctp_address_event = 1;
1997 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1998 events->sctp_send_failure_event = 1;
2000 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2001 events->sctp_peer_error_event = 1;
2003 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2004 events->sctp_shutdown_event = 1;
2006 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2007 events->sctp_partial_delivery_event = 1;
2009 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2010 events->sctp_adaptation_layer_event = 1;
2012 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2013 events->sctp_authentication_event = 1;
2015 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2016 events->sctp_sender_dry_event = 1;
2018 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2019 events->sctp_stream_reset_event = 1;
2020 SCTP_INP_RUNLOCK(inp);
2021 *optsize = sizeof(struct sctp_event_subscribe);
2025 case SCTP_ADAPTATION_LAYER:
2029 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2031 SCTP_INP_RLOCK(inp);
2032 *value = inp->sctp_ep.adaptation_layer_indicator;
2033 SCTP_INP_RUNLOCK(inp);
2034 *optsize = sizeof(uint32_t);
2037 case SCTP_SET_INITIAL_DBG_SEQ:
2041 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2042 SCTP_INP_RLOCK(inp);
2043 *value = inp->sctp_ep.initial_sequence_debug;
2044 SCTP_INP_RUNLOCK(inp);
2045 *optsize = sizeof(uint32_t);
2048 case SCTP_GET_LOCAL_ADDR_SIZE:
2052 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2053 SCTP_INP_RLOCK(inp);
2054 *value = sctp_count_max_addresses(inp);
2055 SCTP_INP_RUNLOCK(inp);
2056 *optsize = sizeof(uint32_t);
2059 case SCTP_GET_REMOTE_ADDR_SIZE:
2063 struct sctp_nets *net;
2065 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2066 /* FIXME MT: change to sctp_assoc_value? */
2067 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2071 /* Count the sizes */
2072 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2073 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2074 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2075 size += sizeof(struct sockaddr_in6);
2076 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2077 size += sizeof(struct sockaddr_in);
2083 SCTP_TCB_UNLOCK(stcb);
2084 *value = (uint32_t) size;
2086 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2089 *optsize = sizeof(uint32_t);
2092 case SCTP_GET_PEER_ADDRESSES:
2094 * Get the address information, an array is passed in to
2095 * fill up we pack it.
2099 struct sockaddr_storage *sas;
2100 struct sctp_nets *net;
2101 struct sctp_getaddresses *saddr;
2103 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2104 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2107 left = (*optsize) - sizeof(struct sctp_getaddresses);
2108 *optsize = sizeof(struct sctp_getaddresses);
2109 sas = (struct sockaddr_storage *)&saddr->addr[0];
2111 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2112 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2113 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2114 cpsz = sizeof(struct sockaddr_in6);
2115 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2116 cpsz = sizeof(struct sockaddr_in);
2122 /* not enough room. */
2126 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2127 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2128 /* Must map the address */
2129 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2130 (struct sockaddr_in6 *)sas);
2133 memcpy(sas, &net->ro._l_addr, cpsz);
2137 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2139 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2143 SCTP_TCB_UNLOCK(stcb);
2145 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2150 case SCTP_GET_LOCAL_ADDRESSES:
2152 size_t limit, actual;
2153 struct sockaddr_storage *sas;
2154 struct sctp_getaddresses *saddr;
2156 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2157 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2159 sas = (struct sockaddr_storage *)&saddr->addr[0];
2160 limit = *optsize - sizeof(sctp_assoc_t);
2161 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2163 SCTP_TCB_UNLOCK(stcb);
2165 *optsize = sizeof(struct sockaddr_storage) + actual;
2168 case SCTP_PEER_ADDR_PARAMS:
2170 struct sctp_paddrparams *paddrp;
2171 struct sctp_nets *net;
2173 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2174 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2178 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2181 * We increment here since
2182 * sctp_findassociation_ep_addr() wil do a
2183 * decrement if it finds the stcb as long as
2184 * the locked tcb (last argument) is NOT a
2187 SCTP_INP_INCR_REF(inp);
2188 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2190 SCTP_INP_DECR_REF(inp);
2193 if (stcb && (net == NULL)) {
2194 struct sockaddr *sa;
2196 sa = (struct sockaddr *)&paddrp->spp_address;
2197 if (sa->sa_family == AF_INET) {
2198 struct sockaddr_in *sin;
2200 sin = (struct sockaddr_in *)sa;
2201 if (sin->sin_addr.s_addr) {
2203 SCTP_TCB_UNLOCK(stcb);
2204 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2207 } else if (sa->sa_family == AF_INET6) {
2208 struct sockaddr_in6 *sin6;
2210 sin6 = (struct sockaddr_in6 *)sa;
2211 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2213 SCTP_TCB_UNLOCK(stcb);
2214 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2218 error = EAFNOSUPPORT;
2219 SCTP_TCB_UNLOCK(stcb);
2220 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2225 /* Applys to the specific association */
2226 paddrp->spp_flags = 0;
2230 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2231 ovh = SCTP_MED_OVERHEAD;
2233 ovh = SCTP_MED_V4_OVERHEAD;
2237 paddrp->spp_pathmaxrxt = net->failure_threshold;
2238 paddrp->spp_pathmtu = net->mtu - ovh;
2239 /* get flags for HB */
2240 if (net->dest_state & SCTP_ADDR_NOHB)
2241 paddrp->spp_flags |= SPP_HB_DISABLE;
2243 paddrp->spp_flags |= SPP_HB_ENABLE;
2244 /* get flags for PMTU */
2245 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2246 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2248 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2251 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2252 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2253 paddrp->spp_flags |= SPP_IPV4_TOS;
2257 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2258 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2259 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2264 * No destination so return default
2269 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2270 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2272 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2273 paddrp->spp_flags |= SPP_IPV4_TOS;
2276 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2277 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2279 /* default settings should be these */
2280 if (stcb->asoc.hb_is_disabled == 0) {
2281 paddrp->spp_flags |= SPP_HB_ENABLE;
2283 paddrp->spp_flags |= SPP_HB_DISABLE;
2285 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2286 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2291 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2294 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2295 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2296 SCTP_TCB_UNLOCK(stcb);
2298 /* Use endpoint defaults */
2299 SCTP_INP_RLOCK(inp);
2300 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2301 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2302 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2303 /* get inp's default */
2305 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2306 paddrp->spp_flags |= SPP_IPV4_TOS;
2309 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2310 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2311 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2314 /* can't return this */
2315 paddrp->spp_pathmtu = 0;
2317 /* default behavior, no stcb */
2318 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2320 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2321 paddrp->spp_flags |= SPP_HB_ENABLE;
2323 paddrp->spp_flags |= SPP_HB_DISABLE;
2325 SCTP_INP_RUNLOCK(inp);
2327 *optsize = sizeof(struct sctp_paddrparams);
2330 case SCTP_GET_PEER_ADDR_INFO:
2332 struct sctp_paddrinfo *paddri;
2333 struct sctp_nets *net;
2335 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2336 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2340 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2343 * We increment here since
2344 * sctp_findassociation_ep_addr() wil do a
2345 * decrement if it finds the stcb as long as
2346 * the locked tcb (last argument) is NOT a
2349 SCTP_INP_INCR_REF(inp);
2350 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2352 SCTP_INP_DECR_REF(inp);
2356 if ((stcb) && (net)) {
2357 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2358 paddri->spinfo_cwnd = net->cwnd;
2359 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2360 paddri->spinfo_rto = net->RTO;
2361 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2362 SCTP_TCB_UNLOCK(stcb);
2365 SCTP_TCB_UNLOCK(stcb);
2367 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2370 *optsize = sizeof(struct sctp_paddrinfo);
2373 case SCTP_PCB_STATUS:
2375 struct sctp_pcbinfo *spcb;
2377 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2378 sctp_fill_pcbinfo(spcb);
2379 *optsize = sizeof(struct sctp_pcbinfo);
2385 struct sctp_nets *net;
2386 struct sctp_status *sstat;
2388 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2389 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2392 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2397 * I think passing the state is fine since
2398 * sctp_constants.h will be available to the user
2401 sstat->sstat_state = stcb->asoc.state;
2402 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2403 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2404 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2406 * We can't include chunks that have been passed to
2407 * the socket layer. Only things in queue.
2409 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2410 stcb->asoc.cnt_on_all_streams);
2413 sstat->sstat_instrms = stcb->asoc.streamincnt;
2414 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2415 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2416 memcpy(&sstat->sstat_primary.spinfo_address,
2417 &stcb->asoc.primary_destination->ro._l_addr,
2418 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2419 net = stcb->asoc.primary_destination;
2420 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2422 * Again the user can get info from sctp_constants.h
2423 * for what the state of the network is.
2425 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2426 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2427 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2428 sstat->sstat_primary.spinfo_rto = net->RTO;
2429 sstat->sstat_primary.spinfo_mtu = net->mtu;
2430 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2431 SCTP_TCB_UNLOCK(stcb);
2432 *optsize = sizeof(*sstat);
2437 struct sctp_rtoinfo *srto;
2439 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2440 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2443 srto->srto_initial = stcb->asoc.initial_rto;
2444 srto->srto_max = stcb->asoc.maxrto;
2445 srto->srto_min = stcb->asoc.minrto;
2446 SCTP_TCB_UNLOCK(stcb);
2448 SCTP_INP_RLOCK(inp);
2449 srto->srto_initial = inp->sctp_ep.initial_rto;
2450 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2451 srto->srto_min = inp->sctp_ep.sctp_minrto;
2452 SCTP_INP_RUNLOCK(inp);
2454 *optsize = sizeof(*srto);
2457 case SCTP_ASSOCINFO:
2459 struct sctp_assocparams *sasoc;
2462 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2463 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2466 oldval = sasoc->sasoc_cookie_life;
2467 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2468 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2469 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2470 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2471 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2472 SCTP_TCB_UNLOCK(stcb);
2474 SCTP_INP_RLOCK(inp);
2475 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2476 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2477 sasoc->sasoc_number_peer_destinations = 0;
2478 sasoc->sasoc_peer_rwnd = 0;
2479 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2480 SCTP_INP_RUNLOCK(inp);
2482 *optsize = sizeof(*sasoc);
2485 case SCTP_DEFAULT_SEND_PARAM:
2487 struct sctp_sndrcvinfo *s_info;
2489 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2490 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2493 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2494 SCTP_TCB_UNLOCK(stcb);
2496 SCTP_INP_RLOCK(inp);
2497 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2498 SCTP_INP_RUNLOCK(inp);
2500 *optsize = sizeof(*s_info);
2505 struct sctp_initmsg *sinit;
2507 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2508 SCTP_INP_RLOCK(inp);
2509 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2510 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2511 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2512 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2513 SCTP_INP_RUNLOCK(inp);
2514 *optsize = sizeof(*sinit);
2517 case SCTP_PRIMARY_ADDR:
2518 /* we allow a "get" operation on this */
2520 struct sctp_setprim *ssp;
2522 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2523 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2526 /* simply copy out the sockaddr_storage... */
2530 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2531 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2533 memcpy(&ssp->ssp_addr,
2534 &stcb->asoc.primary_destination->ro._l_addr,
2536 SCTP_TCB_UNLOCK(stcb);
2538 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2541 *optsize = sizeof(*ssp);
2545 case SCTP_HMAC_IDENT:
2547 struct sctp_hmacalgo *shmac;
2548 sctp_hmaclist_t *hmaclist;
2552 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2554 SCTP_INP_RLOCK(inp);
2555 hmaclist = inp->sctp_ep.local_hmacs;
2556 if (hmaclist == NULL) {
2557 /* no HMACs to return */
2558 *optsize = sizeof(*shmac);
2559 SCTP_INP_RUNLOCK(inp);
2562 /* is there room for all of the hmac ids? */
2563 size = sizeof(*shmac) + (hmaclist->num_algo *
2564 sizeof(shmac->shmac_idents[0]));
2565 if ((size_t)(*optsize) < size) {
2566 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2568 SCTP_INP_RUNLOCK(inp);
2571 /* copy in the list */
2572 shmac->shmac_number_of_idents = hmaclist->num_algo;
2573 for (i = 0; i < hmaclist->num_algo; i++) {
2574 shmac->shmac_idents[i] = hmaclist->hmac[i];
2576 SCTP_INP_RUNLOCK(inp);
2580 case SCTP_AUTH_ACTIVE_KEY:
2582 struct sctp_authkeyid *scact;
2584 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2585 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2588 /* get the active key on the assoc */
2589 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2590 SCTP_TCB_UNLOCK(stcb);
2592 /* get the endpoint active key */
2593 SCTP_INP_RLOCK(inp);
2594 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2595 SCTP_INP_RUNLOCK(inp);
2597 *optsize = sizeof(*scact);
2600 case SCTP_LOCAL_AUTH_CHUNKS:
2602 struct sctp_authchunks *sac;
2603 sctp_auth_chklist_t *chklist = NULL;
2606 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2607 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2610 /* get off the assoc */
2611 chklist = stcb->asoc.local_auth_chunks;
2612 /* is there enough space? */
2613 size = sctp_auth_get_chklist_size(chklist);
2614 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2618 /* copy in the chunks */
2619 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2621 SCTP_TCB_UNLOCK(stcb);
2623 /* get off the endpoint */
2624 SCTP_INP_RLOCK(inp);
2625 chklist = inp->sctp_ep.local_auth_chunks;
2626 /* is there enough space? */
2627 size = sctp_auth_get_chklist_size(chklist);
2628 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2630 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2632 /* copy in the chunks */
2633 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2635 SCTP_INP_RUNLOCK(inp);
2637 *optsize = sizeof(struct sctp_authchunks) + size;
2640 case SCTP_PEER_AUTH_CHUNKS:
2642 struct sctp_authchunks *sac;
2643 sctp_auth_chklist_t *chklist = NULL;
2646 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2647 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2650 /* get off the assoc */
2651 chklist = stcb->asoc.peer_auth_chunks;
2652 /* is there enough space? */
2653 size = sctp_auth_get_chklist_size(chklist);
2654 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2656 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2658 /* copy in the chunks */
2659 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2661 SCTP_TCB_UNLOCK(stcb);
2663 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2666 *optsize = sizeof(struct sctp_authchunks) + size;
2672 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2673 error = ENOPROTOOPT;
2676 } /* end switch (sopt->sopt_name) */
2681 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2686 struct sctp_tcb *stcb = NULL;
2687 struct sctp_inpcb *inp = NULL;
2690 if (optval == NULL) {
2691 SCTP_PRINTF("optval is NULL\n");
2692 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2695 inp = (struct sctp_inpcb *)so->so_pcb;
2697 SCTP_PRINTF("inp is NULL?\n");
2698 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2701 vrf_id = inp->def_vrf_id;
2706 case SCTP_AUTOCLOSE:
2707 case SCTP_AUTO_ASCONF:
2708 case SCTP_EXPLICIT_EOR:
2709 case SCTP_DISABLE_FRAGMENTS:
2710 case SCTP_USE_EXT_RCVINFO:
2711 case SCTP_I_WANT_MAPPED_V4_ADDR:
2712 /* copy in the option value */
2713 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2718 case SCTP_DISABLE_FRAGMENTS:
2719 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2721 case SCTP_AUTO_ASCONF:
2723 * NOTE: we don't really support this flag
2725 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2726 /* only valid for bound all sockets */
2727 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2733 case SCTP_EXPLICIT_EOR:
2734 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2736 case SCTP_USE_EXT_RCVINFO:
2737 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2739 case SCTP_I_WANT_MAPPED_V4_ADDR:
2740 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2741 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2743 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2748 set_opt = SCTP_PCB_FLAGS_NODELAY;
2750 case SCTP_AUTOCLOSE:
2751 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2752 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2753 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2756 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2758 * The value is in ticks. Note this does not effect
2759 * old associations, only new ones.
2761 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2764 SCTP_INP_WLOCK(inp);
2766 sctp_feature_on(inp, set_opt);
2768 sctp_feature_off(inp, set_opt);
2770 SCTP_INP_WUNLOCK(inp);
2772 case SCTP_REUSE_PORT:
2774 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2775 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2776 /* Can't set it after we are bound */
2780 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2781 /* Can't do this for a 1-m socket */
2786 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2788 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2791 case SCTP_PARTIAL_DELIVERY_POINT:
2795 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2796 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2797 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2801 inp->partial_delivery_point = *value;
2804 case SCTP_FRAGMENT_INTERLEAVE:
2805 /* not yet until we re-write sctp_recvmsg() */
2809 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2810 if (*level == SCTP_FRAG_LEVEL_2) {
2811 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2812 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2813 } else if (*level == SCTP_FRAG_LEVEL_1) {
2814 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2815 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2816 } else if (*level == SCTP_FRAG_LEVEL_0) {
2817 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2818 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2821 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2826 case SCTP_CMT_ON_OFF:
2828 struct sctp_assoc_value *av;
2830 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2831 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2832 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2834 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2835 SCTP_TCB_UNLOCK(stcb);
2837 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2842 error = ENOPROTOOPT;
2846 /* EY nr_sack_on_off socket option */
2847 case SCTP_NR_SACK_ON_OFF:
2849 struct sctp_assoc_value *av;
2851 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2852 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
2853 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2855 stcb->asoc.sctp_nr_sack_on_off = (uint8_t) av->assoc_value;
2856 SCTP_TCB_UNLOCK(stcb);
2858 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2862 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2863 error = ENOPROTOOPT;
2867 /* JRS - Set socket option for pluggable congestion control */
2868 case SCTP_PLUGGABLE_CC:
2870 struct sctp_assoc_value *av;
2872 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2873 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2875 switch (av->assoc_value) {
2877 * JRS - Standard TCP congestion
2880 case SCTP_CC_RFC2581:
2882 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2883 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2884 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2885 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2886 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2887 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2888 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2889 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2890 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2891 SCTP_TCB_UNLOCK(stcb);
2895 * JRS - High Speed TCP congestion
2900 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2901 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2902 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2903 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2904 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2905 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2906 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2907 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2908 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2909 SCTP_TCB_UNLOCK(stcb);
2912 /* JRS - HTCP congestion control */
2915 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2916 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2917 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2918 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2919 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2920 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2921 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2922 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2923 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2924 SCTP_TCB_UNLOCK(stcb);
2928 * JRS - All other values are
2933 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2935 SCTP_TCB_UNLOCK(stcb);
2940 switch (av->assoc_value) {
2941 case SCTP_CC_RFC2581:
2944 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2947 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2954 case SCTP_CLR_STAT_LOG:
2955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2960 struct sctp_assoc_value *av;
2962 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2963 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2966 stcb->asoc.context = av->assoc_value;
2967 SCTP_TCB_UNLOCK(stcb);
2969 SCTP_INP_WLOCK(inp);
2970 inp->sctp_context = av->assoc_value;
2971 SCTP_INP_WUNLOCK(inp);
2977 uint32_t *default_vrfid;
2979 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2980 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2981 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2985 inp->def_vrf_id = *default_vrfid;
2988 case SCTP_DEL_VRF_ID:
2990 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2994 case SCTP_ADD_VRF_ID:
2996 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3000 case SCTP_DELAYED_SACK:
3002 struct sctp_sack_info *sack;
3004 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
3005 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
3006 if (sack->sack_delay) {
3007 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3008 sack->sack_delay = SCTP_MAX_SACK_DELAY;
3011 if (sack->sack_delay) {
3012 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3013 sack->sack_delay = TICKS_TO_MSEC(1);
3015 stcb->asoc.delayed_ack = sack->sack_delay;
3017 if (sack->sack_freq) {
3018 stcb->asoc.sack_freq = sack->sack_freq;
3020 SCTP_TCB_UNLOCK(stcb);
3022 SCTP_INP_WLOCK(inp);
3023 if (sack->sack_delay) {
3024 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3025 sack->sack_delay = TICKS_TO_MSEC(1);
3027 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3029 if (sack->sack_freq) {
3030 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3032 SCTP_INP_WUNLOCK(inp);
3036 case SCTP_AUTH_CHUNK:
3038 struct sctp_authchunk *sauth;
3040 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3042 SCTP_INP_WLOCK(inp);
3043 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3044 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3047 SCTP_INP_WUNLOCK(inp);
3052 struct sctp_authkey *sca;
3053 struct sctp_keyhead *shared_keys;
3054 sctp_sharedkey_t *shared_key;
3055 sctp_key_t *key = NULL;
3058 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3059 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3060 size = optsize - sizeof(*sca);
3063 /* set it on the assoc */
3064 shared_keys = &stcb->asoc.shared_keys;
3065 /* clear the cached keys for this key id */
3066 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3068 * create the new shared key and
3072 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3074 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3076 SCTP_TCB_UNLOCK(stcb);
3080 shared_key = sctp_alloc_sharedkey();
3081 if (shared_key == NULL) {
3083 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3085 SCTP_TCB_UNLOCK(stcb);
3088 shared_key->key = key;
3089 shared_key->keyid = sca->sca_keynumber;
3090 error = sctp_insert_sharedkey(shared_keys, shared_key);
3091 SCTP_TCB_UNLOCK(stcb);
3093 /* set it on the endpoint */
3094 SCTP_INP_WLOCK(inp);
3095 shared_keys = &inp->sctp_ep.shared_keys;
3097 * clear the cached keys on all assocs for
3100 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3102 * create the new shared key and
3106 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3108 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3110 SCTP_INP_WUNLOCK(inp);
3114 shared_key = sctp_alloc_sharedkey();
3115 if (shared_key == NULL) {
3117 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3119 SCTP_INP_WUNLOCK(inp);
3122 shared_key->key = key;
3123 shared_key->keyid = sca->sca_keynumber;
3124 error = sctp_insert_sharedkey(shared_keys, shared_key);
3125 SCTP_INP_WUNLOCK(inp);
3129 case SCTP_HMAC_IDENT:
3131 struct sctp_hmacalgo *shmac;
3132 sctp_hmaclist_t *hmaclist;
3138 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3139 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3140 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3144 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3145 if (hmaclist == NULL) {
3146 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3150 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3151 hmacid = shmac->shmac_idents[i];
3152 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3153 /* invalid HMACs were found */ ;
3154 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3156 sctp_free_hmaclist(hmaclist);
3157 goto sctp_set_hmac_done;
3161 for (i = 0; i < hmaclist->num_algo; i++) {
3162 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3163 /* already in list */
3168 sctp_free_hmaclist(hmaclist);
3169 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3173 /* set it on the endpoint */
3174 SCTP_INP_WLOCK(inp);
3175 if (inp->sctp_ep.local_hmacs)
3176 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3177 inp->sctp_ep.local_hmacs = hmaclist;
3178 SCTP_INP_WUNLOCK(inp);
3182 case SCTP_AUTH_ACTIVE_KEY:
3184 struct sctp_authkeyid *scact;
3186 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3188 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3190 /* set the active key on the right place */
3192 /* set the active key on the assoc */
3193 if (sctp_auth_setactivekey(stcb,
3194 scact->scact_keynumber)) {
3195 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3196 SCTP_FROM_SCTP_USRREQ,
3200 SCTP_TCB_UNLOCK(stcb);
3202 /* set the active key on the endpoint */
3203 SCTP_INP_WLOCK(inp);
3204 if (sctp_auth_setactivekey_ep(inp,
3205 scact->scact_keynumber)) {
3206 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3207 SCTP_FROM_SCTP_USRREQ,
3211 SCTP_INP_WUNLOCK(inp);
3215 case SCTP_AUTH_DELETE_KEY:
3217 struct sctp_authkeyid *scdel;
3219 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3221 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3223 /* delete the key from the right place */
3225 if (sctp_delete_sharedkey(stcb,
3226 scdel->scact_keynumber)) {
3227 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3228 SCTP_FROM_SCTP_USRREQ,
3232 SCTP_TCB_UNLOCK(stcb);
3234 SCTP_INP_WLOCK(inp);
3235 if (sctp_delete_sharedkey_ep(inp,
3236 scdel->scact_keynumber)) {
3237 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3238 SCTP_FROM_SCTP_USRREQ,
3242 SCTP_INP_WUNLOCK(inp);
3246 case SCTP_AUTH_DEACTIVATE_KEY:
3248 struct sctp_authkeyid *keyid;
3250 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3252 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3254 /* deactivate the key from the right place */
3256 if (sctp_deact_sharedkey(stcb,
3257 keyid->scact_keynumber)) {
3258 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3259 SCTP_FROM_SCTP_USRREQ,
3263 SCTP_TCB_UNLOCK(stcb);
3265 SCTP_INP_WLOCK(inp);
3266 if (sctp_deact_sharedkey_ep(inp,
3267 keyid->scact_keynumber)) {
3268 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3269 SCTP_FROM_SCTP_USRREQ,
3273 SCTP_INP_WUNLOCK(inp);
3278 case SCTP_RESET_STREAMS:
3280 struct sctp_stream_reset *strrst;
3281 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3283 uint16_t addstrmcnt = 0;
3286 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3287 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3290 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3294 if (stcb->asoc.peer_supports_strreset == 0) {
3296 * Peer does not support it, we return
3297 * protocol not supported since this is true
3298 * for this feature and this peer, not the
3299 * socket request in general.
3301 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3302 error = EPROTONOSUPPORT;
3303 SCTP_TCB_UNLOCK(stcb);
3306 if (stcb->asoc.stream_reset_outstanding) {
3307 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3309 SCTP_TCB_UNLOCK(stcb);
3312 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3314 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3316 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3319 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3321 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3325 /* We can't do that and add streams */
3329 if (stcb->asoc.stream_reset_outstanding) {
3334 /* We allocate here */
3335 addstrmcnt = strrst->strrst_num_streams;
3336 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3337 /* You can't have more than 64k */
3341 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3342 /* Need to allocate more */
3343 struct sctp_stream_out *oldstream;
3344 struct sctp_stream_queue_pending *sp;
3347 oldstream = stcb->asoc.strmout;
3349 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3350 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3352 if (stcb->asoc.strmout == NULL) {
3353 stcb->asoc.strmout = oldstream;
3358 * Ok now we proceed with copying
3359 * the old out stuff and
3360 * initializing the new stuff.
3362 SCTP_TCB_SEND_LOCK(stcb);
3363 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3364 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3365 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3366 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3367 stcb->asoc.strmout[i].stream_no = i;
3368 if (oldstream[i].next_spoke.tqe_next) {
3369 sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3370 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3371 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3374 /* not on out wheel */
3375 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3376 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3380 * now anything on those
3383 while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
3384 sp = TAILQ_FIRST(&oldstream[i].outqueue);
3385 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3386 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3388 /* Did we disrupt the wheel? */
3390 sctp_insert_on_wheel(stcb,
3392 &stcb->asoc.strmout[i],
3396 * Now move assoc pointers
3399 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3400 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3402 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3403 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3406 /* now the new streams */
3407 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3408 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3409 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3410 stcb->asoc.strmout[i].stream_no = i;
3411 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3412 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3413 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3415 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3416 SCTP_FREE(oldstream, SCTP_M_STRMO);
3418 SCTP_TCB_SEND_UNLOCK(stcb);
3421 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3423 SCTP_TCB_UNLOCK(stcb);
3426 for (i = 0; i < strrst->strrst_num_streams; i++) {
3429 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3430 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3435 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3436 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3444 SCTP_TCB_UNLOCK(stcb);
3447 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3448 strrst->strrst_list,
3449 send_out, (stcb->asoc.str_reset_seq_in - 3),
3450 send_in, send_tsn, addstream, addstrmcnt);
3452 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3453 SCTP_TCB_UNLOCK(stcb);
3457 case SCTP_CONNECT_X:
3458 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3459 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3463 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3466 case SCTP_CONNECT_X_DELAYED:
3467 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3468 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3472 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3475 case SCTP_CONNECT_X_COMPLETE:
3477 struct sockaddr *sa;
3478 struct sctp_nets *net;
3480 /* FIXME MT: check correct? */
3481 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3484 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3485 SCTP_INP_RLOCK(inp);
3486 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3488 SCTP_TCB_LOCK(stcb);
3489 net = sctp_findnet(stcb, sa);
3491 SCTP_INP_RUNLOCK(inp);
3494 * We increment here since
3495 * sctp_findassociation_ep_addr() wil do a
3496 * decrement if it finds the stcb as long as
3497 * the locked tcb (last argument) is NOT a
3500 SCTP_INP_INCR_REF(inp);
3501 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3503 SCTP_INP_DECR_REF(inp);
3508 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3512 if (stcb->asoc.delayed_connection == 1) {
3513 stcb->asoc.delayed_connection = 0;
3514 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3515 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3516 stcb->asoc.primary_destination,
3517 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3518 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3521 * already expired or did not use delayed
3524 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3527 SCTP_TCB_UNLOCK(stcb);
3530 case SCTP_MAX_BURST:
3534 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3536 SCTP_INP_WLOCK(inp);
3538 inp->sctp_ep.max_burst = *burst;
3540 SCTP_INP_WUNLOCK(inp);
3545 struct sctp_assoc_value *av;
3548 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3549 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3551 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3552 ovh = SCTP_MED_OVERHEAD;
3554 ovh = SCTP_MED_V4_OVERHEAD;
3557 if (av->assoc_value) {
3558 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3560 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3562 SCTP_TCB_UNLOCK(stcb);
3564 SCTP_INP_WLOCK(inp);
3566 * FIXME MT: I think this is not in tune
3569 if (av->assoc_value) {
3570 inp->sctp_frag_point = (av->assoc_value + ovh);
3572 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3574 SCTP_INP_WUNLOCK(inp);
3580 struct sctp_event_subscribe *events;
3582 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3584 SCTP_INP_WLOCK(inp);
3585 if (events->sctp_data_io_event) {
3586 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3588 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3591 if (events->sctp_association_event) {
3592 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3594 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3597 if (events->sctp_address_event) {
3598 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3600 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3603 if (events->sctp_send_failure_event) {
3604 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3606 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3609 if (events->sctp_peer_error_event) {
3610 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3612 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3615 if (events->sctp_shutdown_event) {
3616 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3618 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3621 if (events->sctp_partial_delivery_event) {
3622 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3624 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3627 if (events->sctp_adaptation_layer_event) {
3628 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3630 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3633 if (events->sctp_authentication_event) {
3634 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3636 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3639 if (events->sctp_sender_dry_event) {
3640 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3641 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3642 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3643 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3645 SCTP_TCB_LOCK(stcb);
3648 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3649 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3650 (stcb->asoc.stream_queue_cnt == 0)) {
3651 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3654 SCTP_TCB_UNLOCK(stcb);
3658 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3661 if (events->sctp_stream_reset_event) {
3662 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3664 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3666 SCTP_INP_WUNLOCK(inp);
3670 case SCTP_ADAPTATION_LAYER:
3672 struct sctp_setadaptation *adap_bits;
3674 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3675 SCTP_INP_WLOCK(inp);
3676 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3677 SCTP_INP_WUNLOCK(inp);
3681 case SCTP_SET_INITIAL_DBG_SEQ:
3685 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3686 SCTP_INP_WLOCK(inp);
3687 inp->sctp_ep.initial_sequence_debug = *vvv;
3688 SCTP_INP_WUNLOCK(inp);
3692 case SCTP_DEFAULT_SEND_PARAM:
3694 struct sctp_sndrcvinfo *s_info;
3696 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3697 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3700 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3701 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3703 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3706 SCTP_TCB_UNLOCK(stcb);
3708 SCTP_INP_WLOCK(inp);
3709 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3710 SCTP_INP_WUNLOCK(inp);
3714 case SCTP_PEER_ADDR_PARAMS:
3715 /* Applys to the specific association */
3717 struct sctp_paddrparams *paddrp;
3718 struct sctp_nets *net;
3720 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3721 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3724 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3727 * We increment here since
3728 * sctp_findassociation_ep_addr() wil do a
3729 * decrement if it finds the stcb as long as
3730 * the locked tcb (last argument) is NOT a
3733 SCTP_INP_INCR_REF(inp);
3734 stcb = sctp_findassociation_ep_addr(&inp,
3735 (struct sockaddr *)&paddrp->spp_address,
3738 SCTP_INP_DECR_REF(inp);
3741 if (stcb && (net == NULL)) {
3742 struct sockaddr *sa;
3744 sa = (struct sockaddr *)&paddrp->spp_address;
3745 if (sa->sa_family == AF_INET) {
3746 struct sockaddr_in *sin;
3748 sin = (struct sockaddr_in *)sa;
3749 if (sin->sin_addr.s_addr) {
3750 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3751 SCTP_TCB_UNLOCK(stcb);
3755 } else if (sa->sa_family == AF_INET6) {
3756 struct sockaddr_in6 *sin6;
3758 sin6 = (struct sockaddr_in6 *)sa;
3759 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3760 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3761 SCTP_TCB_UNLOCK(stcb);
3766 error = EAFNOSUPPORT;
3767 SCTP_TCB_UNLOCK(stcb);
3768 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3773 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3775 SCTP_TCB_UNLOCK(stcb);
3776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3779 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3781 SCTP_TCB_UNLOCK(stcb);
3782 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3786 /************************TCB SPECIFIC SET ******************/
3788 * do we change the timer for HB, we run
3793 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3794 ovh = SCTP_MED_OVERHEAD;
3796 ovh = SCTP_MED_V4_OVERHEAD;
3799 if (paddrp->spp_hbinterval)
3800 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3801 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3802 stcb->asoc.heart_beat_delay = 0;
3804 /* network sets ? */
3806 /************************NET SPECIFIC SET ******************/
3807 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3809 if (sctp_send_hb(stcb, 1, net) < 0) {
3810 /* asoc destroyed */
3811 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3816 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3817 net->dest_state |= SCTP_ADDR_NOHB;
3819 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3820 net->dest_state &= ~SCTP_ADDR_NOHB;
3822 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3823 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3824 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3825 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3827 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3828 net->mtu = paddrp->spp_pathmtu + ovh;
3829 if (net->mtu < stcb->asoc.smallest_mtu) {
3830 #ifdef SCTP_PRINT_FOR_B_AND_M
3831 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3834 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3838 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3839 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3840 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3843 if (paddrp->spp_pathmaxrxt)
3844 net->failure_threshold = paddrp->spp_pathmaxrxt;
3846 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3847 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3848 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3853 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3854 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3855 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3860 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3861 if (paddrp->spp_pathmaxrxt)
3862 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3864 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3865 /* Turn back on the timer */
3866 stcb->asoc.hb_is_disabled = 0;
3867 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3869 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3870 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3871 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3872 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3873 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3875 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3876 net->mtu = paddrp->spp_pathmtu + ovh;
3877 if (net->mtu < stcb->asoc.smallest_mtu) {
3878 #ifdef SCTP_PRINT_FOR_B_AND_M
3879 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3882 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3887 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3888 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3889 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3890 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3894 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3895 int cnt_of_unconf = 0;
3896 struct sctp_nets *lnet;
3898 stcb->asoc.hb_is_disabled = 1;
3899 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3900 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3905 * stop the timer ONLY if we
3906 * have no unconfirmed
3909 if (cnt_of_unconf == 0) {
3910 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3911 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3912 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3916 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3917 /* start up the timer. */
3918 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3919 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3923 if (paddrp->spp_flags & SPP_IPV4_TOS)
3924 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3927 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3928 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3932 SCTP_TCB_UNLOCK(stcb);
3934 /************************NO TCB, SET TO default stuff ******************/
3935 SCTP_INP_WLOCK(inp);
3937 * For the TOS/FLOWLABEL stuff you set it
3938 * with the options on the socket
3940 if (paddrp->spp_pathmaxrxt) {
3941 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3943 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3944 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3945 else if (paddrp->spp_hbinterval) {
3946 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3947 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3948 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3950 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3951 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3953 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3954 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3956 SCTP_INP_WUNLOCK(inp);
3962 struct sctp_rtoinfo *srto;
3963 uint32_t new_init, new_min, new_max;
3965 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3966 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3969 if (srto->srto_initial)
3970 new_init = srto->srto_initial;
3972 new_init = stcb->asoc.initial_rto;
3974 new_max = srto->srto_max;
3976 new_max = stcb->asoc.maxrto;
3978 new_min = srto->srto_min;
3980 new_min = stcb->asoc.minrto;
3981 if ((new_min <= new_init) && (new_init <= new_max)) {
3982 stcb->asoc.initial_rto = new_init;
3983 stcb->asoc.maxrto = new_max;
3984 stcb->asoc.minrto = new_min;
3986 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3989 SCTP_TCB_UNLOCK(stcb);
3991 SCTP_INP_WLOCK(inp);
3992 if (srto->srto_initial)
3993 new_init = srto->srto_initial;
3995 new_init = inp->sctp_ep.initial_rto;
3997 new_max = srto->srto_max;
3999 new_max = inp->sctp_ep.sctp_maxrto;
4001 new_min = srto->srto_min;
4003 new_min = inp->sctp_ep.sctp_minrto;
4004 if ((new_min <= new_init) && (new_init <= new_max)) {
4005 inp->sctp_ep.initial_rto = new_init;
4006 inp->sctp_ep.sctp_maxrto = new_max;
4007 inp->sctp_ep.sctp_minrto = new_min;
4009 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4012 SCTP_INP_WUNLOCK(inp);
4016 case SCTP_ASSOCINFO:
4018 struct sctp_assocparams *sasoc;
4020 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4021 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4022 if (sasoc->sasoc_cookie_life) {
4023 /* boundary check the cookie life */
4024 if (sasoc->sasoc_cookie_life < 1000)
4025 sasoc->sasoc_cookie_life = 1000;
4026 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4027 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4031 if (sasoc->sasoc_asocmaxrxt)
4032 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4033 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4034 sasoc->sasoc_peer_rwnd = 0;
4035 sasoc->sasoc_local_rwnd = 0;
4036 if (sasoc->sasoc_cookie_life) {
4037 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4039 SCTP_TCB_UNLOCK(stcb);
4041 SCTP_INP_WLOCK(inp);
4042 if (sasoc->sasoc_asocmaxrxt)
4043 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4044 sasoc->sasoc_number_peer_destinations = 0;
4045 sasoc->sasoc_peer_rwnd = 0;
4046 sasoc->sasoc_local_rwnd = 0;
4047 if (sasoc->sasoc_cookie_life) {
4048 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4050 SCTP_INP_WUNLOCK(inp);
4056 struct sctp_initmsg *sinit;
4058 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4059 SCTP_INP_WLOCK(inp);
4060 if (sinit->sinit_num_ostreams)
4061 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4063 if (sinit->sinit_max_instreams)
4064 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4066 if (sinit->sinit_max_attempts)
4067 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4069 if (sinit->sinit_max_init_timeo)
4070 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4071 SCTP_INP_WUNLOCK(inp);
4074 case SCTP_PRIMARY_ADDR:
4076 struct sctp_setprim *spa;
4077 struct sctp_nets *net, *lnet;
4079 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4080 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4084 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4087 * We increment here since
4088 * sctp_findassociation_ep_addr() wil do a
4089 * decrement if it finds the stcb as long as
4090 * the locked tcb (last argument) is NOT a
4093 SCTP_INP_INCR_REF(inp);
4094 stcb = sctp_findassociation_ep_addr(&inp,
4095 (struct sockaddr *)&spa->ssp_addr,
4098 SCTP_INP_DECR_REF(inp);
4102 if ((stcb) && (net)) {
4103 if ((net != stcb->asoc.primary_destination) &&
4104 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4105 /* Ok we need to set it */
4106 lnet = stcb->asoc.primary_destination;
4107 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4108 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4109 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4111 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4115 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4119 SCTP_TCB_UNLOCK(stcb);
4123 case SCTP_SET_DYNAMIC_PRIMARY:
4125 union sctp_sockstore *ss;
4127 error = priv_check(curthread,
4128 PRIV_NETINET_RESERVEDPORT);
4132 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4133 /* SUPER USER CHECK? */
4134 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4137 case SCTP_SET_PEER_PRIMARY_ADDR:
4139 struct sctp_setpeerprim *sspp;
4141 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4142 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4144 struct sctp_ifa *ifa;
4146 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4147 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4149 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4153 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4155 * Must validate the ifa found is in
4158 struct sctp_laddr *laddr;
4161 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4162 if (laddr->ifa == NULL) {
4163 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4167 if (laddr->ifa == ifa) {
4173 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4178 if (sctp_set_primary_ip_address_sa(stcb,
4179 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4180 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4184 SCTP_TCB_UNLOCK(stcb);
4186 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4192 case SCTP_BINDX_ADD_ADDR:
4194 struct sctp_getaddresses *addrs;
4198 td = (struct thread *)p;
4199 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4201 if (addrs->addr->sa_family == AF_INET) {
4202 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4204 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4208 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4209 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4213 } else if (addrs->addr->sa_family == AF_INET6) {
4214 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4216 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4220 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4221 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4222 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4227 error = EAFNOSUPPORT;
4230 sctp_bindx_add_address(so, inp, addrs->addr,
4231 addrs->sget_assoc_id, vrf_id,
4235 case SCTP_BINDX_REM_ADDR:
4237 struct sctp_getaddresses *addrs;
4241 td = (struct thread *)p;
4243 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4244 if (addrs->addr->sa_family == AF_INET) {
4245 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4247 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4251 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4252 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4256 } else if (addrs->addr->sa_family == AF_INET6) {
4257 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4259 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4263 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4264 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4265 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4270 error = EAFNOSUPPORT;
4273 sctp_bindx_delete_address(so, inp, addrs->addr,
4274 addrs->sget_assoc_id, vrf_id,
4279 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4280 error = ENOPROTOOPT;
4282 } /* end switch (opt) */
4287 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4289 void *optval = NULL;
4291 struct sctp_inpcb *inp;
4295 inp = (struct sctp_inpcb *)so->so_pcb;
4297 /* I made the same as TCP since we are not setup? */
4298 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4299 return (ECONNRESET);
4301 if (sopt->sopt_level != IPPROTO_SCTP) {
4302 /* wrong proto level... send back up to IP */
4304 if (INP_CHECK_SOCKAF(so, AF_INET6))
4305 error = ip6_ctloutput(so, sopt);
4308 error = ip_ctloutput(so, sopt);
4311 optsize = sopt->sopt_valsize;
4313 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4314 if (optval == NULL) {
4315 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4318 error = sooptcopyin(sopt, optval, optsize, optsize);
4320 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4324 p = (void *)sopt->sopt_td;
4325 if (sopt->sopt_dir == SOPT_SET) {
4326 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4327 } else if (sopt->sopt_dir == SOPT_GET) {
4328 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4330 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4333 if ((error == 0) && (optval != NULL)) {
4334 error = sooptcopyout(sopt, optval, optsize);
4335 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4336 } else if (optval != NULL) {
4337 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4345 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4348 int create_lock_on = 0;
4350 struct sctp_inpcb *inp;
4351 struct sctp_tcb *stcb = NULL;
4353 inp = (struct sctp_inpcb *)so->so_pcb;
4355 /* I made the same as TCP since we are not setup? */
4356 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4357 return (ECONNRESET);
4360 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4364 if (addr->sa_family == AF_INET6) {
4365 struct sockaddr_in6 *sin6p;
4367 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4368 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4371 sin6p = (struct sockaddr_in6 *)addr;
4372 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4373 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4378 if (addr->sa_family == AF_INET) {
4379 struct sockaddr_in *sinp;
4381 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4382 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4385 sinp = (struct sockaddr_in *)addr;
4386 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4387 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4391 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4392 return (EAFNOSUPPORT);
4394 SCTP_INP_INCR_REF(inp);
4395 SCTP_ASOC_CREATE_LOCK(inp);
4399 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4400 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4401 /* Should I really unlock ? */
4402 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4407 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4408 (addr->sa_family == AF_INET6)) {
4409 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4414 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4415 SCTP_PCB_FLAGS_UNBOUND) {
4416 /* Bind a ephemeral port */
4417 error = sctp_inpcb_bind(so, NULL, NULL, p);
4422 /* Now do we connect? */
4423 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4424 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4425 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4429 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4430 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4431 /* We are already connected AND the TCP model */
4432 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4436 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4437 SCTP_INP_RLOCK(inp);
4438 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4439 SCTP_INP_RUNLOCK(inp);
4442 * We increment here since sctp_findassociation_ep_addr()
4443 * will do a decrement if it finds the stcb as long as the
4444 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4446 SCTP_INP_INCR_REF(inp);
4447 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4449 SCTP_INP_DECR_REF(inp);
4451 SCTP_TCB_UNLOCK(stcb);
4455 /* Already have or am bring up an association */
4456 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4460 vrf_id = inp->def_vrf_id;
4461 /* We are GOOD to go */
4462 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4464 /* Gak! no memory */
4467 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4468 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4469 /* Set the connected flag so we can queue data */
4470 SOCKBUF_LOCK(&so->so_rcv);
4471 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4472 SOCKBUF_UNLOCK(&so->so_rcv);
4473 SOCKBUF_LOCK(&so->so_snd);
4474 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4475 SOCKBUF_UNLOCK(&so->so_snd);
4477 so->so_state &= ~SS_ISDISCONNECTING;
4481 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4482 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4484 /* initialize authentication parameters for the assoc */
4485 sctp_initialize_auth_params(inp, stcb);
4487 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4488 SCTP_TCB_UNLOCK(stcb);
4490 if (create_lock_on) {
4491 SCTP_ASOC_CREATE_UNLOCK(inp);
4493 SCTP_INP_DECR_REF(inp);
4498 sctp_listen(struct socket *so, int backlog, struct thread *p)
4501 * Note this module depends on the protocol processing being called
4502 * AFTER any socket level flags and backlog are applied to the
4503 * socket. The traditional way that the socket flags are applied is
4504 * AFTER protocol processing. We have made a change to the
4505 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4506 * place if the socket API for SCTP is to work properly.
4510 struct sctp_inpcb *inp;
4512 inp = (struct sctp_inpcb *)so->so_pcb;
4514 /* I made the same as TCP since we are not setup? */
4515 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4516 return (ECONNRESET);
4518 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4519 /* See if we have a listener */
4520 struct sctp_inpcb *tinp;
4521 union sctp_sockstore store, *sp;
4524 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4526 struct sctp_laddr *laddr;
4528 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4529 memcpy(&store, &laddr->ifa->address, sizeof(store));
4530 sp->sin.sin_port = inp->sctp_lport;
4531 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4532 if (tinp && (tinp != inp) &&
4533 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4534 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4535 (tinp->sctp_socket->so_qlimit)) {
4537 * we have a listener already and
4540 SCTP_INP_DECR_REF(tinp);
4541 return (EADDRINUSE);
4543 SCTP_INP_DECR_REF(tinp);
4547 /* Setup a local addr bound all */
4548 memset(&store, 0, sizeof(store));
4549 store.sin.sin_port = inp->sctp_lport;
4551 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4552 store.sa.sa_family = AF_INET6;
4553 store.sa.sa_len = sizeof(struct sockaddr_in6);
4556 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4557 store.sa.sa_family = AF_INET;
4558 store.sa.sa_len = sizeof(struct sockaddr_in);
4560 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4561 if (tinp && (tinp != inp) &&
4562 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4563 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4564 (tinp->sctp_socket->so_qlimit)) {
4566 * we have a listener already and its not
4569 SCTP_INP_DECR_REF(tinp);
4570 return (EADDRINUSE);
4572 SCTP_INP_DECR_REF(inp);
4576 SCTP_INP_RLOCK(inp);
4577 #ifdef SCTP_LOCK_LOGGING
4578 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4579 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4583 error = solisten_proto_check(so);
4586 SCTP_INP_RUNLOCK(inp);
4589 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4590 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4592 * The unlucky case - We are in the tcp pool with this guy.
4593 * - Someone else is in the main inp slot. - We must move
4594 * this guy (the listener) to the main slot - We must then
4595 * move the guy that was listener to the TCP Pool.
4597 if (sctp_swap_inpcb_for_listen(inp)) {
4601 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4602 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4603 /* We are already connected AND the TCP model */
4605 SCTP_INP_RUNLOCK(inp);
4607 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4608 return (EADDRINUSE);
4610 SCTP_INP_RUNLOCK(inp);
4611 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4612 /* We must do a bind. */
4614 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4615 /* bind error, probably perm */
4620 /* It appears for 7.0 and on, we must always call this. */
4621 solisten_proto(so, backlog);
4622 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4623 /* remove the ACCEPTCONN flag for one-to-many sockets */
4624 so->so_options &= ~SO_ACCEPTCONN;
4627 /* turning off listen */
4628 so->so_options &= ~SO_ACCEPTCONN;
4634 static int sctp_defered_wakeup_cnt = 0;
4637 sctp_accept(struct socket *so, struct sockaddr **addr)
4639 struct sctp_tcb *stcb;
4640 struct sctp_inpcb *inp;
4641 union sctp_sockstore store;
4647 inp = (struct sctp_inpcb *)so->so_pcb;
4650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4651 return (ECONNRESET);
4653 SCTP_INP_RLOCK(inp);
4654 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4655 SCTP_INP_RUNLOCK(inp);
4656 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4657 return (EOPNOTSUPP);
4659 if (so->so_state & SS_ISDISCONNECTED) {
4660 SCTP_INP_RUNLOCK(inp);
4661 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4662 return (ECONNABORTED);
4664 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4666 SCTP_INP_RUNLOCK(inp);
4667 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4668 return (ECONNRESET);
4670 SCTP_TCB_LOCK(stcb);
4671 SCTP_INP_RUNLOCK(inp);
4672 store = stcb->asoc.primary_destination->ro._l_addr;
4673 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4674 SCTP_TCB_UNLOCK(stcb);
4675 switch (store.sa.sa_family) {
4678 struct sockaddr_in *sin;
4680 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4681 sin->sin_family = AF_INET;
4682 sin->sin_len = sizeof(*sin);
4683 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4684 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4685 *addr = (struct sockaddr *)sin;
4691 struct sockaddr_in6 *sin6;
4693 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4694 sin6->sin6_family = AF_INET6;
4695 sin6->sin6_len = sizeof(*sin6);
4696 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4698 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4699 if ((error = sa6_recoverscope(sin6)) != 0) {
4700 SCTP_FREE_SONAME(sin6);
4703 *addr = (struct sockaddr *)sin6;
4711 /* Wake any delayed sleep action */
4712 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4713 SCTP_INP_WLOCK(inp);
4714 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4715 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4716 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4717 SCTP_INP_WUNLOCK(inp);
4718 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4719 if (sowriteable(inp->sctp_socket)) {
4720 sowwakeup_locked(inp->sctp_socket);
4722 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4724 SCTP_INP_WLOCK(inp);
4726 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4727 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4728 SCTP_INP_WUNLOCK(inp);
4729 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4730 if (soreadable(inp->sctp_socket)) {
4731 sctp_defered_wakeup_cnt++;
4732 sorwakeup_locked(inp->sctp_socket);
4734 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4736 SCTP_INP_WLOCK(inp);
4738 SCTP_INP_WUNLOCK(inp);
4740 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4741 SCTP_TCB_LOCK(stcb);
4742 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4748 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4750 struct sockaddr_in *sin;
4752 struct sctp_inpcb *inp;
4753 struct sctp_ifa *sctp_ifa;
4756 * Do the malloc first in case it blocks.
4758 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4759 sin->sin_family = AF_INET;
4760 sin->sin_len = sizeof(*sin);
4761 inp = (struct sctp_inpcb *)so->so_pcb;
4763 SCTP_FREE_SONAME(sin);
4764 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4767 SCTP_INP_RLOCK(inp);
4768 sin->sin_port = inp->sctp_lport;
4769 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4770 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4771 struct sctp_tcb *stcb;
4772 struct sockaddr_in *sin_a;
4773 struct sctp_nets *net;
4776 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4782 SCTP_TCB_LOCK(stcb);
4783 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4784 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4786 /* this will make coverity happy */
4789 if (sin_a->sin_family == AF_INET) {
4794 if ((!fnd) || (sin_a == NULL)) {
4796 SCTP_TCB_UNLOCK(stcb);
4799 vrf_id = inp->def_vrf_id;
4800 sctp_ifa = sctp_source_address_selection(inp,
4802 (sctp_route_t *) & net->ro,
4805 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4806 sctp_free_ifa(sctp_ifa);
4808 SCTP_TCB_UNLOCK(stcb);
4810 /* For the bound all case you get back 0 */
4812 sin->sin_addr.s_addr = 0;
4816 /* Take the first IPv4 address in the list */
4817 struct sctp_laddr *laddr;
4820 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4821 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4822 struct sockaddr_in *sin_a;
4824 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4825 sin->sin_addr = sin_a->sin_addr;
4831 SCTP_FREE_SONAME(sin);
4832 SCTP_INP_RUNLOCK(inp);
4833 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4837 SCTP_INP_RUNLOCK(inp);
4838 (*addr) = (struct sockaddr *)sin;
4843 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4845 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4847 struct sockaddr_in *sin_a;
4848 struct sctp_inpcb *inp;
4849 struct sctp_tcb *stcb;
4850 struct sctp_nets *net;
4852 /* Do the malloc first in case it blocks. */
4853 inp = (struct sctp_inpcb *)so->so_pcb;
4854 if ((inp == NULL) ||
4855 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4856 /* UDP type and listeners will drop out here */
4857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4860 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4861 sin->sin_family = AF_INET;
4862 sin->sin_len = sizeof(*sin);
4864 /* We must recapture incase we blocked */
4865 inp = (struct sctp_inpcb *)so->so_pcb;
4867 SCTP_FREE_SONAME(sin);
4868 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4871 SCTP_INP_RLOCK(inp);
4872 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4874 SCTP_TCB_LOCK(stcb);
4876 SCTP_INP_RUNLOCK(inp);
4878 SCTP_FREE_SONAME(sin);
4879 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4883 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4884 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4885 if (sin_a->sin_family == AF_INET) {
4887 sin->sin_port = stcb->rport;
4888 sin->sin_addr = sin_a->sin_addr;
4892 SCTP_TCB_UNLOCK(stcb);
4894 /* No IPv4 address */
4895 SCTP_FREE_SONAME(sin);
4896 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4899 (*addr) = (struct sockaddr *)sin;
4903 struct pr_usrreqs sctp_usrreqs = {
4904 .pru_abort = sctp_abort,
4905 .pru_accept = sctp_accept,
4906 .pru_attach = sctp_attach,
4907 .pru_bind = sctp_bind,
4908 .pru_connect = sctp_connect,
4909 .pru_control = in_control,
4910 .pru_close = sctp_close,
4911 .pru_detach = sctp_close,
4912 .pru_sopoll = sopoll_generic,
4913 .pru_flush = sctp_flush,
4914 .pru_disconnect = sctp_disconnect,
4915 .pru_listen = sctp_listen,
4916 .pru_peeraddr = sctp_peeraddr,
4917 .pru_send = sctp_sendm,
4918 .pru_shutdown = sctp_shutdown,
4919 .pru_sockaddr = sctp_ingetaddr,
4920 .pru_sosend = sctp_sosend,
4921 .pru_soreceive = sctp_soreceive