2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, 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.
32 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #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)
74 struct sctp_inpcb *in6p = NULL;
75 struct sctp_nets *net;
80 struct inpcb *in6p_ip;
83 struct sctp_chunkhdr *ch;
84 int length, offset, iphlen;
86 struct sctp_tcb *stcb = NULL;
89 #if !defined(SCTP_WITH_NO_CSUM)
90 uint32_t check, calc_check;
96 /* get the VRF and table id's */
97 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
98 SCTP_RELEASE_PKT(*i_pak);
101 m = SCTP_HEADER_TO_CHAIN(*i_pak);
102 pkt_len = SCTP_HEADER_LEN((*i_pak));
104 #ifdef SCTP_PACKET_LOGGING
105 sctp_packet_log(m, pkt_len);
107 ip6 = mtod(m, struct ip6_hdr *);
108 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
109 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
110 (int)(sizeof(*sh) + sizeof(*ch)));
112 SCTP_STAT_INCR(sctps_hdrops);
115 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
117 offset = iphlen + sizeof(*sh) + sizeof(*ch);
118 SCTPDBG(SCTP_DEBUG_INPUT1,
119 "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
122 #if defined(NFAITH) && NFAITH > 0
124 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
125 /* XXX send icmp6 host/port unreach? */
128 #endif /* NFAITH defined and > 0 */
129 SCTP_STAT_INCR(sctps_recvpackets);
130 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
131 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
133 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
134 /* No multi-cast support in SCTP */
137 /* destination port of 0 is illegal, based on RFC2960. */
138 if (sh->dest_port == 0)
141 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
142 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
144 if_name(m->m_pkthdr.rcvif),
145 m->m_pkthdr.csum_flags);
146 #if defined(SCTP_WITH_NO_CSUM)
147 SCTP_STAT_INCR(sctps_recvnocrc);
149 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
150 SCTP_STAT_INCR(sctps_recvhwcrc);
153 check = sh->checksum; /* save incoming checksum */
154 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
155 (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
156 SCTP_STAT_INCR(sctps_recvnocrc);
159 sh->checksum = 0; /* prepare for calc */
160 calc_check = sctp_calculate_cksum(m, iphlen);
161 SCTP_STAT_INCR(sctps_recvswcrc);
162 if (calc_check != check) {
163 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p phlen:%d\n",
164 calc_check, check, m, iphlen);
165 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
166 sh, ch, &in6p, &net, vrf_id);
167 if ((net) && (port)) {
168 if (net->port == 0) {
169 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
173 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
174 net->flowid = m->m_pkthdr.flowid;
179 /* in6p's ref-count increased && stcb locked */
180 if ((in6p) && (stcb)) {
181 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
182 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
183 } else if ((in6p != NULL) && (stcb == NULL)) {
186 SCTP_STAT_INCR(sctps_badsum);
187 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
190 sh->checksum = calc_check;
196 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
197 * IP/SCTP/first chunk header...
199 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
200 sh, ch, &in6p, &net, vrf_id);
201 if ((net) && (port)) {
202 if (net->port == 0) {
203 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
207 if ((net != NULL) && (m->m_flags & M_FLOWID)) {
208 net->flowid = m->m_pkthdr.flowid;
213 /* in6p's ref-count increased */
215 struct sctp_init_chunk *init_chk, chunk_buf;
217 SCTP_STAT_INCR(sctps_noport);
218 if (ch->chunk_type == SCTP_INITIATION) {
220 * we do a trick here to get the INIT tag, dig in
221 * and get the tag from the INIT and put it in the
224 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
225 iphlen + sizeof(*sh), sizeof(*init_chk),
226 (uint8_t *) & chunk_buf);
228 sh->v_tag = init_chk->init.initiate_tag;
232 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
233 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
236 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
239 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
240 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
242 } else if (stcb == NULL) {
247 * Check AH/ESP integrity.
249 in6p_ip = (struct inpcb *)in6p;
250 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
252 MODULE_GLOBAL(ipsec6stat).in_polvio++;
258 * CONTROL chunk processing
260 offset -= sizeof(*ch);
261 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
263 /* Length now holds the total packet length payload + iphlen */
264 length = ntohs(ip6->ip6_plen) + iphlen;
266 /* sa_ignore NO_NULL_CHK */
267 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
268 in6p, stcb, net, ecn_bits, vrf_id, port);
269 /* inp's ref-count reduced && stcb unlocked */
270 /* XXX this stuff below gets moved to appropriate parts later... */
273 if ((in6p) && refcount_up) {
274 /* reduce ref-count */
275 SCTP_INP_WLOCK(in6p);
276 SCTP_INP_DECR_REF(in6p);
277 SCTP_INP_WUNLOCK(in6p);
283 SCTP_TCB_UNLOCK(stcb);
285 if ((in6p) && refcount_up) {
286 /* reduce ref-count */
287 SCTP_INP_WLOCK(in6p);
288 SCTP_INP_DECR_REF(in6p);
289 SCTP_INP_WUNLOCK(in6p);
298 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
299 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
303 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
304 (icmp6 == NULL) || (sh == NULL)) {
307 /* First do we even look at it? */
308 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
311 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
312 /* not PACKET TO BIG */
316 * ok we need to look closely. We could even get smarter and look at
317 * anyone that we sent to in case we get a different ICMP that tells
318 * us there is no way to reach a host, but for this impl, all we
319 * care about is MTU discovery.
321 nxtsz = ntohl(icmp6->icmp6_mtu);
322 /* Stop any PMTU timer */
323 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
325 /* Adjust destination size limit */
326 if (net->mtu > nxtsz) {
329 net->mtu -= sizeof(struct udphdr);
332 /* now what about the ep? */
333 if (stcb->asoc.smallest_mtu > nxtsz) {
334 struct sctp_tmit_chunk *chk;
336 /* Adjust that too */
337 stcb->asoc.smallest_mtu = nxtsz;
338 /* now off to subtract IP_DF flag if needed */
340 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
341 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
342 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
345 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
346 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
348 * For this guy we also mark for immediate
349 * resend since we sent to big of chunk
351 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
352 if (chk->sent != SCTP_DATAGRAM_RESEND)
353 stcb->asoc.sent_queue_retran_cnt++;
354 chk->sent = SCTP_DATAGRAM_RESEND;
355 chk->rec.data.doing_fast_retransmit = 0;
357 chk->sent = SCTP_DATAGRAM_RESEND;
358 /* Clear any time so NO RTT is being done */
359 chk->sent_rcv_time.tv_sec = 0;
360 chk->sent_rcv_time.tv_usec = 0;
361 stcb->asoc.total_flight -= chk->send_size;
362 net->flight_size -= chk->send_size;
366 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
369 SCTP_TCB_UNLOCK(stcb);
375 sctp6_notify(struct sctp_inpcb *inp,
376 struct icmp6_hdr *icmph,
379 struct sctp_tcb *stcb,
380 struct sctp_nets *net)
382 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
390 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
391 (sh == NULL) || (to == NULL)) {
393 SCTP_TCB_UNLOCK(stcb);
396 /* First job is to verify the vtag matches what I would send */
397 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
398 SCTP_TCB_UNLOCK(stcb);
401 if (icmph->icmp6_type != ICMP_UNREACH) {
402 /* We only care about unreachable */
403 SCTP_TCB_UNLOCK(stcb);
406 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
407 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
408 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
409 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
410 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
411 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
412 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
413 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
416 * Hmm reachablity problems we must examine closely. If its
417 * not reachable, we may have lost a network. Or if there is
418 * NO protocol at the other end named SCTP. well we consider
421 if (net->dest_state & SCTP_ADDR_REACHABLE) {
422 /* Ok that destination is NOT reachable */
423 net->dest_state &= ~SCTP_ADDR_REACHABLE;
424 net->dest_state &= ~SCTP_ADDR_PF;
425 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
426 stcb, SCTP_FAILED_THRESHOLD,
427 (void *)net, SCTP_SO_NOT_LOCKED);
429 SCTP_TCB_UNLOCK(stcb);
430 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
431 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
433 * Here the peer is either playing tricks on us, including
434 * an address that belongs to someone who does not support
435 * SCTP OR was a userland implementation that shutdown and
436 * now is dead. In either case treat it like a OOTB abort
439 reason = SCTP_PEER_FAULTY;
440 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
441 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
442 so = SCTP_INP_SO(inp);
443 atomic_add_int(&stcb->asoc.refcnt, 1);
444 SCTP_TCB_UNLOCK(stcb);
445 SCTP_SOCKET_LOCK(so, 1);
447 atomic_subtract_int(&stcb->asoc.refcnt, 1);
449 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
450 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
451 SCTP_SOCKET_UNLOCK(so, 1);
452 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
454 /* no need to unlock here, since the TCB is gone */
456 SCTP_TCB_UNLOCK(stcb);
463 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
466 struct ip6ctlparam *ip6cp = NULL;
469 vrf_id = SCTP_DEFAULT_VRFID;
471 if (pktdst->sa_family != AF_INET6 ||
472 pktdst->sa_len != sizeof(struct sockaddr_in6))
475 if ((unsigned)cmd >= PRC_NCMDS)
477 if (PRC_IS_REDIRECT(cmd)) {
479 } else if (inet6ctlerrmap[cmd] == 0) {
482 /* if the parameter is from icmp6, decode it. */
484 ip6cp = (struct ip6ctlparam *)d;
486 ip6cp = (struct ip6ctlparam *)NULL;
491 * XXX: We assume that when IPV6 is non NULL, M and OFF are
494 /* check if we can safely examine src and dst ports */
495 struct sctp_inpcb *inp = NULL;
496 struct sctp_tcb *stcb = NULL;
497 struct sctp_nets *net = NULL;
498 struct sockaddr_in6 final;
500 if (ip6cp->ip6c_m == NULL)
503 bzero(&sh, sizeof(sh));
504 bzero(&final, sizeof(final));
507 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
509 ip6cp->ip6c_src->sin6_port = sh.src_port;
510 final.sin6_len = sizeof(final);
511 final.sin6_family = AF_INET6;
512 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
513 final.sin6_port = sh.dest_port;
514 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
515 (struct sockaddr *)&final,
516 &inp, &net, 1, vrf_id);
517 /* inp's ref-count increased && stcb locked */
518 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
519 if (cmd == PRC_MSGSIZE) {
520 sctp6_notify_mbuf(inp,
525 /* inp's ref-count reduced && stcb unlocked */
527 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
528 (struct sockaddr *)&final,
530 /* inp's ref-count reduced && stcb unlocked */
533 if (PRC_IS_REDIRECT(cmd) && inp) {
534 in6_rtchange((struct in6pcb *)inp,
535 inet6ctlerrmap[cmd]);
538 /* reduce inp's ref-count */
540 SCTP_INP_DECR_REF(inp);
541 SCTP_INP_WUNLOCK(inp);
544 SCTP_TCB_UNLOCK(stcb);
550 * this routine can probably be collasped into the one in sctp_userreq.c
551 * since they do the same thing and now we lookup with a sockaddr
554 sctp6_getcred(SYSCTL_HANDLER_ARGS)
557 struct sockaddr_in6 addrs[2];
558 struct sctp_inpcb *inp;
559 struct sctp_nets *net;
560 struct sctp_tcb *stcb;
564 vrf_id = SCTP_DEFAULT_VRFID;
566 error = priv_check(req->td, PRIV_NETINET_GETCRED);
570 if (req->newlen != sizeof(addrs)) {
571 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
574 if (req->oldlen != sizeof(struct ucred)) {
575 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
578 error = SYSCTL_IN(req, addrs, sizeof(addrs));
582 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
584 &inp, &net, 1, vrf_id);
585 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
586 if ((inp != NULL) && (stcb == NULL)) {
587 /* reduce ref-count */
589 SCTP_INP_DECR_REF(inp);
592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
596 SCTP_TCB_UNLOCK(stcb);
598 * We use the write lock here, only since in the error leg we need
599 * it. If we used RLOCK, then we would have to
600 * wlock/decr/unlock/rlock. Which in theory could create a hole.
601 * Better to use higher wlock.
605 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
607 SCTP_INP_WUNLOCK(inp);
610 cru2x(inp->sctp_socket->so_cred, &xuc);
611 SCTP_INP_WUNLOCK(inp);
612 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
617 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
619 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
622 /* This is the same as the sctp_abort() could be made common */
624 sctp6_abort(struct socket *so)
626 struct sctp_inpcb *inp;
629 inp = (struct sctp_inpcb *)so->so_pcb;
631 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
635 flags = inp->sctp_flags;
636 #ifdef SCTP_LOG_CLOSING
637 sctp_log_closing(inp, NULL, 17);
639 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
640 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
641 #ifdef SCTP_LOG_CLOSING
642 sctp_log_closing(inp, NULL, 16);
644 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
645 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
647 SCTP_SB_CLEAR(so->so_snd);
649 * same for the rcv ones, they are only here for the
652 SCTP_SB_CLEAR(so->so_rcv);
653 /* Now null out the reference, we are completely detached. */
657 flags = inp->sctp_flags;
658 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
659 goto sctp_must_try_again;
666 sctp6_attach(struct socket *so, int proto, struct thread *p)
670 struct sctp_inpcb *inp;
671 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
673 inp = (struct sctp_inpcb *)so->so_pcb;
675 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
678 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
679 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
683 error = sctp_inpcb_alloc(so, vrf_id);
686 inp = (struct sctp_inpcb *)so->so_pcb;
688 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
689 inp6 = (struct in6pcb *)inp;
691 inp6->inp_vflag |= INP_IPV6;
692 inp6->in6p_hops = -1; /* use kernel default */
693 inp6->in6p_cksum = -1; /* just to be sure */
696 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
697 * socket as well, because the socket may be bound to an IPv6
698 * wildcard address, which may match an IPv4-mapped IPv6 address.
700 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
703 * Hmm what about the IPSEC stuff that is missing here but in
706 SCTP_INP_WUNLOCK(inp);
711 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
713 struct sctp_inpcb *inp;
717 inp = (struct sctp_inpcb *)so->so_pcb;
719 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
723 switch (addr->sa_family) {
726 if (addr->sa_len != sizeof(struct sockaddr_in)) {
727 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
734 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
735 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
741 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
745 inp6 = (struct in6pcb *)inp;
746 inp6->inp_vflag &= ~INP_IPV4;
747 inp6->inp_vflag |= INP_IPV6;
748 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
749 switch (addr->sa_family) {
752 /* binding v4 addr to v6 socket, so reset flags */
753 inp6->inp_vflag |= INP_IPV4;
754 inp6->inp_vflag &= ~INP_IPV6;
760 struct sockaddr_in6 *sin6_p;
762 sin6_p = (struct sockaddr_in6 *)addr;
764 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
765 inp6->inp_vflag |= INP_IPV4;
768 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
769 struct sockaddr_in sin;
771 in6_sin6_2_sin(&sin, sin6_p);
772 inp6->inp_vflag |= INP_IPV4;
773 inp6->inp_vflag &= ~INP_IPV6;
774 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
784 } else if (addr != NULL) {
785 struct sockaddr_in6 *sin6_p;
787 /* IPV6_V6ONLY socket */
789 if (addr->sa_family == AF_INET) {
790 /* can't bind v4 addr to v6 only socket! */
791 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
795 sin6_p = (struct sockaddr_in6 *)addr;
797 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
798 /* can't bind v4-mapped addrs either! */
799 /* NOTE: we don't support SIIT */
800 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
804 error = sctp_inpcb_bind(so, addr, NULL, p);
810 sctp6_close(struct socket *so)
815 /* This could be made common with sctp_detach() since they are identical */
819 sctp6_disconnect(struct socket *so)
821 return (sctp_disconnect(so));
826 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
827 struct mbuf *control, struct thread *p);
831 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
832 struct mbuf *control, struct thread *p)
834 struct sctp_inpcb *inp;
838 struct sockaddr_in6 *sin6;
841 /* No SPL needed since sctp_output does this */
843 inp = (struct sctp_inpcb *)so->so_pcb;
846 SCTP_RELEASE_PKT(control);
850 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
853 inp6 = (struct in6pcb *)inp;
855 * For the TCP model we may get a NULL addr, if we are a connected
858 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
865 SCTP_RELEASE_PKT(control);
868 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
869 return (EDESTADDRREQ);
872 sin6 = (struct sockaddr_in6 *)addr;
873 if (SCTP_IPV6_V6ONLY(inp6)) {
875 * if IPV6_V6ONLY flag, we discard datagrams destined to a
876 * v4 addr or v4-mapped addr
878 if (addr->sa_family == AF_INET) {
879 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
882 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
883 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
887 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
888 if (!MODULE_GLOBAL(ip6_v6only)) {
889 struct sockaddr_in sin;
891 /* convert v4-mapped into v4 addr and send */
892 in6_sin6_2_sin(&sin, sin6);
893 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
896 /* mapped addresses aren't enabled */
897 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
903 /* now what about control */
906 SCTP_PRINTF("huh? control set?\n");
907 SCTP_RELEASE_PKT(inp->control);
910 inp->control = control;
914 SCTP_BUF_NEXT(inp->pkt_last) = m;
917 inp->pkt_last = inp->pkt = m;
920 /* FreeBSD and MacOSX uses a flag passed */
921 ((flags & PRUS_MORETOCOME) == 0)
924 * note with the current version this code will only be used
925 * by OpenBSD, NetBSD and FreeBSD have methods for
926 * re-defining sosend() to use sctp_sosend(). One can
927 * optionaly switch back to this code (by changing back the
928 * defininitions but this is not advisable.
932 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
942 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
946 struct sctp_inpcb *inp;
948 struct sctp_tcb *stcb;
951 struct sockaddr_in6 *sin6;
952 struct sockaddr_storage ss;
956 inp6 = (struct in6pcb *)so->so_pcb;
957 inp = (struct sctp_inpcb *)so->so_pcb;
959 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
960 return (ECONNRESET); /* I made the same as TCP since we are
964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
967 switch (addr->sa_family) {
970 if (addr->sa_len != sizeof(struct sockaddr_in)) {
971 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
978 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
979 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
985 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
989 vrf_id = inp->def_vrf_id;
990 SCTP_ASOC_CREATE_LOCK(inp);
992 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
993 SCTP_PCB_FLAGS_UNBOUND) {
994 /* Bind a ephemeral port */
995 SCTP_INP_RUNLOCK(inp);
996 error = sctp6_bind(so, NULL, p);
998 SCTP_ASOC_CREATE_UNLOCK(inp);
1002 SCTP_INP_RLOCK(inp);
1004 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1005 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1006 /* We are already connected AND the TCP model */
1007 SCTP_INP_RUNLOCK(inp);
1008 SCTP_ASOC_CREATE_UNLOCK(inp);
1009 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1010 return (EADDRINUSE);
1013 sin6 = (struct sockaddr_in6 *)addr;
1014 if (SCTP_IPV6_V6ONLY(inp6)) {
1016 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1017 * addr or v4-mapped addr
1019 if (addr->sa_family == AF_INET) {
1020 SCTP_INP_RUNLOCK(inp);
1021 SCTP_ASOC_CREATE_UNLOCK(inp);
1022 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1025 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1026 SCTP_INP_RUNLOCK(inp);
1027 SCTP_ASOC_CREATE_UNLOCK(inp);
1028 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1032 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1033 if (!MODULE_GLOBAL(ip6_v6only)) {
1034 /* convert v4-mapped into v4 addr */
1035 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1036 addr = (struct sockaddr *)&ss;
1038 /* mapped addresses aren't enabled */
1039 SCTP_INP_RUNLOCK(inp);
1040 SCTP_ASOC_CREATE_UNLOCK(inp);
1041 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1046 /* Now do we connect? */
1047 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1048 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1050 SCTP_TCB_UNLOCK(stcb);
1052 SCTP_INP_RUNLOCK(inp);
1054 SCTP_INP_RUNLOCK(inp);
1055 SCTP_INP_WLOCK(inp);
1056 SCTP_INP_INCR_REF(inp);
1057 SCTP_INP_WUNLOCK(inp);
1058 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1060 SCTP_INP_WLOCK(inp);
1061 SCTP_INP_DECR_REF(inp);
1062 SCTP_INP_WUNLOCK(inp);
1067 /* Already have or am bring up an association */
1068 SCTP_ASOC_CREATE_UNLOCK(inp);
1069 SCTP_TCB_UNLOCK(stcb);
1070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1073 /* We are GOOD to go */
1074 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1075 SCTP_ASOC_CREATE_UNLOCK(inp);
1077 /* Gak! no memory */
1080 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1081 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1082 /* Set the connected flag so we can queue data */
1085 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1086 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1088 /* initialize authentication parameters for the assoc */
1089 sctp_initialize_auth_params(inp, stcb);
1091 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1092 SCTP_TCB_UNLOCK(stcb);
1097 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1099 struct sockaddr_in6 *sin6;
1100 struct sctp_inpcb *inp;
1102 struct sctp_ifa *sctp_ifa;
1107 * Do the malloc first in case it blocks.
1109 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1112 sin6->sin6_family = AF_INET6;
1113 sin6->sin6_len = sizeof(*sin6);
1115 inp = (struct sctp_inpcb *)so->so_pcb;
1117 SCTP_FREE_SONAME(sin6);
1118 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1121 SCTP_INP_RLOCK(inp);
1122 sin6->sin6_port = inp->sctp_lport;
1123 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1124 /* For the bound all case you get back 0 */
1125 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1126 struct sctp_tcb *stcb;
1127 struct sockaddr_in6 *sin_a6;
1128 struct sctp_nets *net;
1131 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1137 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1138 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1140 /* this will make coverity happy */
1143 if (sin_a6->sin6_family == AF_INET6) {
1148 if ((!fnd) || (sin_a6 == NULL)) {
1152 vrf_id = inp->def_vrf_id;
1153 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1155 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1158 /* For the bound all case you get back 0 */
1160 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1163 /* Take the first IPv6 address in the list */
1164 struct sctp_laddr *laddr;
1167 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1168 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1169 struct sockaddr_in6 *sin_a;
1171 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1172 sin6->sin6_addr = sin_a->sin6_addr;
1178 SCTP_FREE_SONAME(sin6);
1179 SCTP_INP_RUNLOCK(inp);
1180 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1184 SCTP_INP_RUNLOCK(inp);
1185 /* Scoping things for v6 */
1186 if ((error = sa6_recoverscope(sin6)) != 0) {
1187 SCTP_FREE_SONAME(sin6);
1190 (*addr) = (struct sockaddr *)sin6;
1195 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1197 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1199 struct sockaddr_in6 *sin_a6;
1200 struct sctp_inpcb *inp;
1201 struct sctp_tcb *stcb;
1202 struct sctp_nets *net;
1207 * Do the malloc first in case it blocks.
1209 inp = (struct sctp_inpcb *)so->so_pcb;
1210 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1211 /* UDP type and listeners will drop out here */
1212 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1215 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1218 sin6->sin6_family = AF_INET6;
1219 sin6->sin6_len = sizeof(*sin6);
1221 /* We must recapture incase we blocked */
1222 inp = (struct sctp_inpcb *)so->so_pcb;
1224 SCTP_FREE_SONAME(sin6);
1225 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1228 SCTP_INP_RLOCK(inp);
1229 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1231 SCTP_TCB_LOCK(stcb);
1233 SCTP_INP_RUNLOCK(inp);
1235 SCTP_FREE_SONAME(sin6);
1236 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1240 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1241 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1242 if (sin_a6->sin6_family == AF_INET6) {
1244 sin6->sin6_port = stcb->rport;
1245 sin6->sin6_addr = sin_a6->sin6_addr;
1249 SCTP_TCB_UNLOCK(stcb);
1251 /* No IPv4 address */
1252 SCTP_FREE_SONAME(sin6);
1253 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1256 if ((error = sa6_recoverscope(sin6)) != 0)
1258 *addr = (struct sockaddr *)sin6;
1263 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1266 struct sockaddr *addr;
1269 struct in6pcb *inp6 = sotoin6pcb(so);
1273 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1276 /* allow v6 addresses precedence */
1277 error = sctp6_getaddr(so, nam);
1280 /* try v4 next if v6 failed */
1281 error = sctp_ingetaddr(so, nam);
1286 /* if I'm V6ONLY, convert it to v4-mapped */
1287 if (SCTP_IPV6_V6ONLY(inp6)) {
1288 struct sockaddr_in6 sin6;
1290 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1291 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1300 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1303 struct sockaddr *addr;
1306 struct in6pcb *inp6 = sotoin6pcb(so);
1310 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1313 /* allow v6 addresses precedence */
1314 error = sctp6_peeraddr(so, nam);
1317 /* try v4 next if v6 failed */
1318 error = sctp_peeraddr(so, nam);
1323 /* if I'm V6ONLY, convert it to v4-mapped */
1324 if (SCTP_IPV6_V6ONLY(inp6)) {
1325 struct sockaddr_in6 sin6;
1327 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1328 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1335 struct pr_usrreqs sctp6_usrreqs = {
1336 .pru_abort = sctp6_abort,
1337 .pru_accept = sctp_accept,
1338 .pru_attach = sctp6_attach,
1339 .pru_bind = sctp6_bind,
1340 .pru_connect = sctp6_connect,
1341 .pru_control = in6_control,
1342 .pru_close = sctp6_close,
1343 .pru_detach = sctp6_close,
1344 .pru_sopoll = sopoll_generic,
1345 .pru_flush = sctp_flush,
1346 .pru_disconnect = sctp6_disconnect,
1347 .pru_listen = sctp_listen,
1348 .pru_peeraddr = sctp6_getpeeraddr,
1349 .pru_send = sctp6_send,
1350 .pru_shutdown = sctp_shutdown,
1351 .pru_sockaddr = sctp6_in6getaddr,
1352 .pru_sosend = sctp_sosend,
1353 .pru_soreceive = sctp_soreceive