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)
90 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
91 SCTP_RELEASE_PKT(*i_pak);
92 return (IPPROTO_DONE);
94 m = SCTP_HEADER_TO_CHAIN(*i_pak);
95 #ifdef SCTP_MBUF_LOGGING
96 /* Log in any input mbufs */
97 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
98 sctp_log_mbc(m, SCTP_MBUF_INPUT);
101 #ifdef SCTP_PACKET_LOGGING
102 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
106 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
107 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
109 if_name(m->m_pkthdr.rcvif),
110 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
111 mflowid = m->m_pkthdr.flowid;
112 mflowtype = M_HASHTYPE_GET(m);
113 fibnum = M_GETFIB(m);
114 SCTP_STAT_INCR(sctps_recvpackets);
115 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
116 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
117 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
118 ip6 = mtod(m, struct ip6_hdr *);
119 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
120 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
122 SCTP_STAT_INCR(sctps_hdrops);
123 return (IPPROTO_DONE);
125 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
126 offset -= sizeof(struct sctp_chunkhdr);
127 memset(&src, 0, sizeof(struct sockaddr_in6));
128 src.sin6_family = AF_INET6;
129 src.sin6_len = sizeof(struct sockaddr_in6);
130 src.sin6_port = sh->src_port;
131 src.sin6_addr = ip6->ip6_src;
132 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
135 memset(&dst, 0, sizeof(struct sockaddr_in6));
136 dst.sin6_family = AF_INET6;
137 dst.sin6_len = sizeof(struct sockaddr_in6);
138 dst.sin6_port = sh->dest_port;
139 dst.sin6_addr = ip6->ip6_dst;
140 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
143 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
144 /* XXX send icmp6 host/port unreach? */
147 length = ntohs(ip6->ip6_plen) + iphlen;
148 /* Validate mbuf chain length with IP payload length. */
149 if (SCTP_HEADER_LEN(m) != length) {
150 SCTPDBG(SCTP_DEBUG_INPUT1,
151 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
152 SCTP_STAT_INCR(sctps_hdrops);
155 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
158 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
159 #if defined(SCTP_WITH_NO_CSUM)
160 SCTP_STAT_INCR(sctps_recvnocrc);
162 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
163 SCTP_STAT_INCR(sctps_recvhwcrc);
166 SCTP_STAT_INCR(sctps_recvswcrc);
170 sctp_common_input_processing(&m, iphlen, offset, length,
171 (struct sockaddr *)&src,
172 (struct sockaddr *)&dst,
174 #if !defined(SCTP_WITH_NO_CSUM)
178 mflowtype, mflowid, fibnum,
184 return (IPPROTO_DONE);
189 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
191 return (sctp6_input_with_port(i_pak, offp, 0));
195 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
196 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
200 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
201 (icmp6 == NULL) || (sh == NULL)) {
204 /* First do we even look at it? */
205 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
208 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
209 /* not PACKET TO BIG */
213 * ok we need to look closely. We could even get smarter and look at
214 * anyone that we sent to in case we get a different ICMP that tells
215 * us there is no way to reach a host, but for this impl, all we
216 * care about is MTU discovery.
218 nxtsz = ntohl(icmp6->icmp6_mtu);
219 /* Stop any PMTU timer */
220 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL,
221 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
223 /* Adjust destination size limit */
224 if (net->mtu > nxtsz) {
227 net->mtu -= sizeof(struct udphdr);
230 /* now what about the ep? */
231 if (stcb->asoc.smallest_mtu > nxtsz) {
232 struct sctp_tmit_chunk *chk;
234 /* Adjust that too */
235 stcb->asoc.smallest_mtu = nxtsz;
236 /* now off to subtract IP_DF flag if needed */
238 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
239 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
240 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
243 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
244 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
246 * For this guy we also mark for immediate
247 * resend since we sent to big of chunk
249 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
250 if (chk->sent != SCTP_DATAGRAM_RESEND)
251 stcb->asoc.sent_queue_retran_cnt++;
252 chk->sent = SCTP_DATAGRAM_RESEND;
253 chk->rec.data.doing_fast_retransmit = 0;
255 chk->sent = SCTP_DATAGRAM_RESEND;
256 /* Clear any time so NO RTT is being done */
257 chk->sent_rcv_time.tv_sec = 0;
258 chk->sent_rcv_time.tv_usec = 0;
259 stcb->asoc.total_flight -= chk->send_size;
260 net->flight_size -= chk->send_size;
264 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
267 SCTP_TCB_UNLOCK(stcb);
273 sctp6_notify(struct sctp_inpcb *inp,
274 struct icmp6_hdr *icmph,
277 struct sctp_tcb *stcb,
278 struct sctp_nets *net)
280 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
286 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
287 (sh == NULL) || (to == NULL)) {
289 SCTP_TCB_UNLOCK(stcb);
292 /* First job is to verify the vtag matches what I would send */
293 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
294 SCTP_TCB_UNLOCK(stcb);
297 if (icmph->icmp6_type != ICMP_UNREACH) {
298 /* We only care about unreachable */
299 SCTP_TCB_UNLOCK(stcb);
302 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
303 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
304 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
305 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
306 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
307 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
308 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
309 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
312 * Hmm reachablity problems we must examine closely. If its
313 * not reachable, we may have lost a network. Or if there is
314 * NO protocol at the other end named SCTP. well we consider
317 if (net->dest_state & SCTP_ADDR_REACHABLE) {
318 /* Ok that destination is NOT reachable */
319 net->dest_state &= ~SCTP_ADDR_REACHABLE;
320 net->dest_state &= ~SCTP_ADDR_PF;
321 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
322 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
324 SCTP_TCB_UNLOCK(stcb);
325 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
326 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
328 * Here the peer is either playing tricks on us, including
329 * an address that belongs to someone who does not support
330 * SCTP OR was a userland implementation that shutdown and
331 * now is dead. In either case treat it like a OOTB abort
334 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
335 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
336 so = SCTP_INP_SO(inp);
337 atomic_add_int(&stcb->asoc.refcnt, 1);
338 SCTP_TCB_UNLOCK(stcb);
339 SCTP_SOCKET_LOCK(so, 1);
341 atomic_subtract_int(&stcb->asoc.refcnt, 1);
343 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
344 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
345 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
346 SCTP_SOCKET_UNLOCK(so, 1);
347 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
349 /* no need to unlock here, since the TCB is gone */
351 SCTP_TCB_UNLOCK(stcb);
358 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
361 struct ip6ctlparam *ip6cp = NULL;
364 vrf_id = SCTP_DEFAULT_VRFID;
366 if (pktdst->sa_family != AF_INET6 ||
367 pktdst->sa_len != sizeof(struct sockaddr_in6))
370 if ((unsigned)cmd >= PRC_NCMDS)
372 if (PRC_IS_REDIRECT(cmd)) {
374 } else if (inet6ctlerrmap[cmd] == 0) {
377 /* if the parameter is from icmp6, decode it. */
379 ip6cp = (struct ip6ctlparam *)d;
381 ip6cp = (struct ip6ctlparam *)NULL;
386 * XXX: We assume that when IPV6 is non NULL, M and OFF are
389 struct sctp_inpcb *inp = NULL;
390 struct sctp_tcb *stcb = NULL;
391 struct sctp_nets *net = NULL;
392 struct sockaddr_in6 final;
394 if (ip6cp->ip6c_m == NULL)
397 /* Check if we can safely examine the SCTP header. */
398 if (ip6cp->ip6c_m->m_pkthdr.len < ip6cp->ip6c_off + sizeof(sh))
401 bzero(&sh, sizeof(sh));
402 bzero(&final, sizeof(final));
405 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
407 ip6cp->ip6c_src->sin6_port = sh.src_port;
408 final.sin6_len = sizeof(final);
409 final.sin6_family = AF_INET6;
410 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
411 final.sin6_port = sh.dest_port;
412 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
413 (struct sockaddr *)ip6cp->ip6c_src,
414 &inp, &net, 1, vrf_id);
415 /* inp's ref-count increased && stcb locked */
416 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
417 if (cmd == PRC_MSGSIZE) {
418 sctp6_notify_mbuf(inp,
423 /* inp's ref-count reduced && stcb unlocked */
425 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
426 (struct sockaddr *)&final,
428 /* inp's ref-count reduced && stcb unlocked */
431 if (PRC_IS_REDIRECT(cmd) && inp) {
432 in6_rtchange((struct in6pcb *)inp,
433 inet6ctlerrmap[cmd]);
436 /* reduce inp's ref-count */
438 SCTP_INP_DECR_REF(inp);
439 SCTP_INP_WUNLOCK(inp);
442 SCTP_TCB_UNLOCK(stcb);
448 * this routine can probably be collasped into the one in sctp_userreq.c
449 * since they do the same thing and now we lookup with a sockaddr
452 sctp6_getcred(SYSCTL_HANDLER_ARGS)
455 struct sockaddr_in6 addrs[2];
456 struct sctp_inpcb *inp;
457 struct sctp_nets *net;
458 struct sctp_tcb *stcb;
462 vrf_id = SCTP_DEFAULT_VRFID;
464 error = priv_check(req->td, PRIV_NETINET_GETCRED);
468 if (req->newlen != sizeof(addrs)) {
469 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
472 if (req->oldlen != sizeof(struct ucred)) {
473 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
476 error = SYSCTL_IN(req, addrs, sizeof(addrs));
480 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
482 &inp, &net, 1, vrf_id);
483 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
484 if ((inp != NULL) && (stcb == NULL)) {
485 /* reduce ref-count */
487 SCTP_INP_DECR_REF(inp);
490 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
494 SCTP_TCB_UNLOCK(stcb);
496 * We use the write lock here, only since in the error leg we need
497 * it. If we used RLOCK, then we would have to
498 * wlock/decr/unlock/rlock. Which in theory could create a hole.
499 * Better to use higher wlock.
503 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
505 SCTP_INP_WUNLOCK(inp);
508 cru2x(inp->sctp_socket->so_cred, &xuc);
509 SCTP_INP_WUNLOCK(inp);
510 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
515 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
517 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
520 /* This is the same as the sctp_abort() could be made common */
522 sctp6_abort(struct socket *so)
524 struct sctp_inpcb *inp;
527 inp = (struct sctp_inpcb *)so->so_pcb;
529 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
533 flags = inp->sctp_flags;
534 #ifdef SCTP_LOG_CLOSING
535 sctp_log_closing(inp, NULL, 17);
537 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
538 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
539 #ifdef SCTP_LOG_CLOSING
540 sctp_log_closing(inp, NULL, 16);
542 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
543 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
545 SCTP_SB_CLEAR(so->so_snd);
547 * same for the rcv ones, they are only here for the
550 SCTP_SB_CLEAR(so->so_rcv);
551 /* Now null out the reference, we are completely detached. */
555 flags = inp->sctp_flags;
556 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
557 goto sctp_must_try_again;
564 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
568 struct sctp_inpcb *inp;
569 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
571 inp = (struct sctp_inpcb *)so->so_pcb;
573 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
576 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
577 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
581 error = sctp_inpcb_alloc(so, vrf_id);
584 inp = (struct sctp_inpcb *)so->so_pcb;
586 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
587 inp6 = (struct in6pcb *)inp;
589 inp6->inp_vflag |= INP_IPV6;
590 inp6->in6p_hops = -1; /* use kernel default */
591 inp6->in6p_cksum = -1; /* just to be sure */
594 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
595 * socket as well, because the socket may be bound to an IPv6
596 * wildcard address, which may match an IPv4-mapped IPv6 address.
598 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
601 * Hmm what about the IPSEC stuff that is missing here but in
604 SCTP_INP_WUNLOCK(inp);
609 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
611 struct sctp_inpcb *inp;
615 inp = (struct sctp_inpcb *)so->so_pcb;
617 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
621 switch (addr->sa_family) {
624 if (addr->sa_len != sizeof(struct sockaddr_in)) {
625 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
632 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
643 inp6 = (struct in6pcb *)inp;
644 inp6->inp_vflag &= ~INP_IPV4;
645 inp6->inp_vflag |= INP_IPV6;
646 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
647 switch (addr->sa_family) {
650 /* binding v4 addr to v6 socket, so reset flags */
651 inp6->inp_vflag |= INP_IPV4;
652 inp6->inp_vflag &= ~INP_IPV6;
658 struct sockaddr_in6 *sin6_p;
660 sin6_p = (struct sockaddr_in6 *)addr;
662 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
663 inp6->inp_vflag |= INP_IPV4;
666 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
667 struct sockaddr_in sin;
669 in6_sin6_2_sin(&sin, sin6_p);
670 inp6->inp_vflag |= INP_IPV4;
671 inp6->inp_vflag &= ~INP_IPV6;
672 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
682 } else if (addr != NULL) {
683 struct sockaddr_in6 *sin6_p;
685 /* IPV6_V6ONLY socket */
687 if (addr->sa_family == AF_INET) {
688 /* can't bind v4 addr to v6 only socket! */
689 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
693 sin6_p = (struct sockaddr_in6 *)addr;
695 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
696 /* can't bind v4-mapped addrs either! */
697 /* NOTE: we don't support SIIT */
698 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
702 error = sctp_inpcb_bind(so, addr, NULL, p);
708 sctp6_close(struct socket *so)
713 /* This could be made common with sctp_detach() since they are identical */
717 sctp6_disconnect(struct socket *so)
719 return (sctp_disconnect(so));
724 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
725 struct mbuf *control, struct thread *p);
729 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
730 struct mbuf *control, struct thread *p)
732 struct sctp_inpcb *inp;
736 struct sockaddr_in6 *sin6;
739 /* No SPL needed since sctp_output does this */
741 inp = (struct sctp_inpcb *)so->so_pcb;
744 SCTP_RELEASE_PKT(control);
748 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
751 inp6 = (struct in6pcb *)inp;
753 * For the TCP model we may get a NULL addr, if we are a connected
756 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
763 SCTP_RELEASE_PKT(control);
766 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
767 return (EDESTADDRREQ);
770 sin6 = (struct sockaddr_in6 *)addr;
771 if (SCTP_IPV6_V6ONLY(inp6)) {
773 * if IPV6_V6ONLY flag, we discard datagrams destined to a
774 * v4 addr or v4-mapped addr
776 if (addr->sa_family == AF_INET) {
777 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
780 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
781 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
785 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
786 struct sockaddr_in sin;
788 /* convert v4-mapped into v4 addr and send */
789 in6_sin6_2_sin(&sin, sin6);
790 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
794 /* now what about control */
797 SCTP_PRINTF("huh? control set?\n");
798 SCTP_RELEASE_PKT(inp->control);
801 inp->control = control;
805 SCTP_BUF_NEXT(inp->pkt_last) = m;
808 inp->pkt_last = inp->pkt = m;
811 /* FreeBSD and MacOSX uses a flag passed */
812 ((flags & PRUS_MORETOCOME) == 0)
815 * note with the current version this code will only be used
816 * by OpenBSD, NetBSD and FreeBSD have methods for
817 * re-defining sosend() to use sctp_sosend(). One can
818 * optionaly switch back to this code (by changing back the
819 * defininitions but this is not advisable.
823 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
833 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
837 struct sctp_inpcb *inp;
838 struct sctp_tcb *stcb;
842 struct sockaddr_in6 *sin6;
843 union sctp_sockstore store;
848 inp6 = (struct in6pcb *)so->so_pcb;
850 inp = (struct sctp_inpcb *)so->so_pcb;
852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
853 return (ECONNRESET); /* I made the same as TCP since we are
857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
860 switch (addr->sa_family) {
863 if (addr->sa_len != sizeof(struct sockaddr_in)) {
864 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
871 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
872 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
878 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
882 vrf_id = inp->def_vrf_id;
883 SCTP_ASOC_CREATE_LOCK(inp);
885 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
886 SCTP_PCB_FLAGS_UNBOUND) {
887 /* Bind a ephemeral port */
888 SCTP_INP_RUNLOCK(inp);
889 error = sctp6_bind(so, NULL, p);
891 SCTP_ASOC_CREATE_UNLOCK(inp);
897 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
898 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
899 /* We are already connected AND the TCP model */
900 SCTP_INP_RUNLOCK(inp);
901 SCTP_ASOC_CREATE_UNLOCK(inp);
902 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
906 sin6 = (struct sockaddr_in6 *)addr;
907 if (SCTP_IPV6_V6ONLY(inp6)) {
909 * if IPV6_V6ONLY flag, ignore connections destined to a v4
910 * addr or v4-mapped addr
912 if (addr->sa_family == AF_INET) {
913 SCTP_INP_RUNLOCK(inp);
914 SCTP_ASOC_CREATE_UNLOCK(inp);
915 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
918 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
919 SCTP_INP_RUNLOCK(inp);
920 SCTP_ASOC_CREATE_UNLOCK(inp);
921 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
925 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
926 /* convert v4-mapped into v4 addr */
927 in6_sin6_2_sin(&store.sin, sin6);
931 /* Now do we connect? */
932 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
933 stcb = LIST_FIRST(&inp->sctp_asoc_list);
935 SCTP_TCB_UNLOCK(stcb);
937 SCTP_INP_RUNLOCK(inp);
939 SCTP_INP_RUNLOCK(inp);
941 SCTP_INP_INCR_REF(inp);
942 SCTP_INP_WUNLOCK(inp);
943 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
946 SCTP_INP_DECR_REF(inp);
947 SCTP_INP_WUNLOCK(inp);
952 /* Already have or am bring up an association */
953 SCTP_ASOC_CREATE_UNLOCK(inp);
954 SCTP_TCB_UNLOCK(stcb);
955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
958 /* We are GOOD to go */
959 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
960 SCTP_ASOC_CREATE_UNLOCK(inp);
965 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
966 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
967 /* Set the connected flag so we can queue data */
970 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
971 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
973 /* initialize authentication parameters for the assoc */
974 sctp_initialize_auth_params(inp, stcb);
976 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
977 SCTP_TCB_UNLOCK(stcb);
982 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
984 struct sockaddr_in6 *sin6;
985 struct sctp_inpcb *inp;
987 struct sctp_ifa *sctp_ifa;
992 * Do the malloc first in case it blocks.
994 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
997 sin6->sin6_family = AF_INET6;
998 sin6->sin6_len = sizeof(*sin6);
1000 inp = (struct sctp_inpcb *)so->so_pcb;
1002 SCTP_FREE_SONAME(sin6);
1003 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1004 return (ECONNRESET);
1006 SCTP_INP_RLOCK(inp);
1007 sin6->sin6_port = inp->sctp_lport;
1008 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1009 /* For the bound all case you get back 0 */
1010 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1011 struct sctp_tcb *stcb;
1012 struct sockaddr_in6 *sin_a6;
1013 struct sctp_nets *net;
1016 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1022 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1023 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1025 /* this will make coverity happy */
1028 if (sin_a6->sin6_family == AF_INET6) {
1033 if ((!fnd) || (sin_a6 == NULL)) {
1037 vrf_id = inp->def_vrf_id;
1038 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1040 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1043 /* For the bound all case you get back 0 */
1045 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1048 /* Take the first IPv6 address in the list */
1049 struct sctp_laddr *laddr;
1052 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1053 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1054 struct sockaddr_in6 *sin_a;
1056 sin_a = &laddr->ifa->address.sin6;
1057 sin6->sin6_addr = sin_a->sin6_addr;
1063 SCTP_FREE_SONAME(sin6);
1064 SCTP_INP_RUNLOCK(inp);
1065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1069 SCTP_INP_RUNLOCK(inp);
1070 /* Scoping things for v6 */
1071 if ((error = sa6_recoverscope(sin6)) != 0) {
1072 SCTP_FREE_SONAME(sin6);
1075 (*addr) = (struct sockaddr *)sin6;
1080 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1082 struct sockaddr_in6 *sin6;
1084 struct sockaddr_in6 *sin_a6;
1085 struct sctp_inpcb *inp;
1086 struct sctp_tcb *stcb;
1087 struct sctp_nets *net;
1090 /* Do the malloc first in case it blocks. */
1091 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1094 sin6->sin6_family = AF_INET6;
1095 sin6->sin6_len = sizeof(*sin6);
1097 inp = (struct sctp_inpcb *)so->so_pcb;
1098 if ((inp == NULL) ||
1099 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1100 /* UDP type and listeners will drop out here */
1101 SCTP_FREE_SONAME(sin6);
1102 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1105 SCTP_INP_RLOCK(inp);
1106 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1108 SCTP_TCB_LOCK(stcb);
1110 SCTP_INP_RUNLOCK(inp);
1112 SCTP_FREE_SONAME(sin6);
1113 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1114 return (ECONNRESET);
1117 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1118 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1119 if (sin_a6->sin6_family == AF_INET6) {
1121 sin6->sin6_port = stcb->rport;
1122 sin6->sin6_addr = sin_a6->sin6_addr;
1126 SCTP_TCB_UNLOCK(stcb);
1128 /* No IPv4 address */
1129 SCTP_FREE_SONAME(sin6);
1130 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1133 if ((error = sa6_recoverscope(sin6)) != 0) {
1134 SCTP_FREE_SONAME(sin6);
1135 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1138 *addr = (struct sockaddr *)sin6;
1143 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1146 struct sockaddr *addr;
1149 struct in6pcb *inp6 = sotoin6pcb(so);
1153 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1156 /* allow v6 addresses precedence */
1157 error = sctp6_getaddr(so, nam);
1160 /* try v4 next if v6 failed */
1161 error = sctp_ingetaddr(so, nam);
1166 /* if I'm V6ONLY, convert it to v4-mapped */
1167 if (SCTP_IPV6_V6ONLY(inp6)) {
1168 struct sockaddr_in6 sin6;
1170 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1171 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1180 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1183 struct sockaddr *addr;
1186 struct in6pcb *inp6 = sotoin6pcb(so);
1190 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1193 /* allow v6 addresses precedence */
1194 error = sctp6_peeraddr(so, nam);
1197 /* try v4 next if v6 failed */
1198 error = sctp_peeraddr(so, nam);
1203 /* if I'm V6ONLY, convert it to v4-mapped */
1204 if (SCTP_IPV6_V6ONLY(inp6)) {
1205 struct sockaddr_in6 sin6;
1207 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1208 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1215 struct pr_usrreqs sctp6_usrreqs = {
1216 .pru_abort = sctp6_abort,
1217 .pru_accept = sctp_accept,
1218 .pru_attach = sctp6_attach,
1219 .pru_bind = sctp6_bind,
1220 .pru_connect = sctp6_connect,
1221 .pru_control = in6_control,
1222 .pru_close = sctp6_close,
1223 .pru_detach = sctp6_close,
1224 .pru_sopoll = sopoll_generic,
1225 .pru_flush = sctp_flush,
1226 .pru_disconnect = sctp6_disconnect,
1227 .pru_listen = sctp_listen,
1228 .pru_peeraddr = sctp6_getpeeraddr,
1229 .pru_send = sctp6_send,
1230 .pru_shutdown = sctp_shutdown,
1231 .pru_sockaddr = sctp6_in6getaddr,
1232 .pru_sosend = sctp_sosend,
1233 .pru_soreceive = sctp_soreceive