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)
77 struct sctp_chunkhdr *ch;
78 struct sctp_inpcb *inp = NULL;
79 struct sctp_tcb *stcb = NULL;
80 struct sctp_nets *net = NULL;
86 #if !defined(SCTP_WITH_NO_CSUM)
87 uint32_t check, calc_check;
93 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
94 SCTP_RELEASE_PKT(*i_pak);
95 return (IPPROTO_DONE);
97 m = SCTP_HEADER_TO_CHAIN(*i_pak);
98 #ifdef SCTP_MBUF_LOGGING
99 /* Log in any input mbufs */
100 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
103 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
104 if (SCTP_BUF_IS_EXTENDED(mat)) {
105 sctp_log_mb(mat, SCTP_MBUF_INPUT);
110 #ifdef SCTP_PACKET_LOGGING
111 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
115 if (m->m_flags & M_FLOWID) {
116 mflowid = m->m_pkthdr.flowid;
122 SCTP_STAT_INCR(sctps_recvpackets);
123 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
124 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
125 ip6 = mtod(m, struct ip6_hdr *);
126 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
127 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
128 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
130 SCTP_STAT_INCR(sctps_hdrops);
131 return (IPPROTO_DONE);
133 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
134 offset -= sizeof(struct sctp_chunkhdr);
135 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
136 /* XXX send icmp6 host/port unreach? */
139 length = ntohs(ip6->ip6_plen) + iphlen;
140 /* Validate mbuf chain length with IP payload length. */
141 if (SCTP_HEADER_LEN(*i_pak) != length) {
142 SCTPDBG(SCTP_DEBUG_INPUT1,
143 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(*i_pak));
144 SCTP_STAT_INCR(sctps_hdrops);
147 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
150 SCTPDBG(SCTP_DEBUG_INPUT1,
151 "sctp6_input() length:%d iphlen:%d\n", length, iphlen);
152 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
153 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
155 if_name(m->m_pkthdr.rcvif),
156 m->m_pkthdr.csum_flags);
157 #if defined(SCTP_WITH_NO_CSUM)
158 SCTP_STAT_INCR(sctps_recvnocrc);
160 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
161 SCTP_STAT_INCR(sctps_recvhwcrc);
164 check = sh->checksum;
166 calc_check = sctp_calculate_cksum(m, iphlen);
167 sh->checksum = check;
168 SCTP_STAT_INCR(sctps_recvswcrc);
169 if (calc_check != check) {
170 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
171 calc_check, check, m, length, iphlen);
172 stcb = sctp_findassociation_addr(m, offset,
173 sh, ch, &inp, &net, vrf_id);
174 if ((net) && (port)) {
175 if (net->port == 0) {
176 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
180 if ((net != NULL) && (use_mflowid != 0)) {
181 net->flowid = mflowid;
186 if ((inp) && (stcb)) {
187 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
188 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
189 } else if ((inp != NULL) && (stcb == NULL)) {
192 SCTP_STAT_INCR(sctps_badsum);
193 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
198 /* destination port of 0 is illegal, based on RFC2960. */
199 if (sh->dest_port == 0) {
200 SCTP_STAT_INCR(sctps_hdrops);
203 stcb = sctp_findassociation_addr(m, offset,
204 sh, ch, &inp, &net, vrf_id);
205 if ((net) && (port)) {
206 if (net->port == 0) {
207 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
211 if ((net != NULL) && (use_mflowid != 0)) {
212 net->flowid = mflowid;
218 SCTP_STAT_INCR(sctps_noport);
219 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0)
221 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
222 sctp_send_shutdown_complete2(m, sh,
223 use_mflowid, mflowid,
227 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
230 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
231 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
232 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
233 (ch->chunk_type != SCTP_INIT))) {
234 sctp_send_abort(m, iphlen, sh, 0, NULL,
235 use_mflowid, mflowid,
240 } else if (stcb == NULL) {
245 * I very much doubt any of the IPSEC stuff will work but I have no
246 * idea, so I will leave it in place.
248 if (inp && ipsec6_in_reject(m, &inp->ip_inp.inp)) {
249 MODULE_GLOBAL(ipsec6stat).in_polvio++;
250 SCTP_STAT_INCR(sctps_hdrops);
255 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
256 /* sa_ignore NO_NULL_CHK */
257 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
258 inp, stcb, net, ecn_bits,
259 use_mflowid, mflowid,
264 if ((inp) && (refcount_up)) {
265 /* reduce ref-count */
267 SCTP_INP_DECR_REF(inp);
268 SCTP_INP_WUNLOCK(inp);
270 return (IPPROTO_DONE);
273 SCTP_TCB_UNLOCK(stcb);
275 if ((inp) && (refcount_up)) {
276 /* reduce ref-count */
278 SCTP_INP_DECR_REF(inp);
279 SCTP_INP_WUNLOCK(inp);
284 return (IPPROTO_DONE);
289 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
290 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
294 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
295 (icmp6 == NULL) || (sh == NULL)) {
298 /* First do we even look at it? */
299 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
302 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
303 /* not PACKET TO BIG */
307 * ok we need to look closely. We could even get smarter and look at
308 * anyone that we sent to in case we get a different ICMP that tells
309 * us there is no way to reach a host, but for this impl, all we
310 * care about is MTU discovery.
312 nxtsz = ntohl(icmp6->icmp6_mtu);
313 /* Stop any PMTU timer */
314 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
316 /* Adjust destination size limit */
317 if (net->mtu > nxtsz) {
320 net->mtu -= sizeof(struct udphdr);
323 /* now what about the ep? */
324 if (stcb->asoc.smallest_mtu > nxtsz) {
325 struct sctp_tmit_chunk *chk;
327 /* Adjust that too */
328 stcb->asoc.smallest_mtu = nxtsz;
329 /* now off to subtract IP_DF flag if needed */
331 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
332 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
333 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
336 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
337 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
339 * For this guy we also mark for immediate
340 * resend since we sent to big of chunk
342 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
343 if (chk->sent != SCTP_DATAGRAM_RESEND)
344 stcb->asoc.sent_queue_retran_cnt++;
345 chk->sent = SCTP_DATAGRAM_RESEND;
346 chk->rec.data.doing_fast_retransmit = 0;
348 chk->sent = SCTP_DATAGRAM_RESEND;
349 /* Clear any time so NO RTT is being done */
350 chk->sent_rcv_time.tv_sec = 0;
351 chk->sent_rcv_time.tv_usec = 0;
352 stcb->asoc.total_flight -= chk->send_size;
353 net->flight_size -= chk->send_size;
357 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
360 SCTP_TCB_UNLOCK(stcb);
366 sctp6_notify(struct sctp_inpcb *inp,
367 struct icmp6_hdr *icmph,
370 struct sctp_tcb *stcb,
371 struct sctp_nets *net)
373 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
379 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
380 (sh == NULL) || (to == NULL)) {
382 SCTP_TCB_UNLOCK(stcb);
385 /* First job is to verify the vtag matches what I would send */
386 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
387 SCTP_TCB_UNLOCK(stcb);
390 if (icmph->icmp6_type != ICMP_UNREACH) {
391 /* We only care about unreachable */
392 SCTP_TCB_UNLOCK(stcb);
395 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
396 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
397 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
398 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
399 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
400 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
401 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
402 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
405 * Hmm reachablity problems we must examine closely. If its
406 * not reachable, we may have lost a network. Or if there is
407 * NO protocol at the other end named SCTP. well we consider
410 if (net->dest_state & SCTP_ADDR_REACHABLE) {
411 /* Ok that destination is NOT reachable */
412 net->dest_state &= ~SCTP_ADDR_REACHABLE;
413 net->dest_state &= ~SCTP_ADDR_PF;
414 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
415 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
417 SCTP_TCB_UNLOCK(stcb);
418 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
419 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
421 * Here the peer is either playing tricks on us, including
422 * an address that belongs to someone who does not support
423 * SCTP OR was a userland implementation that shutdown and
424 * now is dead. In either case treat it like a OOTB abort
427 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
428 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
429 so = SCTP_INP_SO(inp);
430 atomic_add_int(&stcb->asoc.refcnt, 1);
431 SCTP_TCB_UNLOCK(stcb);
432 SCTP_SOCKET_LOCK(so, 1);
434 atomic_subtract_int(&stcb->asoc.refcnt, 1);
436 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
437 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
438 SCTP_SOCKET_UNLOCK(so, 1);
439 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
441 /* no need to unlock here, since the TCB is gone */
443 SCTP_TCB_UNLOCK(stcb);
450 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
453 struct ip6ctlparam *ip6cp = NULL;
456 vrf_id = SCTP_DEFAULT_VRFID;
458 if (pktdst->sa_family != AF_INET6 ||
459 pktdst->sa_len != sizeof(struct sockaddr_in6))
462 if ((unsigned)cmd >= PRC_NCMDS)
464 if (PRC_IS_REDIRECT(cmd)) {
466 } else if (inet6ctlerrmap[cmd] == 0) {
469 /* if the parameter is from icmp6, decode it. */
471 ip6cp = (struct ip6ctlparam *)d;
473 ip6cp = (struct ip6ctlparam *)NULL;
478 * XXX: We assume that when IPV6 is non NULL, M and OFF are
481 /* check if we can safely examine src and dst ports */
482 struct sctp_inpcb *inp = NULL;
483 struct sctp_tcb *stcb = NULL;
484 struct sctp_nets *net = NULL;
485 struct sockaddr_in6 final;
487 if (ip6cp->ip6c_m == NULL)
490 bzero(&sh, sizeof(sh));
491 bzero(&final, sizeof(final));
494 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
496 ip6cp->ip6c_src->sin6_port = sh.src_port;
497 final.sin6_len = sizeof(final);
498 final.sin6_family = AF_INET6;
499 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
500 final.sin6_port = sh.dest_port;
501 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
502 (struct sockaddr *)&final,
503 &inp, &net, 1, vrf_id);
504 /* inp's ref-count increased && stcb locked */
505 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
506 if (cmd == PRC_MSGSIZE) {
507 sctp6_notify_mbuf(inp,
512 /* inp's ref-count reduced && stcb unlocked */
514 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
515 (struct sockaddr *)&final,
517 /* inp's ref-count reduced && stcb unlocked */
520 if (PRC_IS_REDIRECT(cmd) && inp) {
521 in6_rtchange((struct in6pcb *)inp,
522 inet6ctlerrmap[cmd]);
525 /* reduce inp's ref-count */
527 SCTP_INP_DECR_REF(inp);
528 SCTP_INP_WUNLOCK(inp);
531 SCTP_TCB_UNLOCK(stcb);
537 * this routine can probably be collasped into the one in sctp_userreq.c
538 * since they do the same thing and now we lookup with a sockaddr
541 sctp6_getcred(SYSCTL_HANDLER_ARGS)
544 struct sockaddr_in6 addrs[2];
545 struct sctp_inpcb *inp;
546 struct sctp_nets *net;
547 struct sctp_tcb *stcb;
551 vrf_id = SCTP_DEFAULT_VRFID;
553 error = priv_check(req->td, PRIV_NETINET_GETCRED);
557 if (req->newlen != sizeof(addrs)) {
558 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
561 if (req->oldlen != sizeof(struct ucred)) {
562 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
565 error = SYSCTL_IN(req, addrs, sizeof(addrs));
569 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
571 &inp, &net, 1, vrf_id);
572 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
573 if ((inp != NULL) && (stcb == NULL)) {
574 /* reduce ref-count */
576 SCTP_INP_DECR_REF(inp);
579 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
583 SCTP_TCB_UNLOCK(stcb);
585 * We use the write lock here, only since in the error leg we need
586 * it. If we used RLOCK, then we would have to
587 * wlock/decr/unlock/rlock. Which in theory could create a hole.
588 * Better to use higher wlock.
592 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
594 SCTP_INP_WUNLOCK(inp);
597 cru2x(inp->sctp_socket->so_cred, &xuc);
598 SCTP_INP_WUNLOCK(inp);
599 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
604 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
606 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
609 /* This is the same as the sctp_abort() could be made common */
611 sctp6_abort(struct socket *so)
613 struct sctp_inpcb *inp;
616 inp = (struct sctp_inpcb *)so->so_pcb;
618 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
622 flags = inp->sctp_flags;
623 #ifdef SCTP_LOG_CLOSING
624 sctp_log_closing(inp, NULL, 17);
626 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
627 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
628 #ifdef SCTP_LOG_CLOSING
629 sctp_log_closing(inp, NULL, 16);
631 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
632 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
634 SCTP_SB_CLEAR(so->so_snd);
636 * same for the rcv ones, they are only here for the
639 SCTP_SB_CLEAR(so->so_rcv);
640 /* Now null out the reference, we are completely detached. */
644 flags = inp->sctp_flags;
645 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
646 goto sctp_must_try_again;
653 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
657 struct sctp_inpcb *inp;
658 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
660 inp = (struct sctp_inpcb *)so->so_pcb;
662 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
665 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
666 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
670 error = sctp_inpcb_alloc(so, vrf_id);
673 inp = (struct sctp_inpcb *)so->so_pcb;
675 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
676 inp6 = (struct in6pcb *)inp;
678 inp6->inp_vflag |= INP_IPV6;
679 inp6->in6p_hops = -1; /* use kernel default */
680 inp6->in6p_cksum = -1; /* just to be sure */
683 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
684 * socket as well, because the socket may be bound to an IPv6
685 * wildcard address, which may match an IPv4-mapped IPv6 address.
687 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
690 * Hmm what about the IPSEC stuff that is missing here but in
693 SCTP_INP_WUNLOCK(inp);
698 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
700 struct sctp_inpcb *inp;
704 inp = (struct sctp_inpcb *)so->so_pcb;
706 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
710 switch (addr->sa_family) {
713 if (addr->sa_len != sizeof(struct sockaddr_in)) {
714 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
721 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
722 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
732 inp6 = (struct in6pcb *)inp;
733 inp6->inp_vflag &= ~INP_IPV4;
734 inp6->inp_vflag |= INP_IPV6;
735 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
736 switch (addr->sa_family) {
739 /* binding v4 addr to v6 socket, so reset flags */
740 inp6->inp_vflag |= INP_IPV4;
741 inp6->inp_vflag &= ~INP_IPV6;
747 struct sockaddr_in6 *sin6_p;
749 sin6_p = (struct sockaddr_in6 *)addr;
751 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
752 inp6->inp_vflag |= INP_IPV4;
755 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
756 struct sockaddr_in sin;
758 in6_sin6_2_sin(&sin, sin6_p);
759 inp6->inp_vflag |= INP_IPV4;
760 inp6->inp_vflag &= ~INP_IPV6;
761 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
771 } else if (addr != NULL) {
772 struct sockaddr_in6 *sin6_p;
774 /* IPV6_V6ONLY socket */
776 if (addr->sa_family == AF_INET) {
777 /* can't bind v4 addr to v6 only socket! */
778 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
782 sin6_p = (struct sockaddr_in6 *)addr;
784 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
785 /* can't bind v4-mapped addrs either! */
786 /* NOTE: we don't support SIIT */
787 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
791 error = sctp_inpcb_bind(so, addr, NULL, p);
797 sctp6_close(struct socket *so)
802 /* This could be made common with sctp_detach() since they are identical */
806 sctp6_disconnect(struct socket *so)
808 return (sctp_disconnect(so));
813 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
814 struct mbuf *control, struct thread *p);
818 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
819 struct mbuf *control, struct thread *p)
821 struct sctp_inpcb *inp;
825 struct sockaddr_in6 *sin6;
828 /* No SPL needed since sctp_output does this */
830 inp = (struct sctp_inpcb *)so->so_pcb;
833 SCTP_RELEASE_PKT(control);
837 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
840 inp6 = (struct in6pcb *)inp;
842 * For the TCP model we may get a NULL addr, if we are a connected
845 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
852 SCTP_RELEASE_PKT(control);
855 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
856 return (EDESTADDRREQ);
859 sin6 = (struct sockaddr_in6 *)addr;
860 if (SCTP_IPV6_V6ONLY(inp6)) {
862 * if IPV6_V6ONLY flag, we discard datagrams destined to a
863 * v4 addr or v4-mapped addr
865 if (addr->sa_family == AF_INET) {
866 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
869 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
874 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
875 if (!MODULE_GLOBAL(ip6_v6only)) {
876 struct sockaddr_in sin;
878 /* convert v4-mapped into v4 addr and send */
879 in6_sin6_2_sin(&sin, sin6);
880 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
883 /* mapped addresses aren't enabled */
884 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
890 /* now what about control */
893 SCTP_PRINTF("huh? control set?\n");
894 SCTP_RELEASE_PKT(inp->control);
897 inp->control = control;
901 SCTP_BUF_NEXT(inp->pkt_last) = m;
904 inp->pkt_last = inp->pkt = m;
907 /* FreeBSD and MacOSX uses a flag passed */
908 ((flags & PRUS_MORETOCOME) == 0)
911 * note with the current version this code will only be used
912 * by OpenBSD, NetBSD and FreeBSD have methods for
913 * re-defining sosend() to use sctp_sosend(). One can
914 * optionaly switch back to this code (by changing back the
915 * defininitions but this is not advisable.
919 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
929 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
933 struct sctp_inpcb *inp;
935 struct sctp_tcb *stcb;
938 struct sockaddr_in6 *sin6;
939 struct sockaddr_storage ss;
943 inp6 = (struct in6pcb *)so->so_pcb;
944 inp = (struct sctp_inpcb *)so->so_pcb;
946 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
947 return (ECONNRESET); /* I made the same as TCP since we are
951 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
954 switch (addr->sa_family) {
957 if (addr->sa_len != sizeof(struct sockaddr_in)) {
958 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
965 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
966 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
972 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
976 vrf_id = inp->def_vrf_id;
977 SCTP_ASOC_CREATE_LOCK(inp);
979 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
980 SCTP_PCB_FLAGS_UNBOUND) {
981 /* Bind a ephemeral port */
982 SCTP_INP_RUNLOCK(inp);
983 error = sctp6_bind(so, NULL, p);
985 SCTP_ASOC_CREATE_UNLOCK(inp);
991 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
992 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
993 /* We are already connected AND the TCP model */
994 SCTP_INP_RUNLOCK(inp);
995 SCTP_ASOC_CREATE_UNLOCK(inp);
996 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1000 sin6 = (struct sockaddr_in6 *)addr;
1001 if (SCTP_IPV6_V6ONLY(inp6)) {
1003 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1004 * addr or v4-mapped addr
1006 if (addr->sa_family == AF_INET) {
1007 SCTP_INP_RUNLOCK(inp);
1008 SCTP_ASOC_CREATE_UNLOCK(inp);
1009 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1012 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1013 SCTP_INP_RUNLOCK(inp);
1014 SCTP_ASOC_CREATE_UNLOCK(inp);
1015 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1019 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1020 if (!MODULE_GLOBAL(ip6_v6only)) {
1021 /* convert v4-mapped into v4 addr */
1022 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1023 addr = (struct sockaddr *)&ss;
1025 /* mapped addresses aren't enabled */
1026 SCTP_INP_RUNLOCK(inp);
1027 SCTP_ASOC_CREATE_UNLOCK(inp);
1028 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1033 /* Now do we connect? */
1034 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1035 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1037 SCTP_TCB_UNLOCK(stcb);
1039 SCTP_INP_RUNLOCK(inp);
1041 SCTP_INP_RUNLOCK(inp);
1042 SCTP_INP_WLOCK(inp);
1043 SCTP_INP_INCR_REF(inp);
1044 SCTP_INP_WUNLOCK(inp);
1045 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1047 SCTP_INP_WLOCK(inp);
1048 SCTP_INP_DECR_REF(inp);
1049 SCTP_INP_WUNLOCK(inp);
1054 /* Already have or am bring up an association */
1055 SCTP_ASOC_CREATE_UNLOCK(inp);
1056 SCTP_TCB_UNLOCK(stcb);
1057 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1060 /* We are GOOD to go */
1061 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1062 SCTP_ASOC_CREATE_UNLOCK(inp);
1064 /* Gak! no memory */
1067 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1068 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1069 /* Set the connected flag so we can queue data */
1072 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1073 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1075 /* initialize authentication parameters for the assoc */
1076 sctp_initialize_auth_params(inp, stcb);
1078 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1079 SCTP_TCB_UNLOCK(stcb);
1084 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1086 struct sockaddr_in6 *sin6;
1087 struct sctp_inpcb *inp;
1089 struct sctp_ifa *sctp_ifa;
1094 * Do the malloc first in case it blocks.
1096 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1099 sin6->sin6_family = AF_INET6;
1100 sin6->sin6_len = sizeof(*sin6);
1102 inp = (struct sctp_inpcb *)so->so_pcb;
1104 SCTP_FREE_SONAME(sin6);
1105 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1106 return (ECONNRESET);
1108 SCTP_INP_RLOCK(inp);
1109 sin6->sin6_port = inp->sctp_lport;
1110 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1111 /* For the bound all case you get back 0 */
1112 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1113 struct sctp_tcb *stcb;
1114 struct sockaddr_in6 *sin_a6;
1115 struct sctp_nets *net;
1118 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1124 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1125 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1127 /* this will make coverity happy */
1130 if (sin_a6->sin6_family == AF_INET6) {
1135 if ((!fnd) || (sin_a6 == NULL)) {
1139 vrf_id = inp->def_vrf_id;
1140 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1142 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1145 /* For the bound all case you get back 0 */
1147 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1150 /* Take the first IPv6 address in the list */
1151 struct sctp_laddr *laddr;
1154 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1155 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1156 struct sockaddr_in6 *sin_a;
1158 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1159 sin6->sin6_addr = sin_a->sin6_addr;
1165 SCTP_FREE_SONAME(sin6);
1166 SCTP_INP_RUNLOCK(inp);
1167 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1171 SCTP_INP_RUNLOCK(inp);
1172 /* Scoping things for v6 */
1173 if ((error = sa6_recoverscope(sin6)) != 0) {
1174 SCTP_FREE_SONAME(sin6);
1177 (*addr) = (struct sockaddr *)sin6;
1182 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1184 struct sockaddr_in6 *sin6;
1186 struct sockaddr_in6 *sin_a6;
1187 struct sctp_inpcb *inp;
1188 struct sctp_tcb *stcb;
1189 struct sctp_nets *net;
1192 /* Do the malloc first in case it blocks. */
1193 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1196 sin6->sin6_family = AF_INET6;
1197 sin6->sin6_len = sizeof(*sin6);
1199 inp = (struct sctp_inpcb *)so->so_pcb;
1200 if ((inp == NULL) ||
1201 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1202 /* UDP type and listeners will drop out here */
1203 SCTP_FREE_SONAME(sin6);
1204 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1207 SCTP_INP_RLOCK(inp);
1208 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1210 SCTP_TCB_LOCK(stcb);
1212 SCTP_INP_RUNLOCK(inp);
1214 SCTP_FREE_SONAME(sin6);
1215 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1216 return (ECONNRESET);
1219 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1220 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1221 if (sin_a6->sin6_family == AF_INET6) {
1223 sin6->sin6_port = stcb->rport;
1224 sin6->sin6_addr = sin_a6->sin6_addr;
1228 SCTP_TCB_UNLOCK(stcb);
1230 /* No IPv4 address */
1231 SCTP_FREE_SONAME(sin6);
1232 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1235 if ((error = sa6_recoverscope(sin6)) != 0)
1237 *addr = (struct sockaddr *)sin6;
1242 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1245 struct sockaddr *addr;
1248 struct in6pcb *inp6 = sotoin6pcb(so);
1252 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1255 /* allow v6 addresses precedence */
1256 error = sctp6_getaddr(so, nam);
1259 /* try v4 next if v6 failed */
1260 error = sctp_ingetaddr(so, nam);
1265 /* if I'm V6ONLY, convert it to v4-mapped */
1266 if (SCTP_IPV6_V6ONLY(inp6)) {
1267 struct sockaddr_in6 sin6;
1269 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1270 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1279 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1282 struct sockaddr *addr;
1285 struct in6pcb *inp6 = sotoin6pcb(so);
1289 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1292 /* allow v6 addresses precedence */
1293 error = sctp6_peeraddr(so, nam);
1296 /* try v4 next if v6 failed */
1297 error = sctp_peeraddr(so, nam);
1302 /* if I'm V6ONLY, convert it to v4-mapped */
1303 if (SCTP_IPV6_V6ONLY(inp6)) {
1304 struct sockaddr_in6 sin6;
1306 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1307 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1314 struct pr_usrreqs sctp6_usrreqs = {
1315 .pru_abort = sctp6_abort,
1316 .pru_accept = sctp_accept,
1317 .pru_attach = sctp6_attach,
1318 .pru_bind = sctp6_bind,
1319 .pru_connect = sctp6_connect,
1320 .pru_control = in6_control,
1321 .pru_close = sctp6_close,
1322 .pru_detach = sctp6_close,
1323 .pru_sopoll = sopoll_generic,
1324 .pru_flush = sctp_flush,
1325 .pru_disconnect = sctp6_disconnect,
1326 .pru_listen = sctp_listen,
1327 .pru_peeraddr = sctp6_getpeeraddr,
1328 .pru_send = sctp6_send,
1329 .pru_shutdown = sctp_shutdown,
1330 .pru_sockaddr = sctp6_in6getaddr,
1331 .pru_sosend = sctp_sosend,
1332 .pru_soreceive = sctp_soreceive