2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet6/sctp6_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_output.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #include <netinet/sctp_crc32.h>
55 #include <netinet/icmp6.h>
56 #include <netinet/udp.h>
59 #include <netipsec/ipsec.h>
60 #include <netipsec/ipsec6.h>
63 extern struct protosw inetsw[];
66 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
72 struct sockaddr_in6 src, dst;
75 struct sctp_chunkhdr *ch;
77 #if !defined(SCTP_WITH_NO_CSUM)
85 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
86 SCTP_RELEASE_PKT(*i_pak);
87 return (IPPROTO_DONE);
89 m = SCTP_HEADER_TO_CHAIN(*i_pak);
90 #ifdef SCTP_MBUF_LOGGING
91 /* Log in any input mbufs */
92 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
93 sctp_log_mbc(m, SCTP_MBUF_INPUT);
96 #ifdef SCTP_PACKET_LOGGING
97 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
101 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
102 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
104 if_name(m->m_pkthdr.rcvif),
105 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
106 mflowid = m->m_pkthdr.flowid;
107 mflowtype = M_HASHTYPE_GET(m);
108 fibnum = M_GETFIB(m);
109 SCTP_STAT_INCR(sctps_recvpackets);
110 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
111 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
112 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
113 ip6 = mtod(m, struct ip6_hdr *);
114 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
115 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
117 SCTP_STAT_INCR(sctps_hdrops);
118 return (IPPROTO_DONE);
120 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
121 offset -= sizeof(struct sctp_chunkhdr);
122 memset(&src, 0, sizeof(struct sockaddr_in6));
123 src.sin6_family = AF_INET6;
124 src.sin6_len = sizeof(struct sockaddr_in6);
125 src.sin6_port = sh->src_port;
126 src.sin6_addr = ip6->ip6_src;
127 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
130 memset(&dst, 0, sizeof(struct sockaddr_in6));
131 dst.sin6_family = AF_INET6;
132 dst.sin6_len = sizeof(struct sockaddr_in6);
133 dst.sin6_port = sh->dest_port;
134 dst.sin6_addr = ip6->ip6_dst;
135 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
138 length = ntohs(ip6->ip6_plen) + iphlen;
139 /* Validate mbuf chain length with IP payload length. */
140 if (SCTP_HEADER_LEN(m) != length) {
141 SCTPDBG(SCTP_DEBUG_INPUT1,
142 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
143 SCTP_STAT_INCR(sctps_hdrops);
146 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
149 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
150 #if defined(SCTP_WITH_NO_CSUM)
151 SCTP_STAT_INCR(sctps_recvnocrc);
153 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
154 SCTP_STAT_INCR(sctps_recvhwcrc);
157 SCTP_STAT_INCR(sctps_recvswcrc);
161 sctp_common_input_processing(&m, iphlen, offset, length,
162 (struct sockaddr *)&src,
163 (struct sockaddr *)&dst,
165 #if !defined(SCTP_WITH_NO_CSUM)
169 mflowtype, mflowid, fibnum,
175 return (IPPROTO_DONE);
180 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
182 return (sctp6_input_with_port(i_pak, offp, 0));
186 sctp6_notify(struct sctp_inpcb *inp,
187 struct sctp_tcb *stcb,
188 struct sctp_nets *net,
193 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
198 switch (icmp6_type) {
199 case ICMP6_DST_UNREACH:
200 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
201 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
202 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
203 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
204 /* Mark the net unreachable. */
205 if (net->dest_state & SCTP_ADDR_REACHABLE) {
206 /* Ok that destination is not reachable */
207 net->dest_state &= ~SCTP_ADDR_REACHABLE;
208 net->dest_state &= ~SCTP_ADDR_PF;
209 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
210 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
213 SCTP_TCB_UNLOCK(stcb);
215 case ICMP6_PARAM_PROB:
216 /* Treat it like an ABORT. */
217 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
218 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
219 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
220 so = SCTP_INP_SO(inp);
221 atomic_add_int(&stcb->asoc.refcnt, 1);
222 SCTP_TCB_UNLOCK(stcb);
223 SCTP_SOCKET_LOCK(so, 1);
225 atomic_subtract_int(&stcb->asoc.refcnt, 1);
227 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
228 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
229 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
230 SCTP_SOCKET_UNLOCK(so, 1);
233 SCTP_TCB_UNLOCK(stcb);
236 case ICMP6_PACKET_TOO_BIG:
237 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
239 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
240 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
244 /* Update the path MTU. */
245 if (net->mtu > next_mtu) {
248 net->mtu -= sizeof(struct udphdr);
251 /* Update the association MTU */
252 if (stcb->asoc.smallest_mtu > next_mtu) {
253 sctp_pathmtu_adjustment(stcb, next_mtu);
255 /* Finally, start the PMTU timer if it was running before. */
257 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
259 SCTP_TCB_UNLOCK(stcb);
262 SCTP_TCB_UNLOCK(stcb);
268 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
270 struct ip6ctlparam *ip6cp;
271 struct sctp_inpcb *inp;
272 struct sctp_tcb *stcb;
273 struct sctp_nets *net;
275 struct sockaddr_in6 src, dst;
277 if (pktdst->sa_family != AF_INET6 ||
278 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
281 if ((unsigned)cmd >= PRC_NCMDS) {
284 if (PRC_IS_REDIRECT(cmd)) {
286 } else if (inet6ctlerrmap[cmd] == 0) {
289 /* If the parameter is from icmp6, decode it. */
291 ip6cp = (struct ip6ctlparam *)d;
293 ip6cp = (struct ip6ctlparam *)NULL;
298 * XXX: We assume that when IPV6 is non NULL, M and OFF are
301 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))) {
312 /* Copy out the port numbers and the verification tag. */
313 bzero(&sh, sizeof(sh));
314 m_copydata(ip6cp->ip6c_m,
316 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
318 memset(&src, 0, sizeof(struct sockaddr_in6));
319 src.sin6_family = AF_INET6;
320 src.sin6_len = sizeof(struct sockaddr_in6);
321 src.sin6_port = sh.src_port;
322 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
323 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
326 memset(&dst, 0, sizeof(struct sockaddr_in6));
327 dst.sin6_family = AF_INET6;
328 dst.sin6_len = sizeof(struct sockaddr_in6);
329 dst.sin6_port = sh.dest_port;
330 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
331 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
336 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
337 (struct sockaddr *)&src,
338 &inp, &net, 1, SCTP_DEFAULT_VRFID);
339 if ((stcb != NULL) &&
342 /* Check the verification tag */
343 if (ntohl(sh.v_tag) != 0) {
345 * This must be the verification tag used
346 * for sending out packets. We don't
347 * consider packets reflecting the
350 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
351 SCTP_TCB_UNLOCK(stcb);
355 if (ip6cp->ip6c_m->m_pkthdr.len >=
356 ip6cp->ip6c_off + sizeof(struct sctphdr) +
357 sizeof(struct sctp_chunkhdr) +
358 offsetof(struct sctp_init, a_rwnd)) {
360 * In this case we can check if we
361 * got an INIT chunk and if the
362 * initiate tag matches.
364 uint32_t initiate_tag;
367 m_copydata(ip6cp->ip6c_m,
369 sizeof(struct sctphdr),
371 (caddr_t)&chunk_type);
372 m_copydata(ip6cp->ip6c_m,
374 sizeof(struct sctphdr) +
375 sizeof(struct sctp_chunkhdr),
377 (caddr_t)&initiate_tag);
378 if ((chunk_type != SCTP_INITIATION) ||
379 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
380 SCTP_TCB_UNLOCK(stcb);
384 SCTP_TCB_UNLOCK(stcb);
388 sctp6_notify(inp, stcb, net,
389 ip6cp->ip6c_icmp6->icmp6_type,
390 ip6cp->ip6c_icmp6->icmp6_code,
391 (uint16_t)ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
393 if ((stcb == NULL) && (inp != NULL)) {
394 /* reduce inp's ref-count */
396 SCTP_INP_DECR_REF(inp);
397 SCTP_INP_WUNLOCK(inp);
400 SCTP_TCB_UNLOCK(stcb);
407 * this routine can probably be collasped into the one in sctp_userreq.c
408 * since they do the same thing and now we lookup with a sockaddr
411 sctp6_getcred(SYSCTL_HANDLER_ARGS)
414 struct sockaddr_in6 addrs[2];
415 struct sctp_inpcb *inp;
416 struct sctp_nets *net;
417 struct sctp_tcb *stcb;
421 vrf_id = SCTP_DEFAULT_VRFID;
423 error = priv_check(req->td, PRIV_NETINET_GETCRED);
427 if (req->newlen != sizeof(addrs)) {
428 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
431 if (req->oldlen != sizeof(struct ucred)) {
432 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
435 error = SYSCTL_IN(req, addrs, sizeof(addrs));
439 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
441 &inp, &net, 1, vrf_id);
442 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
443 if ((inp != NULL) && (stcb == NULL)) {
444 /* reduce ref-count */
446 SCTP_INP_DECR_REF(inp);
449 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
453 SCTP_TCB_UNLOCK(stcb);
455 * We use the write lock here, only since in the error leg we need
456 * it. If we used RLOCK, then we would have to
457 * wlock/decr/unlock/rlock. Which in theory could create a hole.
458 * Better to use higher wlock.
462 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
464 SCTP_INP_WUNLOCK(inp);
467 cru2x(inp->sctp_socket->so_cred, &xuc);
468 SCTP_INP_WUNLOCK(inp);
469 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
474 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
476 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
479 /* This is the same as the sctp_abort() could be made common */
481 sctp6_abort(struct socket *so)
483 struct sctp_inpcb *inp;
486 inp = (struct sctp_inpcb *)so->so_pcb;
488 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
492 flags = inp->sctp_flags;
493 #ifdef SCTP_LOG_CLOSING
494 sctp_log_closing(inp, NULL, 17);
496 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
497 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
498 #ifdef SCTP_LOG_CLOSING
499 sctp_log_closing(inp, NULL, 16);
501 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
502 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
504 SCTP_SB_CLEAR(so->so_snd);
506 * same for the rcv ones, they are only here for the
509 SCTP_SB_CLEAR(so->so_rcv);
510 /* Now null out the reference, we are completely detached. */
514 flags = inp->sctp_flags;
515 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
516 goto sctp_must_try_again;
523 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
527 struct sctp_inpcb *inp;
528 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
530 inp = (struct sctp_inpcb *)so->so_pcb;
532 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
535 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
536 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
540 error = sctp_inpcb_alloc(so, vrf_id);
543 inp = (struct sctp_inpcb *)so->so_pcb;
545 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
546 inp6 = (struct in6pcb *)inp;
548 inp6->inp_vflag |= INP_IPV6;
549 inp6->in6p_hops = -1; /* use kernel default */
550 inp6->in6p_cksum = -1; /* just to be sure */
553 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
554 * socket as well, because the socket may be bound to an IPv6
555 * wildcard address, which may match an IPv4-mapped IPv6 address.
557 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
560 * Hmm what about the IPSEC stuff that is missing here but in
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);
580 switch (addr->sa_family) {
583 if (addr->sa_len != sizeof(struct sockaddr_in)) {
584 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
591 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
602 inp6 = (struct in6pcb *)inp;
603 inp6->inp_vflag &= ~INP_IPV4;
604 inp6->inp_vflag |= INP_IPV6;
605 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
606 switch (addr->sa_family) {
609 /* binding v4 addr to v6 socket, so reset flags */
610 inp6->inp_vflag |= INP_IPV4;
611 inp6->inp_vflag &= ~INP_IPV6;
617 struct sockaddr_in6 *sin6_p;
619 sin6_p = (struct sockaddr_in6 *)addr;
621 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
622 inp6->inp_vflag |= INP_IPV4;
625 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
626 struct sockaddr_in sin;
628 in6_sin6_2_sin(&sin, sin6_p);
629 inp6->inp_vflag |= INP_IPV4;
630 inp6->inp_vflag &= ~INP_IPV6;
631 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
641 } else if (addr != NULL) {
642 struct sockaddr_in6 *sin6_p;
644 /* IPV6_V6ONLY socket */
646 if (addr->sa_family == AF_INET) {
647 /* can't bind v4 addr to v6 only socket! */
648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
652 sin6_p = (struct sockaddr_in6 *)addr;
654 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
655 /* can't bind v4-mapped addrs either! */
656 /* NOTE: we don't support SIIT */
657 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
661 error = sctp_inpcb_bind(so, addr, NULL, p);
667 sctp6_close(struct socket *so)
672 /* This could be made common with sctp_detach() since they are identical */
676 sctp6_disconnect(struct socket *so)
678 return (sctp_disconnect(so));
683 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
684 struct mbuf *control, struct thread *p);
688 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
689 struct mbuf *control, struct thread *p)
691 struct sctp_inpcb *inp;
695 struct sockaddr_in6 *sin6;
697 /* No SPL needed since sctp_output does this */
699 inp = (struct sctp_inpcb *)so->so_pcb;
702 SCTP_RELEASE_PKT(control);
706 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
709 inp6 = (struct in6pcb *)inp;
711 * For the TCP model we may get a NULL addr, if we are a connected
714 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
721 SCTP_RELEASE_PKT(control);
724 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
725 return (EDESTADDRREQ);
728 sin6 = (struct sockaddr_in6 *)addr;
729 if (SCTP_IPV6_V6ONLY(inp6)) {
731 * if IPV6_V6ONLY flag, we discard datagrams destined to a
732 * v4 addr or v4-mapped addr
734 if (addr->sa_family == AF_INET) {
735 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
738 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
739 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
743 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
744 struct sockaddr_in sin;
746 /* convert v4-mapped into v4 addr and send */
747 in6_sin6_2_sin(&sin, sin6);
748 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
752 /* now what about control */
755 SCTP_PRINTF("huh? control set?\n");
756 SCTP_RELEASE_PKT(inp->control);
759 inp->control = control;
763 SCTP_BUF_NEXT(inp->pkt_last) = m;
766 inp->pkt_last = inp->pkt = m;
769 /* FreeBSD and MacOSX uses a flag passed */
770 ((flags & PRUS_MORETOCOME) == 0)
773 * note with the current version this code will only be used
774 * by OpenBSD, NetBSD and FreeBSD have methods for
775 * re-defining sosend() to use sctp_sosend(). One can
776 * optionaly switch back to this code (by changing back the
777 * defininitions but this is not advisable.
781 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
791 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
795 struct sctp_inpcb *inp;
796 struct sctp_tcb *stcb;
799 struct sockaddr_in6 *sin6;
800 union sctp_sockstore store;
804 inp6 = (struct in6pcb *)so->so_pcb;
806 inp = (struct sctp_inpcb *)so->so_pcb;
808 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
809 return (ECONNRESET); /* I made the same as TCP since we are
813 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
816 switch (addr->sa_family) {
819 if (addr->sa_len != sizeof(struct sockaddr_in)) {
820 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
827 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
828 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
834 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
838 vrf_id = inp->def_vrf_id;
839 SCTP_ASOC_CREATE_LOCK(inp);
841 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
842 SCTP_PCB_FLAGS_UNBOUND) {
843 /* Bind a ephemeral port */
844 SCTP_INP_RUNLOCK(inp);
845 error = sctp6_bind(so, NULL, p);
847 SCTP_ASOC_CREATE_UNLOCK(inp);
853 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
854 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
855 /* We are already connected AND the TCP model */
856 SCTP_INP_RUNLOCK(inp);
857 SCTP_ASOC_CREATE_UNLOCK(inp);
858 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
862 sin6 = (struct sockaddr_in6 *)addr;
863 if (SCTP_IPV6_V6ONLY(inp6)) {
865 * if IPV6_V6ONLY flag, ignore connections destined to a v4
866 * addr or v4-mapped addr
868 if (addr->sa_family == AF_INET) {
869 SCTP_INP_RUNLOCK(inp);
870 SCTP_ASOC_CREATE_UNLOCK(inp);
871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
874 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
875 SCTP_INP_RUNLOCK(inp);
876 SCTP_ASOC_CREATE_UNLOCK(inp);
877 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
881 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
882 /* convert v4-mapped into v4 addr */
883 in6_sin6_2_sin(&store.sin, sin6);
887 /* Now do we connect? */
888 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
889 stcb = LIST_FIRST(&inp->sctp_asoc_list);
891 SCTP_TCB_UNLOCK(stcb);
893 SCTP_INP_RUNLOCK(inp);
895 SCTP_INP_RUNLOCK(inp);
897 SCTP_INP_INCR_REF(inp);
898 SCTP_INP_WUNLOCK(inp);
899 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
902 SCTP_INP_DECR_REF(inp);
903 SCTP_INP_WUNLOCK(inp);
908 /* Already have or am bring up an association */
909 SCTP_ASOC_CREATE_UNLOCK(inp);
910 SCTP_TCB_UNLOCK(stcb);
911 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
914 /* We are GOOD to go */
915 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
916 inp->sctp_ep.pre_open_stream_count,
917 inp->sctp_ep.port, p);
918 SCTP_ASOC_CREATE_UNLOCK(inp);
923 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
924 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
925 /* Set the connected flag so we can queue data */
928 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
929 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
931 /* initialize authentication parameters for the assoc */
932 sctp_initialize_auth_params(inp, stcb);
934 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
935 SCTP_TCB_UNLOCK(stcb);
940 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
942 struct sockaddr_in6 *sin6;
943 struct sctp_inpcb *inp;
945 struct sctp_ifa *sctp_ifa;
950 * Do the malloc first in case it blocks.
952 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
955 sin6->sin6_family = AF_INET6;
956 sin6->sin6_len = sizeof(*sin6);
958 inp = (struct sctp_inpcb *)so->so_pcb;
960 SCTP_FREE_SONAME(sin6);
961 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
965 sin6->sin6_port = inp->sctp_lport;
966 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
967 /* For the bound all case you get back 0 */
968 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
969 struct sctp_tcb *stcb;
970 struct sockaddr_in6 *sin_a6;
971 struct sctp_nets *net;
974 stcb = LIST_FIRST(&inp->sctp_asoc_list);
976 SCTP_INP_RUNLOCK(inp);
977 SCTP_FREE_SONAME(sin6);
978 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
983 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
984 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
986 /* this will make coverity happy */
989 if (sin_a6->sin6_family == AF_INET6) {
994 if ((!fnd) || (sin_a6 == NULL)) {
996 SCTP_INP_RUNLOCK(inp);
997 SCTP_FREE_SONAME(sin6);
998 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1001 vrf_id = inp->def_vrf_id;
1002 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1004 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1007 /* For the bound all case you get back 0 */
1008 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1011 /* Take the first IPv6 address in the list */
1012 struct sctp_laddr *laddr;
1015 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1016 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1017 struct sockaddr_in6 *sin_a;
1019 sin_a = &laddr->ifa->address.sin6;
1020 sin6->sin6_addr = sin_a->sin6_addr;
1026 SCTP_FREE_SONAME(sin6);
1027 SCTP_INP_RUNLOCK(inp);
1028 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1032 SCTP_INP_RUNLOCK(inp);
1033 /* Scoping things for v6 */
1034 if ((error = sa6_recoverscope(sin6)) != 0) {
1035 SCTP_FREE_SONAME(sin6);
1038 (*addr) = (struct sockaddr *)sin6;
1043 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1045 struct sockaddr_in6 *sin6;
1047 struct sockaddr_in6 *sin_a6;
1048 struct sctp_inpcb *inp;
1049 struct sctp_tcb *stcb;
1050 struct sctp_nets *net;
1053 /* Do the malloc first in case it blocks. */
1054 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1057 sin6->sin6_family = AF_INET6;
1058 sin6->sin6_len = sizeof(*sin6);
1060 inp = (struct sctp_inpcb *)so->so_pcb;
1061 if ((inp == NULL) ||
1062 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1063 /* UDP type and listeners will drop out here */
1064 SCTP_FREE_SONAME(sin6);
1065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1068 SCTP_INP_RLOCK(inp);
1069 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1071 SCTP_TCB_LOCK(stcb);
1073 SCTP_INP_RUNLOCK(inp);
1075 SCTP_FREE_SONAME(sin6);
1076 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1077 return (ECONNRESET);
1080 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1081 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1082 if (sin_a6->sin6_family == AF_INET6) {
1084 sin6->sin6_port = stcb->rport;
1085 sin6->sin6_addr = sin_a6->sin6_addr;
1089 SCTP_TCB_UNLOCK(stcb);
1091 /* No IPv4 address */
1092 SCTP_FREE_SONAME(sin6);
1093 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1096 if ((error = sa6_recoverscope(sin6)) != 0) {
1097 SCTP_FREE_SONAME(sin6);
1098 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1101 *addr = (struct sockaddr *)sin6;
1106 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1108 struct in6pcb *inp6 = sotoin6pcb(so);
1112 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;
1141 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1143 struct in6pcb *inp6 = sotoin6pcb(so);
1147 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 struct pr_usrreqs sctp6_usrreqs = {
1175 .pru_abort = sctp6_abort,
1176 .pru_accept = sctp_accept,
1177 .pru_attach = sctp6_attach,
1178 .pru_bind = sctp6_bind,
1179 .pru_connect = sctp6_connect,
1180 .pru_control = in6_control,
1181 .pru_close = sctp6_close,
1182 .pru_detach = sctp6_close,
1183 .pru_sopoll = sopoll_generic,
1184 .pru_flush = sctp_flush,
1185 .pru_disconnect = sctp6_disconnect,
1186 .pru_listen = sctp_listen,
1187 .pru_peeraddr = sctp6_getpeeraddr,
1188 .pru_send = sctp6_send,
1189 .pru_shutdown = sctp_shutdown,
1190 .pru_sockaddr = sctp6_in6getaddr,
1191 .pru_sosend = sctp_sosend,
1192 .pru_soreceive = sctp_soreceive