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)
395 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
396 (sh == NULL) || (to == NULL)) {
398 SCTP_TCB_UNLOCK(stcb);
401 /* First job is to verify the vtag matches what I would send */
402 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
403 SCTP_TCB_UNLOCK(stcb);
406 if (icmph->icmp6_type != ICMP_UNREACH) {
407 /* We only care about unreachable */
408 SCTP_TCB_UNLOCK(stcb);
411 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
412 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
413 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
414 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
415 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
416 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
417 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
418 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
421 * Hmm reachablity problems we must examine closely. If its
422 * not reachable, we may have lost a network. Or if there is
423 * NO protocol at the other end named SCTP. well we consider
426 if (net->dest_state & SCTP_ADDR_REACHABLE) {
427 /* Ok that destination is NOT reachable */
428 net->dest_state &= ~SCTP_ADDR_REACHABLE;
429 net->dest_state &= ~SCTP_ADDR_PF;
430 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
431 stcb, SCTP_FAILED_THRESHOLD,
432 (void *)net, SCTP_SO_NOT_LOCKED);
434 SCTP_TCB_UNLOCK(stcb);
435 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
436 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
438 * Here the peer is either playing tricks on us, including
439 * an address that belongs to someone who does not support
440 * SCTP OR was a userland implementation that shutdown and
441 * now is dead. In either case treat it like a OOTB abort
444 reason = SCTP_PEER_FAULTY;
445 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
446 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
447 so = SCTP_INP_SO(inp);
448 atomic_add_int(&stcb->asoc.refcnt, 1);
449 SCTP_TCB_UNLOCK(stcb);
450 SCTP_SOCKET_LOCK(so, 1);
452 atomic_subtract_int(&stcb->asoc.refcnt, 1);
454 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
455 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
456 SCTP_SOCKET_UNLOCK(so, 1);
457 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
459 /* no need to unlock here, since the TCB is gone */
461 SCTP_TCB_UNLOCK(stcb);
468 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
471 struct ip6ctlparam *ip6cp = NULL;
474 vrf_id = SCTP_DEFAULT_VRFID;
476 if (pktdst->sa_family != AF_INET6 ||
477 pktdst->sa_len != sizeof(struct sockaddr_in6))
480 if ((unsigned)cmd >= PRC_NCMDS)
482 if (PRC_IS_REDIRECT(cmd)) {
484 } else if (inet6ctlerrmap[cmd] == 0) {
487 /* if the parameter is from icmp6, decode it. */
489 ip6cp = (struct ip6ctlparam *)d;
491 ip6cp = (struct ip6ctlparam *)NULL;
496 * XXX: We assume that when IPV6 is non NULL, M and OFF are
499 /* check if we can safely examine src and dst ports */
500 struct sctp_inpcb *inp = NULL;
501 struct sctp_tcb *stcb = NULL;
502 struct sctp_nets *net = NULL;
503 struct sockaddr_in6 final;
505 if (ip6cp->ip6c_m == NULL)
508 bzero(&sh, sizeof(sh));
509 bzero(&final, sizeof(final));
512 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
514 ip6cp->ip6c_src->sin6_port = sh.src_port;
515 final.sin6_len = sizeof(final);
516 final.sin6_family = AF_INET6;
517 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
518 final.sin6_port = sh.dest_port;
519 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
520 (struct sockaddr *)&final,
521 &inp, &net, 1, vrf_id);
522 /* inp's ref-count increased && stcb locked */
523 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
524 if (cmd == PRC_MSGSIZE) {
525 sctp6_notify_mbuf(inp,
530 /* inp's ref-count reduced && stcb unlocked */
532 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
533 (struct sockaddr *)&final,
535 /* inp's ref-count reduced && stcb unlocked */
538 if (PRC_IS_REDIRECT(cmd) && inp) {
539 in6_rtchange((struct in6pcb *)inp,
540 inet6ctlerrmap[cmd]);
543 /* reduce inp's ref-count */
545 SCTP_INP_DECR_REF(inp);
546 SCTP_INP_WUNLOCK(inp);
549 SCTP_TCB_UNLOCK(stcb);
555 * this routine can probably be collasped into the one in sctp_userreq.c
556 * since they do the same thing and now we lookup with a sockaddr
559 sctp6_getcred(SYSCTL_HANDLER_ARGS)
562 struct sockaddr_in6 addrs[2];
563 struct sctp_inpcb *inp;
564 struct sctp_nets *net;
565 struct sctp_tcb *stcb;
569 vrf_id = SCTP_DEFAULT_VRFID;
571 error = priv_check(req->td, PRIV_NETINET_GETCRED);
575 if (req->newlen != sizeof(addrs)) {
576 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
579 if (req->oldlen != sizeof(struct ucred)) {
580 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
583 error = SYSCTL_IN(req, addrs, sizeof(addrs));
587 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
589 &inp, &net, 1, vrf_id);
590 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
591 if ((inp != NULL) && (stcb == NULL)) {
592 /* reduce ref-count */
594 SCTP_INP_DECR_REF(inp);
597 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
601 SCTP_TCB_UNLOCK(stcb);
603 * We use the write lock here, only since in the error leg we need
604 * it. If we used RLOCK, then we would have to
605 * wlock/decr/unlock/rlock. Which in theory could create a hole.
606 * Better to use higher wlock.
610 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
612 SCTP_INP_WUNLOCK(inp);
615 cru2x(inp->sctp_socket->so_cred, &xuc);
616 SCTP_INP_WUNLOCK(inp);
617 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
622 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
624 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
627 /* This is the same as the sctp_abort() could be made common */
629 sctp6_abort(struct socket *so)
631 struct sctp_inpcb *inp;
634 inp = (struct sctp_inpcb *)so->so_pcb;
636 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
640 flags = inp->sctp_flags;
641 #ifdef SCTP_LOG_CLOSING
642 sctp_log_closing(inp, NULL, 17);
644 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
645 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
646 #ifdef SCTP_LOG_CLOSING
647 sctp_log_closing(inp, NULL, 16);
649 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
650 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
652 SCTP_SB_CLEAR(so->so_snd);
654 * same for the rcv ones, they are only here for the
657 SCTP_SB_CLEAR(so->so_rcv);
658 /* Now null out the reference, we are completely detached. */
662 flags = inp->sctp_flags;
663 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
664 goto sctp_must_try_again;
671 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
675 struct sctp_inpcb *inp;
676 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
678 inp = (struct sctp_inpcb *)so->so_pcb;
680 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
683 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
684 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
688 error = sctp_inpcb_alloc(so, vrf_id);
691 inp = (struct sctp_inpcb *)so->so_pcb;
693 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
694 inp6 = (struct in6pcb *)inp;
696 inp6->inp_vflag |= INP_IPV6;
697 inp6->in6p_hops = -1; /* use kernel default */
698 inp6->in6p_cksum = -1; /* just to be sure */
701 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
702 * socket as well, because the socket may be bound to an IPv6
703 * wildcard address, which may match an IPv4-mapped IPv6 address.
705 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
708 * Hmm what about the IPSEC stuff that is missing here but in
711 SCTP_INP_WUNLOCK(inp);
716 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
718 struct sctp_inpcb *inp;
722 inp = (struct sctp_inpcb *)so->so_pcb;
724 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
728 switch (addr->sa_family) {
731 if (addr->sa_len != sizeof(struct sockaddr_in)) {
732 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
739 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
740 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
746 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
750 inp6 = (struct in6pcb *)inp;
751 inp6->inp_vflag &= ~INP_IPV4;
752 inp6->inp_vflag |= INP_IPV6;
753 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
754 switch (addr->sa_family) {
757 /* binding v4 addr to v6 socket, so reset flags */
758 inp6->inp_vflag |= INP_IPV4;
759 inp6->inp_vflag &= ~INP_IPV6;
765 struct sockaddr_in6 *sin6_p;
767 sin6_p = (struct sockaddr_in6 *)addr;
769 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
770 inp6->inp_vflag |= INP_IPV4;
773 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
774 struct sockaddr_in sin;
776 in6_sin6_2_sin(&sin, sin6_p);
777 inp6->inp_vflag |= INP_IPV4;
778 inp6->inp_vflag &= ~INP_IPV6;
779 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
789 } else if (addr != NULL) {
790 struct sockaddr_in6 *sin6_p;
792 /* IPV6_V6ONLY socket */
794 if (addr->sa_family == AF_INET) {
795 /* can't bind v4 addr to v6 only socket! */
796 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
800 sin6_p = (struct sockaddr_in6 *)addr;
802 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
803 /* can't bind v4-mapped addrs either! */
804 /* NOTE: we don't support SIIT */
805 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
809 error = sctp_inpcb_bind(so, addr, NULL, p);
815 sctp6_close(struct socket *so)
820 /* This could be made common with sctp_detach() since they are identical */
824 sctp6_disconnect(struct socket *so)
826 return (sctp_disconnect(so));
831 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
832 struct mbuf *control, struct thread *p);
836 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
837 struct mbuf *control, struct thread *p)
839 struct sctp_inpcb *inp;
843 struct sockaddr_in6 *sin6;
846 /* No SPL needed since sctp_output does this */
848 inp = (struct sctp_inpcb *)so->so_pcb;
851 SCTP_RELEASE_PKT(control);
855 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
858 inp6 = (struct in6pcb *)inp;
860 * For the TCP model we may get a NULL addr, if we are a connected
863 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
870 SCTP_RELEASE_PKT(control);
873 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
874 return (EDESTADDRREQ);
877 sin6 = (struct sockaddr_in6 *)addr;
878 if (SCTP_IPV6_V6ONLY(inp6)) {
880 * if IPV6_V6ONLY flag, we discard datagrams destined to a
881 * v4 addr or v4-mapped addr
883 if (addr->sa_family == AF_INET) {
884 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
887 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
888 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
892 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
893 if (!MODULE_GLOBAL(ip6_v6only)) {
894 struct sockaddr_in sin;
896 /* convert v4-mapped into v4 addr and send */
897 in6_sin6_2_sin(&sin, sin6);
898 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
901 /* mapped addresses aren't enabled */
902 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
908 /* now what about control */
911 SCTP_PRINTF("huh? control set?\n");
912 SCTP_RELEASE_PKT(inp->control);
915 inp->control = control;
919 SCTP_BUF_NEXT(inp->pkt_last) = m;
922 inp->pkt_last = inp->pkt = m;
925 /* FreeBSD and MacOSX uses a flag passed */
926 ((flags & PRUS_MORETOCOME) == 0)
929 * note with the current version this code will only be used
930 * by OpenBSD, NetBSD and FreeBSD have methods for
931 * re-defining sosend() to use sctp_sosend(). One can
932 * optionaly switch back to this code (by changing back the
933 * defininitions but this is not advisable.
937 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
947 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
951 struct sctp_inpcb *inp;
953 struct sctp_tcb *stcb;
956 struct sockaddr_in6 *sin6;
957 struct sockaddr_storage ss;
961 inp6 = (struct in6pcb *)so->so_pcb;
962 inp = (struct sctp_inpcb *)so->so_pcb;
964 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
965 return (ECONNRESET); /* I made the same as TCP since we are
969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
972 switch (addr->sa_family) {
975 if (addr->sa_len != sizeof(struct sockaddr_in)) {
976 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
983 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
984 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
990 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
994 vrf_id = inp->def_vrf_id;
995 SCTP_ASOC_CREATE_LOCK(inp);
997 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
998 SCTP_PCB_FLAGS_UNBOUND) {
999 /* Bind a ephemeral port */
1000 SCTP_INP_RUNLOCK(inp);
1001 error = sctp6_bind(so, NULL, p);
1003 SCTP_ASOC_CREATE_UNLOCK(inp);
1007 SCTP_INP_RLOCK(inp);
1009 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1010 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1011 /* We are already connected AND the TCP model */
1012 SCTP_INP_RUNLOCK(inp);
1013 SCTP_ASOC_CREATE_UNLOCK(inp);
1014 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1015 return (EADDRINUSE);
1018 sin6 = (struct sockaddr_in6 *)addr;
1019 if (SCTP_IPV6_V6ONLY(inp6)) {
1021 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1022 * addr or v4-mapped addr
1024 if (addr->sa_family == AF_INET) {
1025 SCTP_INP_RUNLOCK(inp);
1026 SCTP_ASOC_CREATE_UNLOCK(inp);
1027 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1030 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1031 SCTP_INP_RUNLOCK(inp);
1032 SCTP_ASOC_CREATE_UNLOCK(inp);
1033 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1037 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1038 if (!MODULE_GLOBAL(ip6_v6only)) {
1039 /* convert v4-mapped into v4 addr */
1040 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1041 addr = (struct sockaddr *)&ss;
1043 /* mapped addresses aren't enabled */
1044 SCTP_INP_RUNLOCK(inp);
1045 SCTP_ASOC_CREATE_UNLOCK(inp);
1046 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1051 /* Now do we connect? */
1052 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1053 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1055 SCTP_TCB_UNLOCK(stcb);
1057 SCTP_INP_RUNLOCK(inp);
1059 SCTP_INP_RUNLOCK(inp);
1060 SCTP_INP_WLOCK(inp);
1061 SCTP_INP_INCR_REF(inp);
1062 SCTP_INP_WUNLOCK(inp);
1063 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1065 SCTP_INP_WLOCK(inp);
1066 SCTP_INP_DECR_REF(inp);
1067 SCTP_INP_WUNLOCK(inp);
1072 /* Already have or am bring up an association */
1073 SCTP_ASOC_CREATE_UNLOCK(inp);
1074 SCTP_TCB_UNLOCK(stcb);
1075 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1078 /* We are GOOD to go */
1079 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1080 SCTP_ASOC_CREATE_UNLOCK(inp);
1082 /* Gak! no memory */
1085 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1086 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1087 /* Set the connected flag so we can queue data */
1090 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1091 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1093 /* initialize authentication parameters for the assoc */
1094 sctp_initialize_auth_params(inp, stcb);
1096 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1097 SCTP_TCB_UNLOCK(stcb);
1102 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1104 struct sockaddr_in6 *sin6;
1105 struct sctp_inpcb *inp;
1107 struct sctp_ifa *sctp_ifa;
1112 * Do the malloc first in case it blocks.
1114 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1117 sin6->sin6_family = AF_INET6;
1118 sin6->sin6_len = sizeof(*sin6);
1120 inp = (struct sctp_inpcb *)so->so_pcb;
1122 SCTP_FREE_SONAME(sin6);
1123 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1124 return (ECONNRESET);
1126 SCTP_INP_RLOCK(inp);
1127 sin6->sin6_port = inp->sctp_lport;
1128 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1129 /* For the bound all case you get back 0 */
1130 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1131 struct sctp_tcb *stcb;
1132 struct sockaddr_in6 *sin_a6;
1133 struct sctp_nets *net;
1136 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1142 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1143 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1145 /* this will make coverity happy */
1148 if (sin_a6->sin6_family == AF_INET6) {
1153 if ((!fnd) || (sin_a6 == NULL)) {
1157 vrf_id = inp->def_vrf_id;
1158 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1160 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1163 /* For the bound all case you get back 0 */
1165 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1168 /* Take the first IPv6 address in the list */
1169 struct sctp_laddr *laddr;
1172 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1173 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1174 struct sockaddr_in6 *sin_a;
1176 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1177 sin6->sin6_addr = sin_a->sin6_addr;
1183 SCTP_FREE_SONAME(sin6);
1184 SCTP_INP_RUNLOCK(inp);
1185 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1189 SCTP_INP_RUNLOCK(inp);
1190 /* Scoping things for v6 */
1191 if ((error = sa6_recoverscope(sin6)) != 0) {
1192 SCTP_FREE_SONAME(sin6);
1195 (*addr) = (struct sockaddr *)sin6;
1200 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1202 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1204 struct sockaddr_in6 *sin_a6;
1205 struct sctp_inpcb *inp;
1206 struct sctp_tcb *stcb;
1207 struct sctp_nets *net;
1210 /* Do the malloc first in case it blocks. */
1211 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1214 sin6->sin6_family = AF_INET6;
1215 sin6->sin6_len = sizeof(*sin6);
1217 inp = (struct sctp_inpcb *)so->so_pcb;
1218 if ((inp == NULL) ||
1219 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1220 /* UDP type and listeners will drop out here */
1221 SCTP_FREE_SONAME(sin6);
1222 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1225 SCTP_INP_RLOCK(inp);
1226 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1228 SCTP_TCB_LOCK(stcb);
1230 SCTP_INP_RUNLOCK(inp);
1232 SCTP_FREE_SONAME(sin6);
1233 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1234 return (ECONNRESET);
1237 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1238 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1239 if (sin_a6->sin6_family == AF_INET6) {
1241 sin6->sin6_port = stcb->rport;
1242 sin6->sin6_addr = sin_a6->sin6_addr;
1246 SCTP_TCB_UNLOCK(stcb);
1248 /* No IPv4 address */
1249 SCTP_FREE_SONAME(sin6);
1250 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1253 if ((error = sa6_recoverscope(sin6)) != 0)
1255 *addr = (struct sockaddr *)sin6;
1260 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1263 struct sockaddr *addr;
1266 struct in6pcb *inp6 = sotoin6pcb(so);
1270 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1273 /* allow v6 addresses precedence */
1274 error = sctp6_getaddr(so, nam);
1277 /* try v4 next if v6 failed */
1278 error = sctp_ingetaddr(so, nam);
1283 /* if I'm V6ONLY, convert it to v4-mapped */
1284 if (SCTP_IPV6_V6ONLY(inp6)) {
1285 struct sockaddr_in6 sin6;
1287 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1288 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1297 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1300 struct sockaddr *addr;
1303 struct in6pcb *inp6 = sotoin6pcb(so);
1307 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1310 /* allow v6 addresses precedence */
1311 error = sctp6_peeraddr(so, nam);
1314 /* try v4 next if v6 failed */
1315 error = sctp_peeraddr(so, nam);
1320 /* if I'm V6ONLY, convert it to v4-mapped */
1321 if (SCTP_IPV6_V6ONLY(inp6)) {
1322 struct sockaddr_in6 sin6;
1324 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1325 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1332 struct pr_usrreqs sctp6_usrreqs = {
1333 .pru_abort = sctp6_abort,
1334 .pru_accept = sctp_accept,
1335 .pru_attach = sctp6_attach,
1336 .pru_bind = sctp6_bind,
1337 .pru_connect = sctp6_connect,
1338 .pru_control = in6_control,
1339 .pru_close = sctp6_close,
1340 .pru_detach = sctp6_close,
1341 .pru_sopoll = sopoll_generic,
1342 .pru_flush = sctp_flush,
1343 .pru_disconnect = sctp6_disconnect,
1344 .pru_listen = sctp_listen,
1345 .pru_peeraddr = sctp6_getpeeraddr,
1346 .pru_send = sctp6_send,
1347 .pru_shutdown = sctp_shutdown,
1348 .pru_sockaddr = sctp6_in6getaddr,
1349 .pru_sosend = sctp_sosend,
1350 .pru_soreceive = sctp_soreceive