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(struct ip6ctlparam *ip6cp)
253 struct sctp_inpcb *inp;
254 struct sctp_tcb *stcb;
255 struct sctp_nets *net;
257 struct sockaddr_in6 src, dst;
259 if (icmp6_errmap(ip6cp->ip6c_icmp6) == 0) {
264 * Check if we can safely examine the ports and the verification tag
265 * of the SCTP common header.
267 if (ip6cp->ip6c_m->m_pkthdr.len <
268 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
272 /* Copy out the port numbers and the verification tag. */
273 memset(&sh, 0, sizeof(sh));
274 m_copydata(ip6cp->ip6c_m,
276 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
278 memset(&src, 0, sizeof(struct sockaddr_in6));
279 src.sin6_family = AF_INET6;
280 src.sin6_len = sizeof(struct sockaddr_in6);
281 src.sin6_port = sh.src_port;
282 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
283 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
286 memset(&dst, 0, sizeof(struct sockaddr_in6));
287 dst.sin6_family = AF_INET6;
288 dst.sin6_len = sizeof(struct sockaddr_in6);
289 dst.sin6_port = sh.dest_port;
290 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
291 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
296 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
297 (struct sockaddr *)&src,
298 &inp, &net, 1, SCTP_DEFAULT_VRFID);
299 if ((stcb != NULL) &&
302 /* Check the verification tag */
303 if (ntohl(sh.v_tag) != 0) {
305 * This must be the verification tag used for
306 * sending out packets. We don't consider packets
307 * reflecting the verification tag.
309 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
310 SCTP_TCB_UNLOCK(stcb);
314 if (ip6cp->ip6c_m->m_pkthdr.len >=
315 ip6cp->ip6c_off + sizeof(struct sctphdr) +
316 sizeof(struct sctp_chunkhdr) +
317 offsetof(struct sctp_init, a_rwnd)) {
319 * In this case we can check if we got an
320 * INIT chunk and if the initiate tag
323 uint32_t initiate_tag;
326 m_copydata(ip6cp->ip6c_m,
328 sizeof(struct sctphdr),
330 (caddr_t)&chunk_type);
331 m_copydata(ip6cp->ip6c_m,
333 sizeof(struct sctphdr) +
334 sizeof(struct sctp_chunkhdr),
336 (caddr_t)&initiate_tag);
337 if ((chunk_type != SCTP_INITIATION) ||
338 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
339 SCTP_TCB_UNLOCK(stcb);
343 SCTP_TCB_UNLOCK(stcb);
347 sctp6_notify(inp, stcb, net,
348 ip6cp->ip6c_icmp6->icmp6_type,
349 ip6cp->ip6c_icmp6->icmp6_code,
350 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
352 if ((stcb == NULL) && (inp != NULL)) {
353 /* reduce inp's ref-count */
355 SCTP_INP_DECR_REF(inp);
356 SCTP_INP_WUNLOCK(inp);
359 SCTP_TCB_UNLOCK(stcb);
365 * this routine can probably be collasped into the one in sctp_userreq.c
366 * since they do the same thing and now we lookup with a sockaddr
369 sctp6_getcred(SYSCTL_HANDLER_ARGS)
372 struct sockaddr_in6 addrs[2];
373 struct sctp_inpcb *inp;
374 struct sctp_nets *net;
375 struct sctp_tcb *stcb;
379 vrf_id = SCTP_DEFAULT_VRFID;
381 error = priv_check(req->td, PRIV_NETINET_GETCRED);
385 if (req->newlen != sizeof(addrs)) {
386 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
389 if (req->oldlen != sizeof(struct ucred)) {
390 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
393 error = SYSCTL_IN(req, addrs, sizeof(addrs));
397 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
399 &inp, &net, 1, vrf_id);
400 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
401 if ((inp != NULL) && (stcb == NULL)) {
402 /* reduce ref-count */
404 SCTP_INP_DECR_REF(inp);
407 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
411 SCTP_TCB_UNLOCK(stcb);
413 * We use the write lock here, only since in the error leg we need
414 * it. If we used RLOCK, then we would have to
415 * wlock/decr/unlock/rlock. Which in theory could create a hole.
416 * Better to use higher wlock.
420 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
422 SCTP_INP_WUNLOCK(inp);
425 cru2x(inp->sctp_socket->so_cred, &xuc);
426 SCTP_INP_WUNLOCK(inp);
427 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
432 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
433 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
434 0, 0, sctp6_getcred, "S,ucred",
435 "Get the ucred of a SCTP6 connection");
437 /* This is the same as the sctp_abort() could be made common */
439 sctp6_abort(struct socket *so)
441 struct epoch_tracker et;
442 struct sctp_inpcb *inp;
445 inp = (struct sctp_inpcb *)so->so_pcb;
447 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
452 flags = inp->sctp_flags;
453 #ifdef SCTP_LOG_CLOSING
454 sctp_log_closing(inp, NULL, 17);
456 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
457 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
458 #ifdef SCTP_LOG_CLOSING
459 sctp_log_closing(inp, NULL, 16);
461 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
462 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
464 SCTP_SB_CLEAR(so->so_snd);
466 * same for the rcv ones, they are only here for the
469 SCTP_SB_CLEAR(so->so_rcv);
470 /* Now null out the reference, we are completely detached. */
474 flags = inp->sctp_flags;
475 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
476 goto sctp_must_try_again;
484 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
487 struct sctp_inpcb *inp;
488 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
490 inp = (struct sctp_inpcb *)so->so_pcb;
492 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
496 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
497 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
501 error = sctp_inpcb_alloc(so, vrf_id);
504 inp = (struct sctp_inpcb *)so->so_pcb;
506 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
508 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
509 inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
510 inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
513 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
514 * socket as well, because the socket may be bound to an IPv6
515 * wildcard address, which may match an IPv4-mapped IPv6 address.
517 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
519 SCTP_INP_WUNLOCK(inp);
524 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
526 struct sctp_inpcb *inp;
530 inp = (struct sctp_inpcb *)so->so_pcb;
532 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
537 switch (addr->sa_family) {
540 if (addr->sa_len != sizeof(struct sockaddr_in)) {
541 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
548 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
549 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
555 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
559 vflagsav = inp->ip_inp.inp.inp_vflag;
560 inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
561 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
562 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
563 switch (addr->sa_family) {
566 /* binding v4 addr to v6 socket, so reset flags */
567 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
568 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
574 struct sockaddr_in6 *sin6_p;
576 sin6_p = (struct sockaddr_in6 *)addr;
578 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
579 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
582 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
583 struct sockaddr_in sin;
585 in6_sin6_2_sin(&sin, sin6_p);
586 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
587 inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
588 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
598 } else if (addr != NULL) {
599 struct sockaddr_in6 *sin6_p;
601 /* IPV6_V6ONLY socket */
603 if (addr->sa_family == AF_INET) {
604 /* can't bind v4 addr to v6 only socket! */
605 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
610 sin6_p = (struct sockaddr_in6 *)addr;
612 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
613 /* can't bind v4-mapped addrs either! */
614 /* NOTE: we don't support SIIT */
615 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
620 error = sctp_inpcb_bind(so, addr, NULL, p);
623 inp->ip_inp.inp.inp_vflag = vflagsav;
628 sctp6_close(struct socket *so)
633 /* This could be made common with sctp_detach() since they are identical */
637 sctp6_disconnect(struct socket *so)
639 return (sctp_disconnect(so));
643 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
644 struct mbuf *control, struct thread *p);
647 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
648 struct mbuf *control, struct thread *p)
650 struct sctp_inpcb *inp;
653 struct sockaddr_in6 *sin6;
655 /* No SPL needed since sctp_output does this */
657 inp = (struct sctp_inpcb *)so->so_pcb;
660 SCTP_RELEASE_PKT(control);
664 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
668 * For the TCP model we may get a NULL addr, if we are a connected
671 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
678 SCTP_RELEASE_PKT(control);
681 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
682 return (EDESTADDRREQ);
684 switch (addr->sa_family) {
687 if (addr->sa_len != sizeof(struct sockaddr_in)) {
689 SCTP_RELEASE_PKT(control);
693 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
700 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
702 SCTP_RELEASE_PKT(control);
706 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
713 SCTP_RELEASE_PKT(control);
717 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
721 sin6 = (struct sockaddr_in6 *)addr;
722 if (SCTP_IPV6_V6ONLY(inp)) {
724 * if IPV6_V6ONLY flag, we discard datagrams destined to a
725 * v4 addr or v4-mapped addr
727 if (addr->sa_family == AF_INET) {
729 SCTP_RELEASE_PKT(control);
733 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
736 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
738 SCTP_RELEASE_PKT(control);
742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
746 if ((addr->sa_family == AF_INET6) &&
747 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 ((addr->sa_family == AF_INET6) &&
886 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
887 /* convert v4-mapped into v4 addr */
888 in6_sin6_2_sin(&store.sin, sin6);
892 /* Now do we connect? */
893 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
894 stcb = LIST_FIRST(&inp->sctp_asoc_list);
898 SCTP_INP_RUNLOCK(inp);
900 SCTP_INP_RUNLOCK(inp);
902 SCTP_INP_INCR_REF(inp);
903 SCTP_INP_WUNLOCK(inp);
904 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
907 SCTP_INP_DECR_REF(inp);
908 SCTP_INP_WUNLOCK(inp);
913 /* Already have or am bring up an association */
914 SCTP_ASOC_CREATE_UNLOCK(inp);
915 SCTP_TCB_UNLOCK(stcb);
916 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
919 /* We are GOOD to go */
920 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
921 inp->sctp_ep.pre_open_stream_count,
922 inp->sctp_ep.port, p,
923 SCTP_INITIALIZE_AUTH_PARAMS);
924 SCTP_ASOC_CREATE_UNLOCK(inp);
929 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
930 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
932 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
933 SCTP_TCB_UNLOCK(stcb);
939 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
941 struct sockaddr_in6 *sin6;
942 struct sctp_inpcb *inp;
944 struct sctp_ifa *sctp_ifa;
949 * Do the malloc first in case it blocks.
951 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
954 sin6->sin6_family = AF_INET6;
955 sin6->sin6_len = sizeof(*sin6);
957 inp = (struct sctp_inpcb *)so->so_pcb;
959 SCTP_FREE_SONAME(sin6);
960 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
964 sin6->sin6_port = inp->sctp_lport;
965 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
966 /* For the bound all case you get back 0 */
967 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
968 struct sctp_tcb *stcb;
969 struct sockaddr_in6 *sin_a6;
970 struct sctp_nets *net;
973 stcb = LIST_FIRST(&inp->sctp_asoc_list);
975 SCTP_INP_RUNLOCK(inp);
976 SCTP_FREE_SONAME(sin6);
977 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
982 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
983 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
985 /* this will make coverity happy */
988 if (sin_a6->sin6_family == AF_INET6) {
993 if ((!fnd) || (sin_a6 == NULL)) {
995 SCTP_INP_RUNLOCK(inp);
996 SCTP_FREE_SONAME(sin6);
997 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1000 vrf_id = inp->def_vrf_id;
1001 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1003 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1006 /* For the bound all case you get back 0 */
1007 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1010 /* Take the first IPv6 address in the list */
1011 struct sctp_laddr *laddr;
1014 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1015 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1016 struct sockaddr_in6 *sin_a;
1018 sin_a = &laddr->ifa->address.sin6;
1019 sin6->sin6_addr = sin_a->sin6_addr;
1025 SCTP_FREE_SONAME(sin6);
1026 SCTP_INP_RUNLOCK(inp);
1027 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1031 SCTP_INP_RUNLOCK(inp);
1032 /* Scoping things for v6 */
1033 if ((error = sa6_recoverscope(sin6)) != 0) {
1034 SCTP_FREE_SONAME(sin6);
1037 (*addr) = (struct sockaddr *)sin6;
1042 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1044 struct sockaddr_in6 *sin6;
1046 struct sockaddr_in6 *sin_a6;
1047 struct sctp_inpcb *inp;
1048 struct sctp_tcb *stcb;
1049 struct sctp_nets *net;
1052 /* Do the malloc first in case it blocks. */
1053 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1056 sin6->sin6_family = AF_INET6;
1057 sin6->sin6_len = sizeof(*sin6);
1059 inp = (struct sctp_inpcb *)so->so_pcb;
1060 if ((inp == NULL) ||
1061 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1062 /* UDP type and listeners will drop out here */
1063 SCTP_FREE_SONAME(sin6);
1064 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1067 SCTP_INP_RLOCK(inp);
1068 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1070 SCTP_TCB_LOCK(stcb);
1072 SCTP_INP_RUNLOCK(inp);
1074 SCTP_FREE_SONAME(sin6);
1075 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1076 return (ECONNRESET);
1079 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1080 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1081 if (sin_a6->sin6_family == AF_INET6) {
1083 sin6->sin6_port = stcb->rport;
1084 sin6->sin6_addr = sin_a6->sin6_addr;
1088 SCTP_TCB_UNLOCK(stcb);
1090 /* No IPv4 address */
1091 SCTP_FREE_SONAME(sin6);
1092 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1095 if ((error = sa6_recoverscope(sin6)) != 0) {
1096 SCTP_FREE_SONAME(sin6);
1097 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1100 *addr = (struct sockaddr *)sin6;
1105 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1107 struct inpcb *inp = sotoinpcb(so);
1111 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1115 /* allow v6 addresses precedence */
1116 error = sctp6_getaddr(so, nam);
1119 struct sockaddr_in6 *sin6;
1121 /* try v4 next if v6 failed */
1122 error = sctp_ingetaddr(so, nam);
1126 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1128 SCTP_FREE_SONAME(*nam);
1131 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1132 SCTP_FREE_SONAME(*nam);
1133 *nam = (struct sockaddr *)sin6;
1140 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1142 struct inpcb *inp = sotoinpcb(so);
1146 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1150 /* allow v6 addresses precedence */
1151 error = sctp6_peeraddr(so, nam);
1154 struct sockaddr_in6 *sin6;
1156 /* try v4 next if v6 failed */
1157 error = sctp_peeraddr(so, nam);
1161 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1163 SCTP_FREE_SONAME(*nam);
1166 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1167 SCTP_FREE_SONAME(*nam);
1168 *nam = (struct sockaddr *)sin6;
1174 #define SCTP6_PROTOSW \
1175 .pr_protocol = IPPROTO_SCTP, \
1176 .pr_ctloutput = sctp_ctloutput, \
1177 .pr_abort = sctp6_abort, \
1178 .pr_accept = sctp_accept, \
1179 .pr_attach = sctp6_attach, \
1180 .pr_bind = sctp6_bind, \
1181 .pr_connect = sctp6_connect, \
1182 .pr_control = in6_control, \
1183 .pr_close = sctp6_close, \
1184 .pr_detach = sctp6_close, \
1185 .pr_sopoll = sopoll_generic, \
1186 .pr_flush = sctp_flush, \
1187 .pr_disconnect = sctp6_disconnect, \
1188 .pr_listen = sctp_listen, \
1189 .pr_peeraddr = sctp6_getpeeraddr, \
1190 .pr_send = sctp6_send, \
1191 .pr_shutdown = sctp_shutdown, \
1192 .pr_sockaddr = sctp6_in6getaddr, \
1193 .pr_sosend = sctp_sosend, \
1194 .pr_soreceive = sctp_soreceive
1196 struct protosw sctp6_seqpacket_protosw = {
1197 .pr_type = SOCK_SEQPACKET,
1198 .pr_flags = PR_WANTRCVD,
1202 struct protosw sctp6_stream_protosw = {
1203 .pr_type = SOCK_STREAM,
1204 .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD,