2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_pcb.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet6/sctp6_var.h>
42 #include <netinet/sctp_sysctl.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctp_asconf.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_auth.h>
50 #include <netinet/sctp_input.h>
51 #include <netinet/sctp_output.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/sctp_crc32.h>
54 #include <netinet/icmp6.h>
55 #include <netinet/udp.h>
58 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
64 struct sockaddr_in6 src, dst;
67 struct sctp_chunkhdr *ch;
75 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
76 SCTP_RELEASE_PKT(*i_pak);
77 return (IPPROTO_DONE);
79 m = SCTP_HEADER_TO_CHAIN(*i_pak);
80 #ifdef SCTP_MBUF_LOGGING
81 /* Log in any input mbufs */
82 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
83 sctp_log_mbc(m, SCTP_MBUF_INPUT);
86 #ifdef SCTP_PACKET_LOGGING
87 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
91 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
92 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
94 if_name(m->m_pkthdr.rcvif),
95 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
96 mflowid = m->m_pkthdr.flowid;
97 mflowtype = M_HASHTYPE_GET(m);
99 SCTP_STAT_INCR(sctps_recvpackets);
100 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
101 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
102 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
103 if (m->m_len < offset) {
104 m = m_pullup(m, offset);
106 SCTP_STAT_INCR(sctps_hdrops);
107 return (IPPROTO_DONE);
110 ip6 = mtod(m, struct ip6_hdr *);
111 sh = (struct sctphdr *)(mtod(m, caddr_t)+iphlen);
112 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
113 offset -= sizeof(struct sctp_chunkhdr);
114 memset(&src, 0, sizeof(struct sockaddr_in6));
115 src.sin6_family = AF_INET6;
116 src.sin6_len = sizeof(struct sockaddr_in6);
117 src.sin6_port = sh->src_port;
118 src.sin6_addr = ip6->ip6_src;
119 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
122 memset(&dst, 0, sizeof(struct sockaddr_in6));
123 dst.sin6_family = AF_INET6;
124 dst.sin6_len = sizeof(struct sockaddr_in6);
125 dst.sin6_port = sh->dest_port;
126 dst.sin6_addr = ip6->ip6_dst;
127 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
130 length = ntohs(ip6->ip6_plen) + iphlen;
131 /* Validate mbuf chain length with IP payload length. */
132 if (SCTP_HEADER_LEN(m) != length) {
133 SCTPDBG(SCTP_DEBUG_INPUT1,
134 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
135 SCTP_STAT_INCR(sctps_hdrops);
138 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
141 ecn_bits = IPV6_TRAFFIC_CLASS(ip6);
142 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
143 SCTP_STAT_INCR(sctps_recvhwcrc);
146 SCTP_STAT_INCR(sctps_recvswcrc);
149 sctp_common_input_processing(&m, iphlen, offset, length,
150 (struct sockaddr *)&src,
151 (struct sockaddr *)&dst,
155 mflowtype, mflowid, fibnum,
161 return (IPPROTO_DONE);
165 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
167 return (sctp6_input_with_port(i_pak, offp, 0));
171 sctp6_notify(struct sctp_inpcb *inp,
172 struct sctp_tcb *stcb,
173 struct sctp_nets *net,
180 switch (icmp6_type) {
181 case ICMP6_DST_UNREACH:
182 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
183 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
184 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
185 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
186 /* Mark the net unreachable. */
187 if (net->dest_state & SCTP_ADDR_REACHABLE) {
188 /* Ok that destination is not reachable */
189 net->dest_state &= ~SCTP_ADDR_REACHABLE;
190 net->dest_state &= ~SCTP_ADDR_PF;
191 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
192 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
195 SCTP_TCB_UNLOCK(stcb);
197 case ICMP6_PARAM_PROB:
198 /* Treat it like an ABORT. */
199 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
200 sctp_abort_notification(stcb, true, false, 0, NULL, SCTP_SO_NOT_LOCKED);
201 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
202 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
204 SCTP_TCB_UNLOCK(stcb);
207 case ICMP6_PACKET_TOO_BIG:
208 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
209 SCTP_TCB_UNLOCK(stcb);
212 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
214 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
215 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
219 /* Update the path MTU. */
221 next_mtu -= sizeof(struct udphdr);
223 if (net->mtu > next_mtu) {
226 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
228 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
231 /* Update the association MTU */
232 if (stcb->asoc.smallest_mtu > next_mtu) {
233 sctp_pathmtu_adjustment(stcb, next_mtu, true);
235 /* Finally, start the PMTU timer if it was running before. */
237 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
239 SCTP_TCB_UNLOCK(stcb);
242 SCTP_TCB_UNLOCK(stcb);
248 sctp6_ctlinput(struct ip6ctlparam *ip6cp)
250 struct sctp_inpcb *inp;
251 struct sctp_tcb *stcb;
252 struct sctp_nets *net;
254 struct sockaddr_in6 src, dst;
256 if (icmp6_errmap(ip6cp->ip6c_icmp6) == 0) {
261 * Check if we can safely examine the ports and the verification tag
262 * of the SCTP common header.
264 if (ip6cp->ip6c_m->m_pkthdr.len <
265 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
269 /* Copy out the port numbers and the verification tag. */
270 memset(&sh, 0, sizeof(sh));
271 m_copydata(ip6cp->ip6c_m,
273 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
275 memset(&src, 0, sizeof(struct sockaddr_in6));
276 src.sin6_family = AF_INET6;
277 src.sin6_len = sizeof(struct sockaddr_in6);
278 src.sin6_port = sh.src_port;
279 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
280 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
283 memset(&dst, 0, sizeof(struct sockaddr_in6));
284 dst.sin6_family = AF_INET6;
285 dst.sin6_len = sizeof(struct sockaddr_in6);
286 dst.sin6_port = sh.dest_port;
287 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
288 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
293 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
294 (struct sockaddr *)&src,
295 &inp, &net, 1, SCTP_DEFAULT_VRFID);
296 if ((stcb != NULL) &&
299 /* Check the verification tag */
300 if (ntohl(sh.v_tag) != 0) {
302 * This must be the verification tag used for
303 * sending out packets. We don't consider packets
304 * reflecting the verification tag.
306 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
307 SCTP_TCB_UNLOCK(stcb);
311 if (ip6cp->ip6c_m->m_pkthdr.len >=
312 ip6cp->ip6c_off + sizeof(struct sctphdr) +
313 sizeof(struct sctp_chunkhdr) +
314 offsetof(struct sctp_init, a_rwnd)) {
316 * In this case we can check if we got an
317 * INIT chunk and if the initiate tag
320 uint32_t initiate_tag;
323 m_copydata(ip6cp->ip6c_m,
325 sizeof(struct sctphdr),
327 (caddr_t)&chunk_type);
328 m_copydata(ip6cp->ip6c_m,
330 sizeof(struct sctphdr) +
331 sizeof(struct sctp_chunkhdr),
333 (caddr_t)&initiate_tag);
334 if ((chunk_type != SCTP_INITIATION) ||
335 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
336 SCTP_TCB_UNLOCK(stcb);
340 SCTP_TCB_UNLOCK(stcb);
344 sctp6_notify(inp, stcb, net,
345 ip6cp->ip6c_icmp6->icmp6_type,
346 ip6cp->ip6c_icmp6->icmp6_code,
347 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
349 if ((stcb == NULL) && (inp != NULL)) {
350 /* reduce inp's ref-count */
352 SCTP_INP_DECR_REF(inp);
353 SCTP_INP_WUNLOCK(inp);
356 SCTP_TCB_UNLOCK(stcb);
362 * this routine can probably be collasped into the one in sctp_userreq.c
363 * since they do the same thing and now we lookup with a sockaddr
366 sctp6_getcred(SYSCTL_HANDLER_ARGS)
369 struct sockaddr_in6 addrs[2];
370 struct sctp_inpcb *inp;
371 struct sctp_nets *net;
372 struct sctp_tcb *stcb;
376 vrf_id = SCTP_DEFAULT_VRFID;
378 error = priv_check(req->td, PRIV_NETINET_GETCRED);
382 if (req->newlen != sizeof(addrs)) {
383 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
386 if (req->oldlen != sizeof(struct ucred)) {
387 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
390 error = SYSCTL_IN(req, addrs, sizeof(addrs));
394 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
396 &inp, &net, 1, vrf_id);
397 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
398 if ((inp != NULL) && (stcb == NULL)) {
399 /* reduce ref-count */
401 SCTP_INP_DECR_REF(inp);
404 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
408 SCTP_TCB_UNLOCK(stcb);
410 * We use the write lock here, only since in the error leg we need
411 * it. If we used RLOCK, then we would have to
412 * wlock/decr/unlock/rlock. Which in theory could create a hole.
413 * Better to use higher wlock.
417 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
419 SCTP_INP_WUNLOCK(inp);
422 cru2x(inp->sctp_socket->so_cred, &xuc);
423 SCTP_INP_WUNLOCK(inp);
424 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
429 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
430 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
431 0, 0, sctp6_getcred, "S,ucred",
432 "Get the ucred of a SCTP6 connection");
435 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
438 struct sctp_inpcb *inp;
439 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
441 inp = (struct sctp_inpcb *)so->so_pcb;
443 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
447 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
448 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
452 error = sctp_inpcb_alloc(so, vrf_id);
455 inp = (struct sctp_inpcb *)so->so_pcb;
457 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
459 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
460 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
461 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
464 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
465 * socket as well, because the socket may be bound to an IPv6
466 * wildcard address, which may match an IPv4-mapped IPv6 address.
468 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
470 SCTP_INP_WUNLOCK(inp);
475 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
477 struct sctp_inpcb *inp;
481 inp = (struct sctp_inpcb *)so->so_pcb;
483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
488 switch (addr->sa_family) {
491 if (addr->sa_len != sizeof(struct sockaddr_in)) {
492 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
499 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
500 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
506 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
510 vflagsav = inp->ip_inp.inp.inp_vflag;
511 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
512 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
513 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
514 switch (addr->sa_family) {
517 /* binding v4 addr to v6 socket, so reset flags */
518 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
519 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
525 struct sockaddr_in6 *sin6_p;
527 sin6_p = (struct sockaddr_in6 *)addr;
529 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
530 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
533 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
534 struct sockaddr_in sin;
536 in6_sin6_2_sin(&sin, sin6_p);
537 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
538 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
539 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
549 } else if (addr != NULL) {
550 struct sockaddr_in6 *sin6_p;
552 /* IPV6_V6ONLY socket */
554 if (addr->sa_family == AF_INET) {
555 /* can't bind v4 addr to v6 only socket! */
556 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
561 sin6_p = (struct sockaddr_in6 *)addr;
563 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
564 /* can't bind v4-mapped addrs either! */
565 /* NOTE: we don't support SIIT */
566 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
571 error = sctp_inpcb_bind(so, addr, NULL, p);
574 inp->ip_inp.inp.inp_vflag = vflagsav;
579 sctp6_close(struct socket *so)
584 /* This could be made common with sctp_detach() since they are identical */
587 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
588 struct mbuf *control, struct thread *p);
591 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
592 struct mbuf *control, struct thread *p)
594 struct sctp_inpcb *inp;
597 struct sockaddr_in6 *sin6;
599 /* No SPL needed since sctp_output does this */
601 inp = (struct sctp_inpcb *)so->so_pcb;
604 SCTP_RELEASE_PKT(control);
608 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
612 * For the TCP model we may get a NULL addr, if we are a connected
615 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
622 SCTP_RELEASE_PKT(control);
625 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
626 return (EDESTADDRREQ);
628 switch (addr->sa_family) {
631 if (addr->sa_len != sizeof(struct sockaddr_in)) {
633 SCTP_RELEASE_PKT(control);
637 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
644 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
646 SCTP_RELEASE_PKT(control);
650 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
657 SCTP_RELEASE_PKT(control);
661 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
665 sin6 = (struct sockaddr_in6 *)addr;
666 if (SCTP_IPV6_V6ONLY(inp)) {
668 * if IPV6_V6ONLY flag, we discard datagrams destined to a
669 * v4 addr or v4-mapped addr
671 if (addr->sa_family == AF_INET) {
673 SCTP_RELEASE_PKT(control);
677 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
680 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
682 SCTP_RELEASE_PKT(control);
686 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
690 if ((addr->sa_family == AF_INET6) &&
691 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
692 struct sockaddr_in sin;
694 /* convert v4-mapped into v4 addr and send */
695 in6_sin6_2_sin(&sin, sin6);
696 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
700 /* now what about control */
703 SCTP_PRINTF("huh? control set?\n");
704 SCTP_RELEASE_PKT(inp->control);
707 inp->control = control;
711 SCTP_BUF_NEXT(inp->pkt_last) = m;
714 inp->pkt_last = inp->pkt = m;
717 /* FreeBSD and MacOSX uses a flag passed */
718 ((flags & PRUS_MORETOCOME) == 0)
721 * note with the current version this code will only be used
722 * by OpenBSD, NetBSD and FreeBSD have methods for
723 * re-defining sosend() to use sctp_sosend(). One can
724 * optionaly switch back to this code (by changing back the
725 * defininitions but this is not advisable.
727 struct epoch_tracker et;
731 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
742 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
744 struct epoch_tracker et;
747 struct sctp_inpcb *inp;
748 struct sctp_tcb *stcb;
750 struct sockaddr_in6 *sin6;
751 union sctp_sockstore store;
754 inp = (struct sctp_inpcb *)so->so_pcb;
756 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
757 return (ECONNRESET); /* I made the same as TCP since we are
761 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
764 switch (addr->sa_family) {
767 if (addr->sa_len != sizeof(struct sockaddr_in)) {
768 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
775 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
782 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
786 vrf_id = inp->def_vrf_id;
787 SCTP_ASOC_CREATE_LOCK(inp);
789 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
790 SCTP_PCB_FLAGS_UNBOUND) {
791 /* Bind a ephemeral port */
792 SCTP_INP_RUNLOCK(inp);
793 error = sctp6_bind(so, NULL, p);
795 SCTP_ASOC_CREATE_UNLOCK(inp);
801 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
802 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
803 /* We are already connected AND the TCP model */
804 SCTP_INP_RUNLOCK(inp);
805 SCTP_ASOC_CREATE_UNLOCK(inp);
806 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
810 sin6 = (struct sockaddr_in6 *)addr;
811 if (SCTP_IPV6_V6ONLY(inp)) {
813 * if IPV6_V6ONLY flag, ignore connections destined to a v4
814 * addr or v4-mapped addr
816 if (addr->sa_family == AF_INET) {
817 SCTP_INP_RUNLOCK(inp);
818 SCTP_ASOC_CREATE_UNLOCK(inp);
819 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
822 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
823 SCTP_INP_RUNLOCK(inp);
824 SCTP_ASOC_CREATE_UNLOCK(inp);
825 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
829 if ((addr->sa_family == AF_INET6) &&
830 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
831 /* convert v4-mapped into v4 addr */
832 in6_sin6_2_sin(&store.sin, sin6);
836 /* Now do we connect? */
837 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
838 stcb = LIST_FIRST(&inp->sctp_asoc_list);
842 SCTP_INP_RUNLOCK(inp);
844 SCTP_INP_RUNLOCK(inp);
846 SCTP_INP_INCR_REF(inp);
847 SCTP_INP_WUNLOCK(inp);
848 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
851 SCTP_INP_DECR_REF(inp);
852 SCTP_INP_WUNLOCK(inp);
857 /* Already have or am bring up an association */
858 SCTP_ASOC_CREATE_UNLOCK(inp);
859 SCTP_TCB_UNLOCK(stcb);
860 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
863 /* We are GOOD to go */
864 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
865 inp->sctp_ep.pre_open_stream_count,
866 inp->sctp_ep.port, p,
867 SCTP_INITIALIZE_AUTH_PARAMS);
868 SCTP_ASOC_CREATE_UNLOCK(inp);
873 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
874 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
876 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
877 SCTP_TCB_UNLOCK(stcb);
883 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
885 struct sockaddr_in6 *sin6;
886 struct sctp_inpcb *inp;
888 struct sctp_ifa *sctp_ifa;
893 * Do the malloc first in case it blocks.
895 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
898 sin6->sin6_family = AF_INET6;
899 sin6->sin6_len = sizeof(*sin6);
901 inp = (struct sctp_inpcb *)so->so_pcb;
903 SCTP_FREE_SONAME(sin6);
904 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
908 sin6->sin6_port = inp->sctp_lport;
909 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
910 /* For the bound all case you get back 0 */
911 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
912 struct sctp_tcb *stcb;
913 struct sockaddr_in6 *sin_a6;
914 struct sctp_nets *net;
917 stcb = LIST_FIRST(&inp->sctp_asoc_list);
919 SCTP_INP_RUNLOCK(inp);
920 SCTP_FREE_SONAME(sin6);
921 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
926 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
927 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
929 /* this will make coverity happy */
932 if (sin_a6->sin6_family == AF_INET6) {
937 if ((!fnd) || (sin_a6 == NULL)) {
939 SCTP_INP_RUNLOCK(inp);
940 SCTP_FREE_SONAME(sin6);
941 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
944 vrf_id = inp->def_vrf_id;
945 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
947 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
950 /* For the bound all case you get back 0 */
951 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
954 /* Take the first IPv6 address in the list */
955 struct sctp_laddr *laddr;
958 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
959 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
960 struct sockaddr_in6 *sin_a;
962 sin_a = &laddr->ifa->address.sin6;
963 sin6->sin6_addr = sin_a->sin6_addr;
969 SCTP_FREE_SONAME(sin6);
970 SCTP_INP_RUNLOCK(inp);
971 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
975 SCTP_INP_RUNLOCK(inp);
976 /* Scoping things for v6 */
977 if ((error = sa6_recoverscope(sin6)) != 0) {
978 SCTP_FREE_SONAME(sin6);
981 (*addr) = (struct sockaddr *)sin6;
986 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
988 struct sockaddr_in6 *sin6;
990 struct sockaddr_in6 *sin_a6;
991 struct sctp_inpcb *inp;
992 struct sctp_tcb *stcb;
993 struct sctp_nets *net;
996 /* Do the malloc first in case it blocks. */
997 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1000 sin6->sin6_family = AF_INET6;
1001 sin6->sin6_len = sizeof(*sin6);
1003 inp = (struct sctp_inpcb *)so->so_pcb;
1004 if ((inp == NULL) ||
1005 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1006 /* UDP type and listeners will drop out here */
1007 SCTP_FREE_SONAME(sin6);
1008 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1011 SCTP_INP_RLOCK(inp);
1012 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1014 SCTP_TCB_LOCK(stcb);
1016 SCTP_INP_RUNLOCK(inp);
1018 SCTP_FREE_SONAME(sin6);
1019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1020 return (ECONNRESET);
1023 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1024 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1025 if (sin_a6->sin6_family == AF_INET6) {
1027 sin6->sin6_port = stcb->rport;
1028 sin6->sin6_addr = sin_a6->sin6_addr;
1032 SCTP_TCB_UNLOCK(stcb);
1034 /* No IPv4 address */
1035 SCTP_FREE_SONAME(sin6);
1036 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1039 if ((error = sa6_recoverscope(sin6)) != 0) {
1040 SCTP_FREE_SONAME(sin6);
1041 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1044 *addr = (struct sockaddr *)sin6;
1049 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1051 struct inpcb *inp = sotoinpcb(so);
1055 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1059 /* allow v6 addresses precedence */
1060 error = sctp6_getaddr(so, nam);
1063 struct sockaddr_in6 *sin6;
1065 /* try v4 next if v6 failed */
1066 error = sctp_ingetaddr(so, nam);
1070 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1072 SCTP_FREE_SONAME(*nam);
1075 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1076 SCTP_FREE_SONAME(*nam);
1077 *nam = (struct sockaddr *)sin6;
1084 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1086 struct inpcb *inp = sotoinpcb(so);
1090 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1094 /* allow v6 addresses precedence */
1095 error = sctp6_peeraddr(so, nam);
1098 struct sockaddr_in6 *sin6;
1100 /* try v4 next if v6 failed */
1101 error = sctp_peeraddr(so, nam);
1105 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1107 SCTP_FREE_SONAME(*nam);
1110 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1111 SCTP_FREE_SONAME(*nam);
1112 *nam = (struct sockaddr *)sin6;
1118 #define SCTP6_PROTOSW \
1119 .pr_protocol = IPPROTO_SCTP, \
1120 .pr_ctloutput = sctp_ctloutput, \
1121 .pr_abort = sctp_abort, \
1122 .pr_accept = sctp_accept, \
1123 .pr_attach = sctp6_attach, \
1124 .pr_bind = sctp6_bind, \
1125 .pr_connect = sctp6_connect, \
1126 .pr_control = in6_control, \
1127 .pr_close = sctp6_close, \
1128 .pr_detach = sctp6_close, \
1129 .pr_sopoll = sopoll_generic, \
1130 .pr_flush = sctp_flush, \
1131 .pr_disconnect = sctp_disconnect, \
1132 .pr_listen = sctp_listen, \
1133 .pr_peeraddr = sctp6_getpeeraddr, \
1134 .pr_send = sctp6_send, \
1135 .pr_shutdown = sctp_shutdown, \
1136 .pr_sockaddr = sctp6_in6getaddr, \
1137 .pr_sosend = sctp_sosend, \
1138 .pr_soreceive = sctp_soreceive
1140 struct protosw sctp6_seqpacket_protosw = {
1141 .pr_type = SOCK_SEQPACKET,
1142 .pr_flags = PR_WANTRCVD,
1146 struct protosw sctp6_stream_protosw = {
1147 .pr_type = SOCK_STREAM,
1148 .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD,