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);
113 return (IPPROTO_DONE);
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, offset - sizeof(*ch),
166 sh, ch, &in6p, &net, vrf_id);
167 if ((net) && (port)) {
168 if (net->port == 0) {
169 sctp_pathmtu_adjustment(stcb, 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, offset - sizeof(*ch),
200 sh, ch, &in6p, &net, vrf_id);
201 if ((net) && (port)) {
202 if (net->port == 0) {
203 sctp_pathmtu_adjustment(stcb, 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, sh, vrf_id, port);
236 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
239 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
240 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
241 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
242 (ch->chunk_type != SCTP_INIT))) {
243 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
247 } else if (stcb == NULL) {
252 * Check AH/ESP integrity.
254 in6p_ip = (struct inpcb *)in6p;
255 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
257 MODULE_GLOBAL(ipsec6stat).in_polvio++;
263 * CONTROL chunk processing
265 offset -= sizeof(*ch);
266 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
268 /* Length now holds the total packet length payload + iphlen */
269 length = ntohs(ip6->ip6_plen) + iphlen;
271 /* sa_ignore NO_NULL_CHK */
272 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
273 in6p, stcb, net, ecn_bits, vrf_id, port);
274 /* inp's ref-count reduced && stcb unlocked */
275 /* XXX this stuff below gets moved to appropriate parts later... */
278 if ((in6p) && refcount_up) {
279 /* reduce ref-count */
280 SCTP_INP_WLOCK(in6p);
281 SCTP_INP_DECR_REF(in6p);
282 SCTP_INP_WUNLOCK(in6p);
284 return (IPPROTO_DONE);
288 SCTP_TCB_UNLOCK(stcb);
290 if ((in6p) && refcount_up) {
291 /* reduce ref-count */
292 SCTP_INP_WLOCK(in6p);
293 SCTP_INP_DECR_REF(in6p);
294 SCTP_INP_WUNLOCK(in6p);
298 return (IPPROTO_DONE);
303 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
304 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
308 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
309 (icmp6 == NULL) || (sh == NULL)) {
312 /* First do we even look at it? */
313 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
316 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
317 /* not PACKET TO BIG */
321 * ok we need to look closely. We could even get smarter and look at
322 * anyone that we sent to in case we get a different ICMP that tells
323 * us there is no way to reach a host, but for this impl, all we
324 * care about is MTU discovery.
326 nxtsz = ntohl(icmp6->icmp6_mtu);
327 /* Stop any PMTU timer */
328 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
330 /* Adjust destination size limit */
331 if (net->mtu > nxtsz) {
334 net->mtu -= sizeof(struct udphdr);
337 /* now what about the ep? */
338 if (stcb->asoc.smallest_mtu > nxtsz) {
339 struct sctp_tmit_chunk *chk;
341 /* Adjust that too */
342 stcb->asoc.smallest_mtu = nxtsz;
343 /* now off to subtract IP_DF flag if needed */
345 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
346 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
347 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
350 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
351 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
353 * For this guy we also mark for immediate
354 * resend since we sent to big of chunk
356 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
357 if (chk->sent != SCTP_DATAGRAM_RESEND)
358 stcb->asoc.sent_queue_retran_cnt++;
359 chk->sent = SCTP_DATAGRAM_RESEND;
360 chk->rec.data.doing_fast_retransmit = 0;
362 chk->sent = SCTP_DATAGRAM_RESEND;
363 /* Clear any time so NO RTT is being done */
364 chk->sent_rcv_time.tv_sec = 0;
365 chk->sent_rcv_time.tv_usec = 0;
366 stcb->asoc.total_flight -= chk->send_size;
367 net->flight_size -= chk->send_size;
371 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
374 SCTP_TCB_UNLOCK(stcb);
380 sctp6_notify(struct sctp_inpcb *inp,
381 struct icmp6_hdr *icmph,
384 struct sctp_tcb *stcb,
385 struct sctp_nets *net)
387 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
393 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
394 (sh == NULL) || (to == NULL)) {
396 SCTP_TCB_UNLOCK(stcb);
399 /* First job is to verify the vtag matches what I would send */
400 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
401 SCTP_TCB_UNLOCK(stcb);
404 if (icmph->icmp6_type != ICMP_UNREACH) {
405 /* We only care about unreachable */
406 SCTP_TCB_UNLOCK(stcb);
409 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
410 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
411 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
412 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
413 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
414 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
415 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
416 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
419 * Hmm reachablity problems we must examine closely. If its
420 * not reachable, we may have lost a network. Or if there is
421 * NO protocol at the other end named SCTP. well we consider
424 if (net->dest_state & SCTP_ADDR_REACHABLE) {
425 /* Ok that destination is NOT reachable */
426 net->dest_state &= ~SCTP_ADDR_REACHABLE;
427 net->dest_state &= ~SCTP_ADDR_PF;
428 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
429 stcb, SCTP_FAILED_THRESHOLD,
430 (void *)net, SCTP_SO_NOT_LOCKED);
432 SCTP_TCB_UNLOCK(stcb);
433 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
434 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
436 * Here the peer is either playing tricks on us, including
437 * an address that belongs to someone who does not support
438 * SCTP OR was a userland implementation that shutdown and
439 * now is dead. In either case treat it like a OOTB abort
442 sctp_abort_notification(stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
443 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
444 so = SCTP_INP_SO(inp);
445 atomic_add_int(&stcb->asoc.refcnt, 1);
446 SCTP_TCB_UNLOCK(stcb);
447 SCTP_SOCKET_LOCK(so, 1);
449 atomic_subtract_int(&stcb->asoc.refcnt, 1);
451 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
452 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
453 SCTP_SOCKET_UNLOCK(so, 1);
454 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
456 /* no need to unlock here, since the TCB is gone */
458 SCTP_TCB_UNLOCK(stcb);
465 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
468 struct ip6ctlparam *ip6cp = NULL;
471 vrf_id = SCTP_DEFAULT_VRFID;
473 if (pktdst->sa_family != AF_INET6 ||
474 pktdst->sa_len != sizeof(struct sockaddr_in6))
477 if ((unsigned)cmd >= PRC_NCMDS)
479 if (PRC_IS_REDIRECT(cmd)) {
481 } else if (inet6ctlerrmap[cmd] == 0) {
484 /* if the parameter is from icmp6, decode it. */
486 ip6cp = (struct ip6ctlparam *)d;
488 ip6cp = (struct ip6ctlparam *)NULL;
493 * XXX: We assume that when IPV6 is non NULL, M and OFF are
496 /* check if we can safely examine src and dst ports */
497 struct sctp_inpcb *inp = NULL;
498 struct sctp_tcb *stcb = NULL;
499 struct sctp_nets *net = NULL;
500 struct sockaddr_in6 final;
502 if (ip6cp->ip6c_m == NULL)
505 bzero(&sh, sizeof(sh));
506 bzero(&final, sizeof(final));
509 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
511 ip6cp->ip6c_src->sin6_port = sh.src_port;
512 final.sin6_len = sizeof(final);
513 final.sin6_family = AF_INET6;
514 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
515 final.sin6_port = sh.dest_port;
516 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
517 (struct sockaddr *)&final,
518 &inp, &net, 1, vrf_id);
519 /* inp's ref-count increased && stcb locked */
520 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
521 if (cmd == PRC_MSGSIZE) {
522 sctp6_notify_mbuf(inp,
527 /* inp's ref-count reduced && stcb unlocked */
529 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
530 (struct sockaddr *)&final,
532 /* inp's ref-count reduced && stcb unlocked */
535 if (PRC_IS_REDIRECT(cmd) && inp) {
536 in6_rtchange((struct in6pcb *)inp,
537 inet6ctlerrmap[cmd]);
540 /* reduce inp's ref-count */
542 SCTP_INP_DECR_REF(inp);
543 SCTP_INP_WUNLOCK(inp);
546 SCTP_TCB_UNLOCK(stcb);
552 * this routine can probably be collasped into the one in sctp_userreq.c
553 * since they do the same thing and now we lookup with a sockaddr
556 sctp6_getcred(SYSCTL_HANDLER_ARGS)
559 struct sockaddr_in6 addrs[2];
560 struct sctp_inpcb *inp;
561 struct sctp_nets *net;
562 struct sctp_tcb *stcb;
566 vrf_id = SCTP_DEFAULT_VRFID;
568 error = priv_check(req->td, PRIV_NETINET_GETCRED);
572 if (req->newlen != sizeof(addrs)) {
573 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
576 if (req->oldlen != sizeof(struct ucred)) {
577 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
580 error = SYSCTL_IN(req, addrs, sizeof(addrs));
584 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
586 &inp, &net, 1, vrf_id);
587 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
588 if ((inp != NULL) && (stcb == NULL)) {
589 /* reduce ref-count */
591 SCTP_INP_DECR_REF(inp);
594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
598 SCTP_TCB_UNLOCK(stcb);
600 * We use the write lock here, only since in the error leg we need
601 * it. If we used RLOCK, then we would have to
602 * wlock/decr/unlock/rlock. Which in theory could create a hole.
603 * Better to use higher wlock.
607 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
609 SCTP_INP_WUNLOCK(inp);
612 cru2x(inp->sctp_socket->so_cred, &xuc);
613 SCTP_INP_WUNLOCK(inp);
614 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
619 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
621 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
624 /* This is the same as the sctp_abort() could be made common */
626 sctp6_abort(struct socket *so)
628 struct sctp_inpcb *inp;
631 inp = (struct sctp_inpcb *)so->so_pcb;
633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
637 flags = inp->sctp_flags;
638 #ifdef SCTP_LOG_CLOSING
639 sctp_log_closing(inp, NULL, 17);
641 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
642 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
643 #ifdef SCTP_LOG_CLOSING
644 sctp_log_closing(inp, NULL, 16);
646 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
647 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
649 SCTP_SB_CLEAR(so->so_snd);
651 * same for the rcv ones, they are only here for the
654 SCTP_SB_CLEAR(so->so_rcv);
655 /* Now null out the reference, we are completely detached. */
659 flags = inp->sctp_flags;
660 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
661 goto sctp_must_try_again;
668 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
672 struct sctp_inpcb *inp;
673 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
675 inp = (struct sctp_inpcb *)so->so_pcb;
677 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
680 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
681 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
685 error = sctp_inpcb_alloc(so, vrf_id);
688 inp = (struct sctp_inpcb *)so->so_pcb;
690 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
691 inp6 = (struct in6pcb *)inp;
693 inp6->inp_vflag |= INP_IPV6;
694 inp6->in6p_hops = -1; /* use kernel default */
695 inp6->in6p_cksum = -1; /* just to be sure */
698 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
699 * socket as well, because the socket may be bound to an IPv6
700 * wildcard address, which may match an IPv4-mapped IPv6 address.
702 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
705 * Hmm what about the IPSEC stuff that is missing here but in
708 SCTP_INP_WUNLOCK(inp);
713 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
715 struct sctp_inpcb *inp;
719 inp = (struct sctp_inpcb *)so->so_pcb;
721 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
725 switch (addr->sa_family) {
728 if (addr->sa_len != sizeof(struct sockaddr_in)) {
729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
736 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
737 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
743 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
747 inp6 = (struct in6pcb *)inp;
748 inp6->inp_vflag &= ~INP_IPV4;
749 inp6->inp_vflag |= INP_IPV6;
750 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
751 switch (addr->sa_family) {
754 /* binding v4 addr to v6 socket, so reset flags */
755 inp6->inp_vflag |= INP_IPV4;
756 inp6->inp_vflag &= ~INP_IPV6;
762 struct sockaddr_in6 *sin6_p;
764 sin6_p = (struct sockaddr_in6 *)addr;
766 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
767 inp6->inp_vflag |= INP_IPV4;
770 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
771 struct sockaddr_in sin;
773 in6_sin6_2_sin(&sin, sin6_p);
774 inp6->inp_vflag |= INP_IPV4;
775 inp6->inp_vflag &= ~INP_IPV6;
776 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
786 } else if (addr != NULL) {
787 struct sockaddr_in6 *sin6_p;
789 /* IPV6_V6ONLY socket */
791 if (addr->sa_family == AF_INET) {
792 /* can't bind v4 addr to v6 only socket! */
793 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
797 sin6_p = (struct sockaddr_in6 *)addr;
799 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
800 /* can't bind v4-mapped addrs either! */
801 /* NOTE: we don't support SIIT */
802 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
806 error = sctp_inpcb_bind(so, addr, NULL, p);
812 sctp6_close(struct socket *so)
817 /* This could be made common with sctp_detach() since they are identical */
821 sctp6_disconnect(struct socket *so)
823 return (sctp_disconnect(so));
828 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
829 struct mbuf *control, struct thread *p);
833 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
834 struct mbuf *control, struct thread *p)
836 struct sctp_inpcb *inp;
840 struct sockaddr_in6 *sin6;
843 /* No SPL needed since sctp_output does this */
845 inp = (struct sctp_inpcb *)so->so_pcb;
848 SCTP_RELEASE_PKT(control);
852 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
855 inp6 = (struct in6pcb *)inp;
857 * For the TCP model we may get a NULL addr, if we are a connected
860 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
867 SCTP_RELEASE_PKT(control);
870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
871 return (EDESTADDRREQ);
874 sin6 = (struct sockaddr_in6 *)addr;
875 if (SCTP_IPV6_V6ONLY(inp6)) {
877 * if IPV6_V6ONLY flag, we discard datagrams destined to a
878 * v4 addr or v4-mapped addr
880 if (addr->sa_family == AF_INET) {
881 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
884 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
885 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
889 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
890 if (!MODULE_GLOBAL(ip6_v6only)) {
891 struct sockaddr_in sin;
893 /* convert v4-mapped into v4 addr and send */
894 in6_sin6_2_sin(&sin, sin6);
895 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
898 /* mapped addresses aren't enabled */
899 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
905 /* now what about control */
908 SCTP_PRINTF("huh? control set?\n");
909 SCTP_RELEASE_PKT(inp->control);
912 inp->control = control;
916 SCTP_BUF_NEXT(inp->pkt_last) = m;
919 inp->pkt_last = inp->pkt = m;
922 /* FreeBSD and MacOSX uses a flag passed */
923 ((flags & PRUS_MORETOCOME) == 0)
926 * note with the current version this code will only be used
927 * by OpenBSD, NetBSD and FreeBSD have methods for
928 * re-defining sosend() to use sctp_sosend(). One can
929 * optionaly switch back to this code (by changing back the
930 * defininitions but this is not advisable.
934 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
944 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
948 struct sctp_inpcb *inp;
950 struct sctp_tcb *stcb;
953 struct sockaddr_in6 *sin6;
954 struct sockaddr_storage ss;
958 inp6 = (struct in6pcb *)so->so_pcb;
959 inp = (struct sctp_inpcb *)so->so_pcb;
961 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
962 return (ECONNRESET); /* I made the same as TCP since we are
966 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
969 switch (addr->sa_family) {
972 if (addr->sa_len != sizeof(struct sockaddr_in)) {
973 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
980 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
981 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
987 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
991 vrf_id = inp->def_vrf_id;
992 SCTP_ASOC_CREATE_LOCK(inp);
994 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
995 SCTP_PCB_FLAGS_UNBOUND) {
996 /* Bind a ephemeral port */
997 SCTP_INP_RUNLOCK(inp);
998 error = sctp6_bind(so, NULL, p);
1000 SCTP_ASOC_CREATE_UNLOCK(inp);
1004 SCTP_INP_RLOCK(inp);
1006 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1007 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1008 /* We are already connected AND the TCP model */
1009 SCTP_INP_RUNLOCK(inp);
1010 SCTP_ASOC_CREATE_UNLOCK(inp);
1011 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1012 return (EADDRINUSE);
1015 sin6 = (struct sockaddr_in6 *)addr;
1016 if (SCTP_IPV6_V6ONLY(inp6)) {
1018 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1019 * addr or v4-mapped addr
1021 if (addr->sa_family == AF_INET) {
1022 SCTP_INP_RUNLOCK(inp);
1023 SCTP_ASOC_CREATE_UNLOCK(inp);
1024 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1027 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1028 SCTP_INP_RUNLOCK(inp);
1029 SCTP_ASOC_CREATE_UNLOCK(inp);
1030 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1034 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1035 if (!MODULE_GLOBAL(ip6_v6only)) {
1036 /* convert v4-mapped into v4 addr */
1037 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1038 addr = (struct sockaddr *)&ss;
1040 /* mapped addresses aren't enabled */
1041 SCTP_INP_RUNLOCK(inp);
1042 SCTP_ASOC_CREATE_UNLOCK(inp);
1043 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1048 /* Now do we connect? */
1049 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1050 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1052 SCTP_TCB_UNLOCK(stcb);
1054 SCTP_INP_RUNLOCK(inp);
1056 SCTP_INP_RUNLOCK(inp);
1057 SCTP_INP_WLOCK(inp);
1058 SCTP_INP_INCR_REF(inp);
1059 SCTP_INP_WUNLOCK(inp);
1060 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1062 SCTP_INP_WLOCK(inp);
1063 SCTP_INP_DECR_REF(inp);
1064 SCTP_INP_WUNLOCK(inp);
1069 /* Already have or am bring up an association */
1070 SCTP_ASOC_CREATE_UNLOCK(inp);
1071 SCTP_TCB_UNLOCK(stcb);
1072 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1075 /* We are GOOD to go */
1076 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1077 SCTP_ASOC_CREATE_UNLOCK(inp);
1079 /* Gak! no memory */
1082 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1083 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1084 /* Set the connected flag so we can queue data */
1087 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1088 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1090 /* initialize authentication parameters for the assoc */
1091 sctp_initialize_auth_params(inp, stcb);
1093 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1094 SCTP_TCB_UNLOCK(stcb);
1099 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1101 struct sockaddr_in6 *sin6;
1102 struct sctp_inpcb *inp;
1104 struct sctp_ifa *sctp_ifa;
1109 * Do the malloc first in case it blocks.
1111 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1114 sin6->sin6_family = AF_INET6;
1115 sin6->sin6_len = sizeof(*sin6);
1117 inp = (struct sctp_inpcb *)so->so_pcb;
1119 SCTP_FREE_SONAME(sin6);
1120 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1121 return (ECONNRESET);
1123 SCTP_INP_RLOCK(inp);
1124 sin6->sin6_port = inp->sctp_lport;
1125 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1126 /* For the bound all case you get back 0 */
1127 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1128 struct sctp_tcb *stcb;
1129 struct sockaddr_in6 *sin_a6;
1130 struct sctp_nets *net;
1133 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1139 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1140 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1142 /* this will make coverity happy */
1145 if (sin_a6->sin6_family == AF_INET6) {
1150 if ((!fnd) || (sin_a6 == NULL)) {
1154 vrf_id = inp->def_vrf_id;
1155 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1157 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1160 /* For the bound all case you get back 0 */
1162 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1165 /* Take the first IPv6 address in the list */
1166 struct sctp_laddr *laddr;
1169 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1170 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1171 struct sockaddr_in6 *sin_a;
1173 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1174 sin6->sin6_addr = sin_a->sin6_addr;
1180 SCTP_FREE_SONAME(sin6);
1181 SCTP_INP_RUNLOCK(inp);
1182 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1186 SCTP_INP_RUNLOCK(inp);
1187 /* Scoping things for v6 */
1188 if ((error = sa6_recoverscope(sin6)) != 0) {
1189 SCTP_FREE_SONAME(sin6);
1192 (*addr) = (struct sockaddr *)sin6;
1197 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1199 struct sockaddr_in6 *sin6;
1201 struct sockaddr_in6 *sin_a6;
1202 struct sctp_inpcb *inp;
1203 struct sctp_tcb *stcb;
1204 struct sctp_nets *net;
1207 /* Do the malloc first in case it blocks. */
1208 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1211 sin6->sin6_family = AF_INET6;
1212 sin6->sin6_len = sizeof(*sin6);
1214 inp = (struct sctp_inpcb *)so->so_pcb;
1215 if ((inp == NULL) ||
1216 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1217 /* UDP type and listeners will drop out here */
1218 SCTP_FREE_SONAME(sin6);
1219 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1222 SCTP_INP_RLOCK(inp);
1223 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1225 SCTP_TCB_LOCK(stcb);
1227 SCTP_INP_RUNLOCK(inp);
1229 SCTP_FREE_SONAME(sin6);
1230 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1231 return (ECONNRESET);
1234 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1235 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1236 if (sin_a6->sin6_family == AF_INET6) {
1238 sin6->sin6_port = stcb->rport;
1239 sin6->sin6_addr = sin_a6->sin6_addr;
1243 SCTP_TCB_UNLOCK(stcb);
1245 /* No IPv4 address */
1246 SCTP_FREE_SONAME(sin6);
1247 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1250 if ((error = sa6_recoverscope(sin6)) != 0)
1252 *addr = (struct sockaddr *)sin6;
1257 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1260 struct sockaddr *addr;
1263 struct in6pcb *inp6 = sotoin6pcb(so);
1267 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1270 /* allow v6 addresses precedence */
1271 error = sctp6_getaddr(so, nam);
1274 /* try v4 next if v6 failed */
1275 error = sctp_ingetaddr(so, nam);
1280 /* if I'm V6ONLY, convert it to v4-mapped */
1281 if (SCTP_IPV6_V6ONLY(inp6)) {
1282 struct sockaddr_in6 sin6;
1284 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1285 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1294 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1297 struct sockaddr *addr;
1300 struct in6pcb *inp6 = sotoin6pcb(so);
1304 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1307 /* allow v6 addresses precedence */
1308 error = sctp6_peeraddr(so, nam);
1311 /* try v4 next if v6 failed */
1312 error = sctp_peeraddr(so, nam);
1317 /* if I'm V6ONLY, convert it to v4-mapped */
1318 if (SCTP_IPV6_V6ONLY(inp6)) {
1319 struct sockaddr_in6 sin6;
1321 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1322 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1329 struct pr_usrreqs sctp6_usrreqs = {
1330 .pru_abort = sctp6_abort,
1331 .pru_accept = sctp_accept,
1332 .pru_attach = sctp6_attach,
1333 .pru_bind = sctp6_bind,
1334 .pru_connect = sctp6_connect,
1335 .pru_control = in6_control,
1336 .pru_close = sctp6_close,
1337 .pru_detach = sctp6_close,
1338 .pru_sopoll = sopoll_generic,
1339 .pru_flush = sctp_flush,
1340 .pru_disconnect = sctp6_disconnect,
1341 .pru_listen = sctp_listen,
1342 .pru_peeraddr = sctp6_getpeeraddr,
1343 .pru_send = sctp6_send,
1344 .pru_shutdown = sctp_shutdown,
1345 .pru_sockaddr = sctp6_in6getaddr,
1346 .pru_sosend = sctp_sosend,
1347 .pru_soreceive = sctp_soreceive