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 = IPV6_TRAFFIC_CLASS(ip6);
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);
168 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
170 return (sctp6_input_with_port(i_pak, offp, 0));
174 sctp6_notify(struct sctp_inpcb *inp,
175 struct sctp_tcb *stcb,
176 struct sctp_nets *net,
183 switch (icmp6_type) {
184 case ICMP6_DST_UNREACH:
185 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
186 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
187 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
188 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
189 /* Mark the net unreachable. */
190 if (net->dest_state & SCTP_ADDR_REACHABLE) {
191 /* Ok that destination is not reachable */
192 net->dest_state &= ~SCTP_ADDR_REACHABLE;
193 net->dest_state &= ~SCTP_ADDR_PF;
194 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
195 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
198 SCTP_TCB_UNLOCK(stcb);
200 case ICMP6_PARAM_PROB:
201 /* Treat it like an ABORT. */
202 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
203 sctp_abort_notification(stcb, true, false, 0, NULL, SCTP_SO_NOT_LOCKED);
204 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
205 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
207 SCTP_TCB_UNLOCK(stcb);
210 case ICMP6_PACKET_TOO_BIG:
211 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
212 SCTP_TCB_UNLOCK(stcb);
215 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
217 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
218 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
222 /* Update the path MTU. */
224 next_mtu -= sizeof(struct udphdr);
226 if (net->mtu > next_mtu) {
229 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
231 sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
234 /* Update the association MTU */
235 if (stcb->asoc.smallest_mtu > next_mtu) {
236 sctp_pathmtu_adjustment(stcb, next_mtu, true);
238 /* Finally, start the PMTU timer if it was running before. */
240 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
242 SCTP_TCB_UNLOCK(stcb);
245 SCTP_TCB_UNLOCK(stcb);
251 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
253 struct ip6ctlparam *ip6cp;
254 struct sctp_inpcb *inp;
255 struct sctp_tcb *stcb;
256 struct sctp_nets *net;
258 struct sockaddr_in6 src, dst;
260 if (pktdst->sa_family != AF_INET6 ||
261 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
265 if ((unsigned)cmd >= PRC_NCMDS) {
268 if (PRC_IS_REDIRECT(cmd)) {
270 } else if (inet6ctlerrmap[cmd] == 0) {
273 /* If the parameter is from icmp6, decode it. */
275 ip6cp = (struct ip6ctlparam *)d;
277 ip6cp = (struct ip6ctlparam *)NULL;
282 * XXX: We assume that when IPV6 is non NULL, M and OFF are
285 if (ip6cp->ip6c_m == NULL) {
290 * Check if we can safely examine the ports and the
291 * verification tag of the SCTP common header.
293 if (ip6cp->ip6c_m->m_pkthdr.len <
294 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
298 /* Copy out the port numbers and the verification tag. */
299 memset(&sh, 0, sizeof(sh));
300 m_copydata(ip6cp->ip6c_m,
302 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
304 memset(&src, 0, sizeof(struct sockaddr_in6));
305 src.sin6_family = AF_INET6;
306 src.sin6_len = sizeof(struct sockaddr_in6);
307 src.sin6_port = sh.src_port;
308 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
309 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
312 memset(&dst, 0, sizeof(struct sockaddr_in6));
313 dst.sin6_family = AF_INET6;
314 dst.sin6_len = sizeof(struct sockaddr_in6);
315 dst.sin6_port = sh.dest_port;
316 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
317 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
322 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
323 (struct sockaddr *)&src,
324 &inp, &net, 1, SCTP_DEFAULT_VRFID);
325 if ((stcb != NULL) &&
328 /* Check the verification tag */
329 if (ntohl(sh.v_tag) != 0) {
331 * This must be the verification tag used
332 * for sending out packets. We don't
333 * consider packets reflecting the
336 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
337 SCTP_TCB_UNLOCK(stcb);
341 if (ip6cp->ip6c_m->m_pkthdr.len >=
342 ip6cp->ip6c_off + sizeof(struct sctphdr) +
343 sizeof(struct sctp_chunkhdr) +
344 offsetof(struct sctp_init, a_rwnd)) {
346 * In this case we can check if we
347 * got an INIT chunk and if the
348 * initiate tag matches.
350 uint32_t initiate_tag;
353 m_copydata(ip6cp->ip6c_m,
355 sizeof(struct sctphdr),
357 (caddr_t)&chunk_type);
358 m_copydata(ip6cp->ip6c_m,
360 sizeof(struct sctphdr) +
361 sizeof(struct sctp_chunkhdr),
363 (caddr_t)&initiate_tag);
364 if ((chunk_type != SCTP_INITIATION) ||
365 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
366 SCTP_TCB_UNLOCK(stcb);
370 SCTP_TCB_UNLOCK(stcb);
374 sctp6_notify(inp, stcb, net,
375 ip6cp->ip6c_icmp6->icmp6_type,
376 ip6cp->ip6c_icmp6->icmp6_code,
377 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
379 if ((stcb == NULL) && (inp != NULL)) {
380 /* reduce inp's ref-count */
382 SCTP_INP_DECR_REF(inp);
383 SCTP_INP_WUNLOCK(inp);
386 SCTP_TCB_UNLOCK(stcb);
393 * this routine can probably be collasped into the one in sctp_userreq.c
394 * since they do the same thing and now we lookup with a sockaddr
397 sctp6_getcred(SYSCTL_HANDLER_ARGS)
400 struct sockaddr_in6 addrs[2];
401 struct sctp_inpcb *inp;
402 struct sctp_nets *net;
403 struct sctp_tcb *stcb;
407 vrf_id = SCTP_DEFAULT_VRFID;
409 error = priv_check(req->td, PRIV_NETINET_GETCRED);
413 if (req->newlen != sizeof(addrs)) {
414 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
417 if (req->oldlen != sizeof(struct ucred)) {
418 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
421 error = SYSCTL_IN(req, addrs, sizeof(addrs));
425 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
427 &inp, &net, 1, vrf_id);
428 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
429 if ((inp != NULL) && (stcb == NULL)) {
430 /* reduce ref-count */
432 SCTP_INP_DECR_REF(inp);
435 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
439 SCTP_TCB_UNLOCK(stcb);
441 * We use the write lock here, only since in the error leg we need
442 * it. If we used RLOCK, then we would have to
443 * wlock/decr/unlock/rlock. Which in theory could create a hole.
444 * Better to use higher wlock.
448 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
450 SCTP_INP_WUNLOCK(inp);
453 cru2x(inp->sctp_socket->so_cred, &xuc);
454 SCTP_INP_WUNLOCK(inp);
455 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
460 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
461 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
462 0, 0, sctp6_getcred, "S,ucred",
463 "Get the ucred of a SCTP6 connection");
465 /* This is the same as the sctp_abort() could be made common */
467 sctp6_abort(struct socket *so)
469 struct epoch_tracker et;
470 struct sctp_inpcb *inp;
473 inp = (struct sctp_inpcb *)so->so_pcb;
475 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
480 flags = inp->sctp_flags;
481 #ifdef SCTP_LOG_CLOSING
482 sctp_log_closing(inp, NULL, 17);
484 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
485 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
486 #ifdef SCTP_LOG_CLOSING
487 sctp_log_closing(inp, NULL, 16);
489 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
490 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
492 SCTP_SB_CLEAR(so->so_snd);
494 * same for the rcv ones, they are only here for the
497 SCTP_SB_CLEAR(so->so_rcv);
498 /* Now null out the reference, we are completely detached. */
502 flags = inp->sctp_flags;
503 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
504 goto sctp_must_try_again;
512 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
515 struct sctp_inpcb *inp;
516 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
518 inp = (struct sctp_inpcb *)so->so_pcb;
520 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
524 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
525 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
529 error = sctp_inpcb_alloc(so, vrf_id);
532 inp = (struct sctp_inpcb *)so->so_pcb;
534 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
536 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
537 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
538 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
541 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
542 * socket as well, because the socket may be bound to an IPv6
543 * wildcard address, which may match an IPv4-mapped IPv6 address.
545 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
547 SCTP_INP_WUNLOCK(inp);
552 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
554 struct sctp_inpcb *inp;
558 inp = (struct sctp_inpcb *)so->so_pcb;
560 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
565 switch (addr->sa_family) {
568 if (addr->sa_len != sizeof(struct sockaddr_in)) {
569 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
576 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
577 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
583 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
587 vflagsav = inp->ip_inp.inp.inp_vflag;
588 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
589 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
590 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
591 switch (addr->sa_family) {
594 /* binding v4 addr to v6 socket, so reset flags */
595 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
596 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
602 struct sockaddr_in6 *sin6_p;
604 sin6_p = (struct sockaddr_in6 *)addr;
606 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
607 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
610 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
611 struct sockaddr_in sin;
613 in6_sin6_2_sin(&sin, sin6_p);
614 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
615 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
616 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
626 } else if (addr != NULL) {
627 struct sockaddr_in6 *sin6_p;
629 /* IPV6_V6ONLY socket */
631 if (addr->sa_family == AF_INET) {
632 /* can't bind v4 addr to v6 only socket! */
633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
638 sin6_p = (struct sockaddr_in6 *)addr;
640 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
641 /* can't bind v4-mapped addrs either! */
642 /* NOTE: we don't support SIIT */
643 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
648 error = sctp_inpcb_bind(so, addr, NULL, p);
651 inp->ip_inp.inp.inp_vflag = vflagsav;
656 sctp6_close(struct socket *so)
661 /* This could be made common with sctp_detach() since they are identical */
665 sctp6_disconnect(struct socket *so)
667 return (sctp_disconnect(so));
671 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
672 struct mbuf *control, struct thread *p);
675 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
676 struct mbuf *control, struct thread *p)
678 struct sctp_inpcb *inp;
681 struct sockaddr_in6 *sin6;
683 /* No SPL needed since sctp_output does this */
685 inp = (struct sctp_inpcb *)so->so_pcb;
688 SCTP_RELEASE_PKT(control);
692 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
696 * For the TCP model we may get a NULL addr, if we are a connected
699 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
706 SCTP_RELEASE_PKT(control);
709 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
710 return (EDESTADDRREQ);
712 switch (addr->sa_family) {
715 if (addr->sa_len != sizeof(struct sockaddr_in)) {
717 SCTP_RELEASE_PKT(control);
721 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
728 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
730 SCTP_RELEASE_PKT(control);
734 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
741 SCTP_RELEASE_PKT(control);
745 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
749 sin6 = (struct sockaddr_in6 *)addr;
750 if (SCTP_IPV6_V6ONLY(inp)) {
752 * if IPV6_V6ONLY flag, we discard datagrams destined to a
753 * v4 addr or v4-mapped addr
755 if (addr->sa_family == AF_INET) {
757 SCTP_RELEASE_PKT(control);
761 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
764 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
766 SCTP_RELEASE_PKT(control);
770 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
774 if ((addr->sa_family == AF_INET6) &&
775 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
776 struct sockaddr_in sin;
778 /* convert v4-mapped into v4 addr and send */
779 in6_sin6_2_sin(&sin, sin6);
780 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
784 /* now what about control */
787 SCTP_PRINTF("huh? control set?\n");
788 SCTP_RELEASE_PKT(inp->control);
791 inp->control = control;
795 SCTP_BUF_NEXT(inp->pkt_last) = m;
798 inp->pkt_last = inp->pkt = m;
801 /* FreeBSD and MacOSX uses a flag passed */
802 ((flags & PRUS_MORETOCOME) == 0)
805 * note with the current version this code will only be used
806 * by OpenBSD, NetBSD and FreeBSD have methods for
807 * re-defining sosend() to use sctp_sosend(). One can
808 * optionaly switch back to this code (by changing back the
809 * defininitions but this is not advisable.
811 struct epoch_tracker et;
815 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
826 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
828 struct epoch_tracker et;
831 struct sctp_inpcb *inp;
832 struct sctp_tcb *stcb;
834 struct sockaddr_in6 *sin6;
835 union sctp_sockstore store;
838 inp = (struct sctp_inpcb *)so->so_pcb;
840 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
841 return (ECONNRESET); /* I made the same as TCP since we are
845 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
848 switch (addr->sa_family) {
851 if (addr->sa_len != sizeof(struct sockaddr_in)) {
852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
859 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
860 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
866 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
870 vrf_id = inp->def_vrf_id;
871 SCTP_ASOC_CREATE_LOCK(inp);
873 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
874 SCTP_PCB_FLAGS_UNBOUND) {
875 /* Bind a ephemeral port */
876 SCTP_INP_RUNLOCK(inp);
877 error = sctp6_bind(so, NULL, p);
879 SCTP_ASOC_CREATE_UNLOCK(inp);
885 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
886 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
887 /* We are already connected AND the TCP model */
888 SCTP_INP_RUNLOCK(inp);
889 SCTP_ASOC_CREATE_UNLOCK(inp);
890 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
894 sin6 = (struct sockaddr_in6 *)addr;
895 if (SCTP_IPV6_V6ONLY(inp)) {
897 * if IPV6_V6ONLY flag, ignore connections destined to a v4
898 * addr or v4-mapped addr
900 if (addr->sa_family == AF_INET) {
901 SCTP_INP_RUNLOCK(inp);
902 SCTP_ASOC_CREATE_UNLOCK(inp);
903 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
906 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
907 SCTP_INP_RUNLOCK(inp);
908 SCTP_ASOC_CREATE_UNLOCK(inp);
909 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
913 if ((addr->sa_family == AF_INET6) &&
914 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
915 /* convert v4-mapped into v4 addr */
916 in6_sin6_2_sin(&store.sin, sin6);
920 /* Now do we connect? */
921 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
922 stcb = LIST_FIRST(&inp->sctp_asoc_list);
926 SCTP_INP_RUNLOCK(inp);
928 SCTP_INP_RUNLOCK(inp);
930 SCTP_INP_INCR_REF(inp);
931 SCTP_INP_WUNLOCK(inp);
932 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
935 SCTP_INP_DECR_REF(inp);
936 SCTP_INP_WUNLOCK(inp);
941 /* Already have or am bring up an association */
942 SCTP_ASOC_CREATE_UNLOCK(inp);
943 SCTP_TCB_UNLOCK(stcb);
944 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
947 /* We are GOOD to go */
948 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
949 inp->sctp_ep.pre_open_stream_count,
950 inp->sctp_ep.port, p,
951 SCTP_INITIALIZE_AUTH_PARAMS);
952 SCTP_ASOC_CREATE_UNLOCK(inp);
957 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
958 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
960 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
961 SCTP_TCB_UNLOCK(stcb);
967 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
969 struct sockaddr_in6 *sin6;
970 struct sctp_inpcb *inp;
972 struct sctp_ifa *sctp_ifa;
977 * Do the malloc first in case it blocks.
979 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
982 sin6->sin6_family = AF_INET6;
983 sin6->sin6_len = sizeof(*sin6);
985 inp = (struct sctp_inpcb *)so->so_pcb;
987 SCTP_FREE_SONAME(sin6);
988 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
992 sin6->sin6_port = inp->sctp_lport;
993 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
994 /* For the bound all case you get back 0 */
995 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
996 struct sctp_tcb *stcb;
997 struct sockaddr_in6 *sin_a6;
998 struct sctp_nets *net;
1001 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1003 SCTP_INP_RUNLOCK(inp);
1004 SCTP_FREE_SONAME(sin6);
1005 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1010 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1011 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1013 /* this will make coverity happy */
1016 if (sin_a6->sin6_family == AF_INET6) {
1021 if ((!fnd) || (sin_a6 == NULL)) {
1023 SCTP_INP_RUNLOCK(inp);
1024 SCTP_FREE_SONAME(sin6);
1025 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1028 vrf_id = inp->def_vrf_id;
1029 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1031 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1034 /* For the bound all case you get back 0 */
1035 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1038 /* Take the first IPv6 address in the list */
1039 struct sctp_laddr *laddr;
1042 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1043 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1044 struct sockaddr_in6 *sin_a;
1046 sin_a = &laddr->ifa->address.sin6;
1047 sin6->sin6_addr = sin_a->sin6_addr;
1053 SCTP_FREE_SONAME(sin6);
1054 SCTP_INP_RUNLOCK(inp);
1055 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1059 SCTP_INP_RUNLOCK(inp);
1060 /* Scoping things for v6 */
1061 if ((error = sa6_recoverscope(sin6)) != 0) {
1062 SCTP_FREE_SONAME(sin6);
1065 (*addr) = (struct sockaddr *)sin6;
1070 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1072 struct sockaddr_in6 *sin6;
1074 struct sockaddr_in6 *sin_a6;
1075 struct sctp_inpcb *inp;
1076 struct sctp_tcb *stcb;
1077 struct sctp_nets *net;
1080 /* Do the malloc first in case it blocks. */
1081 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1084 sin6->sin6_family = AF_INET6;
1085 sin6->sin6_len = sizeof(*sin6);
1087 inp = (struct sctp_inpcb *)so->so_pcb;
1088 if ((inp == NULL) ||
1089 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1090 /* UDP type and listeners will drop out here */
1091 SCTP_FREE_SONAME(sin6);
1092 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1095 SCTP_INP_RLOCK(inp);
1096 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1098 SCTP_TCB_LOCK(stcb);
1100 SCTP_INP_RUNLOCK(inp);
1102 SCTP_FREE_SONAME(sin6);
1103 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1104 return (ECONNRESET);
1107 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1108 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1109 if (sin_a6->sin6_family == AF_INET6) {
1111 sin6->sin6_port = stcb->rport;
1112 sin6->sin6_addr = sin_a6->sin6_addr;
1116 SCTP_TCB_UNLOCK(stcb);
1118 /* No IPv4 address */
1119 SCTP_FREE_SONAME(sin6);
1120 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1123 if ((error = sa6_recoverscope(sin6)) != 0) {
1124 SCTP_FREE_SONAME(sin6);
1125 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1128 *addr = (struct sockaddr *)sin6;
1133 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1135 struct inpcb *inp = sotoinpcb(so);
1139 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1143 /* allow v6 addresses precedence */
1144 error = sctp6_getaddr(so, nam);
1147 struct sockaddr_in6 *sin6;
1149 /* try v4 next if v6 failed */
1150 error = sctp_ingetaddr(so, nam);
1154 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1156 SCTP_FREE_SONAME(*nam);
1159 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1160 SCTP_FREE_SONAME(*nam);
1161 *nam = (struct sockaddr *)sin6;
1168 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1170 struct inpcb *inp = sotoinpcb(so);
1174 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1178 /* allow v6 addresses precedence */
1179 error = sctp6_peeraddr(so, nam);
1182 struct sockaddr_in6 *sin6;
1184 /* try v4 next if v6 failed */
1185 error = sctp_peeraddr(so, nam);
1189 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1191 SCTP_FREE_SONAME(*nam);
1194 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1195 SCTP_FREE_SONAME(*nam);
1196 *nam = (struct sockaddr *)sin6;
1202 struct pr_usrreqs sctp6_usrreqs = {
1203 .pru_abort = sctp6_abort,
1204 .pru_accept = sctp_accept,
1205 .pru_attach = sctp6_attach,
1206 .pru_bind = sctp6_bind,
1207 .pru_connect = sctp6_connect,
1208 .pru_control = in6_control,
1209 .pru_close = sctp6_close,
1210 .pru_detach = sctp6_close,
1211 .pru_sopoll = sopoll_generic,
1212 .pru_flush = sctp_flush,
1213 .pru_disconnect = sctp6_disconnect,
1214 .pru_listen = sctp_listen,
1215 .pru_peeraddr = sctp6_getpeeraddr,
1216 .pru_send = sctp6_send,
1217 .pru_shutdown = sctp_shutdown,
1218 .pru_sockaddr = sctp6_in6getaddr,
1219 .pru_sosend = sctp_sosend,
1220 .pru_soreceive = sctp_soreceive