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,
476 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
477 0, 0, sctp6_getcred, "S,ucred",
478 "Get the ucred of a SCTP6 connection");
481 /* This is the same as the sctp_abort() could be made common */
483 sctp6_abort(struct socket *so)
485 struct epoch_tracker et;
486 struct sctp_inpcb *inp;
489 inp = (struct sctp_inpcb *)so->so_pcb;
491 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
496 flags = inp->sctp_flags;
497 #ifdef SCTP_LOG_CLOSING
498 sctp_log_closing(inp, NULL, 17);
500 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
501 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
502 #ifdef SCTP_LOG_CLOSING
503 sctp_log_closing(inp, NULL, 16);
505 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
506 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
508 SCTP_SB_CLEAR(so->so_snd);
510 * same for the rcv ones, they are only here for the
513 SCTP_SB_CLEAR(so->so_rcv);
514 /* Now null out the reference, we are completely detached. */
518 flags = inp->sctp_flags;
519 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
520 goto sctp_must_try_again;
528 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
531 struct sctp_inpcb *inp;
532 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
534 inp = (struct sctp_inpcb *)so->so_pcb;
536 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
540 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
541 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
545 error = sctp_inpcb_alloc(so, vrf_id);
548 inp = (struct sctp_inpcb *)so->so_pcb;
550 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
552 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
553 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
554 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
557 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
558 * socket as well, because the socket may be bound to an IPv6
559 * wildcard address, which may match an IPv4-mapped IPv6 address.
561 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
563 SCTP_INP_WUNLOCK(inp);
568 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
570 struct sctp_inpcb *inp;
574 inp = (struct sctp_inpcb *)so->so_pcb;
576 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
581 switch (addr->sa_family) {
584 if (addr->sa_len != sizeof(struct sockaddr_in)) {
585 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
592 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
593 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
599 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
603 vflagsav = inp->ip_inp.inp.inp_vflag;
604 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
605 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
606 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
607 switch (addr->sa_family) {
610 /* binding v4 addr to v6 socket, so reset flags */
611 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
612 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
618 struct sockaddr_in6 *sin6_p;
620 sin6_p = (struct sockaddr_in6 *)addr;
622 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
623 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
626 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
627 struct sockaddr_in sin;
629 in6_sin6_2_sin(&sin, sin6_p);
630 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
631 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
632 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
642 } else if (addr != NULL) {
643 struct sockaddr_in6 *sin6_p;
645 /* IPV6_V6ONLY socket */
647 if (addr->sa_family == AF_INET) {
648 /* can't bind v4 addr to v6 only socket! */
649 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
654 sin6_p = (struct sockaddr_in6 *)addr;
656 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
657 /* can't bind v4-mapped addrs either! */
658 /* NOTE: we don't support SIIT */
659 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
664 error = sctp_inpcb_bind(so, addr, NULL, p);
667 inp->ip_inp.inp.inp_vflag = vflagsav;
673 sctp6_close(struct socket *so)
678 /* This could be made common with sctp_detach() since they are identical */
682 sctp6_disconnect(struct socket *so)
684 return (sctp_disconnect(so));
689 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
690 struct mbuf *control, struct thread *p);
694 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
695 struct mbuf *control, struct thread *p)
697 struct sctp_inpcb *inp;
700 struct sockaddr_in6 *sin6;
702 /* No SPL needed since sctp_output does this */
704 inp = (struct sctp_inpcb *)so->so_pcb;
707 SCTP_RELEASE_PKT(control);
711 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
715 * For the TCP model we may get a NULL addr, if we are a connected
718 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
725 SCTP_RELEASE_PKT(control);
728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
729 return (EDESTADDRREQ);
732 sin6 = (struct sockaddr_in6 *)addr;
733 if (SCTP_IPV6_V6ONLY(inp)) {
735 * if IPV6_V6ONLY flag, we discard datagrams destined to a
736 * v4 addr or v4-mapped addr
738 if (addr->sa_family == AF_INET) {
739 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
742 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
743 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
747 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
748 struct sockaddr_in sin;
750 /* convert v4-mapped into v4 addr and send */
751 in6_sin6_2_sin(&sin, sin6);
752 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
756 /* now what about control */
759 SCTP_PRINTF("huh? control set?\n");
760 SCTP_RELEASE_PKT(inp->control);
763 inp->control = control;
767 SCTP_BUF_NEXT(inp->pkt_last) = m;
770 inp->pkt_last = inp->pkt = m;
773 /* FreeBSD and MacOSX uses a flag passed */
774 ((flags & PRUS_MORETOCOME) == 0)
777 * note with the current version this code will only be used
778 * by OpenBSD, NetBSD and FreeBSD have methods for
779 * re-defining sosend() to use sctp_sosend(). One can
780 * optionaly switch back to this code (by changing back the
781 * defininitions but this is not advisable.
783 struct epoch_tracker et;
787 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
798 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
800 struct epoch_tracker et;
803 struct sctp_inpcb *inp;
804 struct sctp_tcb *stcb;
806 struct sockaddr_in6 *sin6;
807 union sctp_sockstore store;
810 inp = (struct sctp_inpcb *)so->so_pcb;
812 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
813 return (ECONNRESET); /* I made the same as TCP since we are
817 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
820 switch (addr->sa_family) {
823 if (addr->sa_len != sizeof(struct sockaddr_in)) {
824 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
831 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
832 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
838 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
842 vrf_id = inp->def_vrf_id;
843 SCTP_ASOC_CREATE_LOCK(inp);
845 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
846 SCTP_PCB_FLAGS_UNBOUND) {
847 /* Bind a ephemeral port */
848 SCTP_INP_RUNLOCK(inp);
849 error = sctp6_bind(so, NULL, p);
851 SCTP_ASOC_CREATE_UNLOCK(inp);
857 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
858 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
859 /* We are already connected AND the TCP model */
860 SCTP_INP_RUNLOCK(inp);
861 SCTP_ASOC_CREATE_UNLOCK(inp);
862 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
866 sin6 = (struct sockaddr_in6 *)addr;
867 if (SCTP_IPV6_V6ONLY(inp)) {
869 * if IPV6_V6ONLY flag, ignore connections destined to a v4
870 * addr or v4-mapped addr
872 if (addr->sa_family == AF_INET) {
873 SCTP_INP_RUNLOCK(inp);
874 SCTP_ASOC_CREATE_UNLOCK(inp);
875 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
878 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
879 SCTP_INP_RUNLOCK(inp);
880 SCTP_ASOC_CREATE_UNLOCK(inp);
881 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
885 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
886 /* convert v4-mapped into v4 addr */
887 in6_sin6_2_sin(&store.sin, sin6);
891 /* Now do we connect? */
892 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
893 stcb = LIST_FIRST(&inp->sctp_asoc_list);
897 SCTP_INP_RUNLOCK(inp);
899 SCTP_INP_RUNLOCK(inp);
901 SCTP_INP_INCR_REF(inp);
902 SCTP_INP_WUNLOCK(inp);
903 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
906 SCTP_INP_DECR_REF(inp);
907 SCTP_INP_WUNLOCK(inp);
912 /* Already have or am bring up an association */
913 SCTP_ASOC_CREATE_UNLOCK(inp);
914 SCTP_TCB_UNLOCK(stcb);
915 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
918 /* We are GOOD to go */
919 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
920 inp->sctp_ep.pre_open_stream_count,
921 inp->sctp_ep.port, p,
922 SCTP_INITIALIZE_AUTH_PARAMS);
923 SCTP_ASOC_CREATE_UNLOCK(inp);
928 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
929 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
930 /* Set the connected flag so we can queue data */
933 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
934 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
936 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
937 SCTP_TCB_UNLOCK(stcb);
943 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
945 struct sockaddr_in6 *sin6;
946 struct sctp_inpcb *inp;
948 struct sctp_ifa *sctp_ifa;
953 * Do the malloc first in case it blocks.
955 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
958 sin6->sin6_family = AF_INET6;
959 sin6->sin6_len = sizeof(*sin6);
961 inp = (struct sctp_inpcb *)so->so_pcb;
963 SCTP_FREE_SONAME(sin6);
964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
968 sin6->sin6_port = inp->sctp_lport;
969 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
970 /* For the bound all case you get back 0 */
971 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
972 struct sctp_tcb *stcb;
973 struct sockaddr_in6 *sin_a6;
974 struct sctp_nets *net;
977 stcb = LIST_FIRST(&inp->sctp_asoc_list);
979 SCTP_INP_RUNLOCK(inp);
980 SCTP_FREE_SONAME(sin6);
981 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
986 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
987 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
989 /* this will make coverity happy */
992 if (sin_a6->sin6_family == AF_INET6) {
997 if ((!fnd) || (sin_a6 == NULL)) {
999 SCTP_INP_RUNLOCK(inp);
1000 SCTP_FREE_SONAME(sin6);
1001 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1004 vrf_id = inp->def_vrf_id;
1005 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1007 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1010 /* For the bound all case you get back 0 */
1011 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1014 /* Take the first IPv6 address in the list */
1015 struct sctp_laddr *laddr;
1018 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1019 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1020 struct sockaddr_in6 *sin_a;
1022 sin_a = &laddr->ifa->address.sin6;
1023 sin6->sin6_addr = sin_a->sin6_addr;
1029 SCTP_FREE_SONAME(sin6);
1030 SCTP_INP_RUNLOCK(inp);
1031 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1035 SCTP_INP_RUNLOCK(inp);
1036 /* Scoping things for v6 */
1037 if ((error = sa6_recoverscope(sin6)) != 0) {
1038 SCTP_FREE_SONAME(sin6);
1041 (*addr) = (struct sockaddr *)sin6;
1046 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1048 struct sockaddr_in6 *sin6;
1050 struct sockaddr_in6 *sin_a6;
1051 struct sctp_inpcb *inp;
1052 struct sctp_tcb *stcb;
1053 struct sctp_nets *net;
1056 /* Do the malloc first in case it blocks. */
1057 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1060 sin6->sin6_family = AF_INET6;
1061 sin6->sin6_len = sizeof(*sin6);
1063 inp = (struct sctp_inpcb *)so->so_pcb;
1064 if ((inp == NULL) ||
1065 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1066 /* UDP type and listeners will drop out here */
1067 SCTP_FREE_SONAME(sin6);
1068 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1071 SCTP_INP_RLOCK(inp);
1072 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1074 SCTP_TCB_LOCK(stcb);
1076 SCTP_INP_RUNLOCK(inp);
1078 SCTP_FREE_SONAME(sin6);
1079 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1080 return (ECONNRESET);
1083 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1084 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1085 if (sin_a6->sin6_family == AF_INET6) {
1087 sin6->sin6_port = stcb->rport;
1088 sin6->sin6_addr = sin_a6->sin6_addr;
1092 SCTP_TCB_UNLOCK(stcb);
1094 /* No IPv4 address */
1095 SCTP_FREE_SONAME(sin6);
1096 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1099 if ((error = sa6_recoverscope(sin6)) != 0) {
1100 SCTP_FREE_SONAME(sin6);
1101 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1104 *addr = (struct sockaddr *)sin6;
1109 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1111 struct inpcb *inp = sotoinpcb(so);
1115 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1119 /* allow v6 addresses precedence */
1120 error = sctp6_getaddr(so, nam);
1123 struct sockaddr_in6 *sin6;
1125 /* try v4 next if v6 failed */
1126 error = sctp_ingetaddr(so, nam);
1130 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1132 SCTP_FREE_SONAME(*nam);
1135 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1136 SCTP_FREE_SONAME(*nam);
1137 *nam = (struct sockaddr *)sin6;
1145 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1147 struct inpcb *inp = sotoinpcb(so);
1151 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1155 /* allow v6 addresses precedence */
1156 error = sctp6_peeraddr(so, nam);
1159 struct sockaddr_in6 *sin6;
1161 /* try v4 next if v6 failed */
1162 error = sctp_peeraddr(so, nam);
1166 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1168 SCTP_FREE_SONAME(*nam);
1171 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1172 SCTP_FREE_SONAME(*nam);
1173 *nam = (struct sockaddr *)sin6;
1179 struct pr_usrreqs sctp6_usrreqs = {
1180 .pru_abort = sctp6_abort,
1181 .pru_accept = sctp_accept,
1182 .pru_attach = sctp6_attach,
1183 .pru_bind = sctp6_bind,
1184 .pru_connect = sctp6_connect,
1185 .pru_control = in6_control,
1186 .pru_close = sctp6_close,
1187 .pru_detach = sctp6_close,
1188 .pru_sopoll = sopoll_generic,
1189 .pru_flush = sctp_flush,
1190 .pru_disconnect = sctp6_disconnect,
1191 .pru_listen = sctp_listen,
1192 .pru_peeraddr = sctp6_getpeeraddr,
1193 .pru_send = sctp6_send,
1194 .pru_shutdown = sctp_shutdown,
1195 .pru_sockaddr = sctp6_in6getaddr,
1196 .pru_sosend = sctp_sosend,
1197 .pru_soreceive = sctp_soreceive