2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_var.h>
43 #include <netinet6/sctp6_var.h>
45 #include <netinet/sctp_sysctl.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctputil.h>
50 #include <netinet/sctp_indata.h>
51 #include <netinet/sctp_timer.h>
52 #include <netinet/sctp_auth.h>
53 #include <netinet/sctp_input.h>
54 #include <netinet/sctp_output.h>
55 #include <netinet/sctp_bsd_addr.h>
56 #include <netinet/sctp_crc32.h>
57 #include <netinet/udp.h>
60 #include <netipsec/ipsec.h>
62 #include <netipsec/ipsec6.h>
66 extern struct protosw inetsw[];
69 sctp6_input(struct mbuf **i_pak, int *offp, int proto)
75 struct sockaddr_in6 src, dst;
78 struct sctp_chunkhdr *ch;
81 #if !defined(SCTP_WITH_NO_CSUM)
90 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
91 SCTP_RELEASE_PKT(*i_pak);
92 return (IPPROTO_DONE);
94 m = SCTP_HEADER_TO_CHAIN(*i_pak);
95 #ifdef SCTP_MBUF_LOGGING
96 /* Log in any input mbufs */
97 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
100 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
101 if (SCTP_BUF_IS_EXTENDED(mat)) {
102 sctp_log_mb(mat, SCTP_MBUF_INPUT);
107 #ifdef SCTP_PACKET_LOGGING
108 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
112 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
113 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
115 if_name(m->m_pkthdr.rcvif),
116 m->m_pkthdr.csum_flags);
117 if (m->m_flags & M_FLOWID) {
118 mflowid = m->m_pkthdr.flowid;
124 SCTP_STAT_INCR(sctps_recvpackets);
125 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
126 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
127 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
128 ip6 = mtod(m, struct ip6_hdr *);
129 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
130 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
132 SCTP_STAT_INCR(sctps_hdrops);
133 return (IPPROTO_DONE);
135 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
136 offset -= sizeof(struct sctp_chunkhdr);
137 memset(&src, 0, sizeof(struct sockaddr_in6));
138 src.sin6_family = AF_INET6;
139 src.sin6_len = sizeof(struct sockaddr_in6);
140 src.sin6_port = sh->src_port;
141 src.sin6_addr = ip6->ip6_src;
142 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
145 memset(&dst, 0, sizeof(struct sockaddr_in6));
146 dst.sin6_family = AF_INET6;
147 dst.sin6_len = sizeof(struct sockaddr_in6);
148 dst.sin6_port = sh->dest_port;
149 dst.sin6_addr = ip6->ip6_dst;
150 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
153 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
154 /* XXX send icmp6 host/port unreach? */
157 length = ntohs(ip6->ip6_plen) + iphlen;
158 /* Validate mbuf chain length with IP payload length. */
159 if (SCTP_HEADER_LEN(m) != length) {
160 SCTPDBG(SCTP_DEBUG_INPUT1,
161 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
162 SCTP_STAT_INCR(sctps_hdrops);
165 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
168 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
169 #if defined(SCTP_WITH_NO_CSUM)
170 SCTP_STAT_INCR(sctps_recvnocrc);
172 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
173 SCTP_STAT_INCR(sctps_recvhwcrc);
176 SCTP_STAT_INCR(sctps_recvswcrc);
180 sctp_common_input_processing(&m, iphlen, offset, length,
181 (struct sockaddr *)&src,
182 (struct sockaddr *)&dst,
184 #if !defined(SCTP_WITH_NO_CSUM)
188 use_mflowid, mflowid,
194 return (IPPROTO_DONE);
199 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
200 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
204 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
205 (icmp6 == NULL) || (sh == NULL)) {
208 /* First do we even look at it? */
209 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
212 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
213 /* not PACKET TO BIG */
217 * ok we need to look closely. We could even get smarter and look at
218 * anyone that we sent to in case we get a different ICMP that tells
219 * us there is no way to reach a host, but for this impl, all we
220 * care about is MTU discovery.
222 nxtsz = ntohl(icmp6->icmp6_mtu);
223 /* Stop any PMTU timer */
224 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
226 /* Adjust destination size limit */
227 if (net->mtu > nxtsz) {
230 net->mtu -= sizeof(struct udphdr);
233 /* now what about the ep? */
234 if (stcb->asoc.smallest_mtu > nxtsz) {
235 struct sctp_tmit_chunk *chk;
237 /* Adjust that too */
238 stcb->asoc.smallest_mtu = nxtsz;
239 /* now off to subtract IP_DF flag if needed */
241 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
242 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
243 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
246 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
247 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
249 * For this guy we also mark for immediate
250 * resend since we sent to big of chunk
252 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
253 if (chk->sent != SCTP_DATAGRAM_RESEND)
254 stcb->asoc.sent_queue_retran_cnt++;
255 chk->sent = SCTP_DATAGRAM_RESEND;
256 chk->rec.data.doing_fast_retransmit = 0;
258 chk->sent = SCTP_DATAGRAM_RESEND;
259 /* Clear any time so NO RTT is being done */
260 chk->sent_rcv_time.tv_sec = 0;
261 chk->sent_rcv_time.tv_usec = 0;
262 stcb->asoc.total_flight -= chk->send_size;
263 net->flight_size -= chk->send_size;
267 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
270 SCTP_TCB_UNLOCK(stcb);
276 sctp6_notify(struct sctp_inpcb *inp,
277 struct icmp6_hdr *icmph,
280 struct sctp_tcb *stcb,
281 struct sctp_nets *net)
283 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
289 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
290 (sh == NULL) || (to == NULL)) {
292 SCTP_TCB_UNLOCK(stcb);
295 /* First job is to verify the vtag matches what I would send */
296 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
297 SCTP_TCB_UNLOCK(stcb);
300 if (icmph->icmp6_type != ICMP_UNREACH) {
301 /* We only care about unreachable */
302 SCTP_TCB_UNLOCK(stcb);
305 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
306 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
307 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
308 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
309 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
310 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
311 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
312 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
315 * Hmm reachablity problems we must examine closely. If its
316 * not reachable, we may have lost a network. Or if there is
317 * NO protocol at the other end named SCTP. well we consider
320 if (net->dest_state & SCTP_ADDR_REACHABLE) {
321 /* Ok that destination is NOT reachable */
322 net->dest_state &= ~SCTP_ADDR_REACHABLE;
323 net->dest_state &= ~SCTP_ADDR_PF;
324 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
325 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
327 SCTP_TCB_UNLOCK(stcb);
328 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
329 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
331 * Here the peer is either playing tricks on us, including
332 * an address that belongs to someone who does not support
333 * SCTP OR was a userland implementation that shutdown and
334 * now is dead. In either case treat it like a OOTB abort
337 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
338 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
339 so = SCTP_INP_SO(inp);
340 atomic_add_int(&stcb->asoc.refcnt, 1);
341 SCTP_TCB_UNLOCK(stcb);
342 SCTP_SOCKET_LOCK(so, 1);
344 atomic_subtract_int(&stcb->asoc.refcnt, 1);
346 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
347 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
348 SCTP_SOCKET_UNLOCK(so, 1);
349 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
351 /* no need to unlock here, since the TCB is gone */
353 SCTP_TCB_UNLOCK(stcb);
360 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
363 struct ip6ctlparam *ip6cp = NULL;
366 vrf_id = SCTP_DEFAULT_VRFID;
368 if (pktdst->sa_family != AF_INET6 ||
369 pktdst->sa_len != sizeof(struct sockaddr_in6))
372 if ((unsigned)cmd >= PRC_NCMDS)
374 if (PRC_IS_REDIRECT(cmd)) {
376 } else if (inet6ctlerrmap[cmd] == 0) {
379 /* if the parameter is from icmp6, decode it. */
381 ip6cp = (struct ip6ctlparam *)d;
383 ip6cp = (struct ip6ctlparam *)NULL;
388 * XXX: We assume that when IPV6 is non NULL, M and OFF are
391 /* check if we can safely examine src and dst ports */
392 struct sctp_inpcb *inp = NULL;
393 struct sctp_tcb *stcb = NULL;
394 struct sctp_nets *net = NULL;
395 struct sockaddr_in6 final;
397 if (ip6cp->ip6c_m == NULL)
400 bzero(&sh, sizeof(sh));
401 bzero(&final, sizeof(final));
404 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
406 ip6cp->ip6c_src->sin6_port = sh.src_port;
407 final.sin6_len = sizeof(final);
408 final.sin6_family = AF_INET6;
409 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
410 final.sin6_port = sh.dest_port;
411 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
412 (struct sockaddr *)ip6cp->ip6c_src,
413 &inp, &net, 1, vrf_id);
414 /* inp's ref-count increased && stcb locked */
415 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
416 if (cmd == PRC_MSGSIZE) {
417 sctp6_notify_mbuf(inp,
422 /* inp's ref-count reduced && stcb unlocked */
424 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
425 (struct sockaddr *)&final,
427 /* inp's ref-count reduced && stcb unlocked */
430 if (PRC_IS_REDIRECT(cmd) && inp) {
431 in6_rtchange((struct in6pcb *)inp,
432 inet6ctlerrmap[cmd]);
435 /* reduce inp's ref-count */
437 SCTP_INP_DECR_REF(inp);
438 SCTP_INP_WUNLOCK(inp);
441 SCTP_TCB_UNLOCK(stcb);
447 * this routine can probably be collasped into the one in sctp_userreq.c
448 * since they do the same thing and now we lookup with a sockaddr
451 sctp6_getcred(SYSCTL_HANDLER_ARGS)
454 struct sockaddr_in6 addrs[2];
455 struct sctp_inpcb *inp;
456 struct sctp_nets *net;
457 struct sctp_tcb *stcb;
461 vrf_id = SCTP_DEFAULT_VRFID;
463 error = priv_check(req->td, PRIV_NETINET_GETCRED);
467 if (req->newlen != sizeof(addrs)) {
468 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
471 if (req->oldlen != sizeof(struct ucred)) {
472 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
475 error = SYSCTL_IN(req, addrs, sizeof(addrs));
479 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
481 &inp, &net, 1, vrf_id);
482 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
483 if ((inp != NULL) && (stcb == NULL)) {
484 /* reduce ref-count */
486 SCTP_INP_DECR_REF(inp);
489 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
493 SCTP_TCB_UNLOCK(stcb);
495 * We use the write lock here, only since in the error leg we need
496 * it. If we used RLOCK, then we would have to
497 * wlock/decr/unlock/rlock. Which in theory could create a hole.
498 * Better to use higher wlock.
502 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
504 SCTP_INP_WUNLOCK(inp);
507 cru2x(inp->sctp_socket->so_cred, &xuc);
508 SCTP_INP_WUNLOCK(inp);
509 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
514 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
516 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
519 /* This is the same as the sctp_abort() could be made common */
521 sctp6_abort(struct socket *so)
523 struct sctp_inpcb *inp;
526 inp = (struct sctp_inpcb *)so->so_pcb;
528 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
532 flags = inp->sctp_flags;
533 #ifdef SCTP_LOG_CLOSING
534 sctp_log_closing(inp, NULL, 17);
536 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
537 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
538 #ifdef SCTP_LOG_CLOSING
539 sctp_log_closing(inp, NULL, 16);
541 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
542 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
544 SCTP_SB_CLEAR(so->so_snd);
546 * same for the rcv ones, they are only here for the
549 SCTP_SB_CLEAR(so->so_rcv);
550 /* Now null out the reference, we are completely detached. */
554 flags = inp->sctp_flags;
555 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
556 goto sctp_must_try_again;
563 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
567 struct sctp_inpcb *inp;
568 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
570 inp = (struct sctp_inpcb *)so->so_pcb;
572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
575 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
576 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
580 error = sctp_inpcb_alloc(so, vrf_id);
583 inp = (struct sctp_inpcb *)so->so_pcb;
585 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
586 inp6 = (struct in6pcb *)inp;
588 inp6->inp_vflag |= INP_IPV6;
589 inp6->in6p_hops = -1; /* use kernel default */
590 inp6->in6p_cksum = -1; /* just to be sure */
593 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
594 * socket as well, because the socket may be bound to an IPv6
595 * wildcard address, which may match an IPv4-mapped IPv6 address.
597 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
600 * Hmm what about the IPSEC stuff that is missing here but in
603 SCTP_INP_WUNLOCK(inp);
608 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
610 struct sctp_inpcb *inp;
614 inp = (struct sctp_inpcb *)so->so_pcb;
616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
620 switch (addr->sa_family) {
623 if (addr->sa_len != sizeof(struct sockaddr_in)) {
624 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
631 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
632 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
638 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
642 inp6 = (struct in6pcb *)inp;
643 inp6->inp_vflag &= ~INP_IPV4;
644 inp6->inp_vflag |= INP_IPV6;
645 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
646 switch (addr->sa_family) {
649 /* binding v4 addr to v6 socket, so reset flags */
650 inp6->inp_vflag |= INP_IPV4;
651 inp6->inp_vflag &= ~INP_IPV6;
657 struct sockaddr_in6 *sin6_p;
659 sin6_p = (struct sockaddr_in6 *)addr;
661 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
662 inp6->inp_vflag |= INP_IPV4;
665 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
666 struct sockaddr_in sin;
668 in6_sin6_2_sin(&sin, sin6_p);
669 inp6->inp_vflag |= INP_IPV4;
670 inp6->inp_vflag &= ~INP_IPV6;
671 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
681 } else if (addr != NULL) {
682 struct sockaddr_in6 *sin6_p;
684 /* IPV6_V6ONLY socket */
686 if (addr->sa_family == AF_INET) {
687 /* can't bind v4 addr to v6 only socket! */
688 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
692 sin6_p = (struct sockaddr_in6 *)addr;
694 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
695 /* can't bind v4-mapped addrs either! */
696 /* NOTE: we don't support SIIT */
697 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
701 error = sctp_inpcb_bind(so, addr, NULL, p);
707 sctp6_close(struct socket *so)
712 /* This could be made common with sctp_detach() since they are identical */
716 sctp6_disconnect(struct socket *so)
718 return (sctp_disconnect(so));
723 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
724 struct mbuf *control, struct thread *p);
728 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
729 struct mbuf *control, struct thread *p)
731 struct sctp_inpcb *inp;
735 struct sockaddr_in6 *sin6;
738 /* No SPL needed since sctp_output does this */
740 inp = (struct sctp_inpcb *)so->so_pcb;
743 SCTP_RELEASE_PKT(control);
747 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
750 inp6 = (struct in6pcb *)inp;
752 * For the TCP model we may get a NULL addr, if we are a connected
755 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
762 SCTP_RELEASE_PKT(control);
765 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
766 return (EDESTADDRREQ);
769 sin6 = (struct sockaddr_in6 *)addr;
770 if (SCTP_IPV6_V6ONLY(inp6)) {
772 * if IPV6_V6ONLY flag, we discard datagrams destined to a
773 * v4 addr or v4-mapped addr
775 if (addr->sa_family == AF_INET) {
776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
779 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
780 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
784 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
785 if (!MODULE_GLOBAL(ip6_v6only)) {
786 struct sockaddr_in sin;
788 /* convert v4-mapped into v4 addr and send */
789 in6_sin6_2_sin(&sin, sin6);
790 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
793 /* mapped addresses aren't enabled */
794 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
800 /* now what about control */
803 SCTP_PRINTF("huh? control set?\n");
804 SCTP_RELEASE_PKT(inp->control);
807 inp->control = control;
811 SCTP_BUF_NEXT(inp->pkt_last) = m;
814 inp->pkt_last = inp->pkt = m;
817 /* FreeBSD and MacOSX uses a flag passed */
818 ((flags & PRUS_MORETOCOME) == 0)
821 * note with the current version this code will only be used
822 * by OpenBSD, NetBSD and FreeBSD have methods for
823 * re-defining sosend() to use sctp_sosend(). One can
824 * optionaly switch back to this code (by changing back the
825 * defininitions but this is not advisable.
829 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
839 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
843 struct sctp_inpcb *inp;
845 struct sctp_tcb *stcb;
848 struct sockaddr_in6 *sin6;
849 struct sockaddr_storage ss;
853 inp6 = (struct in6pcb *)so->so_pcb;
854 inp = (struct sctp_inpcb *)so->so_pcb;
856 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
857 return (ECONNRESET); /* I made the same as TCP since we are
861 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
864 switch (addr->sa_family) {
867 if (addr->sa_len != sizeof(struct sockaddr_in)) {
868 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
875 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
876 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
882 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
886 vrf_id = inp->def_vrf_id;
887 SCTP_ASOC_CREATE_LOCK(inp);
889 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
890 SCTP_PCB_FLAGS_UNBOUND) {
891 /* Bind a ephemeral port */
892 SCTP_INP_RUNLOCK(inp);
893 error = sctp6_bind(so, NULL, p);
895 SCTP_ASOC_CREATE_UNLOCK(inp);
901 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
902 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
903 /* We are already connected AND the TCP model */
904 SCTP_INP_RUNLOCK(inp);
905 SCTP_ASOC_CREATE_UNLOCK(inp);
906 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
910 sin6 = (struct sockaddr_in6 *)addr;
911 if (SCTP_IPV6_V6ONLY(inp6)) {
913 * if IPV6_V6ONLY flag, ignore connections destined to a v4
914 * addr or v4-mapped addr
916 if (addr->sa_family == AF_INET) {
917 SCTP_INP_RUNLOCK(inp);
918 SCTP_ASOC_CREATE_UNLOCK(inp);
919 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
922 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
923 SCTP_INP_RUNLOCK(inp);
924 SCTP_ASOC_CREATE_UNLOCK(inp);
925 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
929 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
930 if (!MODULE_GLOBAL(ip6_v6only)) {
931 /* convert v4-mapped into v4 addr */
932 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
933 addr = (struct sockaddr *)&ss;
935 /* mapped addresses aren't enabled */
936 SCTP_INP_RUNLOCK(inp);
937 SCTP_ASOC_CREATE_UNLOCK(inp);
938 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
943 /* Now do we connect? */
944 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
945 stcb = LIST_FIRST(&inp->sctp_asoc_list);
947 SCTP_TCB_UNLOCK(stcb);
949 SCTP_INP_RUNLOCK(inp);
951 SCTP_INP_RUNLOCK(inp);
953 SCTP_INP_INCR_REF(inp);
954 SCTP_INP_WUNLOCK(inp);
955 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
958 SCTP_INP_DECR_REF(inp);
959 SCTP_INP_WUNLOCK(inp);
964 /* Already have or am bring up an association */
965 SCTP_ASOC_CREATE_UNLOCK(inp);
966 SCTP_TCB_UNLOCK(stcb);
967 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
970 /* We are GOOD to go */
971 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
972 SCTP_ASOC_CREATE_UNLOCK(inp);
977 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
978 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
979 /* Set the connected flag so we can queue data */
982 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
983 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
985 /* initialize authentication parameters for the assoc */
986 sctp_initialize_auth_params(inp, stcb);
988 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
989 SCTP_TCB_UNLOCK(stcb);
994 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
996 struct sockaddr_in6 *sin6;
997 struct sctp_inpcb *inp;
999 struct sctp_ifa *sctp_ifa;
1004 * Do the malloc first in case it blocks.
1006 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1009 sin6->sin6_family = AF_INET6;
1010 sin6->sin6_len = sizeof(*sin6);
1012 inp = (struct sctp_inpcb *)so->so_pcb;
1014 SCTP_FREE_SONAME(sin6);
1015 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1016 return (ECONNRESET);
1018 SCTP_INP_RLOCK(inp);
1019 sin6->sin6_port = inp->sctp_lport;
1020 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1021 /* For the bound all case you get back 0 */
1022 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1023 struct sctp_tcb *stcb;
1024 struct sockaddr_in6 *sin_a6;
1025 struct sctp_nets *net;
1028 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1034 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1035 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1037 /* this will make coverity happy */
1040 if (sin_a6->sin6_family == AF_INET6) {
1045 if ((!fnd) || (sin_a6 == NULL)) {
1049 vrf_id = inp->def_vrf_id;
1050 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1052 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1055 /* For the bound all case you get back 0 */
1057 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1060 /* Take the first IPv6 address in the list */
1061 struct sctp_laddr *laddr;
1064 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1065 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1066 struct sockaddr_in6 *sin_a;
1068 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1069 sin6->sin6_addr = sin_a->sin6_addr;
1075 SCTP_FREE_SONAME(sin6);
1076 SCTP_INP_RUNLOCK(inp);
1077 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1081 SCTP_INP_RUNLOCK(inp);
1082 /* Scoping things for v6 */
1083 if ((error = sa6_recoverscope(sin6)) != 0) {
1084 SCTP_FREE_SONAME(sin6);
1087 (*addr) = (struct sockaddr *)sin6;
1092 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1094 struct sockaddr_in6 *sin6;
1096 struct sockaddr_in6 *sin_a6;
1097 struct sctp_inpcb *inp;
1098 struct sctp_tcb *stcb;
1099 struct sctp_nets *net;
1102 /* Do the malloc first in case it blocks. */
1103 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1106 sin6->sin6_family = AF_INET6;
1107 sin6->sin6_len = sizeof(*sin6);
1109 inp = (struct sctp_inpcb *)so->so_pcb;
1110 if ((inp == NULL) ||
1111 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1112 /* UDP type and listeners will drop out here */
1113 SCTP_FREE_SONAME(sin6);
1114 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1117 SCTP_INP_RLOCK(inp);
1118 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1120 SCTP_TCB_LOCK(stcb);
1122 SCTP_INP_RUNLOCK(inp);
1124 SCTP_FREE_SONAME(sin6);
1125 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1126 return (ECONNRESET);
1129 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1130 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1131 if (sin_a6->sin6_family == AF_INET6) {
1133 sin6->sin6_port = stcb->rport;
1134 sin6->sin6_addr = sin_a6->sin6_addr;
1138 SCTP_TCB_UNLOCK(stcb);
1140 /* No IPv4 address */
1141 SCTP_FREE_SONAME(sin6);
1142 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1145 if ((error = sa6_recoverscope(sin6)) != 0)
1147 *addr = (struct sockaddr *)sin6;
1152 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1155 struct sockaddr *addr;
1158 struct in6pcb *inp6 = sotoin6pcb(so);
1162 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1165 /* allow v6 addresses precedence */
1166 error = sctp6_getaddr(so, nam);
1169 /* try v4 next if v6 failed */
1170 error = sctp_ingetaddr(so, nam);
1175 /* if I'm V6ONLY, convert it to v4-mapped */
1176 if (SCTP_IPV6_V6ONLY(inp6)) {
1177 struct sockaddr_in6 sin6;
1179 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1180 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1189 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1192 struct sockaddr *addr;
1195 struct in6pcb *inp6 = sotoin6pcb(so);
1199 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1202 /* allow v6 addresses precedence */
1203 error = sctp6_peeraddr(so, nam);
1206 /* try v4 next if v6 failed */
1207 error = sctp_peeraddr(so, nam);
1212 /* if I'm V6ONLY, convert it to v4-mapped */
1213 if (SCTP_IPV6_V6ONLY(inp6)) {
1214 struct sockaddr_in6 sin6;
1216 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1217 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1224 struct pr_usrreqs sctp6_usrreqs = {
1225 .pru_abort = sctp6_abort,
1226 .pru_accept = sctp_accept,
1227 .pru_attach = sctp6_attach,
1228 .pru_bind = sctp6_bind,
1229 .pru_connect = sctp6_connect,
1230 .pru_control = in6_control,
1231 .pru_close = sctp6_close,
1232 .pru_detach = sctp6_close,
1233 .pru_sopoll = sopoll_generic,
1234 .pru_flush = sctp_flush,
1235 .pru_disconnect = sctp6_disconnect,
1236 .pru_listen = sctp_listen,
1237 .pru_peeraddr = sctp6_getpeeraddr,
1238 .pru_send = sctp6_send,
1239 .pru_shutdown = sctp_shutdown,
1240 .pru_sockaddr = sctp6_in6getaddr,
1241 .pru_sosend = sctp_sosend,
1242 .pru_soreceive = sctp_soreceive