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;
78 struct inpcb *in6p_ip;
81 struct sctp_chunkhdr *ch;
82 int length, offset, iphlen;
84 struct sctp_tcb *stcb = NULL;
87 #if !defined(SCTP_WITH_NO_CSUM)
88 uint32_t check, calc_check;
94 /* get the VRF and table id's */
95 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
96 SCTP_RELEASE_PKT(*i_pak);
99 m = SCTP_HEADER_TO_CHAIN(*i_pak);
100 pkt_len = SCTP_HEADER_LEN((*i_pak));
102 #ifdef SCTP_PACKET_LOGGING
103 sctp_packet_log(m, pkt_len);
105 ip6 = mtod(m, struct ip6_hdr *);
106 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
107 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
108 (int)(sizeof(*sh) + sizeof(*ch)));
110 SCTP_STAT_INCR(sctps_hdrops);
113 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
115 offset = iphlen + sizeof(*sh) + sizeof(*ch);
116 SCTPDBG(SCTP_DEBUG_INPUT1,
117 "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
120 #if defined(NFAITH) && NFAITH > 0
122 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
123 /* XXX send icmp6 host/port unreach? */
126 #endif /* NFAITH defined and > 0 */
127 SCTP_STAT_INCR(sctps_recvpackets);
128 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
129 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
131 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
132 /* No multi-cast support in SCTP */
135 /* destination port of 0 is illegal, based on RFC2960. */
136 if (sh->dest_port == 0)
139 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
140 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
142 if_name(m->m_pkthdr.rcvif),
143 m->m_pkthdr.csum_flags);
144 #if defined(SCTP_WITH_NO_CSUM)
145 SCTP_STAT_INCR(sctps_recvnocrc);
147 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
148 SCTP_STAT_INCR(sctps_recvhwcrc);
151 check = sh->checksum; /* save incoming checksum */
152 if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) &&
153 (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) {
154 SCTP_STAT_INCR(sctps_recvnocrc);
157 sh->checksum = 0; /* prepare for calc */
158 calc_check = sctp_calculate_cksum(m, iphlen);
159 SCTP_STAT_INCR(sctps_recvswcrc);
160 if (calc_check != check) {
161 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p phlen:%d\n",
162 calc_check, check, m, iphlen);
163 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
164 sh, ch, &in6p, &net, vrf_id);
165 if ((net) && (port)) {
166 if (net->port == 0) {
167 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
171 /* in6p's ref-count increased && stcb locked */
172 if ((in6p) && (stcb)) {
173 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
174 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
175 } else if ((in6p != NULL) && (stcb == NULL)) {
178 SCTP_STAT_INCR(sctps_badsum);
179 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
182 sh->checksum = calc_check;
188 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
189 * IP/SCTP/first chunk header...
191 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
192 sh, ch, &in6p, &net, vrf_id);
193 if ((net) && (port)) {
194 if (net->port == 0) {
195 sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr));
199 /* in6p's ref-count increased */
201 struct sctp_init_chunk *init_chk, chunk_buf;
203 SCTP_STAT_INCR(sctps_noport);
204 if (ch->chunk_type == SCTP_INITIATION) {
206 * we do a trick here to get the INIT tag, dig in
207 * and get the tag from the INIT and put it in the
210 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
211 iphlen + sizeof(*sh), sizeof(*init_chk),
212 (uint8_t *) & chunk_buf);
214 sh->v_tag = init_chk->init.initiate_tag;
218 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
219 sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port);
222 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
225 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
226 sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
228 } else if (stcb == NULL) {
233 * Check AH/ESP integrity.
235 in6p_ip = (struct inpcb *)in6p;
236 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
238 MODULE_GLOBAL(ipsec6stat).in_polvio++;
244 * CONTROL chunk processing
246 offset -= sizeof(*ch);
247 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
249 /* Length now holds the total packet length payload + iphlen */
250 length = ntohs(ip6->ip6_plen) + iphlen;
252 /* sa_ignore NO_NULL_CHK */
253 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
254 in6p, stcb, net, ecn_bits, vrf_id, port);
255 /* inp's ref-count reduced && stcb unlocked */
256 /* XXX this stuff below gets moved to appropriate parts later... */
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);
269 SCTP_TCB_UNLOCK(stcb);
271 if ((in6p) && refcount_up) {
272 /* reduce ref-count */
273 SCTP_INP_WLOCK(in6p);
274 SCTP_INP_DECR_REF(in6p);
275 SCTP_INP_WUNLOCK(in6p);
284 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
285 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
289 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
290 (icmp6 == NULL) || (sh == NULL)) {
293 /* First do we even look at it? */
294 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
297 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
298 /* not PACKET TO BIG */
302 * ok we need to look closely. We could even get smarter and look at
303 * anyone that we sent to in case we get a different ICMP that tells
304 * us there is no way to reach a host, but for this impl, all we
305 * care about is MTU discovery.
307 nxtsz = ntohl(icmp6->icmp6_mtu);
308 /* Stop any PMTU timer */
309 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
311 /* Adjust destination size limit */
312 if (net->mtu > nxtsz) {
315 net->mtu -= sizeof(struct udphdr);
318 /* now what about the ep? */
319 if (stcb->asoc.smallest_mtu > nxtsz) {
320 struct sctp_tmit_chunk *chk;
322 /* Adjust that too */
323 stcb->asoc.smallest_mtu = nxtsz;
324 /* now off to subtract IP_DF flag if needed */
326 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
327 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
328 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
331 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
332 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
334 * For this guy we also mark for immediate
335 * resend since we sent to big of chunk
337 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
338 if (chk->sent != SCTP_DATAGRAM_RESEND)
339 stcb->asoc.sent_queue_retran_cnt++;
340 chk->sent = SCTP_DATAGRAM_RESEND;
341 chk->rec.data.doing_fast_retransmit = 0;
343 chk->sent = SCTP_DATAGRAM_RESEND;
344 /* Clear any time so NO RTT is being done */
345 chk->sent_rcv_time.tv_sec = 0;
346 chk->sent_rcv_time.tv_usec = 0;
347 stcb->asoc.total_flight -= chk->send_size;
348 net->flight_size -= chk->send_size;
352 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
355 SCTP_TCB_UNLOCK(stcb);
361 sctp6_notify(struct sctp_inpcb *inp,
362 struct icmp6_hdr *icmph,
365 struct sctp_tcb *stcb,
366 struct sctp_nets *net)
368 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
376 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
377 (sh == NULL) || (to == NULL)) {
379 SCTP_TCB_UNLOCK(stcb);
382 /* First job is to verify the vtag matches what I would send */
383 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
384 SCTP_TCB_UNLOCK(stcb);
387 if (icmph->icmp6_type != ICMP_UNREACH) {
388 /* We only care about unreachable */
389 SCTP_TCB_UNLOCK(stcb);
392 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
393 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
394 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
395 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
396 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
397 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
398 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
399 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
402 * Hmm reachablity problems we must examine closely. If its
403 * not reachable, we may have lost a network. Or if there is
404 * NO protocol at the other end named SCTP. well we consider
407 if (net->dest_state & SCTP_ADDR_REACHABLE) {
408 /* Ok that destination is NOT reachable */
409 SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n",
411 net->failure_threshold,
414 net->dest_state &= ~SCTP_ADDR_REACHABLE;
415 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
417 * JRS 5/14/07 - If a destination is unreachable,
418 * the PF bit is turned off. This allows an
419 * unambiguous use of the PF bit for destinations
420 * that are reachable but potentially failed. If the
421 * destination is set to the unreachable state, also
422 * set the destination to the PF state.
425 * Add debug message here if destination is not in
428 /* Stop any running T3 timers here? */
429 if ((stcb->asoc.sctp_cmt_on_off == 1) &&
430 (stcb->asoc.sctp_cmt_pf > 0)) {
431 net->dest_state &= ~SCTP_ADDR_PF;
432 SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n",
435 net->error_count = net->failure_threshold + 1;
436 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
437 stcb, SCTP_FAILED_THRESHOLD,
438 (void *)net, SCTP_SO_NOT_LOCKED);
440 SCTP_TCB_UNLOCK(stcb);
441 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
442 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
444 * Here the peer is either playing tricks on us, including
445 * an address that belongs to someone who does not support
446 * SCTP OR was a userland implementation that shutdown and
447 * now is dead. In either case treat it like a OOTB abort
450 reason = SCTP_PEER_FAULTY;
451 sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED);
452 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
453 so = SCTP_INP_SO(inp);
454 atomic_add_int(&stcb->asoc.refcnt, 1);
455 SCTP_TCB_UNLOCK(stcb);
456 SCTP_SOCKET_LOCK(so, 1);
458 atomic_subtract_int(&stcb->asoc.refcnt, 1);
460 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
461 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
462 SCTP_SOCKET_UNLOCK(so, 1);
463 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
465 /* no need to unlock here, since the TCB is gone */
467 SCTP_TCB_UNLOCK(stcb);
474 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
477 struct ip6ctlparam *ip6cp = NULL;
480 vrf_id = SCTP_DEFAULT_VRFID;
482 if (pktdst->sa_family != AF_INET6 ||
483 pktdst->sa_len != sizeof(struct sockaddr_in6))
486 if ((unsigned)cmd >= PRC_NCMDS)
488 if (PRC_IS_REDIRECT(cmd)) {
490 } else if (inet6ctlerrmap[cmd] == 0) {
493 /* if the parameter is from icmp6, decode it. */
495 ip6cp = (struct ip6ctlparam *)d;
497 ip6cp = (struct ip6ctlparam *)NULL;
502 * XXX: We assume that when IPV6 is non NULL, M and OFF are
505 /* check if we can safely examine src and dst ports */
506 struct sctp_inpcb *inp = NULL;
507 struct sctp_tcb *stcb = NULL;
508 struct sctp_nets *net = NULL;
509 struct sockaddr_in6 final;
511 if (ip6cp->ip6c_m == NULL)
514 bzero(&sh, sizeof(sh));
515 bzero(&final, sizeof(final));
518 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
520 ip6cp->ip6c_src->sin6_port = sh.src_port;
521 final.sin6_len = sizeof(final);
522 final.sin6_family = AF_INET6;
523 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
524 final.sin6_port = sh.dest_port;
525 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
526 (struct sockaddr *)&final,
527 &inp, &net, 1, vrf_id);
528 /* inp's ref-count increased && stcb locked */
529 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
530 if (cmd == PRC_MSGSIZE) {
531 sctp6_notify_mbuf(inp,
536 /* inp's ref-count reduced && stcb unlocked */
538 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
539 (struct sockaddr *)&final,
541 /* inp's ref-count reduced && stcb unlocked */
544 if (PRC_IS_REDIRECT(cmd) && inp) {
545 in6_rtchange((struct in6pcb *)inp,
546 inet6ctlerrmap[cmd]);
549 /* reduce inp's ref-count */
551 SCTP_INP_DECR_REF(inp);
552 SCTP_INP_WUNLOCK(inp);
555 SCTP_TCB_UNLOCK(stcb);
561 * this routine can probably be collasped into the one in sctp_userreq.c
562 * since they do the same thing and now we lookup with a sockaddr
565 sctp6_getcred(SYSCTL_HANDLER_ARGS)
568 struct sockaddr_in6 addrs[2];
569 struct sctp_inpcb *inp;
570 struct sctp_nets *net;
571 struct sctp_tcb *stcb;
575 vrf_id = SCTP_DEFAULT_VRFID;
577 error = priv_check(req->td, PRIV_NETINET_GETCRED);
581 if (req->newlen != sizeof(addrs)) {
582 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
585 if (req->oldlen != sizeof(struct ucred)) {
586 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
589 error = SYSCTL_IN(req, addrs, sizeof(addrs));
593 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
595 &inp, &net, 1, vrf_id);
596 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
597 if ((inp != NULL) && (stcb == NULL)) {
598 /* reduce ref-count */
600 SCTP_INP_DECR_REF(inp);
603 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
607 SCTP_TCB_UNLOCK(stcb);
609 * We use the write lock here, only since in the error leg we need
610 * it. If we used RLOCK, then we would have to
611 * wlock/decr/unlock/rlock. Which in theory could create a hole.
612 * Better to use higher wlock.
616 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
618 SCTP_INP_WUNLOCK(inp);
621 cru2x(inp->sctp_socket->so_cred, &xuc);
622 SCTP_INP_WUNLOCK(inp);
623 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
628 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
630 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
633 /* This is the same as the sctp_abort() could be made common */
635 sctp6_abort(struct socket *so)
637 struct sctp_inpcb *inp;
640 inp = (struct sctp_inpcb *)so->so_pcb;
642 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
646 flags = inp->sctp_flags;
647 #ifdef SCTP_LOG_CLOSING
648 sctp_log_closing(inp, NULL, 17);
650 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
651 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
652 #ifdef SCTP_LOG_CLOSING
653 sctp_log_closing(inp, NULL, 16);
655 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
656 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
658 SCTP_SB_CLEAR(so->so_snd);
660 * same for the rcv ones, they are only here for the
663 SCTP_SB_CLEAR(so->so_rcv);
664 /* Now null out the reference, we are completely detached. */
668 flags = inp->sctp_flags;
669 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
670 goto sctp_must_try_again;
677 sctp6_attach(struct socket *so, int proto, struct thread *p)
681 struct sctp_inpcb *inp;
682 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
684 inp = (struct sctp_inpcb *)so->so_pcb;
686 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
689 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
690 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
694 error = sctp_inpcb_alloc(so, vrf_id);
697 inp = (struct sctp_inpcb *)so->so_pcb;
699 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
700 inp6 = (struct in6pcb *)inp;
702 inp6->inp_vflag |= INP_IPV6;
703 inp6->in6p_hops = -1; /* use kernel default */
704 inp6->in6p_cksum = -1; /* just to be sure */
707 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
708 * socket as well, because the socket may be bound to an IPv6
709 * wildcard address, which may match an IPv4-mapped IPv6 address.
711 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
714 * Hmm what about the IPSEC stuff that is missing here but in
717 SCTP_INP_WUNLOCK(inp);
722 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
724 struct sctp_inpcb *inp;
728 inp = (struct sctp_inpcb *)so->so_pcb;
730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
734 if ((addr->sa_family == AF_INET6) &&
735 (addr->sa_len != sizeof(struct sockaddr_in6))) {
736 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
739 if ((addr->sa_family == AF_INET) &&
740 (addr->sa_len != sizeof(struct sockaddr_in))) {
741 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
745 inp6 = (struct in6pcb *)inp;
746 inp6->inp_vflag &= ~INP_IPV4;
747 inp6->inp_vflag |= INP_IPV6;
748 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
749 if (addr->sa_family == AF_INET) {
750 /* binding v4 addr to v6 socket, so reset flags */
751 inp6->inp_vflag |= INP_IPV4;
752 inp6->inp_vflag &= ~INP_IPV6;
754 struct sockaddr_in6 *sin6_p;
756 sin6_p = (struct sockaddr_in6 *)addr;
758 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
759 inp6->inp_vflag |= INP_IPV4;
760 } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
761 struct sockaddr_in sin;
763 in6_sin6_2_sin(&sin, sin6_p);
764 inp6->inp_vflag |= INP_IPV4;
765 inp6->inp_vflag &= ~INP_IPV6;
766 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
770 } else if (addr != NULL) {
771 /* IPV6_V6ONLY socket */
772 if (addr->sa_family == AF_INET) {
773 /* can't bind v4 addr to v6 only socket! */
774 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
777 struct sockaddr_in6 *sin6_p;
779 sin6_p = (struct sockaddr_in6 *)addr;
781 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
782 /* can't bind v4-mapped addrs either! */
783 /* NOTE: we don't support SIIT */
784 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
789 error = sctp_inpcb_bind(so, addr, NULL, p);
795 sctp6_close(struct socket *so)
800 /* This could be made common with sctp_detach() since they are identical */
804 sctp6_disconnect(struct socket *so)
806 return (sctp_disconnect(so));
811 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
812 struct mbuf *control, struct thread *p);
816 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
817 struct mbuf *control, struct thread *p)
819 struct sctp_inpcb *inp;
823 struct sockaddr_in6 *sin6;
826 /* No SPL needed since sctp_output does this */
828 inp = (struct sctp_inpcb *)so->so_pcb;
831 SCTP_RELEASE_PKT(control);
835 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
838 inp6 = (struct in6pcb *)inp;
840 * For the TCP model we may get a NULL addr, if we are a connected
843 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
850 SCTP_RELEASE_PKT(control);
853 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
854 return (EDESTADDRREQ);
857 sin6 = (struct sockaddr_in6 *)addr;
858 if (SCTP_IPV6_V6ONLY(inp6)) {
860 * if IPV6_V6ONLY flag, we discard datagrams destined to a
861 * v4 addr or v4-mapped addr
863 if (addr->sa_family == AF_INET) {
864 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
867 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
868 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
872 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
873 if (!MODULE_GLOBAL(ip6_v6only)) {
874 struct sockaddr_in sin;
876 /* convert v4-mapped into v4 addr and send */
877 in6_sin6_2_sin(&sin, sin6);
878 return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
881 /* mapped addresses aren't enabled */
882 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
888 /* now what about control */
891 SCTP_PRINTF("huh? control set?\n");
892 SCTP_RELEASE_PKT(inp->control);
895 inp->control = control;
899 SCTP_BUF_NEXT(inp->pkt_last) = m;
902 inp->pkt_last = inp->pkt = m;
905 /* FreeBSD and MacOSX uses a flag passed */
906 ((flags & PRUS_MORETOCOME) == 0)
909 * note with the current version this code will only be used
910 * by OpenBSD, NetBSD and FreeBSD have methods for
911 * re-defining sosend() to use sctp_sosend(). One can
912 * optionaly switch back to this code (by changing back the
913 * defininitions but this is not advisable.
917 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
927 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
931 struct sctp_inpcb *inp;
933 struct sctp_tcb *stcb;
936 struct sockaddr_in6 *sin6;
937 struct sockaddr_storage ss;
941 inp6 = (struct in6pcb *)so->so_pcb;
942 inp = (struct sctp_inpcb *)so->so_pcb;
944 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
945 return (ECONNRESET); /* I made the same as TCP since we are
949 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
952 if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
953 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
956 if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
957 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
960 vrf_id = inp->def_vrf_id;
961 SCTP_ASOC_CREATE_LOCK(inp);
963 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
964 SCTP_PCB_FLAGS_UNBOUND) {
965 /* Bind a ephemeral port */
966 SCTP_INP_RUNLOCK(inp);
967 error = sctp6_bind(so, NULL, p);
969 SCTP_ASOC_CREATE_UNLOCK(inp);
975 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
976 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
977 /* We are already connected AND the TCP model */
978 SCTP_INP_RUNLOCK(inp);
979 SCTP_ASOC_CREATE_UNLOCK(inp);
980 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
984 sin6 = (struct sockaddr_in6 *)addr;
985 if (SCTP_IPV6_V6ONLY(inp6)) {
987 * if IPV6_V6ONLY flag, ignore connections destined to a v4
988 * addr or v4-mapped addr
990 if (addr->sa_family == AF_INET) {
991 SCTP_INP_RUNLOCK(inp);
992 SCTP_ASOC_CREATE_UNLOCK(inp);
993 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
996 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
997 SCTP_INP_RUNLOCK(inp);
998 SCTP_ASOC_CREATE_UNLOCK(inp);
999 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1003 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1004 if (!MODULE_GLOBAL(ip6_v6only)) {
1005 /* convert v4-mapped into v4 addr */
1006 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1007 addr = (struct sockaddr *)&ss;
1009 /* mapped addresses aren't enabled */
1010 SCTP_INP_RUNLOCK(inp);
1011 SCTP_ASOC_CREATE_UNLOCK(inp);
1012 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1017 addr = addr; /* for true v6 address case */
1019 /* Now do we connect? */
1020 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1021 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1023 SCTP_TCB_UNLOCK(stcb);
1025 SCTP_INP_RUNLOCK(inp);
1027 SCTP_INP_RUNLOCK(inp);
1028 SCTP_INP_WLOCK(inp);
1029 SCTP_INP_INCR_REF(inp);
1030 SCTP_INP_WUNLOCK(inp);
1031 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1033 SCTP_INP_WLOCK(inp);
1034 SCTP_INP_DECR_REF(inp);
1035 SCTP_INP_WUNLOCK(inp);
1040 /* Already have or am bring up an association */
1041 SCTP_ASOC_CREATE_UNLOCK(inp);
1042 SCTP_TCB_UNLOCK(stcb);
1043 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1046 /* We are GOOD to go */
1047 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1048 SCTP_ASOC_CREATE_UNLOCK(inp);
1050 /* Gak! no memory */
1053 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1054 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1055 /* Set the connected flag so we can queue data */
1058 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1059 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1061 /* initialize authentication parameters for the assoc */
1062 sctp_initialize_auth_params(inp, stcb);
1064 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1065 SCTP_TCB_UNLOCK(stcb);
1070 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1072 struct sockaddr_in6 *sin6;
1073 struct sctp_inpcb *inp;
1075 struct sctp_ifa *sctp_ifa;
1080 * Do the malloc first in case it blocks.
1082 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1085 sin6->sin6_family = AF_INET6;
1086 sin6->sin6_len = sizeof(*sin6);
1088 inp = (struct sctp_inpcb *)so->so_pcb;
1090 SCTP_FREE_SONAME(sin6);
1091 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1094 SCTP_INP_RLOCK(inp);
1095 sin6->sin6_port = inp->sctp_lport;
1096 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1097 /* For the bound all case you get back 0 */
1098 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1099 struct sctp_tcb *stcb;
1100 struct sockaddr_in6 *sin_a6;
1101 struct sctp_nets *net;
1104 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1110 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1111 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1113 /* this will make coverity happy */
1116 if (sin_a6->sin6_family == AF_INET6) {
1121 if ((!fnd) || (sin_a6 == NULL)) {
1125 vrf_id = inp->def_vrf_id;
1126 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1128 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1131 /* For the bound all case you get back 0 */
1133 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1136 /* Take the first IPv6 address in the list */
1137 struct sctp_laddr *laddr;
1140 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1141 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1142 struct sockaddr_in6 *sin_a;
1144 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1145 sin6->sin6_addr = sin_a->sin6_addr;
1151 SCTP_FREE_SONAME(sin6);
1152 SCTP_INP_RUNLOCK(inp);
1153 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1157 SCTP_INP_RUNLOCK(inp);
1158 /* Scoping things for v6 */
1159 if ((error = sa6_recoverscope(sin6)) != 0) {
1160 SCTP_FREE_SONAME(sin6);
1163 (*addr) = (struct sockaddr *)sin6;
1168 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1170 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1172 struct sockaddr_in6 *sin_a6;
1173 struct sctp_inpcb *inp;
1174 struct sctp_tcb *stcb;
1175 struct sctp_nets *net;
1180 * Do the malloc first in case it blocks.
1182 inp = (struct sctp_inpcb *)so->so_pcb;
1183 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1184 /* UDP type and listeners will drop out here */
1185 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1188 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1191 sin6->sin6_family = AF_INET6;
1192 sin6->sin6_len = sizeof(*sin6);
1194 /* We must recapture incase we blocked */
1195 inp = (struct sctp_inpcb *)so->so_pcb;
1197 SCTP_FREE_SONAME(sin6);
1198 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1201 SCTP_INP_RLOCK(inp);
1202 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1204 SCTP_TCB_LOCK(stcb);
1206 SCTP_INP_RUNLOCK(inp);
1208 SCTP_FREE_SONAME(sin6);
1209 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1213 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1214 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1215 if (sin_a6->sin6_family == AF_INET6) {
1217 sin6->sin6_port = stcb->rport;
1218 sin6->sin6_addr = sin_a6->sin6_addr;
1222 SCTP_TCB_UNLOCK(stcb);
1224 /* No IPv4 address */
1225 SCTP_FREE_SONAME(sin6);
1226 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1229 if ((error = sa6_recoverscope(sin6)) != 0)
1231 *addr = (struct sockaddr *)sin6;
1236 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1238 struct sockaddr *addr;
1239 struct in6pcb *inp6 = sotoin6pcb(so);
1243 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1246 /* allow v6 addresses precedence */
1247 error = sctp6_getaddr(so, nam);
1249 /* try v4 next if v6 failed */
1250 error = sctp_ingetaddr(so, nam);
1255 /* if I'm V6ONLY, convert it to v4-mapped */
1256 if (SCTP_IPV6_V6ONLY(inp6)) {
1257 struct sockaddr_in6 sin6;
1259 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1260 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1269 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1271 struct sockaddr *addr = *nam;
1272 struct in6pcb *inp6 = sotoin6pcb(so);
1276 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1279 /* allow v6 addresses precedence */
1280 error = sctp6_peeraddr(so, nam);
1282 /* try v4 next if v6 failed */
1283 error = sctp_peeraddr(so, nam);
1287 /* if I'm V6ONLY, convert it to v4-mapped */
1288 if (SCTP_IPV6_V6ONLY(inp6)) {
1289 struct sockaddr_in6 sin6;
1291 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1292 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1298 struct pr_usrreqs sctp6_usrreqs = {
1299 .pru_abort = sctp6_abort,
1300 .pru_accept = sctp_accept,
1301 .pru_attach = sctp6_attach,
1302 .pru_bind = sctp6_bind,
1303 .pru_connect = sctp6_connect,
1304 .pru_control = in6_control,
1305 .pru_close = sctp6_close,
1306 .pru_detach = sctp6_close,
1307 .pru_sopoll = sopoll_generic,
1308 .pru_flush = sctp_flush,
1309 .pru_disconnect = sctp6_disconnect,
1310 .pru_listen = sctp_listen,
1311 .pru_peeraddr = sctp6_getpeeraddr,
1312 .pru_send = sctp6_send,
1313 .pru_shutdown = sctp_shutdown,
1314 .pru_sockaddr = sctp6_in6getaddr,
1315 .pru_sosend = sctp_sosend,
1316 .pru_soreceive = sctp_soreceive