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(MOD_INET, ip_defttl);
549 error = ipsec_init_policy(so, &ip_inp->inp_sp);
550 #ifdef SCTP_LOG_CLOSING
551 sctp_log_closing(inp, NULL, 17);
554 flags = inp->sctp_flags;
555 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
556 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
557 #ifdef SCTP_LOG_CLOSING
558 sctp_log_closing(inp, NULL, 15);
560 SCTP_INP_WUNLOCK(inp);
561 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
562 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
564 SCTP_INP_WUNLOCK(inp);
569 SCTP_INP_WUNLOCK(inp);
574 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
576 struct sctp_inpcb *inp = NULL;
580 if (addr && addr->sa_family != AF_INET) {
581 /* must be a v4 address! */
582 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
586 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
587 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
590 inp = (struct sctp_inpcb *)so->so_pcb;
592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
595 error = sctp_inpcb_bind(so, addr, NULL, p);
600 sctp_close(struct socket *so)
602 struct sctp_inpcb *inp;
605 inp = (struct sctp_inpcb *)so->so_pcb;
610 * Inform all the lower layer assoc that we are done.
613 flags = inp->sctp_flags;
614 #ifdef SCTP_LOG_CLOSING
615 sctp_log_closing(inp, NULL, 17);
617 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
618 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
619 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
620 (so->so_rcv.sb_cc > 0)) {
621 #ifdef SCTP_LOG_CLOSING
622 sctp_log_closing(inp, NULL, 13);
624 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
625 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
627 #ifdef SCTP_LOG_CLOSING
628 sctp_log_closing(inp, NULL, 14);
630 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
631 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
634 * The socket is now detached, no matter what the state of
635 * the SCTP association.
638 SCTP_SB_CLEAR(so->so_snd);
640 * same for the rcv ones, they are only here for the
643 SCTP_SB_CLEAR(so->so_rcv);
645 /* Now null out the reference, we are completely detached. */
649 flags = inp->sctp_flags;
650 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
651 goto sctp_must_try_again;
659 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
660 struct mbuf *control, struct thread *p);
664 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
665 struct mbuf *control, struct thread *p)
667 struct sctp_inpcb *inp;
670 inp = (struct sctp_inpcb *)so->so_pcb;
673 sctp_m_freem(control);
676 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
680 /* Got to have an to address if we are NOT a connected socket */
681 if ((addr == NULL) &&
682 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
683 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
686 } else if (addr == NULL) {
687 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
688 error = EDESTADDRREQ;
691 sctp_m_freem(control);
697 if (addr->sa_family != AF_INET) {
698 /* must be a v4 address! */
699 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
702 sctp_m_freem(control);
705 error = EDESTADDRREQ;
710 /* now what about control */
713 SCTP_PRINTF("huh? control set?\n");
714 sctp_m_freem(inp->control);
717 inp->control = control;
721 SCTP_BUF_NEXT(inp->pkt_last) = m;
724 inp->pkt_last = inp->pkt = m;
727 /* FreeBSD uses a flag passed */
728 ((flags & PRUS_MORETOCOME) == 0)
731 * note with the current version this code will only be used
732 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
733 * re-defining sosend to use the sctp_sosend. One can
734 * optionally switch back to this code (by changing back the
735 * definitions) but this is not advisable. This code is used
736 * by FreeBSD when sending a file with sendfile() though.
740 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
750 sctp_disconnect(struct socket *so)
752 struct sctp_inpcb *inp;
754 inp = (struct sctp_inpcb *)so->so_pcb;
756 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
760 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
761 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
762 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
764 SCTP_INP_RUNLOCK(inp);
767 struct sctp_association *asoc;
768 struct sctp_tcb *stcb;
770 stcb = LIST_FIRST(&inp->sctp_asoc_list);
772 SCTP_INP_RUNLOCK(inp);
773 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
778 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
779 /* We are about to be freed, out of here */
780 SCTP_TCB_UNLOCK(stcb);
781 SCTP_INP_RUNLOCK(inp);
784 if (((so->so_options & SO_LINGER) &&
785 (so->so_linger == 0)) ||
786 (so->so_rcv.sb_cc > 0)) {
787 if (SCTP_GET_STATE(asoc) !=
788 SCTP_STATE_COOKIE_WAIT) {
789 /* Left with Data unread */
792 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
798 struct sctp_paramhdr *ph;
800 ph = mtod(err, struct sctp_paramhdr *);
801 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
802 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
803 ph->param_length = htons(SCTP_BUF_LEN(err));
805 #if defined(SCTP_PANIC_ON_ABORT)
806 panic("disconnect does an abort");
808 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
809 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
811 SCTP_INP_RUNLOCK(inp);
812 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
813 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
814 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
816 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
817 /* No unlock tcb assoc is gone */
820 if (TAILQ_EMPTY(&asoc->send_queue) &&
821 TAILQ_EMPTY(&asoc->sent_queue) &&
822 (asoc->stream_queue_cnt == 0)) {
823 /* there is nothing queued to send, so done */
824 if (asoc->locked_on_sending) {
827 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
828 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
829 /* only send SHUTDOWN 1st time thru */
830 sctp_stop_timers_for_shutdown(stcb);
831 sctp_send_shutdown(stcb,
832 stcb->asoc.primary_destination);
833 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
834 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
835 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
836 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
838 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
839 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
840 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
842 asoc->primary_destination);
843 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
845 asoc->primary_destination);
849 * we still got (or just got) data to send,
850 * so set SHUTDOWN_PENDING
853 * XXX sockets draft says that SCTP_EOF
854 * should be sent with no data. currently,
855 * we will allow user data to be sent first
856 * and move to SHUTDOWN-PENDING
858 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
859 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
860 asoc->primary_destination);
861 if (asoc->locked_on_sending) {
862 /* Locked to send out the data */
863 struct sctp_stream_queue_pending *sp;
865 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
867 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
868 asoc->locked_on_sending->stream_no);
870 if ((sp->length == 0) && (sp->msg_is_complete == 0))
871 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
874 if (TAILQ_EMPTY(&asoc->send_queue) &&
875 TAILQ_EMPTY(&asoc->sent_queue) &&
876 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
880 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
881 0, M_DONTWAIT, 1, MT_DATA);
887 struct sctp_paramhdr *ph;
890 SCTP_BUF_LEN(op_err) =
891 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
893 struct sctp_paramhdr *);
894 ph->param_type = htons(
895 SCTP_CAUSE_USER_INITIATED_ABT);
896 ph->param_length = htons(SCTP_BUF_LEN(op_err));
897 ippp = (uint32_t *) (ph + 1);
898 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
900 #if defined(SCTP_PANIC_ON_ABORT)
901 panic("disconnect does an abort");
904 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
905 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
906 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
907 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
908 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
909 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
911 SCTP_INP_RUNLOCK(inp);
912 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
915 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
918 soisdisconnecting(so);
919 SCTP_TCB_UNLOCK(stcb);
920 SCTP_INP_RUNLOCK(inp);
925 /* UDP model does not support this */
926 SCTP_INP_RUNLOCK(inp);
927 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
933 sctp_flush(struct socket *so, int how)
936 * We will just clear out the values and let subsequent close clear
937 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
938 * they will not be able to read the data, the socket will block
939 * that from happening.
941 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
943 * First make sure the sb will be happy, we don't use these
944 * except maybe the count
946 so->so_rcv.sb_cc = 0;
947 so->so_rcv.sb_mbcnt = 0;
948 so->so_rcv.sb_mb = NULL;
950 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
952 * First make sure the sb will be happy, we don't use these
953 * except maybe the count
955 so->so_snd.sb_cc = 0;
956 so->so_snd.sb_mbcnt = 0;
957 so->so_snd.sb_mb = NULL;
964 sctp_shutdown(struct socket *so)
966 struct sctp_inpcb *inp;
968 inp = (struct sctp_inpcb *)so->so_pcb;
970 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
974 /* For UDP model this is a invalid call */
975 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
976 /* Restore the flags that the soshutdown took away. */
977 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
978 /* This proc will wakeup for read and do nothing (I hope) */
979 SCTP_INP_RUNLOCK(inp);
980 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
984 * Ok if we reach here its the TCP model and it is either a SHUT_WR
985 * or SHUT_RDWR. This means we put the shutdown flag against it.
988 struct sctp_tcb *stcb;
989 struct sctp_association *asoc;
992 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
993 SCTP_INP_RUNLOCK(inp);
998 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1001 * Ok we hit the case that the shutdown call was
1002 * made after an abort or something. Nothing to do
1005 SCTP_INP_RUNLOCK(inp);
1008 SCTP_TCB_LOCK(stcb);
1010 if (TAILQ_EMPTY(&asoc->send_queue) &&
1011 TAILQ_EMPTY(&asoc->sent_queue) &&
1012 (asoc->stream_queue_cnt == 0)) {
1013 if (asoc->locked_on_sending) {
1016 /* there is nothing queued to send, so I'm done... */
1017 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1018 /* only send SHUTDOWN the first time through */
1019 sctp_stop_timers_for_shutdown(stcb);
1020 sctp_send_shutdown(stcb,
1021 stcb->asoc.primary_destination);
1022 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1023 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1024 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1025 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1027 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1028 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1029 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1030 stcb->sctp_ep, stcb,
1031 asoc->primary_destination);
1032 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1033 stcb->sctp_ep, stcb,
1034 asoc->primary_destination);
1038 * we still got (or just got) data to send, so set
1041 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1042 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1043 asoc->primary_destination);
1045 if (asoc->locked_on_sending) {
1046 /* Locked to send out the data */
1047 struct sctp_stream_queue_pending *sp;
1049 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1051 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1052 asoc->locked_on_sending->stream_no);
1054 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1055 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1059 if (TAILQ_EMPTY(&asoc->send_queue) &&
1060 TAILQ_EMPTY(&asoc->sent_queue) &&
1061 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1062 struct mbuf *op_err;
1065 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1066 0, M_DONTWAIT, 1, MT_DATA);
1068 /* Fill in the user initiated abort */
1069 struct sctp_paramhdr *ph;
1072 SCTP_BUF_LEN(op_err) =
1073 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1075 struct sctp_paramhdr *);
1076 ph->param_type = htons(
1077 SCTP_CAUSE_USER_INITIATED_ABT);
1078 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1079 ippp = (uint32_t *) (ph + 1);
1080 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1082 #if defined(SCTP_PANIC_ON_ABORT)
1083 panic("shutdown does an abort");
1085 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1086 sctp_abort_an_association(stcb->sctp_ep, stcb,
1087 SCTP_RESPONSE_TO_USER_REQ,
1088 op_err, SCTP_SO_LOCKED);
1091 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1094 SCTP_TCB_UNLOCK(stcb);
1097 SCTP_INP_RUNLOCK(inp);
1102 * copies a "user" presentable address and removes embedded scope, etc.
1103 * returns 0 on success, 1 on error
1106 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1109 struct sockaddr_in6 lsa6;
1111 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1114 memcpy(ss, sa, sa->sa_len);
1121 * NOTE: assumes addr lock is held
1124 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1125 struct sctp_tcb *stcb,
1127 struct sockaddr_storage *sas,
1130 struct sctp_ifn *sctp_ifn;
1131 struct sctp_ifa *sctp_ifa;
1132 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1134 int ipv4_addr_legal, ipv6_addr_legal;
1135 struct sctp_vrf *vrf;
1142 /* Turn on all the appropriate scope */
1143 loopback_scope = stcb->asoc.loopback_scope;
1144 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1145 local_scope = stcb->asoc.local_scope;
1146 site_scope = stcb->asoc.site_scope;
1148 /* Turn on ALL scope, since we look at the EP */
1149 loopback_scope = ipv4_local_scope = local_scope =
1152 ipv4_addr_legal = ipv6_addr_legal = 0;
1153 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1154 ipv6_addr_legal = 1;
1155 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1156 ipv4_addr_legal = 1;
1159 ipv4_addr_legal = 1;
1161 vrf = sctp_find_vrf(vrf_id);
1165 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1166 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1167 if ((loopback_scope == 0) &&
1168 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1169 /* Skip loopback if loopback_scope not set */
1172 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1175 * For the BOUND-ALL case, the list
1176 * associated with a TCB is Always
1177 * considered a reverse list.. i.e.
1178 * it lists addresses that are NOT
1179 * part of the association. If this
1180 * is one of those we must skip it.
1182 if (sctp_is_addr_restricted(stcb,
1187 switch (sctp_ifa->address.sa.sa_family) {
1189 if (ipv4_addr_legal) {
1190 struct sockaddr_in *sin;
1192 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1193 if (sin->sin_addr.s_addr == 0) {
1201 if ((ipv4_local_scope == 0) &&
1202 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1206 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1207 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1208 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1209 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1210 actual += sizeof(struct sockaddr_in6);
1213 memcpy(sas, sin, sizeof(*sin));
1214 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1215 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1216 actual += sizeof(*sin);
1220 if (actual >= limit) {
1229 if (ipv6_addr_legal) {
1230 struct sockaddr_in6 *sin6;
1232 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1233 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1241 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1242 if (local_scope == 0)
1244 if (sin6->sin6_scope_id == 0) {
1245 if (sa6_recoverscope(sin6) != 0)
1263 if ((site_scope == 0) &&
1264 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1267 memcpy(sas, sin6, sizeof(*sin6));
1268 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1269 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1270 actual += sizeof(*sin6);
1271 if (actual >= limit) {
1286 struct sctp_laddr *laddr;
1288 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1290 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1294 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1297 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1298 sas = (struct sockaddr_storage *)((caddr_t)sas +
1299 laddr->ifa->address.sa.sa_len);
1300 actual += laddr->ifa->address.sa.sa_len;
1301 if (actual >= limit) {
1310 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1311 struct sctp_tcb *stcb,
1313 struct sockaddr_storage *sas)
1317 SCTP_IPI_ADDR_RLOCK();
1318 /* fill up addresses for the endpoint's default vrf */
1319 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1321 SCTP_IPI_ADDR_RUNLOCK();
1326 * NOTE: assumes addr lock is held
1329 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1332 struct sctp_vrf *vrf = NULL;
1335 * In both sub-set bound an bound_all cases we return the MAXIMUM
1336 * number of addresses that you COULD get. In reality the sub-set
1337 * bound may have an exclusion list for a given TCB OR in the
1338 * bound-all case a TCB may NOT include the loopback or other
1339 * addresses as well.
1341 vrf = sctp_find_vrf(vrf_id);
1345 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1346 struct sctp_ifn *sctp_ifn;
1347 struct sctp_ifa *sctp_ifa;
1349 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1350 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1351 /* Count them if they are the right type */
1352 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1353 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1354 cnt += sizeof(struct sockaddr_in6);
1356 cnt += sizeof(struct sockaddr_in);
1358 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1359 cnt += sizeof(struct sockaddr_in6);
1363 struct sctp_laddr *laddr;
1365 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1366 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1367 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1368 cnt += sizeof(struct sockaddr_in6);
1370 cnt += sizeof(struct sockaddr_in);
1372 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1373 cnt += sizeof(struct sockaddr_in6);
1380 sctp_count_max_addresses(struct sctp_inpcb *inp)
1384 SCTP_IPI_ADDR_RLOCK();
1385 /* count addresses for the endpoint's default VRF */
1386 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1387 SCTP_IPI_ADDR_RUNLOCK();
1392 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1393 size_t optsize, void *p, int delay)
1396 int creat_lock_on = 0;
1397 struct sctp_tcb *stcb = NULL;
1398 struct sockaddr *sa;
1399 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1402 int bad_addresses = 0;
1405 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1407 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1408 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1409 /* We are already connected AND the TCP model */
1410 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1411 return (EADDRINUSE);
1413 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1414 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1415 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1418 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1419 SCTP_INP_RLOCK(inp);
1420 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1421 SCTP_INP_RUNLOCK(inp);
1424 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1427 SCTP_INP_INCR_REF(inp);
1428 SCTP_ASOC_CREATE_LOCK(inp);
1430 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1431 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1432 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1436 totaddrp = (int *)optval;
1437 totaddr = *totaddrp;
1438 sa = (struct sockaddr *)(totaddrp + 1);
1439 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1440 if ((stcb != NULL) || bad_addresses) {
1441 /* Already have or am bring up an association */
1442 SCTP_ASOC_CREATE_UNLOCK(inp);
1445 SCTP_TCB_UNLOCK(stcb);
1446 if (bad_addresses == 0) {
1447 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1453 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1458 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1460 struct in6pcb *inp6;
1462 inp6 = (struct in6pcb *)inp;
1463 if (SCTP_IPV6_V6ONLY(inp6)) {
1465 * if IPV6_V6ONLY flag, ignore connections destined
1466 * to a v4 addr or v4-mapped addr
1468 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1474 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1475 SCTP_PCB_FLAGS_UNBOUND) {
1476 /* Bind a ephemeral port */
1477 error = sctp_inpcb_bind(so, NULL, NULL, p);
1482 /* FIX ME: do we want to pass in a vrf on the connect call? */
1483 vrf_id = inp->def_vrf_id;
1486 /* We are GOOD to go */
1487 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id,
1491 /* Gak! no memory */
1494 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1495 /* move to second address */
1496 if (sa->sa_family == AF_INET)
1497 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1499 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1502 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1503 /* Fill in the return id */
1505 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
1508 a_id = (sctp_assoc_t *) optval;
1509 *a_id = sctp_get_associd(stcb);
1511 /* initialize authentication parameters for the assoc */
1512 sctp_initialize_auth_params(inp, stcb);
1515 /* doing delayed connection */
1516 stcb->asoc.delayed_connection = 1;
1517 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1519 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1520 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1522 SCTP_TCB_UNLOCK(stcb);
1523 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1524 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1525 /* Set the connected flag so we can queue data */
1529 if (creat_lock_on) {
1530 SCTP_ASOC_CREATE_UNLOCK(inp);
1532 SCTP_INP_DECR_REF(inp);
1536 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1537 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1538 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1539 SCTP_INP_RLOCK(inp); \
1540 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1542 SCTP_TCB_LOCK(stcb); \
1544 SCTP_INP_RUNLOCK(inp); \
1545 } else if (assoc_id != 0) { \
1546 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1547 if (stcb == NULL) { \
1548 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1558 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1559 if (size < sizeof(type)) { \
1560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1564 destp = (type *)srcp; \
1569 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1572 struct sctp_inpcb *inp = NULL;
1574 struct sctp_tcb *stcb = NULL;
1576 if (optval == NULL) {
1577 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1580 inp = (struct sctp_inpcb *)so->so_pcb;
1582 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1589 case SCTP_AUTOCLOSE:
1590 case SCTP_EXPLICIT_EOR:
1591 case SCTP_AUTO_ASCONF:
1592 case SCTP_DISABLE_FRAGMENTS:
1593 case SCTP_I_WANT_MAPPED_V4_ADDR:
1594 case SCTP_USE_EXT_RCVINFO:
1595 SCTP_INP_RLOCK(inp);
1597 case SCTP_DISABLE_FRAGMENTS:
1598 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1600 case SCTP_I_WANT_MAPPED_V4_ADDR:
1601 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1603 case SCTP_AUTO_ASCONF:
1604 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1605 /* only valid for bound all sockets */
1606 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1608 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1613 case SCTP_EXPLICIT_EOR:
1614 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1617 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1619 case SCTP_USE_EXT_RCVINFO:
1620 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1622 case SCTP_AUTOCLOSE:
1623 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1624 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1630 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1631 error = ENOPROTOOPT;
1632 } /* end switch (sopt->sopt_name) */
1633 if (optname != SCTP_AUTOCLOSE) {
1634 /* make it an "on/off" value */
1637 if (*optsize < sizeof(val)) {
1638 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1642 SCTP_INP_RUNLOCK(inp);
1644 /* return the option value */
1645 *(int *)optval = val;
1646 *optsize = sizeof(val);
1649 case SCTP_GET_PACKET_LOG:
1651 #ifdef SCTP_PACKET_LOGGING
1655 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1656 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1659 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1664 case SCTP_REUSE_PORT:
1668 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1669 /* Can't do this for a 1-m socket */
1673 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1674 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1675 *optsize = sizeof(uint32_t);
1678 case SCTP_PARTIAL_DELIVERY_POINT:
1682 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1683 *value = inp->partial_delivery_point;
1684 *optsize = sizeof(uint32_t);
1687 case SCTP_FRAGMENT_INTERLEAVE:
1691 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1692 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1693 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1694 *value = SCTP_FRAG_LEVEL_2;
1696 *value = SCTP_FRAG_LEVEL_1;
1699 *value = SCTP_FRAG_LEVEL_0;
1701 *optsize = sizeof(uint32_t);
1704 case SCTP_CMT_ON_OFF:
1706 struct sctp_assoc_value *av;
1708 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1709 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
1710 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1712 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1713 SCTP_TCB_UNLOCK(stcb);
1716 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1720 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1721 error = ENOPROTOOPT;
1723 *optsize = sizeof(*av);
1726 /* EY - set socket option for nr_sacks */
1727 case SCTP_NR_SACK_ON_OFF:
1729 struct sctp_assoc_value *av;
1731 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1732 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
1733 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1735 av->assoc_value = stcb->asoc.sctp_nr_sack_on_off;
1736 SCTP_TCB_UNLOCK(stcb);
1739 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1743 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1744 error = ENOPROTOOPT;
1746 *optsize = sizeof(*av);
1749 /* JRS - Get socket option for pluggable congestion control */
1750 case SCTP_PLUGGABLE_CC:
1752 struct sctp_assoc_value *av;
1754 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1755 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1757 av->assoc_value = stcb->asoc.congestion_control_module;
1758 SCTP_TCB_UNLOCK(stcb);
1760 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1762 *optsize = sizeof(*av);
1765 case SCTP_GET_ADDR_LEN:
1767 struct sctp_assoc_value *av;
1769 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1772 if (av->assoc_value == AF_INET) {
1773 av->assoc_value = sizeof(struct sockaddr_in);
1778 if (av->assoc_value == AF_INET6) {
1779 av->assoc_value = sizeof(struct sockaddr_in6);
1784 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1786 *optsize = sizeof(*av);
1789 case SCTP_GET_ASSOC_NUMBER:
1791 uint32_t *value, cnt;
1793 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1795 SCTP_INP_RLOCK(inp);
1796 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1799 SCTP_INP_RUNLOCK(inp);
1801 *optsize = sizeof(uint32_t);
1805 case SCTP_GET_ASSOC_ID_LIST:
1807 struct sctp_assoc_ids *ids;
1808 unsigned int at, limit;
1810 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1812 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1813 SCTP_INP_RLOCK(inp);
1814 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1816 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1819 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1823 SCTP_INP_RUNLOCK(inp);
1824 ids->gaids_number_of_ids = at;
1825 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1830 struct sctp_assoc_value *av;
1832 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1833 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1836 av->assoc_value = stcb->asoc.context;
1837 SCTP_TCB_UNLOCK(stcb);
1839 SCTP_INP_RLOCK(inp);
1840 av->assoc_value = inp->sctp_context;
1841 SCTP_INP_RUNLOCK(inp);
1843 *optsize = sizeof(*av);
1848 uint32_t *default_vrfid;
1850 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1851 *default_vrfid = inp->def_vrf_id;
1854 case SCTP_GET_ASOC_VRF:
1856 struct sctp_assoc_value *id;
1858 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1859 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1862 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1865 id->assoc_value = stcb->asoc.vrf_id;
1868 case SCTP_GET_VRF_IDS:
1870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1874 case SCTP_GET_NONCE_VALUES:
1876 struct sctp_get_nonce_values *gnv;
1878 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1879 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1882 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1883 gnv->gn_local_tag = stcb->asoc.my_vtag;
1884 SCTP_TCB_UNLOCK(stcb);
1886 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1889 *optsize = sizeof(*gnv);
1892 case SCTP_DELAYED_SACK:
1894 struct sctp_sack_info *sack;
1896 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1897 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1899 sack->sack_delay = stcb->asoc.delayed_ack;
1900 sack->sack_freq = stcb->asoc.sack_freq;
1901 SCTP_TCB_UNLOCK(stcb);
1903 SCTP_INP_RLOCK(inp);
1904 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1905 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1906 SCTP_INP_RUNLOCK(inp);
1908 *optsize = sizeof(*sack);
1912 case SCTP_GET_SNDBUF_USE:
1914 struct sctp_sockstat *ss;
1916 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1917 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1920 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1921 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1922 stcb->asoc.size_on_all_streams);
1923 SCTP_TCB_UNLOCK(stcb);
1925 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1928 *optsize = sizeof(struct sctp_sockstat);
1931 case SCTP_MAX_BURST:
1935 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1937 SCTP_INP_RLOCK(inp);
1938 *value = inp->sctp_ep.max_burst;
1939 SCTP_INP_RUNLOCK(inp);
1940 *optsize = sizeof(uint8_t);
1945 struct sctp_assoc_value *av;
1948 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1949 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1952 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1953 SCTP_TCB_UNLOCK(stcb);
1955 SCTP_INP_RLOCK(inp);
1956 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1957 ovh = SCTP_MED_OVERHEAD;
1959 ovh = SCTP_MED_V4_OVERHEAD;
1961 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1962 av->assoc_value = 0;
1964 av->assoc_value = inp->sctp_frag_point - ovh;
1965 SCTP_INP_RUNLOCK(inp);
1967 *optsize = sizeof(struct sctp_assoc_value);
1970 case SCTP_GET_STAT_LOG:
1971 error = sctp_fill_stat_log(optval, optsize);
1975 struct sctp_event_subscribe *events;
1977 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1978 memset(events, 0, sizeof(*events));
1979 SCTP_INP_RLOCK(inp);
1980 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1981 events->sctp_data_io_event = 1;
1983 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1984 events->sctp_association_event = 1;
1986 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1987 events->sctp_address_event = 1;
1989 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1990 events->sctp_send_failure_event = 1;
1992 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1993 events->sctp_peer_error_event = 1;
1995 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1996 events->sctp_shutdown_event = 1;
1998 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1999 events->sctp_partial_delivery_event = 1;
2001 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2002 events->sctp_adaptation_layer_event = 1;
2004 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2005 events->sctp_authentication_event = 1;
2007 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2008 events->sctp_sender_dry_event = 1;
2010 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2011 events->sctp_stream_reset_events = 1;
2012 SCTP_INP_RUNLOCK(inp);
2013 *optsize = sizeof(struct sctp_event_subscribe);
2017 case SCTP_ADAPTATION_LAYER:
2021 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2023 SCTP_INP_RLOCK(inp);
2024 *value = inp->sctp_ep.adaptation_layer_indicator;
2025 SCTP_INP_RUNLOCK(inp);
2026 *optsize = sizeof(uint32_t);
2029 case SCTP_SET_INITIAL_DBG_SEQ:
2033 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2034 SCTP_INP_RLOCK(inp);
2035 *value = inp->sctp_ep.initial_sequence_debug;
2036 SCTP_INP_RUNLOCK(inp);
2037 *optsize = sizeof(uint32_t);
2040 case SCTP_GET_LOCAL_ADDR_SIZE:
2044 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2045 SCTP_INP_RLOCK(inp);
2046 *value = sctp_count_max_addresses(inp);
2047 SCTP_INP_RUNLOCK(inp);
2048 *optsize = sizeof(uint32_t);
2051 case SCTP_GET_REMOTE_ADDR_SIZE:
2055 struct sctp_nets *net;
2057 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2058 /* FIXME MT: change to sctp_assoc_value? */
2059 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2063 /* Count the sizes */
2064 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2065 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2066 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2067 size += sizeof(struct sockaddr_in6);
2068 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2069 size += sizeof(struct sockaddr_in);
2075 SCTP_TCB_UNLOCK(stcb);
2076 *value = (uint32_t) size;
2078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2081 *optsize = sizeof(uint32_t);
2084 case SCTP_GET_PEER_ADDRESSES:
2086 * Get the address information, an array is passed in to
2087 * fill up we pack it.
2091 struct sockaddr_storage *sas;
2092 struct sctp_nets *net;
2093 struct sctp_getaddresses *saddr;
2095 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2096 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2099 left = (*optsize) - sizeof(struct sctp_getaddresses);
2100 *optsize = sizeof(struct sctp_getaddresses);
2101 sas = (struct sockaddr_storage *)&saddr->addr[0];
2103 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2104 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2105 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2106 cpsz = sizeof(struct sockaddr_in6);
2107 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2108 cpsz = sizeof(struct sockaddr_in);
2114 /* not enough room. */
2118 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2119 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2120 /* Must map the address */
2121 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2122 (struct sockaddr_in6 *)sas);
2125 memcpy(sas, &net->ro._l_addr, cpsz);
2129 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2131 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2135 SCTP_TCB_UNLOCK(stcb);
2137 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2142 case SCTP_GET_LOCAL_ADDRESSES:
2144 size_t limit, actual;
2145 struct sockaddr_storage *sas;
2146 struct sctp_getaddresses *saddr;
2148 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2149 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2151 sas = (struct sockaddr_storage *)&saddr->addr[0];
2152 limit = *optsize - sizeof(sctp_assoc_t);
2153 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2155 SCTP_TCB_UNLOCK(stcb);
2157 *optsize = sizeof(struct sockaddr_storage) + actual;
2160 case SCTP_PEER_ADDR_PARAMS:
2162 struct sctp_paddrparams *paddrp;
2163 struct sctp_nets *net;
2165 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2166 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2170 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2173 * We increment here since
2174 * sctp_findassociation_ep_addr() wil do a
2175 * decrement if it finds the stcb as long as
2176 * the locked tcb (last argument) is NOT a
2179 SCTP_INP_INCR_REF(inp);
2180 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2182 SCTP_INP_DECR_REF(inp);
2185 if (stcb && (net == NULL)) {
2186 struct sockaddr *sa;
2188 sa = (struct sockaddr *)&paddrp->spp_address;
2189 if (sa->sa_family == AF_INET) {
2190 struct sockaddr_in *sin;
2192 sin = (struct sockaddr_in *)sa;
2193 if (sin->sin_addr.s_addr) {
2195 SCTP_TCB_UNLOCK(stcb);
2196 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2199 } else if (sa->sa_family == AF_INET6) {
2200 struct sockaddr_in6 *sin6;
2202 sin6 = (struct sockaddr_in6 *)sa;
2203 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2205 SCTP_TCB_UNLOCK(stcb);
2206 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2210 error = EAFNOSUPPORT;
2211 SCTP_TCB_UNLOCK(stcb);
2212 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2217 /* Applys to the specific association */
2218 paddrp->spp_flags = 0;
2222 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2223 ovh = SCTP_MED_OVERHEAD;
2225 ovh = SCTP_MED_V4_OVERHEAD;
2229 paddrp->spp_pathmaxrxt = net->failure_threshold;
2230 paddrp->spp_pathmtu = net->mtu - ovh;
2231 /* get flags for HB */
2232 if (net->dest_state & SCTP_ADDR_NOHB)
2233 paddrp->spp_flags |= SPP_HB_DISABLE;
2235 paddrp->spp_flags |= SPP_HB_ENABLE;
2236 /* get flags for PMTU */
2237 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2238 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2240 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2243 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2244 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2245 paddrp->spp_flags |= SPP_IPV4_TOS;
2249 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2250 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2251 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2256 * No destination so return default
2261 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2262 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2264 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2265 paddrp->spp_flags |= SPP_IPV4_TOS;
2268 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2269 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2271 /* default settings should be these */
2272 if (stcb->asoc.hb_is_disabled == 0) {
2273 paddrp->spp_flags |= SPP_HB_ENABLE;
2275 paddrp->spp_flags |= SPP_HB_DISABLE;
2277 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2278 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2283 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2286 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2287 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2288 SCTP_TCB_UNLOCK(stcb);
2290 /* Use endpoint defaults */
2291 SCTP_INP_RLOCK(inp);
2292 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2293 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2294 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2295 /* get inp's default */
2297 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2298 paddrp->spp_flags |= SPP_IPV4_TOS;
2301 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2302 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2303 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2306 /* can't return this */
2307 paddrp->spp_pathmtu = 0;
2309 /* default behavior, no stcb */
2310 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2312 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2313 paddrp->spp_flags |= SPP_HB_ENABLE;
2315 paddrp->spp_flags |= SPP_HB_DISABLE;
2317 SCTP_INP_RUNLOCK(inp);
2319 *optsize = sizeof(struct sctp_paddrparams);
2322 case SCTP_GET_PEER_ADDR_INFO:
2324 struct sctp_paddrinfo *paddri;
2325 struct sctp_nets *net;
2327 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2328 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2332 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2335 * We increment here since
2336 * sctp_findassociation_ep_addr() wil do a
2337 * decrement if it finds the stcb as long as
2338 * the locked tcb (last argument) is NOT a
2341 SCTP_INP_INCR_REF(inp);
2342 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2344 SCTP_INP_DECR_REF(inp);
2348 if ((stcb) && (net)) {
2349 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2350 paddri->spinfo_cwnd = net->cwnd;
2351 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2352 paddri->spinfo_rto = net->RTO;
2353 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2354 SCTP_TCB_UNLOCK(stcb);
2357 SCTP_TCB_UNLOCK(stcb);
2359 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2362 *optsize = sizeof(struct sctp_paddrinfo);
2365 case SCTP_PCB_STATUS:
2367 struct sctp_pcbinfo *spcb;
2369 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2370 sctp_fill_pcbinfo(spcb);
2371 *optsize = sizeof(struct sctp_pcbinfo);
2377 struct sctp_nets *net;
2378 struct sctp_status *sstat;
2380 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2381 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2384 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2389 * I think passing the state is fine since
2390 * sctp_constants.h will be available to the user
2393 sstat->sstat_state = stcb->asoc.state;
2394 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2395 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2396 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2398 * We can't include chunks that have been passed to
2399 * the socket layer. Only things in queue.
2401 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2402 stcb->asoc.cnt_on_all_streams);
2405 sstat->sstat_instrms = stcb->asoc.streamincnt;
2406 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2407 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2408 memcpy(&sstat->sstat_primary.spinfo_address,
2409 &stcb->asoc.primary_destination->ro._l_addr,
2410 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2411 net = stcb->asoc.primary_destination;
2412 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2414 * Again the user can get info from sctp_constants.h
2415 * for what the state of the network is.
2417 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2418 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2419 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2420 sstat->sstat_primary.spinfo_rto = net->RTO;
2421 sstat->sstat_primary.spinfo_mtu = net->mtu;
2422 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2423 SCTP_TCB_UNLOCK(stcb);
2424 *optsize = sizeof(*sstat);
2429 struct sctp_rtoinfo *srto;
2431 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2432 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2435 srto->srto_initial = stcb->asoc.initial_rto;
2436 srto->srto_max = stcb->asoc.maxrto;
2437 srto->srto_min = stcb->asoc.minrto;
2438 SCTP_TCB_UNLOCK(stcb);
2440 SCTP_INP_RLOCK(inp);
2441 srto->srto_initial = inp->sctp_ep.initial_rto;
2442 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2443 srto->srto_min = inp->sctp_ep.sctp_minrto;
2444 SCTP_INP_RUNLOCK(inp);
2446 *optsize = sizeof(*srto);
2449 case SCTP_ASSOCINFO:
2451 struct sctp_assocparams *sasoc;
2454 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2455 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2458 oldval = sasoc->sasoc_cookie_life;
2459 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2460 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2461 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2462 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2463 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2464 SCTP_TCB_UNLOCK(stcb);
2466 SCTP_INP_RLOCK(inp);
2467 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2468 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2469 sasoc->sasoc_number_peer_destinations = 0;
2470 sasoc->sasoc_peer_rwnd = 0;
2471 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2472 SCTP_INP_RUNLOCK(inp);
2474 *optsize = sizeof(*sasoc);
2477 case SCTP_DEFAULT_SEND_PARAM:
2479 struct sctp_sndrcvinfo *s_info;
2481 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2482 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2485 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2486 SCTP_TCB_UNLOCK(stcb);
2488 SCTP_INP_RLOCK(inp);
2489 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2490 SCTP_INP_RUNLOCK(inp);
2492 *optsize = sizeof(*s_info);
2497 struct sctp_initmsg *sinit;
2499 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2500 SCTP_INP_RLOCK(inp);
2501 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2502 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2503 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2504 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2505 SCTP_INP_RUNLOCK(inp);
2506 *optsize = sizeof(*sinit);
2509 case SCTP_PRIMARY_ADDR:
2510 /* we allow a "get" operation on this */
2512 struct sctp_setprim *ssp;
2514 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2515 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2518 /* simply copy out the sockaddr_storage... */
2522 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2523 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2525 memcpy(&ssp->ssp_addr,
2526 &stcb->asoc.primary_destination->ro._l_addr,
2528 SCTP_TCB_UNLOCK(stcb);
2530 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2533 *optsize = sizeof(*ssp);
2537 case SCTP_HMAC_IDENT:
2539 struct sctp_hmacalgo *shmac;
2540 sctp_hmaclist_t *hmaclist;
2544 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2546 SCTP_INP_RLOCK(inp);
2547 hmaclist = inp->sctp_ep.local_hmacs;
2548 if (hmaclist == NULL) {
2549 /* no HMACs to return */
2550 *optsize = sizeof(*shmac);
2551 SCTP_INP_RUNLOCK(inp);
2554 /* is there room for all of the hmac ids? */
2555 size = sizeof(*shmac) + (hmaclist->num_algo *
2556 sizeof(shmac->shmac_idents[0]));
2557 if ((size_t)(*optsize) < size) {
2558 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2560 SCTP_INP_RUNLOCK(inp);
2563 /* copy in the list */
2564 shmac->shmac_number_of_idents = hmaclist->num_algo;
2565 for (i = 0; i < hmaclist->num_algo; i++) {
2566 shmac->shmac_idents[i] = hmaclist->hmac[i];
2568 SCTP_INP_RUNLOCK(inp);
2572 case SCTP_AUTH_ACTIVE_KEY:
2574 struct sctp_authkeyid *scact;
2576 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2577 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2580 /* get the active key on the assoc */
2581 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2582 SCTP_TCB_UNLOCK(stcb);
2584 /* get the endpoint active key */
2585 SCTP_INP_RLOCK(inp);
2586 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2587 SCTP_INP_RUNLOCK(inp);
2589 *optsize = sizeof(*scact);
2592 case SCTP_LOCAL_AUTH_CHUNKS:
2594 struct sctp_authchunks *sac;
2595 sctp_auth_chklist_t *chklist = NULL;
2598 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2599 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2602 /* get off the assoc */
2603 chklist = stcb->asoc.local_auth_chunks;
2604 /* is there enough space? */
2605 size = sctp_auth_get_chklist_size(chklist);
2606 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2608 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2610 /* copy in the chunks */
2611 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2613 SCTP_TCB_UNLOCK(stcb);
2615 /* get off the endpoint */
2616 SCTP_INP_RLOCK(inp);
2617 chklist = inp->sctp_ep.local_auth_chunks;
2618 /* is there enough space? */
2619 size = sctp_auth_get_chklist_size(chklist);
2620 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2622 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2624 /* copy in the chunks */
2625 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2627 SCTP_INP_RUNLOCK(inp);
2629 *optsize = sizeof(struct sctp_authchunks) + size;
2632 case SCTP_PEER_AUTH_CHUNKS:
2634 struct sctp_authchunks *sac;
2635 sctp_auth_chklist_t *chklist = NULL;
2638 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2639 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2642 /* get off the assoc */
2643 chklist = stcb->asoc.peer_auth_chunks;
2644 /* is there enough space? */
2645 size = sctp_auth_get_chklist_size(chklist);
2646 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2650 /* copy in the chunks */
2651 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2653 SCTP_TCB_UNLOCK(stcb);
2655 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2658 *optsize = sizeof(struct sctp_authchunks) + size;
2664 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2665 error = ENOPROTOOPT;
2668 } /* end switch (sopt->sopt_name) */
2673 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2678 struct sctp_tcb *stcb = NULL;
2679 struct sctp_inpcb *inp = NULL;
2682 if (optval == NULL) {
2683 SCTP_PRINTF("optval is NULL\n");
2684 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2687 inp = (struct sctp_inpcb *)so->so_pcb;
2689 SCTP_PRINTF("inp is NULL?\n");
2690 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2693 vrf_id = inp->def_vrf_id;
2698 case SCTP_AUTOCLOSE:
2699 case SCTP_AUTO_ASCONF:
2700 case SCTP_EXPLICIT_EOR:
2701 case SCTP_DISABLE_FRAGMENTS:
2702 case SCTP_USE_EXT_RCVINFO:
2703 case SCTP_I_WANT_MAPPED_V4_ADDR:
2704 /* copy in the option value */
2705 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2710 case SCTP_DISABLE_FRAGMENTS:
2711 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2713 case SCTP_AUTO_ASCONF:
2715 * NOTE: we don't really support this flag
2717 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2718 /* only valid for bound all sockets */
2719 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2721 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2725 case SCTP_EXPLICIT_EOR:
2726 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2728 case SCTP_USE_EXT_RCVINFO:
2729 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2731 case SCTP_I_WANT_MAPPED_V4_ADDR:
2732 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2733 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2735 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2740 set_opt = SCTP_PCB_FLAGS_NODELAY;
2742 case SCTP_AUTOCLOSE:
2743 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2744 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2745 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2748 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2750 * The value is in ticks. Note this does not effect
2751 * old associations, only new ones.
2753 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2756 SCTP_INP_WLOCK(inp);
2758 sctp_feature_on(inp, set_opt);
2760 sctp_feature_off(inp, set_opt);
2762 SCTP_INP_WUNLOCK(inp);
2764 case SCTP_REUSE_PORT:
2766 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2767 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2768 /* Can't set it after we are bound */
2772 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2773 /* Can't do this for a 1-m socket */
2778 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2780 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2783 case SCTP_PARTIAL_DELIVERY_POINT:
2787 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2788 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2789 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2793 inp->partial_delivery_point = *value;
2796 case SCTP_FRAGMENT_INTERLEAVE:
2797 /* not yet until we re-write sctp_recvmsg() */
2801 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2802 if (*level == SCTP_FRAG_LEVEL_2) {
2803 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2804 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2805 } else if (*level == SCTP_FRAG_LEVEL_1) {
2806 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2807 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2808 } else if (*level == SCTP_FRAG_LEVEL_0) {
2809 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2810 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2813 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2818 case SCTP_CMT_ON_OFF:
2820 struct sctp_assoc_value *av;
2822 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2823 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2824 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2826 stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2827 SCTP_TCB_UNLOCK(stcb);
2829 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2833 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2834 error = ENOPROTOOPT;
2838 /* EY nr_sack_on_off socket option */
2839 case SCTP_NR_SACK_ON_OFF:
2841 struct sctp_assoc_value *av;
2843 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2844 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
2845 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2847 stcb->asoc.sctp_nr_sack_on_off = (uint8_t) av->assoc_value;
2848 SCTP_TCB_UNLOCK(stcb);
2850 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2854 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2855 error = ENOPROTOOPT;
2859 /* JRS - Set socket option for pluggable congestion control */
2860 case SCTP_PLUGGABLE_CC:
2862 struct sctp_assoc_value *av;
2864 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2865 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2867 switch (av->assoc_value) {
2869 * JRS - Standard TCP congestion
2872 case SCTP_CC_RFC2581:
2874 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2875 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2876 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2877 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2878 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2879 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2880 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2881 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2882 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2883 SCTP_TCB_UNLOCK(stcb);
2887 * JRS - High Speed TCP congestion
2892 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2893 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2894 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2895 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2896 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2897 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2898 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2899 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2900 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2901 SCTP_TCB_UNLOCK(stcb);
2904 /* JRS - HTCP congestion control */
2907 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2908 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2909 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2910 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2911 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2912 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2913 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2914 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2915 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2916 SCTP_TCB_UNLOCK(stcb);
2920 * JRS - All other values are
2925 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2927 SCTP_TCB_UNLOCK(stcb);
2932 switch (av->assoc_value) {
2933 case SCTP_CC_RFC2581:
2936 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2939 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2946 case SCTP_CLR_STAT_LOG:
2947 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2952 struct sctp_assoc_value *av;
2954 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2955 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2958 stcb->asoc.context = av->assoc_value;
2959 SCTP_TCB_UNLOCK(stcb);
2961 SCTP_INP_WLOCK(inp);
2962 inp->sctp_context = av->assoc_value;
2963 SCTP_INP_WUNLOCK(inp);
2969 uint32_t *default_vrfid;
2971 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2972 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2973 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2977 inp->def_vrf_id = *default_vrfid;
2980 case SCTP_DEL_VRF_ID:
2982 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2986 case SCTP_ADD_VRF_ID:
2988 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2992 case SCTP_DELAYED_SACK:
2994 struct sctp_sack_info *sack;
2996 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2997 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2998 if (sack->sack_delay) {
2999 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3000 sack->sack_delay = SCTP_MAX_SACK_DELAY;
3003 if (sack->sack_delay) {
3004 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3005 sack->sack_delay = TICKS_TO_MSEC(1);
3007 stcb->asoc.delayed_ack = sack->sack_delay;
3009 if (sack->sack_freq) {
3010 stcb->asoc.sack_freq = sack->sack_freq;
3012 SCTP_TCB_UNLOCK(stcb);
3014 SCTP_INP_WLOCK(inp);
3015 if (sack->sack_delay) {
3016 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3017 sack->sack_delay = TICKS_TO_MSEC(1);
3019 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3021 if (sack->sack_freq) {
3022 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3024 SCTP_INP_WUNLOCK(inp);
3028 case SCTP_AUTH_CHUNK:
3030 struct sctp_authchunk *sauth;
3032 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3034 SCTP_INP_WLOCK(inp);
3035 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3036 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3039 SCTP_INP_WUNLOCK(inp);
3044 struct sctp_authkey *sca;
3045 struct sctp_keyhead *shared_keys;
3046 sctp_sharedkey_t *shared_key;
3047 sctp_key_t *key = NULL;
3050 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3051 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3052 size = optsize - sizeof(*sca);
3055 /* set it on the assoc */
3056 shared_keys = &stcb->asoc.shared_keys;
3057 /* clear the cached keys for this key id */
3058 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3060 * create the new shared key and
3064 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3066 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3068 SCTP_TCB_UNLOCK(stcb);
3072 shared_key = sctp_alloc_sharedkey();
3073 if (shared_key == NULL) {
3075 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3077 SCTP_TCB_UNLOCK(stcb);
3080 shared_key->key = key;
3081 shared_key->keyid = sca->sca_keynumber;
3082 error = sctp_insert_sharedkey(shared_keys, shared_key);
3083 SCTP_TCB_UNLOCK(stcb);
3085 /* set it on the endpoint */
3086 SCTP_INP_WLOCK(inp);
3087 shared_keys = &inp->sctp_ep.shared_keys;
3089 * clear the cached keys on all assocs for
3092 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3094 * create the new shared key and
3098 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3102 SCTP_INP_WUNLOCK(inp);
3106 shared_key = sctp_alloc_sharedkey();
3107 if (shared_key == NULL) {
3109 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3111 SCTP_INP_WUNLOCK(inp);
3114 shared_key->key = key;
3115 shared_key->keyid = sca->sca_keynumber;
3116 error = sctp_insert_sharedkey(shared_keys, shared_key);
3117 SCTP_INP_WUNLOCK(inp);
3121 case SCTP_HMAC_IDENT:
3123 struct sctp_hmacalgo *shmac;
3124 sctp_hmaclist_t *hmaclist;
3130 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3131 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3132 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3136 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3137 if (hmaclist == NULL) {
3138 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3142 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3143 hmacid = shmac->shmac_idents[i];
3144 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3145 /* invalid HMACs were found */ ;
3146 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3148 sctp_free_hmaclist(hmaclist);
3149 goto sctp_set_hmac_done;
3153 for (i = 0; i < hmaclist->num_algo; i++) {
3154 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3155 /* already in list */
3160 sctp_free_hmaclist(hmaclist);
3161 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3165 /* set it on the endpoint */
3166 SCTP_INP_WLOCK(inp);
3167 if (inp->sctp_ep.local_hmacs)
3168 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3169 inp->sctp_ep.local_hmacs = hmaclist;
3170 SCTP_INP_WUNLOCK(inp);
3174 case SCTP_AUTH_ACTIVE_KEY:
3176 struct sctp_authkeyid *scact;
3178 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3180 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3182 /* set the active key on the right place */
3184 /* set the active key on the assoc */
3185 if (sctp_auth_setactivekey(stcb,
3186 scact->scact_keynumber)) {
3187 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3188 SCTP_FROM_SCTP_USRREQ,
3192 SCTP_TCB_UNLOCK(stcb);
3194 /* set the active key on the endpoint */
3195 SCTP_INP_WLOCK(inp);
3196 if (sctp_auth_setactivekey_ep(inp,
3197 scact->scact_keynumber)) {
3198 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3199 SCTP_FROM_SCTP_USRREQ,
3203 SCTP_INP_WUNLOCK(inp);
3207 case SCTP_AUTH_DELETE_KEY:
3209 struct sctp_authkeyid *scdel;
3211 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3213 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3215 /* delete the key from the right place */
3217 if (sctp_delete_sharedkey(stcb,
3218 scdel->scact_keynumber)) {
3219 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3220 SCTP_FROM_SCTP_USRREQ,
3224 SCTP_TCB_UNLOCK(stcb);
3226 SCTP_INP_WLOCK(inp);
3227 if (sctp_delete_sharedkey_ep(inp,
3228 scdel->scact_keynumber)) {
3229 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3230 SCTP_FROM_SCTP_USRREQ,
3234 SCTP_INP_WUNLOCK(inp);
3238 case SCTP_AUTH_DEACTIVATE_KEY:
3240 struct sctp_authkeyid *keyid;
3242 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3244 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3246 /* deactivate the key from the right place */
3248 if (sctp_deact_sharedkey(stcb,
3249 keyid->scact_keynumber)) {
3250 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3251 SCTP_FROM_SCTP_USRREQ,
3255 SCTP_TCB_UNLOCK(stcb);
3257 SCTP_INP_WLOCK(inp);
3258 if (sctp_deact_sharedkey_ep(inp,
3259 keyid->scact_keynumber)) {
3260 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3261 SCTP_FROM_SCTP_USRREQ,
3265 SCTP_INP_WUNLOCK(inp);
3270 case SCTP_RESET_STREAMS:
3272 struct sctp_stream_reset *strrst;
3273 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3275 uint16_t addstrmcnt = 0;
3278 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3279 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3282 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3286 if (stcb->asoc.peer_supports_strreset == 0) {
3288 * Peer does not support it, we return
3289 * protocol not supported since this is true
3290 * for this feature and this peer, not the
3291 * socket request in general.
3293 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3294 error = EPROTONOSUPPORT;
3295 SCTP_TCB_UNLOCK(stcb);
3298 if (stcb->asoc.stream_reset_outstanding) {
3299 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3301 SCTP_TCB_UNLOCK(stcb);
3304 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3306 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3308 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3311 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3313 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3317 /* We can't do that and add streams */
3321 if (stcb->asoc.stream_reset_outstanding) {
3326 /* We allocate here */
3327 addstrmcnt = strrst->strrst_num_streams;
3328 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3329 /* You can't have more than 64k */
3333 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3334 /* Need to allocate more */
3335 struct sctp_stream_out *oldstream;
3336 struct sctp_stream_queue_pending *sp;
3339 oldstream = stcb->asoc.strmout;
3341 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3342 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3344 if (stcb->asoc.strmout == NULL) {
3345 stcb->asoc.strmout = oldstream;
3350 * Ok now we proceed with copying
3351 * the old out stuff and
3352 * initializing the new stuff.
3354 SCTP_TCB_SEND_LOCK(stcb);
3355 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3356 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3357 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3358 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3359 stcb->asoc.strmout[i].stream_no = i;
3360 if (oldstream[i].next_spoke.tqe_next) {
3361 sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3362 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3363 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3366 /* not on out wheel */
3367 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3368 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3372 * now anything on those
3375 while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
3376 sp = TAILQ_FIRST(&oldstream[i].outqueue);
3377 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3378 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3380 /* Did we disrupt the wheel? */
3382 sctp_insert_on_wheel(stcb,
3384 &stcb->asoc.strmout[i],
3388 * Now move assoc pointers
3391 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3392 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3394 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3395 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3398 /* now the new streams */
3399 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3400 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3401 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3402 stcb->asoc.strmout[i].stream_no = i;
3403 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3404 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3405 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3407 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3408 SCTP_FREE(oldstream, SCTP_M_STRMO);
3410 SCTP_TCB_SEND_UNLOCK(stcb);
3413 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3415 SCTP_TCB_UNLOCK(stcb);
3418 for (i = 0; i < strrst->strrst_num_streams; i++) {
3421 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3422 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3427 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3428 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3436 SCTP_TCB_UNLOCK(stcb);
3439 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3440 strrst->strrst_list,
3441 send_out, (stcb->asoc.str_reset_seq_in - 3),
3442 send_in, send_tsn, addstream, addstrmcnt);
3444 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3445 SCTP_TCB_UNLOCK(stcb);
3449 case SCTP_CONNECT_X:
3450 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3451 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3455 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3458 case SCTP_CONNECT_X_DELAYED:
3459 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3460 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3464 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3467 case SCTP_CONNECT_X_COMPLETE:
3469 struct sockaddr *sa;
3470 struct sctp_nets *net;
3472 /* FIXME MT: check correct? */
3473 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3476 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3477 SCTP_INP_RLOCK(inp);
3478 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3480 SCTP_TCB_LOCK(stcb);
3481 net = sctp_findnet(stcb, sa);
3483 SCTP_INP_RUNLOCK(inp);
3486 * We increment here since
3487 * sctp_findassociation_ep_addr() wil do a
3488 * decrement if it finds the stcb as long as
3489 * the locked tcb (last argument) is NOT a
3492 SCTP_INP_INCR_REF(inp);
3493 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3495 SCTP_INP_DECR_REF(inp);
3500 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3504 if (stcb->asoc.delayed_connection == 1) {
3505 stcb->asoc.delayed_connection = 0;
3506 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3507 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3508 stcb->asoc.primary_destination,
3509 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3510 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3513 * already expired or did not use delayed
3516 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3519 SCTP_TCB_UNLOCK(stcb);
3522 case SCTP_MAX_BURST:
3526 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3528 SCTP_INP_WLOCK(inp);
3530 inp->sctp_ep.max_burst = *burst;
3532 SCTP_INP_WUNLOCK(inp);
3537 struct sctp_assoc_value *av;
3540 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3541 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3543 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3544 ovh = SCTP_MED_OVERHEAD;
3546 ovh = SCTP_MED_V4_OVERHEAD;
3549 if (av->assoc_value) {
3550 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3552 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3554 SCTP_TCB_UNLOCK(stcb);
3556 SCTP_INP_WLOCK(inp);
3558 * FIXME MT: I think this is not in tune
3561 if (av->assoc_value) {
3562 inp->sctp_frag_point = (av->assoc_value + ovh);
3564 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3566 SCTP_INP_WUNLOCK(inp);
3572 struct sctp_event_subscribe *events;
3574 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3576 SCTP_INP_WLOCK(inp);
3577 if (events->sctp_data_io_event) {
3578 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3580 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3583 if (events->sctp_association_event) {
3584 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3586 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3589 if (events->sctp_address_event) {
3590 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3592 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3595 if (events->sctp_send_failure_event) {
3596 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3598 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3601 if (events->sctp_peer_error_event) {
3602 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3604 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3607 if (events->sctp_shutdown_event) {
3608 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3610 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3613 if (events->sctp_partial_delivery_event) {
3614 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3616 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3619 if (events->sctp_adaptation_layer_event) {
3620 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3622 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3625 if (events->sctp_authentication_event) {
3626 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3628 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3631 if (events->sctp_sender_dry_event) {
3632 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3633 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3634 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3635 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3637 SCTP_TCB_LOCK(stcb);
3640 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3641 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3642 (stcb->asoc.stream_queue_cnt == 0)) {
3643 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3646 SCTP_TCB_UNLOCK(stcb);
3650 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3653 if (events->sctp_stream_reset_events) {
3654 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3656 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3658 SCTP_INP_WUNLOCK(inp);
3662 case SCTP_ADAPTATION_LAYER:
3664 struct sctp_setadaptation *adap_bits;
3666 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3667 SCTP_INP_WLOCK(inp);
3668 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3669 SCTP_INP_WUNLOCK(inp);
3673 case SCTP_SET_INITIAL_DBG_SEQ:
3677 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3678 SCTP_INP_WLOCK(inp);
3679 inp->sctp_ep.initial_sequence_debug = *vvv;
3680 SCTP_INP_WUNLOCK(inp);
3684 case SCTP_DEFAULT_SEND_PARAM:
3686 struct sctp_sndrcvinfo *s_info;
3688 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3689 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3692 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3693 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3695 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3698 SCTP_TCB_UNLOCK(stcb);
3700 SCTP_INP_WLOCK(inp);
3701 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3702 SCTP_INP_WUNLOCK(inp);
3706 case SCTP_PEER_ADDR_PARAMS:
3707 /* Applys to the specific association */
3709 struct sctp_paddrparams *paddrp;
3710 struct sctp_nets *net;
3712 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3713 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3716 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3719 * We increment here since
3720 * sctp_findassociation_ep_addr() wil do a
3721 * decrement if it finds the stcb as long as
3722 * the locked tcb (last argument) is NOT a
3725 SCTP_INP_INCR_REF(inp);
3726 stcb = sctp_findassociation_ep_addr(&inp,
3727 (struct sockaddr *)&paddrp->spp_address,
3730 SCTP_INP_DECR_REF(inp);
3733 if (stcb && (net == NULL)) {
3734 struct sockaddr *sa;
3736 sa = (struct sockaddr *)&paddrp->spp_address;
3737 if (sa->sa_family == AF_INET) {
3738 struct sockaddr_in *sin;
3740 sin = (struct sockaddr_in *)sa;
3741 if (sin->sin_addr.s_addr) {
3742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3743 SCTP_TCB_UNLOCK(stcb);
3747 } else if (sa->sa_family == AF_INET6) {
3748 struct sockaddr_in6 *sin6;
3750 sin6 = (struct sockaddr_in6 *)sa;
3751 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3752 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3753 SCTP_TCB_UNLOCK(stcb);
3758 error = EAFNOSUPPORT;
3759 SCTP_TCB_UNLOCK(stcb);
3760 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3765 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3767 SCTP_TCB_UNLOCK(stcb);
3768 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3771 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3773 SCTP_TCB_UNLOCK(stcb);
3774 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3778 /************************TCB SPECIFIC SET ******************/
3780 * do we change the timer for HB, we run
3785 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3786 ovh = SCTP_MED_OVERHEAD;
3788 ovh = SCTP_MED_V4_OVERHEAD;
3791 if (paddrp->spp_hbinterval)
3792 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3793 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3794 stcb->asoc.heart_beat_delay = 0;
3796 /* network sets ? */
3798 /************************NET SPECIFIC SET ******************/
3799 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3801 if (sctp_send_hb(stcb, 1, net) < 0) {
3802 /* asoc destroyed */
3803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3808 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3809 net->dest_state |= SCTP_ADDR_NOHB;
3811 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3812 net->dest_state &= ~SCTP_ADDR_NOHB;
3814 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3815 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3816 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3817 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3819 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3820 net->mtu = paddrp->spp_pathmtu + ovh;
3821 if (net->mtu < stcb->asoc.smallest_mtu) {
3822 #ifdef SCTP_PRINT_FOR_B_AND_M
3823 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3826 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3830 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3831 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3832 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3835 if (paddrp->spp_pathmaxrxt)
3836 net->failure_threshold = paddrp->spp_pathmaxrxt;
3838 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3839 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3840 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3845 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3846 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3847 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3852 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3853 if (paddrp->spp_pathmaxrxt)
3854 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3856 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3857 /* Turn back on the timer */
3858 stcb->asoc.hb_is_disabled = 0;
3859 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3861 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3862 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3863 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3864 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3865 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3867 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3868 net->mtu = paddrp->spp_pathmtu + ovh;
3869 if (net->mtu < stcb->asoc.smallest_mtu) {
3870 #ifdef SCTP_PRINT_FOR_B_AND_M
3871 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3874 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3879 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3880 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3881 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3882 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3886 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3887 int cnt_of_unconf = 0;
3888 struct sctp_nets *lnet;
3890 stcb->asoc.hb_is_disabled = 1;
3891 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3892 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3897 * stop the timer ONLY if we
3898 * have no unconfirmed
3901 if (cnt_of_unconf == 0) {
3902 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3903 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3904 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3908 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3909 /* start up the timer. */
3910 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3911 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3915 if (paddrp->spp_flags & SPP_IPV4_TOS)
3916 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3919 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3920 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3924 SCTP_TCB_UNLOCK(stcb);
3926 /************************NO TCB, SET TO default stuff ******************/
3927 SCTP_INP_WLOCK(inp);
3929 * For the TOS/FLOWLABEL stuff you set it
3930 * with the options on the socket
3932 if (paddrp->spp_pathmaxrxt) {
3933 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3935 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3936 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3937 else if (paddrp->spp_hbinterval) {
3938 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3939 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3940 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3942 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3943 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3945 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3946 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3948 SCTP_INP_WUNLOCK(inp);
3954 struct sctp_rtoinfo *srto;
3955 uint32_t new_init, new_min, new_max;
3957 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3958 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3961 if (srto->srto_initial)
3962 new_init = srto->srto_initial;
3964 new_init = stcb->asoc.initial_rto;
3966 new_max = srto->srto_max;
3968 new_max = stcb->asoc.maxrto;
3970 new_min = srto->srto_min;
3972 new_min = stcb->asoc.minrto;
3973 if ((new_min <= new_init) && (new_init <= new_max)) {
3974 stcb->asoc.initial_rto = new_init;
3975 stcb->asoc.maxrto = new_max;
3976 stcb->asoc.minrto = new_min;
3978 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3981 SCTP_TCB_UNLOCK(stcb);
3983 SCTP_INP_WLOCK(inp);
3984 if (srto->srto_initial)
3985 new_init = srto->srto_initial;
3987 new_init = inp->sctp_ep.initial_rto;
3989 new_max = srto->srto_max;
3991 new_max = inp->sctp_ep.sctp_maxrto;
3993 new_min = srto->srto_min;
3995 new_min = inp->sctp_ep.sctp_minrto;
3996 if ((new_min <= new_init) && (new_init <= new_max)) {
3997 inp->sctp_ep.initial_rto = new_init;
3998 inp->sctp_ep.sctp_maxrto = new_max;
3999 inp->sctp_ep.sctp_minrto = new_min;
4001 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4004 SCTP_INP_WUNLOCK(inp);
4008 case SCTP_ASSOCINFO:
4010 struct sctp_assocparams *sasoc;
4012 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4013 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4014 if (sasoc->sasoc_cookie_life) {
4015 /* boundary check the cookie life */
4016 if (sasoc->sasoc_cookie_life < 1000)
4017 sasoc->sasoc_cookie_life = 1000;
4018 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4019 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4023 if (sasoc->sasoc_asocmaxrxt)
4024 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4025 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
4026 sasoc->sasoc_peer_rwnd = 0;
4027 sasoc->sasoc_local_rwnd = 0;
4028 if (sasoc->sasoc_cookie_life) {
4029 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4031 SCTP_TCB_UNLOCK(stcb);
4033 SCTP_INP_WLOCK(inp);
4034 if (sasoc->sasoc_asocmaxrxt)
4035 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4036 sasoc->sasoc_number_peer_destinations = 0;
4037 sasoc->sasoc_peer_rwnd = 0;
4038 sasoc->sasoc_local_rwnd = 0;
4039 if (sasoc->sasoc_cookie_life) {
4040 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4042 SCTP_INP_WUNLOCK(inp);
4048 struct sctp_initmsg *sinit;
4050 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4051 SCTP_INP_WLOCK(inp);
4052 if (sinit->sinit_num_ostreams)
4053 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4055 if (sinit->sinit_max_instreams)
4056 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4058 if (sinit->sinit_max_attempts)
4059 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4061 if (sinit->sinit_max_init_timeo)
4062 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4063 SCTP_INP_WUNLOCK(inp);
4066 case SCTP_PRIMARY_ADDR:
4068 struct sctp_setprim *spa;
4069 struct sctp_nets *net, *lnet;
4071 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4072 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4076 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4079 * We increment here since
4080 * sctp_findassociation_ep_addr() wil do a
4081 * decrement if it finds the stcb as long as
4082 * the locked tcb (last argument) is NOT a
4085 SCTP_INP_INCR_REF(inp);
4086 stcb = sctp_findassociation_ep_addr(&inp,
4087 (struct sockaddr *)&spa->ssp_addr,
4090 SCTP_INP_DECR_REF(inp);
4094 if ((stcb) && (net)) {
4095 if ((net != stcb->asoc.primary_destination) &&
4096 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4097 /* Ok we need to set it */
4098 lnet = stcb->asoc.primary_destination;
4099 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4100 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4101 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4103 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4107 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4111 SCTP_TCB_UNLOCK(stcb);
4115 case SCTP_SET_DYNAMIC_PRIMARY:
4117 union sctp_sockstore *ss;
4119 error = priv_check(curthread,
4120 PRIV_NETINET_RESERVEDPORT);
4124 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4125 /* SUPER USER CHECK? */
4126 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4129 case SCTP_SET_PEER_PRIMARY_ADDR:
4131 struct sctp_setpeerprim *sspp;
4133 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4134 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4136 struct sctp_ifa *ifa;
4138 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4139 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4141 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4145 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4147 * Must validate the ifa found is in
4150 struct sctp_laddr *laddr;
4153 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4154 if (laddr->ifa == NULL) {
4155 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4159 if (laddr->ifa == ifa) {
4165 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4170 if (sctp_set_primary_ip_address_sa(stcb,
4171 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4176 SCTP_TCB_UNLOCK(stcb);
4178 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4184 case SCTP_BINDX_ADD_ADDR:
4186 struct sctp_getaddresses *addrs;
4190 td = (struct thread *)p;
4191 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4193 if (addrs->addr->sa_family == AF_INET) {
4194 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4196 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4200 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4201 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4205 } else if (addrs->addr->sa_family == AF_INET6) {
4206 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4208 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4212 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4213 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4214 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4219 error = EAFNOSUPPORT;
4222 sctp_bindx_add_address(so, inp, addrs->addr,
4223 addrs->sget_assoc_id, vrf_id,
4227 case SCTP_BINDX_REM_ADDR:
4229 struct sctp_getaddresses *addrs;
4233 td = (struct thread *)p;
4235 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4236 if (addrs->addr->sa_family == AF_INET) {
4237 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4239 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4243 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4244 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4248 } else if (addrs->addr->sa_family == AF_INET6) {
4249 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4251 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4255 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4256 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4257 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4262 error = EAFNOSUPPORT;
4265 sctp_bindx_delete_address(so, inp, addrs->addr,
4266 addrs->sget_assoc_id, vrf_id,
4271 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4272 error = ENOPROTOOPT;
4274 } /* end switch (opt) */
4279 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4281 void *optval = NULL;
4283 struct sctp_inpcb *inp;
4287 inp = (struct sctp_inpcb *)so->so_pcb;
4289 /* I made the same as TCP since we are not setup? */
4290 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4291 return (ECONNRESET);
4293 if (sopt->sopt_level != IPPROTO_SCTP) {
4294 /* wrong proto level... send back up to IP */
4296 if (INP_CHECK_SOCKAF(so, AF_INET6))
4297 error = ip6_ctloutput(so, sopt);
4300 error = ip_ctloutput(so, sopt);
4303 optsize = sopt->sopt_valsize;
4305 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4306 if (optval == NULL) {
4307 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4310 error = sooptcopyin(sopt, optval, optsize, optsize);
4312 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4316 p = (void *)sopt->sopt_td;
4317 if (sopt->sopt_dir == SOPT_SET) {
4318 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4319 } else if (sopt->sopt_dir == SOPT_GET) {
4320 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4322 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4325 if ((error == 0) && (optval != NULL)) {
4326 error = sooptcopyout(sopt, optval, optsize);
4327 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4328 } else if (optval != NULL) {
4329 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4337 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4340 int create_lock_on = 0;
4342 struct sctp_inpcb *inp;
4343 struct sctp_tcb *stcb = NULL;
4345 inp = (struct sctp_inpcb *)so->so_pcb;
4347 /* I made the same as TCP since we are not setup? */
4348 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4349 return (ECONNRESET);
4352 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4356 if (addr->sa_family == AF_INET6) {
4357 struct sockaddr_in6 *sin6p;
4359 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4360 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4363 sin6p = (struct sockaddr_in6 *)addr;
4364 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4365 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4370 if (addr->sa_family == AF_INET) {
4371 struct sockaddr_in *sinp;
4373 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4374 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4377 sinp = (struct sockaddr_in *)addr;
4378 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4379 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4383 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4384 return (EAFNOSUPPORT);
4386 SCTP_INP_INCR_REF(inp);
4387 SCTP_ASOC_CREATE_LOCK(inp);
4391 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4392 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4393 /* Should I really unlock ? */
4394 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4399 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4400 (addr->sa_family == AF_INET6)) {
4401 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4406 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4407 SCTP_PCB_FLAGS_UNBOUND) {
4408 /* Bind a ephemeral port */
4409 error = sctp_inpcb_bind(so, NULL, NULL, p);
4414 /* Now do we connect? */
4415 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4416 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4417 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4421 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4422 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4423 /* We are already connected AND the TCP model */
4424 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4428 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4429 SCTP_INP_RLOCK(inp);
4430 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4431 SCTP_INP_RUNLOCK(inp);
4434 * We increment here since sctp_findassociation_ep_addr()
4435 * will do a decrement if it finds the stcb as long as the
4436 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4438 SCTP_INP_INCR_REF(inp);
4439 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4441 SCTP_INP_DECR_REF(inp);
4443 SCTP_TCB_UNLOCK(stcb);
4447 /* Already have or am bring up an association */
4448 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4452 vrf_id = inp->def_vrf_id;
4453 /* We are GOOD to go */
4454 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
4456 /* Gak! no memory */
4459 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4460 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4461 /* Set the connected flag so we can queue data */
4464 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4465 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4467 /* initialize authentication parameters for the assoc */
4468 sctp_initialize_auth_params(inp, stcb);
4470 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4471 SCTP_TCB_UNLOCK(stcb);
4473 if (create_lock_on) {
4474 SCTP_ASOC_CREATE_UNLOCK(inp);
4476 SCTP_INP_DECR_REF(inp);
4481 sctp_listen(struct socket *so, int backlog, struct thread *p)
4484 * Note this module depends on the protocol processing being called
4485 * AFTER any socket level flags and backlog are applied to the
4486 * socket. The traditional way that the socket flags are applied is
4487 * AFTER protocol processing. We have made a change to the
4488 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4489 * place if the socket API for SCTP is to work properly.
4493 struct sctp_inpcb *inp;
4495 inp = (struct sctp_inpcb *)so->so_pcb;
4497 /* I made the same as TCP since we are not setup? */
4498 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4499 return (ECONNRESET);
4501 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4502 /* See if we have a listener */
4503 struct sctp_inpcb *tinp;
4504 union sctp_sockstore store, *sp;
4507 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4509 struct sctp_laddr *laddr;
4511 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4512 memcpy(&store, &laddr->ifa->address, sizeof(store));
4513 sp->sin.sin_port = inp->sctp_lport;
4514 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4515 if (tinp && (tinp != inp) &&
4516 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4517 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4518 (tinp->sctp_socket->so_qlimit)) {
4520 * we have a listener already and
4523 SCTP_INP_DECR_REF(tinp);
4524 return (EADDRINUSE);
4526 SCTP_INP_DECR_REF(tinp);
4530 /* Setup a local addr bound all */
4531 memset(&store, 0, sizeof(store));
4532 store.sin.sin_port = inp->sctp_lport;
4534 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4535 store.sa.sa_family = AF_INET6;
4536 store.sa.sa_len = sizeof(struct sockaddr_in6);
4539 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4540 store.sa.sa_family = AF_INET;
4541 store.sa.sa_len = sizeof(struct sockaddr_in);
4543 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4544 if (tinp && (tinp != inp) &&
4545 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4546 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4547 (tinp->sctp_socket->so_qlimit)) {
4549 * we have a listener already and its not
4552 SCTP_INP_DECR_REF(tinp);
4553 return (EADDRINUSE);
4555 SCTP_INP_DECR_REF(inp);
4559 SCTP_INP_RLOCK(inp);
4560 #ifdef SCTP_LOCK_LOGGING
4561 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4562 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4566 error = solisten_proto_check(so);
4569 SCTP_INP_RUNLOCK(inp);
4572 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4573 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4575 * The unlucky case - We are in the tcp pool with this guy.
4576 * - Someone else is in the main inp slot. - We must move
4577 * this guy (the listener) to the main slot - We must then
4578 * move the guy that was listener to the TCP Pool.
4580 if (sctp_swap_inpcb_for_listen(inp)) {
4584 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4585 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4586 /* We are already connected AND the TCP model */
4588 SCTP_INP_RUNLOCK(inp);
4590 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4591 return (EADDRINUSE);
4593 SCTP_INP_RUNLOCK(inp);
4594 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4595 /* We must do a bind. */
4597 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4598 /* bind error, probably perm */
4603 /* It appears for 7.0 and on, we must always call this. */
4604 solisten_proto(so, backlog);
4605 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4606 /* remove the ACCEPTCONN flag for one-to-many sockets */
4607 so->so_options &= ~SO_ACCEPTCONN;
4610 /* turning off listen */
4611 so->so_options &= ~SO_ACCEPTCONN;
4617 static int sctp_defered_wakeup_cnt = 0;
4620 sctp_accept(struct socket *so, struct sockaddr **addr)
4622 struct sctp_tcb *stcb;
4623 struct sctp_inpcb *inp;
4624 union sctp_sockstore store;
4630 inp = (struct sctp_inpcb *)so->so_pcb;
4633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4634 return (ECONNRESET);
4636 SCTP_INP_RLOCK(inp);
4637 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4638 SCTP_INP_RUNLOCK(inp);
4639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4640 return (EOPNOTSUPP);
4642 if (so->so_state & SS_ISDISCONNECTED) {
4643 SCTP_INP_RUNLOCK(inp);
4644 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4645 return (ECONNABORTED);
4647 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4649 SCTP_INP_RUNLOCK(inp);
4650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4651 return (ECONNRESET);
4653 SCTP_TCB_LOCK(stcb);
4654 SCTP_INP_RUNLOCK(inp);
4655 store = stcb->asoc.primary_destination->ro._l_addr;
4656 SCTP_TCB_UNLOCK(stcb);
4657 switch (store.sa.sa_family) {
4660 struct sockaddr_in *sin;
4662 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4663 sin->sin_family = AF_INET;
4664 sin->sin_len = sizeof(*sin);
4665 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4666 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4667 *addr = (struct sockaddr *)sin;
4673 struct sockaddr_in6 *sin6;
4675 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4676 sin6->sin6_family = AF_INET6;
4677 sin6->sin6_len = sizeof(*sin6);
4678 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4680 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4681 if ((error = sa6_recoverscope(sin6)) != 0) {
4682 SCTP_FREE_SONAME(sin6);
4685 *addr = (struct sockaddr *)sin6;
4693 /* Wake any delayed sleep action */
4694 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4695 SCTP_INP_WLOCK(inp);
4696 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4697 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4698 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4699 SCTP_INP_WUNLOCK(inp);
4700 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4701 if (sowriteable(inp->sctp_socket)) {
4702 sowwakeup_locked(inp->sctp_socket);
4704 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4706 SCTP_INP_WLOCK(inp);
4708 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4709 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4710 SCTP_INP_WUNLOCK(inp);
4711 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4712 if (soreadable(inp->sctp_socket)) {
4713 sctp_defered_wakeup_cnt++;
4714 sorwakeup_locked(inp->sctp_socket);
4716 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4718 SCTP_INP_WLOCK(inp);
4720 SCTP_INP_WUNLOCK(inp);
4726 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4728 struct sockaddr_in *sin;
4730 struct sctp_inpcb *inp;
4731 struct sctp_ifa *sctp_ifa;
4734 * Do the malloc first in case it blocks.
4736 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4737 sin->sin_family = AF_INET;
4738 sin->sin_len = sizeof(*sin);
4739 inp = (struct sctp_inpcb *)so->so_pcb;
4741 SCTP_FREE_SONAME(sin);
4742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4745 SCTP_INP_RLOCK(inp);
4746 sin->sin_port = inp->sctp_lport;
4747 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4748 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4749 struct sctp_tcb *stcb;
4750 struct sockaddr_in *sin_a;
4751 struct sctp_nets *net;
4754 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4760 SCTP_TCB_LOCK(stcb);
4761 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4762 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4764 /* this will make coverity happy */
4767 if (sin_a->sin_family == AF_INET) {
4772 if ((!fnd) || (sin_a == NULL)) {
4774 SCTP_TCB_UNLOCK(stcb);
4777 vrf_id = inp->def_vrf_id;
4778 sctp_ifa = sctp_source_address_selection(inp,
4780 (sctp_route_t *) & net->ro,
4783 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4784 sctp_free_ifa(sctp_ifa);
4786 SCTP_TCB_UNLOCK(stcb);
4788 /* For the bound all case you get back 0 */
4790 sin->sin_addr.s_addr = 0;
4794 /* Take the first IPv4 address in the list */
4795 struct sctp_laddr *laddr;
4798 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4799 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4800 struct sockaddr_in *sin_a;
4802 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4803 sin->sin_addr = sin_a->sin_addr;
4809 SCTP_FREE_SONAME(sin);
4810 SCTP_INP_RUNLOCK(inp);
4811 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4815 SCTP_INP_RUNLOCK(inp);
4816 (*addr) = (struct sockaddr *)sin;
4821 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4823 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4825 struct sockaddr_in *sin_a;
4826 struct sctp_inpcb *inp;
4827 struct sctp_tcb *stcb;
4828 struct sctp_nets *net;
4830 /* Do the malloc first in case it blocks. */
4831 inp = (struct sctp_inpcb *)so->so_pcb;
4832 if ((inp == NULL) ||
4833 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4834 /* UDP type and listeners will drop out here */
4835 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4838 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4839 sin->sin_family = AF_INET;
4840 sin->sin_len = sizeof(*sin);
4842 /* We must recapture incase we blocked */
4843 inp = (struct sctp_inpcb *)so->so_pcb;
4845 SCTP_FREE_SONAME(sin);
4846 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4849 SCTP_INP_RLOCK(inp);
4850 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4852 SCTP_TCB_LOCK(stcb);
4854 SCTP_INP_RUNLOCK(inp);
4856 SCTP_FREE_SONAME(sin);
4857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4861 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4862 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4863 if (sin_a->sin_family == AF_INET) {
4865 sin->sin_port = stcb->rport;
4866 sin->sin_addr = sin_a->sin_addr;
4870 SCTP_TCB_UNLOCK(stcb);
4872 /* No IPv4 address */
4873 SCTP_FREE_SONAME(sin);
4874 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4877 (*addr) = (struct sockaddr *)sin;
4881 struct pr_usrreqs sctp_usrreqs = {
4882 .pru_abort = sctp_abort,
4883 .pru_accept = sctp_accept,
4884 .pru_attach = sctp_attach,
4885 .pru_bind = sctp_bind,
4886 .pru_connect = sctp_connect,
4887 .pru_control = in_control,
4888 .pru_close = sctp_close,
4889 .pru_detach = sctp_close,
4890 .pru_sopoll = sopoll_generic,
4891 .pru_flush = sctp_flush,
4892 .pru_disconnect = sctp_disconnect,
4893 .pru_listen = sctp_listen,
4894 .pru_peeraddr = sctp_peeraddr,
4895 .pru_send = sctp_sendm,
4896 .pru_shutdown = sctp_shutdown,
4897 .pru_sockaddr = sctp_ingetaddr,
4898 .pru_sosend = sctp_sosend,
4899 .pru_soreceive = sctp_soreceive