sctp6_usrreq.c revision 218319
1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3218319Srrs * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved. 4218319Srrs * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved. 5163953Srrs * 6163953Srrs * Redistribution and use in source and binary forms, with or without 7163953Srrs * modification, are permitted provided that the following conditions are met: 8163953Srrs * 9163953Srrs * a) Redistributions of source code must retain the above copyright notice, 10163953Srrs * this list of conditions and the following disclaimer. 11163953Srrs * 12163953Srrs * b) Redistributions in binary form must reproduce the above copyright 13163953Srrs * notice, this list of conditions and the following disclaimer in 14163953Srrs * the documentation and/or other materials provided with the distribution. 15163953Srrs * 16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its 17163953Srrs * contributors may be used to endorse or promote products derived 18163953Srrs * from this software without specific prior written permission. 19163953Srrs * 20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE. 31163953Srrs */ 32163953Srrs/* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */ 33174510Sobrien 34163954Srrs#include <sys/cdefs.h> 35163953Srrs__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 218319 2011-02-05 12:12:51Z rrs $"); 36163953Srrs 37166086Srrs#include <netinet/sctp_os.h> 38163953Srrs#include <sys/proc.h> 39163953Srrs#include <netinet/sctp_pcb.h> 40163953Srrs#include <netinet/sctp_header.h> 41163953Srrs#include <netinet/sctp_var.h> 42168709Srrs#if defined(INET6) 43168709Srrs#include <netinet6/sctp6_var.h> 44168709Srrs#endif 45167598Srrs#include <netinet/sctp_sysctl.h> 46163953Srrs#include <netinet/sctp_output.h> 47166086Srrs#include <netinet/sctp_uio.h> 48163953Srrs#include <netinet/sctp_asconf.h> 49166086Srrs#include <netinet/sctputil.h> 50166086Srrs#include <netinet/sctp_indata.h> 51166086Srrs#include <netinet/sctp_timer.h> 52166086Srrs#include <netinet/sctp_auth.h> 53168709Srrs#include <netinet/sctp_input.h> 54168709Srrs#include <netinet/sctp_output.h> 55170091Srrs#include <netinet/sctp_bsd_addr.h> 56188067Srrs#include <netinet/sctp_crc32.h> 57179783Srrs#include <netinet/udp.h> 58163953Srrs 59171167Sgnn#ifdef IPSEC 60171133Sgnn#include <netipsec/ipsec.h> 61171133Sgnn#if defined(INET6) 62171133Sgnn#include <netipsec/ipsec6.h> 63171440Srrs#endif /* INET6 */ 64171440Srrs#endif /* IPSEC */ 65163953Srrs 66163953Srrsextern struct protosw inetsw[]; 67163953Srrs 68163953Srrsint 69171259Sdelphijsctp6_input(struct mbuf **i_pak, int *offp, int proto) 70163953Srrs{ 71165647Srrs struct mbuf *m; 72163953Srrs struct ip6_hdr *ip6; 73163953Srrs struct sctphdr *sh; 74163953Srrs struct sctp_inpcb *in6p = NULL; 75163953Srrs struct sctp_nets *net; 76163953Srrs int refcount_up = 0; 77170181Srrs uint32_t vrf_id = 0; 78212699Stuexen 79212699Stuexen#ifdef IPSEC 80163953Srrs struct inpcb *in6p_ip; 81212699Stuexen 82212699Stuexen#endif 83163953Srrs struct sctp_chunkhdr *ch; 84188067Srrs int length, offset, iphlen; 85168299Srrs uint8_t ecn_bits; 86163953Srrs struct sctp_tcb *stcb = NULL; 87170056Srrs int pkt_len = 0; 88211969Stuexen 89211969Stuexen#if !defined(SCTP_WITH_NO_CSUM) 90211969Stuexen uint32_t check, calc_check; 91211969Stuexen 92211969Stuexen#endif 93163953Srrs int off = *offp; 94179783Srrs uint16_t port = 0; 95163953Srrs 96169352Srrs /* get the VRF and table id's */ 97169352Srrs if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { 98169352Srrs SCTP_RELEASE_PKT(*i_pak); 99169352Srrs return (-1); 100169352Srrs } 101168709Srrs m = SCTP_HEADER_TO_CHAIN(*i_pak); 102170056Srrs pkt_len = SCTP_HEADER_LEN((*i_pak)); 103165647Srrs 104170091Srrs#ifdef SCTP_PACKET_LOGGING 105170091Srrs sctp_packet_log(m, pkt_len); 106170091Srrs#endif 107163953Srrs ip6 = mtod(m, struct ip6_hdr *); 108163953Srrs /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */ 109170056Srrs IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, 110170056Srrs (int)(sizeof(*sh) + sizeof(*ch))); 111163953Srrs if (sh == NULL) { 112163953Srrs SCTP_STAT_INCR(sctps_hdrops); 113163953Srrs return IPPROTO_DONE; 114163953Srrs } 115163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 116163953Srrs iphlen = off; 117163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 118170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 119170859Srrs "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen); 120163953Srrs 121170859Srrs 122163953Srrs#if defined(NFAITH) && NFAITH > 0 123163953Srrs 124163953Srrs if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) { 125163953Srrs /* XXX send icmp6 host/port unreach? */ 126163953Srrs goto bad; 127163953Srrs } 128163953Srrs#endif /* NFAITH defined and > 0 */ 129163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 130163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 131169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n", 132170056Srrs iphlen, pkt_len); 133163953Srrs if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 134163953Srrs /* No multi-cast support in SCTP */ 135163953Srrs goto bad; 136163953Srrs } 137163953Srrs /* destination port of 0 is illegal, based on RFC2960. */ 138163953Srrs if (sh->dest_port == 0) 139163953Srrs goto bad; 140188067Srrs 141188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 142188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 143188067Srrs m->m_pkthdr.len, 144188067Srrs if_name(m->m_pkthdr.rcvif), 145188067Srrs m->m_pkthdr.csum_flags); 146211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 147211969Stuexen SCTP_STAT_INCR(sctps_recvnocrc); 148211969Stuexen#else 149188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 150188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 151188067Srrs goto sctp_skip_csum; 152188067Srrs } 153171990Srrs check = sh->checksum; /* save incoming checksum */ 154179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 155171990Srrs (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) { 156188067Srrs SCTP_STAT_INCR(sctps_recvnocrc); 157171990Srrs goto sctp_skip_csum; 158171990Srrs } 159171990Srrs sh->checksum = 0; /* prepare for calc */ 160188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 161188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 162171990Srrs if (calc_check != check) { 163188067Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p phlen:%d\n", 164188067Srrs calc_check, check, m, iphlen); 165171990Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 166171990Srrs sh, ch, &in6p, &net, vrf_id); 167179783Srrs if ((net) && (port)) { 168179783Srrs if (net->port == 0) { 169179783Srrs sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr)); 170179783Srrs } 171179783Srrs net->port = port; 172179783Srrs } 173171990Srrs /* in6p's ref-count increased && stcb locked */ 174171990Srrs if ((in6p) && (stcb)) { 175171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 176172090Srrs sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 177171990Srrs } else if ((in6p != NULL) && (stcb == NULL)) { 178171990Srrs refcount_up = 1; 179163953Srrs } 180171990Srrs SCTP_STAT_INCR(sctps_badsum); 181171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 182171990Srrs goto bad; 183165647Srrs } 184171990Srrs sh->checksum = calc_check; 185171990Srrs 186163953Srrssctp_skip_csum: 187211969Stuexen#endif 188163953Srrs net = NULL; 189163953Srrs /* 190163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 191163953Srrs * IP/SCTP/first chunk header... 192163953Srrs */ 193163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 194168299Srrs sh, ch, &in6p, &net, vrf_id); 195179783Srrs if ((net) && (port)) { 196179783Srrs if (net->port == 0) { 197179783Srrs sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr)); 198179783Srrs } 199179783Srrs net->port = port; 200179783Srrs } 201163953Srrs /* in6p's ref-count increased */ 202163953Srrs if (in6p == NULL) { 203163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 204163953Srrs 205163953Srrs SCTP_STAT_INCR(sctps_noport); 206163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 207163953Srrs /* 208163953Srrs * we do a trick here to get the INIT tag, dig in 209163953Srrs * and get the tag from the INIT and put it in the 210163953Srrs * common header. 211163953Srrs */ 212163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 213163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 214168299Srrs (uint8_t *) & chunk_buf); 215169420Srrs if (init_chk) 216169420Srrs sh->v_tag = init_chk->init.initiate_tag; 217169420Srrs else 218169420Srrs sh->v_tag = 0; 219163953Srrs } 220165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 221179783Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 222165220Srrs goto bad; 223165220Srrs } 224165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 225165220Srrs goto bad; 226165220Srrs } 227165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 228179783Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 229163953Srrs goto bad; 230163953Srrs } else if (stcb == NULL) { 231163953Srrs refcount_up = 1; 232163953Srrs } 233171167Sgnn#ifdef IPSEC 234163953Srrs /* 235163953Srrs * Check AH/ESP integrity. 236163953Srrs */ 237212699Stuexen in6p_ip = (struct inpcb *)in6p; 238163996Srrs if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) { 239163953Srrs/* XXX */ 240197288Srrs MODULE_GLOBAL(ipsec6stat).in_polvio++; 241163953Srrs goto bad; 242163996Srrs } 243171440Srrs#endif /* IPSEC */ 244163953Srrs 245163953Srrs /* 246163953Srrs * CONTROL chunk processing 247163953Srrs */ 248163953Srrs offset -= sizeof(*ch); 249163953Srrs ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); 250165647Srrs 251165647Srrs /* Length now holds the total packet length payload + iphlen */ 252165647Srrs length = ntohs(ip6->ip6_plen) + iphlen; 253165647Srrs 254169655Srrs /* sa_ignore NO_NULL_CHK */ 255169378Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 256179783Srrs in6p, stcb, net, ecn_bits, vrf_id, port); 257163953Srrs /* inp's ref-count reduced && stcb unlocked */ 258163953Srrs /* XXX this stuff below gets moved to appropriate parts later... */ 259163953Srrs if (m) 260169352Srrs sctp_m_freem(m); 261163953Srrs if ((in6p) && refcount_up) { 262163953Srrs /* reduce ref-count */ 263163953Srrs SCTP_INP_WLOCK(in6p); 264163953Srrs SCTP_INP_DECR_REF(in6p); 265163953Srrs SCTP_INP_WUNLOCK(in6p); 266163953Srrs } 267163953Srrs return IPPROTO_DONE; 268163953Srrs 269163953Srrsbad: 270169420Srrs if (stcb) { 271163953Srrs SCTP_TCB_UNLOCK(stcb); 272169420Srrs } 273163953Srrs if ((in6p) && refcount_up) { 274163953Srrs /* reduce ref-count */ 275163953Srrs SCTP_INP_WLOCK(in6p); 276163953Srrs SCTP_INP_DECR_REF(in6p); 277163953Srrs SCTP_INP_WUNLOCK(in6p); 278163953Srrs } 279163953Srrs if (m) 280169352Srrs sctp_m_freem(m); 281163953Srrs return IPPROTO_DONE; 282163953Srrs} 283163953Srrs 284163953Srrs 285163953Srrsstatic void 286171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6, 287171259Sdelphij struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net) 288163953Srrs{ 289168299Srrs uint32_t nxtsz; 290163953Srrs 291163953Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 292163953Srrs (icmp6 == NULL) || (sh == NULL)) { 293163953Srrs goto out; 294163953Srrs } 295163953Srrs /* First do we even look at it? */ 296163953Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) 297163953Srrs goto out; 298163953Srrs 299163953Srrs if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) { 300163953Srrs /* not PACKET TO BIG */ 301163953Srrs goto out; 302163953Srrs } 303163953Srrs /* 304163953Srrs * ok we need to look closely. We could even get smarter and look at 305163953Srrs * anyone that we sent to in case we get a different ICMP that tells 306163953Srrs * us there is no way to reach a host, but for this impl, all we 307163953Srrs * care about is MTU discovery. 308163953Srrs */ 309163953Srrs nxtsz = ntohl(icmp6->icmp6_mtu); 310163953Srrs /* Stop any PMTU timer */ 311165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1); 312163953Srrs 313163953Srrs /* Adjust destination size limit */ 314163953Srrs if (net->mtu > nxtsz) { 315163953Srrs net->mtu = nxtsz; 316185694Srrs if (net->port) { 317185694Srrs net->mtu -= sizeof(struct udphdr); 318185694Srrs } 319163953Srrs } 320163953Srrs /* now what about the ep? */ 321163953Srrs if (stcb->asoc.smallest_mtu > nxtsz) { 322163953Srrs struct sctp_tmit_chunk *chk; 323163953Srrs 324163953Srrs /* Adjust that too */ 325163953Srrs stcb->asoc.smallest_mtu = nxtsz; 326163953Srrs /* now off to subtract IP_DF flag if needed */ 327163953Srrs 328163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 329168299Srrs if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 330163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 331163953Srrs } 332163953Srrs } 333163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 334168299Srrs if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 335163953Srrs /* 336163953Srrs * For this guy we also mark for immediate 337163953Srrs * resend since we sent to big of chunk 338163953Srrs */ 339163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 340163953Srrs if (chk->sent != SCTP_DATAGRAM_RESEND) 341163953Srrs stcb->asoc.sent_queue_retran_cnt++; 342163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 343163953Srrs chk->rec.data.doing_fast_retransmit = 0; 344163953Srrs 345163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 346163953Srrs /* Clear any time so NO RTT is being done */ 347163953Srrs chk->sent_rcv_time.tv_sec = 0; 348163953Srrs chk->sent_rcv_time.tv_usec = 0; 349163953Srrs stcb->asoc.total_flight -= chk->send_size; 350163953Srrs net->flight_size -= chk->send_size; 351163953Srrs } 352163953Srrs } 353163953Srrs } 354163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 355163953Srrsout: 356169420Srrs if (stcb) { 357163953Srrs SCTP_TCB_UNLOCK(stcb); 358169420Srrs } 359163953Srrs} 360163953Srrs 361163953Srrs 362172156Srrsvoid 363172091Srrssctp6_notify(struct sctp_inpcb *inp, 364172091Srrs struct icmp6_hdr *icmph, 365172091Srrs struct sctphdr *sh, 366172091Srrs struct sockaddr *to, 367172091Srrs struct sctp_tcb *stcb, 368172091Srrs struct sctp_nets *net) 369172091Srrs{ 370172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 371172091Srrs struct socket *so; 372172091Srrs 373172091Srrs#endif 374172091Srrs /* protection */ 375172091Srrs int reason; 376172091Srrs 377172091Srrs 378172091Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 379172091Srrs (sh == NULL) || (to == NULL)) { 380172091Srrs if (stcb) 381172091Srrs SCTP_TCB_UNLOCK(stcb); 382172091Srrs return; 383172091Srrs } 384172091Srrs /* First job is to verify the vtag matches what I would send */ 385172091Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 386172091Srrs SCTP_TCB_UNLOCK(stcb); 387172091Srrs return; 388172091Srrs } 389172091Srrs if (icmph->icmp6_type != ICMP_UNREACH) { 390172091Srrs /* We only care about unreachable */ 391172091Srrs SCTP_TCB_UNLOCK(stcb); 392172091Srrs return; 393172091Srrs } 394172091Srrs if ((icmph->icmp6_code == ICMP_UNREACH_NET) || 395172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST) || 396172091Srrs (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) || 397172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) || 398172091Srrs (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) || 399172091Srrs (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) || 400172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) || 401172091Srrs (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) { 402172091Srrs 403172091Srrs /* 404172091Srrs * Hmm reachablity problems we must examine closely. If its 405172091Srrs * not reachable, we may have lost a network. Or if there is 406172091Srrs * NO protocol at the other end named SCTP. well we consider 407172091Srrs * it a OOTB abort. 408172091Srrs */ 409172091Srrs if (net->dest_state & SCTP_ADDR_REACHABLE) { 410172091Srrs /* Ok that destination is NOT reachable */ 411172091Srrs SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n", 412172091Srrs net->error_count, 413172091Srrs net->failure_threshold, 414172091Srrs net); 415172091Srrs 416172091Srrs net->dest_state &= ~SCTP_ADDR_REACHABLE; 417172091Srrs net->dest_state |= SCTP_ADDR_NOT_REACHABLE; 418172091Srrs /* 419172091Srrs * JRS 5/14/07 - If a destination is unreachable, 420172091Srrs * the PF bit is turned off. This allows an 421172091Srrs * unambiguous use of the PF bit for destinations 422172091Srrs * that are reachable but potentially failed. If the 423172091Srrs * destination is set to the unreachable state, also 424172091Srrs * set the destination to the PF state. 425172091Srrs */ 426172091Srrs /* 427172091Srrs * Add debug message here if destination is not in 428172091Srrs * PF state. 429172091Srrs */ 430172091Srrs /* Stop any running T3 timers here? */ 431216669Stuexen if ((stcb->asoc.sctp_cmt_on_off > 0) && 432211944Stuexen (stcb->asoc.sctp_cmt_pf > 0)) { 433172091Srrs net->dest_state &= ~SCTP_ADDR_PF; 434172091Srrs SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n", 435172091Srrs net); 436172091Srrs } 437172091Srrs net->error_count = net->failure_threshold + 1; 438172091Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 439172091Srrs stcb, SCTP_FAILED_THRESHOLD, 440172091Srrs (void *)net, SCTP_SO_NOT_LOCKED); 441172091Srrs } 442172091Srrs SCTP_TCB_UNLOCK(stcb); 443172091Srrs } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) || 444172091Srrs (icmph->icmp6_code == ICMP_UNREACH_PORT)) { 445172091Srrs /* 446172091Srrs * Here the peer is either playing tricks on us, including 447172091Srrs * an address that belongs to someone who does not support 448172091Srrs * SCTP OR was a userland implementation that shutdown and 449172091Srrs * now is dead. In either case treat it like a OOTB abort 450172091Srrs * with no TCB 451172091Srrs */ 452172091Srrs reason = SCTP_PEER_FAULTY; 453172091Srrs sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED); 454172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 455172091Srrs so = SCTP_INP_SO(inp); 456172091Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 457172091Srrs SCTP_TCB_UNLOCK(stcb); 458172091Srrs SCTP_SOCKET_LOCK(so, 1); 459172091Srrs SCTP_TCB_LOCK(stcb); 460172091Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 461172091Srrs#endif 462172091Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 463172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 464172091Srrs SCTP_SOCKET_UNLOCK(so, 1); 465172091Srrs /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */ 466172091Srrs#endif 467172091Srrs /* no need to unlock here, since the TCB is gone */ 468172091Srrs } else { 469172091Srrs SCTP_TCB_UNLOCK(stcb); 470172091Srrs } 471172091Srrs} 472172091Srrs 473172091Srrs 474172091Srrs 475163953Srrsvoid 476171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d) 477163953Srrs{ 478163953Srrs struct sctphdr sh; 479163953Srrs struct ip6ctlparam *ip6cp = NULL; 480167598Srrs uint32_t vrf_id; 481163953Srrs 482167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 483167598Srrs 484163953Srrs if (pktdst->sa_family != AF_INET6 || 485163953Srrs pktdst->sa_len != sizeof(struct sockaddr_in6)) 486163953Srrs return; 487163953Srrs 488163953Srrs if ((unsigned)cmd >= PRC_NCMDS) 489163953Srrs return; 490163953Srrs if (PRC_IS_REDIRECT(cmd)) { 491163953Srrs d = NULL; 492163953Srrs } else if (inet6ctlerrmap[cmd] == 0) { 493163953Srrs return; 494163953Srrs } 495163953Srrs /* if the parameter is from icmp6, decode it. */ 496163953Srrs if (d != NULL) { 497163953Srrs ip6cp = (struct ip6ctlparam *)d; 498163953Srrs } else { 499163953Srrs ip6cp = (struct ip6ctlparam *)NULL; 500163953Srrs } 501163953Srrs 502163953Srrs if (ip6cp) { 503163953Srrs /* 504163953Srrs * XXX: We assume that when IPV6 is non NULL, M and OFF are 505163953Srrs * valid. 506163953Srrs */ 507163953Srrs /* check if we can safely examine src and dst ports */ 508163953Srrs struct sctp_inpcb *inp = NULL; 509163953Srrs struct sctp_tcb *stcb = NULL; 510163953Srrs struct sctp_nets *net = NULL; 511163953Srrs struct sockaddr_in6 final; 512163953Srrs 513165647Srrs if (ip6cp->ip6c_m == NULL) 514163953Srrs return; 515163953Srrs 516163953Srrs bzero(&sh, sizeof(sh)); 517163953Srrs bzero(&final, sizeof(final)); 518163953Srrs inp = NULL; 519163953Srrs net = NULL; 520163953Srrs m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), 521163953Srrs (caddr_t)&sh); 522163953Srrs ip6cp->ip6c_src->sin6_port = sh.src_port; 523163953Srrs final.sin6_len = sizeof(final); 524163953Srrs final.sin6_family = AF_INET6; 525163953Srrs final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; 526163953Srrs final.sin6_port = sh.dest_port; 527163953Srrs stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src, 528163953Srrs (struct sockaddr *)&final, 529167598Srrs &inp, &net, 1, vrf_id); 530163953Srrs /* inp's ref-count increased && stcb locked */ 531163953Srrs if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 532163953Srrs if (cmd == PRC_MSGSIZE) { 533163953Srrs sctp6_notify_mbuf(inp, 534163953Srrs ip6cp->ip6c_icmp6, 535163953Srrs &sh, 536163953Srrs stcb, 537163953Srrs net); 538163953Srrs /* inp's ref-count reduced && stcb unlocked */ 539163953Srrs } else { 540172091Srrs sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh, 541163953Srrs (struct sockaddr *)&final, 542163953Srrs stcb, net); 543163953Srrs /* inp's ref-count reduced && stcb unlocked */ 544163953Srrs } 545163953Srrs } else { 546163953Srrs if (PRC_IS_REDIRECT(cmd) && inp) { 547163953Srrs in6_rtchange((struct in6pcb *)inp, 548163953Srrs inet6ctlerrmap[cmd]); 549163953Srrs } 550163953Srrs if (inp) { 551163953Srrs /* reduce inp's ref-count */ 552163953Srrs SCTP_INP_WLOCK(inp); 553163953Srrs SCTP_INP_DECR_REF(inp); 554163953Srrs SCTP_INP_WUNLOCK(inp); 555163953Srrs } 556163953Srrs if (stcb) 557163953Srrs SCTP_TCB_UNLOCK(stcb); 558163953Srrs } 559163953Srrs } 560163953Srrs} 561163953Srrs 562163953Srrs/* 563163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c 564163953Srrs * since they do the same thing and now we lookup with a sockaddr 565163953Srrs */ 566163953Srrsstatic int 567163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS) 568163953Srrs{ 569164085Srrs struct xucred xuc; 570163953Srrs struct sockaddr_in6 addrs[2]; 571163953Srrs struct sctp_inpcb *inp; 572163953Srrs struct sctp_nets *net; 573163953Srrs struct sctp_tcb *stcb; 574164085Srrs int error; 575167598Srrs uint32_t vrf_id; 576163953Srrs 577167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 578167598Srrs 579170587Srwatson error = priv_check(req->td, PRIV_NETINET_GETCRED); 580163953Srrs if (error) 581163953Srrs return (error); 582163953Srrs 583171943Srrs if (req->newlen != sizeof(addrs)) { 584171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 585163953Srrs return (EINVAL); 586171943Srrs } 587171943Srrs if (req->oldlen != sizeof(struct ucred)) { 588171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 589163953Srrs return (EINVAL); 590171943Srrs } 591163953Srrs error = SYSCTL_IN(req, addrs, sizeof(addrs)); 592163953Srrs if (error) 593163953Srrs return (error); 594163953Srrs 595163953Srrs stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]), 596163953Srrs sin6tosa(&addrs[1]), 597167598Srrs &inp, &net, 1, vrf_id); 598163953Srrs if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 599164085Srrs if ((inp != NULL) && (stcb == NULL)) { 600164085Srrs /* reduce ref-count */ 601163953Srrs SCTP_INP_WLOCK(inp); 602163953Srrs SCTP_INP_DECR_REF(inp); 603164085Srrs goto cred_can_cont; 604163953Srrs } 605171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 606164085Srrs error = ENOENT; 607163953Srrs goto out; 608163953Srrs } 609163953Srrs SCTP_TCB_UNLOCK(stcb); 610164085Srrs /* 611164085Srrs * We use the write lock here, only since in the error leg we need 612164085Srrs * it. If we used RLOCK, then we would have to 613164085Srrs * wlock/decr/unlock/rlock. Which in theory could create a hole. 614164085Srrs * Better to use higher wlock. 615164085Srrs */ 616164085Srrs SCTP_INP_WLOCK(inp); 617164085Srrscred_can_cont: 618164085Srrs error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 619164085Srrs if (error) { 620164085Srrs SCTP_INP_WUNLOCK(inp); 621164085Srrs goto out; 622164085Srrs } 623164085Srrs cru2x(inp->sctp_socket->so_cred, &xuc); 624164085Srrs SCTP_INP_WUNLOCK(inp); 625164085Srrs error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 626163953Srrsout: 627163953Srrs return (error); 628163953Srrs} 629163953Srrs 630163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 631163953Srrs 0, 0, 632163953Srrs sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 633163953Srrs 634163953Srrs 635163953Srrs/* This is the same as the sctp_abort() could be made common */ 636163953Srrsstatic void 637163953Srrssctp6_abort(struct socket *so) 638163953Srrs{ 639163953Srrs struct sctp_inpcb *inp; 640163953Srrs uint32_t flags; 641163953Srrs 642163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 643172091Srrs if (inp == 0) { 644172091Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 645163953Srrs return; 646172091Srrs } 647163953Srrssctp_must_try_again: 648163953Srrs flags = inp->sctp_flags; 649163953Srrs#ifdef SCTP_LOG_CLOSING 650163953Srrs sctp_log_closing(inp, NULL, 17); 651163953Srrs#endif 652163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 653163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 654163953Srrs#ifdef SCTP_LOG_CLOSING 655163953Srrs sctp_log_closing(inp, NULL, 16); 656163953Srrs#endif 657169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 658169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 659163953Srrs SOCK_LOCK(so); 660167695Srrs SCTP_SB_CLEAR(so->so_snd); 661163953Srrs /* 662163953Srrs * same for the rcv ones, they are only here for the 663163953Srrs * accounting/select. 664163953Srrs */ 665167695Srrs SCTP_SB_CLEAR(so->so_rcv); 666167695Srrs /* Now null out the reference, we are completely detached. */ 667163953Srrs so->so_pcb = NULL; 668163953Srrs SOCK_UNLOCK(so); 669163953Srrs } else { 670163953Srrs flags = inp->sctp_flags; 671163953Srrs if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 672163953Srrs goto sctp_must_try_again; 673163953Srrs } 674163953Srrs } 675163953Srrs return; 676163953Srrs} 677163953Srrs 678163953Srrsstatic int 679163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p) 680163953Srrs{ 681163953Srrs struct in6pcb *inp6; 682166086Srrs int error; 683163953Srrs struct sctp_inpcb *inp; 684170205Srrs uint32_t vrf_id = SCTP_DEFAULT_VRFID; 685163953Srrs 686163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 687171943Srrs if (inp != NULL) { 688171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 689163953Srrs return EINVAL; 690171943Srrs } 691163953Srrs if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 692179783Srrs error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 693163953Srrs if (error) 694163953Srrs return error; 695163953Srrs } 696170205Srrs error = sctp_inpcb_alloc(so, vrf_id); 697163953Srrs if (error) 698163953Srrs return error; 699163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 700170205Srrs SCTP_INP_WLOCK(inp); 701163953Srrs inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 702163953Srrs inp6 = (struct in6pcb *)inp; 703163953Srrs 704163953Srrs inp6->inp_vflag |= INP_IPV6; 705163953Srrs inp6->in6p_hops = -1; /* use kernel default */ 706163953Srrs inp6->in6p_cksum = -1; /* just to be sure */ 707163953Srrs#ifdef INET 708163953Srrs /* 709163953Srrs * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 710163953Srrs * socket as well, because the socket may be bound to an IPv6 711163953Srrs * wildcard address, which may match an IPv4-mapped IPv6 address. 712163953Srrs */ 713197288Srrs inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 714163953Srrs#endif 715163953Srrs /* 716163953Srrs * Hmm what about the IPSEC stuff that is missing here but in 717163953Srrs * sctp_attach()? 718163953Srrs */ 719170205Srrs SCTP_INP_WUNLOCK(inp); 720163953Srrs return 0; 721163953Srrs} 722163953Srrs 723163953Srrsstatic int 724163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 725163953Srrs{ 726163953Srrs struct sctp_inpcb *inp; 727163953Srrs struct in6pcb *inp6; 728166086Srrs int error; 729163953Srrs 730163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 731171943Srrs if (inp == 0) { 732171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 733163953Srrs return EINVAL; 734171943Srrs } 735170056Srrs if (addr) { 736170056Srrs if ((addr->sa_family == AF_INET6) && 737170056Srrs (addr->sa_len != sizeof(struct sockaddr_in6))) { 738171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 739170056Srrs return EINVAL; 740170056Srrs } 741170056Srrs if ((addr->sa_family == AF_INET) && 742170056Srrs (addr->sa_len != sizeof(struct sockaddr_in))) { 743171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 744170056Srrs return EINVAL; 745170056Srrs } 746170056Srrs } 747163953Srrs inp6 = (struct in6pcb *)inp; 748163953Srrs inp6->inp_vflag &= ~INP_IPV4; 749163953Srrs inp6->inp_vflag |= INP_IPV6; 750166023Srrs if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { 751163953Srrs if (addr->sa_family == AF_INET) { 752163953Srrs /* binding v4 addr to v6 socket, so reset flags */ 753163953Srrs inp6->inp_vflag |= INP_IPV4; 754163953Srrs inp6->inp_vflag &= ~INP_IPV6; 755163953Srrs } else { 756163953Srrs struct sockaddr_in6 *sin6_p; 757163953Srrs 758163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 759163953Srrs 760163953Srrs if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 761163953Srrs inp6->inp_vflag |= INP_IPV4; 762163953Srrs } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 763163953Srrs struct sockaddr_in sin; 764163953Srrs 765163953Srrs in6_sin6_2_sin(&sin, sin6_p); 766163953Srrs inp6->inp_vflag |= INP_IPV4; 767163953Srrs inp6->inp_vflag &= ~INP_IPV6; 768171572Srrs error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); 769163953Srrs return error; 770163953Srrs } 771163953Srrs } 772163953Srrs } else if (addr != NULL) { 773163953Srrs /* IPV6_V6ONLY socket */ 774163953Srrs if (addr->sa_family == AF_INET) { 775163953Srrs /* can't bind v4 addr to v6 only socket! */ 776171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 777163953Srrs return EINVAL; 778163953Srrs } else { 779163953Srrs struct sockaddr_in6 *sin6_p; 780163953Srrs 781163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 782163953Srrs 783172091Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 784163953Srrs /* can't bind v4-mapped addrs either! */ 785163953Srrs /* NOTE: we don't support SIIT */ 786171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 787172091Srrs return EINVAL; 788172091Srrs } 789163953Srrs } 790163953Srrs } 791171572Srrs error = sctp_inpcb_bind(so, addr, NULL, p); 792163953Srrs return error; 793163953Srrs} 794163953Srrs 795163953Srrs 796163953Srrsstatic void 797163953Srrssctp6_close(struct socket *so) 798163953Srrs{ 799171990Srrs sctp_close(so); 800163953Srrs} 801163953Srrs 802167598Srrs/* This could be made common with sctp_detach() since they are identical */ 803163953Srrs 804168709Srrsstatic 805168709Srrsint 806163953Srrssctp6_disconnect(struct socket *so) 807163953Srrs{ 808171990Srrs return (sctp_disconnect(so)); 809163953Srrs} 810163953Srrs 811168709Srrs 812163953Srrsint 813163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 814163953Srrs struct mbuf *control, struct thread *p); 815163953Srrs 816163953Srrs 817163953Srrsstatic int 818163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 819163953Srrs struct mbuf *control, struct thread *p) 820163953Srrs{ 821163953Srrs struct sctp_inpcb *inp; 822163953Srrs struct in6pcb *inp6; 823163953Srrs 824163953Srrs#ifdef INET 825163953Srrs struct sockaddr_in6 *sin6; 826163953Srrs 827163953Srrs#endif /* INET */ 828163953Srrs /* No SPL needed since sctp_output does this */ 829163953Srrs 830163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 831163953Srrs if (inp == NULL) { 832163953Srrs if (control) { 833169352Srrs SCTP_RELEASE_PKT(control); 834163953Srrs control = NULL; 835163953Srrs } 836169352Srrs SCTP_RELEASE_PKT(m); 837171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 838163953Srrs return EINVAL; 839163953Srrs } 840163953Srrs inp6 = (struct in6pcb *)inp; 841163953Srrs /* 842163953Srrs * For the TCP model we may get a NULL addr, if we are a connected 843163953Srrs * socket thats ok. 844163953Srrs */ 845163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 846163953Srrs (addr == NULL)) { 847163953Srrs goto connected_type; 848163953Srrs } 849163953Srrs if (addr == NULL) { 850169352Srrs SCTP_RELEASE_PKT(m); 851163953Srrs if (control) { 852169352Srrs SCTP_RELEASE_PKT(control); 853163953Srrs control = NULL; 854163953Srrs } 855171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); 856163953Srrs return (EDESTADDRREQ); 857163953Srrs } 858163953Srrs#ifdef INET 859163953Srrs sin6 = (struct sockaddr_in6 *)addr; 860166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 861163953Srrs /* 862163953Srrs * if IPV6_V6ONLY flag, we discard datagrams destined to a 863163953Srrs * v4 addr or v4-mapped addr 864163953Srrs */ 865163953Srrs if (addr->sa_family == AF_INET) { 866171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 867163953Srrs return EINVAL; 868163953Srrs } 869163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 870171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 871163953Srrs return EINVAL; 872163953Srrs } 873163953Srrs } 874163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 875197288Srrs if (!MODULE_GLOBAL(ip6_v6only)) { 876163953Srrs struct sockaddr_in sin; 877163953Srrs 878163953Srrs /* convert v4-mapped into v4 addr and send */ 879163953Srrs in6_sin6_2_sin(&sin, sin6); 880163953Srrs return sctp_sendm(so, flags, m, (struct sockaddr *)&sin, 881163953Srrs control, p); 882163953Srrs } else { 883163953Srrs /* mapped addresses aren't enabled */ 884171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 885163953Srrs return EINVAL; 886163953Srrs } 887163953Srrs } 888163953Srrs#endif /* INET */ 889163953Srrsconnected_type: 890163953Srrs /* now what about control */ 891163953Srrs if (control) { 892163953Srrs if (inp->control) { 893169420Srrs SCTP_PRINTF("huh? control set?\n"); 894169352Srrs SCTP_RELEASE_PKT(inp->control); 895163953Srrs inp->control = NULL; 896163953Srrs } 897163953Srrs inp->control = control; 898163953Srrs } 899163953Srrs /* Place the data */ 900163953Srrs if (inp->pkt) { 901165647Srrs SCTP_BUF_NEXT(inp->pkt_last) = m; 902163953Srrs inp->pkt_last = m; 903163953Srrs } else { 904163953Srrs inp->pkt_last = inp->pkt = m; 905163953Srrs } 906163953Srrs if ( 907163953Srrs /* FreeBSD and MacOSX uses a flag passed */ 908163953Srrs ((flags & PRUS_MORETOCOME) == 0) 909163953Srrs ) { 910163953Srrs /* 911163953Srrs * note with the current version this code will only be used 912163953Srrs * by OpenBSD, NetBSD and FreeBSD have methods for 913163953Srrs * re-defining sosend() to use sctp_sosend(). One can 914163953Srrs * optionaly switch back to this code (by changing back the 915163953Srrs * defininitions but this is not advisable. 916163953Srrs */ 917163953Srrs int ret; 918163953Srrs 919163953Srrs ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 920163953Srrs inp->pkt = NULL; 921163953Srrs inp->control = NULL; 922163953Srrs return (ret); 923163953Srrs } else { 924163953Srrs return (0); 925163953Srrs } 926163953Srrs} 927163953Srrs 928163953Srrsstatic int 929163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 930163953Srrs{ 931167598Srrs uint32_t vrf_id; 932163953Srrs int error = 0; 933163953Srrs struct sctp_inpcb *inp; 934163953Srrs struct in6pcb *inp6; 935163953Srrs struct sctp_tcb *stcb; 936163953Srrs 937163953Srrs#ifdef INET 938163953Srrs struct sockaddr_in6 *sin6; 939163953Srrs struct sockaddr_storage ss; 940163953Srrs 941163953Srrs#endif /* INET */ 942163953Srrs 943163953Srrs inp6 = (struct in6pcb *)so->so_pcb; 944163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 945163953Srrs if (inp == 0) { 946171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 947163953Srrs return (ECONNRESET); /* I made the same as TCP since we are 948163953Srrs * not setup? */ 949163953Srrs } 950170056Srrs if (addr == NULL) { 951171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 952170056Srrs return (EINVAL); 953170056Srrs } 954170056Srrs if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) { 955171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 956170056Srrs return (EINVAL); 957170056Srrs } 958170056Srrs if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) { 959171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 960170056Srrs return (EINVAL); 961170056Srrs } 962168299Srrs vrf_id = inp->def_vrf_id; 963163953Srrs SCTP_ASOC_CREATE_LOCK(inp); 964163953Srrs SCTP_INP_RLOCK(inp); 965163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 966163953Srrs SCTP_PCB_FLAGS_UNBOUND) { 967163953Srrs /* Bind a ephemeral port */ 968163953Srrs SCTP_INP_RUNLOCK(inp); 969163953Srrs error = sctp6_bind(so, NULL, p); 970163953Srrs if (error) { 971163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 972163953Srrs 973163953Srrs return (error); 974163953Srrs } 975163953Srrs SCTP_INP_RLOCK(inp); 976163953Srrs } 977163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 978163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 979163953Srrs /* We are already connected AND the TCP model */ 980163953Srrs SCTP_INP_RUNLOCK(inp); 981163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 982171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); 983163953Srrs return (EADDRINUSE); 984163953Srrs } 985163953Srrs#ifdef INET 986163953Srrs sin6 = (struct sockaddr_in6 *)addr; 987166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 988163953Srrs /* 989163953Srrs * if IPV6_V6ONLY flag, ignore connections destined to a v4 990163953Srrs * addr or v4-mapped addr 991163953Srrs */ 992163953Srrs if (addr->sa_family == AF_INET) { 993163953Srrs SCTP_INP_RUNLOCK(inp); 994163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 995171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 996163953Srrs return EINVAL; 997163953Srrs } 998163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 999163953Srrs SCTP_INP_RUNLOCK(inp); 1000163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1001171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1002163953Srrs return EINVAL; 1003163953Srrs } 1004163953Srrs } 1005163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 1006197288Srrs if (!MODULE_GLOBAL(ip6_v6only)) { 1007163953Srrs /* convert v4-mapped into v4 addr */ 1008163953Srrs in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); 1009163953Srrs addr = (struct sockaddr *)&ss; 1010163953Srrs } else { 1011163953Srrs /* mapped addresses aren't enabled */ 1012163953Srrs SCTP_INP_RUNLOCK(inp); 1013163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1014171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1015163953Srrs return EINVAL; 1016163953Srrs } 1017163953Srrs } else 1018163953Srrs#endif /* INET */ 1019163953Srrs addr = addr; /* for true v6 address case */ 1020163953Srrs 1021163953Srrs /* Now do we connect? */ 1022163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1023163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1024169420Srrs if (stcb) { 1025163953Srrs SCTP_TCB_UNLOCK(stcb); 1026169420Srrs } 1027163953Srrs SCTP_INP_RUNLOCK(inp); 1028163953Srrs } else { 1029163953Srrs SCTP_INP_RUNLOCK(inp); 1030163953Srrs SCTP_INP_WLOCK(inp); 1031163953Srrs SCTP_INP_INCR_REF(inp); 1032163953Srrs SCTP_INP_WUNLOCK(inp); 1033163953Srrs stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 1034163953Srrs if (stcb == NULL) { 1035163953Srrs SCTP_INP_WLOCK(inp); 1036163953Srrs SCTP_INP_DECR_REF(inp); 1037163953Srrs SCTP_INP_WUNLOCK(inp); 1038163953Srrs } 1039163953Srrs } 1040163953Srrs 1041163953Srrs if (stcb != NULL) { 1042163953Srrs /* Already have or am bring up an association */ 1043163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1044163953Srrs SCTP_TCB_UNLOCK(stcb); 1045171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); 1046163953Srrs return (EALREADY); 1047163953Srrs } 1048163953Srrs /* We are GOOD to go */ 1049206137Stuexen stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p); 1050163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1051163953Srrs if (stcb == NULL) { 1052163953Srrs /* Gak! no memory */ 1053163953Srrs return (error); 1054163953Srrs } 1055163953Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1056163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1057163953Srrs /* Set the connected flag so we can queue data */ 1058163953Srrs soisconnecting(so); 1059163953Srrs } 1060163953Srrs stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1061169420Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1062163953Srrs 1063163953Srrs /* initialize authentication parameters for the assoc */ 1064163953Srrs sctp_initialize_auth_params(inp, stcb); 1065163953Srrs 1066172090Srrs sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 1067163953Srrs SCTP_TCB_UNLOCK(stcb); 1068163953Srrs return error; 1069163953Srrs} 1070163953Srrs 1071163953Srrsstatic int 1072163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr) 1073163953Srrs{ 1074163953Srrs struct sockaddr_in6 *sin6; 1075163953Srrs struct sctp_inpcb *inp; 1076167598Srrs uint32_t vrf_id; 1077167598Srrs struct sctp_ifa *sctp_ifa; 1078163953Srrs 1079163953Srrs int error; 1080163953Srrs 1081163953Srrs /* 1082163953Srrs * Do the malloc first in case it blocks. 1083163953Srrs */ 1084163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1085211944Stuexen if (sin6 == NULL) 1086211944Stuexen return ENOMEM; 1087163953Srrs sin6->sin6_family = AF_INET6; 1088163953Srrs sin6->sin6_len = sizeof(*sin6); 1089163953Srrs 1090163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1091163953Srrs if (inp == NULL) { 1092163953Srrs SCTP_FREE_SONAME(sin6); 1093171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1094163953Srrs return ECONNRESET; 1095163953Srrs } 1096163953Srrs SCTP_INP_RLOCK(inp); 1097163953Srrs sin6->sin6_port = inp->sctp_lport; 1098163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1099163953Srrs /* For the bound all case you get back 0 */ 1100163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1101163953Srrs struct sctp_tcb *stcb; 1102163953Srrs struct sockaddr_in6 *sin_a6; 1103163953Srrs struct sctp_nets *net; 1104163953Srrs int fnd; 1105163953Srrs 1106163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1107163953Srrs if (stcb == NULL) { 1108163953Srrs goto notConn6; 1109163953Srrs } 1110163953Srrs fnd = 0; 1111163953Srrs sin_a6 = NULL; 1112163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1113163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1114164085Srrs if (sin_a6 == NULL) 1115164085Srrs /* this will make coverity happy */ 1116164085Srrs continue; 1117164085Srrs 1118163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1119163953Srrs fnd = 1; 1120163953Srrs break; 1121163953Srrs } 1122163953Srrs } 1123163953Srrs if ((!fnd) || (sin_a6 == NULL)) { 1124163953Srrs /* punt */ 1125163953Srrs goto notConn6; 1126163953Srrs } 1127168299Srrs vrf_id = inp->def_vrf_id; 1128168299Srrs sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id); 1129167598Srrs if (sctp_ifa) { 1130167598Srrs sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 1131167598Srrs } 1132163953Srrs } else { 1133163953Srrs /* For the bound all case you get back 0 */ 1134163953Srrs notConn6: 1135163953Srrs memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1136163953Srrs } 1137163953Srrs } else { 1138163953Srrs /* Take the first IPv6 address in the list */ 1139163953Srrs struct sctp_laddr *laddr; 1140163953Srrs int fnd = 0; 1141163953Srrs 1142163953Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1143167598Srrs if (laddr->ifa->address.sa.sa_family == AF_INET6) { 1144163953Srrs struct sockaddr_in6 *sin_a; 1145163953Srrs 1146167598Srrs sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; 1147163953Srrs sin6->sin6_addr = sin_a->sin6_addr; 1148163953Srrs fnd = 1; 1149163953Srrs break; 1150163953Srrs } 1151163953Srrs } 1152163953Srrs if (!fnd) { 1153163953Srrs SCTP_FREE_SONAME(sin6); 1154163953Srrs SCTP_INP_RUNLOCK(inp); 1155171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1156163953Srrs return ENOENT; 1157163953Srrs } 1158163953Srrs } 1159163953Srrs SCTP_INP_RUNLOCK(inp); 1160163953Srrs /* Scoping things for v6 */ 1161164085Srrs if ((error = sa6_recoverscope(sin6)) != 0) { 1162164085Srrs SCTP_FREE_SONAME(sin6); 1163163953Srrs return (error); 1164164085Srrs } 1165163953Srrs (*addr) = (struct sockaddr *)sin6; 1166163953Srrs return (0); 1167163953Srrs} 1168163953Srrs 1169163953Srrsstatic int 1170163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1171163953Srrs{ 1172163953Srrs struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr; 1173163953Srrs int fnd; 1174163953Srrs struct sockaddr_in6 *sin_a6; 1175163953Srrs struct sctp_inpcb *inp; 1176163953Srrs struct sctp_tcb *stcb; 1177163953Srrs struct sctp_nets *net; 1178163953Srrs 1179163953Srrs int error; 1180163953Srrs 1181163953Srrs /* 1182163953Srrs * Do the malloc first in case it blocks. 1183163953Srrs */ 1184163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1185163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 1186163953Srrs /* UDP type and listeners will drop out here */ 1187171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); 1188163953Srrs return (ENOTCONN); 1189163953Srrs } 1190163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1191211944Stuexen if (sin6 == NULL) 1192211944Stuexen return (ENOMEM); 1193163953Srrs sin6->sin6_family = AF_INET6; 1194163953Srrs sin6->sin6_len = sizeof(*sin6); 1195163953Srrs 1196163953Srrs /* We must recapture incase we blocked */ 1197163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1198163953Srrs if (inp == NULL) { 1199163953Srrs SCTP_FREE_SONAME(sin6); 1200171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1201163953Srrs return ECONNRESET; 1202163953Srrs } 1203163953Srrs SCTP_INP_RLOCK(inp); 1204163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1205169420Srrs if (stcb) { 1206163953Srrs SCTP_TCB_LOCK(stcb); 1207169420Srrs } 1208163953Srrs SCTP_INP_RUNLOCK(inp); 1209163953Srrs if (stcb == NULL) { 1210163953Srrs SCTP_FREE_SONAME(sin6); 1211171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1212163953Srrs return ECONNRESET; 1213163953Srrs } 1214163953Srrs fnd = 0; 1215163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1216163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1217163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1218163953Srrs fnd = 1; 1219163953Srrs sin6->sin6_port = stcb->rport; 1220163953Srrs sin6->sin6_addr = sin_a6->sin6_addr; 1221163953Srrs break; 1222163953Srrs } 1223163953Srrs } 1224163953Srrs SCTP_TCB_UNLOCK(stcb); 1225163953Srrs if (!fnd) { 1226163953Srrs /* No IPv4 address */ 1227163953Srrs SCTP_FREE_SONAME(sin6); 1228171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1229163953Srrs return ENOENT; 1230163953Srrs } 1231163953Srrs if ((error = sa6_recoverscope(sin6)) != 0) 1232163953Srrs return (error); 1233163953Srrs *addr = (struct sockaddr *)sin6; 1234163953Srrs return (0); 1235163953Srrs} 1236163953Srrs 1237163953Srrsstatic int 1238163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1239163953Srrs{ 1240163953Srrs struct sockaddr *addr; 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_getaddr(so, nam); 1250163953Srrs if (error) { 1251163953Srrs /* try v4 next if v6 failed */ 1252163953Srrs error = sctp_ingetaddr(so, nam); 1253163953Srrs if (error) { 1254163953Srrs return (error); 1255163953Srrs } 1256163953Srrs addr = *nam; 1257163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1258166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1259163953Srrs struct sockaddr_in6 sin6; 1260163953Srrs 1261163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1262163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1263168709Srrs 1264163953Srrs } 1265163953Srrs } 1266163953Srrs return (error); 1267163953Srrs} 1268163953Srrs 1269163953Srrs 1270163953Srrsstatic int 1271163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1272163953Srrs{ 1273163953Srrs struct sockaddr *addr = *nam; 1274163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1275166086Srrs int error; 1276163953Srrs 1277171943Srrs if (inp6 == NULL) { 1278171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1279163953Srrs return EINVAL; 1280171943Srrs } 1281163953Srrs /* allow v6 addresses precedence */ 1282163953Srrs error = sctp6_peeraddr(so, nam); 1283163953Srrs if (error) { 1284163953Srrs /* try v4 next if v6 failed */ 1285163953Srrs error = sctp_peeraddr(so, nam); 1286163953Srrs if (error) { 1287163953Srrs return (error); 1288163953Srrs } 1289163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1290166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1291163953Srrs struct sockaddr_in6 sin6; 1292163953Srrs 1293163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1294163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1295163953Srrs } 1296163953Srrs } 1297163953Srrs return error; 1298163953Srrs} 1299163953Srrs 1300163953Srrsstruct pr_usrreqs sctp6_usrreqs = { 1301163953Srrs .pru_abort = sctp6_abort, 1302163953Srrs .pru_accept = sctp_accept, 1303163953Srrs .pru_attach = sctp6_attach, 1304163953Srrs .pru_bind = sctp6_bind, 1305163953Srrs .pru_connect = sctp6_connect, 1306163953Srrs .pru_control = in6_control, 1307163953Srrs .pru_close = sctp6_close, 1308163953Srrs .pru_detach = sctp6_close, 1309163953Srrs .pru_sopoll = sopoll_generic, 1310178201Srrs .pru_flush = sctp_flush, 1311163953Srrs .pru_disconnect = sctp6_disconnect, 1312163953Srrs .pru_listen = sctp_listen, 1313163953Srrs .pru_peeraddr = sctp6_getpeeraddr, 1314163953Srrs .pru_send = sctp6_send, 1315163953Srrs .pru_shutdown = sctp_shutdown, 1316163953Srrs .pru_sockaddr = sctp6_in6getaddr, 1317163953Srrs .pru_sosend = sctp_sosend, 1318163953Srrs .pru_soreceive = sctp_soreceive 1319163953Srrs}; 1320