2 * Copyright (c) 2001-2007, 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.
30 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
31 #include <sys/cdefs.h>
32 __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>
41 #include <netinet6/sctp6_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_output.h>
53 #include <netinet/sctp_bsd_addr.h>
56 extern struct protosw inetsw[];
62 sctp6_input(i_pak, offp, proto)
70 struct sctp_inpcb *in6p = NULL;
71 struct sctp_nets *net;
73 uint32_t check, calc_check;
75 struct inpcb *in6p_ip;
76 struct sctp_chunkhdr *ch;
77 int length, mlen, offset, iphlen;
79 struct sctp_tcb *stcb = NULL;
83 /* get the VRF and table id's */
84 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
85 SCTP_RELEASE_PKT(*i_pak);
88 m = SCTP_HEADER_TO_CHAIN(*i_pak);
89 pkt_len = SCTP_HEADER_LEN((*i_pak));
91 #ifdef SCTP_PACKET_LOGGING
92 sctp_packet_log(m, pkt_len);
94 ip6 = mtod(m, struct ip6_hdr *);
95 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
96 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
97 (int)(sizeof(*sh) + sizeof(*ch)));
99 SCTP_STAT_INCR(sctps_hdrops);
102 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
104 offset = iphlen + sizeof(*sh) + sizeof(*ch);
106 #if defined(NFAITH) && NFAITH > 0
108 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
109 /* XXX send icmp6 host/port unreach? */
112 #endif /* NFAITH defined and > 0 */
113 SCTP_STAT_INCR(sctps_recvpackets);
114 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
115 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
117 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
118 /* No multi-cast support in SCTP */
121 /* destination port of 0 is illegal, based on RFC2960. */
122 if (sh->dest_port == 0)
124 if ((sctp_no_csum_on_loopback == 0) ||
125 (!SCTP_IS_IT_LOOPBACK(m))) {
127 * we do NOT validate things from the loopback if the sysctl
130 check = sh->checksum; /* save incoming checksum */
131 if ((check == 0) && (sctp_no_csum_on_loopback)) {
133 * special hook for where we got a local address
134 * somehow routed across a non IFT_LOOP type
137 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
140 sh->checksum = 0; /* prepare for calc */
141 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
142 if (calc_check != check) {
143 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
144 calc_check, check, m, mlen, iphlen);
145 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
146 sh, ch, &in6p, &net, vrf_id);
147 /* in6p's ref-count increased && stcb locked */
148 if ((in6p) && (stcb)) {
149 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
150 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
151 } else if ((in6p != NULL) && (stcb == NULL)) {
154 SCTP_STAT_INCR(sctps_badsum);
155 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
158 sh->checksum = calc_check;
163 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
164 * IP/SCTP/first chunk header...
166 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
167 sh, ch, &in6p, &net, vrf_id);
168 /* in6p's ref-count increased */
170 struct sctp_init_chunk *init_chk, chunk_buf;
172 SCTP_STAT_INCR(sctps_noport);
173 if (ch->chunk_type == SCTP_INITIATION) {
175 * we do a trick here to get the INIT tag, dig in
176 * and get the tag from the INIT and put it in the
179 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
180 iphlen + sizeof(*sh), sizeof(*init_chk),
181 (uint8_t *) & chunk_buf);
183 sh->v_tag = init_chk->init.initiate_tag;
187 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
188 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
191 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
194 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
195 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
197 } else if (stcb == NULL) {
200 in6p_ip = (struct inpcb *)in6p;
203 * Check AH/ESP integrity.
205 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
207 ipsec6stat.in_polvio++;
213 * CONTROL chunk processing
215 offset -= sizeof(*ch);
216 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
218 /* Length now holds the total packet length payload + iphlen */
219 length = ntohs(ip6->ip6_plen) + iphlen;
221 /* sa_ignore NO_NULL_CHK */
222 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
223 in6p, stcb, net, ecn_bits, vrf_id);
224 /* inp's ref-count reduced && stcb unlocked */
225 /* XXX this stuff below gets moved to appropriate parts later... */
228 if ((in6p) && refcount_up) {
229 /* reduce ref-count */
230 SCTP_INP_WLOCK(in6p);
231 SCTP_INP_DECR_REF(in6p);
232 SCTP_INP_WUNLOCK(in6p);
238 SCTP_TCB_UNLOCK(stcb);
240 if ((in6p) && refcount_up) {
241 /* reduce ref-count */
242 SCTP_INP_WLOCK(in6p);
243 SCTP_INP_DECR_REF(in6p);
244 SCTP_INP_WUNLOCK(in6p);
253 sctp6_notify_mbuf(struct sctp_inpcb *inp,
254 struct icmp6_hdr *icmp6,
256 struct sctp_tcb *stcb,
257 struct sctp_nets *net)
261 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
262 (icmp6 == NULL) || (sh == NULL)) {
265 /* First do we even look at it? */
266 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
269 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
270 /* not PACKET TO BIG */
274 * ok we need to look closely. We could even get smarter and look at
275 * anyone that we sent to in case we get a different ICMP that tells
276 * us there is no way to reach a host, but for this impl, all we
277 * care about is MTU discovery.
279 nxtsz = ntohl(icmp6->icmp6_mtu);
280 /* Stop any PMTU timer */
281 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
283 /* Adjust destination size limit */
284 if (net->mtu > nxtsz) {
287 /* now what about the ep? */
288 if (stcb->asoc.smallest_mtu > nxtsz) {
289 struct sctp_tmit_chunk *chk;
291 /* Adjust that too */
292 stcb->asoc.smallest_mtu = nxtsz;
293 /* now off to subtract IP_DF flag if needed */
295 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
296 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
297 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
300 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
301 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
303 * For this guy we also mark for immediate
304 * resend since we sent to big of chunk
306 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
307 if (chk->sent != SCTP_DATAGRAM_RESEND)
308 stcb->asoc.sent_queue_retran_cnt++;
309 chk->sent = SCTP_DATAGRAM_RESEND;
310 chk->rec.data.doing_fast_retransmit = 0;
312 chk->sent = SCTP_DATAGRAM_RESEND;
313 /* Clear any time so NO RTT is being done */
314 chk->sent_rcv_time.tv_sec = 0;
315 chk->sent_rcv_time.tv_usec = 0;
316 stcb->asoc.total_flight -= chk->send_size;
317 net->flight_size -= chk->send_size;
321 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
324 SCTP_TCB_UNLOCK(stcb);
330 sctp6_ctlinput(cmd, pktdst, d)
332 struct sockaddr *pktdst;
336 struct ip6ctlparam *ip6cp = NULL;
340 vrf_id = SCTP_DEFAULT_VRFID;
342 if (pktdst->sa_family != AF_INET6 ||
343 pktdst->sa_len != sizeof(struct sockaddr_in6))
346 if ((unsigned)cmd >= PRC_NCMDS)
348 if (PRC_IS_REDIRECT(cmd)) {
350 } else if (inet6ctlerrmap[cmd] == 0) {
353 /* if the parameter is from icmp6, decode it. */
355 ip6cp = (struct ip6ctlparam *)d;
357 ip6cp = (struct ip6ctlparam *)NULL;
362 * XXX: We assume that when IPV6 is non NULL, M and OFF are
365 /* check if we can safely examine src and dst ports */
366 struct sctp_inpcb *inp = NULL;
367 struct sctp_tcb *stcb = NULL;
368 struct sctp_nets *net = NULL;
369 struct sockaddr_in6 final;
371 if (ip6cp->ip6c_m == NULL)
374 bzero(&sh, sizeof(sh));
375 bzero(&final, sizeof(final));
378 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
380 ip6cp->ip6c_src->sin6_port = sh.src_port;
381 final.sin6_len = sizeof(final);
382 final.sin6_family = AF_INET6;
383 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
384 final.sin6_port = sh.dest_port;
385 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
386 (struct sockaddr *)&final,
387 &inp, &net, 1, vrf_id);
388 /* inp's ref-count increased && stcb locked */
389 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
390 if (cmd == PRC_MSGSIZE) {
391 sctp6_notify_mbuf(inp,
396 /* inp's ref-count reduced && stcb unlocked */
398 if (cmd == PRC_HOSTDEAD) {
401 cm = inet6ctlerrmap[cmd];
403 sctp_notify(inp, cm, &sh,
404 (struct sockaddr *)&final,
406 /* inp's ref-count reduced && stcb unlocked */
409 if (PRC_IS_REDIRECT(cmd) && inp) {
410 in6_rtchange((struct in6pcb *)inp,
411 inet6ctlerrmap[cmd]);
414 /* reduce inp's ref-count */
416 SCTP_INP_DECR_REF(inp);
417 SCTP_INP_WUNLOCK(inp);
420 SCTP_TCB_UNLOCK(stcb);
426 * this routine can probably be collasped into the one in sctp_userreq.c
427 * since they do the same thing and now we lookup with a sockaddr
430 sctp6_getcred(SYSCTL_HANDLER_ARGS)
433 struct sockaddr_in6 addrs[2];
434 struct sctp_inpcb *inp;
435 struct sctp_nets *net;
436 struct sctp_tcb *stcb;
440 vrf_id = SCTP_DEFAULT_VRFID;
443 * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
444 * visibility is scoped using cr_canseesocket(), which it is not
447 error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_RESERVEDPORT,
452 if (req->newlen != sizeof(addrs))
454 if (req->oldlen != sizeof(struct ucred))
456 error = SYSCTL_IN(req, addrs, sizeof(addrs));
460 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
462 &inp, &net, 1, vrf_id);
463 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
464 if ((inp != NULL) && (stcb == NULL)) {
465 /* reduce ref-count */
467 SCTP_INP_DECR_REF(inp);
473 SCTP_TCB_UNLOCK(stcb);
475 * We use the write lock here, only since in the error leg we need
476 * it. If we used RLOCK, then we would have to
477 * wlock/decr/unlock/rlock. Which in theory could create a hole.
478 * Better to use higher wlock.
482 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
484 SCTP_INP_WUNLOCK(inp);
487 cru2x(inp->sctp_socket->so_cred, &xuc);
488 SCTP_INP_WUNLOCK(inp);
489 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
494 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
496 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
499 /* This is the same as the sctp_abort() could be made common */
501 sctp6_abort(struct socket *so)
503 struct sctp_inpcb *inp;
506 inp = (struct sctp_inpcb *)so->so_pcb;
510 flags = inp->sctp_flags;
511 #ifdef SCTP_LOG_CLOSING
512 sctp_log_closing(inp, NULL, 17);
514 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
515 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
516 #ifdef SCTP_LOG_CLOSING
517 sctp_log_closing(inp, NULL, 16);
519 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
520 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
522 SCTP_SB_CLEAR(so->so_snd);
524 * same for the rcv ones, they are only here for the
527 SCTP_SB_CLEAR(so->so_rcv);
528 /* Now null out the reference, we are completely detached. */
532 flags = inp->sctp_flags;
533 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
534 goto sctp_must_try_again;
541 sctp6_attach(struct socket *so, int proto, struct thread *p)
545 struct sctp_inpcb *inp;
546 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
548 inp = (struct sctp_inpcb *)so->so_pcb;
552 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
553 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
557 error = sctp_inpcb_alloc(so, vrf_id);
560 inp = (struct sctp_inpcb *)so->so_pcb;
562 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
563 inp6 = (struct in6pcb *)inp;
565 inp6->inp_vflag |= INP_IPV6;
566 inp6->in6p_hops = -1; /* use kernel default */
567 inp6->in6p_cksum = -1; /* just to be sure */
570 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
571 * socket as well, because the socket may be bound to an IPv6
572 * wildcard address, which may match an IPv4-mapped IPv6 address.
574 inp6->inp_ip_ttl = ip_defttl;
577 * Hmm what about the IPSEC stuff that is missing here but in
580 SCTP_INP_WUNLOCK(inp);
585 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
587 struct sctp_inpcb *inp;
591 inp = (struct sctp_inpcb *)so->so_pcb;
596 if ((addr->sa_family == AF_INET6) &&
597 (addr->sa_len != sizeof(struct sockaddr_in6))) {
600 if ((addr->sa_family == AF_INET) &&
601 (addr->sa_len != sizeof(struct sockaddr_in))) {
605 inp6 = (struct in6pcb *)inp;
606 inp6->inp_vflag &= ~INP_IPV4;
607 inp6->inp_vflag |= INP_IPV6;
608 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
609 if (addr->sa_family == AF_INET) {
610 /* binding v4 addr to v6 socket, so reset flags */
611 inp6->inp_vflag |= INP_IPV4;
612 inp6->inp_vflag &= ~INP_IPV6;
614 struct sockaddr_in6 *sin6_p;
616 sin6_p = (struct sockaddr_in6 *)addr;
618 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
619 inp6->inp_vflag |= INP_IPV4;
620 } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
621 struct sockaddr_in sin;
623 in6_sin6_2_sin(&sin, sin6_p);
624 inp6->inp_vflag |= INP_IPV4;
625 inp6->inp_vflag &= ~INP_IPV6;
626 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
630 } else if (addr != NULL) {
631 /* IPV6_V6ONLY socket */
632 if (addr->sa_family == AF_INET) {
633 /* can't bind v4 addr to v6 only socket! */
636 struct sockaddr_in6 *sin6_p;
638 sin6_p = (struct sockaddr_in6 *)addr;
640 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
641 /* can't bind v4-mapped addrs either! */
642 /* NOTE: we don't support SIIT */
646 error = sctp_inpcb_bind(so, addr, p);
652 sctp6_close(struct socket *so)
654 struct sctp_inpcb *inp;
657 inp = (struct sctp_inpcb *)so->so_pcb;
662 * Inform all the lower layer assoc that we are done.
665 flags = inp->sctp_flags;
666 #ifdef SCTP_LOG_CLOSING
667 sctp_log_closing(inp, NULL, 17);
669 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
670 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
671 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
672 (so->so_rcv.sb_cc > 0)) {
673 #ifdef SCTP_LOG_CLOSING
674 sctp_log_closing(inp, NULL, 13);
676 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT
677 ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
679 #ifdef SCTP_LOG_CLOSING
680 sctp_log_closing(inp, NULL, 14);
682 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
683 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
686 * The socket is now detached, no matter what the state of
687 * the SCTP association.
690 SCTP_SB_CLEAR(so->so_snd);
692 * same for the rcv ones, they are only here for the
695 SCTP_SB_CLEAR(so->so_rcv);
696 /* Now null out the reference, we are completely detached. */
700 flags = inp->sctp_flags;
701 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
702 goto sctp_must_try_again;
709 /* This could be made common with sctp_detach() since they are identical */
713 sctp6_disconnect(struct socket *so)
715 struct sctp_inpcb *inp;
717 inp = (struct sctp_inpcb *)so->so_pcb;
722 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
723 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
725 SCTP_INP_RUNLOCK(inp);
728 int some_on_streamwheel = 0;
729 struct sctp_association *asoc;
730 struct sctp_tcb *stcb;
732 stcb = LIST_FIRST(&inp->sctp_asoc_list);
734 SCTP_INP_RUNLOCK(inp);
739 if (((so->so_options & SO_LINGER) &&
740 (so->so_linger == 0)) ||
741 (so->so_rcv.sb_cc > 0)) {
742 if (SCTP_GET_STATE(asoc) !=
743 SCTP_STATE_COOKIE_WAIT) {
744 /* Left with Data unread */
747 op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT);
748 sctp_send_abort_tcb(stcb, op_err);
749 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
751 SCTP_INP_RUNLOCK(inp);
752 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
753 (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
754 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
756 sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
757 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
758 /* No unlock tcb assoc is gone */
761 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
762 /* Check to see if some data queued */
763 struct sctp_stream_out *outs;
765 TAILQ_FOREACH(outs, &asoc->out_wheel,
767 if (!TAILQ_EMPTY(&outs->outqueue)) {
768 some_on_streamwheel = 1;
773 if (TAILQ_EMPTY(&asoc->send_queue) &&
774 TAILQ_EMPTY(&asoc->sent_queue) &&
775 (some_on_streamwheel == 0)) {
776 /* nothing queued to send, so I'm done... */
777 if ((SCTP_GET_STATE(asoc) !=
778 SCTP_STATE_SHUTDOWN_SENT) &&
779 (SCTP_GET_STATE(asoc) !=
780 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
781 /* only send SHUTDOWN the first time */
782 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
783 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
784 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
785 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
786 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
788 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
789 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
791 asoc->primary_destination);
792 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
794 asoc->primary_destination);
798 * we still got (or just got) data to send,
799 * so set SHUTDOWN_PENDING
802 * XXX sockets draft says that MSG_EOF
803 * should be sent with no data. currently,
804 * we will allow user data to be sent first
805 * and move to SHUTDOWN-PENDING
807 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
809 SCTP_TCB_UNLOCK(stcb);
810 SCTP_INP_RUNLOCK(inp);
814 /* UDP model does not support this */
815 SCTP_INP_RUNLOCK(inp);
822 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
823 struct mbuf *control, struct thread *p);
827 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
828 struct mbuf *control, struct thread *p)
830 struct sctp_inpcb *inp;
831 struct inpcb *in_inp;
835 struct sockaddr_in6 *sin6;
838 /* No SPL needed since sctp_output does this */
840 inp = (struct sctp_inpcb *)so->so_pcb;
843 SCTP_RELEASE_PKT(control);
849 in_inp = (struct inpcb *)inp;
850 inp6 = (struct in6pcb *)inp;
852 * For the TCP model we may get a NULL addr, if we are a connected
855 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
862 SCTP_RELEASE_PKT(control);
865 return (EDESTADDRREQ);
868 sin6 = (struct sockaddr_in6 *)addr;
869 if (SCTP_IPV6_V6ONLY(inp6)) {
871 * if IPV6_V6ONLY flag, we discard datagrams destined to a
872 * v4 addr or v4-mapped addr
874 if (addr->sa_family == AF_INET) {
877 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
881 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
883 struct sockaddr_in sin;
885 /* convert v4-mapped into v4 addr and send */
886 in6_sin6_2_sin(&sin, sin6);
887 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
890 /* mapped addresses aren't enabled */
896 /* now what about control */
899 SCTP_PRINTF("huh? control set?\n");
900 SCTP_RELEASE_PKT(inp->control);
903 inp->control = control;
907 SCTP_BUF_NEXT(inp->pkt_last) = m;
910 inp->pkt_last = inp->pkt = m;
913 /* FreeBSD and MacOSX uses a flag passed */
914 ((flags & PRUS_MORETOCOME) == 0)
917 * note with the current version this code will only be used
918 * by OpenBSD, NetBSD and FreeBSD have methods for
919 * re-defining sosend() to use sctp_sosend(). One can
920 * optionaly switch back to this code (by changing back the
921 * defininitions but this is not advisable.
925 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
935 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
939 struct sctp_inpcb *inp;
941 struct sctp_tcb *stcb;
944 struct sockaddr_in6 *sin6;
945 struct sockaddr_storage ss;
949 inp6 = (struct in6pcb *)so->so_pcb;
950 inp = (struct sctp_inpcb *)so->so_pcb;
952 return (ECONNRESET); /* I made the same as TCP since we are
958 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
961 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
964 vrf_id = inp->def_vrf_id;
965 SCTP_ASOC_CREATE_LOCK(inp);
967 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
968 SCTP_PCB_FLAGS_UNBOUND) {
969 /* Bind a ephemeral port */
970 SCTP_INP_RUNLOCK(inp);
971 error = sctp6_bind(so, NULL, p);
973 SCTP_ASOC_CREATE_UNLOCK(inp);
979 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
980 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
981 /* We are already connected AND the TCP model */
982 SCTP_INP_RUNLOCK(inp);
983 SCTP_ASOC_CREATE_UNLOCK(inp);
987 sin6 = (struct sockaddr_in6 *)addr;
988 if (SCTP_IPV6_V6ONLY(inp6)) {
990 * if IPV6_V6ONLY flag, ignore connections destined to a v4
991 * addr or v4-mapped addr
993 if (addr->sa_family == AF_INET) {
994 SCTP_INP_RUNLOCK(inp);
995 SCTP_ASOC_CREATE_UNLOCK(inp);
998 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
999 SCTP_INP_RUNLOCK(inp);
1000 SCTP_ASOC_CREATE_UNLOCK(inp);
1004 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1006 /* convert v4-mapped into v4 addr */
1007 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1008 addr = (struct sockaddr *)&ss;
1010 /* mapped addresses aren't enabled */
1011 SCTP_INP_RUNLOCK(inp);
1012 SCTP_ASOC_CREATE_UNLOCK(inp);
1017 addr = addr; /* for true v6 address case */
1019 /* Now do we connect? */
1020 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1021 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1023 SCTP_TCB_UNLOCK(stcb);
1025 SCTP_INP_RUNLOCK(inp);
1027 SCTP_INP_RUNLOCK(inp);
1028 SCTP_INP_WLOCK(inp);
1029 SCTP_INP_INCR_REF(inp);
1030 SCTP_INP_WUNLOCK(inp);
1031 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1033 SCTP_INP_WLOCK(inp);
1034 SCTP_INP_DECR_REF(inp);
1035 SCTP_INP_WUNLOCK(inp);
1040 /* Already have or am bring up an association */
1041 SCTP_ASOC_CREATE_UNLOCK(inp);
1042 SCTP_TCB_UNLOCK(stcb);
1045 /* We are GOOD to go */
1046 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
1047 SCTP_ASOC_CREATE_UNLOCK(inp);
1049 /* Gak! no memory */
1052 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1053 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1054 /* Set the connected flag so we can queue data */
1057 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1058 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1060 /* initialize authentication parameters for the assoc */
1061 sctp_initialize_auth_params(inp, stcb);
1063 sctp_send_initiate(inp, stcb);
1064 SCTP_TCB_UNLOCK(stcb);
1069 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1071 struct sockaddr_in6 *sin6;
1072 struct sctp_inpcb *inp;
1074 struct sctp_ifa *sctp_ifa;
1079 * Do the malloc first in case it blocks.
1081 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1082 sin6->sin6_family = AF_INET6;
1083 sin6->sin6_len = sizeof(*sin6);
1085 inp = (struct sctp_inpcb *)so->so_pcb;
1087 SCTP_FREE_SONAME(sin6);
1090 SCTP_INP_RLOCK(inp);
1091 sin6->sin6_port = inp->sctp_lport;
1092 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1093 /* For the bound all case you get back 0 */
1094 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1095 struct sctp_tcb *stcb;
1096 struct sockaddr_in6 *sin_a6;
1097 struct sctp_nets *net;
1100 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1106 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1107 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1109 /* this will make coverity happy */
1112 if (sin_a6->sin6_family == AF_INET6) {
1117 if ((!fnd) || (sin_a6 == NULL)) {
1121 vrf_id = inp->def_vrf_id;
1122 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1124 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1127 /* For the bound all case you get back 0 */
1129 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1132 /* Take the first IPv6 address in the list */
1133 struct sctp_laddr *laddr;
1136 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1137 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1138 struct sockaddr_in6 *sin_a;
1140 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1141 sin6->sin6_addr = sin_a->sin6_addr;
1147 SCTP_FREE_SONAME(sin6);
1148 SCTP_INP_RUNLOCK(inp);
1152 SCTP_INP_RUNLOCK(inp);
1153 /* Scoping things for v6 */
1154 if ((error = sa6_recoverscope(sin6)) != 0) {
1155 SCTP_FREE_SONAME(sin6);
1158 (*addr) = (struct sockaddr *)sin6;
1163 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1165 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1167 struct sockaddr_in6 *sin_a6;
1168 struct sctp_inpcb *inp;
1169 struct sctp_tcb *stcb;
1170 struct sctp_nets *net;
1175 * Do the malloc first in case it blocks.
1177 inp = (struct sctp_inpcb *)so->so_pcb;
1178 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1179 /* UDP type and listeners will drop out here */
1182 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1183 sin6->sin6_family = AF_INET6;
1184 sin6->sin6_len = sizeof(*sin6);
1186 /* We must recapture incase we blocked */
1187 inp = (struct sctp_inpcb *)so->so_pcb;
1189 SCTP_FREE_SONAME(sin6);
1192 SCTP_INP_RLOCK(inp);
1193 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1195 SCTP_TCB_LOCK(stcb);
1197 SCTP_INP_RUNLOCK(inp);
1199 SCTP_FREE_SONAME(sin6);
1203 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1204 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1205 if (sin_a6->sin6_family == AF_INET6) {
1207 sin6->sin6_port = stcb->rport;
1208 sin6->sin6_addr = sin_a6->sin6_addr;
1212 SCTP_TCB_UNLOCK(stcb);
1214 /* No IPv4 address */
1215 SCTP_FREE_SONAME(sin6);
1218 if ((error = sa6_recoverscope(sin6)) != 0)
1220 *addr = (struct sockaddr *)sin6;
1225 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1227 struct sockaddr *addr;
1228 struct in6pcb *inp6 = sotoin6pcb(so);
1234 /* allow v6 addresses precedence */
1235 error = sctp6_getaddr(so, nam);
1237 /* try v4 next if v6 failed */
1238 error = sctp_ingetaddr(so, nam);
1243 /* if I'm V6ONLY, convert it to v4-mapped */
1244 if (SCTP_IPV6_V6ONLY(inp6)) {
1245 struct sockaddr_in6 sin6;
1247 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1248 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1257 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1259 struct sockaddr *addr = *nam;
1260 struct in6pcb *inp6 = sotoin6pcb(so);
1266 /* allow v6 addresses precedence */
1267 error = sctp6_peeraddr(so, nam);
1269 /* try v4 next if v6 failed */
1270 error = sctp_peeraddr(so, nam);
1274 /* if I'm V6ONLY, convert it to v4-mapped */
1275 if (SCTP_IPV6_V6ONLY(inp6)) {
1276 struct sockaddr_in6 sin6;
1278 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1279 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1285 struct pr_usrreqs sctp6_usrreqs = {
1286 .pru_abort = sctp6_abort,
1287 .pru_accept = sctp_accept,
1288 .pru_attach = sctp6_attach,
1289 .pru_bind = sctp6_bind,
1290 .pru_connect = sctp6_connect,
1291 .pru_control = in6_control,
1292 .pru_close = sctp6_close,
1293 .pru_detach = sctp6_close,
1294 .pru_sopoll = sopoll_generic,
1295 .pru_disconnect = sctp6_disconnect,
1296 .pru_listen = sctp_listen,
1297 .pru_peeraddr = sctp6_getpeeraddr,
1298 .pru_send = sctp6_send,
1299 .pru_shutdown = sctp_shutdown,
1300 .pru_sockaddr = sctp6_in6getaddr,
1301 .pru_sosend = sctp_sosend,
1302 .pru_soreceive = sctp_soreceive