2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
30 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
41 #include <netinet6/sctp6_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_output.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #include <netinet/udp.h>
57 #include <netipsec/ipsec.h>
59 #include <netipsec/ipsec6.h>
63 extern struct protosw inetsw[];
66 sctp6_input(struct mbuf **i_pak, int *offp, int proto)
71 struct sctp_inpcb *in6p = NULL;
72 struct sctp_nets *net;
74 uint32_t check, calc_check;
76 struct inpcb *in6p_ip;
77 struct sctp_chunkhdr *ch;
78 int length, mlen, offset, iphlen;
80 struct sctp_tcb *stcb = NULL;
85 /* get the VRF and table id's */
86 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
87 SCTP_RELEASE_PKT(*i_pak);
90 m = SCTP_HEADER_TO_CHAIN(*i_pak);
91 pkt_len = SCTP_HEADER_LEN((*i_pak));
93 #ifdef SCTP_PACKET_LOGGING
94 sctp_packet_log(m, pkt_len);
96 ip6 = mtod(m, struct ip6_hdr *);
97 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
98 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
99 (int)(sizeof(*sh) + sizeof(*ch)));
101 SCTP_STAT_INCR(sctps_hdrops);
104 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
106 offset = iphlen + sizeof(*sh) + sizeof(*ch);
107 SCTPDBG(SCTP_DEBUG_INPUT1,
108 "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
111 #if defined(NFAITH) && NFAITH > 0
113 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
114 /* XXX send icmp6 host/port unreach? */
117 #endif /* NFAITH defined and > 0 */
118 SCTP_STAT_INCR(sctps_recvpackets);
119 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
120 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
122 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
123 /* No multi-cast support in SCTP */
126 /* destination port of 0 is illegal, based on RFC2960. */
127 if (sh->dest_port == 0)
129 check = sh->checksum; /* save incoming checksum */
130 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
131 (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
134 sh->checksum = 0; /* prepare for calc */
135 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
136 if (calc_check != check) {
137 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
138 calc_check, check, m, mlen, iphlen);
139 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
140 sh, ch, &in6p, &net, vrf_id);
141 if ((net) && (port)) {
142 if (net->port == 0) {
143 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
147 /* in6p's ref-count increased && stcb locked */
148 if ((in6p) && (stcb)) {
149 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
150 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
151 } else if ((in6p != NULL) && (stcb == NULL)) {
154 SCTP_STAT_INCR(sctps_badsum);
155 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
158 sh->checksum = calc_check;
163 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
164 * IP/SCTP/first chunk header...
166 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
167 sh, ch, &in6p, &net, vrf_id);
168 if ((net) && (port)) {
169 if (net->port == 0) {
170 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
174 /* in6p's ref-count increased */
176 struct sctp_init_chunk *init_chk, chunk_buf;
178 SCTP_STAT_INCR(sctps_noport);
179 if (ch->chunk_type == SCTP_INITIATION) {
181 * we do a trick here to get the INIT tag, dig in
182 * and get the tag from the INIT and put it in the
185 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
186 iphlen + sizeof(*sh), sizeof(*init_chk),
187 (uint8_t *) & chunk_buf);
189 sh->v_tag = init_chk->init.initiate_tag;
193 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
194 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
197 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
200 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
201 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
203 } else if (stcb == NULL) {
206 in6p_ip = (struct inpcb *)in6p;
209 * Check AH/ESP integrity.
211 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
213 MODULE_GLOBAL(MOD_IPSEC, ipsec6stat).in_polvio++;
219 * CONTROL chunk processing
221 offset -= sizeof(*ch);
222 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
224 /* Length now holds the total packet length payload + iphlen */
225 length = ntohs(ip6->ip6_plen) + iphlen;
227 /* sa_ignore NO_NULL_CHK */
228 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
229 in6p, stcb, net, ecn_bits, vrf_id, port);
230 /* inp's ref-count reduced && stcb unlocked */
231 /* XXX this stuff below gets moved to appropriate parts later... */
234 if ((in6p) && refcount_up) {
235 /* reduce ref-count */
236 SCTP_INP_WLOCK(in6p);
237 SCTP_INP_DECR_REF(in6p);
238 SCTP_INP_WUNLOCK(in6p);
244 SCTP_TCB_UNLOCK(stcb);
246 if ((in6p) && refcount_up) {
247 /* reduce ref-count */
248 SCTP_INP_WLOCK(in6p);
249 SCTP_INP_DECR_REF(in6p);
250 SCTP_INP_WUNLOCK(in6p);
259 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
260 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
264 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
265 (icmp6 == NULL) || (sh == NULL)) {
268 /* First do we even look at it? */
269 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
272 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
273 /* not PACKET TO BIG */
277 * ok we need to look closely. We could even get smarter and look at
278 * anyone that we sent to in case we get a different ICMP that tells
279 * us there is no way to reach a host, but for this impl, all we
280 * care about is MTU discovery.
282 nxtsz = ntohl(icmp6->icmp6_mtu);
283 /* Stop any PMTU timer */
284 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
286 /* Adjust destination size limit */
287 if (net->mtu > nxtsz) {
290 /* now what about the ep? */
291 if (stcb->asoc.smallest_mtu > nxtsz) {
292 struct sctp_tmit_chunk *chk;
294 /* Adjust that too */
295 stcb->asoc.smallest_mtu = nxtsz;
296 /* now off to subtract IP_DF flag if needed */
298 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
299 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
300 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
303 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
304 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
306 * For this guy we also mark for immediate
307 * resend since we sent to big of chunk
309 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
310 if (chk->sent != SCTP_DATAGRAM_RESEND)
311 stcb->asoc.sent_queue_retran_cnt++;
312 chk->sent = SCTP_DATAGRAM_RESEND;
313 chk->rec.data.doing_fast_retransmit = 0;
315 chk->sent = SCTP_DATAGRAM_RESEND;
316 /* Clear any time so NO RTT is being done */
317 chk->sent_rcv_time.tv_sec = 0;
318 chk->sent_rcv_time.tv_usec = 0;
319 stcb->asoc.total_flight -= chk->send_size;
320 net->flight_size -= chk->send_size;
324 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
327 SCTP_TCB_UNLOCK(stcb);
333 sctp6_notify(struct sctp_inpcb *inp,
334 struct icmp6_hdr *icmph,
337 struct sctp_tcb *stcb,
338 struct sctp_nets *net)
340 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
348 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
349 (sh == NULL) || (to == NULL)) {
351 SCTP_TCB_UNLOCK(stcb);
354 /* First job is to verify the vtag matches what I would send */
355 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
356 SCTP_TCB_UNLOCK(stcb);
359 if (icmph->icmp6_type != ICMP_UNREACH) {
360 /* We only care about unreachable */
361 SCTP_TCB_UNLOCK(stcb);
364 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
365 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
366 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
367 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
368 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
369 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
370 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
371 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
374 * Hmm reachablity problems we must examine closely. If its
375 * not reachable, we may have lost a network. Or if there is
376 * NO protocol at the other end named SCTP. well we consider
379 if (net->dest_state & SCTP_ADDR_REACHABLE) {
380 /* Ok that destination is NOT reachable */
381 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
383 net->failure_threshold,
386 net->dest_state &= ~SCTP_ADDR_REACHABLE;
387 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
389 * JRS 5/14/07 - If a destination is unreachable,
390 * the PF bit is turned off. This allows an
391 * unambiguous use of the PF bit for destinations
392 * that are reachable but potentially failed. If the
393 * destination is set to the unreachable state, also
394 * set the destination to the PF state.
397 * Add debug message here if destination is not in
400 /* Stop any running T3 timers here? */
401 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
402 net->dest_state &= ~SCTP_ADDR_PF;
403 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
406 net->error_count = net->failure_threshold + 1;
407 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
408 stcb, SCTP_FAILED_THRESHOLD,
409 (void *)net, SCTP_SO_NOT_LOCKED);
411 SCTP_TCB_UNLOCK(stcb);
412 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
413 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
415 * Here the peer is either playing tricks on us, including
416 * an address that belongs to someone who does not support
417 * SCTP OR was a userland implementation that shutdown and
418 * now is dead. In either case treat it like a OOTB abort
421 reason = SCTP_PEER_FAULTY;
422 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
423 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
424 so = SCTP_INP_SO(inp);
425 atomic_add_int(&stcb->asoc.refcnt, 1);
426 SCTP_TCB_UNLOCK(stcb);
427 SCTP_SOCKET_LOCK(so, 1);
429 atomic_subtract_int(&stcb->asoc.refcnt, 1);
431 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
432 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
433 SCTP_SOCKET_UNLOCK(so, 1);
434 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
436 /* no need to unlock here, since the TCB is gone */
438 SCTP_TCB_UNLOCK(stcb);
445 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
448 struct ip6ctlparam *ip6cp = NULL;
451 vrf_id = SCTP_DEFAULT_VRFID;
453 if (pktdst->sa_family != AF_INET6 ||
454 pktdst->sa_len != sizeof(struct sockaddr_in6))
457 if ((unsigned)cmd >= PRC_NCMDS)
459 if (PRC_IS_REDIRECT(cmd)) {
461 } else if (inet6ctlerrmap[cmd] == 0) {
464 /* if the parameter is from icmp6, decode it. */
466 ip6cp = (struct ip6ctlparam *)d;
468 ip6cp = (struct ip6ctlparam *)NULL;
473 * XXX: We assume that when IPV6 is non NULL, M and OFF are
476 /* check if we can safely examine src and dst ports */
477 struct sctp_inpcb *inp = NULL;
478 struct sctp_tcb *stcb = NULL;
479 struct sctp_nets *net = NULL;
480 struct sockaddr_in6 final;
482 if (ip6cp->ip6c_m == NULL)
485 bzero(&sh, sizeof(sh));
486 bzero(&final, sizeof(final));
489 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
491 ip6cp->ip6c_src->sin6_port = sh.src_port;
492 final.sin6_len = sizeof(final);
493 final.sin6_family = AF_INET6;
494 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
495 final.sin6_port = sh.dest_port;
496 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
497 (struct sockaddr *)&final,
498 &inp, &net, 1, vrf_id);
499 /* inp's ref-count increased && stcb locked */
500 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
501 if (cmd == PRC_MSGSIZE) {
502 sctp6_notify_mbuf(inp,
507 /* inp's ref-count reduced && stcb unlocked */
509 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
510 (struct sockaddr *)&final,
512 /* inp's ref-count reduced && stcb unlocked */
515 if (PRC_IS_REDIRECT(cmd) && inp) {
516 in6_rtchange((struct in6pcb *)inp,
517 inet6ctlerrmap[cmd]);
520 /* reduce inp's ref-count */
522 SCTP_INP_DECR_REF(inp);
523 SCTP_INP_WUNLOCK(inp);
526 SCTP_TCB_UNLOCK(stcb);
532 * this routine can probably be collasped into the one in sctp_userreq.c
533 * since they do the same thing and now we lookup with a sockaddr
536 sctp6_getcred(SYSCTL_HANDLER_ARGS)
539 struct sockaddr_in6 addrs[2];
540 struct sctp_inpcb *inp;
541 struct sctp_nets *net;
542 struct sctp_tcb *stcb;
546 vrf_id = SCTP_DEFAULT_VRFID;
548 error = priv_check(req->td, PRIV_NETINET_GETCRED);
552 if (req->newlen != sizeof(addrs)) {
553 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
556 if (req->oldlen != sizeof(struct ucred)) {
557 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
560 error = SYSCTL_IN(req, addrs, sizeof(addrs));
564 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
566 &inp, &net, 1, vrf_id);
567 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
568 if ((inp != NULL) && (stcb == NULL)) {
569 /* reduce ref-count */
571 SCTP_INP_DECR_REF(inp);
574 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
578 SCTP_TCB_UNLOCK(stcb);
580 * We use the write lock here, only since in the error leg we need
581 * it. If we used RLOCK, then we would have to
582 * wlock/decr/unlock/rlock. Which in theory could create a hole.
583 * Better to use higher wlock.
587 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
589 SCTP_INP_WUNLOCK(inp);
592 cru2x(inp->sctp_socket->so_cred, &xuc);
593 SCTP_INP_WUNLOCK(inp);
594 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
599 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
601 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
604 /* This is the same as the sctp_abort() could be made common */
606 sctp6_abort(struct socket *so)
608 struct sctp_inpcb *inp;
611 inp = (struct sctp_inpcb *)so->so_pcb;
613 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
617 flags = inp->sctp_flags;
618 #ifdef SCTP_LOG_CLOSING
619 sctp_log_closing(inp, NULL, 17);
621 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
622 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
623 #ifdef SCTP_LOG_CLOSING
624 sctp_log_closing(inp, NULL, 16);
626 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
627 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
629 SCTP_SB_CLEAR(so->so_snd);
631 * same for the rcv ones, they are only here for the
634 SCTP_SB_CLEAR(so->so_rcv);
635 /* Now null out the reference, we are completely detached. */
639 flags = inp->sctp_flags;
640 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
641 goto sctp_must_try_again;
648 sctp6_attach(struct socket *so, int proto, struct thread *p)
652 struct sctp_inpcb *inp;
653 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
655 inp = (struct sctp_inpcb *)so->so_pcb;
657 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
660 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
661 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
665 error = sctp_inpcb_alloc(so, vrf_id);
668 inp = (struct sctp_inpcb *)so->so_pcb;
670 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
671 inp6 = (struct in6pcb *)inp;
673 inp6->inp_vflag |= INP_IPV6;
674 inp6->in6p_hops = -1; /* use kernel default */
675 inp6->in6p_cksum = -1; /* just to be sure */
678 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
679 * socket as well, because the socket may be bound to an IPv6
680 * wildcard address, which may match an IPv4-mapped IPv6 address.
682 inp6->inp_ip_ttl = MODULE_GLOBAL(MOD_INET, ip_defttl);
685 * Hmm what about the IPSEC stuff that is missing here but in
688 SCTP_INP_WUNLOCK(inp);
693 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
695 struct sctp_inpcb *inp;
699 inp = (struct sctp_inpcb *)so->so_pcb;
701 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
705 if ((addr->sa_family == AF_INET6) &&
706 (addr->sa_len != sizeof(struct sockaddr_in6))) {
707 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
710 if ((addr->sa_family == AF_INET) &&
711 (addr->sa_len != sizeof(struct sockaddr_in))) {
712 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
716 inp6 = (struct in6pcb *)inp;
717 inp6->inp_vflag &= ~INP_IPV4;
718 inp6->inp_vflag |= INP_IPV6;
719 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
720 if (addr->sa_family == AF_INET) {
721 /* binding v4 addr to v6 socket, so reset flags */
722 inp6->inp_vflag |= INP_IPV4;
723 inp6->inp_vflag &= ~INP_IPV6;
725 struct sockaddr_in6 *sin6_p;
727 sin6_p = (struct sockaddr_in6 *)addr;
729 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
730 inp6->inp_vflag |= INP_IPV4;
731 } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
732 struct sockaddr_in sin;
734 in6_sin6_2_sin(&sin, sin6_p);
735 inp6->inp_vflag |= INP_IPV4;
736 inp6->inp_vflag &= ~INP_IPV6;
737 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
741 } else if (addr != NULL) {
742 /* IPV6_V6ONLY socket */
743 if (addr->sa_family == AF_INET) {
744 /* can't bind v4 addr to v6 only socket! */
745 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
748 struct sockaddr_in6 *sin6_p;
750 sin6_p = (struct sockaddr_in6 *)addr;
752 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
753 /* can't bind v4-mapped addrs either! */
754 /* NOTE: we don't support SIIT */
755 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
760 error = sctp_inpcb_bind(so, addr, NULL, p);
766 sctp6_close(struct socket *so)
771 /* This could be made common with sctp_detach() since they are identical */
775 sctp6_disconnect(struct socket *so)
777 return (sctp_disconnect(so));
782 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
783 struct mbuf *control, struct thread *p);
787 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
788 struct mbuf *control, struct thread *p)
790 struct sctp_inpcb *inp;
791 struct inpcb *in_inp;
795 struct sockaddr_in6 *sin6;
798 /* No SPL needed since sctp_output does this */
800 inp = (struct sctp_inpcb *)so->so_pcb;
803 SCTP_RELEASE_PKT(control);
807 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
810 in_inp = (struct inpcb *)inp;
811 inp6 = (struct in6pcb *)inp;
813 * For the TCP model we may get a NULL addr, if we are a connected
816 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
823 SCTP_RELEASE_PKT(control);
826 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
827 return (EDESTADDRREQ);
830 sin6 = (struct sockaddr_in6 *)addr;
831 if (SCTP_IPV6_V6ONLY(inp6)) {
833 * if IPV6_V6ONLY flag, we discard datagrams destined to a
834 * v4 addr or v4-mapped addr
836 if (addr->sa_family == AF_INET) {
837 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
840 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
845 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
846 if (!MODULE_GLOBAL(MOD_INET6, ip6_v6only)) {
847 struct sockaddr_in sin;
849 /* convert v4-mapped into v4 addr and send */
850 in6_sin6_2_sin(&sin, sin6);
851 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
854 /* mapped addresses aren't enabled */
855 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
861 /* now what about control */
864 SCTP_PRINTF("huh? control set?\n");
865 SCTP_RELEASE_PKT(inp->control);
868 inp->control = control;
872 SCTP_BUF_NEXT(inp->pkt_last) = m;
875 inp->pkt_last = inp->pkt = m;
878 /* FreeBSD and MacOSX uses a flag passed */
879 ((flags & PRUS_MORETOCOME) == 0)
882 * note with the current version this code will only be used
883 * by OpenBSD, NetBSD and FreeBSD have methods for
884 * re-defining sosend() to use sctp_sosend(). One can
885 * optionaly switch back to this code (by changing back the
886 * defininitions but this is not advisable.
890 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
900 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
904 struct sctp_inpcb *inp;
906 struct sctp_tcb *stcb;
909 struct sockaddr_in6 *sin6;
910 struct sockaddr_storage ss;
914 inp6 = (struct in6pcb *)so->so_pcb;
915 inp = (struct sctp_inpcb *)so->so_pcb;
917 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
918 return (ECONNRESET); /* I made the same as TCP since we are
922 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
925 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
926 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
929 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
930 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
933 vrf_id = inp->def_vrf_id;
934 SCTP_ASOC_CREATE_LOCK(inp);
936 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
937 SCTP_PCB_FLAGS_UNBOUND) {
938 /* Bind a ephemeral port */
939 SCTP_INP_RUNLOCK(inp);
940 error = sctp6_bind(so, NULL, p);
942 SCTP_ASOC_CREATE_UNLOCK(inp);
948 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
949 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
950 /* We are already connected AND the TCP model */
951 SCTP_INP_RUNLOCK(inp);
952 SCTP_ASOC_CREATE_UNLOCK(inp);
953 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
957 sin6 = (struct sockaddr_in6 *)addr;
958 if (SCTP_IPV6_V6ONLY(inp6)) {
960 * if IPV6_V6ONLY flag, ignore connections destined to a v4
961 * addr or v4-mapped addr
963 if (addr->sa_family == AF_INET) {
964 SCTP_INP_RUNLOCK(inp);
965 SCTP_ASOC_CREATE_UNLOCK(inp);
966 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
969 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
970 SCTP_INP_RUNLOCK(inp);
971 SCTP_ASOC_CREATE_UNLOCK(inp);
972 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
976 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
977 if (!MODULE_GLOBAL(MOD_INET6, ip6_v6only)) {
978 /* convert v4-mapped into v4 addr */
979 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
980 addr = (struct sockaddr *)&ss;
982 /* mapped addresses aren't enabled */
983 SCTP_INP_RUNLOCK(inp);
984 SCTP_ASOC_CREATE_UNLOCK(inp);
985 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
990 addr = addr; /* for true v6 address case */
992 /* Now do we connect? */
993 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
994 stcb = LIST_FIRST(&inp->sctp_asoc_list);
996 SCTP_TCB_UNLOCK(stcb);
998 SCTP_INP_RUNLOCK(inp);
1000 SCTP_INP_RUNLOCK(inp);
1001 SCTP_INP_WLOCK(inp);
1002 SCTP_INP_INCR_REF(inp);
1003 SCTP_INP_WUNLOCK(inp);
1004 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1006 SCTP_INP_WLOCK(inp);
1007 SCTP_INP_DECR_REF(inp);
1008 SCTP_INP_WUNLOCK(inp);
1013 /* Already have or am bring up an association */
1014 SCTP_ASOC_CREATE_UNLOCK(inp);
1015 SCTP_TCB_UNLOCK(stcb);
1016 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1019 /* We are GOOD to go */
1020 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
1021 SCTP_ASOC_CREATE_UNLOCK(inp);
1023 /* Gak! no memory */
1026 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1027 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1028 /* Set the connected flag so we can queue data */
1031 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1032 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1034 /* initialize authentication parameters for the assoc */
1035 sctp_initialize_auth_params(inp, stcb);
1037 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1038 SCTP_TCB_UNLOCK(stcb);
1043 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1045 struct sockaddr_in6 *sin6;
1046 struct sctp_inpcb *inp;
1048 struct sctp_ifa *sctp_ifa;
1053 * Do the malloc first in case it blocks.
1055 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1056 sin6->sin6_family = AF_INET6;
1057 sin6->sin6_len = sizeof(*sin6);
1059 inp = (struct sctp_inpcb *)so->so_pcb;
1061 SCTP_FREE_SONAME(sin6);
1062 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1065 SCTP_INP_RLOCK(inp);
1066 sin6->sin6_port = inp->sctp_lport;
1067 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1068 /* For the bound all case you get back 0 */
1069 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1070 struct sctp_tcb *stcb;
1071 struct sockaddr_in6 *sin_a6;
1072 struct sctp_nets *net;
1075 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1081 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1082 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1084 /* this will make coverity happy */
1087 if (sin_a6->sin6_family == AF_INET6) {
1092 if ((!fnd) || (sin_a6 == NULL)) {
1096 vrf_id = inp->def_vrf_id;
1097 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1099 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1102 /* For the bound all case you get back 0 */
1104 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1107 /* Take the first IPv6 address in the list */
1108 struct sctp_laddr *laddr;
1111 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1112 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1113 struct sockaddr_in6 *sin_a;
1115 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1116 sin6->sin6_addr = sin_a->sin6_addr;
1122 SCTP_FREE_SONAME(sin6);
1123 SCTP_INP_RUNLOCK(inp);
1124 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1128 SCTP_INP_RUNLOCK(inp);
1129 /* Scoping things for v6 */
1130 if ((error = sa6_recoverscope(sin6)) != 0) {
1131 SCTP_FREE_SONAME(sin6);
1134 (*addr) = (struct sockaddr *)sin6;
1139 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1141 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1143 struct sockaddr_in6 *sin_a6;
1144 struct sctp_inpcb *inp;
1145 struct sctp_tcb *stcb;
1146 struct sctp_nets *net;
1151 * Do the malloc first in case it blocks.
1153 inp = (struct sctp_inpcb *)so->so_pcb;
1154 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1155 /* UDP type and listeners will drop out here */
1156 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1159 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1160 sin6->sin6_family = AF_INET6;
1161 sin6->sin6_len = sizeof(*sin6);
1163 /* We must recapture incase we blocked */
1164 inp = (struct sctp_inpcb *)so->so_pcb;
1166 SCTP_FREE_SONAME(sin6);
1167 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1170 SCTP_INP_RLOCK(inp);
1171 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1173 SCTP_TCB_LOCK(stcb);
1175 SCTP_INP_RUNLOCK(inp);
1177 SCTP_FREE_SONAME(sin6);
1178 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1182 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1183 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1184 if (sin_a6->sin6_family == AF_INET6) {
1186 sin6->sin6_port = stcb->rport;
1187 sin6->sin6_addr = sin_a6->sin6_addr;
1191 SCTP_TCB_UNLOCK(stcb);
1193 /* No IPv4 address */
1194 SCTP_FREE_SONAME(sin6);
1195 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1198 if ((error = sa6_recoverscope(sin6)) != 0)
1200 *addr = (struct sockaddr *)sin6;
1205 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1207 struct sockaddr *addr;
1208 struct in6pcb *inp6 = sotoin6pcb(so);
1212 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1215 /* allow v6 addresses precedence */
1216 error = sctp6_getaddr(so, nam);
1218 /* try v4 next if v6 failed */
1219 error = sctp_ingetaddr(so, nam);
1224 /* if I'm V6ONLY, convert it to v4-mapped */
1225 if (SCTP_IPV6_V6ONLY(inp6)) {
1226 struct sockaddr_in6 sin6;
1228 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1229 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1238 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1240 struct sockaddr *addr = *nam;
1241 struct in6pcb *inp6 = sotoin6pcb(so);
1245 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1248 /* allow v6 addresses precedence */
1249 error = sctp6_peeraddr(so, nam);
1251 /* try v4 next if v6 failed */
1252 error = sctp_peeraddr(so, nam);
1256 /* if I'm V6ONLY, convert it to v4-mapped */
1257 if (SCTP_IPV6_V6ONLY(inp6)) {
1258 struct sockaddr_in6 sin6;
1260 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1261 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1267 struct pr_usrreqs sctp6_usrreqs = {
1268 .pru_abort = sctp6_abort,
1269 .pru_accept = sctp_accept,
1270 .pru_attach = sctp6_attach,
1271 .pru_bind = sctp6_bind,
1272 .pru_connect = sctp6_connect,
1273 .pru_control = in6_control,
1274 .pru_close = sctp6_close,
1275 .pru_detach = sctp6_close,
1276 .pru_sopoll = sopoll_generic,
1277 .pru_flush = sctp_flush,
1278 .pru_disconnect = sctp6_disconnect,
1279 .pru_listen = sctp_listen,
1280 .pru_peeraddr = sctp6_getpeeraddr,
1281 .pru_send = sctp6_send,
1282 .pru_shutdown = sctp_shutdown,
1283 .pru_sockaddr = sctp6_in6getaddr,
1284 .pru_sosend = sctp_sosend,
1285 .pru_soreceive = sctp_soreceive