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,
184 switch (icmp6_type) {
185 case ICMP6_DST_UNREACH:
186 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
187 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
188 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
189 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
190 /* Mark the net unreachable. */
191 if (net->dest_state & SCTP_ADDR_REACHABLE) {
192 /* Ok that destination is not reachable */
193 net->dest_state &= ~SCTP_ADDR_REACHABLE;
194 net->dest_state &= ~SCTP_ADDR_PF;
195 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
196 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
199 SCTP_TCB_UNLOCK(stcb);
201 case ICMP6_PARAM_PROB:
202 /* Treat it like an ABORT. */
203 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
204 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
205 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
206 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
208 SCTP_TCB_UNLOCK(stcb);
211 case ICMP6_PACKET_TOO_BIG:
212 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
213 SCTP_TCB_UNLOCK(stcb);
216 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
218 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
219 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
223 /* Update the path MTU. */
225 next_mtu -= sizeof(struct udphdr);
227 if (net->mtu > next_mtu) {
230 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
232 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
235 /* Update the association MTU */
236 if (stcb->asoc.smallest_mtu > next_mtu) {
237 sctp_pathmtu_adjustment(stcb, next_mtu);
239 /* Finally, start the PMTU timer if it was running before. */
241 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
243 SCTP_TCB_UNLOCK(stcb);
246 SCTP_TCB_UNLOCK(stcb);
252 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
254 struct ip6ctlparam *ip6cp;
255 struct sctp_inpcb *inp;
256 struct sctp_tcb *stcb;
257 struct sctp_nets *net;
259 struct sockaddr_in6 src, dst;
261 if (pktdst->sa_family != AF_INET6 ||
262 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
266 if ((unsigned)cmd >= PRC_NCMDS) {
269 if (PRC_IS_REDIRECT(cmd)) {
271 } else if (inet6ctlerrmap[cmd] == 0) {
274 /* If the parameter is from icmp6, decode it. */
276 ip6cp = (struct ip6ctlparam *)d;
278 ip6cp = (struct ip6ctlparam *)NULL;
283 * XXX: We assume that when IPV6 is non NULL, M and OFF are
286 if (ip6cp->ip6c_m == NULL) {
291 * Check if we can safely examine the ports and the
292 * verification tag of the SCTP common header.
294 if (ip6cp->ip6c_m->m_pkthdr.len <
295 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
299 /* Copy out the port numbers and the verification tag. */
300 memset(&sh, 0, sizeof(sh));
301 m_copydata(ip6cp->ip6c_m,
303 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
305 memset(&src, 0, sizeof(struct sockaddr_in6));
306 src.sin6_family = AF_INET6;
307 src.sin6_len = sizeof(struct sockaddr_in6);
308 src.sin6_port = sh.src_port;
309 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
310 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
313 memset(&dst, 0, sizeof(struct sockaddr_in6));
314 dst.sin6_family = AF_INET6;
315 dst.sin6_len = sizeof(struct sockaddr_in6);
316 dst.sin6_port = sh.dest_port;
317 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
318 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
323 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
324 (struct sockaddr *)&src,
325 &inp, &net, 1, SCTP_DEFAULT_VRFID);
326 if ((stcb != NULL) &&
329 /* Check the verification tag */
330 if (ntohl(sh.v_tag) != 0) {
332 * This must be the verification tag used
333 * for sending out packets. We don't
334 * consider packets reflecting the
337 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
338 SCTP_TCB_UNLOCK(stcb);
342 if (ip6cp->ip6c_m->m_pkthdr.len >=
343 ip6cp->ip6c_off + sizeof(struct sctphdr) +
344 sizeof(struct sctp_chunkhdr) +
345 offsetof(struct sctp_init, a_rwnd)) {
347 * In this case we can check if we
348 * got an INIT chunk and if the
349 * initiate tag matches.
351 uint32_t initiate_tag;
354 m_copydata(ip6cp->ip6c_m,
356 sizeof(struct sctphdr),
358 (caddr_t)&chunk_type);
359 m_copydata(ip6cp->ip6c_m,
361 sizeof(struct sctphdr) +
362 sizeof(struct sctp_chunkhdr),
364 (caddr_t)&initiate_tag);
365 if ((chunk_type != SCTP_INITIATION) ||
366 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
367 SCTP_TCB_UNLOCK(stcb);
371 SCTP_TCB_UNLOCK(stcb);
375 sctp6_notify(inp, stcb, net,
376 ip6cp->ip6c_icmp6->icmp6_type,
377 ip6cp->ip6c_icmp6->icmp6_code,
378 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
380 if ((stcb == NULL) && (inp != NULL)) {
381 /* reduce inp's ref-count */
383 SCTP_INP_DECR_REF(inp);
384 SCTP_INP_WUNLOCK(inp);
387 SCTP_TCB_UNLOCK(stcb);
394 * this routine can probably be collasped into the one in sctp_userreq.c
395 * since they do the same thing and now we lookup with a sockaddr
398 sctp6_getcred(SYSCTL_HANDLER_ARGS)
401 struct sockaddr_in6 addrs[2];
402 struct sctp_inpcb *inp;
403 struct sctp_nets *net;
404 struct sctp_tcb *stcb;
408 vrf_id = SCTP_DEFAULT_VRFID;
410 error = priv_check(req->td, PRIV_NETINET_GETCRED);
414 if (req->newlen != sizeof(addrs)) {
415 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
418 if (req->oldlen != sizeof(struct ucred)) {
419 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
422 error = SYSCTL_IN(req, addrs, sizeof(addrs));
426 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
428 &inp, &net, 1, vrf_id);
429 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
430 if ((inp != NULL) && (stcb == NULL)) {
431 /* reduce ref-count */
433 SCTP_INP_DECR_REF(inp);
436 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
440 SCTP_TCB_UNLOCK(stcb);
442 * We use the write lock here, only since in the error leg we need
443 * it. If we used RLOCK, then we would have to
444 * wlock/decr/unlock/rlock. Which in theory could create a hole.
445 * Better to use higher wlock.
449 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
451 SCTP_INP_WUNLOCK(inp);
454 cru2x(inp->sctp_socket->so_cred, &xuc);
455 SCTP_INP_WUNLOCK(inp);
456 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
461 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
462 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
463 0, 0, sctp6_getcred, "S,ucred",
464 "Get the ucred of a SCTP6 connection");
467 /* This is the same as the sctp_abort() could be made common */
469 sctp6_abort(struct socket *so)
471 struct epoch_tracker et;
472 struct sctp_inpcb *inp;
475 inp = (struct sctp_inpcb *)so->so_pcb;
477 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
482 flags = inp->sctp_flags;
483 #ifdef SCTP_LOG_CLOSING
484 sctp_log_closing(inp, NULL, 17);
486 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
487 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
488 #ifdef SCTP_LOG_CLOSING
489 sctp_log_closing(inp, NULL, 16);
491 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
492 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
494 SCTP_SB_CLEAR(so->so_snd);
496 * same for the rcv ones, they are only here for the
499 SCTP_SB_CLEAR(so->so_rcv);
500 /* Now null out the reference, we are completely detached. */
504 flags = inp->sctp_flags;
505 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
506 goto sctp_must_try_again;
514 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
517 struct sctp_inpcb *inp;
518 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
520 inp = (struct sctp_inpcb *)so->so_pcb;
522 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
526 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
527 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
531 error = sctp_inpcb_alloc(so, vrf_id);
534 inp = (struct sctp_inpcb *)so->so_pcb;
536 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
538 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
539 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
540 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
543 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
544 * socket as well, because the socket may be bound to an IPv6
545 * wildcard address, which may match an IPv4-mapped IPv6 address.
547 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
549 SCTP_INP_WUNLOCK(inp);
554 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
556 struct sctp_inpcb *inp;
560 inp = (struct sctp_inpcb *)so->so_pcb;
562 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
567 switch (addr->sa_family) {
570 if (addr->sa_len != sizeof(struct sockaddr_in)) {
571 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
578 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
579 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
589 vflagsav = inp->ip_inp.inp.inp_vflag;
590 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
591 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
592 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
593 switch (addr->sa_family) {
596 /* binding v4 addr to v6 socket, so reset flags */
597 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
598 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
604 struct sockaddr_in6 *sin6_p;
606 sin6_p = (struct sockaddr_in6 *)addr;
608 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
609 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
612 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
613 struct sockaddr_in sin;
615 in6_sin6_2_sin(&sin, sin6_p);
616 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
617 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
618 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
628 } else if (addr != NULL) {
629 struct sockaddr_in6 *sin6_p;
631 /* IPV6_V6ONLY socket */
633 if (addr->sa_family == AF_INET) {
634 /* can't bind v4 addr to v6 only socket! */
635 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
640 sin6_p = (struct sockaddr_in6 *)addr;
642 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
643 /* can't bind v4-mapped addrs either! */
644 /* NOTE: we don't support SIIT */
645 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
650 error = sctp_inpcb_bind(so, addr, NULL, p);
653 inp->ip_inp.inp.inp_vflag = vflagsav;
659 sctp6_close(struct socket *so)
664 /* This could be made common with sctp_detach() since they are identical */
668 sctp6_disconnect(struct socket *so)
670 return (sctp_disconnect(so));
675 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
676 struct mbuf *control, struct thread *p);
680 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
681 struct mbuf *control, struct thread *p)
683 struct sctp_inpcb *inp;
686 struct sockaddr_in6 *sin6;
688 /* No SPL needed since sctp_output does this */
690 inp = (struct sctp_inpcb *)so->so_pcb;
693 SCTP_RELEASE_PKT(control);
697 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
701 * For the TCP model we may get a NULL addr, if we are a connected
704 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
711 SCTP_RELEASE_PKT(control);
714 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
715 return (EDESTADDRREQ);
718 sin6 = (struct sockaddr_in6 *)addr;
719 if (SCTP_IPV6_V6ONLY(inp)) {
721 * if IPV6_V6ONLY flag, we discard datagrams destined to a
722 * v4 addr or v4-mapped addr
724 if (addr->sa_family == AF_INET) {
725 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
728 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
733 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
734 struct sockaddr_in sin;
736 /* convert v4-mapped into v4 addr and send */
737 in6_sin6_2_sin(&sin, sin6);
738 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
742 /* now what about control */
745 SCTP_PRINTF("huh? control set?\n");
746 SCTP_RELEASE_PKT(inp->control);
749 inp->control = control;
753 SCTP_BUF_NEXT(inp->pkt_last) = m;
756 inp->pkt_last = inp->pkt = m;
759 /* FreeBSD and MacOSX uses a flag passed */
760 ((flags & PRUS_MORETOCOME) == 0)
763 * note with the current version this code will only be used
764 * by OpenBSD, NetBSD and FreeBSD have methods for
765 * re-defining sosend() to use sctp_sosend(). One can
766 * optionaly switch back to this code (by changing back the
767 * defininitions but this is not advisable.
769 struct epoch_tracker et;
773 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
784 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
786 struct epoch_tracker et;
789 struct sctp_inpcb *inp;
790 struct sctp_tcb *stcb;
792 struct sockaddr_in6 *sin6;
793 union sctp_sockstore store;
796 inp = (struct sctp_inpcb *)so->so_pcb;
798 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
799 return (ECONNRESET); /* I made the same as TCP since we are
803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
806 switch (addr->sa_family) {
809 if (addr->sa_len != sizeof(struct sockaddr_in)) {
810 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
817 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
818 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
824 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
828 vrf_id = inp->def_vrf_id;
829 SCTP_ASOC_CREATE_LOCK(inp);
831 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
832 SCTP_PCB_FLAGS_UNBOUND) {
833 /* Bind a ephemeral port */
834 SCTP_INP_RUNLOCK(inp);
835 error = sctp6_bind(so, NULL, p);
837 SCTP_ASOC_CREATE_UNLOCK(inp);
843 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
844 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
845 /* We are already connected AND the TCP model */
846 SCTP_INP_RUNLOCK(inp);
847 SCTP_ASOC_CREATE_UNLOCK(inp);
848 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
852 sin6 = (struct sockaddr_in6 *)addr;
853 if (SCTP_IPV6_V6ONLY(inp)) {
855 * if IPV6_V6ONLY flag, ignore connections destined to a v4
856 * addr or v4-mapped addr
858 if (addr->sa_family == AF_INET) {
859 SCTP_INP_RUNLOCK(inp);
860 SCTP_ASOC_CREATE_UNLOCK(inp);
861 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
864 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
865 SCTP_INP_RUNLOCK(inp);
866 SCTP_ASOC_CREATE_UNLOCK(inp);
867 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
871 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
872 /* convert v4-mapped into v4 addr */
873 in6_sin6_2_sin(&store.sin, sin6);
877 /* Now do we connect? */
878 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
879 stcb = LIST_FIRST(&inp->sctp_asoc_list);
883 SCTP_INP_RUNLOCK(inp);
885 SCTP_INP_RUNLOCK(inp);
887 SCTP_INP_INCR_REF(inp);
888 SCTP_INP_WUNLOCK(inp);
889 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
892 SCTP_INP_DECR_REF(inp);
893 SCTP_INP_WUNLOCK(inp);
898 /* Already have or am bring up an association */
899 SCTP_ASOC_CREATE_UNLOCK(inp);
900 SCTP_TCB_UNLOCK(stcb);
901 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
904 /* We are GOOD to go */
905 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
906 inp->sctp_ep.pre_open_stream_count,
907 inp->sctp_ep.port, p,
908 SCTP_INITIALIZE_AUTH_PARAMS);
909 SCTP_ASOC_CREATE_UNLOCK(inp);
914 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
915 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
916 /* Set the connected flag so we can queue data */
919 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
920 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
922 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
923 SCTP_TCB_UNLOCK(stcb);
929 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
931 struct sockaddr_in6 *sin6;
932 struct sctp_inpcb *inp;
934 struct sctp_ifa *sctp_ifa;
939 * Do the malloc first in case it blocks.
941 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
944 sin6->sin6_family = AF_INET6;
945 sin6->sin6_len = sizeof(*sin6);
947 inp = (struct sctp_inpcb *)so->so_pcb;
949 SCTP_FREE_SONAME(sin6);
950 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
954 sin6->sin6_port = inp->sctp_lport;
955 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
956 /* For the bound all case you get back 0 */
957 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
958 struct sctp_tcb *stcb;
959 struct sockaddr_in6 *sin_a6;
960 struct sctp_nets *net;
963 stcb = LIST_FIRST(&inp->sctp_asoc_list);
965 SCTP_INP_RUNLOCK(inp);
966 SCTP_FREE_SONAME(sin6);
967 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
972 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
973 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
975 /* this will make coverity happy */
978 if (sin_a6->sin6_family == AF_INET6) {
983 if ((!fnd) || (sin_a6 == NULL)) {
985 SCTP_INP_RUNLOCK(inp);
986 SCTP_FREE_SONAME(sin6);
987 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
990 vrf_id = inp->def_vrf_id;
991 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
993 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
996 /* For the bound all case you get back 0 */
997 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1000 /* Take the first IPv6 address in the list */
1001 struct sctp_laddr *laddr;
1004 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1005 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1006 struct sockaddr_in6 *sin_a;
1008 sin_a = &laddr->ifa->address.sin6;
1009 sin6->sin6_addr = sin_a->sin6_addr;
1015 SCTP_FREE_SONAME(sin6);
1016 SCTP_INP_RUNLOCK(inp);
1017 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1021 SCTP_INP_RUNLOCK(inp);
1022 /* Scoping things for v6 */
1023 if ((error = sa6_recoverscope(sin6)) != 0) {
1024 SCTP_FREE_SONAME(sin6);
1027 (*addr) = (struct sockaddr *)sin6;
1032 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1034 struct sockaddr_in6 *sin6;
1036 struct sockaddr_in6 *sin_a6;
1037 struct sctp_inpcb *inp;
1038 struct sctp_tcb *stcb;
1039 struct sctp_nets *net;
1042 /* Do the malloc first in case it blocks. */
1043 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1046 sin6->sin6_family = AF_INET6;
1047 sin6->sin6_len = sizeof(*sin6);
1049 inp = (struct sctp_inpcb *)so->so_pcb;
1050 if ((inp == NULL) ||
1051 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1052 /* UDP type and listeners will drop out here */
1053 SCTP_FREE_SONAME(sin6);
1054 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1057 SCTP_INP_RLOCK(inp);
1058 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1060 SCTP_TCB_LOCK(stcb);
1062 SCTP_INP_RUNLOCK(inp);
1064 SCTP_FREE_SONAME(sin6);
1065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1066 return (ECONNRESET);
1069 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1070 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1071 if (sin_a6->sin6_family == AF_INET6) {
1073 sin6->sin6_port = stcb->rport;
1074 sin6->sin6_addr = sin_a6->sin6_addr;
1078 SCTP_TCB_UNLOCK(stcb);
1080 /* No IPv4 address */
1081 SCTP_FREE_SONAME(sin6);
1082 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1085 if ((error = sa6_recoverscope(sin6)) != 0) {
1086 SCTP_FREE_SONAME(sin6);
1087 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1090 *addr = (struct sockaddr *)sin6;
1095 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1097 struct inpcb *inp = sotoinpcb(so);
1101 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1105 /* allow v6 addresses precedence */
1106 error = sctp6_getaddr(so, nam);
1109 struct sockaddr_in6 *sin6;
1111 /* try v4 next if v6 failed */
1112 error = sctp_ingetaddr(so, nam);
1116 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1118 SCTP_FREE_SONAME(*nam);
1121 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1122 SCTP_FREE_SONAME(*nam);
1123 *nam = (struct sockaddr *)sin6;
1131 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1133 struct inpcb *inp = sotoinpcb(so);
1137 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1141 /* allow v6 addresses precedence */
1142 error = sctp6_peeraddr(so, nam);
1145 struct sockaddr_in6 *sin6;
1147 /* try v4 next if v6 failed */
1148 error = sctp_peeraddr(so, nam);
1152 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1154 SCTP_FREE_SONAME(*nam);
1157 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1158 SCTP_FREE_SONAME(*nam);
1159 *nam = (struct sockaddr *)sin6;
1165 struct pr_usrreqs sctp6_usrreqs = {
1166 .pru_abort = sctp6_abort,
1167 .pru_accept = sctp_accept,
1168 .pru_attach = sctp6_attach,
1169 .pru_bind = sctp6_bind,
1170 .pru_connect = sctp6_connect,
1171 .pru_control = in6_control,
1172 .pru_close = sctp6_close,
1173 .pru_detach = sctp6_close,
1174 .pru_sopoll = sopoll_generic,
1175 .pru_flush = sctp_flush,
1176 .pru_disconnect = sctp6_disconnect,
1177 .pru_listen = sctp_listen,
1178 .pru_peeraddr = sctp6_getpeeraddr,
1179 .pru_send = sctp6_send,
1180 .pru_shutdown = sctp_shutdown,
1181 .pru_sockaddr = sctp6_in6getaddr,
1182 .pru_sosend = sctp_sosend,
1183 .pru_soreceive = sctp_soreceive