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(struct mbuf **i_pak, int *offp, int proto)
75 struct sockaddr_in6 src, dst;
78 struct sctp_chunkhdr *ch;
79 struct sctp_inpcb *inp = NULL;
80 struct sctp_tcb *stcb = NULL;
81 struct sctp_nets *net = NULL;
87 #if !defined(SCTP_WITH_NO_CSUM)
88 uint32_t check, calc_check;
94 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
95 SCTP_RELEASE_PKT(*i_pak);
96 return (IPPROTO_DONE);
98 m = SCTP_HEADER_TO_CHAIN(*i_pak);
99 #ifdef SCTP_MBUF_LOGGING
100 /* Log in any input mbufs */
101 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
104 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
105 if (SCTP_BUF_IS_EXTENDED(mat)) {
106 sctp_log_mb(mat, SCTP_MBUF_INPUT);
111 #ifdef SCTP_PACKET_LOGGING
112 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
116 if (m->m_flags & M_FLOWID) {
117 mflowid = m->m_pkthdr.flowid;
123 SCTP_STAT_INCR(sctps_recvpackets);
124 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
125 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
126 ip6 = mtod(m, struct ip6_hdr *);
127 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
128 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
129 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
131 SCTP_STAT_INCR(sctps_hdrops);
132 return (IPPROTO_DONE);
134 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
135 offset -= sizeof(struct sctp_chunkhdr);
136 memset(&src, 0, sizeof(struct sockaddr_in6));
137 src.sin6_family = AF_INET6;
138 src.sin6_len = sizeof(struct sockaddr_in6);
139 src.sin6_port = sh->src_port;
140 src.sin6_addr = ip6->ip6_src;
141 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
144 memset(&dst, 0, sizeof(struct sockaddr_in6));
145 dst.sin6_family = AF_INET6;
146 dst.sin6_len = sizeof(struct sockaddr_in6);
147 dst.sin6_port = sh->dest_port;
148 dst.sin6_addr = ip6->ip6_dst;
149 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
152 if (faithprefix_p != NULL && (*faithprefix_p) (&dst.sin6_addr)) {
153 /* XXX send icmp6 host/port unreach? */
156 length = ntohs(ip6->ip6_plen) + iphlen;
157 /* Validate mbuf chain length with IP payload length. */
158 if (SCTP_HEADER_LEN(*i_pak) != length) {
159 SCTPDBG(SCTP_DEBUG_INPUT1,
160 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(*i_pak));
161 SCTP_STAT_INCR(sctps_hdrops);
164 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
167 SCTPDBG(SCTP_DEBUG_INPUT1,
168 "sctp6_input() length:%d iphlen:%d\n", length, iphlen);
169 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
170 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
172 if_name(m->m_pkthdr.rcvif),
173 m->m_pkthdr.csum_flags);
174 #if defined(SCTP_WITH_NO_CSUM)
175 SCTP_STAT_INCR(sctps_recvnocrc);
177 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
178 SCTP_STAT_INCR(sctps_recvhwcrc);
181 check = sh->checksum;
183 calc_check = sctp_calculate_cksum(m, iphlen);
184 sh->checksum = check;
185 SCTP_STAT_INCR(sctps_recvswcrc);
186 if (calc_check != check) {
187 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
188 calc_check, check, m, length, iphlen);
189 stcb = sctp_findassociation_addr(m, offset,
190 (struct sockaddr *)&src,
191 (struct sockaddr *)&dst,
192 sh, ch, &inp, &net, vrf_id);
193 if ((net) && (port)) {
194 if (net->port == 0) {
195 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
199 if ((net != NULL) && (use_mflowid != 0)) {
200 net->flowid = mflowid;
205 if ((inp) && (stcb)) {
206 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
207 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
208 } else if ((inp != NULL) && (stcb == NULL)) {
211 SCTP_STAT_INCR(sctps_badsum);
212 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
217 /* destination port of 0 is illegal, based on RFC2960. */
218 if (sh->dest_port == 0) {
219 SCTP_STAT_INCR(sctps_hdrops);
222 stcb = sctp_findassociation_addr(m, offset,
223 (struct sockaddr *)&src,
224 (struct sockaddr *)&dst,
225 sh, ch, &inp, &net, vrf_id);
226 if ((net) && (port)) {
227 if (net->port == 0) {
228 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
232 if ((net != NULL) && (use_mflowid != 0)) {
233 net->flowid = mflowid;
239 SCTP_STAT_INCR(sctps_noport);
240 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0)
242 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
243 sctp_send_shutdown_complete2((struct sockaddr *)&src,
244 (struct sockaddr *)&dst,
246 use_mflowid, mflowid,
250 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
253 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
254 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
255 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
256 (ch->chunk_type != SCTP_INIT))) {
257 sctp_send_abort(m, iphlen,
258 (struct sockaddr *)&src,
259 (struct sockaddr *)&dst,
261 use_mflowid, mflowid,
266 } else if (stcb == NULL) {
271 * I very much doubt any of the IPSEC stuff will work but I have no
272 * idea, so I will leave it in place.
274 if (inp && ipsec6_in_reject(m, &inp->ip_inp.inp)) {
275 MODULE_GLOBAL(ipsec6stat).in_polvio++;
276 SCTP_STAT_INCR(sctps_hdrops);
281 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
282 /* sa_ignore NO_NULL_CHK */
283 sctp_common_input_processing(&m, iphlen, offset, length,
284 (struct sockaddr *)&src,
285 (struct sockaddr *)&dst,
286 sh, ch, inp, stcb, net, ecn_bits,
287 use_mflowid, mflowid,
292 if ((inp) && (refcount_up)) {
293 /* reduce ref-count */
295 SCTP_INP_DECR_REF(inp);
296 SCTP_INP_WUNLOCK(inp);
298 return (IPPROTO_DONE);
301 SCTP_TCB_UNLOCK(stcb);
303 if ((inp) && (refcount_up)) {
304 /* reduce ref-count */
306 SCTP_INP_DECR_REF(inp);
307 SCTP_INP_WUNLOCK(inp);
312 return (IPPROTO_DONE);
317 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
318 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
322 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
323 (icmp6 == NULL) || (sh == NULL)) {
326 /* First do we even look at it? */
327 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
330 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
331 /* not PACKET TO BIG */
335 * ok we need to look closely. We could even get smarter and look at
336 * anyone that we sent to in case we get a different ICMP that tells
337 * us there is no way to reach a host, but for this impl, all we
338 * care about is MTU discovery.
340 nxtsz = ntohl(icmp6->icmp6_mtu);
341 /* Stop any PMTU timer */
342 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
344 /* Adjust destination size limit */
345 if (net->mtu > nxtsz) {
348 net->mtu -= sizeof(struct udphdr);
351 /* now what about the ep? */
352 if (stcb->asoc.smallest_mtu > nxtsz) {
353 struct sctp_tmit_chunk *chk;
355 /* Adjust that too */
356 stcb->asoc.smallest_mtu = nxtsz;
357 /* now off to subtract IP_DF flag if needed */
359 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
360 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
361 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
364 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
365 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
367 * For this guy we also mark for immediate
368 * resend since we sent to big of chunk
370 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
371 if (chk->sent != SCTP_DATAGRAM_RESEND)
372 stcb->asoc.sent_queue_retran_cnt++;
373 chk->sent = SCTP_DATAGRAM_RESEND;
374 chk->rec.data.doing_fast_retransmit = 0;
376 chk->sent = SCTP_DATAGRAM_RESEND;
377 /* Clear any time so NO RTT is being done */
378 chk->sent_rcv_time.tv_sec = 0;
379 chk->sent_rcv_time.tv_usec = 0;
380 stcb->asoc.total_flight -= chk->send_size;
381 net->flight_size -= chk->send_size;
385 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
388 SCTP_TCB_UNLOCK(stcb);
394 sctp6_notify(struct sctp_inpcb *inp,
395 struct icmp6_hdr *icmph,
398 struct sctp_tcb *stcb,
399 struct sctp_nets *net)
401 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
407 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
408 (sh == NULL) || (to == NULL)) {
410 SCTP_TCB_UNLOCK(stcb);
413 /* First job is to verify the vtag matches what I would send */
414 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
415 SCTP_TCB_UNLOCK(stcb);
418 if (icmph->icmp6_type != ICMP_UNREACH) {
419 /* We only care about unreachable */
420 SCTP_TCB_UNLOCK(stcb);
423 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
424 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
425 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
426 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
427 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
428 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
429 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
430 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
433 * Hmm reachablity problems we must examine closely. If its
434 * not reachable, we may have lost a network. Or if there is
435 * NO protocol at the other end named SCTP. well we consider
438 if (net->dest_state & SCTP_ADDR_REACHABLE) {
439 /* Ok that destination is NOT reachable */
440 net->dest_state &= ~SCTP_ADDR_REACHABLE;
441 net->dest_state &= ~SCTP_ADDR_PF;
442 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
443 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
445 SCTP_TCB_UNLOCK(stcb);
446 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
447 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
449 * Here the peer is either playing tricks on us, including
450 * an address that belongs to someone who does not support
451 * SCTP OR was a userland implementation that shutdown and
452 * now is dead. In either case treat it like a OOTB abort
455 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
456 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
457 so = SCTP_INP_SO(inp);
458 atomic_add_int(&stcb->asoc.refcnt, 1);
459 SCTP_TCB_UNLOCK(stcb);
460 SCTP_SOCKET_LOCK(so, 1);
462 atomic_subtract_int(&stcb->asoc.refcnt, 1);
464 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
465 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
466 SCTP_SOCKET_UNLOCK(so, 1);
467 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
469 /* no need to unlock here, since the TCB is gone */
471 SCTP_TCB_UNLOCK(stcb);
478 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
481 struct ip6ctlparam *ip6cp = NULL;
484 vrf_id = SCTP_DEFAULT_VRFID;
486 if (pktdst->sa_family != AF_INET6 ||
487 pktdst->sa_len != sizeof(struct sockaddr_in6))
490 if ((unsigned)cmd >= PRC_NCMDS)
492 if (PRC_IS_REDIRECT(cmd)) {
494 } else if (inet6ctlerrmap[cmd] == 0) {
497 /* if the parameter is from icmp6, decode it. */
499 ip6cp = (struct ip6ctlparam *)d;
501 ip6cp = (struct ip6ctlparam *)NULL;
506 * XXX: We assume that when IPV6 is non NULL, M and OFF are
509 /* check if we can safely examine src and dst ports */
510 struct sctp_inpcb *inp = NULL;
511 struct sctp_tcb *stcb = NULL;
512 struct sctp_nets *net = NULL;
513 struct sockaddr_in6 final;
515 if (ip6cp->ip6c_m == NULL)
518 bzero(&sh, sizeof(sh));
519 bzero(&final, sizeof(final));
522 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
524 ip6cp->ip6c_src->sin6_port = sh.src_port;
525 final.sin6_len = sizeof(final);
526 final.sin6_family = AF_INET6;
527 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
528 final.sin6_port = sh.dest_port;
529 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&final,
530 (struct sockaddr *)ip6cp->ip6c_src,
531 &inp, &net, 1, vrf_id);
532 /* inp's ref-count increased && stcb locked */
533 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
534 if (cmd == PRC_MSGSIZE) {
535 sctp6_notify_mbuf(inp,
540 /* inp's ref-count reduced && stcb unlocked */
542 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
543 (struct sockaddr *)&final,
545 /* inp's ref-count reduced && stcb unlocked */
548 if (PRC_IS_REDIRECT(cmd) && inp) {
549 in6_rtchange((struct in6pcb *)inp,
550 inet6ctlerrmap[cmd]);
553 /* reduce inp's ref-count */
555 SCTP_INP_DECR_REF(inp);
556 SCTP_INP_WUNLOCK(inp);
559 SCTP_TCB_UNLOCK(stcb);
565 * this routine can probably be collasped into the one in sctp_userreq.c
566 * since they do the same thing and now we lookup with a sockaddr
569 sctp6_getcred(SYSCTL_HANDLER_ARGS)
572 struct sockaddr_in6 addrs[2];
573 struct sctp_inpcb *inp;
574 struct sctp_nets *net;
575 struct sctp_tcb *stcb;
579 vrf_id = SCTP_DEFAULT_VRFID;
581 error = priv_check(req->td, PRIV_NETINET_GETCRED);
585 if (req->newlen != sizeof(addrs)) {
586 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
589 if (req->oldlen != sizeof(struct ucred)) {
590 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
593 error = SYSCTL_IN(req, addrs, sizeof(addrs));
597 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
599 &inp, &net, 1, vrf_id);
600 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
601 if ((inp != NULL) && (stcb == NULL)) {
602 /* reduce ref-count */
604 SCTP_INP_DECR_REF(inp);
607 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
611 SCTP_TCB_UNLOCK(stcb);
613 * We use the write lock here, only since in the error leg we need
614 * it. If we used RLOCK, then we would have to
615 * wlock/decr/unlock/rlock. Which in theory could create a hole.
616 * Better to use higher wlock.
620 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
622 SCTP_INP_WUNLOCK(inp);
625 cru2x(inp->sctp_socket->so_cred, &xuc);
626 SCTP_INP_WUNLOCK(inp);
627 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
632 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
634 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
637 /* This is the same as the sctp_abort() could be made common */
639 sctp6_abort(struct socket *so)
641 struct sctp_inpcb *inp;
644 inp = (struct sctp_inpcb *)so->so_pcb;
646 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
650 flags = inp->sctp_flags;
651 #ifdef SCTP_LOG_CLOSING
652 sctp_log_closing(inp, NULL, 17);
654 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
655 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
656 #ifdef SCTP_LOG_CLOSING
657 sctp_log_closing(inp, NULL, 16);
659 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
660 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
662 SCTP_SB_CLEAR(so->so_snd);
664 * same for the rcv ones, they are only here for the
667 SCTP_SB_CLEAR(so->so_rcv);
668 /* Now null out the reference, we are completely detached. */
672 flags = inp->sctp_flags;
673 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
674 goto sctp_must_try_again;
681 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
685 struct sctp_inpcb *inp;
686 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
688 inp = (struct sctp_inpcb *)so->so_pcb;
690 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
693 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
694 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
698 error = sctp_inpcb_alloc(so, vrf_id);
701 inp = (struct sctp_inpcb *)so->so_pcb;
703 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
704 inp6 = (struct in6pcb *)inp;
706 inp6->inp_vflag |= INP_IPV6;
707 inp6->in6p_hops = -1; /* use kernel default */
708 inp6->in6p_cksum = -1; /* just to be sure */
711 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
712 * socket as well, because the socket may be bound to an IPv6
713 * wildcard address, which may match an IPv4-mapped IPv6 address.
715 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
718 * Hmm what about the IPSEC stuff that is missing here but in
721 SCTP_INP_WUNLOCK(inp);
726 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
728 struct sctp_inpcb *inp;
732 inp = (struct sctp_inpcb *)so->so_pcb;
734 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
738 switch (addr->sa_family) {
741 if (addr->sa_len != sizeof(struct sockaddr_in)) {
742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
749 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
750 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
756 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
760 inp6 = (struct in6pcb *)inp;
761 inp6->inp_vflag &= ~INP_IPV4;
762 inp6->inp_vflag |= INP_IPV6;
763 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
764 switch (addr->sa_family) {
767 /* binding v4 addr to v6 socket, so reset flags */
768 inp6->inp_vflag |= INP_IPV4;
769 inp6->inp_vflag &= ~INP_IPV6;
775 struct sockaddr_in6 *sin6_p;
777 sin6_p = (struct sockaddr_in6 *)addr;
779 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
780 inp6->inp_vflag |= INP_IPV4;
783 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
784 struct sockaddr_in sin;
786 in6_sin6_2_sin(&sin, sin6_p);
787 inp6->inp_vflag |= INP_IPV4;
788 inp6->inp_vflag &= ~INP_IPV6;
789 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
799 } else if (addr != NULL) {
800 struct sockaddr_in6 *sin6_p;
802 /* IPV6_V6ONLY socket */
804 if (addr->sa_family == AF_INET) {
805 /* can't bind v4 addr to v6 only socket! */
806 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
810 sin6_p = (struct sockaddr_in6 *)addr;
812 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
813 /* can't bind v4-mapped addrs either! */
814 /* NOTE: we don't support SIIT */
815 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
819 error = sctp_inpcb_bind(so, addr, NULL, p);
825 sctp6_close(struct socket *so)
830 /* This could be made common with sctp_detach() since they are identical */
834 sctp6_disconnect(struct socket *so)
836 return (sctp_disconnect(so));
841 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
842 struct mbuf *control, struct thread *p);
846 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
847 struct mbuf *control, struct thread *p)
849 struct sctp_inpcb *inp;
853 struct sockaddr_in6 *sin6;
856 /* No SPL needed since sctp_output does this */
858 inp = (struct sctp_inpcb *)so->so_pcb;
861 SCTP_RELEASE_PKT(control);
865 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
868 inp6 = (struct in6pcb *)inp;
870 * For the TCP model we may get a NULL addr, if we are a connected
873 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
880 SCTP_RELEASE_PKT(control);
883 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
884 return (EDESTADDRREQ);
887 sin6 = (struct sockaddr_in6 *)addr;
888 if (SCTP_IPV6_V6ONLY(inp6)) {
890 * if IPV6_V6ONLY flag, we discard datagrams destined to a
891 * v4 addr or v4-mapped addr
893 if (addr->sa_family == AF_INET) {
894 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
897 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
898 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
902 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
903 if (!MODULE_GLOBAL(ip6_v6only)) {
904 struct sockaddr_in sin;
906 /* convert v4-mapped into v4 addr and send */
907 in6_sin6_2_sin(&sin, sin6);
908 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
911 /* mapped addresses aren't enabled */
912 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
918 /* now what about control */
921 SCTP_PRINTF("huh? control set?\n");
922 SCTP_RELEASE_PKT(inp->control);
925 inp->control = control;
929 SCTP_BUF_NEXT(inp->pkt_last) = m;
932 inp->pkt_last = inp->pkt = m;
935 /* FreeBSD and MacOSX uses a flag passed */
936 ((flags & PRUS_MORETOCOME) == 0)
939 * note with the current version this code will only be used
940 * by OpenBSD, NetBSD and FreeBSD have methods for
941 * re-defining sosend() to use sctp_sosend(). One can
942 * optionaly switch back to this code (by changing back the
943 * defininitions but this is not advisable.
947 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
957 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
961 struct sctp_inpcb *inp;
963 struct sctp_tcb *stcb;
966 struct sockaddr_in6 *sin6;
967 struct sockaddr_storage ss;
971 inp6 = (struct in6pcb *)so->so_pcb;
972 inp = (struct sctp_inpcb *)so->so_pcb;
974 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
975 return (ECONNRESET); /* I made the same as TCP since we are
979 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
982 switch (addr->sa_family) {
985 if (addr->sa_len != sizeof(struct sockaddr_in)) {
986 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
993 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
994 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1000 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1004 vrf_id = inp->def_vrf_id;
1005 SCTP_ASOC_CREATE_LOCK(inp);
1006 SCTP_INP_RLOCK(inp);
1007 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1008 SCTP_PCB_FLAGS_UNBOUND) {
1009 /* Bind a ephemeral port */
1010 SCTP_INP_RUNLOCK(inp);
1011 error = sctp6_bind(so, NULL, p);
1013 SCTP_ASOC_CREATE_UNLOCK(inp);
1017 SCTP_INP_RLOCK(inp);
1019 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1020 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1021 /* We are already connected AND the TCP model */
1022 SCTP_INP_RUNLOCK(inp);
1023 SCTP_ASOC_CREATE_UNLOCK(inp);
1024 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1025 return (EADDRINUSE);
1028 sin6 = (struct sockaddr_in6 *)addr;
1029 if (SCTP_IPV6_V6ONLY(inp6)) {
1031 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1032 * addr or v4-mapped addr
1034 if (addr->sa_family == AF_INET) {
1035 SCTP_INP_RUNLOCK(inp);
1036 SCTP_ASOC_CREATE_UNLOCK(inp);
1037 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1040 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1041 SCTP_INP_RUNLOCK(inp);
1042 SCTP_ASOC_CREATE_UNLOCK(inp);
1043 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1047 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1048 if (!MODULE_GLOBAL(ip6_v6only)) {
1049 /* convert v4-mapped into v4 addr */
1050 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1051 addr = (struct sockaddr *)&ss;
1053 /* mapped addresses aren't enabled */
1054 SCTP_INP_RUNLOCK(inp);
1055 SCTP_ASOC_CREATE_UNLOCK(inp);
1056 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1061 /* Now do we connect? */
1062 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1063 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1065 SCTP_TCB_UNLOCK(stcb);
1067 SCTP_INP_RUNLOCK(inp);
1069 SCTP_INP_RUNLOCK(inp);
1070 SCTP_INP_WLOCK(inp);
1071 SCTP_INP_INCR_REF(inp);
1072 SCTP_INP_WUNLOCK(inp);
1073 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1075 SCTP_INP_WLOCK(inp);
1076 SCTP_INP_DECR_REF(inp);
1077 SCTP_INP_WUNLOCK(inp);
1082 /* Already have or am bring up an association */
1083 SCTP_ASOC_CREATE_UNLOCK(inp);
1084 SCTP_TCB_UNLOCK(stcb);
1085 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1088 /* We are GOOD to go */
1089 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1090 SCTP_ASOC_CREATE_UNLOCK(inp);
1092 /* Gak! no memory */
1095 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1096 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1097 /* Set the connected flag so we can queue data */
1100 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1101 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1103 /* initialize authentication parameters for the assoc */
1104 sctp_initialize_auth_params(inp, stcb);
1106 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1107 SCTP_TCB_UNLOCK(stcb);
1112 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1114 struct sockaddr_in6 *sin6;
1115 struct sctp_inpcb *inp;
1117 struct sctp_ifa *sctp_ifa;
1122 * Do the malloc first in case it blocks.
1124 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1127 sin6->sin6_family = AF_INET6;
1128 sin6->sin6_len = sizeof(*sin6);
1130 inp = (struct sctp_inpcb *)so->so_pcb;
1132 SCTP_FREE_SONAME(sin6);
1133 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1134 return (ECONNRESET);
1136 SCTP_INP_RLOCK(inp);
1137 sin6->sin6_port = inp->sctp_lport;
1138 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1139 /* For the bound all case you get back 0 */
1140 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1141 struct sctp_tcb *stcb;
1142 struct sockaddr_in6 *sin_a6;
1143 struct sctp_nets *net;
1146 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1152 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1153 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1155 /* this will make coverity happy */
1158 if (sin_a6->sin6_family == AF_INET6) {
1163 if ((!fnd) || (sin_a6 == NULL)) {
1167 vrf_id = inp->def_vrf_id;
1168 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1170 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1173 /* For the bound all case you get back 0 */
1175 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1178 /* Take the first IPv6 address in the list */
1179 struct sctp_laddr *laddr;
1182 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1183 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1184 struct sockaddr_in6 *sin_a;
1186 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1187 sin6->sin6_addr = sin_a->sin6_addr;
1193 SCTP_FREE_SONAME(sin6);
1194 SCTP_INP_RUNLOCK(inp);
1195 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1199 SCTP_INP_RUNLOCK(inp);
1200 /* Scoping things for v6 */
1201 if ((error = sa6_recoverscope(sin6)) != 0) {
1202 SCTP_FREE_SONAME(sin6);
1205 (*addr) = (struct sockaddr *)sin6;
1210 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1212 struct sockaddr_in6 *sin6;
1214 struct sockaddr_in6 *sin_a6;
1215 struct sctp_inpcb *inp;
1216 struct sctp_tcb *stcb;
1217 struct sctp_nets *net;
1220 /* Do the malloc first in case it blocks. */
1221 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1224 sin6->sin6_family = AF_INET6;
1225 sin6->sin6_len = sizeof(*sin6);
1227 inp = (struct sctp_inpcb *)so->so_pcb;
1228 if ((inp == NULL) ||
1229 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1230 /* UDP type and listeners will drop out here */
1231 SCTP_FREE_SONAME(sin6);
1232 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1235 SCTP_INP_RLOCK(inp);
1236 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1238 SCTP_TCB_LOCK(stcb);
1240 SCTP_INP_RUNLOCK(inp);
1242 SCTP_FREE_SONAME(sin6);
1243 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1244 return (ECONNRESET);
1247 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1248 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1249 if (sin_a6->sin6_family == AF_INET6) {
1251 sin6->sin6_port = stcb->rport;
1252 sin6->sin6_addr = sin_a6->sin6_addr;
1256 SCTP_TCB_UNLOCK(stcb);
1258 /* No IPv4 address */
1259 SCTP_FREE_SONAME(sin6);
1260 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1263 if ((error = sa6_recoverscope(sin6)) != 0)
1265 *addr = (struct sockaddr *)sin6;
1270 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1273 struct sockaddr *addr;
1276 struct in6pcb *inp6 = sotoin6pcb(so);
1280 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1283 /* allow v6 addresses precedence */
1284 error = sctp6_getaddr(so, nam);
1287 /* try v4 next if v6 failed */
1288 error = sctp_ingetaddr(so, nam);
1293 /* if I'm V6ONLY, convert it to v4-mapped */
1294 if (SCTP_IPV6_V6ONLY(inp6)) {
1295 struct sockaddr_in6 sin6;
1297 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1298 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1307 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1310 struct sockaddr *addr;
1313 struct in6pcb *inp6 = sotoin6pcb(so);
1317 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1320 /* allow v6 addresses precedence */
1321 error = sctp6_peeraddr(so, nam);
1324 /* try v4 next if v6 failed */
1325 error = sctp_peeraddr(so, nam);
1330 /* if I'm V6ONLY, convert it to v4-mapped */
1331 if (SCTP_IPV6_V6ONLY(inp6)) {
1332 struct sockaddr_in6 sin6;
1334 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1335 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1342 struct pr_usrreqs sctp6_usrreqs = {
1343 .pru_abort = sctp6_abort,
1344 .pru_accept = sctp_accept,
1345 .pru_attach = sctp6_attach,
1346 .pru_bind = sctp6_bind,
1347 .pru_connect = sctp6_connect,
1348 .pru_control = in6_control,
1349 .pru_close = sctp6_close,
1350 .pru_detach = sctp6_close,
1351 .pru_sopoll = sopoll_generic,
1352 .pru_flush = sctp_flush,
1353 .pru_disconnect = sctp6_disconnect,
1354 .pru_listen = sctp_listen,
1355 .pru_peeraddr = sctp6_getpeeraddr,
1356 .pru_send = sctp6_send,
1357 .pru_shutdown = sctp_shutdown,
1358 .pru_sockaddr = sctp6_in6getaddr,
1359 .pru_sosend = sctp_sosend,
1360 .pru_soreceive = sctp_soreceive