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 ((stcb->asoc.sctp_cmt_on_off == 1) &&
304 (stcb->asoc.sctp_cmt_pf > 0)) {
305 net->dest_state &= ~SCTP_ADDR_PF;
306 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
309 net->error_count = net->failure_threshold + 1;
310 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
311 stcb, SCTP_FAILED_THRESHOLD,
312 (void *)net, SCTP_SO_NOT_LOCKED);
314 SCTP_TCB_UNLOCK(stcb);
315 } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
316 (icmph->icmp_code == ICMP_UNREACH_PORT)) {
318 * Here the peer is either playing tricks on us, including
319 * an address that belongs to someone who does not support
320 * SCTP OR was a userland implementation that shutdown and
321 * now is dead. In either case treat it like a OOTB abort
324 reason = SCTP_PEER_FAULTY;
325 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
326 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
327 so = SCTP_INP_SO(inp);
328 atomic_add_int(&stcb->asoc.refcnt, 1);
329 SCTP_TCB_UNLOCK(stcb);
330 SCTP_SOCKET_LOCK(so, 1);
332 atomic_subtract_int(&stcb->asoc.refcnt, 1);
334 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
335 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
336 SCTP_SOCKET_UNLOCK(so, 1);
337 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
339 /* no need to unlock here, since the TCB is gone */
341 SCTP_TCB_UNLOCK(stcb);
346 sctp_ctlinput(cmd, sa, vip)
355 /* FIX, for non-bsd is this right? */
356 vrf_id = SCTP_DEFAULT_VRFID;
357 if (sa->sa_family != AF_INET ||
358 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
361 if (PRC_IS_REDIRECT(cmd)) {
363 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
367 struct sctp_inpcb *inp = NULL;
368 struct sctp_tcb *stcb = NULL;
369 struct sctp_nets *net = NULL;
370 struct sockaddr_in to, from;
372 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
373 bzero(&to, sizeof(to));
374 bzero(&from, sizeof(from));
375 from.sin_family = to.sin_family = AF_INET;
376 from.sin_len = to.sin_len = sizeof(to);
377 from.sin_port = sh->src_port;
378 from.sin_addr = ip->ip_src;
379 to.sin_port = sh->dest_port;
380 to.sin_addr = ip->ip_dst;
383 * 'to' holds the dest of the packet that failed to be sent.
384 * 'from' holds our local endpoint address. Thus we reverse
385 * the to and the from in the lookup.
387 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
388 (struct sockaddr *)&to,
389 &inp, &net, 1, vrf_id);
390 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
391 if (cmd != PRC_MSGSIZE) {
392 sctp_notify(inp, ip, sh,
393 (struct sockaddr *)&to, stcb,
396 /* handle possible ICMP size messages */
397 sctp_notify_mbuf(inp, stcb, net, ip, sh);
400 if ((stcb == NULL) && (inp != NULL)) {
401 /* reduce ref-count */
403 SCTP_INP_DECR_REF(inp);
404 SCTP_INP_WUNLOCK(inp);
407 SCTP_TCB_UNLOCK(stcb);
415 sctp_getcred(SYSCTL_HANDLER_ARGS)
418 struct sockaddr_in addrs[2];
419 struct sctp_inpcb *inp;
420 struct sctp_nets *net;
421 struct sctp_tcb *stcb;
425 /* FIX, for non-bsd is this right? */
426 vrf_id = SCTP_DEFAULT_VRFID;
428 error = priv_check(req->td, PRIV_NETINET_GETCRED);
433 error = SYSCTL_IN(req, addrs, sizeof(addrs));
437 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
439 &inp, &net, 1, vrf_id);
440 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
441 if ((inp != NULL) && (stcb == NULL)) {
442 /* reduce ref-count */
444 SCTP_INP_DECR_REF(inp);
447 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
451 SCTP_TCB_UNLOCK(stcb);
453 * We use the write lock here, only since in the error leg we need
454 * it. If we used RLOCK, then we would have to
455 * wlock/decr/unlock/rlock. Which in theory could create a hole.
456 * Better to use higher wlock.
460 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
462 SCTP_INP_WUNLOCK(inp);
465 cru2x(inp->sctp_socket->so_cred, &xuc);
466 SCTP_INP_WUNLOCK(inp);
467 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
472 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
473 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
477 sctp_abort(struct socket *so)
479 struct sctp_inpcb *inp;
482 inp = (struct sctp_inpcb *)so->so_pcb;
487 flags = inp->sctp_flags;
488 #ifdef SCTP_LOG_CLOSING
489 sctp_log_closing(inp, NULL, 17);
491 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
492 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
493 #ifdef SCTP_LOG_CLOSING
494 sctp_log_closing(inp, NULL, 16);
496 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
497 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
499 SCTP_SB_CLEAR(so->so_snd);
501 * same for the rcv ones, they are only here for the
504 SCTP_SB_CLEAR(so->so_rcv);
506 /* Now null out the reference, we are completely detached. */
510 flags = inp->sctp_flags;
511 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
512 goto sctp_must_try_again;
519 sctp_attach(struct socket *so, int proto, struct thread *p)
521 struct sctp_inpcb *inp;
522 struct inpcb *ip_inp;
524 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
531 inp = (struct sctp_inpcb *)so->so_pcb;
533 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
536 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
537 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
542 error = sctp_inpcb_alloc(so, vrf_id);
546 inp = (struct sctp_inpcb *)so->so_pcb;
548 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
549 ip_inp = &inp->ip_inp.inp;
550 ip_inp->inp_vflag |= INP_IPV4;
551 ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
553 error = ipsec_init_policy(so, &ip_inp->inp_sp);
554 #ifdef SCTP_LOG_CLOSING
555 sctp_log_closing(inp, NULL, 17);
559 flags = inp->sctp_flags;
560 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
561 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
562 #ifdef SCTP_LOG_CLOSING
563 sctp_log_closing(inp, NULL, 15);
565 SCTP_INP_WUNLOCK(inp);
566 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
567 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
569 flags = inp->sctp_flags;
570 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
573 SCTP_INP_WUNLOCK(inp);
579 SCTP_INP_WUNLOCK(inp);
584 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
586 struct sctp_inpcb *inp = NULL;
590 if (addr && addr->sa_family != AF_INET) {
591 /* must be a v4 address! */
592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
596 if (addr && (addr->sa_len != sizeof(struct sockaddr_in))) {
597 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
600 inp = (struct sctp_inpcb *)so->so_pcb;
602 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
605 error = sctp_inpcb_bind(so, addr, NULL, p);
610 sctp_close(struct socket *so)
612 struct sctp_inpcb *inp;
615 inp = (struct sctp_inpcb *)so->so_pcb;
620 * Inform all the lower layer assoc that we are done.
623 flags = inp->sctp_flags;
624 #ifdef SCTP_LOG_CLOSING
625 sctp_log_closing(inp, NULL, 17);
627 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
628 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
629 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
630 (so->so_rcv.sb_cc > 0)) {
631 #ifdef SCTP_LOG_CLOSING
632 sctp_log_closing(inp, NULL, 13);
634 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
635 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
637 #ifdef SCTP_LOG_CLOSING
638 sctp_log_closing(inp, NULL, 14);
640 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
641 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
644 * The socket is now detached, no matter what the state of
645 * the SCTP association.
648 SCTP_SB_CLEAR(so->so_snd);
650 * same for the rcv ones, they are only here for the
653 SCTP_SB_CLEAR(so->so_rcv);
655 /* Now null out the reference, we are completely detached. */
659 flags = inp->sctp_flags;
660 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
661 goto sctp_must_try_again;
669 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
670 struct mbuf *control, struct thread *p);
674 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
675 struct mbuf *control, struct thread *p)
677 struct sctp_inpcb *inp;
680 inp = (struct sctp_inpcb *)so->so_pcb;
683 sctp_m_freem(control);
686 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
690 /* Got to have an to address if we are NOT a connected socket */
691 if ((addr == NULL) &&
692 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
693 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
696 } else if (addr == NULL) {
697 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
698 error = EDESTADDRREQ;
701 sctp_m_freem(control);
707 if (addr->sa_family != AF_INET) {
708 /* must be a v4 address! */
709 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
712 sctp_m_freem(control);
715 error = EDESTADDRREQ;
720 /* now what about control */
723 SCTP_PRINTF("huh? control set?\n");
724 sctp_m_freem(inp->control);
727 inp->control = control;
731 SCTP_BUF_NEXT(inp->pkt_last) = m;
734 inp->pkt_last = inp->pkt = m;
737 /* FreeBSD uses a flag passed */
738 ((flags & PRUS_MORETOCOME) == 0)
741 * note with the current version this code will only be used
742 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
743 * re-defining sosend to use the sctp_sosend. One can
744 * optionally switch back to this code (by changing back the
745 * definitions) but this is not advisable. This code is used
746 * by FreeBSD when sending a file with sendfile() though.
750 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
760 sctp_disconnect(struct socket *so)
762 struct sctp_inpcb *inp;
764 inp = (struct sctp_inpcb *)so->so_pcb;
766 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
770 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
771 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
772 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
774 SCTP_INP_RUNLOCK(inp);
777 struct sctp_association *asoc;
778 struct sctp_tcb *stcb;
780 stcb = LIST_FIRST(&inp->sctp_asoc_list);
782 SCTP_INP_RUNLOCK(inp);
783 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
788 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
789 /* We are about to be freed, out of here */
790 SCTP_TCB_UNLOCK(stcb);
791 SCTP_INP_RUNLOCK(inp);
794 if (((so->so_options & SO_LINGER) &&
795 (so->so_linger == 0)) ||
796 (so->so_rcv.sb_cc > 0)) {
797 if (SCTP_GET_STATE(asoc) !=
798 SCTP_STATE_COOKIE_WAIT) {
799 /* Left with Data unread */
802 err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
808 struct sctp_paramhdr *ph;
810 ph = mtod(err, struct sctp_paramhdr *);
811 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
812 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
813 ph->param_length = htons(SCTP_BUF_LEN(err));
815 #if defined(SCTP_PANIC_ON_ABORT)
816 panic("disconnect does an abort");
818 sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
819 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
821 SCTP_INP_RUNLOCK(inp);
822 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
823 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
824 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
826 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
827 /* No unlock tcb assoc is gone */
830 if (TAILQ_EMPTY(&asoc->send_queue) &&
831 TAILQ_EMPTY(&asoc->sent_queue) &&
832 (asoc->stream_queue_cnt == 0)) {
833 /* there is nothing queued to send, so done */
834 if (asoc->locked_on_sending) {
837 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
838 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
839 /* only send SHUTDOWN 1st time thru */
840 sctp_stop_timers_for_shutdown(stcb);
841 sctp_send_shutdown(stcb,
842 stcb->asoc.primary_destination);
843 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
844 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
845 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
846 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
848 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
849 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
850 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
852 asoc->primary_destination);
853 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
855 asoc->primary_destination);
859 * we still got (or just got) data to send,
860 * so set SHUTDOWN_PENDING
863 * XXX sockets draft says that SCTP_EOF
864 * should be sent with no data. currently,
865 * we will allow user data to be sent first
866 * and move to SHUTDOWN-PENDING
868 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
869 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
870 asoc->primary_destination);
871 if (asoc->locked_on_sending) {
872 /* Locked to send out the data */
873 struct sctp_stream_queue_pending *sp;
875 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
877 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
878 asoc->locked_on_sending->stream_no);
880 if ((sp->length == 0) && (sp->msg_is_complete == 0))
881 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
884 if (TAILQ_EMPTY(&asoc->send_queue) &&
885 TAILQ_EMPTY(&asoc->sent_queue) &&
886 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
890 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
891 0, M_DONTWAIT, 1, MT_DATA);
897 struct sctp_paramhdr *ph;
900 SCTP_BUF_LEN(op_err) =
901 (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
903 struct sctp_paramhdr *);
904 ph->param_type = htons(
905 SCTP_CAUSE_USER_INITIATED_ABT);
906 ph->param_length = htons(SCTP_BUF_LEN(op_err));
907 ippp = (uint32_t *) (ph + 1);
908 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
910 #if defined(SCTP_PANIC_ON_ABORT)
911 panic("disconnect does an abort");
914 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
915 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
916 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
917 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
918 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
919 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
921 SCTP_INP_RUNLOCK(inp);
922 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
925 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
928 soisdisconnecting(so);
929 SCTP_TCB_UNLOCK(stcb);
930 SCTP_INP_RUNLOCK(inp);
935 /* UDP model does not support this */
936 SCTP_INP_RUNLOCK(inp);
937 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
943 sctp_flush(struct socket *so, int how)
946 * We will just clear out the values and let subsequent close clear
947 * out the data, if any. Note if the user did a shutdown(SHUT_RD)
948 * they will not be able to read the data, the socket will block
949 * that from happening.
951 if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
953 * First make sure the sb will be happy, we don't use these
954 * except maybe the count
956 so->so_rcv.sb_cc = 0;
957 so->so_rcv.sb_mbcnt = 0;
958 so->so_rcv.sb_mb = NULL;
960 if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
962 * First make sure the sb will be happy, we don't use these
963 * except maybe the count
965 so->so_snd.sb_cc = 0;
966 so->so_snd.sb_mbcnt = 0;
967 so->so_snd.sb_mb = NULL;
974 sctp_shutdown(struct socket *so)
976 struct sctp_inpcb *inp;
978 inp = (struct sctp_inpcb *)so->so_pcb;
980 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
984 /* For UDP model this is a invalid call */
985 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
986 /* Restore the flags that the soshutdown took away. */
987 SOCKBUF_LOCK(&so->so_rcv);
988 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
989 SOCKBUF_UNLOCK(&so->so_rcv);
990 /* This proc will wakeup for read and do nothing (I hope) */
991 SCTP_INP_RUNLOCK(inp);
992 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
996 * Ok if we reach here its the TCP model and it is either a SHUT_WR
997 * or SHUT_RDWR. This means we put the shutdown flag against it.
1000 struct sctp_tcb *stcb;
1001 struct sctp_association *asoc;
1004 (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
1005 SCTP_INP_RUNLOCK(inp);
1010 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1013 * Ok we hit the case that the shutdown call was
1014 * made after an abort or something. Nothing to do
1017 SCTP_INP_RUNLOCK(inp);
1020 SCTP_TCB_LOCK(stcb);
1022 if (TAILQ_EMPTY(&asoc->send_queue) &&
1023 TAILQ_EMPTY(&asoc->sent_queue) &&
1024 (asoc->stream_queue_cnt == 0)) {
1025 if (asoc->locked_on_sending) {
1028 /* there is nothing queued to send, so I'm done... */
1029 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1030 /* only send SHUTDOWN the first time through */
1031 sctp_stop_timers_for_shutdown(stcb);
1032 sctp_send_shutdown(stcb,
1033 stcb->asoc.primary_destination);
1034 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1035 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1036 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1037 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1039 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1040 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1041 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1042 stcb->sctp_ep, stcb,
1043 asoc->primary_destination);
1044 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1045 stcb->sctp_ep, stcb,
1046 asoc->primary_destination);
1050 * we still got (or just got) data to send, so set
1053 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1054 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1055 asoc->primary_destination);
1057 if (asoc->locked_on_sending) {
1058 /* Locked to send out the data */
1059 struct sctp_stream_queue_pending *sp;
1061 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1063 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1064 asoc->locked_on_sending->stream_no);
1066 if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1067 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1071 if (TAILQ_EMPTY(&asoc->send_queue) &&
1072 TAILQ_EMPTY(&asoc->sent_queue) &&
1073 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1074 struct mbuf *op_err;
1077 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
1078 0, M_DONTWAIT, 1, MT_DATA);
1080 /* Fill in the user initiated abort */
1081 struct sctp_paramhdr *ph;
1084 SCTP_BUF_LEN(op_err) =
1085 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
1087 struct sctp_paramhdr *);
1088 ph->param_type = htons(
1089 SCTP_CAUSE_USER_INITIATED_ABT);
1090 ph->param_length = htons(SCTP_BUF_LEN(op_err));
1091 ippp = (uint32_t *) (ph + 1);
1092 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1094 #if defined(SCTP_PANIC_ON_ABORT)
1095 panic("shutdown does an abort");
1097 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1098 sctp_abort_an_association(stcb->sctp_ep, stcb,
1099 SCTP_RESPONSE_TO_USER_REQ,
1100 op_err, SCTP_SO_LOCKED);
1103 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1106 SCTP_TCB_UNLOCK(stcb);
1109 SCTP_INP_RUNLOCK(inp);
1114 * copies a "user" presentable address and removes embedded scope, etc.
1115 * returns 0 on success, 1 on error
1118 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1121 struct sockaddr_in6 lsa6;
1123 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1126 memcpy(ss, sa, sa->sa_len);
1133 * NOTE: assumes addr lock is held
1136 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1137 struct sctp_tcb *stcb,
1139 struct sockaddr_storage *sas,
1142 struct sctp_ifn *sctp_ifn;
1143 struct sctp_ifa *sctp_ifa;
1144 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1146 int ipv4_addr_legal, ipv6_addr_legal;
1147 struct sctp_vrf *vrf;
1154 /* Turn on all the appropriate scope */
1155 loopback_scope = stcb->asoc.loopback_scope;
1156 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1157 local_scope = stcb->asoc.local_scope;
1158 site_scope = stcb->asoc.site_scope;
1160 /* Turn on ALL scope, since we look at the EP */
1161 loopback_scope = ipv4_local_scope = local_scope =
1164 ipv4_addr_legal = ipv6_addr_legal = 0;
1165 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1166 ipv6_addr_legal = 1;
1167 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1168 ipv4_addr_legal = 1;
1171 ipv4_addr_legal = 1;
1173 vrf = sctp_find_vrf(vrf_id);
1177 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1178 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1179 if ((loopback_scope == 0) &&
1180 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1181 /* Skip loopback if loopback_scope not set */
1184 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1187 * For the BOUND-ALL case, the list
1188 * associated with a TCB is Always
1189 * considered a reverse list.. i.e.
1190 * it lists addresses that are NOT
1191 * part of the association. If this
1192 * is one of those we must skip it.
1194 if (sctp_is_addr_restricted(stcb,
1199 switch (sctp_ifa->address.sa.sa_family) {
1201 if (ipv4_addr_legal) {
1202 struct sockaddr_in *sin;
1204 sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1205 if (sin->sin_addr.s_addr == 0) {
1213 if ((ipv4_local_scope == 0) &&
1214 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1218 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1219 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1220 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1221 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1222 actual += sizeof(struct sockaddr_in6);
1225 memcpy(sas, sin, sizeof(*sin));
1226 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1227 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1228 actual += sizeof(*sin);
1232 if (actual >= limit) {
1241 if (ipv6_addr_legal) {
1242 struct sockaddr_in6 *sin6;
1244 sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1245 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1253 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1254 if (local_scope == 0)
1256 if (sin6->sin6_scope_id == 0) {
1257 if (sa6_recoverscope(sin6) != 0)
1275 if ((site_scope == 0) &&
1276 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1279 memcpy(sas, sin6, sizeof(*sin6));
1280 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1281 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1282 actual += sizeof(*sin6);
1283 if (actual >= limit) {
1298 struct sctp_laddr *laddr;
1300 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1302 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1306 if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1309 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1310 sas = (struct sockaddr_storage *)((caddr_t)sas +
1311 laddr->ifa->address.sa.sa_len);
1312 actual += laddr->ifa->address.sa.sa_len;
1313 if (actual >= limit) {
1322 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1323 struct sctp_tcb *stcb,
1325 struct sockaddr_storage *sas)
1329 SCTP_IPI_ADDR_RLOCK();
1330 /* fill up addresses for the endpoint's default vrf */
1331 size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1333 SCTP_IPI_ADDR_RUNLOCK();
1338 * NOTE: assumes addr lock is held
1341 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1344 struct sctp_vrf *vrf = NULL;
1347 * In both sub-set bound an bound_all cases we return the MAXIMUM
1348 * number of addresses that you COULD get. In reality the sub-set
1349 * bound may have an exclusion list for a given TCB OR in the
1350 * bound-all case a TCB may NOT include the loopback or other
1351 * addresses as well.
1353 vrf = sctp_find_vrf(vrf_id);
1357 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1358 struct sctp_ifn *sctp_ifn;
1359 struct sctp_ifa *sctp_ifa;
1361 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1362 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1363 /* Count them if they are the right type */
1364 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1365 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1366 cnt += sizeof(struct sockaddr_in6);
1368 cnt += sizeof(struct sockaddr_in);
1370 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1371 cnt += sizeof(struct sockaddr_in6);
1375 struct sctp_laddr *laddr;
1377 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1378 if (laddr->ifa->address.sa.sa_family == AF_INET) {
1379 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1380 cnt += sizeof(struct sockaddr_in6);
1382 cnt += sizeof(struct sockaddr_in);
1384 } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1385 cnt += sizeof(struct sockaddr_in6);
1392 sctp_count_max_addresses(struct sctp_inpcb *inp)
1396 SCTP_IPI_ADDR_RLOCK();
1397 /* count addresses for the endpoint's default VRF */
1398 cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1399 SCTP_IPI_ADDR_RUNLOCK();
1404 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1405 size_t optsize, void *p, int delay)
1408 int creat_lock_on = 0;
1409 struct sctp_tcb *stcb = NULL;
1410 struct sockaddr *sa;
1411 int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1414 int bad_addresses = 0;
1417 SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1419 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1420 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1421 /* We are already connected AND the TCP model */
1422 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1423 return (EADDRINUSE);
1425 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1426 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1427 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1430 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1431 SCTP_INP_RLOCK(inp);
1432 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1433 SCTP_INP_RUNLOCK(inp);
1436 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1439 SCTP_INP_INCR_REF(inp);
1440 SCTP_ASOC_CREATE_LOCK(inp);
1442 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1443 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1444 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1448 totaddrp = (int *)optval;
1449 totaddr = *totaddrp;
1450 sa = (struct sockaddr *)(totaddrp + 1);
1451 stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1452 if ((stcb != NULL) || bad_addresses) {
1453 /* Already have or am bring up an association */
1454 SCTP_ASOC_CREATE_UNLOCK(inp);
1457 SCTP_TCB_UNLOCK(stcb);
1458 if (bad_addresses == 0) {
1459 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1465 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1470 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1472 struct in6pcb *inp6;
1474 inp6 = (struct in6pcb *)inp;
1475 if (SCTP_IPV6_V6ONLY(inp6)) {
1477 * if IPV6_V6ONLY flag, ignore connections destined
1478 * to a v4 addr or v4-mapped addr
1480 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1486 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1487 SCTP_PCB_FLAGS_UNBOUND) {
1488 /* Bind a ephemeral port */
1489 error = sctp_inpcb_bind(so, NULL, NULL, p);
1494 /* FIX ME: do we want to pass in a vrf on the connect call? */
1495 vrf_id = inp->def_vrf_id;
1498 /* We are GOOD to go */
1499 stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1503 /* Gak! no memory */
1506 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1507 /* move to second address */
1508 if (sa->sa_family == AF_INET)
1509 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1511 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1514 added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1515 /* Fill in the return id */
1517 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
1520 a_id = (sctp_assoc_t *) optval;
1521 *a_id = sctp_get_associd(stcb);
1523 /* initialize authentication parameters for the assoc */
1524 sctp_initialize_auth_params(inp, stcb);
1527 /* doing delayed connection */
1528 stcb->asoc.delayed_connection = 1;
1529 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1531 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1532 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1534 SCTP_TCB_UNLOCK(stcb);
1535 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1536 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1537 /* Set the connected flag so we can queue data */
1541 if (creat_lock_on) {
1542 SCTP_ASOC_CREATE_UNLOCK(inp);
1544 SCTP_INP_DECR_REF(inp);
1548 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1549 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1550 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1551 SCTP_INP_RLOCK(inp); \
1552 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1554 SCTP_TCB_LOCK(stcb); \
1556 SCTP_INP_RUNLOCK(inp); \
1557 } else if (assoc_id != 0) { \
1558 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1559 if (stcb == NULL) { \
1560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1570 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1571 if (size < sizeof(type)) { \
1572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1576 destp = (type *)srcp; \
1581 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1584 struct sctp_inpcb *inp = NULL;
1586 struct sctp_tcb *stcb = NULL;
1588 if (optval == NULL) {
1589 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1592 inp = (struct sctp_inpcb *)so->so_pcb;
1594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1601 case SCTP_AUTOCLOSE:
1602 case SCTP_EXPLICIT_EOR:
1603 case SCTP_AUTO_ASCONF:
1604 case SCTP_DISABLE_FRAGMENTS:
1605 case SCTP_I_WANT_MAPPED_V4_ADDR:
1606 case SCTP_USE_EXT_RCVINFO:
1607 SCTP_INP_RLOCK(inp);
1609 case SCTP_DISABLE_FRAGMENTS:
1610 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1612 case SCTP_I_WANT_MAPPED_V4_ADDR:
1613 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1615 case SCTP_AUTO_ASCONF:
1616 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1617 /* only valid for bound all sockets */
1618 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1620 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1625 case SCTP_EXPLICIT_EOR:
1626 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1629 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1631 case SCTP_USE_EXT_RCVINFO:
1632 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1634 case SCTP_AUTOCLOSE:
1635 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1636 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1642 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1643 error = ENOPROTOOPT;
1644 } /* end switch (sopt->sopt_name) */
1645 if (optname != SCTP_AUTOCLOSE) {
1646 /* make it an "on/off" value */
1649 if (*optsize < sizeof(val)) {
1650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1654 SCTP_INP_RUNLOCK(inp);
1656 /* return the option value */
1657 *(int *)optval = val;
1658 *optsize = sizeof(val);
1661 case SCTP_GET_PACKET_LOG:
1663 #ifdef SCTP_PACKET_LOGGING
1667 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1668 ret = sctp_copy_out_packet_log(target, (int)*optsize);
1671 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1676 case SCTP_REUSE_PORT:
1680 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1681 /* Can't do this for a 1-m socket */
1685 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1686 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1687 *optsize = sizeof(uint32_t);
1690 case SCTP_PARTIAL_DELIVERY_POINT:
1694 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1695 *value = inp->partial_delivery_point;
1696 *optsize = sizeof(uint32_t);
1699 case SCTP_FRAGMENT_INTERLEAVE:
1703 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1704 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1705 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1706 *value = SCTP_FRAG_LEVEL_2;
1708 *value = SCTP_FRAG_LEVEL_1;
1711 *value = SCTP_FRAG_LEVEL_0;
1713 *optsize = sizeof(uint32_t);
1716 case SCTP_CMT_ON_OFF:
1718 struct sctp_assoc_value *av;
1720 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1721 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1723 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1724 SCTP_TCB_UNLOCK(stcb);
1726 SCTP_INP_RLOCK(inp);
1727 av->assoc_value = inp->sctp_cmt_on_off;
1728 SCTP_INP_RUNLOCK(inp);
1730 *optsize = sizeof(*av);
1733 /* JRS - Get socket option for pluggable congestion control */
1734 case SCTP_PLUGGABLE_CC:
1736 struct sctp_assoc_value *av;
1738 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1739 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1741 av->assoc_value = stcb->asoc.congestion_control_module;
1742 SCTP_TCB_UNLOCK(stcb);
1744 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1746 *optsize = sizeof(*av);
1749 case SCTP_GET_ADDR_LEN:
1751 struct sctp_assoc_value *av;
1753 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1756 if (av->assoc_value == AF_INET) {
1757 av->assoc_value = sizeof(struct sockaddr_in);
1762 if (av->assoc_value == AF_INET6) {
1763 av->assoc_value = sizeof(struct sockaddr_in6);
1768 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1770 *optsize = sizeof(*av);
1773 case SCTP_GET_ASSOC_NUMBER:
1775 uint32_t *value, cnt;
1777 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1779 SCTP_INP_RLOCK(inp);
1780 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1783 SCTP_INP_RUNLOCK(inp);
1785 *optsize = sizeof(uint32_t);
1789 case SCTP_GET_ASSOC_ID_LIST:
1791 struct sctp_assoc_ids *ids;
1792 unsigned int at, limit;
1794 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1796 limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1797 SCTP_INP_RLOCK(inp);
1798 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1800 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1807 SCTP_INP_RUNLOCK(inp);
1808 ids->gaids_number_of_ids = at;
1809 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1814 struct sctp_assoc_value *av;
1816 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1817 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1820 av->assoc_value = stcb->asoc.context;
1821 SCTP_TCB_UNLOCK(stcb);
1823 SCTP_INP_RLOCK(inp);
1824 av->assoc_value = inp->sctp_context;
1825 SCTP_INP_RUNLOCK(inp);
1827 *optsize = sizeof(*av);
1832 uint32_t *default_vrfid;
1834 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1835 *default_vrfid = inp->def_vrf_id;
1838 case SCTP_GET_ASOC_VRF:
1840 struct sctp_assoc_value *id;
1842 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1843 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1846 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1849 id->assoc_value = stcb->asoc.vrf_id;
1852 case SCTP_GET_VRF_IDS:
1854 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1858 case SCTP_GET_NONCE_VALUES:
1860 struct sctp_get_nonce_values *gnv;
1862 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1863 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1866 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1867 gnv->gn_local_tag = stcb->asoc.my_vtag;
1868 SCTP_TCB_UNLOCK(stcb);
1870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1873 *optsize = sizeof(*gnv);
1876 case SCTP_DELAYED_SACK:
1878 struct sctp_sack_info *sack;
1880 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1881 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1883 sack->sack_delay = stcb->asoc.delayed_ack;
1884 sack->sack_freq = stcb->asoc.sack_freq;
1885 SCTP_TCB_UNLOCK(stcb);
1887 SCTP_INP_RLOCK(inp);
1888 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1889 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1890 SCTP_INP_RUNLOCK(inp);
1892 *optsize = sizeof(*sack);
1896 case SCTP_GET_SNDBUF_USE:
1898 struct sctp_sockstat *ss;
1900 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1901 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1904 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1905 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1906 stcb->asoc.size_on_all_streams);
1907 SCTP_TCB_UNLOCK(stcb);
1909 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1912 *optsize = sizeof(struct sctp_sockstat);
1915 case SCTP_MAX_BURST:
1919 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1921 SCTP_INP_RLOCK(inp);
1922 *value = inp->sctp_ep.max_burst;
1923 SCTP_INP_RUNLOCK(inp);
1924 *optsize = sizeof(uint8_t);
1929 struct sctp_assoc_value *av;
1932 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1933 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1936 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1937 SCTP_TCB_UNLOCK(stcb);
1939 SCTP_INP_RLOCK(inp);
1940 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1941 ovh = SCTP_MED_OVERHEAD;
1943 ovh = SCTP_MED_V4_OVERHEAD;
1945 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
1946 av->assoc_value = 0;
1948 av->assoc_value = inp->sctp_frag_point - ovh;
1949 SCTP_INP_RUNLOCK(inp);
1951 *optsize = sizeof(struct sctp_assoc_value);
1954 case SCTP_GET_STAT_LOG:
1955 error = sctp_fill_stat_log(optval, optsize);
1959 struct sctp_event_subscribe *events;
1961 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1962 memset(events, 0, sizeof(*events));
1963 SCTP_INP_RLOCK(inp);
1964 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1965 events->sctp_data_io_event = 1;
1967 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1968 events->sctp_association_event = 1;
1970 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1971 events->sctp_address_event = 1;
1973 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1974 events->sctp_send_failure_event = 1;
1976 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1977 events->sctp_peer_error_event = 1;
1979 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1980 events->sctp_shutdown_event = 1;
1982 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1983 events->sctp_partial_delivery_event = 1;
1985 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1986 events->sctp_adaptation_layer_event = 1;
1988 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1989 events->sctp_authentication_event = 1;
1991 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
1992 events->sctp_sender_dry_event = 1;
1994 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1995 events->sctp_stream_reset_event = 1;
1996 SCTP_INP_RUNLOCK(inp);
1997 *optsize = sizeof(struct sctp_event_subscribe);
2001 case SCTP_ADAPTATION_LAYER:
2005 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2007 SCTP_INP_RLOCK(inp);
2008 *value = inp->sctp_ep.adaptation_layer_indicator;
2009 SCTP_INP_RUNLOCK(inp);
2010 *optsize = sizeof(uint32_t);
2013 case SCTP_SET_INITIAL_DBG_SEQ:
2017 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2018 SCTP_INP_RLOCK(inp);
2019 *value = inp->sctp_ep.initial_sequence_debug;
2020 SCTP_INP_RUNLOCK(inp);
2021 *optsize = sizeof(uint32_t);
2024 case SCTP_GET_LOCAL_ADDR_SIZE:
2028 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2029 SCTP_INP_RLOCK(inp);
2030 *value = sctp_count_max_addresses(inp);
2031 SCTP_INP_RUNLOCK(inp);
2032 *optsize = sizeof(uint32_t);
2035 case SCTP_GET_REMOTE_ADDR_SIZE:
2039 struct sctp_nets *net;
2041 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2042 /* FIXME MT: change to sctp_assoc_value? */
2043 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2047 /* Count the sizes */
2048 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2049 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2050 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2051 size += sizeof(struct sockaddr_in6);
2052 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2053 size += sizeof(struct sockaddr_in);
2059 SCTP_TCB_UNLOCK(stcb);
2060 *value = (uint32_t) size;
2062 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2065 *optsize = sizeof(uint32_t);
2068 case SCTP_GET_PEER_ADDRESSES:
2070 * Get the address information, an array is passed in to
2071 * fill up we pack it.
2075 struct sockaddr_storage *sas;
2076 struct sctp_nets *net;
2077 struct sctp_getaddresses *saddr;
2079 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2080 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2083 left = (*optsize) - sizeof(struct sctp_getaddresses);
2084 *optsize = sizeof(struct sctp_getaddresses);
2085 sas = (struct sockaddr_storage *)&saddr->addr[0];
2087 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2088 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) ||
2089 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2090 cpsz = sizeof(struct sockaddr_in6);
2091 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2092 cpsz = sizeof(struct sockaddr_in);
2098 /* not enough room. */
2102 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2103 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2104 /* Must map the address */
2105 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2106 (struct sockaddr_in6 *)sas);
2109 memcpy(sas, &net->ro._l_addr, cpsz);
2113 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2115 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2119 SCTP_TCB_UNLOCK(stcb);
2121 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2126 case SCTP_GET_LOCAL_ADDRESSES:
2128 size_t limit, actual;
2129 struct sockaddr_storage *sas;
2130 struct sctp_getaddresses *saddr;
2132 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2133 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2135 sas = (struct sockaddr_storage *)&saddr->addr[0];
2136 limit = *optsize - sizeof(sctp_assoc_t);
2137 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2139 SCTP_TCB_UNLOCK(stcb);
2141 *optsize = sizeof(struct sockaddr_storage) + actual;
2144 case SCTP_PEER_ADDR_PARAMS:
2146 struct sctp_paddrparams *paddrp;
2147 struct sctp_nets *net;
2149 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2150 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2154 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2157 * We increment here since
2158 * sctp_findassociation_ep_addr() wil do a
2159 * decrement if it finds the stcb as long as
2160 * the locked tcb (last argument) is NOT a
2163 SCTP_INP_INCR_REF(inp);
2164 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2166 SCTP_INP_DECR_REF(inp);
2169 if (stcb && (net == NULL)) {
2170 struct sockaddr *sa;
2172 sa = (struct sockaddr *)&paddrp->spp_address;
2173 if (sa->sa_family == AF_INET) {
2174 struct sockaddr_in *sin;
2176 sin = (struct sockaddr_in *)sa;
2177 if (sin->sin_addr.s_addr) {
2179 SCTP_TCB_UNLOCK(stcb);
2180 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2183 } else if (sa->sa_family == AF_INET6) {
2184 struct sockaddr_in6 *sin6;
2186 sin6 = (struct sockaddr_in6 *)sa;
2187 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2189 SCTP_TCB_UNLOCK(stcb);
2190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2194 error = EAFNOSUPPORT;
2195 SCTP_TCB_UNLOCK(stcb);
2196 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2201 /* Applys to the specific association */
2202 paddrp->spp_flags = 0;
2206 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2207 ovh = SCTP_MED_OVERHEAD;
2209 ovh = SCTP_MED_V4_OVERHEAD;
2213 paddrp->spp_pathmaxrxt = net->failure_threshold;
2214 paddrp->spp_pathmtu = net->mtu - ovh;
2215 /* get flags for HB */
2216 if (net->dest_state & SCTP_ADDR_NOHB)
2217 paddrp->spp_flags |= SPP_HB_DISABLE;
2219 paddrp->spp_flags |= SPP_HB_ENABLE;
2220 /* get flags for PMTU */
2221 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2222 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2224 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2227 if (net->ro._l_addr.sin.sin_family == AF_INET) {
2228 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
2229 paddrp->spp_flags |= SPP_IPV4_TOS;
2233 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
2234 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
2235 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2240 * No destination so return default
2245 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2246 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2248 paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
2249 paddrp->spp_flags |= SPP_IPV4_TOS;
2252 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
2253 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2255 /* default settings should be these */
2256 if (stcb->asoc.hb_is_disabled == 0) {
2257 paddrp->spp_flags |= SPP_HB_ENABLE;
2259 paddrp->spp_flags |= SPP_HB_DISABLE;
2261 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2262 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
2267 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2270 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2271 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2272 SCTP_TCB_UNLOCK(stcb);
2274 /* Use endpoint defaults */
2275 SCTP_INP_RLOCK(inp);
2276 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2277 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2278 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
2279 /* get inp's default */
2281 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
2282 paddrp->spp_flags |= SPP_IPV4_TOS;
2285 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2286 paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
2287 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2290 /* can't return this */
2291 paddrp->spp_pathmtu = 0;
2293 /* default behavior, no stcb */
2294 paddrp->spp_flags = SPP_PMTUD_ENABLE;
2296 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2297 paddrp->spp_flags |= SPP_HB_ENABLE;
2299 paddrp->spp_flags |= SPP_HB_DISABLE;
2301 SCTP_INP_RUNLOCK(inp);
2303 *optsize = sizeof(struct sctp_paddrparams);
2306 case SCTP_GET_PEER_ADDR_INFO:
2308 struct sctp_paddrinfo *paddri;
2309 struct sctp_nets *net;
2311 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2312 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2316 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2319 * We increment here since
2320 * sctp_findassociation_ep_addr() wil do a
2321 * decrement if it finds the stcb as long as
2322 * the locked tcb (last argument) is NOT a
2325 SCTP_INP_INCR_REF(inp);
2326 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2328 SCTP_INP_DECR_REF(inp);
2332 if ((stcb) && (net)) {
2333 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2334 paddri->spinfo_cwnd = net->cwnd;
2335 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2336 paddri->spinfo_rto = net->RTO;
2337 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2338 SCTP_TCB_UNLOCK(stcb);
2341 SCTP_TCB_UNLOCK(stcb);
2343 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2346 *optsize = sizeof(struct sctp_paddrinfo);
2349 case SCTP_PCB_STATUS:
2351 struct sctp_pcbinfo *spcb;
2353 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2354 sctp_fill_pcbinfo(spcb);
2355 *optsize = sizeof(struct sctp_pcbinfo);
2361 struct sctp_nets *net;
2362 struct sctp_status *sstat;
2364 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2365 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2368 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2373 * I think passing the state is fine since
2374 * sctp_constants.h will be available to the user
2377 sstat->sstat_state = stcb->asoc.state;
2378 sstat->sstat_assoc_id = sctp_get_associd(stcb);
2379 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2380 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2382 * We can't include chunks that have been passed to
2383 * the socket layer. Only things in queue.
2385 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2386 stcb->asoc.cnt_on_all_streams);
2389 sstat->sstat_instrms = stcb->asoc.streamincnt;
2390 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2391 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2392 memcpy(&sstat->sstat_primary.spinfo_address,
2393 &stcb->asoc.primary_destination->ro._l_addr,
2394 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2395 net = stcb->asoc.primary_destination;
2396 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2398 * Again the user can get info from sctp_constants.h
2399 * for what the state of the network is.
2401 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2402 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2403 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2404 sstat->sstat_primary.spinfo_rto = net->RTO;
2405 sstat->sstat_primary.spinfo_mtu = net->mtu;
2406 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2407 SCTP_TCB_UNLOCK(stcb);
2408 *optsize = sizeof(*sstat);
2413 struct sctp_rtoinfo *srto;
2415 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2416 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2419 srto->srto_initial = stcb->asoc.initial_rto;
2420 srto->srto_max = stcb->asoc.maxrto;
2421 srto->srto_min = stcb->asoc.minrto;
2422 SCTP_TCB_UNLOCK(stcb);
2424 SCTP_INP_RLOCK(inp);
2425 srto->srto_initial = inp->sctp_ep.initial_rto;
2426 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2427 srto->srto_min = inp->sctp_ep.sctp_minrto;
2428 SCTP_INP_RUNLOCK(inp);
2430 *optsize = sizeof(*srto);
2433 case SCTP_ASSOCINFO:
2435 struct sctp_assocparams *sasoc;
2438 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2439 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2442 oldval = sasoc->sasoc_cookie_life;
2443 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2444 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2445 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2446 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2447 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2448 SCTP_TCB_UNLOCK(stcb);
2450 SCTP_INP_RLOCK(inp);
2451 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2452 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2453 sasoc->sasoc_number_peer_destinations = 0;
2454 sasoc->sasoc_peer_rwnd = 0;
2455 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2456 SCTP_INP_RUNLOCK(inp);
2458 *optsize = sizeof(*sasoc);
2461 case SCTP_DEFAULT_SEND_PARAM:
2463 struct sctp_sndrcvinfo *s_info;
2465 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2466 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2469 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2470 SCTP_TCB_UNLOCK(stcb);
2472 SCTP_INP_RLOCK(inp);
2473 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2474 SCTP_INP_RUNLOCK(inp);
2476 *optsize = sizeof(*s_info);
2481 struct sctp_initmsg *sinit;
2483 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2484 SCTP_INP_RLOCK(inp);
2485 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2486 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2487 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2488 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2489 SCTP_INP_RUNLOCK(inp);
2490 *optsize = sizeof(*sinit);
2493 case SCTP_PRIMARY_ADDR:
2494 /* we allow a "get" operation on this */
2496 struct sctp_setprim *ssp;
2498 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2499 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2502 /* simply copy out the sockaddr_storage... */
2506 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2507 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2509 memcpy(&ssp->ssp_addr,
2510 &stcb->asoc.primary_destination->ro._l_addr,
2512 SCTP_TCB_UNLOCK(stcb);
2514 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2517 *optsize = sizeof(*ssp);
2521 case SCTP_HMAC_IDENT:
2523 struct sctp_hmacalgo *shmac;
2524 sctp_hmaclist_t *hmaclist;
2528 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2530 SCTP_INP_RLOCK(inp);
2531 hmaclist = inp->sctp_ep.local_hmacs;
2532 if (hmaclist == NULL) {
2533 /* no HMACs to return */
2534 *optsize = sizeof(*shmac);
2535 SCTP_INP_RUNLOCK(inp);
2538 /* is there room for all of the hmac ids? */
2539 size = sizeof(*shmac) + (hmaclist->num_algo *
2540 sizeof(shmac->shmac_idents[0]));
2541 if ((size_t)(*optsize) < size) {
2542 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2544 SCTP_INP_RUNLOCK(inp);
2547 /* copy in the list */
2548 shmac->shmac_number_of_idents = hmaclist->num_algo;
2549 for (i = 0; i < hmaclist->num_algo; i++) {
2550 shmac->shmac_idents[i] = hmaclist->hmac[i];
2552 SCTP_INP_RUNLOCK(inp);
2556 case SCTP_AUTH_ACTIVE_KEY:
2558 struct sctp_authkeyid *scact;
2560 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2561 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2564 /* get the active key on the assoc */
2565 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2566 SCTP_TCB_UNLOCK(stcb);
2568 /* get the endpoint active key */
2569 SCTP_INP_RLOCK(inp);
2570 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2571 SCTP_INP_RUNLOCK(inp);
2573 *optsize = sizeof(*scact);
2576 case SCTP_LOCAL_AUTH_CHUNKS:
2578 struct sctp_authchunks *sac;
2579 sctp_auth_chklist_t *chklist = NULL;
2582 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2583 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2586 /* get off the assoc */
2587 chklist = stcb->asoc.local_auth_chunks;
2588 /* is there enough space? */
2589 size = sctp_auth_get_chklist_size(chklist);
2590 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2594 /* copy in the chunks */
2595 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2597 SCTP_TCB_UNLOCK(stcb);
2599 /* get off the endpoint */
2600 SCTP_INP_RLOCK(inp);
2601 chklist = inp->sctp_ep.local_auth_chunks;
2602 /* is there enough space? */
2603 size = sctp_auth_get_chklist_size(chklist);
2604 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2606 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2608 /* copy in the chunks */
2609 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2611 SCTP_INP_RUNLOCK(inp);
2613 *optsize = sizeof(struct sctp_authchunks) + size;
2616 case SCTP_PEER_AUTH_CHUNKS:
2618 struct sctp_authchunks *sac;
2619 sctp_auth_chklist_t *chklist = NULL;
2622 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2623 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2626 /* get off the assoc */
2627 chklist = stcb->asoc.peer_auth_chunks;
2628 /* is there enough space? */
2629 size = sctp_auth_get_chklist_size(chklist);
2630 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2632 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2634 /* copy in the chunks */
2635 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2637 SCTP_TCB_UNLOCK(stcb);
2639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2642 *optsize = sizeof(struct sctp_authchunks) + size;
2648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2649 error = ENOPROTOOPT;
2652 } /* end switch (sopt->sopt_name) */
2657 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2662 struct sctp_tcb *stcb = NULL;
2663 struct sctp_inpcb *inp = NULL;
2666 if (optval == NULL) {
2667 SCTP_PRINTF("optval is NULL\n");
2668 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2671 inp = (struct sctp_inpcb *)so->so_pcb;
2673 SCTP_PRINTF("inp is NULL?\n");
2674 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2677 vrf_id = inp->def_vrf_id;
2682 case SCTP_AUTOCLOSE:
2683 case SCTP_AUTO_ASCONF:
2684 case SCTP_EXPLICIT_EOR:
2685 case SCTP_DISABLE_FRAGMENTS:
2686 case SCTP_USE_EXT_RCVINFO:
2687 case SCTP_I_WANT_MAPPED_V4_ADDR:
2688 /* copy in the option value */
2689 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2694 case SCTP_DISABLE_FRAGMENTS:
2695 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2697 case SCTP_AUTO_ASCONF:
2699 * NOTE: we don't really support this flag
2701 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2702 /* only valid for bound all sockets */
2703 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2705 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2709 case SCTP_EXPLICIT_EOR:
2710 set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2712 case SCTP_USE_EXT_RCVINFO:
2713 set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2715 case SCTP_I_WANT_MAPPED_V4_ADDR:
2716 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2717 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2719 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2724 set_opt = SCTP_PCB_FLAGS_NODELAY;
2726 case SCTP_AUTOCLOSE:
2727 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2728 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2732 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2734 * The value is in ticks. Note this does not effect
2735 * old associations, only new ones.
2737 inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2740 SCTP_INP_WLOCK(inp);
2742 sctp_feature_on(inp, set_opt);
2744 sctp_feature_off(inp, set_opt);
2746 SCTP_INP_WUNLOCK(inp);
2748 case SCTP_REUSE_PORT:
2750 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2751 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2752 /* Can't set it after we are bound */
2756 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2757 /* Can't do this for a 1-m socket */
2762 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2764 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
2767 case SCTP_PARTIAL_DELIVERY_POINT:
2771 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2772 if (*value > SCTP_SB_LIMIT_RCV(so)) {
2773 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2777 inp->partial_delivery_point = *value;
2780 case SCTP_FRAGMENT_INTERLEAVE:
2781 /* not yet until we re-write sctp_recvmsg() */
2785 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2786 if (*level == SCTP_FRAG_LEVEL_2) {
2787 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2788 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2789 } else if (*level == SCTP_FRAG_LEVEL_1) {
2790 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2791 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2792 } else if (*level == SCTP_FRAG_LEVEL_0) {
2793 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2794 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2797 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2802 case SCTP_CMT_ON_OFF:
2803 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
2804 struct sctp_assoc_value *av;
2806 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2807 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2809 if (av->assoc_value != 0)
2810 stcb->asoc.sctp_cmt_on_off = 1;
2812 stcb->asoc.sctp_cmt_on_off = 0;
2813 SCTP_TCB_UNLOCK(stcb);
2815 SCTP_INP_WLOCK(inp);
2816 if (av->assoc_value != 0)
2817 inp->sctp_cmt_on_off = 1;
2819 inp->sctp_cmt_on_off = 0;
2820 SCTP_INP_WUNLOCK(inp);
2823 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2824 error = ENOPROTOOPT;
2827 /* JRS - Set socket option for pluggable congestion control */
2828 case SCTP_PLUGGABLE_CC:
2830 struct sctp_assoc_value *av;
2832 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2833 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2835 switch (av->assoc_value) {
2837 * JRS - Standard TCP congestion
2840 case SCTP_CC_RFC2581:
2842 stcb->asoc.congestion_control_module = SCTP_CC_RFC2581;
2843 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2844 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_cwnd_update_after_sack;
2845 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_cwnd_update_after_fr;
2846 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2847 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2848 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2849 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2850 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2851 SCTP_TCB_UNLOCK(stcb);
2855 * JRS - High Speed TCP congestion
2860 stcb->asoc.congestion_control_module = SCTP_CC_HSTCP;
2861 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_set_initial_cc_param;
2862 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_hs_cwnd_update_after_sack;
2863 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_hs_cwnd_update_after_fr;
2864 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_cwnd_update_after_timeout;
2865 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_cwnd_update_after_ecn_echo;
2866 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2867 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2868 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_cwnd_update_after_fr_timer;
2869 SCTP_TCB_UNLOCK(stcb);
2872 /* JRS - HTCP congestion control */
2875 stcb->asoc.congestion_control_module = SCTP_CC_HTCP;
2876 stcb->asoc.cc_functions.sctp_set_initial_cc_param = &sctp_htcp_set_initial_cc_param;
2877 stcb->asoc.cc_functions.sctp_cwnd_update_after_sack = &sctp_htcp_cwnd_update_after_sack;
2878 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr = &sctp_htcp_cwnd_update_after_fr;
2879 stcb->asoc.cc_functions.sctp_cwnd_update_after_timeout = &sctp_htcp_cwnd_update_after_timeout;
2880 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo = &sctp_htcp_cwnd_update_after_ecn_echo;
2881 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped = &sctp_cwnd_update_after_packet_dropped;
2882 stcb->asoc.cc_functions.sctp_cwnd_update_after_output = &sctp_cwnd_update_after_output;
2883 stcb->asoc.cc_functions.sctp_cwnd_update_after_fr_timer = &sctp_htcp_cwnd_update_after_fr_timer;
2884 SCTP_TCB_UNLOCK(stcb);
2888 * JRS - All other values are
2893 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2895 SCTP_TCB_UNLOCK(stcb);
2900 switch (av->assoc_value) {
2901 case SCTP_CC_RFC2581:
2904 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
2907 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2914 case SCTP_CLR_STAT_LOG:
2915 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2920 struct sctp_assoc_value *av;
2922 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2923 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2926 stcb->asoc.context = av->assoc_value;
2927 SCTP_TCB_UNLOCK(stcb);
2929 SCTP_INP_WLOCK(inp);
2930 inp->sctp_context = av->assoc_value;
2931 SCTP_INP_WUNLOCK(inp);
2937 uint32_t *default_vrfid;
2939 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
2940 if (*default_vrfid > SCTP_MAX_VRF_ID) {
2941 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2945 inp->def_vrf_id = *default_vrfid;
2948 case SCTP_DEL_VRF_ID:
2950 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2954 case SCTP_ADD_VRF_ID:
2956 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2960 case SCTP_DELAYED_SACK:
2962 struct sctp_sack_info *sack;
2964 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
2965 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2966 if (sack->sack_delay) {
2967 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
2968 sack->sack_delay = SCTP_MAX_SACK_DELAY;
2971 if (sack->sack_delay) {
2972 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2973 sack->sack_delay = TICKS_TO_MSEC(1);
2975 stcb->asoc.delayed_ack = sack->sack_delay;
2977 if (sack->sack_freq) {
2978 stcb->asoc.sack_freq = sack->sack_freq;
2980 SCTP_TCB_UNLOCK(stcb);
2982 SCTP_INP_WLOCK(inp);
2983 if (sack->sack_delay) {
2984 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
2985 sack->sack_delay = TICKS_TO_MSEC(1);
2987 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
2989 if (sack->sack_freq) {
2990 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
2992 SCTP_INP_WUNLOCK(inp);
2996 case SCTP_AUTH_CHUNK:
2998 struct sctp_authchunk *sauth;
3000 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3002 SCTP_INP_WLOCK(inp);
3003 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3004 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3007 SCTP_INP_WUNLOCK(inp);
3012 struct sctp_authkey *sca;
3013 struct sctp_keyhead *shared_keys;
3014 sctp_sharedkey_t *shared_key;
3015 sctp_key_t *key = NULL;
3018 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3019 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3020 size = optsize - sizeof(*sca);
3023 /* set it on the assoc */
3024 shared_keys = &stcb->asoc.shared_keys;
3025 /* clear the cached keys for this key id */
3026 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3028 * create the new shared key and
3032 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3034 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3036 SCTP_TCB_UNLOCK(stcb);
3040 shared_key = sctp_alloc_sharedkey();
3041 if (shared_key == NULL) {
3043 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3045 SCTP_TCB_UNLOCK(stcb);
3048 shared_key->key = key;
3049 shared_key->keyid = sca->sca_keynumber;
3050 error = sctp_insert_sharedkey(shared_keys, shared_key);
3051 SCTP_TCB_UNLOCK(stcb);
3053 /* set it on the endpoint */
3054 SCTP_INP_WLOCK(inp);
3055 shared_keys = &inp->sctp_ep.shared_keys;
3057 * clear the cached keys on all assocs for
3060 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3062 * create the new shared key and
3066 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3068 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3070 SCTP_INP_WUNLOCK(inp);
3074 shared_key = sctp_alloc_sharedkey();
3075 if (shared_key == NULL) {
3077 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3079 SCTP_INP_WUNLOCK(inp);
3082 shared_key->key = key;
3083 shared_key->keyid = sca->sca_keynumber;
3084 error = sctp_insert_sharedkey(shared_keys, shared_key);
3085 SCTP_INP_WUNLOCK(inp);
3089 case SCTP_HMAC_IDENT:
3091 struct sctp_hmacalgo *shmac;
3092 sctp_hmaclist_t *hmaclist;
3098 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3099 if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3104 hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3105 if (hmaclist == NULL) {
3106 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3110 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3111 hmacid = shmac->shmac_idents[i];
3112 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3113 /* invalid HMACs were found */ ;
3114 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3116 sctp_free_hmaclist(hmaclist);
3117 goto sctp_set_hmac_done;
3121 for (i = 0; i < hmaclist->num_algo; i++) {
3122 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3123 /* already in list */
3128 sctp_free_hmaclist(hmaclist);
3129 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3133 /* set it on the endpoint */
3134 SCTP_INP_WLOCK(inp);
3135 if (inp->sctp_ep.local_hmacs)
3136 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3137 inp->sctp_ep.local_hmacs = hmaclist;
3138 SCTP_INP_WUNLOCK(inp);
3142 case SCTP_AUTH_ACTIVE_KEY:
3144 struct sctp_authkeyid *scact;
3146 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid,
3148 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3150 /* set the active key on the right place */
3152 /* set the active key on the assoc */
3153 if (sctp_auth_setactivekey(stcb,
3154 scact->scact_keynumber)) {
3155 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3156 SCTP_FROM_SCTP_USRREQ,
3160 SCTP_TCB_UNLOCK(stcb);
3162 /* set the active key on the endpoint */
3163 SCTP_INP_WLOCK(inp);
3164 if (sctp_auth_setactivekey_ep(inp,
3165 scact->scact_keynumber)) {
3166 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3167 SCTP_FROM_SCTP_USRREQ,
3171 SCTP_INP_WUNLOCK(inp);
3175 case SCTP_AUTH_DELETE_KEY:
3177 struct sctp_authkeyid *scdel;
3179 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid,
3181 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3183 /* delete the key from the right place */
3185 if (sctp_delete_sharedkey(stcb,
3186 scdel->scact_keynumber)) {
3187 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3188 SCTP_FROM_SCTP_USRREQ,
3192 SCTP_TCB_UNLOCK(stcb);
3194 SCTP_INP_WLOCK(inp);
3195 if (sctp_delete_sharedkey_ep(inp,
3196 scdel->scact_keynumber)) {
3197 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3198 SCTP_FROM_SCTP_USRREQ,
3202 SCTP_INP_WUNLOCK(inp);
3206 case SCTP_AUTH_DEACTIVATE_KEY:
3208 struct sctp_authkeyid *keyid;
3210 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid,
3212 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3214 /* deactivate the key from the right place */
3216 if (sctp_deact_sharedkey(stcb,
3217 keyid->scact_keynumber)) {
3218 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3219 SCTP_FROM_SCTP_USRREQ,
3223 SCTP_TCB_UNLOCK(stcb);
3225 SCTP_INP_WLOCK(inp);
3226 if (sctp_deact_sharedkey_ep(inp,
3227 keyid->scact_keynumber)) {
3228 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3229 SCTP_FROM_SCTP_USRREQ,
3233 SCTP_INP_WUNLOCK(inp);
3238 case SCTP_RESET_STREAMS:
3240 struct sctp_stream_reset *strrst;
3241 uint8_t send_in = 0, send_tsn = 0, send_out = 0,
3243 uint16_t addstrmcnt = 0;
3246 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
3247 SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
3250 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3254 if (stcb->asoc.peer_supports_strreset == 0) {
3256 * Peer does not support it, we return
3257 * protocol not supported since this is true
3258 * for this feature and this peer, not the
3259 * socket request in general.
3261 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
3262 error = EPROTONOSUPPORT;
3263 SCTP_TCB_UNLOCK(stcb);
3266 if (stcb->asoc.stream_reset_outstanding) {
3267 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3269 SCTP_TCB_UNLOCK(stcb);
3272 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
3274 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
3276 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
3279 } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
3281 } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
3285 /* We can't do that and add streams */
3289 if (stcb->asoc.stream_reset_outstanding) {
3294 /* We allocate here */
3295 addstrmcnt = strrst->strrst_num_streams;
3296 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
3297 /* You can't have more than 64k */
3301 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
3302 /* Need to allocate more */
3303 struct sctp_stream_out *oldstream;
3304 struct sctp_stream_queue_pending *sp;
3307 oldstream = stcb->asoc.strmout;
3309 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
3310 ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
3312 if (stcb->asoc.strmout == NULL) {
3313 stcb->asoc.strmout = oldstream;
3318 * Ok now we proceed with copying
3319 * the old out stuff and
3320 * initializing the new stuff.
3322 SCTP_TCB_SEND_LOCK(stcb);
3323 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3324 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3325 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
3326 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
3327 stcb->asoc.strmout[i].stream_no = i;
3328 if (oldstream[i].next_spoke.tqe_next) {
3329 sctp_remove_from_wheel(stcb, &stcb->asoc, &oldstream[i], 1);
3330 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3331 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3334 /* not on out wheel */
3335 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3336 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3340 * now anything on those
3343 while (TAILQ_EMPTY(&oldstream[i].outqueue) == 0) {
3344 sp = TAILQ_FIRST(&oldstream[i].outqueue);
3345 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
3346 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
3348 /* Did we disrupt the wheel? */
3350 sctp_insert_on_wheel(stcb,
3352 &stcb->asoc.strmout[i],
3356 * Now move assoc pointers
3359 if (stcb->asoc.last_out_stream == &oldstream[i]) {
3360 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
3362 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
3363 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
3366 /* now the new streams */
3367 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
3368 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
3369 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3370 stcb->asoc.strmout[i].stream_no = i;
3371 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3372 stcb->asoc.strmout[i].next_spoke.tqe_next = NULL;
3373 stcb->asoc.strmout[i].next_spoke.tqe_prev = NULL;
3375 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
3376 SCTP_FREE(oldstream, SCTP_M_STRMO);
3378 SCTP_TCB_SEND_UNLOCK(stcb);
3381 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3383 SCTP_TCB_UNLOCK(stcb);
3386 for (i = 0; i < strrst->strrst_num_streams; i++) {
3389 (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
3390 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3395 (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
3396 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3404 SCTP_TCB_UNLOCK(stcb);
3407 error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
3408 strrst->strrst_list,
3409 send_out, (stcb->asoc.str_reset_seq_in - 3),
3410 send_in, send_tsn, addstream, addstrmcnt);
3412 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
3413 SCTP_TCB_UNLOCK(stcb);
3417 case SCTP_CONNECT_X:
3418 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3419 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3423 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
3426 case SCTP_CONNECT_X_DELAYED:
3427 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
3428 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3432 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
3435 case SCTP_CONNECT_X_COMPLETE:
3437 struct sockaddr *sa;
3438 struct sctp_nets *net;
3440 /* FIXME MT: check correct? */
3441 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
3444 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3445 SCTP_INP_RLOCK(inp);
3446 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3448 SCTP_TCB_LOCK(stcb);
3449 net = sctp_findnet(stcb, sa);
3451 SCTP_INP_RUNLOCK(inp);
3454 * We increment here since
3455 * sctp_findassociation_ep_addr() wil do a
3456 * decrement if it finds the stcb as long as
3457 * the locked tcb (last argument) is NOT a
3460 SCTP_INP_INCR_REF(inp);
3461 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
3463 SCTP_INP_DECR_REF(inp);
3468 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3472 if (stcb->asoc.delayed_connection == 1) {
3473 stcb->asoc.delayed_connection = 0;
3474 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3475 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
3476 stcb->asoc.primary_destination,
3477 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
3478 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
3481 * already expired or did not use delayed
3484 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
3487 SCTP_TCB_UNLOCK(stcb);
3490 case SCTP_MAX_BURST:
3494 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
3496 SCTP_INP_WLOCK(inp);
3498 inp->sctp_ep.max_burst = *burst;
3500 SCTP_INP_WUNLOCK(inp);
3505 struct sctp_assoc_value *av;
3508 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3509 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3511 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3512 ovh = SCTP_MED_OVERHEAD;
3514 ovh = SCTP_MED_V4_OVERHEAD;
3517 if (av->assoc_value) {
3518 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
3520 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3522 SCTP_TCB_UNLOCK(stcb);
3524 SCTP_INP_WLOCK(inp);
3526 * FIXME MT: I think this is not in tune
3529 if (av->assoc_value) {
3530 inp->sctp_frag_point = (av->assoc_value + ovh);
3532 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
3534 SCTP_INP_WUNLOCK(inp);
3540 struct sctp_event_subscribe *events;
3542 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
3544 SCTP_INP_WLOCK(inp);
3545 if (events->sctp_data_io_event) {
3546 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3548 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
3551 if (events->sctp_association_event) {
3552 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3554 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
3557 if (events->sctp_address_event) {
3558 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3560 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
3563 if (events->sctp_send_failure_event) {
3564 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3566 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
3569 if (events->sctp_peer_error_event) {
3570 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3572 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
3575 if (events->sctp_shutdown_event) {
3576 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3578 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
3581 if (events->sctp_partial_delivery_event) {
3582 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3584 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
3587 if (events->sctp_adaptation_layer_event) {
3588 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3590 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
3593 if (events->sctp_authentication_event) {
3594 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3596 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
3599 if (events->sctp_sender_dry_event) {
3600 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
3601 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3602 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3603 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3605 SCTP_TCB_LOCK(stcb);
3608 TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3609 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3610 (stcb->asoc.stream_queue_cnt == 0)) {
3611 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
3614 SCTP_TCB_UNLOCK(stcb);
3618 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
3621 if (events->sctp_stream_reset_event) {
3622 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3624 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
3626 SCTP_INP_WUNLOCK(inp);
3630 case SCTP_ADAPTATION_LAYER:
3632 struct sctp_setadaptation *adap_bits;
3634 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
3635 SCTP_INP_WLOCK(inp);
3636 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
3637 SCTP_INP_WUNLOCK(inp);
3641 case SCTP_SET_INITIAL_DBG_SEQ:
3645 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
3646 SCTP_INP_WLOCK(inp);
3647 inp->sctp_ep.initial_sequence_debug = *vvv;
3648 SCTP_INP_WUNLOCK(inp);
3652 case SCTP_DEFAULT_SEND_PARAM:
3654 struct sctp_sndrcvinfo *s_info;
3656 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
3657 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3660 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
3661 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
3663 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3666 SCTP_TCB_UNLOCK(stcb);
3668 SCTP_INP_WLOCK(inp);
3669 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
3670 SCTP_INP_WUNLOCK(inp);
3674 case SCTP_PEER_ADDR_PARAMS:
3675 /* Applys to the specific association */
3677 struct sctp_paddrparams *paddrp;
3678 struct sctp_nets *net;
3680 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3681 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3684 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3687 * We increment here since
3688 * sctp_findassociation_ep_addr() wil do a
3689 * decrement if it finds the stcb as long as
3690 * the locked tcb (last argument) is NOT a
3693 SCTP_INP_INCR_REF(inp);
3694 stcb = sctp_findassociation_ep_addr(&inp,
3695 (struct sockaddr *)&paddrp->spp_address,
3698 SCTP_INP_DECR_REF(inp);
3701 if (stcb && (net == NULL)) {
3702 struct sockaddr *sa;
3704 sa = (struct sockaddr *)&paddrp->spp_address;
3705 if (sa->sa_family == AF_INET) {
3706 struct sockaddr_in *sin;
3708 sin = (struct sockaddr_in *)sa;
3709 if (sin->sin_addr.s_addr) {
3710 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3711 SCTP_TCB_UNLOCK(stcb);
3715 } else if (sa->sa_family == AF_INET6) {
3716 struct sockaddr_in6 *sin6;
3718 sin6 = (struct sockaddr_in6 *)sa;
3719 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3720 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3721 SCTP_TCB_UNLOCK(stcb);
3726 error = EAFNOSUPPORT;
3727 SCTP_TCB_UNLOCK(stcb);
3728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3733 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
3735 SCTP_TCB_UNLOCK(stcb);
3736 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3739 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
3741 SCTP_TCB_UNLOCK(stcb);
3742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3746 /************************TCB SPECIFIC SET ******************/
3748 * do we change the timer for HB, we run
3753 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3754 ovh = SCTP_MED_OVERHEAD;
3756 ovh = SCTP_MED_V4_OVERHEAD;
3759 if (paddrp->spp_hbinterval)
3760 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3761 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3762 stcb->asoc.heart_beat_delay = 0;
3764 /* network sets ? */
3766 /************************NET SPECIFIC SET ******************/
3767 if (paddrp->spp_flags & SPP_HB_DEMAND) {
3769 if (sctp_send_hb(stcb, 1, net) < 0) {
3770 /* asoc destroyed */
3771 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3776 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3777 net->dest_state |= SCTP_ADDR_NOHB;
3779 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3780 net->dest_state &= ~SCTP_ADDR_NOHB;
3782 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3783 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3784 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3785 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3787 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3788 net->mtu = paddrp->spp_pathmtu + ovh;
3789 if (net->mtu < stcb->asoc.smallest_mtu) {
3790 #ifdef SCTP_PRINT_FOR_B_AND_M
3791 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3794 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3798 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3799 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3800 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3803 if (paddrp->spp_pathmaxrxt)
3804 net->failure_threshold = paddrp->spp_pathmaxrxt;
3806 if (paddrp->spp_flags & SPP_IPV4_TOS) {
3807 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3808 net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3813 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3814 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3815 net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3820 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3821 if (paddrp->spp_pathmaxrxt)
3822 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3824 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3825 /* Turn back on the timer */
3826 stcb->asoc.hb_is_disabled = 0;
3827 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3829 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
3830 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3831 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3832 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3833 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3835 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3836 net->mtu = paddrp->spp_pathmtu + ovh;
3837 if (net->mtu < stcb->asoc.smallest_mtu) {
3838 #ifdef SCTP_PRINT_FOR_B_AND_M
3839 SCTP_PRINTF("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3842 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3847 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3848 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3849 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3850 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3854 if (paddrp->spp_flags & SPP_HB_DISABLE) {
3855 int cnt_of_unconf = 0;
3856 struct sctp_nets *lnet;
3858 stcb->asoc.hb_is_disabled = 1;
3859 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3860 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3865 * stop the timer ONLY if we
3866 * have no unconfirmed
3869 if (cnt_of_unconf == 0) {
3870 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3871 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
3872 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3876 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3877 /* start up the timer. */
3878 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3879 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3883 if (paddrp->spp_flags & SPP_IPV4_TOS)
3884 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3887 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3888 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3892 SCTP_TCB_UNLOCK(stcb);
3894 /************************NO TCB, SET TO default stuff ******************/
3895 SCTP_INP_WLOCK(inp);
3897 * For the TOS/FLOWLABEL stuff you set it
3898 * with the options on the socket
3900 if (paddrp->spp_pathmaxrxt) {
3901 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3903 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3904 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
3905 else if (paddrp->spp_hbinterval) {
3906 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
3907 paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
3908 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3910 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3911 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3913 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3914 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3916 SCTP_INP_WUNLOCK(inp);
3922 struct sctp_rtoinfo *srto;
3923 uint32_t new_init, new_min, new_max;
3925 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3926 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3929 if (srto->srto_initial)
3930 new_init = srto->srto_initial;
3932 new_init = stcb->asoc.initial_rto;
3934 new_max = srto->srto_max;
3936 new_max = stcb->asoc.maxrto;
3938 new_min = srto->srto_min;
3940 new_min = stcb->asoc.minrto;
3941 if ((new_min <= new_init) && (new_init <= new_max)) {
3942 stcb->asoc.initial_rto = new_init;
3943 stcb->asoc.maxrto = new_max;
3944 stcb->asoc.minrto = new_min;
3946 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3949 SCTP_TCB_UNLOCK(stcb);
3951 SCTP_INP_WLOCK(inp);
3952 if (srto->srto_initial)
3953 new_init = srto->srto_initial;
3955 new_init = inp->sctp_ep.initial_rto;
3957 new_max = srto->srto_max;
3959 new_max = inp->sctp_ep.sctp_maxrto;
3961 new_min = srto->srto_min;
3963 new_min = inp->sctp_ep.sctp_minrto;
3964 if ((new_min <= new_init) && (new_init <= new_max)) {
3965 inp->sctp_ep.initial_rto = new_init;
3966 inp->sctp_ep.sctp_maxrto = new_max;
3967 inp->sctp_ep.sctp_minrto = new_min;
3969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3972 SCTP_INP_WUNLOCK(inp);
3976 case SCTP_ASSOCINFO:
3978 struct sctp_assocparams *sasoc;
3980 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3981 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3982 if (sasoc->sasoc_cookie_life) {
3983 /* boundary check the cookie life */
3984 if (sasoc->sasoc_cookie_life < 1000)
3985 sasoc->sasoc_cookie_life = 1000;
3986 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
3987 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
3991 if (sasoc->sasoc_asocmaxrxt)
3992 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3993 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3994 sasoc->sasoc_peer_rwnd = 0;
3995 sasoc->sasoc_local_rwnd = 0;
3996 if (sasoc->sasoc_cookie_life) {
3997 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
3999 SCTP_TCB_UNLOCK(stcb);
4001 SCTP_INP_WLOCK(inp);
4002 if (sasoc->sasoc_asocmaxrxt)
4003 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4004 sasoc->sasoc_number_peer_destinations = 0;
4005 sasoc->sasoc_peer_rwnd = 0;
4006 sasoc->sasoc_local_rwnd = 0;
4007 if (sasoc->sasoc_cookie_life) {
4008 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4010 SCTP_INP_WUNLOCK(inp);
4016 struct sctp_initmsg *sinit;
4018 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4019 SCTP_INP_WLOCK(inp);
4020 if (sinit->sinit_num_ostreams)
4021 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4023 if (sinit->sinit_max_instreams)
4024 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4026 if (sinit->sinit_max_attempts)
4027 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4029 if (sinit->sinit_max_init_timeo)
4030 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4031 SCTP_INP_WUNLOCK(inp);
4034 case SCTP_PRIMARY_ADDR:
4036 struct sctp_setprim *spa;
4037 struct sctp_nets *net, *lnet;
4039 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
4040 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
4044 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
4047 * We increment here since
4048 * sctp_findassociation_ep_addr() wil do a
4049 * decrement if it finds the stcb as long as
4050 * the locked tcb (last argument) is NOT a
4053 SCTP_INP_INCR_REF(inp);
4054 stcb = sctp_findassociation_ep_addr(&inp,
4055 (struct sockaddr *)&spa->ssp_addr,
4058 SCTP_INP_DECR_REF(inp);
4062 if ((stcb) && (net)) {
4063 if ((net != stcb->asoc.primary_destination) &&
4064 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
4065 /* Ok we need to set it */
4066 lnet = stcb->asoc.primary_destination;
4067 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
4068 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
4069 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
4071 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
4075 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4079 SCTP_TCB_UNLOCK(stcb);
4083 case SCTP_SET_DYNAMIC_PRIMARY:
4085 union sctp_sockstore *ss;
4087 error = priv_check(curthread,
4088 PRIV_NETINET_RESERVEDPORT);
4092 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
4093 /* SUPER USER CHECK? */
4094 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
4097 case SCTP_SET_PEER_PRIMARY_ADDR:
4099 struct sctp_setpeerprim *sspp;
4101 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
4102 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
4104 struct sctp_ifa *ifa;
4106 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
4107 stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
4109 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4113 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4115 * Must validate the ifa found is in
4118 struct sctp_laddr *laddr;
4121 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4122 if (laddr->ifa == NULL) {
4123 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4127 if (laddr->ifa == ifa) {
4133 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4138 if (sctp_set_primary_ip_address_sa(stcb,
4139 (struct sockaddr *)&sspp->sspp_addr) != 0) {
4140 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4144 SCTP_TCB_UNLOCK(stcb);
4146 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4152 case SCTP_BINDX_ADD_ADDR:
4154 struct sctp_getaddresses *addrs;
4158 td = (struct thread *)p;
4159 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
4161 if (addrs->addr->sa_family == AF_INET) {
4162 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4164 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4168 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4169 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4173 } else if (addrs->addr->sa_family == AF_INET6) {
4174 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4176 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4180 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4181 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4182 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4187 error = EAFNOSUPPORT;
4190 sctp_bindx_add_address(so, inp, addrs->addr,
4191 addrs->sget_assoc_id, vrf_id,
4195 case SCTP_BINDX_REM_ADDR:
4197 struct sctp_getaddresses *addrs;
4201 td = (struct thread *)p;
4203 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
4204 if (addrs->addr->sa_family == AF_INET) {
4205 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
4207 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4211 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
4212 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4216 } else if (addrs->addr->sa_family == AF_INET6) {
4217 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
4219 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4223 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
4224 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
4225 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
4230 error = EAFNOSUPPORT;
4233 sctp_bindx_delete_address(so, inp, addrs->addr,
4234 addrs->sget_assoc_id, vrf_id,
4239 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4240 error = ENOPROTOOPT;
4242 } /* end switch (opt) */
4247 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
4249 void *optval = NULL;
4251 struct sctp_inpcb *inp;
4255 inp = (struct sctp_inpcb *)so->so_pcb;
4257 /* I made the same as TCP since we are not setup? */
4258 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4259 return (ECONNRESET);
4261 if (sopt->sopt_level != IPPROTO_SCTP) {
4262 /* wrong proto level... send back up to IP */
4264 if (INP_CHECK_SOCKAF(so, AF_INET6))
4265 error = ip6_ctloutput(so, sopt);
4268 error = ip_ctloutput(so, sopt);
4271 optsize = sopt->sopt_valsize;
4273 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
4274 if (optval == NULL) {
4275 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
4278 error = sooptcopyin(sopt, optval, optsize, optsize);
4280 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4284 p = (void *)sopt->sopt_td;
4285 if (sopt->sopt_dir == SOPT_SET) {
4286 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
4287 } else if (sopt->sopt_dir == SOPT_GET) {
4288 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
4290 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4293 if ((error == 0) && (optval != NULL)) {
4294 error = sooptcopyout(sopt, optval, optsize);
4295 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4296 } else if (optval != NULL) {
4297 SCTP_FREE(optval, SCTP_M_SOCKOPT);
4305 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
4308 int create_lock_on = 0;
4310 struct sctp_inpcb *inp;
4311 struct sctp_tcb *stcb = NULL;
4313 inp = (struct sctp_inpcb *)so->so_pcb;
4315 /* I made the same as TCP since we are not setup? */
4316 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4317 return (ECONNRESET);
4320 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4324 if (addr->sa_family == AF_INET6) {
4325 struct sockaddr_in6 *sin6p;
4327 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
4328 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4331 sin6p = (struct sockaddr_in6 *)addr;
4332 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
4333 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4338 if (addr->sa_family == AF_INET) {
4339 struct sockaddr_in *sinp;
4341 if (addr->sa_len != sizeof(struct sockaddr_in)) {
4342 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4345 sinp = (struct sockaddr_in *)addr;
4346 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
4347 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4351 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
4352 return (EAFNOSUPPORT);
4354 SCTP_INP_INCR_REF(inp);
4355 SCTP_ASOC_CREATE_LOCK(inp);
4359 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4360 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4361 /* Should I really unlock ? */
4362 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
4367 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
4368 (addr->sa_family == AF_INET6)) {
4369 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4374 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
4375 SCTP_PCB_FLAGS_UNBOUND) {
4376 /* Bind a ephemeral port */
4377 error = sctp_inpcb_bind(so, NULL, NULL, p);
4382 /* Now do we connect? */
4383 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4384 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
4385 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4389 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4390 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4391 /* We are already connected AND the TCP model */
4392 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4396 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4397 SCTP_INP_RLOCK(inp);
4398 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4399 SCTP_INP_RUNLOCK(inp);
4402 * We increment here since sctp_findassociation_ep_addr()
4403 * will do a decrement if it finds the stcb as long as the
4404 * locked tcb (last argument) is NOT a TCB.. aka NULL.
4406 SCTP_INP_INCR_REF(inp);
4407 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
4409 SCTP_INP_DECR_REF(inp);
4411 SCTP_TCB_UNLOCK(stcb);
4415 /* Already have or am bring up an association */
4416 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4420 vrf_id = inp->def_vrf_id;
4421 /* We are GOOD to go */
4422 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
4424 /* Gak! no memory */
4427 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
4428 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4429 /* Set the connected flag so we can queue data */
4430 SOCKBUF_LOCK(&so->so_rcv);
4431 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
4432 SOCKBUF_UNLOCK(&so->so_rcv);
4433 SOCKBUF_LOCK(&so->so_snd);
4434 so->so_snd.sb_state &= ~SBS_CANTSENDMORE;
4435 SOCKBUF_UNLOCK(&so->so_snd);
4437 so->so_state &= ~SS_ISDISCONNECTING;
4441 SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
4442 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4444 /* initialize authentication parameters for the assoc */
4445 sctp_initialize_auth_params(inp, stcb);
4447 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4448 SCTP_TCB_UNLOCK(stcb);
4450 if (create_lock_on) {
4451 SCTP_ASOC_CREATE_UNLOCK(inp);
4453 SCTP_INP_DECR_REF(inp);
4458 sctp_listen(struct socket *so, int backlog, struct thread *p)
4461 * Note this module depends on the protocol processing being called
4462 * AFTER any socket level flags and backlog are applied to the
4463 * socket. The traditional way that the socket flags are applied is
4464 * AFTER protocol processing. We have made a change to the
4465 * sys/kern/uipc_socket.c module to reverse this but this MUST be in
4466 * place if the socket API for SCTP is to work properly.
4470 struct sctp_inpcb *inp;
4472 inp = (struct sctp_inpcb *)so->so_pcb;
4474 /* I made the same as TCP since we are not setup? */
4475 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4476 return (ECONNRESET);
4478 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
4479 /* See if we have a listener */
4480 struct sctp_inpcb *tinp;
4481 union sctp_sockstore store, *sp;
4484 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4486 struct sctp_laddr *laddr;
4488 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4489 memcpy(&store, &laddr->ifa->address, sizeof(store));
4490 sp->sin.sin_port = inp->sctp_lport;
4491 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4492 if (tinp && (tinp != inp) &&
4493 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4494 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4495 (tinp->sctp_socket->so_qlimit)) {
4497 * we have a listener already and
4500 SCTP_INP_DECR_REF(tinp);
4501 return (EADDRINUSE);
4503 SCTP_INP_DECR_REF(tinp);
4507 /* Setup a local addr bound all */
4508 memset(&store, 0, sizeof(store));
4509 store.sin.sin_port = inp->sctp_lport;
4511 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4512 store.sa.sa_family = AF_INET6;
4513 store.sa.sa_len = sizeof(struct sockaddr_in6);
4516 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
4517 store.sa.sa_family = AF_INET;
4518 store.sa.sa_len = sizeof(struct sockaddr_in);
4520 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
4521 if (tinp && (tinp != inp) &&
4522 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
4523 ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
4524 (tinp->sctp_socket->so_qlimit)) {
4526 * we have a listener already and its not
4529 SCTP_INP_DECR_REF(tinp);
4530 return (EADDRINUSE);
4532 SCTP_INP_DECR_REF(inp);
4536 SCTP_INP_RLOCK(inp);
4537 #ifdef SCTP_LOCK_LOGGING
4538 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
4539 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
4543 error = solisten_proto_check(so);
4546 SCTP_INP_RUNLOCK(inp);
4549 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
4550 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4552 * The unlucky case - We are in the tcp pool with this guy.
4553 * - Someone else is in the main inp slot. - We must move
4554 * this guy (the listener) to the main slot - We must then
4555 * move the guy that was listener to the TCP Pool.
4557 if (sctp_swap_inpcb_for_listen(inp)) {
4561 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4562 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4563 /* We are already connected AND the TCP model */
4565 SCTP_INP_RUNLOCK(inp);
4567 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
4568 return (EADDRINUSE);
4570 SCTP_INP_RUNLOCK(inp);
4571 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4572 /* We must do a bind. */
4574 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
4575 /* bind error, probably perm */
4580 /* It appears for 7.0 and on, we must always call this. */
4581 solisten_proto(so, backlog);
4582 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4583 /* remove the ACCEPTCONN flag for one-to-many sockets */
4584 so->so_options &= ~SO_ACCEPTCONN;
4587 /* turning off listen */
4588 so->so_options &= ~SO_ACCEPTCONN;
4594 static int sctp_defered_wakeup_cnt = 0;
4597 sctp_accept(struct socket *so, struct sockaddr **addr)
4599 struct sctp_tcb *stcb;
4600 struct sctp_inpcb *inp;
4601 union sctp_sockstore store;
4607 inp = (struct sctp_inpcb *)so->so_pcb;
4610 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4611 return (ECONNRESET);
4613 SCTP_INP_RLOCK(inp);
4614 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4615 SCTP_INP_RUNLOCK(inp);
4616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4617 return (EOPNOTSUPP);
4619 if (so->so_state & SS_ISDISCONNECTED) {
4620 SCTP_INP_RUNLOCK(inp);
4621 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
4622 return (ECONNABORTED);
4624 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4626 SCTP_INP_RUNLOCK(inp);
4627 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4628 return (ECONNRESET);
4630 SCTP_TCB_LOCK(stcb);
4631 SCTP_INP_RUNLOCK(inp);
4632 store = stcb->asoc.primary_destination->ro._l_addr;
4633 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4634 SCTP_TCB_UNLOCK(stcb);
4635 switch (store.sa.sa_family) {
4638 struct sockaddr_in *sin;
4640 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4643 sin->sin_family = AF_INET;
4644 sin->sin_len = sizeof(*sin);
4645 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
4646 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
4647 *addr = (struct sockaddr *)sin;
4653 struct sockaddr_in6 *sin6;
4655 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
4658 sin6->sin6_family = AF_INET6;
4659 sin6->sin6_len = sizeof(*sin6);
4660 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
4662 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
4663 if ((error = sa6_recoverscope(sin6)) != 0) {
4664 SCTP_FREE_SONAME(sin6);
4667 *addr = (struct sockaddr *)sin6;
4675 /* Wake any delayed sleep action */
4676 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4677 SCTP_INP_WLOCK(inp);
4678 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4679 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4680 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4681 SCTP_INP_WUNLOCK(inp);
4682 SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
4683 if (sowriteable(inp->sctp_socket)) {
4684 sowwakeup_locked(inp->sctp_socket);
4686 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
4688 SCTP_INP_WLOCK(inp);
4690 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4691 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4692 SCTP_INP_WUNLOCK(inp);
4693 SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
4694 if (soreadable(inp->sctp_socket)) {
4695 sctp_defered_wakeup_cnt++;
4696 sorwakeup_locked(inp->sctp_socket);
4698 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
4700 SCTP_INP_WLOCK(inp);
4702 SCTP_INP_WUNLOCK(inp);
4704 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4705 SCTP_TCB_LOCK(stcb);
4706 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
4712 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4714 struct sockaddr_in *sin;
4716 struct sctp_inpcb *inp;
4717 struct sctp_ifa *sctp_ifa;
4720 * Do the malloc first in case it blocks.
4722 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4725 sin->sin_family = AF_INET;
4726 sin->sin_len = sizeof(*sin);
4727 inp = (struct sctp_inpcb *)so->so_pcb;
4729 SCTP_FREE_SONAME(sin);
4730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4733 SCTP_INP_RLOCK(inp);
4734 sin->sin_port = inp->sctp_lport;
4735 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4736 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4737 struct sctp_tcb *stcb;
4738 struct sockaddr_in *sin_a;
4739 struct sctp_nets *net;
4742 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4748 SCTP_TCB_LOCK(stcb);
4749 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4750 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4752 /* this will make coverity happy */
4755 if (sin_a->sin_family == AF_INET) {
4760 if ((!fnd) || (sin_a == NULL)) {
4762 SCTP_TCB_UNLOCK(stcb);
4765 vrf_id = inp->def_vrf_id;
4766 sctp_ifa = sctp_source_address_selection(inp,
4768 (sctp_route_t *) & net->ro,
4771 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
4772 sctp_free_ifa(sctp_ifa);
4774 SCTP_TCB_UNLOCK(stcb);
4776 /* For the bound all case you get back 0 */
4778 sin->sin_addr.s_addr = 0;
4782 /* Take the first IPv4 address in the list */
4783 struct sctp_laddr *laddr;
4786 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4787 if (laddr->ifa->address.sa.sa_family == AF_INET) {
4788 struct sockaddr_in *sin_a;
4790 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
4791 sin->sin_addr = sin_a->sin_addr;
4797 SCTP_FREE_SONAME(sin);
4798 SCTP_INP_RUNLOCK(inp);
4799 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4803 SCTP_INP_RUNLOCK(inp);
4804 (*addr) = (struct sockaddr *)sin;
4809 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4811 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4813 struct sockaddr_in *sin_a;
4814 struct sctp_inpcb *inp;
4815 struct sctp_tcb *stcb;
4816 struct sctp_nets *net;
4818 /* Do the malloc first in case it blocks. */
4819 inp = (struct sctp_inpcb *)so->so_pcb;
4820 if ((inp == NULL) ||
4821 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4822 /* UDP type and listeners will drop out here */
4823 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
4826 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
4829 sin->sin_family = AF_INET;
4830 sin->sin_len = sizeof(*sin);
4832 /* We must recapture incase we blocked */
4833 inp = (struct sctp_inpcb *)so->so_pcb;
4835 SCTP_FREE_SONAME(sin);
4836 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4839 SCTP_INP_RLOCK(inp);
4840 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4842 SCTP_TCB_LOCK(stcb);
4844 SCTP_INP_RUNLOCK(inp);
4846 SCTP_FREE_SONAME(sin);
4847 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4851 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4852 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4853 if (sin_a->sin_family == AF_INET) {
4855 sin->sin_port = stcb->rport;
4856 sin->sin_addr = sin_a->sin_addr;
4860 SCTP_TCB_UNLOCK(stcb);
4862 /* No IPv4 address */
4863 SCTP_FREE_SONAME(sin);
4864 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4867 (*addr) = (struct sockaddr *)sin;
4871 struct pr_usrreqs sctp_usrreqs = {
4872 .pru_abort = sctp_abort,
4873 .pru_accept = sctp_accept,
4874 .pru_attach = sctp_attach,
4875 .pru_bind = sctp_bind,
4876 .pru_connect = sctp_connect,
4877 .pru_control = in_control,
4878 .pru_close = sctp_close,
4879 .pru_detach = sctp_close,
4880 .pru_sopoll = sopoll_generic,
4881 .pru_flush = sctp_flush,
4882 .pru_disconnect = sctp_disconnect,
4883 .pru_listen = sctp_listen,
4884 .pru_peeraddr = sctp_peeraddr,
4885 .pru_send = sctp_sendm,
4886 .pru_shutdown = sctp_shutdown,
4887 .pru_sockaddr = sctp_ingetaddr,
4888 .pru_sosend = sctp_sosend,
4889 .pru_soreceive = sctp_soreceive