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>
42 #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>
54 #include <netinet/sctp_crc32.h>
55 #include <netinet/icmp6.h>
56 #include <netinet/udp.h>
59 #include <netipsec/ipsec.h>
60 #include <netipsec/ipsec6.h>
63 extern struct protosw inetsw[];
66 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
72 struct sockaddr_in6 src, dst;
75 struct sctp_chunkhdr *ch;
78 #if !defined(SCTP_WITH_NO_CSUM)
87 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
88 SCTP_RELEASE_PKT(*i_pak);
89 return (IPPROTO_DONE);
91 m = SCTP_HEADER_TO_CHAIN(*i_pak);
92 #ifdef SCTP_MBUF_LOGGING
93 /* Log in any input mbufs */
94 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
95 sctp_log_mbc(m, SCTP_MBUF_INPUT);
98 #ifdef SCTP_PACKET_LOGGING
99 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
103 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
104 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
106 if_name(m->m_pkthdr.rcvif),
107 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
108 mflowid = m->m_pkthdr.flowid;
109 mflowtype = M_HASHTYPE_GET(m);
110 fibnum = M_GETFIB(m);
111 SCTP_STAT_INCR(sctps_recvpackets);
112 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
113 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
114 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
115 ip6 = mtod(m, struct ip6_hdr *);
116 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
117 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
119 SCTP_STAT_INCR(sctps_hdrops);
120 return (IPPROTO_DONE);
122 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
123 offset -= sizeof(struct sctp_chunkhdr);
124 memset(&src, 0, sizeof(struct sockaddr_in6));
125 src.sin6_family = AF_INET6;
126 src.sin6_len = sizeof(struct sockaddr_in6);
127 src.sin6_port = sh->src_port;
128 src.sin6_addr = ip6->ip6_src;
129 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
132 memset(&dst, 0, sizeof(struct sockaddr_in6));
133 dst.sin6_family = AF_INET6;
134 dst.sin6_len = sizeof(struct sockaddr_in6);
135 dst.sin6_port = sh->dest_port;
136 dst.sin6_addr = ip6->ip6_dst;
137 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
140 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
141 /* XXX send icmp6 host/port unreach? */
144 length = ntohs(ip6->ip6_plen) + iphlen;
145 /* Validate mbuf chain length with IP payload length. */
146 if (SCTP_HEADER_LEN(m) != length) {
147 SCTPDBG(SCTP_DEBUG_INPUT1,
148 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
149 SCTP_STAT_INCR(sctps_hdrops);
152 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
155 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
156 #if defined(SCTP_WITH_NO_CSUM)
157 SCTP_STAT_INCR(sctps_recvnocrc);
159 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
160 SCTP_STAT_INCR(sctps_recvhwcrc);
163 SCTP_STAT_INCR(sctps_recvswcrc);
167 sctp_common_input_processing(&m, iphlen, offset, length,
168 (struct sockaddr *)&src,
169 (struct sockaddr *)&dst,
171 #if !defined(SCTP_WITH_NO_CSUM)
175 mflowtype, mflowid, fibnum,
181 return (IPPROTO_DONE);
186 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
188 return (sctp6_input_with_port(i_pak, offp, 0));
192 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
193 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
197 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
198 (icmp6 == NULL) || (sh == NULL)) {
201 /* First do we even look at it? */
202 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
205 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
206 /* not PACKET TO BIG */
210 * ok we need to look closely. We could even get smarter and look at
211 * anyone that we sent to in case we get a different ICMP that tells
212 * us there is no way to reach a host, but for this impl, all we
213 * care about is MTU discovery.
215 nxtsz = ntohl(icmp6->icmp6_mtu);
216 /* Stop any PMTU timer */
217 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL,
218 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
220 /* Adjust destination size limit */
221 if (net->mtu > nxtsz) {
224 net->mtu -= sizeof(struct udphdr);
227 /* now what about the ep? */
228 if (stcb->asoc.smallest_mtu > nxtsz) {
229 struct sctp_tmit_chunk *chk;
231 /* Adjust that too */
232 stcb->asoc.smallest_mtu = nxtsz;
233 /* now off to subtract IP_DF flag if needed */
235 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
236 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
237 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
240 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
241 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
243 * For this guy we also mark for immediate
244 * resend since we sent to big of chunk
246 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
247 if (chk->sent != SCTP_DATAGRAM_RESEND)
248 stcb->asoc.sent_queue_retran_cnt++;
249 chk->sent = SCTP_DATAGRAM_RESEND;
250 chk->rec.data.doing_fast_retransmit = 0;
252 chk->sent = SCTP_DATAGRAM_RESEND;
253 /* Clear any time so NO RTT is being done */
254 chk->sent_rcv_time.tv_sec = 0;
255 chk->sent_rcv_time.tv_usec = 0;
256 stcb->asoc.total_flight -= chk->send_size;
257 net->flight_size -= chk->send_size;
261 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
264 SCTP_TCB_UNLOCK(stcb);
270 sctp6_notify(struct sctp_inpcb *inp,
271 struct icmp6_hdr *icmph,
274 struct sctp_tcb *stcb,
275 struct sctp_nets *net)
277 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
283 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
284 (sh == NULL) || (to == NULL)) {
286 SCTP_TCB_UNLOCK(stcb);
289 /* First job is to verify the vtag matches what I would send */
290 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
291 SCTP_TCB_UNLOCK(stcb);
294 if (icmph->icmp6_type != ICMP_UNREACH) {
295 /* We only care about unreachable */
296 SCTP_TCB_UNLOCK(stcb);
299 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
300 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
301 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
302 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
303 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
304 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
305 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
306 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
309 * Hmm reachablity problems we must examine closely. If its
310 * not reachable, we may have lost a network. Or if there is
311 * NO protocol at the other end named SCTP. well we consider
314 if (net->dest_state & SCTP_ADDR_REACHABLE) {
315 /* Ok that destination is NOT reachable */
316 net->dest_state &= ~SCTP_ADDR_REACHABLE;
317 net->dest_state &= ~SCTP_ADDR_PF;
318 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
319 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
321 SCTP_TCB_UNLOCK(stcb);
322 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
323 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
325 * Here the peer is either playing tricks on us, including
326 * an address that belongs to someone who does not support
327 * SCTP OR was a userland implementation that shutdown and
328 * now is dead. In either case treat it like a OOTB abort
331 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
332 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
333 so = SCTP_INP_SO(inp);
334 atomic_add_int(&stcb->asoc.refcnt, 1);
335 SCTP_TCB_UNLOCK(stcb);
336 SCTP_SOCKET_LOCK(so, 1);
338 atomic_subtract_int(&stcb->asoc.refcnt, 1);
340 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
341 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
342 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
343 SCTP_SOCKET_UNLOCK(so, 1);
344 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
346 /* no need to unlock here, since the TCB is gone */
348 SCTP_TCB_UNLOCK(stcb);
355 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
358 struct ip6ctlparam *ip6cp = NULL;
361 vrf_id = SCTP_DEFAULT_VRFID;
363 if (pktdst->sa_family != AF_INET6 ||
364 pktdst->sa_len != sizeof(struct sockaddr_in6))
367 if ((unsigned)cmd >= PRC_NCMDS)
369 if (PRC_IS_REDIRECT(cmd)) {
371 } else if (inet6ctlerrmap[cmd] == 0) {
374 /* if the parameter is from icmp6, decode it. */
376 ip6cp = (struct ip6ctlparam *)d;
378 ip6cp = (struct ip6ctlparam *)NULL;
383 * XXX: We assume that when IPV6 is non NULL, M and OFF are
386 struct sctp_inpcb *inp = NULL;
387 struct sctp_tcb *stcb = NULL;
388 struct sctp_nets *net = NULL;
389 struct sockaddr_in6 final;
391 if (ip6cp->ip6c_m == NULL)
394 /* Check if we can safely examine the SCTP header. */
395 if (ip6cp->ip6c_m->m_pkthdr.len < ip6cp->ip6c_off + sizeof(sh))
398 bzero(&sh, sizeof(sh));
399 bzero(&final, sizeof(final));
402 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
404 ip6cp->ip6c_src->sin6_port = sh.src_port;
405 final.sin6_len = sizeof(final);
406 final.sin6_family = AF_INET6;
407 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
408 final.sin6_port = sh.dest_port;
409 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
410 (struct sockaddr *)ip6cp->ip6c_src,
411 &inp, &net, 1, vrf_id);
412 /* inp's ref-count increased && stcb locked */
413 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
414 if (cmd == PRC_MSGSIZE) {
415 sctp6_notify_mbuf(inp,
420 /* inp's ref-count reduced && stcb unlocked */
422 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
423 (struct sockaddr *)&final,
425 /* inp's ref-count reduced && stcb unlocked */
428 if (PRC_IS_REDIRECT(cmd) && inp) {
429 in6_rtchange((struct in6pcb *)inp,
430 inet6ctlerrmap[cmd]);
433 /* reduce inp's ref-count */
435 SCTP_INP_DECR_REF(inp);
436 SCTP_INP_WUNLOCK(inp);
439 SCTP_TCB_UNLOCK(stcb);
445 * this routine can probably be collasped into the one in sctp_userreq.c
446 * since they do the same thing and now we lookup with a sockaddr
449 sctp6_getcred(SYSCTL_HANDLER_ARGS)
452 struct sockaddr_in6 addrs[2];
453 struct sctp_inpcb *inp;
454 struct sctp_nets *net;
455 struct sctp_tcb *stcb;
459 vrf_id = SCTP_DEFAULT_VRFID;
461 error = priv_check(req->td, PRIV_NETINET_GETCRED);
465 if (req->newlen != sizeof(addrs)) {
466 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
469 if (req->oldlen != sizeof(struct ucred)) {
470 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
473 error = SYSCTL_IN(req, addrs, sizeof(addrs));
477 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
479 &inp, &net, 1, vrf_id);
480 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
481 if ((inp != NULL) && (stcb == NULL)) {
482 /* reduce ref-count */
484 SCTP_INP_DECR_REF(inp);
487 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
491 SCTP_TCB_UNLOCK(stcb);
493 * We use the write lock here, only since in the error leg we need
494 * it. If we used RLOCK, then we would have to
495 * wlock/decr/unlock/rlock. Which in theory could create a hole.
496 * Better to use higher wlock.
500 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
502 SCTP_INP_WUNLOCK(inp);
505 cru2x(inp->sctp_socket->so_cred, &xuc);
506 SCTP_INP_WUNLOCK(inp);
507 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
512 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
514 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
517 /* This is the same as the sctp_abort() could be made common */
519 sctp6_abort(struct socket *so)
521 struct sctp_inpcb *inp;
524 inp = (struct sctp_inpcb *)so->so_pcb;
526 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
530 flags = inp->sctp_flags;
531 #ifdef SCTP_LOG_CLOSING
532 sctp_log_closing(inp, NULL, 17);
534 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
535 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
536 #ifdef SCTP_LOG_CLOSING
537 sctp_log_closing(inp, NULL, 16);
539 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
540 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
542 SCTP_SB_CLEAR(so->so_snd);
544 * same for the rcv ones, they are only here for the
547 SCTP_SB_CLEAR(so->so_rcv);
548 /* Now null out the reference, we are completely detached. */
552 flags = inp->sctp_flags;
553 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
554 goto sctp_must_try_again;
561 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
565 struct sctp_inpcb *inp;
566 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
568 inp = (struct sctp_inpcb *)so->so_pcb;
570 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
573 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
574 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
578 error = sctp_inpcb_alloc(so, vrf_id);
581 inp = (struct sctp_inpcb *)so->so_pcb;
583 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
584 inp6 = (struct in6pcb *)inp;
586 inp6->inp_vflag |= INP_IPV6;
587 inp6->in6p_hops = -1; /* use kernel default */
588 inp6->in6p_cksum = -1; /* just to be sure */
591 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
592 * socket as well, because the socket may be bound to an IPv6
593 * wildcard address, which may match an IPv4-mapped IPv6 address.
595 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
598 * Hmm what about the IPSEC stuff that is missing here but in
601 SCTP_INP_WUNLOCK(inp);
606 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
608 struct sctp_inpcb *inp;
613 inp = (struct sctp_inpcb *)so->so_pcb;
615 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
619 switch (addr->sa_family) {
622 if (addr->sa_len != sizeof(struct sockaddr_in)) {
623 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
630 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
631 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
637 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
641 inp6 = (struct in6pcb *)inp;
642 vflagsav = inp6->inp_vflag;
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);
693 sin6_p = (struct sockaddr_in6 *)addr;
695 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
696 /* can't bind v4-mapped addrs either! */
697 /* NOTE: we don't support SIIT */
698 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
703 error = sctp_inpcb_bind(so, addr, NULL, p);
706 inp6->inp_vflag = vflagsav;
712 sctp6_close(struct socket *so)
717 /* This could be made common with sctp_detach() since they are identical */
721 sctp6_disconnect(struct socket *so)
723 return (sctp_disconnect(so));
728 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
729 struct mbuf *control, struct thread *p);
733 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
734 struct mbuf *control, struct thread *p)
736 struct sctp_inpcb *inp;
740 struct sockaddr_in6 *sin6;
743 /* No SPL needed since sctp_output does this */
745 inp = (struct sctp_inpcb *)so->so_pcb;
748 SCTP_RELEASE_PKT(control);
752 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
755 inp6 = (struct in6pcb *)inp;
757 * For the TCP model we may get a NULL addr, if we are a connected
760 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
767 SCTP_RELEASE_PKT(control);
770 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
771 return (EDESTADDRREQ);
774 sin6 = (struct sockaddr_in6 *)addr;
775 if (SCTP_IPV6_V6ONLY(inp6)) {
777 * if IPV6_V6ONLY flag, we discard datagrams destined to a
778 * v4 addr or v4-mapped addr
780 if (addr->sa_family == AF_INET) {
781 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
784 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
785 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
789 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
790 struct sockaddr_in sin;
792 /* convert v4-mapped into v4 addr and send */
793 in6_sin6_2_sin(&sin, sin6);
794 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
798 /* now what about control */
801 SCTP_PRINTF("huh? control set?\n");
802 SCTP_RELEASE_PKT(inp->control);
805 inp->control = control;
809 SCTP_BUF_NEXT(inp->pkt_last) = m;
812 inp->pkt_last = inp->pkt = m;
815 /* FreeBSD and MacOSX uses a flag passed */
816 ((flags & PRUS_MORETOCOME) == 0)
819 * note with the current version this code will only be used
820 * by OpenBSD, NetBSD and FreeBSD have methods for
821 * re-defining sosend() to use sctp_sosend(). One can
822 * optionaly switch back to this code (by changing back the
823 * defininitions but this is not advisable.
827 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
837 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
841 struct sctp_inpcb *inp;
842 struct sctp_tcb *stcb;
846 struct sockaddr_in6 *sin6;
847 union sctp_sockstore store;
852 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 /* convert v4-mapped into v4 addr */
931 in6_sin6_2_sin(&store.sin, sin6);
935 /* Now do we connect? */
936 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
937 stcb = LIST_FIRST(&inp->sctp_asoc_list);
939 SCTP_TCB_UNLOCK(stcb);
941 SCTP_INP_RUNLOCK(inp);
943 SCTP_INP_RUNLOCK(inp);
945 SCTP_INP_INCR_REF(inp);
946 SCTP_INP_WUNLOCK(inp);
947 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
950 SCTP_INP_DECR_REF(inp);
951 SCTP_INP_WUNLOCK(inp);
956 /* Already have or am bring up an association */
957 SCTP_ASOC_CREATE_UNLOCK(inp);
958 SCTP_TCB_UNLOCK(stcb);
959 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
962 /* We are GOOD to go */
963 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, inp->sctp_ep.pre_open_stream_count, p);
964 SCTP_ASOC_CREATE_UNLOCK(inp);
969 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
970 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
971 /* Set the connected flag so we can queue data */
974 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
975 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
977 /* initialize authentication parameters for the assoc */
978 sctp_initialize_auth_params(inp, stcb);
980 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
981 SCTP_TCB_UNLOCK(stcb);
986 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
988 struct sockaddr_in6 *sin6;
989 struct sctp_inpcb *inp;
991 struct sctp_ifa *sctp_ifa;
996 * Do the malloc first in case it blocks.
998 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1001 sin6->sin6_family = AF_INET6;
1002 sin6->sin6_len = sizeof(*sin6);
1004 inp = (struct sctp_inpcb *)so->so_pcb;
1006 SCTP_FREE_SONAME(sin6);
1007 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1008 return (ECONNRESET);
1010 SCTP_INP_RLOCK(inp);
1011 sin6->sin6_port = inp->sctp_lport;
1012 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1013 /* For the bound all case you get back 0 */
1014 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1015 struct sctp_tcb *stcb;
1016 struct sockaddr_in6 *sin_a6;
1017 struct sctp_nets *net;
1020 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1022 SCTP_INP_RUNLOCK(inp);
1023 SCTP_FREE_SONAME(sin6);
1024 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1029 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1030 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1032 /* this will make coverity happy */
1035 if (sin_a6->sin6_family == AF_INET6) {
1040 if ((!fnd) || (sin_a6 == NULL)) {
1042 SCTP_INP_RUNLOCK(inp);
1043 SCTP_FREE_SONAME(sin6);
1044 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1047 vrf_id = inp->def_vrf_id;
1048 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1050 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1053 /* For the bound all case you get back 0 */
1054 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1057 /* Take the first IPv6 address in the list */
1058 struct sctp_laddr *laddr;
1061 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1062 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1063 struct sockaddr_in6 *sin_a;
1065 sin_a = &laddr->ifa->address.sin6;
1066 sin6->sin6_addr = sin_a->sin6_addr;
1072 SCTP_FREE_SONAME(sin6);
1073 SCTP_INP_RUNLOCK(inp);
1074 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1078 SCTP_INP_RUNLOCK(inp);
1079 /* Scoping things for v6 */
1080 if ((error = sa6_recoverscope(sin6)) != 0) {
1081 SCTP_FREE_SONAME(sin6);
1084 (*addr) = (struct sockaddr *)sin6;
1089 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1091 struct sockaddr_in6 *sin6;
1093 struct sockaddr_in6 *sin_a6;
1094 struct sctp_inpcb *inp;
1095 struct sctp_tcb *stcb;
1096 struct sctp_nets *net;
1099 /* Do the malloc first in case it blocks. */
1100 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1103 sin6->sin6_family = AF_INET6;
1104 sin6->sin6_len = sizeof(*sin6);
1106 inp = (struct sctp_inpcb *)so->so_pcb;
1107 if ((inp == NULL) ||
1108 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1109 /* UDP type and listeners will drop out here */
1110 SCTP_FREE_SONAME(sin6);
1111 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1114 SCTP_INP_RLOCK(inp);
1115 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1117 SCTP_TCB_LOCK(stcb);
1119 SCTP_INP_RUNLOCK(inp);
1121 SCTP_FREE_SONAME(sin6);
1122 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1123 return (ECONNRESET);
1126 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1127 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1128 if (sin_a6->sin6_family == AF_INET6) {
1130 sin6->sin6_port = stcb->rport;
1131 sin6->sin6_addr = sin_a6->sin6_addr;
1135 SCTP_TCB_UNLOCK(stcb);
1137 /* No IPv4 address */
1138 SCTP_FREE_SONAME(sin6);
1139 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1142 if ((error = sa6_recoverscope(sin6)) != 0) {
1143 SCTP_FREE_SONAME(sin6);
1144 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1147 *addr = (struct sockaddr *)sin6;
1152 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1154 struct in6pcb *inp6 = sotoin6pcb(so);
1158 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1161 /* allow v6 addresses precedence */
1162 error = sctp6_getaddr(so, nam);
1165 struct sockaddr_in6 *sin6;
1167 /* try v4 next if v6 failed */
1168 error = sctp_ingetaddr(so, nam);
1172 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1174 SCTP_FREE_SONAME(*nam);
1177 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1178 SCTP_FREE_SONAME(*nam);
1179 *nam = (struct sockaddr *)sin6;
1187 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1189 struct in6pcb *inp6 = sotoin6pcb(so);
1193 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1196 /* allow v6 addresses precedence */
1197 error = sctp6_peeraddr(so, nam);
1200 struct sockaddr_in6 *sin6;
1202 /* try v4 next if v6 failed */
1203 error = sctp_peeraddr(so, nam);
1207 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1209 SCTP_FREE_SONAME(*nam);
1212 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1213 SCTP_FREE_SONAME(*nam);
1214 *nam = (struct sockaddr *)sin6;
1220 struct pr_usrreqs sctp6_usrreqs = {
1221 .pru_abort = sctp6_abort,
1222 .pru_accept = sctp_accept,
1223 .pru_attach = sctp6_attach,
1224 .pru_bind = sctp6_bind,
1225 .pru_connect = sctp6_connect,
1226 .pru_control = in6_control,
1227 .pru_close = sctp6_close,
1228 .pru_detach = sctp6_close,
1229 .pru_sopoll = sopoll_generic,
1230 .pru_flush = sctp_flush,
1231 .pru_disconnect = sctp6_disconnect,
1232 .pru_listen = sctp_listen,
1233 .pru_peeraddr = sctp6_getpeeraddr,
1234 .pru_send = sctp6_send,
1235 .pru_shutdown = sctp_shutdown,
1236 .pru_sockaddr = sctp6_in6getaddr,
1237 .pru_sosend = sctp_sosend,
1238 .pru_soreceive = sctp_soreceive