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 collapsed 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 * optionally switch back to this code (by changing back the
725 * definitions 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
*sa
)
885 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
886 struct sctp_inpcb
*inp
;
888 struct sctp_ifa
*sctp_ifa
;
891 *sin6
= (struct sockaddr_in6
){
892 .sin6_len
= sizeof(struct sockaddr_in6
),
893 .sin6_family
= AF_INET6
,
896 inp
= (struct sctp_inpcb
*)so
->so_pcb
;
898 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ECONNRESET
);
902 sin6
->sin6_port
= inp
->sctp_lport
;
903 if (inp
->sctp_flags
& SCTP_PCB_FLAGS_BOUNDALL
) {
904 /* For the bound all case you get back 0 */
905 if (inp
->sctp_flags
& SCTP_PCB_FLAGS_CONNECTED
) {
906 struct sctp_tcb
*stcb
;
907 struct sockaddr_in6
*sin_a6
;
908 struct sctp_nets
*net
;
911 stcb
= LIST_FIRST(&inp
->sctp_asoc_list
);
913 SCTP_INP_RUNLOCK(inp
);
914 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ENOENT
);
919 TAILQ_FOREACH(net
, &stcb
->asoc
.nets
, sctp_next
) {
920 sin_a6
= (struct sockaddr_in6
*)&net
->ro
._l_addr
;
922 /* this will make coverity happy */
925 if (sin_a6
->sin6_family
== AF_INET6
) {
930 if ((!fnd
) || (sin_a6
== NULL
)) {
932 SCTP_INP_RUNLOCK(inp
);
933 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ENOENT
);
936 vrf_id
= inp
->def_vrf_id
;
937 sctp_ifa
= sctp_source_address_selection(inp
, stcb
, (sctp_route_t
*)&net
->ro
, net
, 0, vrf_id
);
939 sin6
->sin6_addr
= sctp_ifa
->address
.sin6
.sin6_addr
;
942 /* For the bound all case you get back 0 */
943 memset(&sin6
->sin6_addr
, 0, sizeof(sin6
->sin6_addr
));
946 /* Take the first IPv6 address in the list */
947 struct sctp_laddr
*laddr
;
950 LIST_FOREACH(laddr
, &inp
->sctp_addr_list
, sctp_nxt_addr
) {
951 if (laddr
->ifa
->address
.sa
.sa_family
== AF_INET6
) {
952 struct sockaddr_in6
*sin_a
;
954 sin_a
= &laddr
->ifa
->address
.sin6
;
955 sin6
->sin6_addr
= sin_a
->sin6_addr
;
961 SCTP_INP_RUNLOCK(inp
);
962 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ENOENT
);
966 SCTP_INP_RUNLOCK(inp
);
967 /* Scoping things for v6 */
968 if ((error
= sa6_recoverscope(sin6
)) != 0) {
976 sctp6_peeraddr(struct socket
*so
, struct sockaddr
*sa
)
978 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
980 struct sockaddr_in6
*sin_a6
;
981 struct sctp_inpcb
*inp
;
982 struct sctp_tcb
*stcb
;
983 struct sctp_nets
*net
;
986 *sin6
= (struct sockaddr_in6
){
987 .sin6_len
= sizeof(struct sockaddr_in6
),
988 .sin6_family
= AF_INET6
,
991 inp
= (struct sctp_inpcb
*)so
->so_pcb
;
993 ((inp
->sctp_flags
& SCTP_PCB_FLAGS_CONNECTED
) == 0)) {
994 /* UDP type and listeners will drop out here */
995 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ENOTCONN
);
999 stcb
= LIST_FIRST(&inp
->sctp_asoc_list
);
1001 SCTP_TCB_LOCK(stcb
);
1003 SCTP_INP_RUNLOCK(inp
);
1005 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ECONNRESET
);
1006 return (ECONNRESET
);
1009 TAILQ_FOREACH(net
, &stcb
->asoc
.nets
, sctp_next
) {
1010 sin_a6
= (struct sockaddr_in6
*)&net
->ro
._l_addr
;
1011 if (sin_a6
->sin6_family
== AF_INET6
) {
1013 sin6
->sin6_port
= stcb
->rport
;
1014 sin6
->sin6_addr
= sin_a6
->sin6_addr
;
1018 SCTP_TCB_UNLOCK(stcb
);
1020 /* No IPv4 address */
1021 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, ENOENT
);
1024 if ((error
= sa6_recoverscope(sin6
)) != 0) {
1025 SCTP_LTRACE_ERR_RET(inp
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, error
);
1033 sctp6_in6getaddr(struct socket
*so
, struct sockaddr
*sa
)
1035 struct inpcb
*inp
= sotoinpcb(so
);
1039 SCTP_LTRACE_ERR_RET(NULL
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, EINVAL
);
1043 /* allow v6 addresses precedence */
1044 error
= sctp6_getaddr(so
, sa
);
1047 struct sockaddr_in sin
;
1049 /* try v4 next if v6 failed */
1050 error
= sctp_ingetaddr(so
, (struct sockaddr
*)&sin
);
1053 in6_sin_2_v4mapsin6(&sin
, (struct sockaddr_in6
*)sa
);
1060 sctp6_getpeeraddr(struct socket
*so
, struct sockaddr
*sa
)
1062 struct inpcb
*inp
= sotoinpcb(so
);
1066 SCTP_LTRACE_ERR_RET(NULL
, NULL
, NULL
, SCTP_FROM_SCTP6_USRREQ
, EINVAL
);
1070 /* allow v6 addresses precedence */
1071 error
= sctp6_peeraddr(so
, sa
);
1074 struct sockaddr_in sin
;
1076 /* try v4 next if v6 failed */
1077 error
= sctp_peeraddr(so
, (struct sockaddr
*)&sin
);
1080 in6_sin_2_v4mapsin6(&sin
, (struct sockaddr_in6
*)sa
);
1086 #define SCTP6_PROTOSW \
1087 .pr_protocol = IPPROTO_SCTP, \
1088 .pr_ctloutput = sctp_ctloutput, \
1089 .pr_abort = sctp_abort, \
1090 .pr_accept = sctp_accept, \
1091 .pr_attach = sctp6_attach, \
1092 .pr_bind = sctp6_bind, \
1093 .pr_connect = sctp6_connect, \
1094 .pr_control = in6_control, \
1095 .pr_close = sctp6_close, \
1096 .pr_detach = sctp6_close, \
1097 .pr_disconnect = sctp_disconnect, \
1098 .pr_listen = sctp_listen, \
1099 .pr_peeraddr = sctp6_getpeeraddr, \
1100 .pr_send = sctp6_send, \
1101 .pr_shutdown = sctp_shutdown, \
1102 .pr_sockaddr = sctp6_in6getaddr, \
1103 .pr_sosend = sctp_sosend, \
1104 .pr_soreceive = sctp_soreceive
1106 struct protosw sctp6_seqpacket_protosw
= {
1107 .pr_type
= SOCK_SEQPACKET
,
1108 .pr_flags
= PR_WANTRCVD
,
1112 struct protosw sctp6_stream_protosw
= {
1113 .pr_type
= SOCK_STREAM
,
1114 .pr_flags
= PR_CONNREQUIRED
| PR_WANTRCVD
,