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_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
75 struct sockaddr_in6 src, dst;
78 struct sctp_chunkhdr *ch;
81 #if !defined(SCTP_WITH_NO_CSUM)
89 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
90 SCTP_RELEASE_PKT(*i_pak);
91 return (IPPROTO_DONE);
93 m = SCTP_HEADER_TO_CHAIN(*i_pak);
94 #ifdef SCTP_MBUF_LOGGING
95 /* Log in any input mbufs */
96 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
97 sctp_log_mbc(m, SCTP_MBUF_INPUT);
100 #ifdef SCTP_PACKET_LOGGING
101 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
105 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
106 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
108 if_name(m->m_pkthdr.rcvif),
109 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
110 mflowid = m->m_pkthdr.flowid;
111 mflowtype = M_HASHTYPE_GET(m);
112 SCTP_STAT_INCR(sctps_recvpackets);
113 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
114 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
115 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
116 ip6 = mtod(m, struct ip6_hdr *);
117 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
118 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
120 SCTP_STAT_INCR(sctps_hdrops);
121 return (IPPROTO_DONE);
123 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
124 offset -= sizeof(struct sctp_chunkhdr);
125 memset(&src, 0, sizeof(struct sockaddr_in6));
126 src.sin6_family = AF_INET6;
127 src.sin6_len = sizeof(struct sockaddr_in6);
128 src.sin6_port = sh->src_port;
129 src.sin6_addr = ip6->ip6_src;
130 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
133 memset(&dst, 0, sizeof(struct sockaddr_in6));
134 dst.sin6_family = AF_INET6;
135 dst.sin6_len = sizeof(struct sockaddr_in6);
136 dst.sin6_port = sh->dest_port;
137 dst.sin6_addr = ip6->ip6_dst;
138 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
141 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
142 /* XXX send icmp6 host/port unreach? */
145 length = ntohs(ip6->ip6_plen) + iphlen;
146 /* Validate mbuf chain length with IP payload length. */
147 if (SCTP_HEADER_LEN(m) != length) {
148 SCTPDBG(SCTP_DEBUG_INPUT1,
149 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
150 SCTP_STAT_INCR(sctps_hdrops);
153 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
156 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
157 #if defined(SCTP_WITH_NO_CSUM)
158 SCTP_STAT_INCR(sctps_recvnocrc);
160 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
161 SCTP_STAT_INCR(sctps_recvhwcrc);
164 SCTP_STAT_INCR(sctps_recvswcrc);
168 sctp_common_input_processing(&m, iphlen, offset, length,
169 (struct sockaddr *)&src,
170 (struct sockaddr *)&dst,
172 #if !defined(SCTP_WITH_NO_CSUM)
182 return (IPPROTO_DONE);
187 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
189 return (sctp6_input_with_port(i_pak, offp, 0));
193 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
194 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
198 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
199 (icmp6 == NULL) || (sh == NULL)) {
202 /* First do we even look at it? */
203 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
206 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
207 /* not PACKET TO BIG */
211 * ok we need to look closely. We could even get smarter and look at
212 * anyone that we sent to in case we get a different ICMP that tells
213 * us there is no way to reach a host, but for this impl, all we
214 * care about is MTU discovery.
216 nxtsz = ntohl(icmp6->icmp6_mtu);
217 /* Stop any PMTU timer */
218 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL,
219 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
221 /* Adjust destination size limit */
222 if (net->mtu > nxtsz) {
225 net->mtu -= sizeof(struct udphdr);
228 /* now what about the ep? */
229 if (stcb->asoc.smallest_mtu > nxtsz) {
230 struct sctp_tmit_chunk *chk;
232 /* Adjust that too */
233 stcb->asoc.smallest_mtu = nxtsz;
234 /* now off to subtract IP_DF flag if needed */
236 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
237 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
238 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
241 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
242 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
244 * For this guy we also mark for immediate
245 * resend since we sent to big of chunk
247 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
248 if (chk->sent != SCTP_DATAGRAM_RESEND)
249 stcb->asoc.sent_queue_retran_cnt++;
250 chk->sent = SCTP_DATAGRAM_RESEND;
251 chk->rec.data.doing_fast_retransmit = 0;
253 chk->sent = SCTP_DATAGRAM_RESEND;
254 /* Clear any time so NO RTT is being done */
255 chk->sent_rcv_time.tv_sec = 0;
256 chk->sent_rcv_time.tv_usec = 0;
257 stcb->asoc.total_flight -= chk->send_size;
258 net->flight_size -= chk->send_size;
262 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
265 SCTP_TCB_UNLOCK(stcb);
271 sctp6_notify(struct sctp_inpcb *inp,
272 struct icmp6_hdr *icmph,
275 struct sctp_tcb *stcb,
276 struct sctp_nets *net)
278 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
284 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
285 (sh == NULL) || (to == NULL)) {
287 SCTP_TCB_UNLOCK(stcb);
290 /* First job is to verify the vtag matches what I would send */
291 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
292 SCTP_TCB_UNLOCK(stcb);
295 if (icmph->icmp6_type != ICMP_UNREACH) {
296 /* We only care about unreachable */
297 SCTP_TCB_UNLOCK(stcb);
300 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
301 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
302 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
303 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
304 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
305 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
306 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
307 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
310 * Hmm reachablity problems we must examine closely. If its
311 * not reachable, we may have lost a network. Or if there is
312 * NO protocol at the other end named SCTP. well we consider
315 if (net->dest_state & SCTP_ADDR_REACHABLE) {
316 /* Ok that destination is NOT reachable */
317 net->dest_state &= ~SCTP_ADDR_REACHABLE;
318 net->dest_state &= ~SCTP_ADDR_PF;
319 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
320 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
322 SCTP_TCB_UNLOCK(stcb);
323 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
324 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
326 * Here the peer is either playing tricks on us, including
327 * an address that belongs to someone who does not support
328 * SCTP OR was a userland implementation that shutdown and
329 * now is dead. In either case treat it like a OOTB abort
332 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
333 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
334 so = SCTP_INP_SO(inp);
335 atomic_add_int(&stcb->asoc.refcnt, 1);
336 SCTP_TCB_UNLOCK(stcb);
337 SCTP_SOCKET_LOCK(so, 1);
339 atomic_subtract_int(&stcb->asoc.refcnt, 1);
341 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
342 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
343 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
344 SCTP_SOCKET_UNLOCK(so, 1);
345 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
347 /* no need to unlock here, since the TCB is gone */
349 SCTP_TCB_UNLOCK(stcb);
356 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
359 struct ip6ctlparam *ip6cp = NULL;
362 vrf_id = SCTP_DEFAULT_VRFID;
364 if (pktdst->sa_family != AF_INET6 ||
365 pktdst->sa_len != sizeof(struct sockaddr_in6))
368 if ((unsigned)cmd >= PRC_NCMDS)
370 if (PRC_IS_REDIRECT(cmd)) {
372 } else if (inet6ctlerrmap[cmd] == 0) {
375 /* if the parameter is from icmp6, decode it. */
377 ip6cp = (struct ip6ctlparam *)d;
379 ip6cp = (struct ip6ctlparam *)NULL;
384 * XXX: We assume that when IPV6 is non NULL, M and OFF are
387 /* check if we can safely examine src and dst ports */
388 struct sctp_inpcb *inp = NULL;
389 struct sctp_tcb *stcb = NULL;
390 struct sctp_nets *net = NULL;
391 struct sockaddr_in6 final;
393 if (ip6cp->ip6c_m == NULL)
396 bzero(&sh, sizeof(sh));
397 bzero(&final, sizeof(final));
400 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
402 ip6cp->ip6c_src->sin6_port = sh.src_port;
403 final.sin6_len = sizeof(final);
404 final.sin6_family = AF_INET6;
405 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
406 final.sin6_port = sh.dest_port;
407 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
408 (struct sockaddr *)ip6cp->ip6c_src,
409 &inp, &net, 1, vrf_id);
410 /* inp's ref-count increased && stcb locked */
411 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
412 if (cmd == PRC_MSGSIZE) {
413 sctp6_notify_mbuf(inp,
418 /* inp's ref-count reduced && stcb unlocked */
420 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
421 (struct sockaddr *)&final,
423 /* inp's ref-count reduced && stcb unlocked */
426 if (PRC_IS_REDIRECT(cmd) && inp) {
427 in6_rtchange((struct in6pcb *)inp,
428 inet6ctlerrmap[cmd]);
431 /* reduce inp's ref-count */
433 SCTP_INP_DECR_REF(inp);
434 SCTP_INP_WUNLOCK(inp);
437 SCTP_TCB_UNLOCK(stcb);
443 * this routine can probably be collasped into the one in sctp_userreq.c
444 * since they do the same thing and now we lookup with a sockaddr
447 sctp6_getcred(SYSCTL_HANDLER_ARGS)
450 struct sockaddr_in6 addrs[2];
451 struct sctp_inpcb *inp;
452 struct sctp_nets *net;
453 struct sctp_tcb *stcb;
457 vrf_id = SCTP_DEFAULT_VRFID;
459 error = priv_check(req->td, PRIV_NETINET_GETCRED);
463 if (req->newlen != sizeof(addrs)) {
464 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
467 if (req->oldlen != sizeof(struct ucred)) {
468 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
471 error = SYSCTL_IN(req, addrs, sizeof(addrs));
475 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
477 &inp, &net, 1, vrf_id);
478 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
479 if ((inp != NULL) && (stcb == NULL)) {
480 /* reduce ref-count */
482 SCTP_INP_DECR_REF(inp);
485 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
489 SCTP_TCB_UNLOCK(stcb);
491 * We use the write lock here, only since in the error leg we need
492 * it. If we used RLOCK, then we would have to
493 * wlock/decr/unlock/rlock. Which in theory could create a hole.
494 * Better to use higher wlock.
498 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
500 SCTP_INP_WUNLOCK(inp);
503 cru2x(inp->sctp_socket->so_cred, &xuc);
504 SCTP_INP_WUNLOCK(inp);
505 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
510 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
512 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
515 /* This is the same as the sctp_abort() could be made common */
517 sctp6_abort(struct socket *so)
519 struct sctp_inpcb *inp;
522 inp = (struct sctp_inpcb *)so->so_pcb;
524 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
528 flags = inp->sctp_flags;
529 #ifdef SCTP_LOG_CLOSING
530 sctp_log_closing(inp, NULL, 17);
532 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
533 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
534 #ifdef SCTP_LOG_CLOSING
535 sctp_log_closing(inp, NULL, 16);
537 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
538 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
540 SCTP_SB_CLEAR(so->so_snd);
542 * same for the rcv ones, they are only here for the
545 SCTP_SB_CLEAR(so->so_rcv);
546 /* Now null out the reference, we are completely detached. */
550 flags = inp->sctp_flags;
551 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
552 goto sctp_must_try_again;
559 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
563 struct sctp_inpcb *inp;
564 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
566 inp = (struct sctp_inpcb *)so->so_pcb;
568 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
571 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
572 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
576 error = sctp_inpcb_alloc(so, vrf_id);
579 inp = (struct sctp_inpcb *)so->so_pcb;
581 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
582 inp6 = (struct in6pcb *)inp;
584 inp6->inp_vflag |= INP_IPV6;
585 inp6->in6p_hops = -1; /* use kernel default */
586 inp6->in6p_cksum = -1; /* just to be sure */
589 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
590 * socket as well, because the socket may be bound to an IPv6
591 * wildcard address, which may match an IPv4-mapped IPv6 address.
593 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
596 * Hmm what about the IPSEC stuff that is missing here but in
599 SCTP_INP_WUNLOCK(inp);
604 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
606 struct sctp_inpcb *inp;
610 inp = (struct sctp_inpcb *)so->so_pcb;
612 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
616 switch (addr->sa_family) {
619 if (addr->sa_len != sizeof(struct sockaddr_in)) {
620 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
627 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
628 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
634 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
638 inp6 = (struct in6pcb *)inp;
639 inp6->inp_vflag &= ~INP_IPV4;
640 inp6->inp_vflag |= INP_IPV6;
641 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
642 switch (addr->sa_family) {
645 /* binding v4 addr to v6 socket, so reset flags */
646 inp6->inp_vflag |= INP_IPV4;
647 inp6->inp_vflag &= ~INP_IPV6;
653 struct sockaddr_in6 *sin6_p;
655 sin6_p = (struct sockaddr_in6 *)addr;
657 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
658 inp6->inp_vflag |= INP_IPV4;
661 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
662 struct sockaddr_in sin;
664 in6_sin6_2_sin(&sin, sin6_p);
665 inp6->inp_vflag |= INP_IPV4;
666 inp6->inp_vflag &= ~INP_IPV6;
667 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
677 } else if (addr != NULL) {
678 struct sockaddr_in6 *sin6_p;
680 /* IPV6_V6ONLY socket */
682 if (addr->sa_family == AF_INET) {
683 /* can't bind v4 addr to v6 only socket! */
684 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
688 sin6_p = (struct sockaddr_in6 *)addr;
690 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
691 /* can't bind v4-mapped addrs either! */
692 /* NOTE: we don't support SIIT */
693 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
697 error = sctp_inpcb_bind(so, addr, NULL, p);
703 sctp6_close(struct socket *so)
708 /* This could be made common with sctp_detach() since they are identical */
712 sctp6_disconnect(struct socket *so)
714 return (sctp_disconnect(so));
719 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
720 struct mbuf *control, struct thread *p);
724 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
725 struct mbuf *control, struct thread *p)
727 struct sctp_inpcb *inp;
731 struct sockaddr_in6 *sin6;
734 /* No SPL needed since sctp_output does this */
736 inp = (struct sctp_inpcb *)so->so_pcb;
739 SCTP_RELEASE_PKT(control);
743 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
746 inp6 = (struct in6pcb *)inp;
748 * For the TCP model we may get a NULL addr, if we are a connected
751 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
758 SCTP_RELEASE_PKT(control);
761 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
762 return (EDESTADDRREQ);
765 sin6 = (struct sockaddr_in6 *)addr;
766 if (SCTP_IPV6_V6ONLY(inp6)) {
768 * if IPV6_V6ONLY flag, we discard datagrams destined to a
769 * v4 addr or v4-mapped addr
771 if (addr->sa_family == AF_INET) {
772 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
775 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
780 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
781 struct sockaddr_in sin;
783 /* convert v4-mapped into v4 addr and send */
784 in6_sin6_2_sin(&sin, sin6);
785 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
789 /* now what about control */
792 SCTP_PRINTF("huh? control set?\n");
793 SCTP_RELEASE_PKT(inp->control);
796 inp->control = control;
800 SCTP_BUF_NEXT(inp->pkt_last) = m;
803 inp->pkt_last = inp->pkt = m;
806 /* FreeBSD and MacOSX uses a flag passed */
807 ((flags & PRUS_MORETOCOME) == 0)
810 * note with the current version this code will only be used
811 * by OpenBSD, NetBSD and FreeBSD have methods for
812 * re-defining sosend() to use sctp_sosend(). One can
813 * optionaly switch back to this code (by changing back the
814 * defininitions but this is not advisable.
818 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
828 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
832 struct sctp_inpcb *inp;
833 struct sctp_tcb *stcb;
837 struct sockaddr_in6 *sin6;
838 union sctp_sockstore store;
843 inp6 = (struct in6pcb *)so->so_pcb;
845 inp = (struct sctp_inpcb *)so->so_pcb;
847 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
848 return (ECONNRESET); /* I made the same as TCP since we are
852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
855 switch (addr->sa_family) {
858 if (addr->sa_len != sizeof(struct sockaddr_in)) {
859 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
866 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
867 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
873 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
877 vrf_id = inp->def_vrf_id;
878 SCTP_ASOC_CREATE_LOCK(inp);
880 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
881 SCTP_PCB_FLAGS_UNBOUND) {
882 /* Bind a ephemeral port */
883 SCTP_INP_RUNLOCK(inp);
884 error = sctp6_bind(so, NULL, p);
886 SCTP_ASOC_CREATE_UNLOCK(inp);
892 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
893 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
894 /* We are already connected AND the TCP model */
895 SCTP_INP_RUNLOCK(inp);
896 SCTP_ASOC_CREATE_UNLOCK(inp);
897 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
901 sin6 = (struct sockaddr_in6 *)addr;
902 if (SCTP_IPV6_V6ONLY(inp6)) {
904 * if IPV6_V6ONLY flag, ignore connections destined to a v4
905 * addr or v4-mapped addr
907 if (addr->sa_family == AF_INET) {
908 SCTP_INP_RUNLOCK(inp);
909 SCTP_ASOC_CREATE_UNLOCK(inp);
910 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
913 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
914 SCTP_INP_RUNLOCK(inp);
915 SCTP_ASOC_CREATE_UNLOCK(inp);
916 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
920 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
921 /* convert v4-mapped into v4 addr */
922 in6_sin6_2_sin(&store.sin, sin6);
926 /* Now do we connect? */
927 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
928 stcb = LIST_FIRST(&inp->sctp_asoc_list);
930 SCTP_TCB_UNLOCK(stcb);
932 SCTP_INP_RUNLOCK(inp);
934 SCTP_INP_RUNLOCK(inp);
936 SCTP_INP_INCR_REF(inp);
937 SCTP_INP_WUNLOCK(inp);
938 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
941 SCTP_INP_DECR_REF(inp);
942 SCTP_INP_WUNLOCK(inp);
947 /* Already have or am bring up an association */
948 SCTP_ASOC_CREATE_UNLOCK(inp);
949 SCTP_TCB_UNLOCK(stcb);
950 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
953 /* We are GOOD to go */
954 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
955 SCTP_ASOC_CREATE_UNLOCK(inp);
960 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
961 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
962 /* Set the connected flag so we can queue data */
965 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
966 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
968 /* initialize authentication parameters for the assoc */
969 sctp_initialize_auth_params(inp, stcb);
971 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
972 SCTP_TCB_UNLOCK(stcb);
977 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
979 struct sockaddr_in6 *sin6;
980 struct sctp_inpcb *inp;
982 struct sctp_ifa *sctp_ifa;
987 * Do the malloc first in case it blocks.
989 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
992 sin6->sin6_family = AF_INET6;
993 sin6->sin6_len = sizeof(*sin6);
995 inp = (struct sctp_inpcb *)so->so_pcb;
997 SCTP_FREE_SONAME(sin6);
998 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1001 SCTP_INP_RLOCK(inp);
1002 sin6->sin6_port = inp->sctp_lport;
1003 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1004 /* For the bound all case you get back 0 */
1005 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1006 struct sctp_tcb *stcb;
1007 struct sockaddr_in6 *sin_a6;
1008 struct sctp_nets *net;
1011 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1017 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1018 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1020 /* this will make coverity happy */
1023 if (sin_a6->sin6_family == AF_INET6) {
1028 if ((!fnd) || (sin_a6 == NULL)) {
1032 vrf_id = inp->def_vrf_id;
1033 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1035 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1038 /* For the bound all case you get back 0 */
1040 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1043 /* Take the first IPv6 address in the list */
1044 struct sctp_laddr *laddr;
1047 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1048 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1049 struct sockaddr_in6 *sin_a;
1051 sin_a = &laddr->ifa->address.sin6;
1052 sin6->sin6_addr = sin_a->sin6_addr;
1058 SCTP_FREE_SONAME(sin6);
1059 SCTP_INP_RUNLOCK(inp);
1060 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1064 SCTP_INP_RUNLOCK(inp);
1065 /* Scoping things for v6 */
1066 if ((error = sa6_recoverscope(sin6)) != 0) {
1067 SCTP_FREE_SONAME(sin6);
1070 (*addr) = (struct sockaddr *)sin6;
1075 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1077 struct sockaddr_in6 *sin6;
1079 struct sockaddr_in6 *sin_a6;
1080 struct sctp_inpcb *inp;
1081 struct sctp_tcb *stcb;
1082 struct sctp_nets *net;
1085 /* Do the malloc first in case it blocks. */
1086 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1089 sin6->sin6_family = AF_INET6;
1090 sin6->sin6_len = sizeof(*sin6);
1092 inp = (struct sctp_inpcb *)so->so_pcb;
1093 if ((inp == NULL) ||
1094 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1095 /* UDP type and listeners will drop out here */
1096 SCTP_FREE_SONAME(sin6);
1097 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1100 SCTP_INP_RLOCK(inp);
1101 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1103 SCTP_TCB_LOCK(stcb);
1105 SCTP_INP_RUNLOCK(inp);
1107 SCTP_FREE_SONAME(sin6);
1108 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1109 return (ECONNRESET);
1112 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1113 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1114 if (sin_a6->sin6_family == AF_INET6) {
1116 sin6->sin6_port = stcb->rport;
1117 sin6->sin6_addr = sin_a6->sin6_addr;
1121 SCTP_TCB_UNLOCK(stcb);
1123 /* No IPv4 address */
1124 SCTP_FREE_SONAME(sin6);
1125 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1128 if ((error = sa6_recoverscope(sin6)) != 0) {
1129 SCTP_FREE_SONAME(sin6);
1130 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1133 *addr = (struct sockaddr *)sin6;
1138 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1141 struct sockaddr *addr;
1144 struct in6pcb *inp6 = sotoin6pcb(so);
1148 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1151 /* allow v6 addresses precedence */
1152 error = sctp6_getaddr(so, nam);
1155 /* try v4 next if v6 failed */
1156 error = sctp_ingetaddr(so, nam);
1161 /* if I'm V6ONLY, convert it to v4-mapped */
1162 if (SCTP_IPV6_V6ONLY(inp6)) {
1163 struct sockaddr_in6 sin6;
1165 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1166 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1175 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1178 struct sockaddr *addr;
1181 struct in6pcb *inp6 = sotoin6pcb(so);
1185 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1188 /* allow v6 addresses precedence */
1189 error = sctp6_peeraddr(so, nam);
1192 /* try v4 next if v6 failed */
1193 error = sctp_peeraddr(so, nam);
1198 /* if I'm V6ONLY, convert it to v4-mapped */
1199 if (SCTP_IPV6_V6ONLY(inp6)) {
1200 struct sockaddr_in6 sin6;
1202 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1203 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1210 struct pr_usrreqs sctp6_usrreqs = {
1211 .pru_abort = sctp6_abort,
1212 .pru_accept = sctp_accept,
1213 .pru_attach = sctp6_attach,
1214 .pru_bind = sctp6_bind,
1215 .pru_connect = sctp6_connect,
1216 .pru_control = in6_control,
1217 .pru_close = sctp6_close,
1218 .pru_detach = sctp6_close,
1219 .pru_sopoll = sopoll_generic,
1220 .pru_flush = sctp_flush,
1221 .pru_disconnect = sctp6_disconnect,
1222 .pru_listen = sctp_listen,
1223 .pru_peeraddr = sctp6_getpeeraddr,
1224 .pru_send = sctp6_send,
1225 .pru_shutdown = sctp_shutdown,
1226 .pru_sockaddr = sctp6_in6getaddr,
1227 .pru_sosend = sctp_sosend,
1228 .pru_soreceive = sctp_soreceive