sctp6_usrreq.c revision 179783
1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3163953Srrs * 4163953Srrs * Redistribution and use in source and binary forms, with or without 5163953Srrs * modification, are permitted provided that the following conditions are met: 6163953Srrs * 7163953Srrs * a) Redistributions of source code must retain the above copyright notice, 8163953Srrs * this list of conditions and the following disclaimer. 9163953Srrs * 10163953Srrs * b) Redistributions in binary form must reproduce the above copyright 11163953Srrs * notice, this list of conditions and the following disclaimer in 12163953Srrs * the documentation and/or other materials provided with the distribution. 13163953Srrs * 14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 15163953Srrs * contributors may be used to endorse or promote products derived 16163953Srrs * from this software without specific prior written permission. 17163953Srrs * 18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 29163953Srrs */ 30163953Srrs/* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */ 31174510Sobrien 32163954Srrs#include <sys/cdefs.h> 33163953Srrs__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 179783 2008-06-14 07:58:05Z rrs $"); 34163953Srrs 35166086Srrs#include <netinet/sctp_os.h> 36163953Srrs#include <sys/proc.h> 37163953Srrs#include <netinet/sctp_pcb.h> 38163953Srrs#include <netinet/sctp_header.h> 39163953Srrs#include <netinet/sctp_var.h> 40168709Srrs#if defined(INET6) 41168709Srrs#include <netinet6/sctp6_var.h> 42168709Srrs#endif 43167598Srrs#include <netinet/sctp_sysctl.h> 44163953Srrs#include <netinet/sctp_output.h> 45166086Srrs#include <netinet/sctp_uio.h> 46163953Srrs#include <netinet/sctp_asconf.h> 47166086Srrs#include <netinet/sctputil.h> 48166086Srrs#include <netinet/sctp_indata.h> 49166086Srrs#include <netinet/sctp_timer.h> 50166086Srrs#include <netinet/sctp_auth.h> 51168709Srrs#include <netinet/sctp_input.h> 52168709Srrs#include <netinet/sctp_output.h> 53170091Srrs#include <netinet/sctp_bsd_addr.h> 54179783Srrs#include <netinet/udp.h> 55163953Srrs 56171167Sgnn#ifdef IPSEC 57171133Sgnn#include <netipsec/ipsec.h> 58171133Sgnn#if defined(INET6) 59171133Sgnn#include <netipsec/ipsec6.h> 60171440Srrs#endif /* INET6 */ 61171440Srrs#endif /* IPSEC */ 62163953Srrs 63163953Srrsextern struct protosw inetsw[]; 64163953Srrs 65163953Srrsint 66171259Sdelphijsctp6_input(struct mbuf **i_pak, int *offp, int proto) 67163953Srrs{ 68165647Srrs struct mbuf *m; 69163953Srrs struct ip6_hdr *ip6; 70163953Srrs struct sctphdr *sh; 71163953Srrs struct sctp_inpcb *in6p = NULL; 72163953Srrs struct sctp_nets *net; 73163953Srrs int refcount_up = 0; 74169352Srrs uint32_t check, calc_check; 75170181Srrs uint32_t vrf_id = 0; 76163953Srrs struct inpcb *in6p_ip; 77163953Srrs struct sctp_chunkhdr *ch; 78163953Srrs int length, mlen, offset, iphlen; 79168299Srrs uint8_t ecn_bits; 80163953Srrs struct sctp_tcb *stcb = NULL; 81170056Srrs int pkt_len = 0; 82163953Srrs int off = *offp; 83179783Srrs uint16_t port = 0; 84163953Srrs 85169352Srrs /* get the VRF and table id's */ 86169352Srrs if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { 87169352Srrs SCTP_RELEASE_PKT(*i_pak); 88169352Srrs return (-1); 89169352Srrs } 90168709Srrs m = SCTP_HEADER_TO_CHAIN(*i_pak); 91170056Srrs pkt_len = SCTP_HEADER_LEN((*i_pak)); 92165647Srrs 93170091Srrs#ifdef SCTP_PACKET_LOGGING 94170091Srrs sctp_packet_log(m, pkt_len); 95170091Srrs#endif 96163953Srrs ip6 = mtod(m, struct ip6_hdr *); 97163953Srrs /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */ 98170056Srrs IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, 99170056Srrs (int)(sizeof(*sh) + sizeof(*ch))); 100163953Srrs if (sh == NULL) { 101163953Srrs SCTP_STAT_INCR(sctps_hdrops); 102163953Srrs return IPPROTO_DONE; 103163953Srrs } 104163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 105163953Srrs iphlen = off; 106163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 107170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 108170859Srrs "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen); 109163953Srrs 110170859Srrs 111163953Srrs#if defined(NFAITH) && NFAITH > 0 112163953Srrs 113163953Srrs if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) { 114163953Srrs /* XXX send icmp6 host/port unreach? */ 115163953Srrs goto bad; 116163953Srrs } 117163953Srrs#endif /* NFAITH defined and > 0 */ 118163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 119163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 120169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n", 121170056Srrs iphlen, pkt_len); 122163953Srrs if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 123163953Srrs /* No multi-cast support in SCTP */ 124163953Srrs goto bad; 125163953Srrs } 126163953Srrs /* destination port of 0 is illegal, based on RFC2960. */ 127163953Srrs if (sh->dest_port == 0) 128163953Srrs goto bad; 129171990Srrs check = sh->checksum; /* save incoming checksum */ 130179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 131171990Srrs (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) { 132171990Srrs goto sctp_skip_csum; 133171990Srrs } 134171990Srrs sh->checksum = 0; /* prepare for calc */ 135171990Srrs calc_check = sctp_calculate_sum(m, &mlen, iphlen); 136171990Srrs if (calc_check != check) { 137171990Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n", 138171990Srrs calc_check, check, m, mlen, iphlen); 139171990Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 140171990Srrs sh, ch, &in6p, &net, vrf_id); 141179783Srrs if ((net) && (port)) { 142179783Srrs if (net->port == 0) { 143179783Srrs sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr)); 144179783Srrs } 145179783Srrs net->port = port; 146179783Srrs } 147171990Srrs /* in6p's ref-count increased && stcb locked */ 148171990Srrs if ((in6p) && (stcb)) { 149171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 150172090Srrs sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 151171990Srrs } else if ((in6p != NULL) && (stcb == NULL)) { 152171990Srrs refcount_up = 1; 153163953Srrs } 154171990Srrs SCTP_STAT_INCR(sctps_badsum); 155171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 156171990Srrs goto bad; 157165647Srrs } 158171990Srrs sh->checksum = calc_check; 159171990Srrs 160163953Srrssctp_skip_csum: 161163953Srrs net = NULL; 162163953Srrs /* 163163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 164163953Srrs * IP/SCTP/first chunk header... 165163953Srrs */ 166163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 167168299Srrs sh, ch, &in6p, &net, vrf_id); 168179783Srrs if ((net) && (port)) { 169179783Srrs if (net->port == 0) { 170179783Srrs sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr)); 171179783Srrs } 172179783Srrs net->port = port; 173179783Srrs } 174163953Srrs /* in6p's ref-count increased */ 175163953Srrs if (in6p == NULL) { 176163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 177163953Srrs 178163953Srrs SCTP_STAT_INCR(sctps_noport); 179163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 180163953Srrs /* 181163953Srrs * we do a trick here to get the INIT tag, dig in 182163953Srrs * and get the tag from the INIT and put it in the 183163953Srrs * common header. 184163953Srrs */ 185163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 186163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 187168299Srrs (uint8_t *) & chunk_buf); 188169420Srrs if (init_chk) 189169420Srrs sh->v_tag = init_chk->init.initiate_tag; 190169420Srrs else 191169420Srrs sh->v_tag = 0; 192163953Srrs } 193165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 194179783Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 195165220Srrs goto bad; 196165220Srrs } 197165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 198165220Srrs goto bad; 199165220Srrs } 200165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 201179783Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 202163953Srrs goto bad; 203163953Srrs } else if (stcb == NULL) { 204163953Srrs refcount_up = 1; 205163953Srrs } 206163953Srrs in6p_ip = (struct inpcb *)in6p; 207171167Sgnn#ifdef IPSEC 208163953Srrs /* 209163953Srrs * Check AH/ESP integrity. 210163953Srrs */ 211163996Srrs if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) { 212163953Srrs/* XXX */ 213163953Srrs ipsec6stat.in_polvio++; 214163953Srrs goto bad; 215163996Srrs } 216171440Srrs#endif /* IPSEC */ 217163953Srrs 218163953Srrs /* 219163953Srrs * CONTROL chunk processing 220163953Srrs */ 221163953Srrs offset -= sizeof(*ch); 222163953Srrs ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); 223165647Srrs 224165647Srrs /* Length now holds the total packet length payload + iphlen */ 225165647Srrs length = ntohs(ip6->ip6_plen) + iphlen; 226165647Srrs 227169655Srrs /* sa_ignore NO_NULL_CHK */ 228169378Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 229179783Srrs in6p, stcb, net, ecn_bits, vrf_id, port); 230163953Srrs /* inp's ref-count reduced && stcb unlocked */ 231163953Srrs /* XXX this stuff below gets moved to appropriate parts later... */ 232163953Srrs if (m) 233169352Srrs sctp_m_freem(m); 234163953Srrs if ((in6p) && refcount_up) { 235163953Srrs /* reduce ref-count */ 236163953Srrs SCTP_INP_WLOCK(in6p); 237163953Srrs SCTP_INP_DECR_REF(in6p); 238163953Srrs SCTP_INP_WUNLOCK(in6p); 239163953Srrs } 240163953Srrs return IPPROTO_DONE; 241163953Srrs 242163953Srrsbad: 243169420Srrs if (stcb) { 244163953Srrs SCTP_TCB_UNLOCK(stcb); 245169420Srrs } 246163953Srrs if ((in6p) && refcount_up) { 247163953Srrs /* reduce ref-count */ 248163953Srrs SCTP_INP_WLOCK(in6p); 249163953Srrs SCTP_INP_DECR_REF(in6p); 250163953Srrs SCTP_INP_WUNLOCK(in6p); 251163953Srrs } 252163953Srrs if (m) 253169352Srrs sctp_m_freem(m); 254163953Srrs return IPPROTO_DONE; 255163953Srrs} 256163953Srrs 257163953Srrs 258163953Srrsstatic void 259171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6, 260171259Sdelphij struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net) 261163953Srrs{ 262168299Srrs uint32_t nxtsz; 263163953Srrs 264163953Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 265163953Srrs (icmp6 == NULL) || (sh == NULL)) { 266163953Srrs goto out; 267163953Srrs } 268163953Srrs /* First do we even look at it? */ 269163953Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) 270163953Srrs goto out; 271163953Srrs 272163953Srrs if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) { 273163953Srrs /* not PACKET TO BIG */ 274163953Srrs goto out; 275163953Srrs } 276163953Srrs /* 277163953Srrs * ok we need to look closely. We could even get smarter and look at 278163953Srrs * anyone that we sent to in case we get a different ICMP that tells 279163953Srrs * us there is no way to reach a host, but for this impl, all we 280163953Srrs * care about is MTU discovery. 281163953Srrs */ 282163953Srrs nxtsz = ntohl(icmp6->icmp6_mtu); 283163953Srrs /* Stop any PMTU timer */ 284165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1); 285163953Srrs 286163953Srrs /* Adjust destination size limit */ 287163953Srrs if (net->mtu > nxtsz) { 288163953Srrs net->mtu = nxtsz; 289163953Srrs } 290163953Srrs /* now what about the ep? */ 291163953Srrs if (stcb->asoc.smallest_mtu > nxtsz) { 292163953Srrs struct sctp_tmit_chunk *chk; 293163953Srrs 294163953Srrs /* Adjust that too */ 295163953Srrs stcb->asoc.smallest_mtu = nxtsz; 296163953Srrs /* now off to subtract IP_DF flag if needed */ 297163953Srrs 298163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 299168299Srrs if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 300163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 301163953Srrs } 302163953Srrs } 303163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 304168299Srrs if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 305163953Srrs /* 306163953Srrs * For this guy we also mark for immediate 307163953Srrs * resend since we sent to big of chunk 308163953Srrs */ 309163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 310163953Srrs if (chk->sent != SCTP_DATAGRAM_RESEND) 311163953Srrs stcb->asoc.sent_queue_retran_cnt++; 312163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 313163953Srrs chk->rec.data.doing_fast_retransmit = 0; 314163953Srrs 315163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 316163953Srrs /* Clear any time so NO RTT is being done */ 317163953Srrs chk->sent_rcv_time.tv_sec = 0; 318163953Srrs chk->sent_rcv_time.tv_usec = 0; 319163953Srrs stcb->asoc.total_flight -= chk->send_size; 320163953Srrs net->flight_size -= chk->send_size; 321163953Srrs } 322163953Srrs } 323163953Srrs } 324163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 325163953Srrsout: 326169420Srrs if (stcb) { 327163953Srrs SCTP_TCB_UNLOCK(stcb); 328169420Srrs } 329163953Srrs} 330163953Srrs 331163953Srrs 332172156Srrsvoid 333172091Srrssctp6_notify(struct sctp_inpcb *inp, 334172091Srrs struct icmp6_hdr *icmph, 335172091Srrs struct sctphdr *sh, 336172091Srrs struct sockaddr *to, 337172091Srrs struct sctp_tcb *stcb, 338172091Srrs struct sctp_nets *net) 339172091Srrs{ 340172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 341172091Srrs struct socket *so; 342172091Srrs 343172091Srrs#endif 344172091Srrs /* protection */ 345172091Srrs int reason; 346172091Srrs 347172091Srrs 348172091Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 349172091Srrs (sh == NULL) || (to == NULL)) { 350172091Srrs if (stcb) 351172091Srrs SCTP_TCB_UNLOCK(stcb); 352172091Srrs return; 353172091Srrs } 354172091Srrs /* First job is to verify the vtag matches what I would send */ 355172091Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 356172091Srrs SCTP_TCB_UNLOCK(stcb); 357172091Srrs return; 358172091Srrs } 359172091Srrs if (icmph->icmp6_type != ICMP_UNREACH) { 360172091Srrs /* We only care about unreachable */ 361172091Srrs SCTP_TCB_UNLOCK(stcb); 362172091Srrs return; 363172091Srrs } 364172091Srrs if ((icmph->icmp6_code == ICMP_UNREACH_NET) || 365172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST) || 366172091Srrs (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) || 367172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) || 368172091Srrs (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) || 369172091Srrs (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) || 370172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) || 371172091Srrs (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) { 372172091Srrs 373172091Srrs /* 374172091Srrs * Hmm reachablity problems we must examine closely. If its 375172091Srrs * not reachable, we may have lost a network. Or if there is 376172091Srrs * NO protocol at the other end named SCTP. well we consider 377172091Srrs * it a OOTB abort. 378172091Srrs */ 379172091Srrs if (net->dest_state & SCTP_ADDR_REACHABLE) { 380172091Srrs /* Ok that destination is NOT reachable */ 381172091Srrs SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n", 382172091Srrs net->error_count, 383172091Srrs net->failure_threshold, 384172091Srrs net); 385172091Srrs 386172091Srrs net->dest_state &= ~SCTP_ADDR_REACHABLE; 387172091Srrs net->dest_state |= SCTP_ADDR_NOT_REACHABLE; 388172091Srrs /* 389172091Srrs * JRS 5/14/07 - If a destination is unreachable, 390172091Srrs * the PF bit is turned off. This allows an 391172091Srrs * unambiguous use of the PF bit for destinations 392172091Srrs * that are reachable but potentially failed. If the 393172091Srrs * destination is set to the unreachable state, also 394172091Srrs * set the destination to the PF state. 395172091Srrs */ 396172091Srrs /* 397172091Srrs * Add debug message here if destination is not in 398172091Srrs * PF state. 399172091Srrs */ 400172091Srrs /* Stop any running T3 timers here? */ 401179783Srrs if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) { 402172091Srrs net->dest_state &= ~SCTP_ADDR_PF; 403172091Srrs SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n", 404172091Srrs net); 405172091Srrs } 406172091Srrs net->error_count = net->failure_threshold + 1; 407172091Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 408172091Srrs stcb, SCTP_FAILED_THRESHOLD, 409172091Srrs (void *)net, SCTP_SO_NOT_LOCKED); 410172091Srrs } 411172091Srrs SCTP_TCB_UNLOCK(stcb); 412172091Srrs } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) || 413172091Srrs (icmph->icmp6_code == ICMP_UNREACH_PORT)) { 414172091Srrs /* 415172091Srrs * Here the peer is either playing tricks on us, including 416172091Srrs * an address that belongs to someone who does not support 417172091Srrs * SCTP OR was a userland implementation that shutdown and 418172091Srrs * now is dead. In either case treat it like a OOTB abort 419172091Srrs * with no TCB 420172091Srrs */ 421172091Srrs reason = SCTP_PEER_FAULTY; 422172091Srrs sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED); 423172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 424172091Srrs so = SCTP_INP_SO(inp); 425172091Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 426172091Srrs SCTP_TCB_UNLOCK(stcb); 427172091Srrs SCTP_SOCKET_LOCK(so, 1); 428172091Srrs SCTP_TCB_LOCK(stcb); 429172091Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 430172091Srrs#endif 431172091Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 432172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 433172091Srrs SCTP_SOCKET_UNLOCK(so, 1); 434172091Srrs /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */ 435172091Srrs#endif 436172091Srrs /* no need to unlock here, since the TCB is gone */ 437172091Srrs } else { 438172091Srrs SCTP_TCB_UNLOCK(stcb); 439172091Srrs } 440172091Srrs} 441172091Srrs 442172091Srrs 443172091Srrs 444163953Srrsvoid 445171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d) 446163953Srrs{ 447163953Srrs struct sctphdr sh; 448163953Srrs struct ip6ctlparam *ip6cp = NULL; 449167598Srrs uint32_t vrf_id; 450163953Srrs 451167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 452167598Srrs 453163953Srrs if (pktdst->sa_family != AF_INET6 || 454163953Srrs pktdst->sa_len != sizeof(struct sockaddr_in6)) 455163953Srrs return; 456163953Srrs 457163953Srrs if ((unsigned)cmd >= PRC_NCMDS) 458163953Srrs return; 459163953Srrs if (PRC_IS_REDIRECT(cmd)) { 460163953Srrs d = NULL; 461163953Srrs } else if (inet6ctlerrmap[cmd] == 0) { 462163953Srrs return; 463163953Srrs } 464163953Srrs /* if the parameter is from icmp6, decode it. */ 465163953Srrs if (d != NULL) { 466163953Srrs ip6cp = (struct ip6ctlparam *)d; 467163953Srrs } else { 468163953Srrs ip6cp = (struct ip6ctlparam *)NULL; 469163953Srrs } 470163953Srrs 471163953Srrs if (ip6cp) { 472163953Srrs /* 473163953Srrs * XXX: We assume that when IPV6 is non NULL, M and OFF are 474163953Srrs * valid. 475163953Srrs */ 476163953Srrs /* check if we can safely examine src and dst ports */ 477163953Srrs struct sctp_inpcb *inp = NULL; 478163953Srrs struct sctp_tcb *stcb = NULL; 479163953Srrs struct sctp_nets *net = NULL; 480163953Srrs struct sockaddr_in6 final; 481163953Srrs 482165647Srrs if (ip6cp->ip6c_m == NULL) 483163953Srrs return; 484163953Srrs 485163953Srrs bzero(&sh, sizeof(sh)); 486163953Srrs bzero(&final, sizeof(final)); 487163953Srrs inp = NULL; 488163953Srrs net = NULL; 489163953Srrs m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), 490163953Srrs (caddr_t)&sh); 491163953Srrs ip6cp->ip6c_src->sin6_port = sh.src_port; 492163953Srrs final.sin6_len = sizeof(final); 493163953Srrs final.sin6_family = AF_INET6; 494163953Srrs final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; 495163953Srrs final.sin6_port = sh.dest_port; 496163953Srrs stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src, 497163953Srrs (struct sockaddr *)&final, 498167598Srrs &inp, &net, 1, vrf_id); 499163953Srrs /* inp's ref-count increased && stcb locked */ 500163953Srrs if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 501163953Srrs if (cmd == PRC_MSGSIZE) { 502163953Srrs sctp6_notify_mbuf(inp, 503163953Srrs ip6cp->ip6c_icmp6, 504163953Srrs &sh, 505163953Srrs stcb, 506163953Srrs net); 507163953Srrs /* inp's ref-count reduced && stcb unlocked */ 508163953Srrs } else { 509172091Srrs sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh, 510163953Srrs (struct sockaddr *)&final, 511163953Srrs stcb, net); 512163953Srrs /* inp's ref-count reduced && stcb unlocked */ 513163953Srrs } 514163953Srrs } else { 515163953Srrs if (PRC_IS_REDIRECT(cmd) && inp) { 516163953Srrs in6_rtchange((struct in6pcb *)inp, 517163953Srrs inet6ctlerrmap[cmd]); 518163953Srrs } 519163953Srrs if (inp) { 520163953Srrs /* reduce inp's ref-count */ 521163953Srrs SCTP_INP_WLOCK(inp); 522163953Srrs SCTP_INP_DECR_REF(inp); 523163953Srrs SCTP_INP_WUNLOCK(inp); 524163953Srrs } 525163953Srrs if (stcb) 526163953Srrs SCTP_TCB_UNLOCK(stcb); 527163953Srrs } 528163953Srrs } 529163953Srrs} 530163953Srrs 531163953Srrs/* 532163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c 533163953Srrs * since they do the same thing and now we lookup with a sockaddr 534163953Srrs */ 535163953Srrsstatic int 536163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS) 537163953Srrs{ 538164085Srrs struct xucred xuc; 539163953Srrs struct sockaddr_in6 addrs[2]; 540163953Srrs struct sctp_inpcb *inp; 541163953Srrs struct sctp_nets *net; 542163953Srrs struct sctp_tcb *stcb; 543164085Srrs int error; 544167598Srrs uint32_t vrf_id; 545163953Srrs 546167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 547167598Srrs 548170587Srwatson error = priv_check(req->td, PRIV_NETINET_GETCRED); 549163953Srrs if (error) 550163953Srrs return (error); 551163953Srrs 552171943Srrs if (req->newlen != sizeof(addrs)) { 553171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 554163953Srrs return (EINVAL); 555171943Srrs } 556171943Srrs if (req->oldlen != sizeof(struct ucred)) { 557171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 558163953Srrs return (EINVAL); 559171943Srrs } 560163953Srrs error = SYSCTL_IN(req, addrs, sizeof(addrs)); 561163953Srrs if (error) 562163953Srrs return (error); 563163953Srrs 564163953Srrs stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]), 565163953Srrs sin6tosa(&addrs[1]), 566167598Srrs &inp, &net, 1, vrf_id); 567163953Srrs if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 568164085Srrs if ((inp != NULL) && (stcb == NULL)) { 569164085Srrs /* reduce ref-count */ 570163953Srrs SCTP_INP_WLOCK(inp); 571163953Srrs SCTP_INP_DECR_REF(inp); 572164085Srrs goto cred_can_cont; 573163953Srrs } 574171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 575164085Srrs error = ENOENT; 576163953Srrs goto out; 577163953Srrs } 578163953Srrs SCTP_TCB_UNLOCK(stcb); 579164085Srrs /* 580164085Srrs * We use the write lock here, only since in the error leg we need 581164085Srrs * it. If we used RLOCK, then we would have to 582164085Srrs * wlock/decr/unlock/rlock. Which in theory could create a hole. 583164085Srrs * Better to use higher wlock. 584164085Srrs */ 585164085Srrs SCTP_INP_WLOCK(inp); 586164085Srrscred_can_cont: 587164085Srrs error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 588164085Srrs if (error) { 589164085Srrs SCTP_INP_WUNLOCK(inp); 590164085Srrs goto out; 591164085Srrs } 592164085Srrs cru2x(inp->sctp_socket->so_cred, &xuc); 593164085Srrs SCTP_INP_WUNLOCK(inp); 594164085Srrs error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 595163953Srrsout: 596163953Srrs return (error); 597163953Srrs} 598163953Srrs 599163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 600163953Srrs 0, 0, 601163953Srrs sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 602163953Srrs 603163953Srrs 604163953Srrs/* This is the same as the sctp_abort() could be made common */ 605163953Srrsstatic void 606163953Srrssctp6_abort(struct socket *so) 607163953Srrs{ 608163953Srrs struct sctp_inpcb *inp; 609163953Srrs uint32_t flags; 610163953Srrs 611163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 612172091Srrs if (inp == 0) { 613172091Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 614163953Srrs return; 615172091Srrs } 616163953Srrssctp_must_try_again: 617163953Srrs flags = inp->sctp_flags; 618163953Srrs#ifdef SCTP_LOG_CLOSING 619163953Srrs sctp_log_closing(inp, NULL, 17); 620163953Srrs#endif 621163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 622163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 623163953Srrs#ifdef SCTP_LOG_CLOSING 624163953Srrs sctp_log_closing(inp, NULL, 16); 625163953Srrs#endif 626169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 627169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 628163953Srrs SOCK_LOCK(so); 629167695Srrs SCTP_SB_CLEAR(so->so_snd); 630163953Srrs /* 631163953Srrs * same for the rcv ones, they are only here for the 632163953Srrs * accounting/select. 633163953Srrs */ 634167695Srrs SCTP_SB_CLEAR(so->so_rcv); 635167695Srrs /* Now null out the reference, we are completely detached. */ 636163953Srrs so->so_pcb = NULL; 637163953Srrs SOCK_UNLOCK(so); 638163953Srrs } else { 639163953Srrs flags = inp->sctp_flags; 640163953Srrs if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 641163953Srrs goto sctp_must_try_again; 642163953Srrs } 643163953Srrs } 644163953Srrs return; 645163953Srrs} 646163953Srrs 647163953Srrsstatic int 648163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p) 649163953Srrs{ 650163953Srrs struct in6pcb *inp6; 651166086Srrs int error; 652163953Srrs struct sctp_inpcb *inp; 653170205Srrs uint32_t vrf_id = SCTP_DEFAULT_VRFID; 654163953Srrs 655163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 656171943Srrs if (inp != NULL) { 657171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 658163953Srrs return EINVAL; 659171943Srrs } 660163953Srrs if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 661179783Srrs error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 662163953Srrs if (error) 663163953Srrs return error; 664163953Srrs } 665170205Srrs error = sctp_inpcb_alloc(so, vrf_id); 666163953Srrs if (error) 667163953Srrs return error; 668163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 669170205Srrs SCTP_INP_WLOCK(inp); 670163953Srrs inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 671163953Srrs inp6 = (struct in6pcb *)inp; 672163953Srrs 673163953Srrs inp6->inp_vflag |= INP_IPV6; 674163953Srrs inp6->in6p_hops = -1; /* use kernel default */ 675163953Srrs inp6->in6p_cksum = -1; /* just to be sure */ 676163953Srrs#ifdef INET 677163953Srrs /* 678163953Srrs * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 679163953Srrs * socket as well, because the socket may be bound to an IPv6 680163953Srrs * wildcard address, which may match an IPv4-mapped IPv6 address. 681163953Srrs */ 682163953Srrs inp6->inp_ip_ttl = ip_defttl; 683163953Srrs#endif 684163953Srrs /* 685163953Srrs * Hmm what about the IPSEC stuff that is missing here but in 686163953Srrs * sctp_attach()? 687163953Srrs */ 688170205Srrs SCTP_INP_WUNLOCK(inp); 689163953Srrs return 0; 690163953Srrs} 691163953Srrs 692163953Srrsstatic int 693163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 694163953Srrs{ 695163953Srrs struct sctp_inpcb *inp; 696163953Srrs struct in6pcb *inp6; 697166086Srrs int error; 698163953Srrs 699163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 700171943Srrs if (inp == 0) { 701171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 702163953Srrs return EINVAL; 703171943Srrs } 704170056Srrs if (addr) { 705170056Srrs if ((addr->sa_family == AF_INET6) && 706170056Srrs (addr->sa_len != sizeof(struct sockaddr_in6))) { 707171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 708170056Srrs return EINVAL; 709170056Srrs } 710170056Srrs if ((addr->sa_family == AF_INET) && 711170056Srrs (addr->sa_len != sizeof(struct sockaddr_in))) { 712171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 713170056Srrs return EINVAL; 714170056Srrs } 715170056Srrs } 716163953Srrs inp6 = (struct in6pcb *)inp; 717163953Srrs inp6->inp_vflag &= ~INP_IPV4; 718163953Srrs inp6->inp_vflag |= INP_IPV6; 719166023Srrs if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { 720163953Srrs if (addr->sa_family == AF_INET) { 721163953Srrs /* binding v4 addr to v6 socket, so reset flags */ 722163953Srrs inp6->inp_vflag |= INP_IPV4; 723163953Srrs inp6->inp_vflag &= ~INP_IPV6; 724163953Srrs } else { 725163953Srrs struct sockaddr_in6 *sin6_p; 726163953Srrs 727163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 728163953Srrs 729163953Srrs if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 730163953Srrs inp6->inp_vflag |= INP_IPV4; 731163953Srrs } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 732163953Srrs struct sockaddr_in sin; 733163953Srrs 734163953Srrs in6_sin6_2_sin(&sin, sin6_p); 735163953Srrs inp6->inp_vflag |= INP_IPV4; 736163953Srrs inp6->inp_vflag &= ~INP_IPV6; 737171572Srrs error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); 738163953Srrs return error; 739163953Srrs } 740163953Srrs } 741163953Srrs } else if (addr != NULL) { 742163953Srrs /* IPV6_V6ONLY socket */ 743163953Srrs if (addr->sa_family == AF_INET) { 744163953Srrs /* can't bind v4 addr to v6 only socket! */ 745171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 746163953Srrs return EINVAL; 747163953Srrs } else { 748163953Srrs struct sockaddr_in6 *sin6_p; 749163953Srrs 750163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 751163953Srrs 752172091Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 753163953Srrs /* can't bind v4-mapped addrs either! */ 754163953Srrs /* NOTE: we don't support SIIT */ 755171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 756172091Srrs return EINVAL; 757172091Srrs } 758163953Srrs } 759163953Srrs } 760171572Srrs error = sctp_inpcb_bind(so, addr, NULL, p); 761163953Srrs return error; 762163953Srrs} 763163953Srrs 764163953Srrs 765163953Srrsstatic void 766163953Srrssctp6_close(struct socket *so) 767163953Srrs{ 768171990Srrs sctp_close(so); 769163953Srrs} 770163953Srrs 771167598Srrs/* This could be made common with sctp_detach() since they are identical */ 772163953Srrs 773168709Srrsstatic 774168709Srrsint 775163953Srrssctp6_disconnect(struct socket *so) 776163953Srrs{ 777171990Srrs return (sctp_disconnect(so)); 778163953Srrs} 779163953Srrs 780168709Srrs 781163953Srrsint 782163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 783163953Srrs struct mbuf *control, struct thread *p); 784163953Srrs 785163953Srrs 786163953Srrsstatic int 787163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 788163953Srrs struct mbuf *control, struct thread *p) 789163953Srrs{ 790163953Srrs struct sctp_inpcb *inp; 791163953Srrs struct inpcb *in_inp; 792163953Srrs struct in6pcb *inp6; 793163953Srrs 794163953Srrs#ifdef INET 795163953Srrs struct sockaddr_in6 *sin6; 796163953Srrs 797163953Srrs#endif /* INET */ 798163953Srrs /* No SPL needed since sctp_output does this */ 799163953Srrs 800163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 801163953Srrs if (inp == NULL) { 802163953Srrs if (control) { 803169352Srrs SCTP_RELEASE_PKT(control); 804163953Srrs control = NULL; 805163953Srrs } 806169352Srrs SCTP_RELEASE_PKT(m); 807171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 808163953Srrs return EINVAL; 809163953Srrs } 810163953Srrs in_inp = (struct inpcb *)inp; 811163953Srrs inp6 = (struct in6pcb *)inp; 812163953Srrs /* 813163953Srrs * For the TCP model we may get a NULL addr, if we are a connected 814163953Srrs * socket thats ok. 815163953Srrs */ 816163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 817163953Srrs (addr == NULL)) { 818163953Srrs goto connected_type; 819163953Srrs } 820163953Srrs if (addr == NULL) { 821169352Srrs SCTP_RELEASE_PKT(m); 822163953Srrs if (control) { 823169352Srrs SCTP_RELEASE_PKT(control); 824163953Srrs control = NULL; 825163953Srrs } 826171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); 827163953Srrs return (EDESTADDRREQ); 828163953Srrs } 829163953Srrs#ifdef INET 830163953Srrs sin6 = (struct sockaddr_in6 *)addr; 831166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 832163953Srrs /* 833163953Srrs * if IPV6_V6ONLY flag, we discard datagrams destined to a 834163953Srrs * v4 addr or v4-mapped addr 835163953Srrs */ 836163953Srrs if (addr->sa_family == AF_INET) { 837171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 838163953Srrs return EINVAL; 839163953Srrs } 840163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 841171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 842163953Srrs return EINVAL; 843163953Srrs } 844163953Srrs } 845163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 846163953Srrs if (!ip6_v6only) { 847163953Srrs struct sockaddr_in sin; 848163953Srrs 849163953Srrs /* convert v4-mapped into v4 addr and send */ 850163953Srrs in6_sin6_2_sin(&sin, sin6); 851163953Srrs return sctp_sendm(so, flags, m, (struct sockaddr *)&sin, 852163953Srrs control, p); 853163953Srrs } else { 854163953Srrs /* mapped addresses aren't enabled */ 855171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 856163953Srrs return EINVAL; 857163953Srrs } 858163953Srrs } 859163953Srrs#endif /* INET */ 860163953Srrsconnected_type: 861163953Srrs /* now what about control */ 862163953Srrs if (control) { 863163953Srrs if (inp->control) { 864169420Srrs SCTP_PRINTF("huh? control set?\n"); 865169352Srrs SCTP_RELEASE_PKT(inp->control); 866163953Srrs inp->control = NULL; 867163953Srrs } 868163953Srrs inp->control = control; 869163953Srrs } 870163953Srrs /* Place the data */ 871163953Srrs if (inp->pkt) { 872165647Srrs SCTP_BUF_NEXT(inp->pkt_last) = m; 873163953Srrs inp->pkt_last = m; 874163953Srrs } else { 875163953Srrs inp->pkt_last = inp->pkt = m; 876163953Srrs } 877163953Srrs if ( 878163953Srrs /* FreeBSD and MacOSX uses a flag passed */ 879163953Srrs ((flags & PRUS_MORETOCOME) == 0) 880163953Srrs ) { 881163953Srrs /* 882163953Srrs * note with the current version this code will only be used 883163953Srrs * by OpenBSD, NetBSD and FreeBSD have methods for 884163953Srrs * re-defining sosend() to use sctp_sosend(). One can 885163953Srrs * optionaly switch back to this code (by changing back the 886163953Srrs * defininitions but this is not advisable. 887163953Srrs */ 888163953Srrs int ret; 889163953Srrs 890163953Srrs ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 891163953Srrs inp->pkt = NULL; 892163953Srrs inp->control = NULL; 893163953Srrs return (ret); 894163953Srrs } else { 895163953Srrs return (0); 896163953Srrs } 897163953Srrs} 898163953Srrs 899163953Srrsstatic int 900163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 901163953Srrs{ 902167598Srrs uint32_t vrf_id; 903163953Srrs int error = 0; 904163953Srrs struct sctp_inpcb *inp; 905163953Srrs struct in6pcb *inp6; 906163953Srrs struct sctp_tcb *stcb; 907163953Srrs 908163953Srrs#ifdef INET 909163953Srrs struct sockaddr_in6 *sin6; 910163953Srrs struct sockaddr_storage ss; 911163953Srrs 912163953Srrs#endif /* INET */ 913163953Srrs 914163953Srrs inp6 = (struct in6pcb *)so->so_pcb; 915163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 916163953Srrs if (inp == 0) { 917171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 918163953Srrs return (ECONNRESET); /* I made the same as TCP since we are 919163953Srrs * not setup? */ 920163953Srrs } 921170056Srrs if (addr == NULL) { 922171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 923170056Srrs return (EINVAL); 924170056Srrs } 925170056Srrs if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) { 926171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 927170056Srrs return (EINVAL); 928170056Srrs } 929170056Srrs if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) { 930171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 931170056Srrs return (EINVAL); 932170056Srrs } 933168299Srrs vrf_id = inp->def_vrf_id; 934163953Srrs SCTP_ASOC_CREATE_LOCK(inp); 935163953Srrs SCTP_INP_RLOCK(inp); 936163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 937163953Srrs SCTP_PCB_FLAGS_UNBOUND) { 938163953Srrs /* Bind a ephemeral port */ 939163953Srrs SCTP_INP_RUNLOCK(inp); 940163953Srrs error = sctp6_bind(so, NULL, p); 941163953Srrs if (error) { 942163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 943163953Srrs 944163953Srrs return (error); 945163953Srrs } 946163953Srrs SCTP_INP_RLOCK(inp); 947163953Srrs } 948163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 949163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 950163953Srrs /* We are already connected AND the TCP model */ 951163953Srrs SCTP_INP_RUNLOCK(inp); 952163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 953171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); 954163953Srrs return (EADDRINUSE); 955163953Srrs } 956163953Srrs#ifdef INET 957163953Srrs sin6 = (struct sockaddr_in6 *)addr; 958166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 959163953Srrs /* 960163953Srrs * if IPV6_V6ONLY flag, ignore connections destined to a v4 961163953Srrs * addr or v4-mapped addr 962163953Srrs */ 963163953Srrs if (addr->sa_family == AF_INET) { 964163953Srrs SCTP_INP_RUNLOCK(inp); 965163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 966171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 967163953Srrs return EINVAL; 968163953Srrs } 969163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 970163953Srrs SCTP_INP_RUNLOCK(inp); 971163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 972171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 973163953Srrs return EINVAL; 974163953Srrs } 975163953Srrs } 976163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 977163953Srrs if (!ip6_v6only) { 978163953Srrs /* convert v4-mapped into v4 addr */ 979163953Srrs in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); 980163953Srrs addr = (struct sockaddr *)&ss; 981163953Srrs } else { 982163953Srrs /* mapped addresses aren't enabled */ 983163953Srrs SCTP_INP_RUNLOCK(inp); 984163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 985171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 986163953Srrs return EINVAL; 987163953Srrs } 988163953Srrs } else 989163953Srrs#endif /* INET */ 990163953Srrs addr = addr; /* for true v6 address case */ 991163953Srrs 992163953Srrs /* Now do we connect? */ 993163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 994163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 995169420Srrs if (stcb) { 996163953Srrs SCTP_TCB_UNLOCK(stcb); 997169420Srrs } 998163953Srrs SCTP_INP_RUNLOCK(inp); 999163953Srrs } else { 1000163953Srrs SCTP_INP_RUNLOCK(inp); 1001163953Srrs SCTP_INP_WLOCK(inp); 1002163953Srrs SCTP_INP_INCR_REF(inp); 1003163953Srrs SCTP_INP_WUNLOCK(inp); 1004163953Srrs stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 1005163953Srrs if (stcb == NULL) { 1006163953Srrs SCTP_INP_WLOCK(inp); 1007163953Srrs SCTP_INP_DECR_REF(inp); 1008163953Srrs SCTP_INP_WUNLOCK(inp); 1009163953Srrs } 1010163953Srrs } 1011163953Srrs 1012163953Srrs if (stcb != NULL) { 1013163953Srrs /* Already have or am bring up an association */ 1014163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1015163953Srrs SCTP_TCB_UNLOCK(stcb); 1016171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); 1017163953Srrs return (EALREADY); 1018163953Srrs } 1019163953Srrs /* We are GOOD to go */ 1020171531Srrs stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p); 1021163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1022163953Srrs if (stcb == NULL) { 1023163953Srrs /* Gak! no memory */ 1024163953Srrs return (error); 1025163953Srrs } 1026163953Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1027163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1028163953Srrs /* Set the connected flag so we can queue data */ 1029163953Srrs soisconnecting(so); 1030163953Srrs } 1031163953Srrs stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1032169420Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1033163953Srrs 1034163953Srrs /* initialize authentication parameters for the assoc */ 1035163953Srrs sctp_initialize_auth_params(inp, stcb); 1036163953Srrs 1037172090Srrs sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 1038163953Srrs SCTP_TCB_UNLOCK(stcb); 1039163953Srrs return error; 1040163953Srrs} 1041163953Srrs 1042163953Srrsstatic int 1043163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr) 1044163953Srrs{ 1045163953Srrs struct sockaddr_in6 *sin6; 1046163953Srrs struct sctp_inpcb *inp; 1047167598Srrs uint32_t vrf_id; 1048167598Srrs struct sctp_ifa *sctp_ifa; 1049163953Srrs 1050163953Srrs int error; 1051163953Srrs 1052163953Srrs /* 1053163953Srrs * Do the malloc first in case it blocks. 1054163953Srrs */ 1055163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1056163953Srrs sin6->sin6_family = AF_INET6; 1057163953Srrs sin6->sin6_len = sizeof(*sin6); 1058163953Srrs 1059163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1060163953Srrs if (inp == NULL) { 1061163953Srrs SCTP_FREE_SONAME(sin6); 1062171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1063163953Srrs return ECONNRESET; 1064163953Srrs } 1065163953Srrs SCTP_INP_RLOCK(inp); 1066163953Srrs sin6->sin6_port = inp->sctp_lport; 1067163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1068163953Srrs /* For the bound all case you get back 0 */ 1069163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1070163953Srrs struct sctp_tcb *stcb; 1071163953Srrs struct sockaddr_in6 *sin_a6; 1072163953Srrs struct sctp_nets *net; 1073163953Srrs int fnd; 1074163953Srrs 1075163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1076163953Srrs if (stcb == NULL) { 1077163953Srrs goto notConn6; 1078163953Srrs } 1079163953Srrs fnd = 0; 1080163953Srrs sin_a6 = NULL; 1081163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1082163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1083164085Srrs if (sin_a6 == NULL) 1084164085Srrs /* this will make coverity happy */ 1085164085Srrs continue; 1086164085Srrs 1087163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1088163953Srrs fnd = 1; 1089163953Srrs break; 1090163953Srrs } 1091163953Srrs } 1092163953Srrs if ((!fnd) || (sin_a6 == NULL)) { 1093163953Srrs /* punt */ 1094163953Srrs goto notConn6; 1095163953Srrs } 1096168299Srrs vrf_id = inp->def_vrf_id; 1097168299Srrs sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id); 1098167598Srrs if (sctp_ifa) { 1099167598Srrs sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 1100167598Srrs } 1101163953Srrs } else { 1102163953Srrs /* For the bound all case you get back 0 */ 1103163953Srrs notConn6: 1104163953Srrs memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1105163953Srrs } 1106163953Srrs } else { 1107163953Srrs /* Take the first IPv6 address in the list */ 1108163953Srrs struct sctp_laddr *laddr; 1109163953Srrs int fnd = 0; 1110163953Srrs 1111163953Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1112167598Srrs if (laddr->ifa->address.sa.sa_family == AF_INET6) { 1113163953Srrs struct sockaddr_in6 *sin_a; 1114163953Srrs 1115167598Srrs sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; 1116163953Srrs sin6->sin6_addr = sin_a->sin6_addr; 1117163953Srrs fnd = 1; 1118163953Srrs break; 1119163953Srrs } 1120163953Srrs } 1121163953Srrs if (!fnd) { 1122163953Srrs SCTP_FREE_SONAME(sin6); 1123163953Srrs SCTP_INP_RUNLOCK(inp); 1124171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1125163953Srrs return ENOENT; 1126163953Srrs } 1127163953Srrs } 1128163953Srrs SCTP_INP_RUNLOCK(inp); 1129163953Srrs /* Scoping things for v6 */ 1130164085Srrs if ((error = sa6_recoverscope(sin6)) != 0) { 1131164085Srrs SCTP_FREE_SONAME(sin6); 1132163953Srrs return (error); 1133164085Srrs } 1134163953Srrs (*addr) = (struct sockaddr *)sin6; 1135163953Srrs return (0); 1136163953Srrs} 1137163953Srrs 1138163953Srrsstatic int 1139163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1140163953Srrs{ 1141163953Srrs struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr; 1142163953Srrs int fnd; 1143163953Srrs struct sockaddr_in6 *sin_a6; 1144163953Srrs struct sctp_inpcb *inp; 1145163953Srrs struct sctp_tcb *stcb; 1146163953Srrs struct sctp_nets *net; 1147163953Srrs 1148163953Srrs int error; 1149163953Srrs 1150163953Srrs /* 1151163953Srrs * Do the malloc first in case it blocks. 1152163953Srrs */ 1153163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1154163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 1155163953Srrs /* UDP type and listeners will drop out here */ 1156171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); 1157163953Srrs return (ENOTCONN); 1158163953Srrs } 1159163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1160163953Srrs sin6->sin6_family = AF_INET6; 1161163953Srrs sin6->sin6_len = sizeof(*sin6); 1162163953Srrs 1163163953Srrs /* We must recapture incase we blocked */ 1164163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1165163953Srrs if (inp == NULL) { 1166163953Srrs SCTP_FREE_SONAME(sin6); 1167171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1168163953Srrs return ECONNRESET; 1169163953Srrs } 1170163953Srrs SCTP_INP_RLOCK(inp); 1171163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1172169420Srrs if (stcb) { 1173163953Srrs SCTP_TCB_LOCK(stcb); 1174169420Srrs } 1175163953Srrs SCTP_INP_RUNLOCK(inp); 1176163953Srrs if (stcb == NULL) { 1177163953Srrs SCTP_FREE_SONAME(sin6); 1178171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1179163953Srrs return ECONNRESET; 1180163953Srrs } 1181163953Srrs fnd = 0; 1182163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1183163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1184163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1185163953Srrs fnd = 1; 1186163953Srrs sin6->sin6_port = stcb->rport; 1187163953Srrs sin6->sin6_addr = sin_a6->sin6_addr; 1188163953Srrs break; 1189163953Srrs } 1190163953Srrs } 1191163953Srrs SCTP_TCB_UNLOCK(stcb); 1192163953Srrs if (!fnd) { 1193163953Srrs /* No IPv4 address */ 1194163953Srrs SCTP_FREE_SONAME(sin6); 1195171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1196163953Srrs return ENOENT; 1197163953Srrs } 1198163953Srrs if ((error = sa6_recoverscope(sin6)) != 0) 1199163953Srrs return (error); 1200163953Srrs *addr = (struct sockaddr *)sin6; 1201163953Srrs return (0); 1202163953Srrs} 1203163953Srrs 1204163953Srrsstatic int 1205163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1206163953Srrs{ 1207163953Srrs struct sockaddr *addr; 1208163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1209166086Srrs int error; 1210163953Srrs 1211171943Srrs if (inp6 == NULL) { 1212171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1213163953Srrs return EINVAL; 1214171943Srrs } 1215163953Srrs /* allow v6 addresses precedence */ 1216163953Srrs error = sctp6_getaddr(so, nam); 1217163953Srrs if (error) { 1218163953Srrs /* try v4 next if v6 failed */ 1219163953Srrs error = sctp_ingetaddr(so, nam); 1220163953Srrs if (error) { 1221163953Srrs return (error); 1222163953Srrs } 1223163953Srrs addr = *nam; 1224163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1225166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1226163953Srrs struct sockaddr_in6 sin6; 1227163953Srrs 1228163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1229163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1230168709Srrs 1231163953Srrs } 1232163953Srrs } 1233163953Srrs return (error); 1234163953Srrs} 1235163953Srrs 1236163953Srrs 1237163953Srrsstatic int 1238163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1239163953Srrs{ 1240163953Srrs struct sockaddr *addr = *nam; 1241163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1242166086Srrs int error; 1243163953Srrs 1244171943Srrs if (inp6 == NULL) { 1245171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1246163953Srrs return EINVAL; 1247171943Srrs } 1248163953Srrs /* allow v6 addresses precedence */ 1249163953Srrs error = sctp6_peeraddr(so, nam); 1250163953Srrs if (error) { 1251163953Srrs /* try v4 next if v6 failed */ 1252163953Srrs error = sctp_peeraddr(so, nam); 1253163953Srrs if (error) { 1254163953Srrs return (error); 1255163953Srrs } 1256163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1257166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1258163953Srrs struct sockaddr_in6 sin6; 1259163953Srrs 1260163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1261163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1262163953Srrs } 1263163953Srrs } 1264163953Srrs return error; 1265163953Srrs} 1266163953Srrs 1267163953Srrsstruct pr_usrreqs sctp6_usrreqs = { 1268163953Srrs .pru_abort = sctp6_abort, 1269163953Srrs .pru_accept = sctp_accept, 1270163953Srrs .pru_attach = sctp6_attach, 1271163953Srrs .pru_bind = sctp6_bind, 1272163953Srrs .pru_connect = sctp6_connect, 1273163953Srrs .pru_control = in6_control, 1274163953Srrs .pru_close = sctp6_close, 1275163953Srrs .pru_detach = sctp6_close, 1276163953Srrs .pru_sopoll = sopoll_generic, 1277178201Srrs .pru_flush = sctp_flush, 1278163953Srrs .pru_disconnect = sctp6_disconnect, 1279163953Srrs .pru_listen = sctp_listen, 1280163953Srrs .pru_peeraddr = sctp6_getpeeraddr, 1281163953Srrs .pru_send = sctp6_send, 1282163953Srrs .pru_shutdown = sctp_shutdown, 1283163953Srrs .pru_sockaddr = sctp6_in6getaddr, 1284163953Srrs .pru_sosend = sctp_sosend, 1285163953Srrs .pru_soreceive = sctp_soreceive 1286163953Srrs}; 1287