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/sctp_crc32.h>
55 #include <netinet/udp.h>
58 #include <netipsec/ipsec.h>
60 #include <netipsec/ipsec6.h>
64 extern struct protosw inetsw[];
67 sctp6_input(struct mbuf **i_pak, int *offp, int proto)
72 struct sctp_inpcb *in6p = NULL;
73 struct sctp_nets *net;
75 uint32_t check, calc_check;
77 struct inpcb *in6p_ip;
78 struct sctp_chunkhdr *ch;
79 int length, offset, iphlen;
81 struct sctp_tcb *stcb = NULL;
86 /* get the VRF and table id's */
87 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
88 SCTP_RELEASE_PKT(*i_pak);
91 m = SCTP_HEADER_TO_CHAIN(*i_pak);
92 pkt_len = SCTP_HEADER_LEN((*i_pak));
94 #ifdef SCTP_PACKET_LOGGING
95 sctp_packet_log(m, pkt_len);
97 ip6 = mtod(m, struct ip6_hdr *);
98 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
99 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
100 (int)(sizeof(*sh) + sizeof(*ch)));
102 SCTP_STAT_INCR(sctps_hdrops);
105 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
107 offset = iphlen + sizeof(*sh) + sizeof(*ch);
108 SCTPDBG(SCTP_DEBUG_INPUT1,
109 "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
112 #if defined(NFAITH) && NFAITH > 0
114 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
115 /* XXX send icmp6 host/port unreach? */
118 #endif /* NFAITH defined and > 0 */
119 SCTP_STAT_INCR(sctps_recvpackets);
120 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
121 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
123 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
124 /* No multi-cast support in SCTP */
127 /* destination port of 0 is illegal, based on RFC2960. */
128 if (sh->dest_port == 0)
131 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
132 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
134 if_name(m->m_pkthdr.rcvif),
135 m->m_pkthdr.csum_flags);
136 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
137 SCTP_STAT_INCR(sctps_recvhwcrc);
140 check = sh->checksum; /* save incoming checksum */
141 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
142 (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
143 SCTP_STAT_INCR(sctps_recvnocrc);
146 sh->checksum = 0; /* prepare for calc */
147 calc_check = sctp_calculate_cksum(m, iphlen);
148 SCTP_STAT_INCR(sctps_recvswcrc);
149 if (calc_check != check) {
150 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p phlen:%d\n",
151 calc_check, check, m, iphlen);
152 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
153 sh, ch, &in6p, &net, vrf_id);
154 if ((net) && (port)) {
155 if (net->port == 0) {
156 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
160 /* in6p's ref-count increased && stcb locked */
161 if ((in6p) && (stcb)) {
162 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
163 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
164 } else if ((in6p != NULL) && (stcb == NULL)) {
167 SCTP_STAT_INCR(sctps_badsum);
168 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
171 sh->checksum = calc_check;
176 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
177 * IP/SCTP/first chunk header...
179 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
180 sh, ch, &in6p, &net, vrf_id);
181 if ((net) && (port)) {
182 if (net->port == 0) {
183 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
187 /* in6p's ref-count increased */
189 struct sctp_init_chunk *init_chk, chunk_buf;
191 SCTP_STAT_INCR(sctps_noport);
192 if (ch->chunk_type == SCTP_INITIATION) {
194 * we do a trick here to get the INIT tag, dig in
195 * and get the tag from the INIT and put it in the
198 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
199 iphlen + sizeof(*sh), sizeof(*init_chk),
200 (uint8_t *) & chunk_buf);
202 sh->v_tag = init_chk->init.initiate_tag;
206 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
207 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
210 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
213 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
214 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
216 } else if (stcb == NULL) {
219 in6p_ip = (struct inpcb *)in6p;
222 * Check AH/ESP integrity.
224 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
226 MODULE_GLOBAL(MOD_IPSEC, ipsec6stat).in_polvio++;
232 * CONTROL chunk processing
234 offset -= sizeof(*ch);
235 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
237 /* Length now holds the total packet length payload + iphlen */
238 length = ntohs(ip6->ip6_plen) + iphlen;
240 /* sa_ignore NO_NULL_CHK */
241 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
242 in6p, stcb, net, ecn_bits, vrf_id, port);
243 /* inp's ref-count reduced && stcb unlocked */
244 /* XXX this stuff below gets moved to appropriate parts later... */
247 if ((in6p) && refcount_up) {
248 /* reduce ref-count */
249 SCTP_INP_WLOCK(in6p);
250 SCTP_INP_DECR_REF(in6p);
251 SCTP_INP_WUNLOCK(in6p);
257 SCTP_TCB_UNLOCK(stcb);
259 if ((in6p) && refcount_up) {
260 /* reduce ref-count */
261 SCTP_INP_WLOCK(in6p);
262 SCTP_INP_DECR_REF(in6p);
263 SCTP_INP_WUNLOCK(in6p);
272 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
273 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
277 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
278 (icmp6 == NULL) || (sh == NULL)) {
281 /* First do we even look at it? */
282 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
285 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
286 /* not PACKET TO BIG */
290 * ok we need to look closely. We could even get smarter and look at
291 * anyone that we sent to in case we get a different ICMP that tells
292 * us there is no way to reach a host, but for this impl, all we
293 * care about is MTU discovery.
295 nxtsz = ntohl(icmp6->icmp6_mtu);
296 /* Stop any PMTU timer */
297 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
299 /* Adjust destination size limit */
300 if (net->mtu > nxtsz) {
303 net->mtu -= sizeof(struct udphdr);
306 /* now what about the ep? */
307 if (stcb->asoc.smallest_mtu > nxtsz) {
308 struct sctp_tmit_chunk *chk;
310 /* Adjust that too */
311 stcb->asoc.smallest_mtu = nxtsz;
312 /* now off to subtract IP_DF flag if needed */
314 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
315 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
316 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
319 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
320 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
322 * For this guy we also mark for immediate
323 * resend since we sent to big of chunk
325 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
326 if (chk->sent != SCTP_DATAGRAM_RESEND)
327 stcb->asoc.sent_queue_retran_cnt++;
328 chk->sent = SCTP_DATAGRAM_RESEND;
329 chk->rec.data.doing_fast_retransmit = 0;
331 chk->sent = SCTP_DATAGRAM_RESEND;
332 /* Clear any time so NO RTT is being done */
333 chk->sent_rcv_time.tv_sec = 0;
334 chk->sent_rcv_time.tv_usec = 0;
335 stcb->asoc.total_flight -= chk->send_size;
336 net->flight_size -= chk->send_size;
340 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
343 SCTP_TCB_UNLOCK(stcb);
349 sctp6_notify(struct sctp_inpcb *inp,
350 struct icmp6_hdr *icmph,
353 struct sctp_tcb *stcb,
354 struct sctp_nets *net)
356 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
364 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
365 (sh == NULL) || (to == NULL)) {
367 SCTP_TCB_UNLOCK(stcb);
370 /* First job is to verify the vtag matches what I would send */
371 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
372 SCTP_TCB_UNLOCK(stcb);
375 if (icmph->icmp6_type != ICMP_UNREACH) {
376 /* We only care about unreachable */
377 SCTP_TCB_UNLOCK(stcb);
380 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
381 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
382 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
383 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
384 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
385 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
386 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
387 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
390 * Hmm reachablity problems we must examine closely. If its
391 * not reachable, we may have lost a network. Or if there is
392 * NO protocol at the other end named SCTP. well we consider
395 if (net->dest_state & SCTP_ADDR_REACHABLE) {
396 /* Ok that destination is NOT reachable */
397 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
399 net->failure_threshold,
402 net->dest_state &= ~SCTP_ADDR_REACHABLE;
403 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
405 * JRS 5/14/07 - If a destination is unreachable,
406 * the PF bit is turned off. This allows an
407 * unambiguous use of the PF bit for destinations
408 * that are reachable but potentially failed. If the
409 * destination is set to the unreachable state, also
410 * set the destination to the PF state.
413 * Add debug message here if destination is not in
416 /* Stop any running T3 timers here? */
417 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
418 net->dest_state &= ~SCTP_ADDR_PF;
419 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
422 net->error_count = net->failure_threshold + 1;
423 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
424 stcb, SCTP_FAILED_THRESHOLD,
425 (void *)net, SCTP_SO_NOT_LOCKED);
427 SCTP_TCB_UNLOCK(stcb);
428 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
429 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
431 * Here the peer is either playing tricks on us, including
432 * an address that belongs to someone who does not support
433 * SCTP OR was a userland implementation that shutdown and
434 * now is dead. In either case treat it like a OOTB abort
437 reason = SCTP_PEER_FAULTY;
438 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
439 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
440 so = SCTP_INP_SO(inp);
441 atomic_add_int(&stcb->asoc.refcnt, 1);
442 SCTP_TCB_UNLOCK(stcb);
443 SCTP_SOCKET_LOCK(so, 1);
445 atomic_subtract_int(&stcb->asoc.refcnt, 1);
447 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
448 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
449 SCTP_SOCKET_UNLOCK(so, 1);
450 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
452 /* no need to unlock here, since the TCB is gone */
454 SCTP_TCB_UNLOCK(stcb);
461 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
464 struct ip6ctlparam *ip6cp = NULL;
467 vrf_id = SCTP_DEFAULT_VRFID;
469 if (pktdst->sa_family != AF_INET6 ||
470 pktdst->sa_len != sizeof(struct sockaddr_in6))
473 if ((unsigned)cmd >= PRC_NCMDS)
475 if (PRC_IS_REDIRECT(cmd)) {
477 } else if (inet6ctlerrmap[cmd] == 0) {
480 /* if the parameter is from icmp6, decode it. */
482 ip6cp = (struct ip6ctlparam *)d;
484 ip6cp = (struct ip6ctlparam *)NULL;
489 * XXX: We assume that when IPV6 is non NULL, M and OFF are
492 /* check if we can safely examine src and dst ports */
493 struct sctp_inpcb *inp = NULL;
494 struct sctp_tcb *stcb = NULL;
495 struct sctp_nets *net = NULL;
496 struct sockaddr_in6 final;
498 if (ip6cp->ip6c_m == NULL)
501 bzero(&sh, sizeof(sh));
502 bzero(&final, sizeof(final));
505 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
507 ip6cp->ip6c_src->sin6_port = sh.src_port;
508 final.sin6_len = sizeof(final);
509 final.sin6_family = AF_INET6;
510 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
511 final.sin6_port = sh.dest_port;
512 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
513 (struct sockaddr *)&final,
514 &inp, &net, 1, vrf_id);
515 /* inp's ref-count increased && stcb locked */
516 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
517 if (cmd == PRC_MSGSIZE) {
518 sctp6_notify_mbuf(inp,
523 /* inp's ref-count reduced && stcb unlocked */
525 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
526 (struct sockaddr *)&final,
528 /* inp's ref-count reduced && stcb unlocked */
531 if (PRC_IS_REDIRECT(cmd) && inp) {
532 in6_rtchange((struct in6pcb *)inp,
533 inet6ctlerrmap[cmd]);
536 /* reduce inp's ref-count */
538 SCTP_INP_DECR_REF(inp);
539 SCTP_INP_WUNLOCK(inp);
542 SCTP_TCB_UNLOCK(stcb);
548 * this routine can probably be collasped into the one in sctp_userreq.c
549 * since they do the same thing and now we lookup with a sockaddr
552 sctp6_getcred(SYSCTL_HANDLER_ARGS)
555 struct sockaddr_in6 addrs[2];
556 struct sctp_inpcb *inp;
557 struct sctp_nets *net;
558 struct sctp_tcb *stcb;
562 vrf_id = SCTP_DEFAULT_VRFID;
564 error = priv_check(req->td, PRIV_NETINET_GETCRED);
568 if (req->newlen != sizeof(addrs)) {
569 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
572 if (req->oldlen != sizeof(struct ucred)) {
573 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
576 error = SYSCTL_IN(req, addrs, sizeof(addrs));
580 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
582 &inp, &net, 1, vrf_id);
583 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
584 if ((inp != NULL) && (stcb == NULL)) {
585 /* reduce ref-count */
587 SCTP_INP_DECR_REF(inp);
590 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
594 SCTP_TCB_UNLOCK(stcb);
596 * We use the write lock here, only since in the error leg we need
597 * it. If we used RLOCK, then we would have to
598 * wlock/decr/unlock/rlock. Which in theory could create a hole.
599 * Better to use higher wlock.
603 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
605 SCTP_INP_WUNLOCK(inp);
608 cru2x(inp->sctp_socket->so_cred, &xuc);
609 SCTP_INP_WUNLOCK(inp);
610 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
615 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
617 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
620 /* This is the same as the sctp_abort() could be made common */
622 sctp6_abort(struct socket *so)
624 struct sctp_inpcb *inp;
627 inp = (struct sctp_inpcb *)so->so_pcb;
629 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
633 flags = inp->sctp_flags;
634 #ifdef SCTP_LOG_CLOSING
635 sctp_log_closing(inp, NULL, 17);
637 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
638 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
639 #ifdef SCTP_LOG_CLOSING
640 sctp_log_closing(inp, NULL, 16);
642 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
643 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
645 SCTP_SB_CLEAR(so->so_snd);
647 * same for the rcv ones, they are only here for the
650 SCTP_SB_CLEAR(so->so_rcv);
651 /* Now null out the reference, we are completely detached. */
655 flags = inp->sctp_flags;
656 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
657 goto sctp_must_try_again;
664 sctp6_attach(struct socket *so, int proto, struct thread *p)
668 struct sctp_inpcb *inp;
669 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
671 inp = (struct sctp_inpcb *)so->so_pcb;
673 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
676 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
677 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
681 error = sctp_inpcb_alloc(so, vrf_id);
684 inp = (struct sctp_inpcb *)so->so_pcb;
686 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
687 inp6 = (struct in6pcb *)inp;
689 inp6->inp_vflag |= INP_IPV6;
690 inp6->in6p_hops = -1; /* use kernel default */
691 inp6->in6p_cksum = -1; /* just to be sure */
694 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
695 * socket as well, because the socket may be bound to an IPv6
696 * wildcard address, which may match an IPv4-mapped IPv6 address.
698 inp6->inp_ip_ttl = MODULE_GLOBAL(MOD_INET, ip_defttl);
701 * Hmm what about the IPSEC stuff that is missing here but in
704 SCTP_INP_WUNLOCK(inp);
709 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
711 struct sctp_inpcb *inp;
715 inp = (struct sctp_inpcb *)so->so_pcb;
717 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
721 if ((addr->sa_family == AF_INET6) &&
722 (addr->sa_len != sizeof(struct sockaddr_in6))) {
723 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
726 if ((addr->sa_family == AF_INET) &&
727 (addr->sa_len != sizeof(struct sockaddr_in))) {
728 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
732 inp6 = (struct in6pcb *)inp;
733 inp6->inp_vflag &= ~INP_IPV4;
734 inp6->inp_vflag |= INP_IPV6;
735 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
736 if (addr->sa_family == AF_INET) {
737 /* binding v4 addr to v6 socket, so reset flags */
738 inp6->inp_vflag |= INP_IPV4;
739 inp6->inp_vflag &= ~INP_IPV6;
741 struct sockaddr_in6 *sin6_p;
743 sin6_p = (struct sockaddr_in6 *)addr;
745 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
746 inp6->inp_vflag |= INP_IPV4;
747 } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
748 struct sockaddr_in sin;
750 in6_sin6_2_sin(&sin, sin6_p);
751 inp6->inp_vflag |= INP_IPV4;
752 inp6->inp_vflag &= ~INP_IPV6;
753 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
757 } else if (addr != NULL) {
758 /* IPV6_V6ONLY socket */
759 if (addr->sa_family == AF_INET) {
760 /* can't bind v4 addr to v6 only socket! */
761 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
764 struct sockaddr_in6 *sin6_p;
766 sin6_p = (struct sockaddr_in6 *)addr;
768 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
769 /* can't bind v4-mapped addrs either! */
770 /* NOTE: we don't support SIIT */
771 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
776 error = sctp_inpcb_bind(so, addr, NULL, p);
782 sctp6_close(struct socket *so)
787 /* This could be made common with sctp_detach() since they are identical */
791 sctp6_disconnect(struct socket *so)
793 return (sctp_disconnect(so));
798 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
799 struct mbuf *control, struct thread *p);
803 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
804 struct mbuf *control, struct thread *p)
806 struct sctp_inpcb *inp;
807 struct inpcb *in_inp;
811 struct sockaddr_in6 *sin6;
814 /* No SPL needed since sctp_output does this */
816 inp = (struct sctp_inpcb *)so->so_pcb;
819 SCTP_RELEASE_PKT(control);
823 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
826 in_inp = (struct inpcb *)inp;
827 inp6 = (struct in6pcb *)inp;
829 * For the TCP model we may get a NULL addr, if we are a connected
832 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
839 SCTP_RELEASE_PKT(control);
842 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
843 return (EDESTADDRREQ);
846 sin6 = (struct sockaddr_in6 *)addr;
847 if (SCTP_IPV6_V6ONLY(inp6)) {
849 * if IPV6_V6ONLY flag, we discard datagrams destined to a
850 * v4 addr or v4-mapped addr
852 if (addr->sa_family == AF_INET) {
853 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
856 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
861 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
862 if (!MODULE_GLOBAL(MOD_INET6, ip6_v6only)) {
863 struct sockaddr_in sin;
865 /* convert v4-mapped into v4 addr and send */
866 in6_sin6_2_sin(&sin, sin6);
867 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
870 /* mapped addresses aren't enabled */
871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
877 /* now what about control */
880 SCTP_PRINTF("huh? control set?\n");
881 SCTP_RELEASE_PKT(inp->control);
884 inp->control = control;
888 SCTP_BUF_NEXT(inp->pkt_last) = m;
891 inp->pkt_last = inp->pkt = m;
894 /* FreeBSD and MacOSX uses a flag passed */
895 ((flags & PRUS_MORETOCOME) == 0)
898 * note with the current version this code will only be used
899 * by OpenBSD, NetBSD and FreeBSD have methods for
900 * re-defining sosend() to use sctp_sosend(). One can
901 * optionaly switch back to this code (by changing back the
902 * defininitions but this is not advisable.
906 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
916 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
920 struct sctp_inpcb *inp;
922 struct sctp_tcb *stcb;
925 struct sockaddr_in6 *sin6;
926 struct sockaddr_storage ss;
930 inp6 = (struct in6pcb *)so->so_pcb;
931 inp = (struct sctp_inpcb *)so->so_pcb;
933 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
934 return (ECONNRESET); /* I made the same as TCP since we are
938 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
941 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
942 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
945 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
946 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
949 vrf_id = inp->def_vrf_id;
950 SCTP_ASOC_CREATE_LOCK(inp);
952 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
953 SCTP_PCB_FLAGS_UNBOUND) {
954 /* Bind a ephemeral port */
955 SCTP_INP_RUNLOCK(inp);
956 error = sctp6_bind(so, NULL, p);
958 SCTP_ASOC_CREATE_UNLOCK(inp);
964 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
965 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
966 /* We are already connected AND the TCP model */
967 SCTP_INP_RUNLOCK(inp);
968 SCTP_ASOC_CREATE_UNLOCK(inp);
969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
973 sin6 = (struct sockaddr_in6 *)addr;
974 if (SCTP_IPV6_V6ONLY(inp6)) {
976 * if IPV6_V6ONLY flag, ignore connections destined to a v4
977 * addr or v4-mapped addr
979 if (addr->sa_family == AF_INET) {
980 SCTP_INP_RUNLOCK(inp);
981 SCTP_ASOC_CREATE_UNLOCK(inp);
982 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
985 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
986 SCTP_INP_RUNLOCK(inp);
987 SCTP_ASOC_CREATE_UNLOCK(inp);
988 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
992 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
993 if (!MODULE_GLOBAL(MOD_INET6, ip6_v6only)) {
994 /* convert v4-mapped into v4 addr */
995 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
996 addr = (struct sockaddr *)&ss;
998 /* mapped addresses aren't enabled */
999 SCTP_INP_RUNLOCK(inp);
1000 SCTP_ASOC_CREATE_UNLOCK(inp);
1001 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1006 addr = addr; /* for true v6 address case */
1008 /* Now do we connect? */
1009 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1010 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1012 SCTP_TCB_UNLOCK(stcb);
1014 SCTP_INP_RUNLOCK(inp);
1016 SCTP_INP_RUNLOCK(inp);
1017 SCTP_INP_WLOCK(inp);
1018 SCTP_INP_INCR_REF(inp);
1019 SCTP_INP_WUNLOCK(inp);
1020 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1022 SCTP_INP_WLOCK(inp);
1023 SCTP_INP_DECR_REF(inp);
1024 SCTP_INP_WUNLOCK(inp);
1029 /* Already have or am bring up an association */
1030 SCTP_ASOC_CREATE_UNLOCK(inp);
1031 SCTP_TCB_UNLOCK(stcb);
1032 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1035 /* We are GOOD to go */
1036 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p);
1037 SCTP_ASOC_CREATE_UNLOCK(inp);
1039 /* Gak! no memory */
1042 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1043 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1044 /* Set the connected flag so we can queue data */
1047 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1048 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1050 /* initialize authentication parameters for the assoc */
1051 sctp_initialize_auth_params(inp, stcb);
1053 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1054 SCTP_TCB_UNLOCK(stcb);
1059 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1061 struct sockaddr_in6 *sin6;
1062 struct sctp_inpcb *inp;
1064 struct sctp_ifa *sctp_ifa;
1069 * Do the malloc first in case it blocks.
1071 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1072 sin6->sin6_family = AF_INET6;
1073 sin6->sin6_len = sizeof(*sin6);
1075 inp = (struct sctp_inpcb *)so->so_pcb;
1077 SCTP_FREE_SONAME(sin6);
1078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1081 SCTP_INP_RLOCK(inp);
1082 sin6->sin6_port = inp->sctp_lport;
1083 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1084 /* For the bound all case you get back 0 */
1085 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1086 struct sctp_tcb *stcb;
1087 struct sockaddr_in6 *sin_a6;
1088 struct sctp_nets *net;
1091 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1097 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1098 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1100 /* this will make coverity happy */
1103 if (sin_a6->sin6_family == AF_INET6) {
1108 if ((!fnd) || (sin_a6 == NULL)) {
1112 vrf_id = inp->def_vrf_id;
1113 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1115 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1118 /* For the bound all case you get back 0 */
1120 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1123 /* Take the first IPv6 address in the list */
1124 struct sctp_laddr *laddr;
1127 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1128 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1129 struct sockaddr_in6 *sin_a;
1131 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1132 sin6->sin6_addr = sin_a->sin6_addr;
1138 SCTP_FREE_SONAME(sin6);
1139 SCTP_INP_RUNLOCK(inp);
1140 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1144 SCTP_INP_RUNLOCK(inp);
1145 /* Scoping things for v6 */
1146 if ((error = sa6_recoverscope(sin6)) != 0) {
1147 SCTP_FREE_SONAME(sin6);
1150 (*addr) = (struct sockaddr *)sin6;
1155 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1157 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1159 struct sockaddr_in6 *sin_a6;
1160 struct sctp_inpcb *inp;
1161 struct sctp_tcb *stcb;
1162 struct sctp_nets *net;
1167 * Do the malloc first in case it blocks.
1169 inp = (struct sctp_inpcb *)so->so_pcb;
1170 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1171 /* UDP type and listeners will drop out here */
1172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1175 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1176 sin6->sin6_family = AF_INET6;
1177 sin6->sin6_len = sizeof(*sin6);
1179 /* We must recapture incase we blocked */
1180 inp = (struct sctp_inpcb *)so->so_pcb;
1182 SCTP_FREE_SONAME(sin6);
1183 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1186 SCTP_INP_RLOCK(inp);
1187 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1189 SCTP_TCB_LOCK(stcb);
1191 SCTP_INP_RUNLOCK(inp);
1193 SCTP_FREE_SONAME(sin6);
1194 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1198 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1199 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1200 if (sin_a6->sin6_family == AF_INET6) {
1202 sin6->sin6_port = stcb->rport;
1203 sin6->sin6_addr = sin_a6->sin6_addr;
1207 SCTP_TCB_UNLOCK(stcb);
1209 /* No IPv4 address */
1210 SCTP_FREE_SONAME(sin6);
1211 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1214 if ((error = sa6_recoverscope(sin6)) != 0)
1216 *addr = (struct sockaddr *)sin6;
1221 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1223 struct sockaddr *addr;
1224 struct in6pcb *inp6 = sotoin6pcb(so);
1228 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1231 /* allow v6 addresses precedence */
1232 error = sctp6_getaddr(so, nam);
1234 /* try v4 next if v6 failed */
1235 error = sctp_ingetaddr(so, nam);
1240 /* if I'm V6ONLY, convert it to v4-mapped */
1241 if (SCTP_IPV6_V6ONLY(inp6)) {
1242 struct sockaddr_in6 sin6;
1244 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1245 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1254 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1256 struct sockaddr *addr = *nam;
1257 struct in6pcb *inp6 = sotoin6pcb(so);
1261 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1264 /* allow v6 addresses precedence */
1265 error = sctp6_peeraddr(so, nam);
1267 /* try v4 next if v6 failed */
1268 error = sctp_peeraddr(so, nam);
1272 /* if I'm V6ONLY, convert it to v4-mapped */
1273 if (SCTP_IPV6_V6ONLY(inp6)) {
1274 struct sockaddr_in6 sin6;
1276 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1277 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1283 struct pr_usrreqs sctp6_usrreqs = {
1284 .pru_abort = sctp6_abort,
1285 .pru_accept = sctp_accept,
1286 .pru_attach = sctp6_attach,
1287 .pru_bind = sctp6_bind,
1288 .pru_connect = sctp6_connect,
1289 .pru_control = in6_control,
1290 .pru_close = sctp6_close,
1291 .pru_detach = sctp6_close,
1292 .pru_sopoll = sopoll_generic,
1293 .pru_flush = sctp_flush,
1294 .pru_disconnect = sctp6_disconnect,
1295 .pru_listen = sctp_listen,
1296 .pru_peeraddr = sctp6_getpeeraddr,
1297 .pru_send = sctp6_send,
1298 .pru_shutdown = sctp_shutdown,
1299 .pru_sockaddr = sctp6_in6getaddr,
1300 .pru_sosend = sctp_sosend,
1301 .pru_soreceive = sctp_soreceive