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>
60 extern struct protosw inetsw[];
63 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
69 struct sockaddr_in6 src, dst;
72 struct sctp_chunkhdr *ch;
80 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
81 SCTP_RELEASE_PKT(*i_pak);
82 return (IPPROTO_DONE);
84 m = SCTP_HEADER_TO_CHAIN(*i_pak);
85 #ifdef SCTP_MBUF_LOGGING
86 /* Log in any input mbufs */
87 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
88 sctp_log_mbc(m, SCTP_MBUF_INPUT);
91 #ifdef SCTP_PACKET_LOGGING
92 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
96 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
97 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
99 if_name(m->m_pkthdr.rcvif),
100 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
101 mflowid = m->m_pkthdr.flowid;
102 mflowtype = M_HASHTYPE_GET(m);
103 fibnum = M_GETFIB(m);
104 SCTP_STAT_INCR(sctps_recvpackets);
105 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
106 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
107 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
108 ip6 = mtod(m, struct ip6_hdr *);
109 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
110 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
112 SCTP_STAT_INCR(sctps_hdrops);
113 return (IPPROTO_DONE);
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 /* Update the association MTU */
245 if (stcb->asoc.smallest_mtu > next_mtu) {
246 sctp_pathmtu_adjustment(stcb, next_mtu);
248 /* Finally, start the PMTU timer if it was running before. */
250 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
252 SCTP_TCB_UNLOCK(stcb);
255 SCTP_TCB_UNLOCK(stcb);
261 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
263 struct ip6ctlparam *ip6cp;
264 struct sctp_inpcb *inp;
265 struct sctp_tcb *stcb;
266 struct sctp_nets *net;
268 struct sockaddr_in6 src, dst;
270 if (pktdst->sa_family != AF_INET6 ||
271 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
275 if ((unsigned)cmd >= PRC_NCMDS) {
278 if (PRC_IS_REDIRECT(cmd)) {
280 } else if (inet6ctlerrmap[cmd] == 0) {
283 /* If the parameter is from icmp6, decode it. */
285 ip6cp = (struct ip6ctlparam *)d;
287 ip6cp = (struct ip6ctlparam *)NULL;
292 * XXX: We assume that when IPV6 is non NULL, M and OFF are
295 if (ip6cp->ip6c_m == NULL) {
300 * Check if we can safely examine the ports and the
301 * verification tag of the SCTP common header.
303 if (ip6cp->ip6c_m->m_pkthdr.len <
304 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
308 /* Copy out the port numbers and the verification tag. */
309 memset(&sh, 0, sizeof(sh));
310 m_copydata(ip6cp->ip6c_m,
312 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
314 memset(&src, 0, sizeof(struct sockaddr_in6));
315 src.sin6_family = AF_INET6;
316 src.sin6_len = sizeof(struct sockaddr_in6);
317 src.sin6_port = sh.src_port;
318 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
319 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
322 memset(&dst, 0, sizeof(struct sockaddr_in6));
323 dst.sin6_family = AF_INET6;
324 dst.sin6_len = sizeof(struct sockaddr_in6);
325 dst.sin6_port = sh.dest_port;
326 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
327 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
332 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
333 (struct sockaddr *)&src,
334 &inp, &net, 1, SCTP_DEFAULT_VRFID);
335 if ((stcb != NULL) &&
338 /* Check the verification tag */
339 if (ntohl(sh.v_tag) != 0) {
341 * This must be the verification tag used
342 * for sending out packets. We don't
343 * consider packets reflecting the
346 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
347 SCTP_TCB_UNLOCK(stcb);
351 if (ip6cp->ip6c_m->m_pkthdr.len >=
352 ip6cp->ip6c_off + sizeof(struct sctphdr) +
353 sizeof(struct sctp_chunkhdr) +
354 offsetof(struct sctp_init, a_rwnd)) {
356 * In this case we can check if we
357 * got an INIT chunk and if the
358 * initiate tag matches.
360 uint32_t initiate_tag;
363 m_copydata(ip6cp->ip6c_m,
365 sizeof(struct sctphdr),
367 (caddr_t)&chunk_type);
368 m_copydata(ip6cp->ip6c_m,
370 sizeof(struct sctphdr) +
371 sizeof(struct sctp_chunkhdr),
373 (caddr_t)&initiate_tag);
374 if ((chunk_type != SCTP_INITIATION) ||
375 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
376 SCTP_TCB_UNLOCK(stcb);
380 SCTP_TCB_UNLOCK(stcb);
384 sctp6_notify(inp, stcb, net,
385 ip6cp->ip6c_icmp6->icmp6_type,
386 ip6cp->ip6c_icmp6->icmp6_code,
387 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
389 if ((stcb == NULL) && (inp != NULL)) {
390 /* reduce inp's ref-count */
392 SCTP_INP_DECR_REF(inp);
393 SCTP_INP_WUNLOCK(inp);
396 SCTP_TCB_UNLOCK(stcb);
403 * this routine can probably be collasped into the one in sctp_userreq.c
404 * since they do the same thing and now we lookup with a sockaddr
407 sctp6_getcred(SYSCTL_HANDLER_ARGS)
410 struct sockaddr_in6 addrs[2];
411 struct sctp_inpcb *inp;
412 struct sctp_nets *net;
413 struct sctp_tcb *stcb;
417 vrf_id = SCTP_DEFAULT_VRFID;
419 error = priv_check(req->td, PRIV_NETINET_GETCRED);
423 if (req->newlen != sizeof(addrs)) {
424 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
427 if (req->oldlen != sizeof(struct ucred)) {
428 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
431 error = SYSCTL_IN(req, addrs, sizeof(addrs));
435 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
437 &inp, &net, 1, vrf_id);
438 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
439 if ((inp != NULL) && (stcb == NULL)) {
440 /* reduce ref-count */
442 SCTP_INP_DECR_REF(inp);
445 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
449 SCTP_TCB_UNLOCK(stcb);
451 * We use the write lock here, only since in the error leg we need
452 * it. If we used RLOCK, then we would have to
453 * wlock/decr/unlock/rlock. Which in theory could create a hole.
454 * Better to use higher wlock.
458 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
460 SCTP_INP_WUNLOCK(inp);
463 cru2x(inp->sctp_socket->so_cred, &xuc);
464 SCTP_INP_WUNLOCK(inp);
465 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
470 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
472 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
475 /* This is the same as the sctp_abort() could be made common */
477 sctp6_abort(struct socket *so)
479 struct sctp_inpcb *inp;
482 inp = (struct sctp_inpcb *)so->so_pcb;
484 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
488 flags = inp->sctp_flags;
489 #ifdef SCTP_LOG_CLOSING
490 sctp_log_closing(inp, NULL, 17);
492 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
493 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
494 #ifdef SCTP_LOG_CLOSING
495 sctp_log_closing(inp, NULL, 16);
497 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
498 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
500 SCTP_SB_CLEAR(so->so_snd);
502 * same for the rcv ones, they are only here for the
505 SCTP_SB_CLEAR(so->so_rcv);
506 /* Now null out the reference, we are completely detached. */
510 flags = inp->sctp_flags;
511 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
512 goto sctp_must_try_again;
519 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
523 struct sctp_inpcb *inp;
524 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
526 inp = (struct sctp_inpcb *)so->so_pcb;
528 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
532 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
533 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
537 error = sctp_inpcb_alloc(so, vrf_id);
540 inp = (struct sctp_inpcb *)so->so_pcb;
542 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
543 inp6 = (struct in6pcb *)inp;
545 inp6->inp_vflag |= INP_IPV6;
546 inp6->in6p_hops = -1; /* use kernel default */
547 inp6->in6p_cksum = -1; /* just to be sure */
550 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
551 * socket as well, because the socket may be bound to an IPv6
552 * wildcard address, which may match an IPv4-mapped IPv6 address.
554 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
556 SCTP_INP_WUNLOCK(inp);
561 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
563 struct sctp_inpcb *inp;
567 inp = (struct sctp_inpcb *)so->so_pcb;
569 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
574 switch (addr->sa_family) {
577 if (addr->sa_len != sizeof(struct sockaddr_in)) {
578 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
585 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
586 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
596 inp6 = (struct in6pcb *)inp;
597 inp6->inp_vflag &= ~INP_IPV4;
598 inp6->inp_vflag |= INP_IPV6;
599 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
600 switch (addr->sa_family) {
603 /* binding v4 addr to v6 socket, so reset flags */
604 inp6->inp_vflag |= INP_IPV4;
605 inp6->inp_vflag &= ~INP_IPV6;
611 struct sockaddr_in6 *sin6_p;
613 sin6_p = (struct sockaddr_in6 *)addr;
615 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
616 inp6->inp_vflag |= INP_IPV4;
619 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
620 struct sockaddr_in sin;
622 in6_sin6_2_sin(&sin, sin6_p);
623 inp6->inp_vflag |= INP_IPV4;
624 inp6->inp_vflag &= ~INP_IPV6;
625 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
635 } else if (addr != NULL) {
636 struct sockaddr_in6 *sin6_p;
638 /* IPV6_V6ONLY socket */
640 if (addr->sa_family == AF_INET) {
641 /* can't bind v4 addr to v6 only socket! */
642 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
646 sin6_p = (struct sockaddr_in6 *)addr;
648 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
649 /* can't bind v4-mapped addrs either! */
650 /* NOTE: we don't support SIIT */
651 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
655 error = sctp_inpcb_bind(so, addr, NULL, p);
661 sctp6_close(struct socket *so)
666 /* This could be made common with sctp_detach() since they are identical */
670 sctp6_disconnect(struct socket *so)
672 return (sctp_disconnect(so));
677 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
678 struct mbuf *control, struct thread *p);
682 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
683 struct mbuf *control, struct thread *p)
685 struct sctp_inpcb *inp;
689 struct sockaddr_in6 *sin6;
691 /* No SPL needed since sctp_output does this */
693 inp = (struct sctp_inpcb *)so->so_pcb;
696 SCTP_RELEASE_PKT(control);
700 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
703 inp6 = (struct in6pcb *)inp;
705 * For the TCP model we may get a NULL addr, if we are a connected
708 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
715 SCTP_RELEASE_PKT(control);
718 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
719 return (EDESTADDRREQ);
722 sin6 = (struct sockaddr_in6 *)addr;
723 if (SCTP_IPV6_V6ONLY(inp6)) {
725 * if IPV6_V6ONLY flag, we discard datagrams destined to a
726 * v4 addr or v4-mapped addr
728 if (addr->sa_family == AF_INET) {
729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
732 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
733 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
737 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
738 struct sockaddr_in sin;
740 /* convert v4-mapped into v4 addr and send */
741 in6_sin6_2_sin(&sin, sin6);
742 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
746 /* now what about control */
749 SCTP_PRINTF("huh? control set?\n");
750 SCTP_RELEASE_PKT(inp->control);
753 inp->control = control;
757 SCTP_BUF_NEXT(inp->pkt_last) = m;
760 inp->pkt_last = inp->pkt = m;
763 /* FreeBSD and MacOSX uses a flag passed */
764 ((flags & PRUS_MORETOCOME) == 0)
767 * note with the current version this code will only be used
768 * by OpenBSD, NetBSD and FreeBSD have methods for
769 * re-defining sosend() to use sctp_sosend(). One can
770 * optionaly switch back to this code (by changing back the
771 * defininitions but this is not advisable.
775 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
785 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
789 struct sctp_inpcb *inp;
790 struct sctp_tcb *stcb;
793 struct sockaddr_in6 *sin6;
794 union sctp_sockstore store;
798 inp6 = (struct in6pcb *)so->so_pcb;
800 inp = (struct sctp_inpcb *)so->so_pcb;
802 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
803 return (ECONNRESET); /* I made the same as TCP since we are
807 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
810 switch (addr->sa_family) {
813 if (addr->sa_len != sizeof(struct sockaddr_in)) {
814 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
821 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
822 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
828 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
832 vrf_id = inp->def_vrf_id;
833 SCTP_ASOC_CREATE_LOCK(inp);
835 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
836 SCTP_PCB_FLAGS_UNBOUND) {
837 /* Bind a ephemeral port */
838 SCTP_INP_RUNLOCK(inp);
839 error = sctp6_bind(so, NULL, p);
841 SCTP_ASOC_CREATE_UNLOCK(inp);
847 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
848 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
849 /* We are already connected AND the TCP model */
850 SCTP_INP_RUNLOCK(inp);
851 SCTP_ASOC_CREATE_UNLOCK(inp);
852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
856 sin6 = (struct sockaddr_in6 *)addr;
857 if (SCTP_IPV6_V6ONLY(inp6)) {
859 * if IPV6_V6ONLY flag, ignore connections destined to a v4
860 * addr or v4-mapped addr
862 if (addr->sa_family == AF_INET) {
863 SCTP_INP_RUNLOCK(inp);
864 SCTP_ASOC_CREATE_UNLOCK(inp);
865 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
868 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
869 SCTP_INP_RUNLOCK(inp);
870 SCTP_ASOC_CREATE_UNLOCK(inp);
871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
875 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
876 /* convert v4-mapped into v4 addr */
877 in6_sin6_2_sin(&store.sin, sin6);
881 /* Now do we connect? */
882 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
883 stcb = LIST_FIRST(&inp->sctp_asoc_list);
887 SCTP_INP_RUNLOCK(inp);
889 SCTP_INP_RUNLOCK(inp);
891 SCTP_INP_INCR_REF(inp);
892 SCTP_INP_WUNLOCK(inp);
893 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
896 SCTP_INP_DECR_REF(inp);
897 SCTP_INP_WUNLOCK(inp);
902 /* Already have or am bring up an association */
903 SCTP_ASOC_CREATE_UNLOCK(inp);
904 SCTP_TCB_UNLOCK(stcb);
905 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
908 /* We are GOOD to go */
909 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
910 inp->sctp_ep.pre_open_stream_count,
911 inp->sctp_ep.port, p);
912 SCTP_ASOC_CREATE_UNLOCK(inp);
917 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
918 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
919 /* Set the connected flag so we can queue data */
922 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
923 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
925 /* initialize authentication parameters for the assoc */
926 sctp_initialize_auth_params(inp, stcb);
928 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
929 SCTP_TCB_UNLOCK(stcb);
934 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
936 struct sockaddr_in6 *sin6;
937 struct sctp_inpcb *inp;
939 struct sctp_ifa *sctp_ifa;
944 * Do the malloc first in case it blocks.
946 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
949 sin6->sin6_family = AF_INET6;
950 sin6->sin6_len = sizeof(*sin6);
952 inp = (struct sctp_inpcb *)so->so_pcb;
954 SCTP_FREE_SONAME(sin6);
955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
959 sin6->sin6_port = inp->sctp_lport;
960 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
961 /* For the bound all case you get back 0 */
962 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
963 struct sctp_tcb *stcb;
964 struct sockaddr_in6 *sin_a6;
965 struct sctp_nets *net;
968 stcb = LIST_FIRST(&inp->sctp_asoc_list);
970 SCTP_INP_RUNLOCK(inp);
971 SCTP_FREE_SONAME(sin6);
972 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
977 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
978 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
980 /* this will make coverity happy */
983 if (sin_a6->sin6_family == AF_INET6) {
988 if ((!fnd) || (sin_a6 == NULL)) {
990 SCTP_INP_RUNLOCK(inp);
991 SCTP_FREE_SONAME(sin6);
992 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
995 vrf_id = inp->def_vrf_id;
996 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
998 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1001 /* For the bound all case you get back 0 */
1002 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1005 /* Take the first IPv6 address in the list */
1006 struct sctp_laddr *laddr;
1009 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1010 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1011 struct sockaddr_in6 *sin_a;
1013 sin_a = &laddr->ifa->address.sin6;
1014 sin6->sin6_addr = sin_a->sin6_addr;
1020 SCTP_FREE_SONAME(sin6);
1021 SCTP_INP_RUNLOCK(inp);
1022 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1026 SCTP_INP_RUNLOCK(inp);
1027 /* Scoping things for v6 */
1028 if ((error = sa6_recoverscope(sin6)) != 0) {
1029 SCTP_FREE_SONAME(sin6);
1032 (*addr) = (struct sockaddr *)sin6;
1037 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1039 struct sockaddr_in6 *sin6;
1041 struct sockaddr_in6 *sin_a6;
1042 struct sctp_inpcb *inp;
1043 struct sctp_tcb *stcb;
1044 struct sctp_nets *net;
1047 /* Do the malloc first in case it blocks. */
1048 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1051 sin6->sin6_family = AF_INET6;
1052 sin6->sin6_len = sizeof(*sin6);
1054 inp = (struct sctp_inpcb *)so->so_pcb;
1055 if ((inp == NULL) ||
1056 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1057 /* UDP type and listeners will drop out here */
1058 SCTP_FREE_SONAME(sin6);
1059 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1062 SCTP_INP_RLOCK(inp);
1063 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1065 SCTP_TCB_LOCK(stcb);
1067 SCTP_INP_RUNLOCK(inp);
1069 SCTP_FREE_SONAME(sin6);
1070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1071 return (ECONNRESET);
1074 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1075 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1076 if (sin_a6->sin6_family == AF_INET6) {
1078 sin6->sin6_port = stcb->rport;
1079 sin6->sin6_addr = sin_a6->sin6_addr;
1083 SCTP_TCB_UNLOCK(stcb);
1085 /* No IPv4 address */
1086 SCTP_FREE_SONAME(sin6);
1087 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1090 if ((error = sa6_recoverscope(sin6)) != 0) {
1091 SCTP_FREE_SONAME(sin6);
1092 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1095 *addr = (struct sockaddr *)sin6;
1100 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1102 struct in6pcb *inp6 = sotoin6pcb(so);
1106 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1110 /* allow v6 addresses precedence */
1111 error = sctp6_getaddr(so, nam);
1114 struct sockaddr_in6 *sin6;
1116 /* try v4 next if v6 failed */
1117 error = sctp_ingetaddr(so, nam);
1121 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1123 SCTP_FREE_SONAME(*nam);
1126 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1127 SCTP_FREE_SONAME(*nam);
1128 *nam = (struct sockaddr *)sin6;
1136 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1138 struct in6pcb *inp6 = sotoin6pcb(so);
1142 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1146 /* allow v6 addresses precedence */
1147 error = sctp6_peeraddr(so, nam);
1150 struct sockaddr_in6 *sin6;
1152 /* try v4 next if v6 failed */
1153 error = sctp_peeraddr(so, nam);
1157 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1159 SCTP_FREE_SONAME(*nam);
1162 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1163 SCTP_FREE_SONAME(*nam);
1164 *nam = (struct sockaddr *)sin6;
1170 struct pr_usrreqs sctp6_usrreqs = {
1171 .pru_abort = sctp6_abort,
1172 .pru_accept = sctp_accept,
1173 .pru_attach = sctp6_attach,
1174 .pru_bind = sctp6_bind,
1175 .pru_connect = sctp6_connect,
1176 .pru_control = in6_control,
1177 .pru_close = sctp6_close,
1178 .pru_detach = sctp6_close,
1179 .pru_sopoll = sopoll_generic,
1180 .pru_flush = sctp_flush,
1181 .pru_disconnect = sctp6_disconnect,
1182 .pru_listen = sctp_listen,
1183 .pru_peeraddr = sctp6_getpeeraddr,
1184 .pru_send = sctp6_send,
1185 .pru_shutdown = sctp_shutdown,
1186 .pru_sockaddr = sctp6_in6getaddr,
1187 .pru_sosend = sctp_sosend,
1188 .pru_soreceive = sctp_soreceive