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>
58 extern struct protosw inetsw[];
61 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
67 struct sockaddr_in6 src, dst;
70 struct sctp_chunkhdr *ch;
72 #if !defined(SCTP_WITH_NO_CSUM)
80 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
81 SCTP_RELEASE_PKT(*i_pak);
82 return (IPPROTO_DONE);
84 m = SCTP_HEADER_TO_CHAIN(*i_pak);
85 #ifdef SCTP_MBUF_LOGGING
86 /* Log in any input mbufs */
87 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
88 sctp_log_mbc(m, SCTP_MBUF_INPUT);
91 #ifdef SCTP_PACKET_LOGGING
92 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
96 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
97 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
99 if_name(m->m_pkthdr.rcvif),
100 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
101 mflowid = m->m_pkthdr.flowid;
102 mflowtype = M_HASHTYPE_GET(m);
103 fibnum = M_GETFIB(m);
104 SCTP_STAT_INCR(sctps_recvpackets);
105 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
106 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
107 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
108 ip6 = mtod(m, struct ip6_hdr *);
109 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
110 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
112 SCTP_STAT_INCR(sctps_hdrops);
113 return (IPPROTO_DONE);
115 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
116 offset -= sizeof(struct sctp_chunkhdr);
117 memset(&src, 0, sizeof(struct sockaddr_in6));
118 src.sin6_family = AF_INET6;
119 src.sin6_len = sizeof(struct sockaddr_in6);
120 src.sin6_port = sh->src_port;
121 src.sin6_addr = ip6->ip6_src;
122 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
125 memset(&dst, 0, sizeof(struct sockaddr_in6));
126 dst.sin6_family = AF_INET6;
127 dst.sin6_len = sizeof(struct sockaddr_in6);
128 dst.sin6_port = sh->dest_port;
129 dst.sin6_addr = ip6->ip6_dst;
130 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
133 length = ntohs(ip6->ip6_plen) + iphlen;
134 /* Validate mbuf chain length with IP payload length. */
135 if (SCTP_HEADER_LEN(m) != length) {
136 SCTPDBG(SCTP_DEBUG_INPUT1,
137 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
138 SCTP_STAT_INCR(sctps_hdrops);
141 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
144 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
145 #if defined(SCTP_WITH_NO_CSUM)
146 SCTP_STAT_INCR(sctps_recvnocrc);
148 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
149 SCTP_STAT_INCR(sctps_recvhwcrc);
152 SCTP_STAT_INCR(sctps_recvswcrc);
156 sctp_common_input_processing(&m, iphlen, offset, length,
157 (struct sockaddr *)&src,
158 (struct sockaddr *)&dst,
160 #if !defined(SCTP_WITH_NO_CSUM)
164 mflowtype, mflowid, fibnum,
170 return (IPPROTO_DONE);
175 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
177 return (sctp6_input_with_port(i_pak, offp, 0));
181 sctp6_notify(struct sctp_inpcb *inp,
182 struct sctp_tcb *stcb,
183 struct sctp_nets *net,
188 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
193 switch (icmp6_type) {
194 case ICMP6_DST_UNREACH:
195 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
196 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
197 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
198 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
199 /* Mark the net unreachable. */
200 if (net->dest_state & SCTP_ADDR_REACHABLE) {
201 /* Ok that destination is not reachable */
202 net->dest_state &= ~SCTP_ADDR_REACHABLE;
203 net->dest_state &= ~SCTP_ADDR_PF;
204 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
205 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
208 SCTP_TCB_UNLOCK(stcb);
210 case ICMP6_PARAM_PROB:
211 /* Treat it like an ABORT. */
212 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
213 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
214 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
215 so = SCTP_INP_SO(inp);
216 atomic_add_int(&stcb->asoc.refcnt, 1);
217 SCTP_TCB_UNLOCK(stcb);
218 SCTP_SOCKET_LOCK(so, 1);
220 atomic_subtract_int(&stcb->asoc.refcnt, 1);
222 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
223 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
224 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
225 SCTP_SOCKET_UNLOCK(so, 1);
228 SCTP_TCB_UNLOCK(stcb);
231 case ICMP6_PACKET_TOO_BIG:
232 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
234 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
235 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
239 /* Update the path MTU. */
241 next_mtu -= sizeof(struct udphdr);
243 if (net->mtu > next_mtu) {
246 /* Update the association MTU */
247 if (stcb->asoc.smallest_mtu > next_mtu) {
248 sctp_pathmtu_adjustment(stcb, next_mtu);
250 /* Finally, start the PMTU timer if it was running before. */
252 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
254 SCTP_TCB_UNLOCK(stcb);
257 SCTP_TCB_UNLOCK(stcb);
263 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
265 struct ip6ctlparam *ip6cp;
266 struct sctp_inpcb *inp;
267 struct sctp_tcb *stcb;
268 struct sctp_nets *net;
270 struct sockaddr_in6 src, dst;
272 if (pktdst->sa_family != AF_INET6 ||
273 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
276 if ((unsigned)cmd >= PRC_NCMDS) {
279 if (PRC_IS_REDIRECT(cmd)) {
281 } else if (inet6ctlerrmap[cmd] == 0) {
284 /* If the parameter is from icmp6, decode it. */
286 ip6cp = (struct ip6ctlparam *)d;
288 ip6cp = (struct ip6ctlparam *)NULL;
293 * XXX: We assume that when IPV6 is non NULL, M and OFF are
296 if (ip6cp->ip6c_m == NULL) {
300 * Check if we can safely examine the ports and the
301 * verification tag of the SCTP common header.
303 if (ip6cp->ip6c_m->m_pkthdr.len <
304 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
307 /* Copy out the port numbers and the verification tag. */
308 bzero(&sh, sizeof(sh));
309 m_copydata(ip6cp->ip6c_m,
311 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
313 memset(&src, 0, sizeof(struct sockaddr_in6));
314 src.sin6_family = AF_INET6;
315 src.sin6_len = sizeof(struct sockaddr_in6);
316 src.sin6_port = sh.src_port;
317 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
318 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
321 memset(&dst, 0, sizeof(struct sockaddr_in6));
322 dst.sin6_family = AF_INET6;
323 dst.sin6_len = sizeof(struct sockaddr_in6);
324 dst.sin6_port = sh.dest_port;
325 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
326 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
331 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
332 (struct sockaddr *)&src,
333 &inp, &net, 1, SCTP_DEFAULT_VRFID);
334 if ((stcb != NULL) &&
337 /* Check the verification tag */
338 if (ntohl(sh.v_tag) != 0) {
340 * This must be the verification tag used
341 * for sending out packets. We don't
342 * consider packets reflecting the
345 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
346 SCTP_TCB_UNLOCK(stcb);
350 if (ip6cp->ip6c_m->m_pkthdr.len >=
351 ip6cp->ip6c_off + sizeof(struct sctphdr) +
352 sizeof(struct sctp_chunkhdr) +
353 offsetof(struct sctp_init, a_rwnd)) {
355 * In this case we can check if we
356 * got an INIT chunk and if the
357 * initiate tag matches.
359 uint32_t initiate_tag;
362 m_copydata(ip6cp->ip6c_m,
364 sizeof(struct sctphdr),
366 (caddr_t)&chunk_type);
367 m_copydata(ip6cp->ip6c_m,
369 sizeof(struct sctphdr) +
370 sizeof(struct sctp_chunkhdr),
372 (caddr_t)&initiate_tag);
373 if ((chunk_type != SCTP_INITIATION) ||
374 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
375 SCTP_TCB_UNLOCK(stcb);
379 SCTP_TCB_UNLOCK(stcb);
383 sctp6_notify(inp, stcb, net,
384 ip6cp->ip6c_icmp6->icmp6_type,
385 ip6cp->ip6c_icmp6->icmp6_code,
386 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
388 if ((stcb == NULL) && (inp != NULL)) {
389 /* reduce inp's ref-count */
391 SCTP_INP_DECR_REF(inp);
392 SCTP_INP_WUNLOCK(inp);
395 SCTP_TCB_UNLOCK(stcb);
402 * this routine can probably be collasped into the one in sctp_userreq.c
403 * since they do the same thing and now we lookup with a sockaddr
406 sctp6_getcred(SYSCTL_HANDLER_ARGS)
409 struct sockaddr_in6 addrs[2];
410 struct sctp_inpcb *inp;
411 struct sctp_nets *net;
412 struct sctp_tcb *stcb;
416 vrf_id = SCTP_DEFAULT_VRFID;
418 error = priv_check(req->td, PRIV_NETINET_GETCRED);
422 if (req->newlen != sizeof(addrs)) {
423 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
426 if (req->oldlen != sizeof(struct ucred)) {
427 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
430 error = SYSCTL_IN(req, addrs, sizeof(addrs));
434 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
436 &inp, &net, 1, vrf_id);
437 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
438 if ((inp != NULL) && (stcb == NULL)) {
439 /* reduce ref-count */
441 SCTP_INP_DECR_REF(inp);
444 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
448 SCTP_TCB_UNLOCK(stcb);
450 * We use the write lock here, only since in the error leg we need
451 * it. If we used RLOCK, then we would have to
452 * wlock/decr/unlock/rlock. Which in theory could create a hole.
453 * Better to use higher wlock.
457 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
459 SCTP_INP_WUNLOCK(inp);
462 cru2x(inp->sctp_socket->so_cred, &xuc);
463 SCTP_INP_WUNLOCK(inp);
464 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
469 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
471 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
474 /* This is the same as the sctp_abort() could be made common */
476 sctp6_abort(struct socket *so)
478 struct sctp_inpcb *inp;
481 inp = (struct sctp_inpcb *)so->so_pcb;
483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
487 flags = inp->sctp_flags;
488 #ifdef SCTP_LOG_CLOSING
489 sctp_log_closing(inp, NULL, 17);
491 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
492 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
493 #ifdef SCTP_LOG_CLOSING
494 sctp_log_closing(inp, NULL, 16);
496 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
497 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
499 SCTP_SB_CLEAR(so->so_snd);
501 * same for the rcv ones, they are only here for the
504 SCTP_SB_CLEAR(so->so_rcv);
505 /* Now null out the reference, we are completely detached. */
509 flags = inp->sctp_flags;
510 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
511 goto sctp_must_try_again;
518 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
522 struct sctp_inpcb *inp;
523 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
525 inp = (struct sctp_inpcb *)so->so_pcb;
527 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
530 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
531 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
535 error = sctp_inpcb_alloc(so, vrf_id);
538 inp = (struct sctp_inpcb *)so->so_pcb;
540 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
541 inp6 = (struct in6pcb *)inp;
543 inp6->inp_vflag |= INP_IPV6;
544 inp6->in6p_hops = -1; /* use kernel default */
545 inp6->in6p_cksum = -1; /* just to be sure */
548 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
549 * socket as well, because the socket may be bound to an IPv6
550 * wildcard address, which may match an IPv4-mapped IPv6 address.
552 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
554 SCTP_INP_WUNLOCK(inp);
559 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
561 struct sctp_inpcb *inp;
565 inp = (struct sctp_inpcb *)so->so_pcb;
567 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
571 switch (addr->sa_family) {
574 if (addr->sa_len != sizeof(struct sockaddr_in)) {
575 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
582 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
583 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
589 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
593 inp6 = (struct in6pcb *)inp;
594 inp6->inp_vflag &= ~INP_IPV4;
595 inp6->inp_vflag |= INP_IPV6;
596 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
597 switch (addr->sa_family) {
600 /* binding v4 addr to v6 socket, so reset flags */
601 inp6->inp_vflag |= INP_IPV4;
602 inp6->inp_vflag &= ~INP_IPV6;
608 struct sockaddr_in6 *sin6_p;
610 sin6_p = (struct sockaddr_in6 *)addr;
612 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
613 inp6->inp_vflag |= INP_IPV4;
616 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
617 struct sockaddr_in sin;
619 in6_sin6_2_sin(&sin, sin6_p);
620 inp6->inp_vflag |= INP_IPV4;
621 inp6->inp_vflag &= ~INP_IPV6;
622 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
632 } else if (addr != NULL) {
633 struct sockaddr_in6 *sin6_p;
635 /* IPV6_V6ONLY socket */
637 if (addr->sa_family == AF_INET) {
638 /* can't bind v4 addr to v6 only socket! */
639 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
643 sin6_p = (struct sockaddr_in6 *)addr;
645 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
646 /* can't bind v4-mapped addrs either! */
647 /* NOTE: we don't support SIIT */
648 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
652 error = sctp_inpcb_bind(so, addr, NULL, p);
658 sctp6_close(struct socket *so)
663 /* This could be made common with sctp_detach() since they are identical */
667 sctp6_disconnect(struct socket *so)
669 return (sctp_disconnect(so));
674 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
675 struct mbuf *control, struct thread *p);
679 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
680 struct mbuf *control, struct thread *p)
682 struct sctp_inpcb *inp;
686 struct sockaddr_in6 *sin6;
688 /* No SPL needed since sctp_output does this */
690 inp = (struct sctp_inpcb *)so->so_pcb;
693 SCTP_RELEASE_PKT(control);
697 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
700 inp6 = (struct in6pcb *)inp;
702 * For the TCP model we may get a NULL addr, if we are a connected
705 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
712 SCTP_RELEASE_PKT(control);
715 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
716 return (EDESTADDRREQ);
719 sin6 = (struct sockaddr_in6 *)addr;
720 if (SCTP_IPV6_V6ONLY(inp6)) {
722 * if IPV6_V6ONLY flag, we discard datagrams destined to a
723 * v4 addr or v4-mapped addr
725 if (addr->sa_family == AF_INET) {
726 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
729 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
734 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
735 struct sockaddr_in sin;
737 /* convert v4-mapped into v4 addr and send */
738 in6_sin6_2_sin(&sin, sin6);
739 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
743 /* now what about control */
746 SCTP_PRINTF("huh? control set?\n");
747 SCTP_RELEASE_PKT(inp->control);
750 inp->control = control;
754 SCTP_BUF_NEXT(inp->pkt_last) = m;
757 inp->pkt_last = inp->pkt = m;
760 /* FreeBSD and MacOSX uses a flag passed */
761 ((flags & PRUS_MORETOCOME) == 0)
764 * note with the current version this code will only be used
765 * by OpenBSD, NetBSD and FreeBSD have methods for
766 * re-defining sosend() to use sctp_sosend(). One can
767 * optionaly switch back to this code (by changing back the
768 * defininitions but this is not advisable.
772 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
782 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
786 struct sctp_inpcb *inp;
787 struct sctp_tcb *stcb;
790 struct sockaddr_in6 *sin6;
791 union sctp_sockstore store;
795 inp6 = (struct in6pcb *)so->so_pcb;
797 inp = (struct sctp_inpcb *)so->so_pcb;
799 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
800 return (ECONNRESET); /* I made the same as TCP since we are
804 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
807 switch (addr->sa_family) {
810 if (addr->sa_len != sizeof(struct sockaddr_in)) {
811 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
819 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
825 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
829 vrf_id = inp->def_vrf_id;
830 SCTP_ASOC_CREATE_LOCK(inp);
832 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
833 SCTP_PCB_FLAGS_UNBOUND) {
834 /* Bind a ephemeral port */
835 SCTP_INP_RUNLOCK(inp);
836 error = sctp6_bind(so, NULL, p);
838 SCTP_ASOC_CREATE_UNLOCK(inp);
844 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
845 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
846 /* We are already connected AND the TCP model */
847 SCTP_INP_RUNLOCK(inp);
848 SCTP_ASOC_CREATE_UNLOCK(inp);
849 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
853 sin6 = (struct sockaddr_in6 *)addr;
854 if (SCTP_IPV6_V6ONLY(inp6)) {
856 * if IPV6_V6ONLY flag, ignore connections destined to a v4
857 * addr or v4-mapped addr
859 if (addr->sa_family == AF_INET) {
860 SCTP_INP_RUNLOCK(inp);
861 SCTP_ASOC_CREATE_UNLOCK(inp);
862 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
865 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
866 SCTP_INP_RUNLOCK(inp);
867 SCTP_ASOC_CREATE_UNLOCK(inp);
868 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
872 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
873 /* convert v4-mapped into v4 addr */
874 in6_sin6_2_sin(&store.sin, sin6);
878 /* Now do we connect? */
879 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
880 stcb = LIST_FIRST(&inp->sctp_asoc_list);
882 SCTP_TCB_UNLOCK(stcb);
884 SCTP_INP_RUNLOCK(inp);
886 SCTP_INP_RUNLOCK(inp);
888 SCTP_INP_INCR_REF(inp);
889 SCTP_INP_WUNLOCK(inp);
890 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
893 SCTP_INP_DECR_REF(inp);
894 SCTP_INP_WUNLOCK(inp);
899 /* Already have or am bring up an association */
900 SCTP_ASOC_CREATE_UNLOCK(inp);
901 SCTP_TCB_UNLOCK(stcb);
902 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
905 /* We are GOOD to go */
906 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
907 inp->sctp_ep.pre_open_stream_count,
908 inp->sctp_ep.port, p);
909 SCTP_ASOC_CREATE_UNLOCK(inp);
914 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
915 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
916 /* Set the connected flag so we can queue data */
919 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
920 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
922 /* initialize authentication parameters for the assoc */
923 sctp_initialize_auth_params(inp, stcb);
925 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
926 SCTP_TCB_UNLOCK(stcb);
931 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
933 struct sockaddr_in6 *sin6;
934 struct sctp_inpcb *inp;
936 struct sctp_ifa *sctp_ifa;
941 * Do the malloc first in case it blocks.
943 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
946 sin6->sin6_family = AF_INET6;
947 sin6->sin6_len = sizeof(*sin6);
949 inp = (struct sctp_inpcb *)so->so_pcb;
951 SCTP_FREE_SONAME(sin6);
952 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
956 sin6->sin6_port = inp->sctp_lport;
957 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
958 /* For the bound all case you get back 0 */
959 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
960 struct sctp_tcb *stcb;
961 struct sockaddr_in6 *sin_a6;
962 struct sctp_nets *net;
965 stcb = LIST_FIRST(&inp->sctp_asoc_list);
967 SCTP_INP_RUNLOCK(inp);
968 SCTP_FREE_SONAME(sin6);
969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
974 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
975 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
977 /* this will make coverity happy */
980 if (sin_a6->sin6_family == AF_INET6) {
985 if ((!fnd) || (sin_a6 == NULL)) {
987 SCTP_INP_RUNLOCK(inp);
988 SCTP_FREE_SONAME(sin6);
989 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
992 vrf_id = inp->def_vrf_id;
993 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
995 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
998 /* For the bound all case you get back 0 */
999 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1002 /* Take the first IPv6 address in the list */
1003 struct sctp_laddr *laddr;
1006 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1007 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1008 struct sockaddr_in6 *sin_a;
1010 sin_a = &laddr->ifa->address.sin6;
1011 sin6->sin6_addr = sin_a->sin6_addr;
1017 SCTP_FREE_SONAME(sin6);
1018 SCTP_INP_RUNLOCK(inp);
1019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1023 SCTP_INP_RUNLOCK(inp);
1024 /* Scoping things for v6 */
1025 if ((error = sa6_recoverscope(sin6)) != 0) {
1026 SCTP_FREE_SONAME(sin6);
1029 (*addr) = (struct sockaddr *)sin6;
1034 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1036 struct sockaddr_in6 *sin6;
1038 struct sockaddr_in6 *sin_a6;
1039 struct sctp_inpcb *inp;
1040 struct sctp_tcb *stcb;
1041 struct sctp_nets *net;
1044 /* Do the malloc first in case it blocks. */
1045 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1048 sin6->sin6_family = AF_INET6;
1049 sin6->sin6_len = sizeof(*sin6);
1051 inp = (struct sctp_inpcb *)so->so_pcb;
1052 if ((inp == NULL) ||
1053 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1054 /* UDP type and listeners will drop out here */
1055 SCTP_FREE_SONAME(sin6);
1056 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1059 SCTP_INP_RLOCK(inp);
1060 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1062 SCTP_TCB_LOCK(stcb);
1064 SCTP_INP_RUNLOCK(inp);
1066 SCTP_FREE_SONAME(sin6);
1067 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1068 return (ECONNRESET);
1071 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1072 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1073 if (sin_a6->sin6_family == AF_INET6) {
1075 sin6->sin6_port = stcb->rport;
1076 sin6->sin6_addr = sin_a6->sin6_addr;
1080 SCTP_TCB_UNLOCK(stcb);
1082 /* No IPv4 address */
1083 SCTP_FREE_SONAME(sin6);
1084 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1087 if ((error = sa6_recoverscope(sin6)) != 0) {
1088 SCTP_FREE_SONAME(sin6);
1089 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1092 *addr = (struct sockaddr *)sin6;
1097 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1099 struct in6pcb *inp6 = sotoin6pcb(so);
1103 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1106 /* allow v6 addresses precedence */
1107 error = sctp6_getaddr(so, nam);
1110 struct sockaddr_in6 *sin6;
1112 /* try v4 next if v6 failed */
1113 error = sctp_ingetaddr(so, nam);
1117 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1119 SCTP_FREE_SONAME(*nam);
1122 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1123 SCTP_FREE_SONAME(*nam);
1124 *nam = (struct sockaddr *)sin6;
1132 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1134 struct in6pcb *inp6 = sotoin6pcb(so);
1138 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1141 /* allow v6 addresses precedence */
1142 error = sctp6_peeraddr(so, nam);
1145 struct sockaddr_in6 *sin6;
1147 /* try v4 next if v6 failed */
1148 error = sctp_peeraddr(so, nam);
1152 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1154 SCTP_FREE_SONAME(*nam);
1157 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1158 SCTP_FREE_SONAME(*nam);
1159 *nam = (struct sockaddr *)sin6;
1165 struct pr_usrreqs sctp6_usrreqs = {
1166 .pru_abort = sctp6_abort,
1167 .pru_accept = sctp_accept,
1168 .pru_attach = sctp6_attach,
1169 .pru_bind = sctp6_bind,
1170 .pru_connect = sctp6_connect,
1171 .pru_control = in6_control,
1172 .pru_close = sctp6_close,
1173 .pru_detach = sctp6_close,
1174 .pru_sopoll = sopoll_generic,
1175 .pru_flush = sctp_flush,
1176 .pru_disconnect = sctp6_disconnect,
1177 .pru_listen = sctp_listen,
1178 .pru_peeraddr = sctp6_getpeeraddr,
1179 .pru_send = sctp6_send,
1180 .pru_shutdown = sctp_shutdown,
1181 .pru_sockaddr = sctp6_in6getaddr,
1182 .pru_sosend = sctp_sosend,
1183 .pru_soreceive = sctp_soreceive