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 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0) {
233 SCTP_TCB_UNLOCK(stcb);
236 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
238 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
239 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
243 /* Update the path MTU. */
245 next_mtu -= sizeof(struct udphdr);
247 if (net->mtu > next_mtu) {
250 /* Update the association MTU */
251 if (stcb->asoc.smallest_mtu > next_mtu) {
252 sctp_pathmtu_adjustment(stcb, next_mtu);
254 /* Finally, start the PMTU timer if it was running before. */
256 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
258 SCTP_TCB_UNLOCK(stcb);
261 SCTP_TCB_UNLOCK(stcb);
267 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
269 struct ip6ctlparam *ip6cp;
270 struct sctp_inpcb *inp;
271 struct sctp_tcb *stcb;
272 struct sctp_nets *net;
274 struct sockaddr_in6 src, dst;
276 if (pktdst->sa_family != AF_INET6 ||
277 pktdst->sa_len != sizeof(struct sockaddr_in6)) {
280 if ((unsigned)cmd >= PRC_NCMDS) {
283 if (PRC_IS_REDIRECT(cmd)) {
285 } else if (inet6ctlerrmap[cmd] == 0) {
288 /* If the parameter is from icmp6, decode it. */
290 ip6cp = (struct ip6ctlparam *)d;
292 ip6cp = (struct ip6ctlparam *)NULL;
297 * XXX: We assume that when IPV6 is non NULL, M and OFF are
300 if (ip6cp->ip6c_m == NULL) {
304 * Check if we can safely examine the ports and the
305 * verification tag of the SCTP common header.
307 if (ip6cp->ip6c_m->m_pkthdr.len <
308 (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
311 /* Copy out the port numbers and the verification tag. */
312 memset(&sh, 0, sizeof(sh));
313 m_copydata(ip6cp->ip6c_m,
315 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
317 memset(&src, 0, sizeof(struct sockaddr_in6));
318 src.sin6_family = AF_INET6;
319 src.sin6_len = sizeof(struct sockaddr_in6);
320 src.sin6_port = sh.src_port;
321 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
322 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
325 memset(&dst, 0, sizeof(struct sockaddr_in6));
326 dst.sin6_family = AF_INET6;
327 dst.sin6_len = sizeof(struct sockaddr_in6);
328 dst.sin6_port = sh.dest_port;
329 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
330 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
335 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
336 (struct sockaddr *)&src,
337 &inp, &net, 1, SCTP_DEFAULT_VRFID);
338 if ((stcb != NULL) &&
341 /* Check the verification tag */
342 if (ntohl(sh.v_tag) != 0) {
344 * This must be the verification tag used
345 * for sending out packets. We don't
346 * consider packets reflecting the
349 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
350 SCTP_TCB_UNLOCK(stcb);
354 if (ip6cp->ip6c_m->m_pkthdr.len >=
355 ip6cp->ip6c_off + sizeof(struct sctphdr) +
356 sizeof(struct sctp_chunkhdr) +
357 offsetof(struct sctp_init, a_rwnd)) {
359 * In this case we can check if we
360 * got an INIT chunk and if the
361 * initiate tag matches.
363 uint32_t initiate_tag;
366 m_copydata(ip6cp->ip6c_m,
368 sizeof(struct sctphdr),
370 (caddr_t)&chunk_type);
371 m_copydata(ip6cp->ip6c_m,
373 sizeof(struct sctphdr) +
374 sizeof(struct sctp_chunkhdr),
376 (caddr_t)&initiate_tag);
377 if ((chunk_type != SCTP_INITIATION) ||
378 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
379 SCTP_TCB_UNLOCK(stcb);
383 SCTP_TCB_UNLOCK(stcb);
387 sctp6_notify(inp, stcb, net,
388 ip6cp->ip6c_icmp6->icmp6_type,
389 ip6cp->ip6c_icmp6->icmp6_code,
390 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
392 if ((stcb == NULL) && (inp != NULL)) {
393 /* reduce inp's ref-count */
395 SCTP_INP_DECR_REF(inp);
396 SCTP_INP_WUNLOCK(inp);
399 SCTP_TCB_UNLOCK(stcb);
406 * this routine can probably be collasped into the one in sctp_userreq.c
407 * since they do the same thing and now we lookup with a sockaddr
410 sctp6_getcred(SYSCTL_HANDLER_ARGS)
413 struct sockaddr_in6 addrs[2];
414 struct sctp_inpcb *inp;
415 struct sctp_nets *net;
416 struct sctp_tcb *stcb;
420 vrf_id = SCTP_DEFAULT_VRFID;
422 error = priv_check(req->td, PRIV_NETINET_GETCRED);
426 if (req->newlen != sizeof(addrs)) {
427 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
430 if (req->oldlen != sizeof(struct ucred)) {
431 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
434 error = SYSCTL_IN(req, addrs, sizeof(addrs));
438 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
440 &inp, &net, 1, vrf_id);
441 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
442 if ((inp != NULL) && (stcb == NULL)) {
443 /* reduce ref-count */
445 SCTP_INP_DECR_REF(inp);
448 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
452 SCTP_TCB_UNLOCK(stcb);
454 * We use the write lock here, only since in the error leg we need
455 * it. If we used RLOCK, then we would have to
456 * wlock/decr/unlock/rlock. Which in theory could create a hole.
457 * Better to use higher wlock.
461 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
463 SCTP_INP_WUNLOCK(inp);
466 cru2x(inp->sctp_socket->so_cred, &xuc);
467 SCTP_INP_WUNLOCK(inp);
468 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
473 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
475 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
478 /* This is the same as the sctp_abort() could be made common */
480 sctp6_abort(struct socket *so)
482 struct sctp_inpcb *inp;
485 inp = (struct sctp_inpcb *)so->so_pcb;
487 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
491 flags = inp->sctp_flags;
492 #ifdef SCTP_LOG_CLOSING
493 sctp_log_closing(inp, NULL, 17);
495 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
496 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
497 #ifdef SCTP_LOG_CLOSING
498 sctp_log_closing(inp, NULL, 16);
500 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
501 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
503 SCTP_SB_CLEAR(so->so_snd);
505 * same for the rcv ones, they are only here for the
508 SCTP_SB_CLEAR(so->so_rcv);
509 /* Now null out the reference, we are completely detached. */
513 flags = inp->sctp_flags;
514 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
515 goto sctp_must_try_again;
522 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
526 struct sctp_inpcb *inp;
527 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
529 inp = (struct sctp_inpcb *)so->so_pcb;
531 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
534 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
535 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
539 error = sctp_inpcb_alloc(so, vrf_id);
542 inp = (struct sctp_inpcb *)so->so_pcb;
544 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
545 inp6 = (struct in6pcb *)inp;
547 inp6->inp_vflag |= INP_IPV6;
548 inp6->in6p_hops = -1; /* use kernel default */
549 inp6->in6p_cksum = -1; /* just to be sure */
552 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
553 * socket as well, because the socket may be bound to an IPv6
554 * wildcard address, which may match an IPv4-mapped IPv6 address.
556 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
558 SCTP_INP_WUNLOCK(inp);
563 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
565 struct sctp_inpcb *inp;
569 inp = (struct sctp_inpcb *)so->so_pcb;
571 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
575 switch (addr->sa_family) {
578 if (addr->sa_len != sizeof(struct sockaddr_in)) {
579 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
586 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
587 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
593 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
597 inp6 = (struct in6pcb *)inp;
598 inp6->inp_vflag &= ~INP_IPV4;
599 inp6->inp_vflag |= INP_IPV6;
600 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
601 switch (addr->sa_family) {
604 /* binding v4 addr to v6 socket, so reset flags */
605 inp6->inp_vflag |= INP_IPV4;
606 inp6->inp_vflag &= ~INP_IPV6;
612 struct sockaddr_in6 *sin6_p;
614 sin6_p = (struct sockaddr_in6 *)addr;
616 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
617 inp6->inp_vflag |= INP_IPV4;
620 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
621 struct sockaddr_in sin;
623 in6_sin6_2_sin(&sin, sin6_p);
624 inp6->inp_vflag |= INP_IPV4;
625 inp6->inp_vflag &= ~INP_IPV6;
626 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
636 } else if (addr != NULL) {
637 struct sockaddr_in6 *sin6_p;
639 /* IPV6_V6ONLY socket */
641 if (addr->sa_family == AF_INET) {
642 /* can't bind v4 addr to v6 only socket! */
643 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
647 sin6_p = (struct sockaddr_in6 *)addr;
649 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
650 /* can't bind v4-mapped addrs either! */
651 /* NOTE: we don't support SIIT */
652 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
656 error = sctp_inpcb_bind(so, addr, NULL, p);
662 sctp6_close(struct socket *so)
667 /* This could be made common with sctp_detach() since they are identical */
671 sctp6_disconnect(struct socket *so)
673 return (sctp_disconnect(so));
678 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
679 struct mbuf *control, struct thread *p);
683 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
684 struct mbuf *control, struct thread *p)
686 struct sctp_inpcb *inp;
690 struct sockaddr_in6 *sin6;
692 /* No SPL needed since sctp_output does this */
694 inp = (struct sctp_inpcb *)so->so_pcb;
697 SCTP_RELEASE_PKT(control);
701 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
704 inp6 = (struct in6pcb *)inp;
706 * For the TCP model we may get a NULL addr, if we are a connected
709 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
716 SCTP_RELEASE_PKT(control);
719 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
720 return (EDESTADDRREQ);
723 sin6 = (struct sockaddr_in6 *)addr;
724 if (SCTP_IPV6_V6ONLY(inp6)) {
726 * if IPV6_V6ONLY flag, we discard datagrams destined to a
727 * v4 addr or v4-mapped addr
729 if (addr->sa_family == AF_INET) {
730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
733 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
734 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
738 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
739 struct sockaddr_in sin;
741 /* convert v4-mapped into v4 addr and send */
742 in6_sin6_2_sin(&sin, sin6);
743 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
747 /* now what about control */
750 SCTP_PRINTF("huh? control set?\n");
751 SCTP_RELEASE_PKT(inp->control);
754 inp->control = control;
758 SCTP_BUF_NEXT(inp->pkt_last) = m;
761 inp->pkt_last = inp->pkt = m;
764 /* FreeBSD and MacOSX uses a flag passed */
765 ((flags & PRUS_MORETOCOME) == 0)
768 * note with the current version this code will only be used
769 * by OpenBSD, NetBSD and FreeBSD have methods for
770 * re-defining sosend() to use sctp_sosend(). One can
771 * optionaly switch back to this code (by changing back the
772 * defininitions but this is not advisable.
776 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
786 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
790 struct sctp_inpcb *inp;
791 struct sctp_tcb *stcb;
794 struct sockaddr_in6 *sin6;
795 union sctp_sockstore store;
799 inp6 = (struct in6pcb *)so->so_pcb;
801 inp = (struct sctp_inpcb *)so->so_pcb;
803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
804 return (ECONNRESET); /* I made the same as TCP since we are
808 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
811 switch (addr->sa_family) {
814 if (addr->sa_len != sizeof(struct sockaddr_in)) {
815 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
822 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
823 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
829 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
833 vrf_id = inp->def_vrf_id;
834 SCTP_ASOC_CREATE_LOCK(inp);
836 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
837 SCTP_PCB_FLAGS_UNBOUND) {
838 /* Bind a ephemeral port */
839 SCTP_INP_RUNLOCK(inp);
840 error = sctp6_bind(so, NULL, p);
842 SCTP_ASOC_CREATE_UNLOCK(inp);
848 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
849 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
850 /* We are already connected AND the TCP model */
851 SCTP_INP_RUNLOCK(inp);
852 SCTP_ASOC_CREATE_UNLOCK(inp);
853 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
857 sin6 = (struct sockaddr_in6 *)addr;
858 if (SCTP_IPV6_V6ONLY(inp6)) {
860 * if IPV6_V6ONLY flag, ignore connections destined to a v4
861 * addr or v4-mapped addr
863 if (addr->sa_family == AF_INET) {
864 SCTP_INP_RUNLOCK(inp);
865 SCTP_ASOC_CREATE_UNLOCK(inp);
866 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
869 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
870 SCTP_INP_RUNLOCK(inp);
871 SCTP_ASOC_CREATE_UNLOCK(inp);
872 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
876 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
877 /* convert v4-mapped into v4 addr */
878 in6_sin6_2_sin(&store.sin, sin6);
882 /* Now do we connect? */
883 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
884 stcb = LIST_FIRST(&inp->sctp_asoc_list);
888 SCTP_INP_RUNLOCK(inp);
890 SCTP_INP_RUNLOCK(inp);
892 SCTP_INP_INCR_REF(inp);
893 SCTP_INP_WUNLOCK(inp);
894 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
897 SCTP_INP_DECR_REF(inp);
898 SCTP_INP_WUNLOCK(inp);
903 /* Already have or am bring up an association */
904 SCTP_ASOC_CREATE_UNLOCK(inp);
905 SCTP_TCB_UNLOCK(stcb);
906 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
909 /* We are GOOD to go */
910 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
911 inp->sctp_ep.pre_open_stream_count,
912 inp->sctp_ep.port, p);
913 SCTP_ASOC_CREATE_UNLOCK(inp);
918 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
919 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
920 /* Set the connected flag so we can queue data */
923 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
924 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
926 /* initialize authentication parameters for the assoc */
927 sctp_initialize_auth_params(inp, stcb);
929 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
930 SCTP_TCB_UNLOCK(stcb);
935 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
937 struct sockaddr_in6 *sin6;
938 struct sctp_inpcb *inp;
940 struct sctp_ifa *sctp_ifa;
945 * Do the malloc first in case it blocks.
947 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
950 sin6->sin6_family = AF_INET6;
951 sin6->sin6_len = sizeof(*sin6);
953 inp = (struct sctp_inpcb *)so->so_pcb;
955 SCTP_FREE_SONAME(sin6);
956 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
960 sin6->sin6_port = inp->sctp_lport;
961 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
962 /* For the bound all case you get back 0 */
963 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
964 struct sctp_tcb *stcb;
965 struct sockaddr_in6 *sin_a6;
966 struct sctp_nets *net;
969 stcb = LIST_FIRST(&inp->sctp_asoc_list);
971 SCTP_INP_RUNLOCK(inp);
972 SCTP_FREE_SONAME(sin6);
973 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
978 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
979 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
981 /* this will make coverity happy */
984 if (sin_a6->sin6_family == AF_INET6) {
989 if ((!fnd) || (sin_a6 == NULL)) {
991 SCTP_INP_RUNLOCK(inp);
992 SCTP_FREE_SONAME(sin6);
993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
996 vrf_id = inp->def_vrf_id;
997 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
999 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1002 /* For the bound all case you get back 0 */
1003 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1006 /* Take the first IPv6 address in the list */
1007 struct sctp_laddr *laddr;
1010 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1011 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1012 struct sockaddr_in6 *sin_a;
1014 sin_a = &laddr->ifa->address.sin6;
1015 sin6->sin6_addr = sin_a->sin6_addr;
1021 SCTP_FREE_SONAME(sin6);
1022 SCTP_INP_RUNLOCK(inp);
1023 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1027 SCTP_INP_RUNLOCK(inp);
1028 /* Scoping things for v6 */
1029 if ((error = sa6_recoverscope(sin6)) != 0) {
1030 SCTP_FREE_SONAME(sin6);
1033 (*addr) = (struct sockaddr *)sin6;
1038 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1040 struct sockaddr_in6 *sin6;
1042 struct sockaddr_in6 *sin_a6;
1043 struct sctp_inpcb *inp;
1044 struct sctp_tcb *stcb;
1045 struct sctp_nets *net;
1048 /* Do the malloc first in case it blocks. */
1049 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1052 sin6->sin6_family = AF_INET6;
1053 sin6->sin6_len = sizeof(*sin6);
1055 inp = (struct sctp_inpcb *)so->so_pcb;
1056 if ((inp == NULL) ||
1057 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1058 /* UDP type and listeners will drop out here */
1059 SCTP_FREE_SONAME(sin6);
1060 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1063 SCTP_INP_RLOCK(inp);
1064 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1066 SCTP_TCB_LOCK(stcb);
1068 SCTP_INP_RUNLOCK(inp);
1070 SCTP_FREE_SONAME(sin6);
1071 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1072 return (ECONNRESET);
1075 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1076 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1077 if (sin_a6->sin6_family == AF_INET6) {
1079 sin6->sin6_port = stcb->rport;
1080 sin6->sin6_addr = sin_a6->sin6_addr;
1084 SCTP_TCB_UNLOCK(stcb);
1086 /* No IPv4 address */
1087 SCTP_FREE_SONAME(sin6);
1088 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1091 if ((error = sa6_recoverscope(sin6)) != 0) {
1092 SCTP_FREE_SONAME(sin6);
1093 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1096 *addr = (struct sockaddr *)sin6;
1101 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1103 struct in6pcb *inp6 = sotoin6pcb(so);
1107 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1110 /* allow v6 addresses precedence */
1111 error = sctp6_getaddr(so, nam);
1114 struct sockaddr_in6 *sin6;
1116 /* try v4 next if v6 failed */
1117 error = sctp_ingetaddr(so, nam);
1121 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1123 SCTP_FREE_SONAME(*nam);
1126 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1127 SCTP_FREE_SONAME(*nam);
1128 *nam = (struct sockaddr *)sin6;
1136 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1138 struct in6pcb *inp6 = sotoin6pcb(so);
1142 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1145 /* allow v6 addresses precedence */
1146 error = sctp6_peeraddr(so, nam);
1149 struct sockaddr_in6 *sin6;
1151 /* try v4 next if v6 failed */
1152 error = sctp_peeraddr(so, nam);
1156 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1158 SCTP_FREE_SONAME(*nam);
1161 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1162 SCTP_FREE_SONAME(*nam);
1163 *nam = (struct sockaddr *)sin6;
1169 struct pr_usrreqs sctp6_usrreqs = {
1170 .pru_abort = sctp6_abort,
1171 .pru_accept = sctp_accept,
1172 .pru_attach = sctp6_attach,
1173 .pru_bind = sctp6_bind,
1174 .pru_connect = sctp6_connect,
1175 .pru_control = in6_control,
1176 .pru_close = sctp6_close,
1177 .pru_detach = sctp6_close,
1178 .pru_sopoll = sopoll_generic,
1179 .pru_flush = sctp_flush,
1180 .pru_disconnect = sctp6_disconnect,
1181 .pru_listen = sctp_listen,
1182 .pru_peeraddr = sctp6_getpeeraddr,
1183 .pru_send = sctp6_send,
1184 .pru_shutdown = sctp_shutdown,
1185 .pru_sockaddr = sctp6_in6getaddr,
1186 .pru_sosend = sctp_sosend,
1187 .pru_soreceive = sctp_soreceive