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 <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_var.h>
44 #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/icmp6.h>
58 #include <netinet/udp.h>
61 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
67 struct sockaddr_in6 src, dst;
70 struct sctp_chunkhdr *ch;
78 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
79 SCTP_RELEASE_PKT(*i_pak);
80 return (IPPROTO_DONE);
82 m = SCTP_HEADER_TO_CHAIN(*i_pak);
83 #ifdef SCTP_MBUF_LOGGING
84 /* Log in any input mbufs */
85 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
86 sctp_log_mbc(m, SCTP_MBUF_INPUT);
89 #ifdef SCTP_PACKET_LOGGING
90 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
94 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
95 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
97 if_name(m->m_pkthdr.rcvif),
98 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
99 mflowid = m->m_pkthdr.flowid;
100 mflowtype = M_HASHTYPE_GET(m);
101 fibnum = M_GETFIB(m);
102 SCTP_STAT_INCR(sctps_recvpackets);
103 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
104 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
105 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
106 if (m->m_len < offset) {
107 m = m_pullup(m, offset);
109 SCTP_STAT_INCR(sctps_hdrops);
110 return (IPPROTO_DONE);
113 ip6 = mtod(m, struct ip6_hdr *);
114 sh = (struct sctphdr *)(mtod(m, caddr_t)+iphlen);
115 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
116 offset -= sizeof(struct sctp_chunkhdr);
117 memset(&src, 0, sizeof(struct sockaddr_in6));
118 src.sin6_family = AF_INET6;
119 src.sin6_len = sizeof(struct sockaddr_in6);
120 src.sin6_port = sh->src_port;
121 src.sin6_addr = ip6->ip6_src;
122 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
125 memset(&dst, 0, sizeof(struct sockaddr_in6));
126 dst.sin6_family = AF_INET6;
127 dst.sin6_len = sizeof(struct sockaddr_in6);
128 dst.sin6_port = sh->dest_port;
129 dst.sin6_addr = ip6->ip6_dst;
130 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
133 length = ntohs(ip6->ip6_plen) + iphlen;
134 /* Validate mbuf chain length with IP payload length. */
135 if (SCTP_HEADER_LEN(m) != length) {
136 SCTPDBG(SCTP_DEBUG_INPUT1,
137 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
138 SCTP_STAT_INCR(sctps_hdrops);
141 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
144 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
145 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
146 SCTP_STAT_INCR(sctps_recvhwcrc);
149 SCTP_STAT_INCR(sctps_recvswcrc);
152 sctp_common_input_processing(&m, iphlen, offset, length,
153 (struct sockaddr *)&src,
154 (struct sockaddr *)&dst,
158 mflowtype, mflowid, fibnum,
164 return (IPPROTO_DONE);
169 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
171 return (sctp6_input_with_port(i_pak, offp, 0));
175 sctp6_notify(struct sctp_inpcb *inp,
176 struct sctp_tcb *stcb,
177 struct sctp_nets *net,
182 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
187 switch (icmp6_type) {
188 case ICMP6_DST_UNREACH:
189 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
190 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
191 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
192 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
193 /* Mark the net unreachable. */
194 if (net->dest_state & SCTP_ADDR_REACHABLE) {
195 /* Ok that destination is not reachable */
196 net->dest_state &= ~SCTP_ADDR_REACHABLE;
197 net->dest_state &= ~SCTP_ADDR_PF;
198 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
199 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
202 SCTP_TCB_UNLOCK(stcb);
204 case ICMP6_PARAM_PROB:
205 /* Treat it like an ABORT. */
206 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
207 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
208 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
209 so = SCTP_INP_SO(inp);
210 atomic_add_int(&stcb->asoc.refcnt, 1);
211 SCTP_TCB_UNLOCK(stcb);
212 SCTP_SOCKET_LOCK(so, 1);
214 atomic_subtract_int(&stcb->asoc.refcnt, 1);
216 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
217 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
218 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
219 SCTP_SOCKET_UNLOCK(so, 1);
222 SCTP_TCB_UNLOCK(stcb);
225 case ICMP6_PACKET_TOO_BIG:
226 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
227 SCTP_TCB_UNLOCK(stcb);
230 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
232 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
233 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
237 /* Update the path MTU. */
239 next_mtu -= sizeof(struct udphdr);
241 if (net->mtu > next_mtu) {
244 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
246 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
249 /* Update the association MTU */
250 if (stcb->asoc.smallest_mtu > next_mtu) {
251 sctp_pathmtu_adjustment(stcb, next_mtu);
253 /* Finally, start the PMTU timer if it was running before. */
255 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
257 SCTP_TCB_UNLOCK(stcb);
260 SCTP_TCB_UNLOCK(stcb);
266 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
268 struct ip6ctlparam *ip6cp;
269 struct sctp_inpcb *inp;
270 struct sctp_tcb *stcb;
271 struct sctp_nets *net;
273 struct sockaddr_in6 src, dst;
275 if (pktdst->sa_family != AF_INET6 ||
276 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
280 if ((unsigned)cmd >= PRC_NCMDS) {
283 if (PRC_IS_REDIRECT(cmd)) {
285 } else if (inet6ctlerrmap[cmd] == 0) {
288 /* If the parameter is from icmp6, decode it. */
290 ip6cp = (struct ip6ctlparam *)d;
292 ip6cp = (struct ip6ctlparam *)NULL;
297 * XXX: We assume that when IPV6 is non NULL, M and OFF are
300 if (ip6cp->ip6c_m == NULL) {
305 * Check if we can safely examine the ports and the
306 * verification tag of the SCTP common header.
308 if (ip6cp->ip6c_m->m_pkthdr.len <
309 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
313 /* Copy out the port numbers and the verification tag. */
314 memset(&sh, 0, sizeof(sh));
315 m_copydata(ip6cp->ip6c_m,
317 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
319 memset(&src, 0, sizeof(struct sockaddr_in6));
320 src.sin6_family = AF_INET6;
321 src.sin6_len = sizeof(struct sockaddr_in6);
322 src.sin6_port = sh.src_port;
323 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
324 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
327 memset(&dst, 0, sizeof(struct sockaddr_in6));
328 dst.sin6_family = AF_INET6;
329 dst.sin6_len = sizeof(struct sockaddr_in6);
330 dst.sin6_port = sh.dest_port;
331 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
332 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
337 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
338 (struct sockaddr *)&src,
339 &inp, &net, 1, SCTP_DEFAULT_VRFID);
340 if ((stcb != NULL) &&
343 /* Check the verification tag */
344 if (ntohl(sh.v_tag) != 0) {
346 * This must be the verification tag used
347 * for sending out packets. We don't
348 * consider packets reflecting the
351 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
352 SCTP_TCB_UNLOCK(stcb);
356 if (ip6cp->ip6c_m->m_pkthdr.len >=
357 ip6cp->ip6c_off + sizeof(struct sctphdr) +
358 sizeof(struct sctp_chunkhdr) +
359 offsetof(struct sctp_init, a_rwnd)) {
361 * In this case we can check if we
362 * got an INIT chunk and if the
363 * initiate tag matches.
365 uint32_t initiate_tag;
368 m_copydata(ip6cp->ip6c_m,
370 sizeof(struct sctphdr),
372 (caddr_t)&chunk_type);
373 m_copydata(ip6cp->ip6c_m,
375 sizeof(struct sctphdr) +
376 sizeof(struct sctp_chunkhdr),
378 (caddr_t)&initiate_tag);
379 if ((chunk_type != SCTP_INITIATION) ||
380 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
381 SCTP_TCB_UNLOCK(stcb);
385 SCTP_TCB_UNLOCK(stcb);
389 sctp6_notify(inp, stcb, net,
390 ip6cp->ip6c_icmp6->icmp6_type,
391 ip6cp->ip6c_icmp6->icmp6_code,
392 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
394 if ((stcb == NULL) && (inp != NULL)) {
395 /* reduce inp's ref-count */
397 SCTP_INP_DECR_REF(inp);
398 SCTP_INP_WUNLOCK(inp);
401 SCTP_TCB_UNLOCK(stcb);
408 * this routine can probably be collasped into the one in sctp_userreq.c
409 * since they do the same thing and now we lookup with a sockaddr
412 sctp6_getcred(SYSCTL_HANDLER_ARGS)
415 struct sockaddr_in6 addrs[2];
416 struct sctp_inpcb *inp;
417 struct sctp_nets *net;
418 struct sctp_tcb *stcb;
422 vrf_id = SCTP_DEFAULT_VRFID;
424 error = priv_check(req->td, PRIV_NETINET_GETCRED);
428 if (req->newlen != sizeof(addrs)) {
429 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
432 if (req->oldlen != sizeof(struct ucred)) {
433 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
436 error = SYSCTL_IN(req, addrs, sizeof(addrs));
440 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
442 &inp, &net, 1, vrf_id);
443 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
444 if ((inp != NULL) && (stcb == NULL)) {
445 /* reduce ref-count */
447 SCTP_INP_DECR_REF(inp);
450 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
454 SCTP_TCB_UNLOCK(stcb);
456 * We use the write lock here, only since in the error leg we need
457 * it. If we used RLOCK, then we would have to
458 * wlock/decr/unlock/rlock. Which in theory could create a hole.
459 * Better to use higher wlock.
463 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
465 SCTP_INP_WUNLOCK(inp);
468 cru2x(inp->sctp_socket->so_cred, &xuc);
469 SCTP_INP_WUNLOCK(inp);
470 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
475 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
477 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
480 /* This is the same as the sctp_abort() could be made common */
482 sctp6_abort(struct socket *so)
484 struct epoch_tracker et;
485 struct sctp_inpcb *inp;
488 inp = (struct sctp_inpcb *)so->so_pcb;
490 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
495 flags = inp->sctp_flags;
496 #ifdef SCTP_LOG_CLOSING
497 sctp_log_closing(inp, NULL, 17);
499 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
500 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
501 #ifdef SCTP_LOG_CLOSING
502 sctp_log_closing(inp, NULL, 16);
504 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
505 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
507 SCTP_SB_CLEAR(so->so_snd);
509 * same for the rcv ones, they are only here for the
512 SCTP_SB_CLEAR(so->so_rcv);
513 /* Now null out the reference, we are completely detached. */
517 flags = inp->sctp_flags;
518 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
519 goto sctp_must_try_again;
527 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
530 struct sctp_inpcb *inp;
531 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
533 inp = (struct sctp_inpcb *)so->so_pcb;
535 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
539 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
540 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
544 error = sctp_inpcb_alloc(so, vrf_id);
547 inp = (struct sctp_inpcb *)so->so_pcb;
549 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
551 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
552 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
553 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
556 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
557 * socket as well, because the socket may be bound to an IPv6
558 * wildcard address, which may match an IPv4-mapped IPv6 address.
560 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
562 SCTP_INP_WUNLOCK(inp);
567 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
569 struct sctp_inpcb *inp;
573 inp = (struct sctp_inpcb *)so->so_pcb;
575 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
580 switch (addr->sa_family) {
583 if (addr->sa_len != sizeof(struct sockaddr_in)) {
584 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
591 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
602 vflagsav = inp->ip_inp.inp.inp_vflag;
603 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
604 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
605 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
606 switch (addr->sa_family) {
609 /* binding v4 addr to v6 socket, so reset flags */
610 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
611 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
617 struct sockaddr_in6 *sin6_p;
619 sin6_p = (struct sockaddr_in6 *)addr;
621 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
622 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
625 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
626 struct sockaddr_in sin;
628 in6_sin6_2_sin(&sin, sin6_p);
629 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
630 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
631 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
641 } else if (addr != NULL) {
642 struct sockaddr_in6 *sin6_p;
644 /* IPV6_V6ONLY socket */
646 if (addr->sa_family == AF_INET) {
647 /* can't bind v4 addr to v6 only socket! */
648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
653 sin6_p = (struct sockaddr_in6 *)addr;
655 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
656 /* can't bind v4-mapped addrs either! */
657 /* NOTE: we don't support SIIT */
658 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
663 error = sctp_inpcb_bind(so, addr, NULL, p);
666 inp->ip_inp.inp.inp_vflag = vflagsav;
672 sctp6_close(struct socket *so)
677 /* This could be made common with sctp_detach() since they are identical */
681 sctp6_disconnect(struct socket *so)
683 return (sctp_disconnect(so));
688 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
689 struct mbuf *control, struct thread *p);
693 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
694 struct mbuf *control, struct thread *p)
696 struct sctp_inpcb *inp;
699 struct sockaddr_in6 *sin6;
701 /* No SPL needed since sctp_output does this */
703 inp = (struct sctp_inpcb *)so->so_pcb;
706 SCTP_RELEASE_PKT(control);
710 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
714 * For the TCP model we may get a NULL addr, if we are a connected
717 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
724 SCTP_RELEASE_PKT(control);
727 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
728 return (EDESTADDRREQ);
731 sin6 = (struct sockaddr_in6 *)addr;
732 if (SCTP_IPV6_V6ONLY(inp)) {
734 * if IPV6_V6ONLY flag, we discard datagrams destined to a
735 * v4 addr or v4-mapped addr
737 if (addr->sa_family == AF_INET) {
738 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
741 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
746 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
747 struct sockaddr_in sin;
749 /* convert v4-mapped into v4 addr and send */
750 in6_sin6_2_sin(&sin, sin6);
751 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
755 /* now what about control */
758 SCTP_PRINTF("huh? control set?\n");
759 SCTP_RELEASE_PKT(inp->control);
762 inp->control = control;
766 SCTP_BUF_NEXT(inp->pkt_last) = m;
769 inp->pkt_last = inp->pkt = m;
772 /* FreeBSD and MacOSX uses a flag passed */
773 ((flags & PRUS_MORETOCOME) == 0)
776 * note with the current version this code will only be used
777 * by OpenBSD, NetBSD and FreeBSD have methods for
778 * re-defining sosend() to use sctp_sosend(). One can
779 * optionaly switch back to this code (by changing back the
780 * defininitions but this is not advisable.
782 struct epoch_tracker et;
786 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
797 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
799 struct epoch_tracker et;
802 struct sctp_inpcb *inp;
803 struct sctp_tcb *stcb;
805 struct sockaddr_in6 *sin6;
806 union sctp_sockstore store;
809 inp = (struct sctp_inpcb *)so->so_pcb;
811 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
812 return (ECONNRESET); /* I made the same as TCP since we are
816 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
819 switch (addr->sa_family) {
822 if (addr->sa_len != sizeof(struct sockaddr_in)) {
823 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
830 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
831 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
837 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
841 vrf_id = inp->def_vrf_id;
842 SCTP_ASOC_CREATE_LOCK(inp);
844 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
845 SCTP_PCB_FLAGS_UNBOUND) {
846 /* Bind a ephemeral port */
847 SCTP_INP_RUNLOCK(inp);
848 error = sctp6_bind(so, NULL, p);
850 SCTP_ASOC_CREATE_UNLOCK(inp);
856 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
857 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
858 /* We are already connected AND the TCP model */
859 SCTP_INP_RUNLOCK(inp);
860 SCTP_ASOC_CREATE_UNLOCK(inp);
861 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
865 sin6 = (struct sockaddr_in6 *)addr;
866 if (SCTP_IPV6_V6ONLY(inp)) {
868 * if IPV6_V6ONLY flag, ignore connections destined to a v4
869 * addr or v4-mapped addr
871 if (addr->sa_family == AF_INET) {
872 SCTP_INP_RUNLOCK(inp);
873 SCTP_ASOC_CREATE_UNLOCK(inp);
874 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
877 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
878 SCTP_INP_RUNLOCK(inp);
879 SCTP_ASOC_CREATE_UNLOCK(inp);
880 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
884 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
885 /* convert v4-mapped into v4 addr */
886 in6_sin6_2_sin(&store.sin, sin6);
890 /* Now do we connect? */
891 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
892 stcb = LIST_FIRST(&inp->sctp_asoc_list);
896 SCTP_INP_RUNLOCK(inp);
898 SCTP_INP_RUNLOCK(inp);
900 SCTP_INP_INCR_REF(inp);
901 SCTP_INP_WUNLOCK(inp);
902 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
905 SCTP_INP_DECR_REF(inp);
906 SCTP_INP_WUNLOCK(inp);
911 /* Already have or am bring up an association */
912 SCTP_ASOC_CREATE_UNLOCK(inp);
913 SCTP_TCB_UNLOCK(stcb);
914 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
917 /* We are GOOD to go */
918 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
919 inp->sctp_ep.pre_open_stream_count,
920 inp->sctp_ep.port, p,
921 SCTP_INITIALIZE_AUTH_PARAMS);
922 SCTP_ASOC_CREATE_UNLOCK(inp);
927 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
928 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
929 /* Set the connected flag so we can queue data */
932 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
933 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
935 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
936 SCTP_TCB_UNLOCK(stcb);
942 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
944 struct sockaddr_in6 *sin6;
945 struct sctp_inpcb *inp;
947 struct sctp_ifa *sctp_ifa;
952 * Do the malloc first in case it blocks.
954 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
957 sin6->sin6_family = AF_INET6;
958 sin6->sin6_len = sizeof(*sin6);
960 inp = (struct sctp_inpcb *)so->so_pcb;
962 SCTP_FREE_SONAME(sin6);
963 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
967 sin6->sin6_port = inp->sctp_lport;
968 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
969 /* For the bound all case you get back 0 */
970 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
971 struct sctp_tcb *stcb;
972 struct sockaddr_in6 *sin_a6;
973 struct sctp_nets *net;
976 stcb = LIST_FIRST(&inp->sctp_asoc_list);
978 SCTP_INP_RUNLOCK(inp);
979 SCTP_FREE_SONAME(sin6);
980 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
985 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
986 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
988 /* this will make coverity happy */
991 if (sin_a6->sin6_family == AF_INET6) {
996 if ((!fnd) || (sin_a6 == NULL)) {
998 SCTP_INP_RUNLOCK(inp);
999 SCTP_FREE_SONAME(sin6);
1000 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1003 vrf_id = inp->def_vrf_id;
1004 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1006 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1009 /* For the bound all case you get back 0 */
1010 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1013 /* Take the first IPv6 address in the list */
1014 struct sctp_laddr *laddr;
1017 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1018 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1019 struct sockaddr_in6 *sin_a;
1021 sin_a = &laddr->ifa->address.sin6;
1022 sin6->sin6_addr = sin_a->sin6_addr;
1028 SCTP_FREE_SONAME(sin6);
1029 SCTP_INP_RUNLOCK(inp);
1030 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1034 SCTP_INP_RUNLOCK(inp);
1035 /* Scoping things for v6 */
1036 if ((error = sa6_recoverscope(sin6)) != 0) {
1037 SCTP_FREE_SONAME(sin6);
1040 (*addr) = (struct sockaddr *)sin6;
1045 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1047 struct sockaddr_in6 *sin6;
1049 struct sockaddr_in6 *sin_a6;
1050 struct sctp_inpcb *inp;
1051 struct sctp_tcb *stcb;
1052 struct sctp_nets *net;
1055 /* Do the malloc first in case it blocks. */
1056 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1059 sin6->sin6_family = AF_INET6;
1060 sin6->sin6_len = sizeof(*sin6);
1062 inp = (struct sctp_inpcb *)so->so_pcb;
1063 if ((inp == NULL) ||
1064 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1065 /* UDP type and listeners will drop out here */
1066 SCTP_FREE_SONAME(sin6);
1067 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1070 SCTP_INP_RLOCK(inp);
1071 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1073 SCTP_TCB_LOCK(stcb);
1075 SCTP_INP_RUNLOCK(inp);
1077 SCTP_FREE_SONAME(sin6);
1078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1079 return (ECONNRESET);
1082 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1083 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1084 if (sin_a6->sin6_family == AF_INET6) {
1086 sin6->sin6_port = stcb->rport;
1087 sin6->sin6_addr = sin_a6->sin6_addr;
1091 SCTP_TCB_UNLOCK(stcb);
1093 /* No IPv4 address */
1094 SCTP_FREE_SONAME(sin6);
1095 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1098 if ((error = sa6_recoverscope(sin6)) != 0) {
1099 SCTP_FREE_SONAME(sin6);
1100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1103 *addr = (struct sockaddr *)sin6;
1108 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1110 struct inpcb *inp = sotoinpcb(so);
1114 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1118 /* allow v6 addresses precedence */
1119 error = sctp6_getaddr(so, nam);
1122 struct sockaddr_in6 *sin6;
1124 /* try v4 next if v6 failed */
1125 error = sctp_ingetaddr(so, nam);
1129 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1131 SCTP_FREE_SONAME(*nam);
1134 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1135 SCTP_FREE_SONAME(*nam);
1136 *nam = (struct sockaddr *)sin6;
1144 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1146 struct inpcb *inp = sotoinpcb(so);
1150 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1154 /* allow v6 addresses precedence */
1155 error = sctp6_peeraddr(so, nam);
1158 struct sockaddr_in6 *sin6;
1160 /* try v4 next if v6 failed */
1161 error = sctp_peeraddr(so, nam);
1165 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1167 SCTP_FREE_SONAME(*nam);
1170 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1171 SCTP_FREE_SONAME(*nam);
1172 *nam = (struct sockaddr *)sin6;
1178 struct pr_usrreqs sctp6_usrreqs = {
1179 .pru_abort = sctp6_abort,
1180 .pru_accept = sctp_accept,
1181 .pru_attach = sctp6_attach,
1182 .pru_bind = sctp6_bind,
1183 .pru_connect = sctp6_connect,
1184 .pru_control = in6_control,
1185 .pru_close = sctp6_close,
1186 .pru_detach = sctp6_close,
1187 .pru_sopoll = sopoll_generic,
1188 .pru_flush = sctp_flush,
1189 .pru_disconnect = sctp6_disconnect,
1190 .pru_listen = sctp_listen,
1191 .pru_peeraddr = sctp6_getpeeraddr,
1192 .pru_send = sctp6_send,
1193 .pru_shutdown = sctp_shutdown,
1194 .pru_sockaddr = sctp6_in6getaddr,
1195 .pru_sosend = sctp_sosend,
1196 .pru_soreceive = sctp_soreceive