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;
78 #if !defined(SCTP_WITH_NO_CSUM)
87 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
88 SCTP_RELEASE_PKT(*i_pak);
89 return (IPPROTO_DONE);
91 m = SCTP_HEADER_TO_CHAIN(*i_pak);
92 #ifdef SCTP_MBUF_LOGGING
93 /* Log in any input mbufs */
94 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
95 sctp_log_mbc(m, SCTP_MBUF_INPUT);
98 #ifdef SCTP_PACKET_LOGGING
99 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
103 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
104 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
106 if_name(m->m_pkthdr.rcvif),
107 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
108 mflowid = m->m_pkthdr.flowid;
109 mflowtype = M_HASHTYPE_GET(m);
110 fibnum = M_GETFIB(m);
111 SCTP_STAT_INCR(sctps_recvpackets);
112 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
113 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
114 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
115 ip6 = mtod(m, struct ip6_hdr *);
116 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
117 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
119 SCTP_STAT_INCR(sctps_hdrops);
120 return (IPPROTO_DONE);
122 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
123 offset -= sizeof(struct sctp_chunkhdr);
124 memset(&src, 0, sizeof(struct sockaddr_in6));
125 src.sin6_family = AF_INET6;
126 src.sin6_len = sizeof(struct sockaddr_in6);
127 src.sin6_port = sh->src_port;
128 src.sin6_addr = ip6->ip6_src;
129 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
132 memset(&dst, 0, sizeof(struct sockaddr_in6));
133 dst.sin6_family = AF_INET6;
134 dst.sin6_len = sizeof(struct sockaddr_in6);
135 dst.sin6_port = sh->dest_port;
136 dst.sin6_addr = ip6->ip6_dst;
137 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
140 length = ntohs(ip6->ip6_plen) + iphlen;
141 /* Validate mbuf chain length with IP payload length. */
142 if (SCTP_HEADER_LEN(m) != length) {
143 SCTPDBG(SCTP_DEBUG_INPUT1,
144 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
145 SCTP_STAT_INCR(sctps_hdrops);
148 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
151 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
152 #if defined(SCTP_WITH_NO_CSUM)
153 SCTP_STAT_INCR(sctps_recvnocrc);
155 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
156 SCTP_STAT_INCR(sctps_recvhwcrc);
159 SCTP_STAT_INCR(sctps_recvswcrc);
163 sctp_common_input_processing(&m, iphlen, offset, length,
164 (struct sockaddr *)&src,
165 (struct sockaddr *)&dst,
167 #if !defined(SCTP_WITH_NO_CSUM)
171 mflowtype, mflowid, fibnum,
177 return (IPPROTO_DONE);
182 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
184 return (sctp6_input_with_port(i_pak, offp, 0));
188 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
189 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
193 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
194 (icmp6 == NULL) || (sh == NULL)) {
197 /* First do we even look at it? */
198 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
201 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
202 /* not PACKET TO BIG */
206 * ok we need to look closely. We could even get smarter and look at
207 * anyone that we sent to in case we get a different ICMP that tells
208 * us there is no way to reach a host, but for this impl, all we
209 * care about is MTU discovery.
211 nxtsz = ntohl(icmp6->icmp6_mtu);
212 /* Stop any PMTU timer */
213 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL,
214 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
216 /* Adjust destination size limit */
217 if (net->mtu > nxtsz) {
220 net->mtu -= sizeof(struct udphdr);
223 /* now what about the ep? */
224 if (stcb->asoc.smallest_mtu > nxtsz) {
225 struct sctp_tmit_chunk *chk;
227 /* Adjust that too */
228 stcb->asoc.smallest_mtu = nxtsz;
229 /* now off to subtract IP_DF flag if needed */
231 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
232 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
233 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
236 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
237 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
239 * For this guy we also mark for immediate
240 * resend since we sent to big of chunk
242 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
243 if (chk->sent != SCTP_DATAGRAM_RESEND)
244 stcb->asoc.sent_queue_retran_cnt++;
245 chk->sent = SCTP_DATAGRAM_RESEND;
246 chk->rec.data.doing_fast_retransmit = 0;
248 chk->sent = SCTP_DATAGRAM_RESEND;
249 /* Clear any time so NO RTT is being done */
250 chk->sent_rcv_time.tv_sec = 0;
251 chk->sent_rcv_time.tv_usec = 0;
252 stcb->asoc.total_flight -= chk->send_size;
253 net->flight_size -= chk->send_size;
257 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
260 SCTP_TCB_UNLOCK(stcb);
266 sctp6_notify(struct sctp_inpcb *inp,
267 struct icmp6_hdr *icmph,
270 struct sctp_tcb *stcb,
271 struct sctp_nets *net)
273 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
279 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
280 (sh == NULL) || (to == NULL)) {
282 SCTP_TCB_UNLOCK(stcb);
285 /* First job is to verify the vtag matches what I would send */
286 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
287 SCTP_TCB_UNLOCK(stcb);
290 if (icmph->icmp6_type != ICMP_UNREACH) {
291 /* We only care about unreachable */
292 SCTP_TCB_UNLOCK(stcb);
295 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
296 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
297 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
298 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
299 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
300 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
301 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
302 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
305 * Hmm reachablity problems we must examine closely. If its
306 * not reachable, we may have lost a network. Or if there is
307 * NO protocol at the other end named SCTP. well we consider
310 if (net->dest_state & SCTP_ADDR_REACHABLE) {
311 /* Ok that destination is NOT reachable */
312 net->dest_state &= ~SCTP_ADDR_REACHABLE;
313 net->dest_state &= ~SCTP_ADDR_PF;
314 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
315 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
317 SCTP_TCB_UNLOCK(stcb);
318 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
319 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
321 * Here the peer is either playing tricks on us, including
322 * an address that belongs to someone who does not support
323 * SCTP OR was a userland implementation that shutdown and
324 * now is dead. In either case treat it like a OOTB abort
327 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
328 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
329 so = SCTP_INP_SO(inp);
330 atomic_add_int(&stcb->asoc.refcnt, 1);
331 SCTP_TCB_UNLOCK(stcb);
332 SCTP_SOCKET_LOCK(so, 1);
334 atomic_subtract_int(&stcb->asoc.refcnt, 1);
336 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
337 SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
338 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
339 SCTP_SOCKET_UNLOCK(so, 1);
340 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
342 /* no need to unlock here, since the TCB is gone */
344 SCTP_TCB_UNLOCK(stcb);
351 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
354 struct ip6ctlparam *ip6cp = NULL;
357 vrf_id = SCTP_DEFAULT_VRFID;
359 if (pktdst->sa_family != AF_INET6 ||
360 pktdst->sa_len != sizeof(struct sockaddr_in6))
363 if ((unsigned)cmd >= PRC_NCMDS)
365 if (PRC_IS_REDIRECT(cmd)) {
367 } else if (inet6ctlerrmap[cmd] == 0) {
370 /* if the parameter is from icmp6, decode it. */
372 ip6cp = (struct ip6ctlparam *)d;
374 ip6cp = (struct ip6ctlparam *)NULL;
379 * XXX: We assume that when IPV6 is non NULL, M and OFF are
382 struct sctp_inpcb *inp = NULL;
383 struct sctp_tcb *stcb = NULL;
384 struct sctp_nets *net = NULL;
385 struct sockaddr_in6 final;
387 if (ip6cp->ip6c_m == NULL)
390 /* Check if we can safely examine the SCTP header. */
391 if (ip6cp->ip6c_m->m_pkthdr.len < ip6cp->ip6c_off + sizeof(sh))
394 bzero(&sh, sizeof(sh));
395 bzero(&final, sizeof(final));
398 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
400 ip6cp->ip6c_src->sin6_port = sh.src_port;
401 final.sin6_len = sizeof(final);
402 final.sin6_family = AF_INET6;
403 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
404 final.sin6_port = sh.dest_port;
405 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
406 (struct sockaddr *)ip6cp->ip6c_src,
407 &inp, &net, 1, vrf_id);
408 /* inp's ref-count increased && stcb locked */
409 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
410 if (cmd == PRC_MSGSIZE) {
411 sctp6_notify_mbuf(inp,
416 /* inp's ref-count reduced && stcb unlocked */
418 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
419 (struct sockaddr *)&final,
421 /* inp's ref-count reduced && stcb unlocked */
424 if (PRC_IS_REDIRECT(cmd) && inp) {
425 in6_rtchange((struct in6pcb *)inp,
426 inet6ctlerrmap[cmd]);
429 /* reduce inp's ref-count */
431 SCTP_INP_DECR_REF(inp);
432 SCTP_INP_WUNLOCK(inp);
435 SCTP_TCB_UNLOCK(stcb);
441 * this routine can probably be collasped into the one in sctp_userreq.c
442 * since they do the same thing and now we lookup with a sockaddr
445 sctp6_getcred(SYSCTL_HANDLER_ARGS)
448 struct sockaddr_in6 addrs[2];
449 struct sctp_inpcb *inp;
450 struct sctp_nets *net;
451 struct sctp_tcb *stcb;
455 vrf_id = SCTP_DEFAULT_VRFID;
457 error = priv_check(req->td, PRIV_NETINET_GETCRED);
461 if (req->newlen != sizeof(addrs)) {
462 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
465 if (req->oldlen != sizeof(struct ucred)) {
466 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
469 error = SYSCTL_IN(req, addrs, sizeof(addrs));
473 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
475 &inp, &net, 1, vrf_id);
476 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
477 if ((inp != NULL) && (stcb == NULL)) {
478 /* reduce ref-count */
480 SCTP_INP_DECR_REF(inp);
483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
487 SCTP_TCB_UNLOCK(stcb);
489 * We use the write lock here, only since in the error leg we need
490 * it. If we used RLOCK, then we would have to
491 * wlock/decr/unlock/rlock. Which in theory could create a hole.
492 * Better to use higher wlock.
496 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
498 SCTP_INP_WUNLOCK(inp);
501 cru2x(inp->sctp_socket->so_cred, &xuc);
502 SCTP_INP_WUNLOCK(inp);
503 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
508 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
510 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
513 /* This is the same as the sctp_abort() could be made common */
515 sctp6_abort(struct socket *so)
517 struct sctp_inpcb *inp;
520 inp = (struct sctp_inpcb *)so->so_pcb;
522 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
526 flags = inp->sctp_flags;
527 #ifdef SCTP_LOG_CLOSING
528 sctp_log_closing(inp, NULL, 17);
530 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
531 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
532 #ifdef SCTP_LOG_CLOSING
533 sctp_log_closing(inp, NULL, 16);
535 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
536 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
538 SCTP_SB_CLEAR(so->so_snd);
540 * same for the rcv ones, they are only here for the
543 SCTP_SB_CLEAR(so->so_rcv);
544 /* Now null out the reference, we are completely detached. */
548 flags = inp->sctp_flags;
549 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
550 goto sctp_must_try_again;
557 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
561 struct sctp_inpcb *inp;
562 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
564 inp = (struct sctp_inpcb *)so->so_pcb;
566 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
569 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
570 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
574 error = sctp_inpcb_alloc(so, vrf_id);
577 inp = (struct sctp_inpcb *)so->so_pcb;
579 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
580 inp6 = (struct in6pcb *)inp;
582 inp6->inp_vflag |= INP_IPV6;
583 inp6->in6p_hops = -1; /* use kernel default */
584 inp6->in6p_cksum = -1; /* just to be sure */
587 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
588 * socket as well, because the socket may be bound to an IPv6
589 * wildcard address, which may match an IPv4-mapped IPv6 address.
591 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
594 * Hmm what about the IPSEC stuff that is missing here but in
597 SCTP_INP_WUNLOCK(inp);
602 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
604 struct sctp_inpcb *inp;
608 inp = (struct sctp_inpcb *)so->so_pcb;
610 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
614 switch (addr->sa_family) {
617 if (addr->sa_len != sizeof(struct sockaddr_in)) {
618 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
625 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
626 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
632 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
636 inp6 = (struct in6pcb *)inp;
637 inp6->inp_vflag &= ~INP_IPV4;
638 inp6->inp_vflag |= INP_IPV6;
639 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
640 switch (addr->sa_family) {
643 /* binding v4 addr to v6 socket, so reset flags */
644 inp6->inp_vflag |= INP_IPV4;
645 inp6->inp_vflag &= ~INP_IPV6;
651 struct sockaddr_in6 *sin6_p;
653 sin6_p = (struct sockaddr_in6 *)addr;
655 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
656 inp6->inp_vflag |= INP_IPV4;
659 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
660 struct sockaddr_in sin;
662 in6_sin6_2_sin(&sin, sin6_p);
663 inp6->inp_vflag |= INP_IPV4;
664 inp6->inp_vflag &= ~INP_IPV6;
665 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
675 } else if (addr != NULL) {
676 struct sockaddr_in6 *sin6_p;
678 /* IPV6_V6ONLY socket */
680 if (addr->sa_family == AF_INET) {
681 /* can't bind v4 addr to v6 only socket! */
682 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
686 sin6_p = (struct sockaddr_in6 *)addr;
688 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
689 /* can't bind v4-mapped addrs either! */
690 /* NOTE: we don't support SIIT */
691 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
695 error = sctp_inpcb_bind(so, addr, NULL, p);
701 sctp6_close(struct socket *so)
706 /* This could be made common with sctp_detach() since they are identical */
710 sctp6_disconnect(struct socket *so)
712 return (sctp_disconnect(so));
717 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
718 struct mbuf *control, struct thread *p);
722 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
723 struct mbuf *control, struct thread *p)
725 struct sctp_inpcb *inp;
729 struct sockaddr_in6 *sin6;
732 /* No SPL needed since sctp_output does this */
734 inp = (struct sctp_inpcb *)so->so_pcb;
737 SCTP_RELEASE_PKT(control);
741 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
744 inp6 = (struct in6pcb *)inp;
746 * For the TCP model we may get a NULL addr, if we are a connected
749 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
756 SCTP_RELEASE_PKT(control);
759 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
760 return (EDESTADDRREQ);
763 sin6 = (struct sockaddr_in6 *)addr;
764 if (SCTP_IPV6_V6ONLY(inp6)) {
766 * if IPV6_V6ONLY flag, we discard datagrams destined to a
767 * v4 addr or v4-mapped addr
769 if (addr->sa_family == AF_INET) {
770 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
773 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
774 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
778 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
779 struct sockaddr_in sin;
781 /* convert v4-mapped into v4 addr and send */
782 in6_sin6_2_sin(&sin, sin6);
783 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
787 /* now what about control */
790 SCTP_PRINTF("huh? control set?\n");
791 SCTP_RELEASE_PKT(inp->control);
794 inp->control = control;
798 SCTP_BUF_NEXT(inp->pkt_last) = m;
801 inp->pkt_last = inp->pkt = m;
804 /* FreeBSD and MacOSX uses a flag passed */
805 ((flags & PRUS_MORETOCOME) == 0)
808 * note with the current version this code will only be used
809 * by OpenBSD, NetBSD and FreeBSD have methods for
810 * re-defining sosend() to use sctp_sosend(). One can
811 * optionaly switch back to this code (by changing back the
812 * defininitions but this is not advisable.
816 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
826 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
830 struct sctp_inpcb *inp;
831 struct sctp_tcb *stcb;
835 struct sockaddr_in6 *sin6;
836 union sctp_sockstore store;
841 inp6 = (struct in6pcb *)so->so_pcb;
843 inp = (struct sctp_inpcb *)so->so_pcb;
845 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
846 return (ECONNRESET); /* I made the same as TCP since we are
850 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
853 switch (addr->sa_family) {
856 if (addr->sa_len != sizeof(struct sockaddr_in)) {
857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
864 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
865 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
875 vrf_id = inp->def_vrf_id;
876 SCTP_ASOC_CREATE_LOCK(inp);
878 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
879 SCTP_PCB_FLAGS_UNBOUND) {
880 /* Bind a ephemeral port */
881 SCTP_INP_RUNLOCK(inp);
882 error = sctp6_bind(so, NULL, p);
884 SCTP_ASOC_CREATE_UNLOCK(inp);
890 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
891 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
892 /* We are already connected AND the TCP model */
893 SCTP_INP_RUNLOCK(inp);
894 SCTP_ASOC_CREATE_UNLOCK(inp);
895 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
899 sin6 = (struct sockaddr_in6 *)addr;
900 if (SCTP_IPV6_V6ONLY(inp6)) {
902 * if IPV6_V6ONLY flag, ignore connections destined to a v4
903 * addr or v4-mapped addr
905 if (addr->sa_family == AF_INET) {
906 SCTP_INP_RUNLOCK(inp);
907 SCTP_ASOC_CREATE_UNLOCK(inp);
908 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
911 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
912 SCTP_INP_RUNLOCK(inp);
913 SCTP_ASOC_CREATE_UNLOCK(inp);
914 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
918 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
919 /* convert v4-mapped into v4 addr */
920 in6_sin6_2_sin(&store.sin, sin6);
924 /* Now do we connect? */
925 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
926 stcb = LIST_FIRST(&inp->sctp_asoc_list);
928 SCTP_TCB_UNLOCK(stcb);
930 SCTP_INP_RUNLOCK(inp);
932 SCTP_INP_RUNLOCK(inp);
934 SCTP_INP_INCR_REF(inp);
935 SCTP_INP_WUNLOCK(inp);
936 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
939 SCTP_INP_DECR_REF(inp);
940 SCTP_INP_WUNLOCK(inp);
945 /* Already have or am bring up an association */
946 SCTP_ASOC_CREATE_UNLOCK(inp);
947 SCTP_TCB_UNLOCK(stcb);
948 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
951 /* We are GOOD to go */
952 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, inp->sctp_ep.pre_open_stream_count, p);
953 SCTP_ASOC_CREATE_UNLOCK(inp);
958 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
959 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
960 /* Set the connected flag so we can queue data */
963 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
964 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
966 /* initialize authentication parameters for the assoc */
967 sctp_initialize_auth_params(inp, stcb);
969 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
970 SCTP_TCB_UNLOCK(stcb);
975 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
977 struct sockaddr_in6 *sin6;
978 struct sctp_inpcb *inp;
980 struct sctp_ifa *sctp_ifa;
985 * Do the malloc first in case it blocks.
987 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
990 sin6->sin6_family = AF_INET6;
991 sin6->sin6_len = sizeof(*sin6);
993 inp = (struct sctp_inpcb *)so->so_pcb;
995 SCTP_FREE_SONAME(sin6);
996 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1000 sin6->sin6_port = inp->sctp_lport;
1001 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1002 /* For the bound all case you get back 0 */
1003 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1004 struct sctp_tcb *stcb;
1005 struct sockaddr_in6 *sin_a6;
1006 struct sctp_nets *net;
1009 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1015 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1016 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1018 /* this will make coverity happy */
1021 if (sin_a6->sin6_family == AF_INET6) {
1026 if ((!fnd) || (sin_a6 == NULL)) {
1030 vrf_id = inp->def_vrf_id;
1031 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1033 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1036 /* For the bound all case you get back 0 */
1038 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1041 /* Take the first IPv6 address in the list */
1042 struct sctp_laddr *laddr;
1045 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1046 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1047 struct sockaddr_in6 *sin_a;
1049 sin_a = &laddr->ifa->address.sin6;
1050 sin6->sin6_addr = sin_a->sin6_addr;
1056 SCTP_FREE_SONAME(sin6);
1057 SCTP_INP_RUNLOCK(inp);
1058 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1062 SCTP_INP_RUNLOCK(inp);
1063 /* Scoping things for v6 */
1064 if ((error = sa6_recoverscope(sin6)) != 0) {
1065 SCTP_FREE_SONAME(sin6);
1068 (*addr) = (struct sockaddr *)sin6;
1073 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1075 struct sockaddr_in6 *sin6;
1077 struct sockaddr_in6 *sin_a6;
1078 struct sctp_inpcb *inp;
1079 struct sctp_tcb *stcb;
1080 struct sctp_nets *net;
1083 /* Do the malloc first in case it blocks. */
1084 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1087 sin6->sin6_family = AF_INET6;
1088 sin6->sin6_len = sizeof(*sin6);
1090 inp = (struct sctp_inpcb *)so->so_pcb;
1091 if ((inp == NULL) ||
1092 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1093 /* UDP type and listeners will drop out here */
1094 SCTP_FREE_SONAME(sin6);
1095 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1098 SCTP_INP_RLOCK(inp);
1099 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1101 SCTP_TCB_LOCK(stcb);
1103 SCTP_INP_RUNLOCK(inp);
1105 SCTP_FREE_SONAME(sin6);
1106 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1107 return (ECONNRESET);
1110 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1111 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1112 if (sin_a6->sin6_family == AF_INET6) {
1114 sin6->sin6_port = stcb->rport;
1115 sin6->sin6_addr = sin_a6->sin6_addr;
1119 SCTP_TCB_UNLOCK(stcb);
1121 /* No IPv4 address */
1122 SCTP_FREE_SONAME(sin6);
1123 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1126 if ((error = sa6_recoverscope(sin6)) != 0) {
1127 SCTP_FREE_SONAME(sin6);
1128 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1131 *addr = (struct sockaddr *)sin6;
1136 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1139 struct sockaddr *addr;
1142 struct in6pcb *inp6 = sotoin6pcb(so);
1146 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1149 /* allow v6 addresses precedence */
1150 error = sctp6_getaddr(so, nam);
1153 /* try v4 next if v6 failed */
1154 error = sctp_ingetaddr(so, nam);
1159 /* if I'm V6ONLY, convert it to v4-mapped */
1160 if (SCTP_IPV6_V6ONLY(inp6)) {
1161 struct sockaddr_in6 sin6;
1163 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1164 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1173 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1176 struct sockaddr *addr;
1179 struct in6pcb *inp6 = sotoin6pcb(so);
1183 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1186 /* allow v6 addresses precedence */
1187 error = sctp6_peeraddr(so, nam);
1190 /* try v4 next if v6 failed */
1191 error = sctp_peeraddr(so, nam);
1196 /* if I'm V6ONLY, convert it to v4-mapped */
1197 if (SCTP_IPV6_V6ONLY(inp6)) {
1198 struct sockaddr_in6 sin6;
1200 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1201 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1208 struct pr_usrreqs sctp6_usrreqs = {
1209 .pru_abort = sctp6_abort,
1210 .pru_accept = sctp_accept,
1211 .pru_attach = sctp6_attach,
1212 .pru_bind = sctp6_bind,
1213 .pru_connect = sctp6_connect,
1214 .pru_control = in6_control,
1215 .pru_close = sctp6_close,
1216 .pru_detach = sctp6_close,
1217 .pru_sopoll = sopoll_generic,
1218 .pru_flush = sctp_flush,
1219 .pru_disconnect = sctp6_disconnect,
1220 .pru_listen = sctp_listen,
1221 .pru_peeraddr = sctp6_getpeeraddr,
1222 .pru_send = sctp6_send,
1223 .pru_shutdown = sctp_shutdown,
1224 .pru_sockaddr = sctp6_in6getaddr,
1225 .pru_sosend = sctp_sosend,
1226 .pru_soreceive = sctp_soreceive