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>
43 #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/udp.h>
60 #include <netipsec/ipsec.h>
62 #include <netipsec/ipsec6.h>
66 extern struct protosw inetsw[];
69 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
75 struct sockaddr_in6 src, dst;
78 struct sctp_chunkhdr *ch;
81 #if !defined(SCTP_WITH_NO_CSUM)
89 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
90 SCTP_RELEASE_PKT(*i_pak);
91 return (IPPROTO_DONE);
93 m = SCTP_HEADER_TO_CHAIN(*i_pak);
94 #ifdef SCTP_MBUF_LOGGING
95 /* Log in any input mbufs */
96 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
99 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
100 if (SCTP_BUF_IS_EXTENDED(mat)) {
101 sctp_log_mb(mat, SCTP_MBUF_INPUT);
106 #ifdef SCTP_PACKET_LOGGING
107 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
111 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
112 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
114 if_name(m->m_pkthdr.rcvif),
115 m->m_pkthdr.csum_flags);
116 if (m->m_flags & M_FLOWID) {
117 mflowid = m->m_pkthdr.flowid;
123 SCTP_STAT_INCR(sctps_recvpackets);
124 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
125 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
126 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
127 ip6 = mtod(m, struct ip6_hdr *);
128 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
129 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
131 SCTP_STAT_INCR(sctps_hdrops);
132 return (IPPROTO_DONE);
134 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
135 offset -= sizeof(struct sctp_chunkhdr);
136 memset(&src, 0, sizeof(struct sockaddr_in6));
137 src.sin6_family = AF_INET6;
138 src.sin6_len = sizeof(struct sockaddr_in6);
139 src.sin6_port = sh->src_port;
140 src.sin6_addr = ip6->ip6_src;
141 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
144 memset(&dst, 0, sizeof(struct sockaddr_in6));
145 dst.sin6_family = AF_INET6;
146 dst.sin6_len = sizeof(struct sockaddr_in6);
147 dst.sin6_port = sh->dest_port;
148 dst.sin6_addr = ip6->ip6_dst;
149 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
152 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
153 /* XXX send icmp6 host/port unreach? */
156 length = ntohs(ip6->ip6_plen) + iphlen;
157 /* Validate mbuf chain length with IP payload length. */
158 if (SCTP_HEADER_LEN(m) != length) {
159 SCTPDBG(SCTP_DEBUG_INPUT1,
160 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
161 SCTP_STAT_INCR(sctps_hdrops);
164 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
167 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
168 #if defined(SCTP_WITH_NO_CSUM)
169 SCTP_STAT_INCR(sctps_recvnocrc);
171 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
172 SCTP_STAT_INCR(sctps_recvhwcrc);
175 SCTP_STAT_INCR(sctps_recvswcrc);
179 sctp_common_input_processing(&m, iphlen, offset, length,
180 (struct sockaddr *)&src,
181 (struct sockaddr *)&dst,
183 #if !defined(SCTP_WITH_NO_CSUM)
187 use_mflowid, mflowid,
193 return (IPPROTO_DONE);
198 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
200 return (sctp6_input_with_port(i_pak, offp, 0));
204 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
205 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
209 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
210 (icmp6 == NULL) || (sh == NULL)) {
213 /* First do we even look at it? */
214 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
217 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
218 /* not PACKET TO BIG */
222 * ok we need to look closely. We could even get smarter and look at
223 * anyone that we sent to in case we get a different ICMP that tells
224 * us there is no way to reach a host, but for this impl, all we
225 * care about is MTU discovery.
227 nxtsz = ntohl(icmp6->icmp6_mtu);
228 /* Stop any PMTU timer */
229 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
231 /* Adjust destination size limit */
232 if (net->mtu > nxtsz) {
235 net->mtu -= sizeof(struct udphdr);
238 /* now what about the ep? */
239 if (stcb->asoc.smallest_mtu > nxtsz) {
240 struct sctp_tmit_chunk *chk;
242 /* Adjust that too */
243 stcb->asoc.smallest_mtu = nxtsz;
244 /* now off to subtract IP_DF flag if needed */
246 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
247 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
248 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
251 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
252 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
254 * For this guy we also mark for immediate
255 * resend since we sent to big of chunk
257 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
258 if (chk->sent != SCTP_DATAGRAM_RESEND)
259 stcb->asoc.sent_queue_retran_cnt++;
260 chk->sent = SCTP_DATAGRAM_RESEND;
261 chk->rec.data.doing_fast_retransmit = 0;
263 chk->sent = SCTP_DATAGRAM_RESEND;
264 /* Clear any time so NO RTT is being done */
265 chk->sent_rcv_time.tv_sec = 0;
266 chk->sent_rcv_time.tv_usec = 0;
267 stcb->asoc.total_flight -= chk->send_size;
268 net->flight_size -= chk->send_size;
272 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
275 SCTP_TCB_UNLOCK(stcb);
281 sctp6_notify(struct sctp_inpcb *inp,
282 struct icmp6_hdr *icmph,
285 struct sctp_tcb *stcb,
286 struct sctp_nets *net)
288 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
294 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
295 (sh == NULL) || (to == NULL)) {
297 SCTP_TCB_UNLOCK(stcb);
300 /* First job is to verify the vtag matches what I would send */
301 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
302 SCTP_TCB_UNLOCK(stcb);
305 if (icmph->icmp6_type != ICMP_UNREACH) {
306 /* We only care about unreachable */
307 SCTP_TCB_UNLOCK(stcb);
310 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
311 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
312 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
313 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
314 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
315 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
316 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
317 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
320 * Hmm reachablity problems we must examine closely. If its
321 * not reachable, we may have lost a network. Or if there is
322 * NO protocol at the other end named SCTP. well we consider
325 if (net->dest_state & SCTP_ADDR_REACHABLE) {
326 /* Ok that destination is NOT reachable */
327 net->dest_state &= ~SCTP_ADDR_REACHABLE;
328 net->dest_state &= ~SCTP_ADDR_PF;
329 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
330 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
332 SCTP_TCB_UNLOCK(stcb);
333 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
334 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
336 * Here the peer is either playing tricks on us, including
337 * an address that belongs to someone who does not support
338 * SCTP OR was a userland implementation that shutdown and
339 * now is dead. In either case treat it like a OOTB abort
342 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
343 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
344 so = SCTP_INP_SO(inp);
345 atomic_add_int(&stcb->asoc.refcnt, 1);
346 SCTP_TCB_UNLOCK(stcb);
347 SCTP_SOCKET_LOCK(so, 1);
349 atomic_subtract_int(&stcb->asoc.refcnt, 1);
351 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
352 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
353 SCTP_SOCKET_UNLOCK(so, 1);
354 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
356 /* no need to unlock here, since the TCB is gone */
358 SCTP_TCB_UNLOCK(stcb);
365 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
368 struct ip6ctlparam *ip6cp = NULL;
371 vrf_id = SCTP_DEFAULT_VRFID;
373 if (pktdst->sa_family != AF_INET6 ||
374 pktdst->sa_len != sizeof(struct sockaddr_in6))
377 if ((unsigned)cmd >= PRC_NCMDS)
379 if (PRC_IS_REDIRECT(cmd)) {
381 } else if (inet6ctlerrmap[cmd] == 0) {
384 /* if the parameter is from icmp6, decode it. */
386 ip6cp = (struct ip6ctlparam *)d;
388 ip6cp = (struct ip6ctlparam *)NULL;
393 * XXX: We assume that when IPV6 is non NULL, M and OFF are
396 struct sctp_inpcb *inp = NULL;
397 struct sctp_tcb *stcb = NULL;
398 struct sctp_nets *net = NULL;
399 struct sockaddr_in6 final;
401 if (ip6cp->ip6c_m == NULL)
404 /* Check if we can safely examine the SCTP header. */
405 if (ip6cp->ip6c_m->m_pkthdr.len < ip6cp->ip6c_off + sizeof(sh))
408 bzero(&sh, sizeof(sh));
409 bzero(&final, sizeof(final));
412 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
414 ip6cp->ip6c_src->sin6_port = sh.src_port;
415 final.sin6_len = sizeof(final);
416 final.sin6_family = AF_INET6;
417 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
418 final.sin6_port = sh.dest_port;
419 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
420 (struct sockaddr *)ip6cp->ip6c_src,
421 &inp, &net, 1, vrf_id);
422 /* inp's ref-count increased && stcb locked */
423 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
424 if (cmd == PRC_MSGSIZE) {
425 sctp6_notify_mbuf(inp,
430 /* inp's ref-count reduced && stcb unlocked */
432 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
433 (struct sockaddr *)&final,
435 /* inp's ref-count reduced && stcb unlocked */
438 if (PRC_IS_REDIRECT(cmd) && inp) {
439 in6_rtchange((struct in6pcb *)inp,
440 inet6ctlerrmap[cmd]);
443 /* reduce inp's ref-count */
445 SCTP_INP_DECR_REF(inp);
446 SCTP_INP_WUNLOCK(inp);
449 SCTP_TCB_UNLOCK(stcb);
455 * this routine can probably be collasped into the one in sctp_userreq.c
456 * since they do the same thing and now we lookup with a sockaddr
459 sctp6_getcred(SYSCTL_HANDLER_ARGS)
462 struct sockaddr_in6 addrs[2];
463 struct sctp_inpcb *inp;
464 struct sctp_nets *net;
465 struct sctp_tcb *stcb;
469 vrf_id = SCTP_DEFAULT_VRFID;
471 error = priv_check(req->td, PRIV_NETINET_GETCRED);
475 if (req->newlen != sizeof(addrs)) {
476 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
479 if (req->oldlen != sizeof(struct ucred)) {
480 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
483 error = SYSCTL_IN(req, addrs, sizeof(addrs));
487 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
489 &inp, &net, 1, vrf_id);
490 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
491 if ((inp != NULL) && (stcb == NULL)) {
492 /* reduce ref-count */
494 SCTP_INP_DECR_REF(inp);
497 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
501 SCTP_TCB_UNLOCK(stcb);
503 * We use the write lock here, only since in the error leg we need
504 * it. If we used RLOCK, then we would have to
505 * wlock/decr/unlock/rlock. Which in theory could create a hole.
506 * Better to use higher wlock.
510 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
512 SCTP_INP_WUNLOCK(inp);
515 cru2x(inp->sctp_socket->so_cred, &xuc);
516 SCTP_INP_WUNLOCK(inp);
517 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
522 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
524 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
527 /* This is the same as the sctp_abort() could be made common */
529 sctp6_abort(struct socket *so)
531 struct sctp_inpcb *inp;
534 inp = (struct sctp_inpcb *)so->so_pcb;
536 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
540 flags = inp->sctp_flags;
541 #ifdef SCTP_LOG_CLOSING
542 sctp_log_closing(inp, NULL, 17);
544 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
545 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
546 #ifdef SCTP_LOG_CLOSING
547 sctp_log_closing(inp, NULL, 16);
549 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
550 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
552 SCTP_SB_CLEAR(so->so_snd);
554 * same for the rcv ones, they are only here for the
557 SCTP_SB_CLEAR(so->so_rcv);
558 /* Now null out the reference, we are completely detached. */
562 flags = inp->sctp_flags;
563 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
564 goto sctp_must_try_again;
571 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
575 struct sctp_inpcb *inp;
576 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
578 inp = (struct sctp_inpcb *)so->so_pcb;
580 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
583 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
584 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
588 error = sctp_inpcb_alloc(so, vrf_id);
591 inp = (struct sctp_inpcb *)so->so_pcb;
593 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
594 inp6 = (struct in6pcb *)inp;
596 inp6->inp_vflag |= INP_IPV6;
597 inp6->in6p_hops = -1; /* use kernel default */
598 inp6->in6p_cksum = -1; /* just to be sure */
601 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
602 * socket as well, because the socket may be bound to an IPv6
603 * wildcard address, which may match an IPv4-mapped IPv6 address.
605 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
608 * Hmm what about the IPSEC stuff that is missing here but in
611 SCTP_INP_WUNLOCK(inp);
616 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
618 struct sctp_inpcb *inp;
622 inp = (struct sctp_inpcb *)so->so_pcb;
624 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
628 switch (addr->sa_family) {
631 if (addr->sa_len != sizeof(struct sockaddr_in)) {
632 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
639 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
640 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
646 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
650 inp6 = (struct in6pcb *)inp;
651 inp6->inp_vflag &= ~INP_IPV4;
652 inp6->inp_vflag |= INP_IPV6;
653 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
654 switch (addr->sa_family) {
657 /* binding v4 addr to v6 socket, so reset flags */
658 inp6->inp_vflag |= INP_IPV4;
659 inp6->inp_vflag &= ~INP_IPV6;
665 struct sockaddr_in6 *sin6_p;
667 sin6_p = (struct sockaddr_in6 *)addr;
669 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
670 inp6->inp_vflag |= INP_IPV4;
673 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
674 struct sockaddr_in sin;
676 in6_sin6_2_sin(&sin, sin6_p);
677 inp6->inp_vflag |= INP_IPV4;
678 inp6->inp_vflag &= ~INP_IPV6;
679 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
689 } else if (addr != NULL) {
690 struct sockaddr_in6 *sin6_p;
692 /* IPV6_V6ONLY socket */
694 if (addr->sa_family == AF_INET) {
695 /* can't bind v4 addr to v6 only socket! */
696 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
700 sin6_p = (struct sockaddr_in6 *)addr;
702 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
703 /* can't bind v4-mapped addrs either! */
704 /* NOTE: we don't support SIIT */
705 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
709 error = sctp_inpcb_bind(so, addr, NULL, p);
715 sctp6_close(struct socket *so)
720 /* This could be made common with sctp_detach() since they are identical */
724 sctp6_disconnect(struct socket *so)
726 return (sctp_disconnect(so));
731 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
732 struct mbuf *control, struct thread *p);
736 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
737 struct mbuf *control, struct thread *p)
739 struct sctp_inpcb *inp;
743 struct sockaddr_in6 *sin6;
746 /* No SPL needed since sctp_output does this */
748 inp = (struct sctp_inpcb *)so->so_pcb;
751 SCTP_RELEASE_PKT(control);
755 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
758 inp6 = (struct in6pcb *)inp;
760 * For the TCP model we may get a NULL addr, if we are a connected
763 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
770 SCTP_RELEASE_PKT(control);
773 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
774 return (EDESTADDRREQ);
777 sin6 = (struct sockaddr_in6 *)addr;
778 if (SCTP_IPV6_V6ONLY(inp6)) {
780 * if IPV6_V6ONLY flag, we discard datagrams destined to a
781 * v4 addr or v4-mapped addr
783 if (addr->sa_family == AF_INET) {
784 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
787 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
788 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
792 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
793 struct sockaddr_in sin;
795 /* convert v4-mapped into v4 addr and send */
796 in6_sin6_2_sin(&sin, sin6);
797 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
801 /* now what about control */
804 SCTP_PRINTF("huh? control set?\n");
805 SCTP_RELEASE_PKT(inp->control);
808 inp->control = control;
812 SCTP_BUF_NEXT(inp->pkt_last) = m;
815 inp->pkt_last = inp->pkt = m;
818 /* FreeBSD and MacOSX uses a flag passed */
819 ((flags & PRUS_MORETOCOME) == 0)
822 * note with the current version this code will only be used
823 * by OpenBSD, NetBSD and FreeBSD have methods for
824 * re-defining sosend() to use sctp_sosend(). One can
825 * optionaly switch back to this code (by changing back the
826 * defininitions but this is not advisable.
830 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
840 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
844 struct sctp_inpcb *inp;
845 struct sctp_tcb *stcb;
849 struct sockaddr_in6 *sin6;
850 struct sockaddr_storage ss;
855 inp6 = (struct in6pcb *)so->so_pcb;
857 inp = (struct sctp_inpcb *)so->so_pcb;
859 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
860 return (ECONNRESET); /* I made the same as TCP since we are
864 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
867 switch (addr->sa_family) {
870 if (addr->sa_len != sizeof(struct sockaddr_in)) {
871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
878 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
879 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
885 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
889 vrf_id = inp->def_vrf_id;
890 SCTP_ASOC_CREATE_LOCK(inp);
892 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
893 SCTP_PCB_FLAGS_UNBOUND) {
894 /* Bind a ephemeral port */
895 SCTP_INP_RUNLOCK(inp);
896 error = sctp6_bind(so, NULL, p);
898 SCTP_ASOC_CREATE_UNLOCK(inp);
904 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
905 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
906 /* We are already connected AND the TCP model */
907 SCTP_INP_RUNLOCK(inp);
908 SCTP_ASOC_CREATE_UNLOCK(inp);
909 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
913 sin6 = (struct sockaddr_in6 *)addr;
914 if (SCTP_IPV6_V6ONLY(inp6)) {
916 * if IPV6_V6ONLY flag, ignore connections destined to a v4
917 * addr or v4-mapped addr
919 if (addr->sa_family == AF_INET) {
920 SCTP_INP_RUNLOCK(inp);
921 SCTP_ASOC_CREATE_UNLOCK(inp);
922 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
925 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
926 SCTP_INP_RUNLOCK(inp);
927 SCTP_ASOC_CREATE_UNLOCK(inp);
928 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
932 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
933 /* convert v4-mapped into v4 addr */
934 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
935 addr = (struct sockaddr *)&ss;
938 /* Now do we connect? */
939 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
940 stcb = LIST_FIRST(&inp->sctp_asoc_list);
942 SCTP_TCB_UNLOCK(stcb);
944 SCTP_INP_RUNLOCK(inp);
946 SCTP_INP_RUNLOCK(inp);
948 SCTP_INP_INCR_REF(inp);
949 SCTP_INP_WUNLOCK(inp);
950 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
953 SCTP_INP_DECR_REF(inp);
954 SCTP_INP_WUNLOCK(inp);
959 /* Already have or am bring up an association */
960 SCTP_ASOC_CREATE_UNLOCK(inp);
961 SCTP_TCB_UNLOCK(stcb);
962 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
965 /* We are GOOD to go */
966 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
967 SCTP_ASOC_CREATE_UNLOCK(inp);
972 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
973 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
974 /* Set the connected flag so we can queue data */
977 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
978 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
980 /* initialize authentication parameters for the assoc */
981 sctp_initialize_auth_params(inp, stcb);
983 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
984 SCTP_TCB_UNLOCK(stcb);
989 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
991 struct sockaddr_in6 *sin6;
992 struct sctp_inpcb *inp;
994 struct sctp_ifa *sctp_ifa;
999 * Do the malloc first in case it blocks.
1001 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1004 sin6->sin6_family = AF_INET6;
1005 sin6->sin6_len = sizeof(*sin6);
1007 inp = (struct sctp_inpcb *)so->so_pcb;
1009 SCTP_FREE_SONAME(sin6);
1010 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1011 return (ECONNRESET);
1013 SCTP_INP_RLOCK(inp);
1014 sin6->sin6_port = inp->sctp_lport;
1015 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1016 /* For the bound all case you get back 0 */
1017 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1018 struct sctp_tcb *stcb;
1019 struct sockaddr_in6 *sin_a6;
1020 struct sctp_nets *net;
1023 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1029 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1030 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1032 /* this will make coverity happy */
1035 if (sin_a6->sin6_family == AF_INET6) {
1040 if ((!fnd) || (sin_a6 == NULL)) {
1044 vrf_id = inp->def_vrf_id;
1045 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1047 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1050 /* For the bound all case you get back 0 */
1052 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1055 /* Take the first IPv6 address in the list */
1056 struct sctp_laddr *laddr;
1059 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1060 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1061 struct sockaddr_in6 *sin_a;
1063 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1064 sin6->sin6_addr = sin_a->sin6_addr;
1070 SCTP_FREE_SONAME(sin6);
1071 SCTP_INP_RUNLOCK(inp);
1072 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1076 SCTP_INP_RUNLOCK(inp);
1077 /* Scoping things for v6 */
1078 if ((error = sa6_recoverscope(sin6)) != 0) {
1079 SCTP_FREE_SONAME(sin6);
1082 (*addr) = (struct sockaddr *)sin6;
1087 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1089 struct sockaddr_in6 *sin6;
1091 struct sockaddr_in6 *sin_a6;
1092 struct sctp_inpcb *inp;
1093 struct sctp_tcb *stcb;
1094 struct sctp_nets *net;
1097 /* Do the malloc first in case it blocks. */
1098 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1101 sin6->sin6_family = AF_INET6;
1102 sin6->sin6_len = sizeof(*sin6);
1104 inp = (struct sctp_inpcb *)so->so_pcb;
1105 if ((inp == NULL) ||
1106 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1107 /* UDP type and listeners will drop out here */
1108 SCTP_FREE_SONAME(sin6);
1109 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1112 SCTP_INP_RLOCK(inp);
1113 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1115 SCTP_TCB_LOCK(stcb);
1117 SCTP_INP_RUNLOCK(inp);
1119 SCTP_FREE_SONAME(sin6);
1120 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1121 return (ECONNRESET);
1124 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1125 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1126 if (sin_a6->sin6_family == AF_INET6) {
1128 sin6->sin6_port = stcb->rport;
1129 sin6->sin6_addr = sin_a6->sin6_addr;
1133 SCTP_TCB_UNLOCK(stcb);
1135 /* No IPv4 address */
1136 SCTP_FREE_SONAME(sin6);
1137 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1140 if ((error = sa6_recoverscope(sin6)) != 0)
1142 *addr = (struct sockaddr *)sin6;
1147 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1150 struct sockaddr *addr;
1153 struct in6pcb *inp6 = sotoin6pcb(so);
1157 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1160 /* allow v6 addresses precedence */
1161 error = sctp6_getaddr(so, nam);
1164 /* try v4 next if v6 failed */
1165 error = sctp_ingetaddr(so, nam);
1170 /* if I'm V6ONLY, convert it to v4-mapped */
1171 if (SCTP_IPV6_V6ONLY(inp6)) {
1172 struct sockaddr_in6 sin6;
1174 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1175 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1184 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1187 struct sockaddr *addr;
1190 struct in6pcb *inp6 = sotoin6pcb(so);
1194 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1197 /* allow v6 addresses precedence */
1198 error = sctp6_peeraddr(so, nam);
1201 /* try v4 next if v6 failed */
1202 error = sctp_peeraddr(so, nam);
1207 /* if I'm V6ONLY, convert it to v4-mapped */
1208 if (SCTP_IPV6_V6ONLY(inp6)) {
1209 struct sockaddr_in6 sin6;
1211 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1212 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1219 struct pr_usrreqs sctp6_usrreqs = {
1220 .pru_abort = sctp6_abort,
1221 .pru_accept = sctp_accept,
1222 .pru_attach = sctp6_attach,
1223 .pru_bind = sctp6_bind,
1224 .pru_connect = sctp6_connect,
1225 .pru_control = in6_control,
1226 .pru_close = sctp6_close,
1227 .pru_detach = sctp6_close,
1228 .pru_sopoll = sopoll_generic,
1229 .pru_flush = sctp_flush,
1230 .pru_disconnect = sctp6_disconnect,
1231 .pru_listen = sctp_listen,
1232 .pru_peeraddr = sctp6_getpeeraddr,
1233 .pru_send = sctp6_send,
1234 .pru_shutdown = sctp_shutdown,
1235 .pru_sockaddr = sctp6_in6getaddr,
1236 .pru_sosend = sctp_sosend,
1237 .pru_soreceive = sctp_soreceive