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>
38 #include <netinet/sctp_pcb.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_var.h>
42 #include <netinet6/sctp6_var.h>
44 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_input.h>
53 #include <netinet/sctp_output.h>
54 #include <netinet/sctp_bsd_addr.h>
55 #include <netinet/sctp_crc32.h>
56 #include <netinet/udp.h>
59 #include <netipsec/ipsec.h>
61 #include <netipsec/ipsec6.h>
65 extern struct protosw inetsw[];
68 sctp6_input(struct mbuf **i_pak, int *offp, int proto)
74 struct sockaddr_in6 src, dst;
77 struct sctp_chunkhdr *ch;
80 #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) {
99 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
100 if (SCTP_BUF_IS_EXTENDED(mat)) {
101 sctp_log_mb(mat, SCTP_MBUF_INPUT);
106 #ifdef SCTP_PACKET_LOGGING
107 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
111 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
112 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
114 if_name(m->m_pkthdr.rcvif),
115 m->m_pkthdr.csum_flags);
116 if (m->m_flags & M_FLOWID) {
117 mflowid = m->m_pkthdr.flowid;
123 SCTP_STAT_INCR(sctps_recvpackets);
124 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
125 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
126 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
127 ip6 = mtod(m, struct ip6_hdr *);
128 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
129 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
131 SCTP_STAT_INCR(sctps_hdrops);
132 return (IPPROTO_DONE);
134 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
135 offset -= sizeof(struct sctp_chunkhdr);
136 memset(&src, 0, sizeof(struct sockaddr_in6));
137 src.sin6_family = AF_INET6;
138 src.sin6_len = sizeof(struct sockaddr_in6);
139 src.sin6_port = sh->src_port;
140 src.sin6_addr = ip6->ip6_src;
141 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
144 memset(&dst, 0, sizeof(struct sockaddr_in6));
145 dst.sin6_family = AF_INET6;
146 dst.sin6_len = sizeof(struct sockaddr_in6);
147 dst.sin6_port = sh->dest_port;
148 dst.sin6_addr = ip6->ip6_dst;
149 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
152 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
153 /* XXX send icmp6 host/port unreach? */
156 length = ntohs(ip6->ip6_plen) + iphlen;
157 /* Validate mbuf chain length with IP payload length. */
158 if (SCTP_HEADER_LEN(m) != length) {
159 SCTPDBG(SCTP_DEBUG_INPUT1,
160 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
161 SCTP_STAT_INCR(sctps_hdrops);
164 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
167 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
168 #if defined(SCTP_WITH_NO_CSUM)
169 SCTP_STAT_INCR(sctps_recvnocrc);
171 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
172 SCTP_STAT_INCR(sctps_recvhwcrc);
175 SCTP_STAT_INCR(sctps_recvswcrc);
179 sctp_common_input_processing(&m, iphlen, offset, length,
180 (struct sockaddr *)&src,
181 (struct sockaddr *)&dst,
183 #if !defined(SCTP_WITH_NO_CSUM)
187 use_mflowid, mflowid,
193 return (IPPROTO_DONE);
198 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
199 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
203 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
204 (icmp6 == NULL) || (sh == NULL)) {
207 /* First do we even look at it? */
208 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
211 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
212 /* not PACKET TO BIG */
216 * ok we need to look closely. We could even get smarter and look at
217 * anyone that we sent to in case we get a different ICMP that tells
218 * us there is no way to reach a host, but for this impl, all we
219 * care about is MTU discovery.
221 nxtsz = ntohl(icmp6->icmp6_mtu);
222 /* Stop any PMTU timer */
223 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
225 /* Adjust destination size limit */
226 if (net->mtu > nxtsz) {
229 net->mtu -= sizeof(struct udphdr);
232 /* now what about the ep? */
233 if (stcb->asoc.smallest_mtu > nxtsz) {
234 struct sctp_tmit_chunk *chk;
236 /* Adjust that too */
237 stcb->asoc.smallest_mtu = nxtsz;
238 /* now off to subtract IP_DF flag if needed */
240 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
241 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
242 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
245 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
246 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
248 * For this guy we also mark for immediate
249 * resend since we sent to big of chunk
251 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
252 if (chk->sent != SCTP_DATAGRAM_RESEND)
253 stcb->asoc.sent_queue_retran_cnt++;
254 chk->sent = SCTP_DATAGRAM_RESEND;
255 chk->rec.data.doing_fast_retransmit = 0;
257 chk->sent = SCTP_DATAGRAM_RESEND;
258 /* Clear any time so NO RTT is being done */
259 chk->sent_rcv_time.tv_sec = 0;
260 chk->sent_rcv_time.tv_usec = 0;
261 stcb->asoc.total_flight -= chk->send_size;
262 net->flight_size -= chk->send_size;
266 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
269 SCTP_TCB_UNLOCK(stcb);
275 sctp6_notify(struct sctp_inpcb *inp,
276 struct icmp6_hdr *icmph,
279 struct sctp_tcb *stcb,
280 struct sctp_nets *net)
282 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
288 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
289 (sh == NULL) || (to == NULL)) {
291 SCTP_TCB_UNLOCK(stcb);
294 /* First job is to verify the vtag matches what I would send */
295 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
296 SCTP_TCB_UNLOCK(stcb);
299 if (icmph->icmp6_type != ICMP_UNREACH) {
300 /* We only care about unreachable */
301 SCTP_TCB_UNLOCK(stcb);
304 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
305 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
306 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
307 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
308 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
309 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
310 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
311 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
314 * Hmm reachablity problems we must examine closely. If its
315 * not reachable, we may have lost a network. Or if there is
316 * NO protocol at the other end named SCTP. well we consider
319 if (net->dest_state & SCTP_ADDR_REACHABLE) {
320 /* Ok that destination is NOT reachable */
321 net->dest_state &= ~SCTP_ADDR_REACHABLE;
322 net->dest_state &= ~SCTP_ADDR_PF;
323 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
324 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
326 SCTP_TCB_UNLOCK(stcb);
327 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
328 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
330 * Here the peer is either playing tricks on us, including
331 * an address that belongs to someone who does not support
332 * SCTP OR was a userland implementation that shutdown and
333 * now is dead. In either case treat it like a OOTB abort
336 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
337 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
338 so = SCTP_INP_SO(inp);
339 atomic_add_int(&stcb->asoc.refcnt, 1);
340 SCTP_TCB_UNLOCK(stcb);
341 SCTP_SOCKET_LOCK(so, 1);
343 atomic_subtract_int(&stcb->asoc.refcnt, 1);
345 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
346 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
347 SCTP_SOCKET_UNLOCK(so, 1);
348 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
350 /* no need to unlock here, since the TCB is gone */
352 SCTP_TCB_UNLOCK(stcb);
359 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
362 struct ip6ctlparam *ip6cp = NULL;
365 vrf_id = SCTP_DEFAULT_VRFID;
367 if (pktdst->sa_family != AF_INET6 ||
368 pktdst->sa_len != sizeof(struct sockaddr_in6))
371 if ((unsigned)cmd >= PRC_NCMDS)
373 if (PRC_IS_REDIRECT(cmd)) {
375 } else if (inet6ctlerrmap[cmd] == 0) {
378 /* if the parameter is from icmp6, decode it. */
380 ip6cp = (struct ip6ctlparam *)d;
382 ip6cp = (struct ip6ctlparam *)NULL;
387 * XXX: We assume that when IPV6 is non NULL, M and OFF are
390 /* check if we can safely examine src and dst ports */
391 struct sctp_inpcb *inp = NULL;
392 struct sctp_tcb *stcb = NULL;
393 struct sctp_nets *net = NULL;
394 struct sockaddr_in6 final;
396 if (ip6cp->ip6c_m == NULL)
399 bzero(&sh, sizeof(sh));
400 bzero(&final, sizeof(final));
403 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
405 ip6cp->ip6c_src->sin6_port = sh.src_port;
406 final.sin6_len = sizeof(final);
407 final.sin6_family = AF_INET6;
408 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
409 final.sin6_port = sh.dest_port;
410 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
411 (struct sockaddr *)ip6cp->ip6c_src,
412 &inp, &net, 1, vrf_id);
413 /* inp's ref-count increased && stcb locked */
414 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
415 if (cmd == PRC_MSGSIZE) {
416 sctp6_notify_mbuf(inp,
421 /* inp's ref-count reduced && stcb unlocked */
423 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
424 (struct sockaddr *)&final,
426 /* inp's ref-count reduced && stcb unlocked */
429 if (PRC_IS_REDIRECT(cmd) && inp) {
430 in6_rtchange((struct in6pcb *)inp,
431 inet6ctlerrmap[cmd]);
434 /* reduce inp's ref-count */
436 SCTP_INP_DECR_REF(inp);
437 SCTP_INP_WUNLOCK(inp);
440 SCTP_TCB_UNLOCK(stcb);
446 * this routine can probably be collasped into the one in sctp_userreq.c
447 * since they do the same thing and now we lookup with a sockaddr
450 sctp6_getcred(SYSCTL_HANDLER_ARGS)
453 struct sockaddr_in6 addrs[2];
454 struct sctp_inpcb *inp;
455 struct sctp_nets *net;
456 struct sctp_tcb *stcb;
460 vrf_id = SCTP_DEFAULT_VRFID;
462 error = priv_check(req->td, PRIV_NETINET_GETCRED);
466 if (req->newlen != sizeof(addrs)) {
467 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
470 if (req->oldlen != sizeof(struct ucred)) {
471 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
474 error = SYSCTL_IN(req, addrs, sizeof(addrs));
478 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
480 &inp, &net, 1, vrf_id);
481 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
482 if ((inp != NULL) && (stcb == NULL)) {
483 /* reduce ref-count */
485 SCTP_INP_DECR_REF(inp);
488 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
492 SCTP_TCB_UNLOCK(stcb);
494 * We use the write lock here, only since in the error leg we need
495 * it. If we used RLOCK, then we would have to
496 * wlock/decr/unlock/rlock. Which in theory could create a hole.
497 * Better to use higher wlock.
501 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
503 SCTP_INP_WUNLOCK(inp);
506 cru2x(inp->sctp_socket->so_cred, &xuc);
507 SCTP_INP_WUNLOCK(inp);
508 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
513 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
515 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
518 /* This is the same as the sctp_abort() could be made common */
520 sctp6_abort(struct socket *so)
522 struct sctp_inpcb *inp;
525 inp = (struct sctp_inpcb *)so->so_pcb;
527 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
531 flags = inp->sctp_flags;
532 #ifdef SCTP_LOG_CLOSING
533 sctp_log_closing(inp, NULL, 17);
535 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
536 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
537 #ifdef SCTP_LOG_CLOSING
538 sctp_log_closing(inp, NULL, 16);
540 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
541 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
543 SCTP_SB_CLEAR(so->so_snd);
545 * same for the rcv ones, they are only here for the
548 SCTP_SB_CLEAR(so->so_rcv);
549 /* Now null out the reference, we are completely detached. */
553 flags = inp->sctp_flags;
554 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
555 goto sctp_must_try_again;
562 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
566 struct sctp_inpcb *inp;
567 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
569 inp = (struct sctp_inpcb *)so->so_pcb;
571 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
574 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
575 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
579 error = sctp_inpcb_alloc(so, vrf_id);
582 inp = (struct sctp_inpcb *)so->so_pcb;
584 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
585 inp6 = (struct in6pcb *)inp;
587 inp6->inp_vflag |= INP_IPV6;
588 inp6->in6p_hops = -1; /* use kernel default */
589 inp6->in6p_cksum = -1; /* just to be sure */
592 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
593 * socket as well, because the socket may be bound to an IPv6
594 * wildcard address, which may match an IPv4-mapped IPv6 address.
596 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
599 * Hmm what about the IPSEC stuff that is missing here but in
602 SCTP_INP_WUNLOCK(inp);
607 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
609 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 inp6->inp_vflag &= ~INP_IPV4;
643 inp6->inp_vflag |= INP_IPV6;
644 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
645 switch (addr->sa_family) {
648 /* binding v4 addr to v6 socket, so reset flags */
649 inp6->inp_vflag |= INP_IPV4;
650 inp6->inp_vflag &= ~INP_IPV6;
656 struct sockaddr_in6 *sin6_p;
658 sin6_p = (struct sockaddr_in6 *)addr;
660 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
661 inp6->inp_vflag |= INP_IPV4;
664 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
665 struct sockaddr_in sin;
667 in6_sin6_2_sin(&sin, sin6_p);
668 inp6->inp_vflag |= INP_IPV4;
669 inp6->inp_vflag &= ~INP_IPV6;
670 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
680 } else if (addr != NULL) {
681 struct sockaddr_in6 *sin6_p;
683 /* IPV6_V6ONLY socket */
685 if (addr->sa_family == AF_INET) {
686 /* can't bind v4 addr to v6 only socket! */
687 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
691 sin6_p = (struct sockaddr_in6 *)addr;
693 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
694 /* can't bind v4-mapped addrs either! */
695 /* NOTE: we don't support SIIT */
696 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
700 error = sctp_inpcb_bind(so, addr, NULL, p);
706 sctp6_close(struct socket *so)
711 /* This could be made common with sctp_detach() since they are identical */
715 sctp6_disconnect(struct socket *so)
717 return (sctp_disconnect(so));
722 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
723 struct mbuf *control, struct thread *p);
727 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
728 struct mbuf *control, struct thread *p)
730 struct sctp_inpcb *inp;
734 struct sockaddr_in6 *sin6;
737 /* No SPL needed since sctp_output does this */
739 inp = (struct sctp_inpcb *)so->so_pcb;
742 SCTP_RELEASE_PKT(control);
746 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
749 inp6 = (struct in6pcb *)inp;
751 * For the TCP model we may get a NULL addr, if we are a connected
754 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
761 SCTP_RELEASE_PKT(control);
764 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
765 return (EDESTADDRREQ);
768 sin6 = (struct sockaddr_in6 *)addr;
769 if (SCTP_IPV6_V6ONLY(inp6)) {
771 * if IPV6_V6ONLY flag, we discard datagrams destined to a
772 * v4 addr or v4-mapped addr
774 if (addr->sa_family == AF_INET) {
775 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
778 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
779 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
783 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
784 if (!MODULE_GLOBAL(ip6_v6only)) {
785 struct sockaddr_in sin;
787 /* convert v4-mapped into v4 addr and send */
788 in6_sin6_2_sin(&sin, sin6);
789 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
792 /* mapped addresses aren't enabled */
793 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
799 /* now what about control */
802 SCTP_PRINTF("huh? control set?\n");
803 SCTP_RELEASE_PKT(inp->control);
806 inp->control = control;
810 SCTP_BUF_NEXT(inp->pkt_last) = m;
813 inp->pkt_last = inp->pkt = m;
816 /* FreeBSD and MacOSX uses a flag passed */
817 ((flags & PRUS_MORETOCOME) == 0)
820 * note with the current version this code will only be used
821 * by OpenBSD, NetBSD and FreeBSD have methods for
822 * re-defining sosend() to use sctp_sosend(). One can
823 * optionaly switch back to this code (by changing back the
824 * defininitions but this is not advisable.
828 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
838 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
842 struct sctp_inpcb *inp;
844 struct sctp_tcb *stcb;
847 struct sockaddr_in6 *sin6;
848 struct sockaddr_storage ss;
852 inp6 = (struct in6pcb *)so->so_pcb;
853 inp = (struct sctp_inpcb *)so->so_pcb;
855 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
856 return (ECONNRESET); /* I made the same as TCP since we are
860 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
863 switch (addr->sa_family) {
866 if (addr->sa_len != sizeof(struct sockaddr_in)) {
867 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
874 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
875 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
881 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
885 vrf_id = inp->def_vrf_id;
886 SCTP_ASOC_CREATE_LOCK(inp);
888 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
889 SCTP_PCB_FLAGS_UNBOUND) {
890 /* Bind a ephemeral port */
891 SCTP_INP_RUNLOCK(inp);
892 error = sctp6_bind(so, NULL, p);
894 SCTP_ASOC_CREATE_UNLOCK(inp);
900 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
901 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
902 /* We are already connected AND the TCP model */
903 SCTP_INP_RUNLOCK(inp);
904 SCTP_ASOC_CREATE_UNLOCK(inp);
905 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
909 sin6 = (struct sockaddr_in6 *)addr;
910 if (SCTP_IPV6_V6ONLY(inp6)) {
912 * if IPV6_V6ONLY flag, ignore connections destined to a v4
913 * addr or v4-mapped addr
915 if (addr->sa_family == AF_INET) {
916 SCTP_INP_RUNLOCK(inp);
917 SCTP_ASOC_CREATE_UNLOCK(inp);
918 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
921 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
922 SCTP_INP_RUNLOCK(inp);
923 SCTP_ASOC_CREATE_UNLOCK(inp);
924 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
928 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
929 if (!MODULE_GLOBAL(ip6_v6only)) {
930 /* convert v4-mapped into v4 addr */
931 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
932 addr = (struct sockaddr *)&ss;
934 /* mapped addresses aren't enabled */
935 SCTP_INP_RUNLOCK(inp);
936 SCTP_ASOC_CREATE_UNLOCK(inp);
937 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
942 /* Now do we connect? */
943 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
944 stcb = LIST_FIRST(&inp->sctp_asoc_list);
946 SCTP_TCB_UNLOCK(stcb);
948 SCTP_INP_RUNLOCK(inp);
950 SCTP_INP_RUNLOCK(inp);
952 SCTP_INP_INCR_REF(inp);
953 SCTP_INP_WUNLOCK(inp);
954 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
957 SCTP_INP_DECR_REF(inp);
958 SCTP_INP_WUNLOCK(inp);
963 /* Already have or am bring up an association */
964 SCTP_ASOC_CREATE_UNLOCK(inp);
965 SCTP_TCB_UNLOCK(stcb);
966 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
969 /* We are GOOD to go */
970 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
971 SCTP_ASOC_CREATE_UNLOCK(inp);
976 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
977 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
978 /* Set the connected flag so we can queue data */
981 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
982 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
984 /* initialize authentication parameters for the assoc */
985 sctp_initialize_auth_params(inp, stcb);
987 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
988 SCTP_TCB_UNLOCK(stcb);
993 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
995 struct sockaddr_in6 *sin6;
996 struct sctp_inpcb *inp;
998 struct sctp_ifa *sctp_ifa;
1003 * Do the malloc first in case it blocks.
1005 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1008 sin6->sin6_family = AF_INET6;
1009 sin6->sin6_len = sizeof(*sin6);
1011 inp = (struct sctp_inpcb *)so->so_pcb;
1013 SCTP_FREE_SONAME(sin6);
1014 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1015 return (ECONNRESET);
1017 SCTP_INP_RLOCK(inp);
1018 sin6->sin6_port = inp->sctp_lport;
1019 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1020 /* For the bound all case you get back 0 */
1021 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1022 struct sctp_tcb *stcb;
1023 struct sockaddr_in6 *sin_a6;
1024 struct sctp_nets *net;
1027 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1033 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1034 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1036 /* this will make coverity happy */
1039 if (sin_a6->sin6_family == AF_INET6) {
1044 if ((!fnd) || (sin_a6 == NULL)) {
1048 vrf_id = inp->def_vrf_id;
1049 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1051 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1054 /* For the bound all case you get back 0 */
1056 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1059 /* Take the first IPv6 address in the list */
1060 struct sctp_laddr *laddr;
1063 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1064 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1065 struct sockaddr_in6 *sin_a;
1067 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1068 sin6->sin6_addr = sin_a->sin6_addr;
1074 SCTP_FREE_SONAME(sin6);
1075 SCTP_INP_RUNLOCK(inp);
1076 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1080 SCTP_INP_RUNLOCK(inp);
1081 /* Scoping things for v6 */
1082 if ((error = sa6_recoverscope(sin6)) != 0) {
1083 SCTP_FREE_SONAME(sin6);
1086 (*addr) = (struct sockaddr *)sin6;
1091 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1093 struct sockaddr_in6 *sin6;
1095 struct sockaddr_in6 *sin_a6;
1096 struct sctp_inpcb *inp;
1097 struct sctp_tcb *stcb;
1098 struct sctp_nets *net;
1101 /* Do the malloc first in case it blocks. */
1102 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1105 sin6->sin6_family = AF_INET6;
1106 sin6->sin6_len = sizeof(*sin6);
1108 inp = (struct sctp_inpcb *)so->so_pcb;
1109 if ((inp == NULL) ||
1110 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1111 /* UDP type and listeners will drop out here */
1112 SCTP_FREE_SONAME(sin6);
1113 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1116 SCTP_INP_RLOCK(inp);
1117 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1119 SCTP_TCB_LOCK(stcb);
1121 SCTP_INP_RUNLOCK(inp);
1123 SCTP_FREE_SONAME(sin6);
1124 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1125 return (ECONNRESET);
1128 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1129 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1130 if (sin_a6->sin6_family == AF_INET6) {
1132 sin6->sin6_port = stcb->rport;
1133 sin6->sin6_addr = sin_a6->sin6_addr;
1137 SCTP_TCB_UNLOCK(stcb);
1139 /* No IPv4 address */
1140 SCTP_FREE_SONAME(sin6);
1141 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1144 if ((error = sa6_recoverscope(sin6)) != 0)
1146 *addr = (struct sockaddr *)sin6;
1151 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1154 struct sockaddr *addr;
1157 struct in6pcb *inp6 = sotoin6pcb(so);
1161 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1164 /* allow v6 addresses precedence */
1165 error = sctp6_getaddr(so, nam);
1168 /* try v4 next if v6 failed */
1169 error = sctp_ingetaddr(so, nam);
1174 /* if I'm V6ONLY, convert it to v4-mapped */
1175 if (SCTP_IPV6_V6ONLY(inp6)) {
1176 struct sockaddr_in6 sin6;
1178 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1179 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1188 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1191 struct sockaddr *addr;
1194 struct in6pcb *inp6 = sotoin6pcb(so);
1198 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1201 /* allow v6 addresses precedence */
1202 error = sctp6_peeraddr(so, nam);
1205 /* try v4 next if v6 failed */
1206 error = sctp_peeraddr(so, nam);
1211 /* if I'm V6ONLY, convert it to v4-mapped */
1212 if (SCTP_IPV6_V6ONLY(inp6)) {
1213 struct sockaddr_in6 sin6;
1215 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1216 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1223 struct pr_usrreqs sctp6_usrreqs = {
1224 .pru_abort = sctp6_abort,
1225 .pru_accept = sctp_accept,
1226 .pru_attach = sctp6_attach,
1227 .pru_bind = sctp6_bind,
1228 .pru_connect = sctp6_connect,
1229 .pru_control = in6_control,
1230 .pru_close = sctp6_close,
1231 .pru_detach = sctp6_close,
1232 .pru_sopoll = sopoll_generic,
1233 .pru_flush = sctp_flush,
1234 .pru_disconnect = sctp6_disconnect,
1235 .pru_listen = sctp_listen,
1236 .pru_peeraddr = sctp6_getpeeraddr,
1237 .pru_send = sctp6_send,
1238 .pru_shutdown = sctp_shutdown,
1239 .pru_sockaddr = sctp6_in6getaddr,
1240 .pru_sosend = sctp_sosend,
1241 .pru_soreceive = sctp_soreceive