sctp6_usrreq.c revision 197288
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 197288 2009-09-17 15:11:12Z 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> 54188067Srrs#include <netinet/sctp_crc32.h> 55179783Srrs#include <netinet/udp.h> 56163953Srrs 57171167Sgnn#ifdef IPSEC 58171133Sgnn#include <netipsec/ipsec.h> 59171133Sgnn#if defined(INET6) 60171133Sgnn#include <netipsec/ipsec6.h> 61171440Srrs#endif /* INET6 */ 62171440Srrs#endif /* IPSEC */ 63163953Srrs 64163953Srrsextern struct protosw inetsw[]; 65163953Srrs 66163953Srrsint 67171259Sdelphijsctp6_input(struct mbuf **i_pak, int *offp, int proto) 68163953Srrs{ 69165647Srrs struct mbuf *m; 70163953Srrs struct ip6_hdr *ip6; 71163953Srrs struct sctphdr *sh; 72163953Srrs struct sctp_inpcb *in6p = NULL; 73163953Srrs struct sctp_nets *net; 74163953Srrs int refcount_up = 0; 75169352Srrs uint32_t check, calc_check; 76170181Srrs uint32_t vrf_id = 0; 77163953Srrs struct inpcb *in6p_ip; 78163953Srrs struct sctp_chunkhdr *ch; 79188067Srrs int length, offset, iphlen; 80168299Srrs uint8_t ecn_bits; 81163953Srrs struct sctp_tcb *stcb = NULL; 82170056Srrs int pkt_len = 0; 83163953Srrs int off = *offp; 84179783Srrs uint16_t port = 0; 85163953Srrs 86169352Srrs /* get the VRF and table id's */ 87169352Srrs if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { 88169352Srrs SCTP_RELEASE_PKT(*i_pak); 89169352Srrs return (-1); 90169352Srrs } 91168709Srrs m = SCTP_HEADER_TO_CHAIN(*i_pak); 92170056Srrs pkt_len = SCTP_HEADER_LEN((*i_pak)); 93165647Srrs 94170091Srrs#ifdef SCTP_PACKET_LOGGING 95170091Srrs sctp_packet_log(m, pkt_len); 96170091Srrs#endif 97163953Srrs ip6 = mtod(m, struct ip6_hdr *); 98163953Srrs /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */ 99170056Srrs IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, 100170056Srrs (int)(sizeof(*sh) + sizeof(*ch))); 101163953Srrs if (sh == NULL) { 102163953Srrs SCTP_STAT_INCR(sctps_hdrops); 103163953Srrs return IPPROTO_DONE; 104163953Srrs } 105163953Srrs ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 106163953Srrs iphlen = off; 107163953Srrs offset = iphlen + sizeof(*sh) + sizeof(*ch); 108170859Srrs SCTPDBG(SCTP_DEBUG_INPUT1, 109170859Srrs "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen); 110163953Srrs 111170859Srrs 112163953Srrs#if defined(NFAITH) && NFAITH > 0 113163953Srrs 114163953Srrs if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) { 115163953Srrs /* XXX send icmp6 host/port unreach? */ 116163953Srrs goto bad; 117163953Srrs } 118163953Srrs#endif /* NFAITH defined and > 0 */ 119163953Srrs SCTP_STAT_INCR(sctps_recvpackets); 120163953Srrs SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 121169420Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n", 122170056Srrs iphlen, pkt_len); 123163953Srrs if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 124163953Srrs /* No multi-cast support in SCTP */ 125163953Srrs goto bad; 126163953Srrs } 127163953Srrs /* destination port of 0 is illegal, based on RFC2960. */ 128163953Srrs if (sh->dest_port == 0) 129163953Srrs goto bad; 130188067Srrs 131188067Srrs SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 132188067Srrs "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n", 133188067Srrs m->m_pkthdr.len, 134188067Srrs if_name(m->m_pkthdr.rcvif), 135188067Srrs m->m_pkthdr.csum_flags); 136188067Srrs if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 137188067Srrs SCTP_STAT_INCR(sctps_recvhwcrc); 138188067Srrs goto sctp_skip_csum; 139188067Srrs } 140171990Srrs check = sh->checksum; /* save incoming checksum */ 141179783Srrs if ((check == 0) && (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback)) && 142171990Srrs (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))) { 143188067Srrs SCTP_STAT_INCR(sctps_recvnocrc); 144171990Srrs goto sctp_skip_csum; 145171990Srrs } 146171990Srrs sh->checksum = 0; /* prepare for calc */ 147188067Srrs calc_check = sctp_calculate_cksum(m, iphlen); 148188067Srrs SCTP_STAT_INCR(sctps_recvswcrc); 149171990Srrs if (calc_check != check) { 150188067Srrs SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p phlen:%d\n", 151188067Srrs calc_check, check, m, iphlen); 152171990Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 153171990Srrs sh, ch, &in6p, &net, vrf_id); 154179783Srrs if ((net) && (port)) { 155179783Srrs if (net->port == 0) { 156179783Srrs sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr)); 157179783Srrs } 158179783Srrs net->port = port; 159179783Srrs } 160171990Srrs /* in6p's ref-count increased && stcb locked */ 161171990Srrs if ((in6p) && (stcb)) { 162171990Srrs sctp_send_packet_dropped(stcb, net, m, iphlen, 1); 163172090Srrs sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED); 164171990Srrs } else if ((in6p != NULL) && (stcb == NULL)) { 165171990Srrs refcount_up = 1; 166163953Srrs } 167171990Srrs SCTP_STAT_INCR(sctps_badsum); 168171990Srrs SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors); 169171990Srrs goto bad; 170165647Srrs } 171171990Srrs sh->checksum = calc_check; 172171990Srrs 173163953Srrssctp_skip_csum: 174163953Srrs net = NULL; 175163953Srrs /* 176163953Srrs * Locate pcb and tcb for datagram sctp_findassociation_addr() wants 177163953Srrs * IP/SCTP/first chunk header... 178163953Srrs */ 179163953Srrs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch), 180168299Srrs sh, ch, &in6p, &net, vrf_id); 181179783Srrs if ((net) && (port)) { 182179783Srrs if (net->port == 0) { 183179783Srrs sctp_pathmtu_adjustment(in6p, stcb, net, net->mtu - sizeof(struct udphdr)); 184179783Srrs } 185179783Srrs net->port = port; 186179783Srrs } 187163953Srrs /* in6p's ref-count increased */ 188163953Srrs if (in6p == NULL) { 189163953Srrs struct sctp_init_chunk *init_chk, chunk_buf; 190163953Srrs 191163953Srrs SCTP_STAT_INCR(sctps_noport); 192163953Srrs if (ch->chunk_type == SCTP_INITIATION) { 193163953Srrs /* 194163953Srrs * we do a trick here to get the INIT tag, dig in 195163953Srrs * and get the tag from the INIT and put it in the 196163953Srrs * common header. 197163953Srrs */ 198163953Srrs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m, 199163953Srrs iphlen + sizeof(*sh), sizeof(*init_chk), 200168299Srrs (uint8_t *) & chunk_buf); 201169420Srrs if (init_chk) 202169420Srrs sh->v_tag = init_chk->init.initiate_tag; 203169420Srrs else 204169420Srrs sh->v_tag = 0; 205163953Srrs } 206165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_ACK) { 207179783Srrs sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id, port); 208165220Srrs goto bad; 209165220Srrs } 210165220Srrs if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) { 211165220Srrs goto bad; 212165220Srrs } 213165220Srrs if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) 214179783Srrs sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port); 215163953Srrs goto bad; 216163953Srrs } else if (stcb == NULL) { 217163953Srrs refcount_up = 1; 218163953Srrs } 219163953Srrs in6p_ip = (struct inpcb *)in6p; 220171167Sgnn#ifdef IPSEC 221163953Srrs /* 222163953Srrs * Check AH/ESP integrity. 223163953Srrs */ 224163996Srrs if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) { 225163953Srrs/* XXX */ 226197288Srrs MODULE_GLOBAL(ipsec6stat).in_polvio++; 227163953Srrs goto bad; 228163996Srrs } 229171440Srrs#endif /* IPSEC */ 230163953Srrs 231163953Srrs /* 232163953Srrs * CONTROL chunk processing 233163953Srrs */ 234163953Srrs offset -= sizeof(*ch); 235163953Srrs ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); 236165647Srrs 237165647Srrs /* Length now holds the total packet length payload + iphlen */ 238165647Srrs length = ntohs(ip6->ip6_plen) + iphlen; 239165647Srrs 240169655Srrs /* sa_ignore NO_NULL_CHK */ 241169378Srrs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch, 242179783Srrs in6p, stcb, net, ecn_bits, vrf_id, port); 243163953Srrs /* inp's ref-count reduced && stcb unlocked */ 244163953Srrs /* XXX this stuff below gets moved to appropriate parts later... */ 245163953Srrs if (m) 246169352Srrs sctp_m_freem(m); 247163953Srrs if ((in6p) && refcount_up) { 248163953Srrs /* reduce ref-count */ 249163953Srrs SCTP_INP_WLOCK(in6p); 250163953Srrs SCTP_INP_DECR_REF(in6p); 251163953Srrs SCTP_INP_WUNLOCK(in6p); 252163953Srrs } 253163953Srrs return IPPROTO_DONE; 254163953Srrs 255163953Srrsbad: 256169420Srrs if (stcb) { 257163953Srrs SCTP_TCB_UNLOCK(stcb); 258169420Srrs } 259163953Srrs if ((in6p) && refcount_up) { 260163953Srrs /* reduce ref-count */ 261163953Srrs SCTP_INP_WLOCK(in6p); 262163953Srrs SCTP_INP_DECR_REF(in6p); 263163953Srrs SCTP_INP_WUNLOCK(in6p); 264163953Srrs } 265163953Srrs if (m) 266169352Srrs sctp_m_freem(m); 267163953Srrs return IPPROTO_DONE; 268163953Srrs} 269163953Srrs 270163953Srrs 271163953Srrsstatic void 272171259Sdelphijsctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6, 273171259Sdelphij struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net) 274163953Srrs{ 275168299Srrs uint32_t nxtsz; 276163953Srrs 277163953Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 278163953Srrs (icmp6 == NULL) || (sh == NULL)) { 279163953Srrs goto out; 280163953Srrs } 281163953Srrs /* First do we even look at it? */ 282163953Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) 283163953Srrs goto out; 284163953Srrs 285163953Srrs if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) { 286163953Srrs /* not PACKET TO BIG */ 287163953Srrs goto out; 288163953Srrs } 289163953Srrs /* 290163953Srrs * ok we need to look closely. We could even get smarter and look at 291163953Srrs * anyone that we sent to in case we get a different ICMP that tells 292163953Srrs * us there is no way to reach a host, but for this impl, all we 293163953Srrs * care about is MTU discovery. 294163953Srrs */ 295163953Srrs nxtsz = ntohl(icmp6->icmp6_mtu); 296163953Srrs /* Stop any PMTU timer */ 297165220Srrs sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1); 298163953Srrs 299163953Srrs /* Adjust destination size limit */ 300163953Srrs if (net->mtu > nxtsz) { 301163953Srrs net->mtu = nxtsz; 302185694Srrs if (net->port) { 303185694Srrs net->mtu -= sizeof(struct udphdr); 304185694Srrs } 305163953Srrs } 306163953Srrs /* now what about the ep? */ 307163953Srrs if (stcb->asoc.smallest_mtu > nxtsz) { 308163953Srrs struct sctp_tmit_chunk *chk; 309163953Srrs 310163953Srrs /* Adjust that too */ 311163953Srrs stcb->asoc.smallest_mtu = nxtsz; 312163953Srrs /* now off to subtract IP_DF flag if needed */ 313163953Srrs 314163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) { 315168299Srrs if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 316163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 317163953Srrs } 318163953Srrs } 319163953Srrs TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) { 320168299Srrs if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) { 321163953Srrs /* 322163953Srrs * For this guy we also mark for immediate 323163953Srrs * resend since we sent to big of chunk 324163953Srrs */ 325163953Srrs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK; 326163953Srrs if (chk->sent != SCTP_DATAGRAM_RESEND) 327163953Srrs stcb->asoc.sent_queue_retran_cnt++; 328163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 329163953Srrs chk->rec.data.doing_fast_retransmit = 0; 330163953Srrs 331163953Srrs chk->sent = SCTP_DATAGRAM_RESEND; 332163953Srrs /* Clear any time so NO RTT is being done */ 333163953Srrs chk->sent_rcv_time.tv_sec = 0; 334163953Srrs chk->sent_rcv_time.tv_usec = 0; 335163953Srrs stcb->asoc.total_flight -= chk->send_size; 336163953Srrs net->flight_size -= chk->send_size; 337163953Srrs } 338163953Srrs } 339163953Srrs } 340163953Srrs sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL); 341163953Srrsout: 342169420Srrs if (stcb) { 343163953Srrs SCTP_TCB_UNLOCK(stcb); 344169420Srrs } 345163953Srrs} 346163953Srrs 347163953Srrs 348172156Srrsvoid 349172091Srrssctp6_notify(struct sctp_inpcb *inp, 350172091Srrs struct icmp6_hdr *icmph, 351172091Srrs struct sctphdr *sh, 352172091Srrs struct sockaddr *to, 353172091Srrs struct sctp_tcb *stcb, 354172091Srrs struct sctp_nets *net) 355172091Srrs{ 356172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 357172091Srrs struct socket *so; 358172091Srrs 359172091Srrs#endif 360172091Srrs /* protection */ 361172091Srrs int reason; 362172091Srrs 363172091Srrs 364172091Srrs if ((inp == NULL) || (stcb == NULL) || (net == NULL) || 365172091Srrs (sh == NULL) || (to == NULL)) { 366172091Srrs if (stcb) 367172091Srrs SCTP_TCB_UNLOCK(stcb); 368172091Srrs return; 369172091Srrs } 370172091Srrs /* First job is to verify the vtag matches what I would send */ 371172091Srrs if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) { 372172091Srrs SCTP_TCB_UNLOCK(stcb); 373172091Srrs return; 374172091Srrs } 375172091Srrs if (icmph->icmp6_type != ICMP_UNREACH) { 376172091Srrs /* We only care about unreachable */ 377172091Srrs SCTP_TCB_UNLOCK(stcb); 378172091Srrs return; 379172091Srrs } 380172091Srrs if ((icmph->icmp6_code == ICMP_UNREACH_NET) || 381172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST) || 382172091Srrs (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) || 383172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) || 384172091Srrs (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) || 385172091Srrs (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) || 386172091Srrs (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) || 387172091Srrs (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) { 388172091Srrs 389172091Srrs /* 390172091Srrs * Hmm reachablity problems we must examine closely. If its 391172091Srrs * not reachable, we may have lost a network. Or if there is 392172091Srrs * NO protocol at the other end named SCTP. well we consider 393172091Srrs * it a OOTB abort. 394172091Srrs */ 395172091Srrs if (net->dest_state & SCTP_ADDR_REACHABLE) { 396172091Srrs /* Ok that destination is NOT reachable */ 397172091Srrs SCTP_PRINTF("ICMP (thresh %d/%d) takes interface %p down\n", 398172091Srrs net->error_count, 399172091Srrs net->failure_threshold, 400172091Srrs net); 401172091Srrs 402172091Srrs net->dest_state &= ~SCTP_ADDR_REACHABLE; 403172091Srrs net->dest_state |= SCTP_ADDR_NOT_REACHABLE; 404172091Srrs /* 405172091Srrs * JRS 5/14/07 - If a destination is unreachable, 406172091Srrs * the PF bit is turned off. This allows an 407172091Srrs * unambiguous use of the PF bit for destinations 408172091Srrs * that are reachable but potentially failed. If the 409172091Srrs * destination is set to the unreachable state, also 410172091Srrs * set the destination to the PF state. 411172091Srrs */ 412172091Srrs /* 413172091Srrs * Add debug message here if destination is not in 414172091Srrs * PF state. 415172091Srrs */ 416172091Srrs /* Stop any running T3 timers here? */ 417179783Srrs if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) { 418172091Srrs net->dest_state &= ~SCTP_ADDR_PF; 419172091Srrs SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n", 420172091Srrs net); 421172091Srrs } 422172091Srrs net->error_count = net->failure_threshold + 1; 423172091Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 424172091Srrs stcb, SCTP_FAILED_THRESHOLD, 425172091Srrs (void *)net, SCTP_SO_NOT_LOCKED); 426172091Srrs } 427172091Srrs SCTP_TCB_UNLOCK(stcb); 428172091Srrs } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) || 429172091Srrs (icmph->icmp6_code == ICMP_UNREACH_PORT)) { 430172091Srrs /* 431172091Srrs * Here the peer is either playing tricks on us, including 432172091Srrs * an address that belongs to someone who does not support 433172091Srrs * SCTP OR was a userland implementation that shutdown and 434172091Srrs * now is dead. In either case treat it like a OOTB abort 435172091Srrs * with no TCB 436172091Srrs */ 437172091Srrs reason = SCTP_PEER_FAULTY; 438172091Srrs sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED); 439172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 440172091Srrs so = SCTP_INP_SO(inp); 441172091Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 442172091Srrs SCTP_TCB_UNLOCK(stcb); 443172091Srrs SCTP_SOCKET_LOCK(so, 1); 444172091Srrs SCTP_TCB_LOCK(stcb); 445172091Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 446172091Srrs#endif 447172091Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 448172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 449172091Srrs SCTP_SOCKET_UNLOCK(so, 1); 450172091Srrs /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */ 451172091Srrs#endif 452172091Srrs /* no need to unlock here, since the TCB is gone */ 453172091Srrs } else { 454172091Srrs SCTP_TCB_UNLOCK(stcb); 455172091Srrs } 456172091Srrs} 457172091Srrs 458172091Srrs 459172091Srrs 460163953Srrsvoid 461171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d) 462163953Srrs{ 463163953Srrs struct sctphdr sh; 464163953Srrs struct ip6ctlparam *ip6cp = NULL; 465167598Srrs uint32_t vrf_id; 466163953Srrs 467167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 468167598Srrs 469163953Srrs if (pktdst->sa_family != AF_INET6 || 470163953Srrs pktdst->sa_len != sizeof(struct sockaddr_in6)) 471163953Srrs return; 472163953Srrs 473163953Srrs if ((unsigned)cmd >= PRC_NCMDS) 474163953Srrs return; 475163953Srrs if (PRC_IS_REDIRECT(cmd)) { 476163953Srrs d = NULL; 477163953Srrs } else if (inet6ctlerrmap[cmd] == 0) { 478163953Srrs return; 479163953Srrs } 480163953Srrs /* if the parameter is from icmp6, decode it. */ 481163953Srrs if (d != NULL) { 482163953Srrs ip6cp = (struct ip6ctlparam *)d; 483163953Srrs } else { 484163953Srrs ip6cp = (struct ip6ctlparam *)NULL; 485163953Srrs } 486163953Srrs 487163953Srrs if (ip6cp) { 488163953Srrs /* 489163953Srrs * XXX: We assume that when IPV6 is non NULL, M and OFF are 490163953Srrs * valid. 491163953Srrs */ 492163953Srrs /* check if we can safely examine src and dst ports */ 493163953Srrs struct sctp_inpcb *inp = NULL; 494163953Srrs struct sctp_tcb *stcb = NULL; 495163953Srrs struct sctp_nets *net = NULL; 496163953Srrs struct sockaddr_in6 final; 497163953Srrs 498165647Srrs if (ip6cp->ip6c_m == NULL) 499163953Srrs return; 500163953Srrs 501163953Srrs bzero(&sh, sizeof(sh)); 502163953Srrs bzero(&final, sizeof(final)); 503163953Srrs inp = NULL; 504163953Srrs net = NULL; 505163953Srrs m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), 506163953Srrs (caddr_t)&sh); 507163953Srrs ip6cp->ip6c_src->sin6_port = sh.src_port; 508163953Srrs final.sin6_len = sizeof(final); 509163953Srrs final.sin6_family = AF_INET6; 510163953Srrs final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; 511163953Srrs final.sin6_port = sh.dest_port; 512163953Srrs stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src, 513163953Srrs (struct sockaddr *)&final, 514167598Srrs &inp, &net, 1, vrf_id); 515163953Srrs /* inp's ref-count increased && stcb locked */ 516163953Srrs if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 517163953Srrs if (cmd == PRC_MSGSIZE) { 518163953Srrs sctp6_notify_mbuf(inp, 519163953Srrs ip6cp->ip6c_icmp6, 520163953Srrs &sh, 521163953Srrs stcb, 522163953Srrs net); 523163953Srrs /* inp's ref-count reduced && stcb unlocked */ 524163953Srrs } else { 525172091Srrs sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh, 526163953Srrs (struct sockaddr *)&final, 527163953Srrs stcb, net); 528163953Srrs /* inp's ref-count reduced && stcb unlocked */ 529163953Srrs } 530163953Srrs } else { 531163953Srrs if (PRC_IS_REDIRECT(cmd) && inp) { 532163953Srrs in6_rtchange((struct in6pcb *)inp, 533163953Srrs inet6ctlerrmap[cmd]); 534163953Srrs } 535163953Srrs if (inp) { 536163953Srrs /* reduce inp's ref-count */ 537163953Srrs SCTP_INP_WLOCK(inp); 538163953Srrs SCTP_INP_DECR_REF(inp); 539163953Srrs SCTP_INP_WUNLOCK(inp); 540163953Srrs } 541163953Srrs if (stcb) 542163953Srrs SCTP_TCB_UNLOCK(stcb); 543163953Srrs } 544163953Srrs } 545163953Srrs} 546163953Srrs 547163953Srrs/* 548163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c 549163953Srrs * since they do the same thing and now we lookup with a sockaddr 550163953Srrs */ 551163953Srrsstatic int 552163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS) 553163953Srrs{ 554164085Srrs struct xucred xuc; 555163953Srrs struct sockaddr_in6 addrs[2]; 556163953Srrs struct sctp_inpcb *inp; 557163953Srrs struct sctp_nets *net; 558163953Srrs struct sctp_tcb *stcb; 559164085Srrs int error; 560167598Srrs uint32_t vrf_id; 561163953Srrs 562167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 563167598Srrs 564170587Srwatson error = priv_check(req->td, PRIV_NETINET_GETCRED); 565163953Srrs if (error) 566163953Srrs return (error); 567163953Srrs 568171943Srrs if (req->newlen != sizeof(addrs)) { 569171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 570163953Srrs return (EINVAL); 571171943Srrs } 572171943Srrs if (req->oldlen != sizeof(struct ucred)) { 573171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 574163953Srrs return (EINVAL); 575171943Srrs } 576163953Srrs error = SYSCTL_IN(req, addrs, sizeof(addrs)); 577163953Srrs if (error) 578163953Srrs return (error); 579163953Srrs 580163953Srrs stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]), 581163953Srrs sin6tosa(&addrs[1]), 582167598Srrs &inp, &net, 1, vrf_id); 583163953Srrs if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 584164085Srrs if ((inp != NULL) && (stcb == NULL)) { 585164085Srrs /* reduce ref-count */ 586163953Srrs SCTP_INP_WLOCK(inp); 587163953Srrs SCTP_INP_DECR_REF(inp); 588164085Srrs goto cred_can_cont; 589163953Srrs } 590171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 591164085Srrs error = ENOENT; 592163953Srrs goto out; 593163953Srrs } 594163953Srrs SCTP_TCB_UNLOCK(stcb); 595164085Srrs /* 596164085Srrs * We use the write lock here, only since in the error leg we need 597164085Srrs * it. If we used RLOCK, then we would have to 598164085Srrs * wlock/decr/unlock/rlock. Which in theory could create a hole. 599164085Srrs * Better to use higher wlock. 600164085Srrs */ 601164085Srrs SCTP_INP_WLOCK(inp); 602164085Srrscred_can_cont: 603164085Srrs error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 604164085Srrs if (error) { 605164085Srrs SCTP_INP_WUNLOCK(inp); 606164085Srrs goto out; 607164085Srrs } 608164085Srrs cru2x(inp->sctp_socket->so_cred, &xuc); 609164085Srrs SCTP_INP_WUNLOCK(inp); 610164085Srrs error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 611163953Srrsout: 612163953Srrs return (error); 613163953Srrs} 614163953Srrs 615163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 616163953Srrs 0, 0, 617163953Srrs sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 618163953Srrs 619163953Srrs 620163953Srrs/* This is the same as the sctp_abort() could be made common */ 621163953Srrsstatic void 622163953Srrssctp6_abort(struct socket *so) 623163953Srrs{ 624163953Srrs struct sctp_inpcb *inp; 625163953Srrs uint32_t flags; 626163953Srrs 627163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 628172091Srrs if (inp == 0) { 629172091Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 630163953Srrs return; 631172091Srrs } 632163953Srrssctp_must_try_again: 633163953Srrs flags = inp->sctp_flags; 634163953Srrs#ifdef SCTP_LOG_CLOSING 635163953Srrs sctp_log_closing(inp, NULL, 17); 636163953Srrs#endif 637163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 638163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 639163953Srrs#ifdef SCTP_LOG_CLOSING 640163953Srrs sctp_log_closing(inp, NULL, 16); 641163953Srrs#endif 642169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 643169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 644163953Srrs SOCK_LOCK(so); 645167695Srrs SCTP_SB_CLEAR(so->so_snd); 646163953Srrs /* 647163953Srrs * same for the rcv ones, they are only here for the 648163953Srrs * accounting/select. 649163953Srrs */ 650167695Srrs SCTP_SB_CLEAR(so->so_rcv); 651167695Srrs /* Now null out the reference, we are completely detached. */ 652163953Srrs so->so_pcb = NULL; 653163953Srrs SOCK_UNLOCK(so); 654163953Srrs } else { 655163953Srrs flags = inp->sctp_flags; 656163953Srrs if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 657163953Srrs goto sctp_must_try_again; 658163953Srrs } 659163953Srrs } 660163953Srrs return; 661163953Srrs} 662163953Srrs 663163953Srrsstatic int 664163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p) 665163953Srrs{ 666163953Srrs struct in6pcb *inp6; 667166086Srrs int error; 668163953Srrs struct sctp_inpcb *inp; 669170205Srrs uint32_t vrf_id = SCTP_DEFAULT_VRFID; 670163953Srrs 671163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 672171943Srrs if (inp != NULL) { 673171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 674163953Srrs return EINVAL; 675171943Srrs } 676163953Srrs if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 677179783Srrs error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 678163953Srrs if (error) 679163953Srrs return error; 680163953Srrs } 681170205Srrs error = sctp_inpcb_alloc(so, vrf_id); 682163953Srrs if (error) 683163953Srrs return error; 684163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 685170205Srrs SCTP_INP_WLOCK(inp); 686163953Srrs inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 687163953Srrs inp6 = (struct in6pcb *)inp; 688163953Srrs 689163953Srrs inp6->inp_vflag |= INP_IPV6; 690163953Srrs inp6->in6p_hops = -1; /* use kernel default */ 691163953Srrs inp6->in6p_cksum = -1; /* just to be sure */ 692163953Srrs#ifdef INET 693163953Srrs /* 694163953Srrs * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 695163953Srrs * socket as well, because the socket may be bound to an IPv6 696163953Srrs * wildcard address, which may match an IPv4-mapped IPv6 address. 697163953Srrs */ 698197288Srrs inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 699163953Srrs#endif 700163953Srrs /* 701163953Srrs * Hmm what about the IPSEC stuff that is missing here but in 702163953Srrs * sctp_attach()? 703163953Srrs */ 704170205Srrs SCTP_INP_WUNLOCK(inp); 705163953Srrs return 0; 706163953Srrs} 707163953Srrs 708163953Srrsstatic int 709163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 710163953Srrs{ 711163953Srrs struct sctp_inpcb *inp; 712163953Srrs struct in6pcb *inp6; 713166086Srrs int error; 714163953Srrs 715163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 716171943Srrs if (inp == 0) { 717171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 718163953Srrs return EINVAL; 719171943Srrs } 720170056Srrs if (addr) { 721170056Srrs if ((addr->sa_family == AF_INET6) && 722170056Srrs (addr->sa_len != sizeof(struct sockaddr_in6))) { 723171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 724170056Srrs return EINVAL; 725170056Srrs } 726170056Srrs if ((addr->sa_family == AF_INET) && 727170056Srrs (addr->sa_len != sizeof(struct sockaddr_in))) { 728171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 729170056Srrs return EINVAL; 730170056Srrs } 731170056Srrs } 732163953Srrs inp6 = (struct in6pcb *)inp; 733163953Srrs inp6->inp_vflag &= ~INP_IPV4; 734163953Srrs inp6->inp_vflag |= INP_IPV6; 735166023Srrs if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { 736163953Srrs if (addr->sa_family == AF_INET) { 737163953Srrs /* binding v4 addr to v6 socket, so reset flags */ 738163953Srrs inp6->inp_vflag |= INP_IPV4; 739163953Srrs inp6->inp_vflag &= ~INP_IPV6; 740163953Srrs } else { 741163953Srrs struct sockaddr_in6 *sin6_p; 742163953Srrs 743163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 744163953Srrs 745163953Srrs if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 746163953Srrs inp6->inp_vflag |= INP_IPV4; 747163953Srrs } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 748163953Srrs struct sockaddr_in sin; 749163953Srrs 750163953Srrs in6_sin6_2_sin(&sin, sin6_p); 751163953Srrs inp6->inp_vflag |= INP_IPV4; 752163953Srrs inp6->inp_vflag &= ~INP_IPV6; 753171572Srrs error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); 754163953Srrs return error; 755163953Srrs } 756163953Srrs } 757163953Srrs } else if (addr != NULL) { 758163953Srrs /* IPV6_V6ONLY socket */ 759163953Srrs if (addr->sa_family == AF_INET) { 760163953Srrs /* can't bind v4 addr to v6 only socket! */ 761171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 762163953Srrs return EINVAL; 763163953Srrs } else { 764163953Srrs struct sockaddr_in6 *sin6_p; 765163953Srrs 766163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 767163953Srrs 768172091Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 769163953Srrs /* can't bind v4-mapped addrs either! */ 770163953Srrs /* NOTE: we don't support SIIT */ 771171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 772172091Srrs return EINVAL; 773172091Srrs } 774163953Srrs } 775163953Srrs } 776171572Srrs error = sctp_inpcb_bind(so, addr, NULL, p); 777163953Srrs return error; 778163953Srrs} 779163953Srrs 780163953Srrs 781163953Srrsstatic void 782163953Srrssctp6_close(struct socket *so) 783163953Srrs{ 784171990Srrs sctp_close(so); 785163953Srrs} 786163953Srrs 787167598Srrs/* This could be made common with sctp_detach() since they are identical */ 788163953Srrs 789168709Srrsstatic 790168709Srrsint 791163953Srrssctp6_disconnect(struct socket *so) 792163953Srrs{ 793171990Srrs return (sctp_disconnect(so)); 794163953Srrs} 795163953Srrs 796168709Srrs 797163953Srrsint 798163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 799163953Srrs struct mbuf *control, struct thread *p); 800163953Srrs 801163953Srrs 802163953Srrsstatic int 803163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 804163953Srrs struct mbuf *control, struct thread *p) 805163953Srrs{ 806163953Srrs struct sctp_inpcb *inp; 807163953Srrs struct inpcb *in_inp; 808163953Srrs struct in6pcb *inp6; 809163953Srrs 810163953Srrs#ifdef INET 811163953Srrs struct sockaddr_in6 *sin6; 812163953Srrs 813163953Srrs#endif /* INET */ 814163953Srrs /* No SPL needed since sctp_output does this */ 815163953Srrs 816163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 817163953Srrs if (inp == NULL) { 818163953Srrs if (control) { 819169352Srrs SCTP_RELEASE_PKT(control); 820163953Srrs control = NULL; 821163953Srrs } 822169352Srrs SCTP_RELEASE_PKT(m); 823171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 824163953Srrs return EINVAL; 825163953Srrs } 826163953Srrs in_inp = (struct inpcb *)inp; 827163953Srrs inp6 = (struct in6pcb *)inp; 828163953Srrs /* 829163953Srrs * For the TCP model we may get a NULL addr, if we are a connected 830163953Srrs * socket thats ok. 831163953Srrs */ 832163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 833163953Srrs (addr == NULL)) { 834163953Srrs goto connected_type; 835163953Srrs } 836163953Srrs if (addr == NULL) { 837169352Srrs SCTP_RELEASE_PKT(m); 838163953Srrs if (control) { 839169352Srrs SCTP_RELEASE_PKT(control); 840163953Srrs control = NULL; 841163953Srrs } 842171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); 843163953Srrs return (EDESTADDRREQ); 844163953Srrs } 845163953Srrs#ifdef INET 846163953Srrs sin6 = (struct sockaddr_in6 *)addr; 847166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 848163953Srrs /* 849163953Srrs * if IPV6_V6ONLY flag, we discard datagrams destined to a 850163953Srrs * v4 addr or v4-mapped addr 851163953Srrs */ 852163953Srrs if (addr->sa_family == AF_INET) { 853171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 854163953Srrs return EINVAL; 855163953Srrs } 856163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 857171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 858163953Srrs return EINVAL; 859163953Srrs } 860163953Srrs } 861163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 862197288Srrs if (!MODULE_GLOBAL(ip6_v6only)) { 863163953Srrs struct sockaddr_in sin; 864163953Srrs 865163953Srrs /* convert v4-mapped into v4 addr and send */ 866163953Srrs in6_sin6_2_sin(&sin, sin6); 867163953Srrs return sctp_sendm(so, flags, m, (struct sockaddr *)&sin, 868163953Srrs control, p); 869163953Srrs } else { 870163953Srrs /* mapped addresses aren't enabled */ 871171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 872163953Srrs return EINVAL; 873163953Srrs } 874163953Srrs } 875163953Srrs#endif /* INET */ 876163953Srrsconnected_type: 877163953Srrs /* now what about control */ 878163953Srrs if (control) { 879163953Srrs if (inp->control) { 880169420Srrs SCTP_PRINTF("huh? control set?\n"); 881169352Srrs SCTP_RELEASE_PKT(inp->control); 882163953Srrs inp->control = NULL; 883163953Srrs } 884163953Srrs inp->control = control; 885163953Srrs } 886163953Srrs /* Place the data */ 887163953Srrs if (inp->pkt) { 888165647Srrs SCTP_BUF_NEXT(inp->pkt_last) = m; 889163953Srrs inp->pkt_last = m; 890163953Srrs } else { 891163953Srrs inp->pkt_last = inp->pkt = m; 892163953Srrs } 893163953Srrs if ( 894163953Srrs /* FreeBSD and MacOSX uses a flag passed */ 895163953Srrs ((flags & PRUS_MORETOCOME) == 0) 896163953Srrs ) { 897163953Srrs /* 898163953Srrs * note with the current version this code will only be used 899163953Srrs * by OpenBSD, NetBSD and FreeBSD have methods for 900163953Srrs * re-defining sosend() to use sctp_sosend(). One can 901163953Srrs * optionaly switch back to this code (by changing back the 902163953Srrs * defininitions but this is not advisable. 903163953Srrs */ 904163953Srrs int ret; 905163953Srrs 906163953Srrs ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 907163953Srrs inp->pkt = NULL; 908163953Srrs inp->control = NULL; 909163953Srrs return (ret); 910163953Srrs } else { 911163953Srrs return (0); 912163953Srrs } 913163953Srrs} 914163953Srrs 915163953Srrsstatic int 916163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 917163953Srrs{ 918167598Srrs uint32_t vrf_id; 919163953Srrs int error = 0; 920163953Srrs struct sctp_inpcb *inp; 921163953Srrs struct in6pcb *inp6; 922163953Srrs struct sctp_tcb *stcb; 923163953Srrs 924163953Srrs#ifdef INET 925163953Srrs struct sockaddr_in6 *sin6; 926163953Srrs struct sockaddr_storage ss; 927163953Srrs 928163953Srrs#endif /* INET */ 929163953Srrs 930163953Srrs inp6 = (struct in6pcb *)so->so_pcb; 931163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 932163953Srrs if (inp == 0) { 933171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 934163953Srrs return (ECONNRESET); /* I made the same as TCP since we are 935163953Srrs * not setup? */ 936163953Srrs } 937170056Srrs if (addr == NULL) { 938171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 939170056Srrs return (EINVAL); 940170056Srrs } 941170056Srrs if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) { 942171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 943170056Srrs return (EINVAL); 944170056Srrs } 945170056Srrs if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) { 946171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 947170056Srrs return (EINVAL); 948170056Srrs } 949168299Srrs vrf_id = inp->def_vrf_id; 950163953Srrs SCTP_ASOC_CREATE_LOCK(inp); 951163953Srrs SCTP_INP_RLOCK(inp); 952163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 953163953Srrs SCTP_PCB_FLAGS_UNBOUND) { 954163953Srrs /* Bind a ephemeral port */ 955163953Srrs SCTP_INP_RUNLOCK(inp); 956163953Srrs error = sctp6_bind(so, NULL, p); 957163953Srrs if (error) { 958163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 959163953Srrs 960163953Srrs return (error); 961163953Srrs } 962163953Srrs SCTP_INP_RLOCK(inp); 963163953Srrs } 964163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 965163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 966163953Srrs /* We are already connected AND the TCP model */ 967163953Srrs SCTP_INP_RUNLOCK(inp); 968163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 969171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); 970163953Srrs return (EADDRINUSE); 971163953Srrs } 972163953Srrs#ifdef INET 973163953Srrs sin6 = (struct sockaddr_in6 *)addr; 974166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 975163953Srrs /* 976163953Srrs * if IPV6_V6ONLY flag, ignore connections destined to a v4 977163953Srrs * addr or v4-mapped addr 978163953Srrs */ 979163953Srrs if (addr->sa_family == AF_INET) { 980163953Srrs SCTP_INP_RUNLOCK(inp); 981163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 982171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 983163953Srrs return EINVAL; 984163953Srrs } 985163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 986163953Srrs SCTP_INP_RUNLOCK(inp); 987163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 988171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 989163953Srrs return EINVAL; 990163953Srrs } 991163953Srrs } 992163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 993197288Srrs if (!MODULE_GLOBAL(ip6_v6only)) { 994163953Srrs /* convert v4-mapped into v4 addr */ 995163953Srrs in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); 996163953Srrs addr = (struct sockaddr *)&ss; 997163953Srrs } else { 998163953Srrs /* mapped addresses aren't enabled */ 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 } else 1005163953Srrs#endif /* INET */ 1006163953Srrs addr = addr; /* for true v6 address case */ 1007163953Srrs 1008163953Srrs /* Now do we connect? */ 1009163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1010163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1011169420Srrs if (stcb) { 1012163953Srrs SCTP_TCB_UNLOCK(stcb); 1013169420Srrs } 1014163953Srrs SCTP_INP_RUNLOCK(inp); 1015163953Srrs } else { 1016163953Srrs SCTP_INP_RUNLOCK(inp); 1017163953Srrs SCTP_INP_WLOCK(inp); 1018163953Srrs SCTP_INP_INCR_REF(inp); 1019163953Srrs SCTP_INP_WUNLOCK(inp); 1020163953Srrs stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 1021163953Srrs if (stcb == NULL) { 1022163953Srrs SCTP_INP_WLOCK(inp); 1023163953Srrs SCTP_INP_DECR_REF(inp); 1024163953Srrs SCTP_INP_WUNLOCK(inp); 1025163953Srrs } 1026163953Srrs } 1027163953Srrs 1028163953Srrs if (stcb != NULL) { 1029163953Srrs /* Already have or am bring up an association */ 1030163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1031163953Srrs SCTP_TCB_UNLOCK(stcb); 1032171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); 1033163953Srrs return (EALREADY); 1034163953Srrs } 1035163953Srrs /* We are GOOD to go */ 1036171531Srrs stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id, p); 1037163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1038163953Srrs if (stcb == NULL) { 1039163953Srrs /* Gak! no memory */ 1040163953Srrs return (error); 1041163953Srrs } 1042163953Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1043163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1044163953Srrs /* Set the connected flag so we can queue data */ 1045163953Srrs soisconnecting(so); 1046163953Srrs } 1047163953Srrs stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1048169420Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1049163953Srrs 1050163953Srrs /* initialize authentication parameters for the assoc */ 1051163953Srrs sctp_initialize_auth_params(inp, stcb); 1052163953Srrs 1053172090Srrs sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 1054163953Srrs SCTP_TCB_UNLOCK(stcb); 1055163953Srrs return error; 1056163953Srrs} 1057163953Srrs 1058163953Srrsstatic int 1059163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr) 1060163953Srrs{ 1061163953Srrs struct sockaddr_in6 *sin6; 1062163953Srrs struct sctp_inpcb *inp; 1063167598Srrs uint32_t vrf_id; 1064167598Srrs struct sctp_ifa *sctp_ifa; 1065163953Srrs 1066163953Srrs int error; 1067163953Srrs 1068163953Srrs /* 1069163953Srrs * Do the malloc first in case it blocks. 1070163953Srrs */ 1071163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1072163953Srrs sin6->sin6_family = AF_INET6; 1073163953Srrs sin6->sin6_len = sizeof(*sin6); 1074163953Srrs 1075163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1076163953Srrs if (inp == NULL) { 1077163953Srrs SCTP_FREE_SONAME(sin6); 1078171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1079163953Srrs return ECONNRESET; 1080163953Srrs } 1081163953Srrs SCTP_INP_RLOCK(inp); 1082163953Srrs sin6->sin6_port = inp->sctp_lport; 1083163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1084163953Srrs /* For the bound all case you get back 0 */ 1085163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1086163953Srrs struct sctp_tcb *stcb; 1087163953Srrs struct sockaddr_in6 *sin_a6; 1088163953Srrs struct sctp_nets *net; 1089163953Srrs int fnd; 1090163953Srrs 1091163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1092163953Srrs if (stcb == NULL) { 1093163953Srrs goto notConn6; 1094163953Srrs } 1095163953Srrs fnd = 0; 1096163953Srrs sin_a6 = NULL; 1097163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1098163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1099164085Srrs if (sin_a6 == NULL) 1100164085Srrs /* this will make coverity happy */ 1101164085Srrs continue; 1102164085Srrs 1103163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1104163953Srrs fnd = 1; 1105163953Srrs break; 1106163953Srrs } 1107163953Srrs } 1108163953Srrs if ((!fnd) || (sin_a6 == NULL)) { 1109163953Srrs /* punt */ 1110163953Srrs goto notConn6; 1111163953Srrs } 1112168299Srrs vrf_id = inp->def_vrf_id; 1113168299Srrs sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id); 1114167598Srrs if (sctp_ifa) { 1115167598Srrs sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 1116167598Srrs } 1117163953Srrs } else { 1118163953Srrs /* For the bound all case you get back 0 */ 1119163953Srrs notConn6: 1120163953Srrs memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1121163953Srrs } 1122163953Srrs } else { 1123163953Srrs /* Take the first IPv6 address in the list */ 1124163953Srrs struct sctp_laddr *laddr; 1125163953Srrs int fnd = 0; 1126163953Srrs 1127163953Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1128167598Srrs if (laddr->ifa->address.sa.sa_family == AF_INET6) { 1129163953Srrs struct sockaddr_in6 *sin_a; 1130163953Srrs 1131167598Srrs sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; 1132163953Srrs sin6->sin6_addr = sin_a->sin6_addr; 1133163953Srrs fnd = 1; 1134163953Srrs break; 1135163953Srrs } 1136163953Srrs } 1137163953Srrs if (!fnd) { 1138163953Srrs SCTP_FREE_SONAME(sin6); 1139163953Srrs SCTP_INP_RUNLOCK(inp); 1140171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1141163953Srrs return ENOENT; 1142163953Srrs } 1143163953Srrs } 1144163953Srrs SCTP_INP_RUNLOCK(inp); 1145163953Srrs /* Scoping things for v6 */ 1146164085Srrs if ((error = sa6_recoverscope(sin6)) != 0) { 1147164085Srrs SCTP_FREE_SONAME(sin6); 1148163953Srrs return (error); 1149164085Srrs } 1150163953Srrs (*addr) = (struct sockaddr *)sin6; 1151163953Srrs return (0); 1152163953Srrs} 1153163953Srrs 1154163953Srrsstatic int 1155163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1156163953Srrs{ 1157163953Srrs struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr; 1158163953Srrs int fnd; 1159163953Srrs struct sockaddr_in6 *sin_a6; 1160163953Srrs struct sctp_inpcb *inp; 1161163953Srrs struct sctp_tcb *stcb; 1162163953Srrs struct sctp_nets *net; 1163163953Srrs 1164163953Srrs int error; 1165163953Srrs 1166163953Srrs /* 1167163953Srrs * Do the malloc first in case it blocks. 1168163953Srrs */ 1169163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1170163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 1171163953Srrs /* UDP type and listeners will drop out here */ 1172171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); 1173163953Srrs return (ENOTCONN); 1174163953Srrs } 1175163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1176163953Srrs sin6->sin6_family = AF_INET6; 1177163953Srrs sin6->sin6_len = sizeof(*sin6); 1178163953Srrs 1179163953Srrs /* We must recapture incase we blocked */ 1180163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1181163953Srrs if (inp == NULL) { 1182163953Srrs SCTP_FREE_SONAME(sin6); 1183171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1184163953Srrs return ECONNRESET; 1185163953Srrs } 1186163953Srrs SCTP_INP_RLOCK(inp); 1187163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1188169420Srrs if (stcb) { 1189163953Srrs SCTP_TCB_LOCK(stcb); 1190169420Srrs } 1191163953Srrs SCTP_INP_RUNLOCK(inp); 1192163953Srrs if (stcb == NULL) { 1193163953Srrs SCTP_FREE_SONAME(sin6); 1194171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1195163953Srrs return ECONNRESET; 1196163953Srrs } 1197163953Srrs fnd = 0; 1198163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1199163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1200163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1201163953Srrs fnd = 1; 1202163953Srrs sin6->sin6_port = stcb->rport; 1203163953Srrs sin6->sin6_addr = sin_a6->sin6_addr; 1204163953Srrs break; 1205163953Srrs } 1206163953Srrs } 1207163953Srrs SCTP_TCB_UNLOCK(stcb); 1208163953Srrs if (!fnd) { 1209163953Srrs /* No IPv4 address */ 1210163953Srrs SCTP_FREE_SONAME(sin6); 1211171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1212163953Srrs return ENOENT; 1213163953Srrs } 1214163953Srrs if ((error = sa6_recoverscope(sin6)) != 0) 1215163953Srrs return (error); 1216163953Srrs *addr = (struct sockaddr *)sin6; 1217163953Srrs return (0); 1218163953Srrs} 1219163953Srrs 1220163953Srrsstatic int 1221163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1222163953Srrs{ 1223163953Srrs struct sockaddr *addr; 1224163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1225166086Srrs int error; 1226163953Srrs 1227171943Srrs if (inp6 == NULL) { 1228171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1229163953Srrs return EINVAL; 1230171943Srrs } 1231163953Srrs /* allow v6 addresses precedence */ 1232163953Srrs error = sctp6_getaddr(so, nam); 1233163953Srrs if (error) { 1234163953Srrs /* try v4 next if v6 failed */ 1235163953Srrs error = sctp_ingetaddr(so, nam); 1236163953Srrs if (error) { 1237163953Srrs return (error); 1238163953Srrs } 1239163953Srrs addr = *nam; 1240163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1241166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1242163953Srrs struct sockaddr_in6 sin6; 1243163953Srrs 1244163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1245163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1246168709Srrs 1247163953Srrs } 1248163953Srrs } 1249163953Srrs return (error); 1250163953Srrs} 1251163953Srrs 1252163953Srrs 1253163953Srrsstatic int 1254163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1255163953Srrs{ 1256163953Srrs struct sockaddr *addr = *nam; 1257163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1258166086Srrs int error; 1259163953Srrs 1260171943Srrs if (inp6 == NULL) { 1261171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1262163953Srrs return EINVAL; 1263171943Srrs } 1264163953Srrs /* allow v6 addresses precedence */ 1265163953Srrs error = sctp6_peeraddr(so, nam); 1266163953Srrs if (error) { 1267163953Srrs /* try v4 next if v6 failed */ 1268163953Srrs error = sctp_peeraddr(so, nam); 1269163953Srrs if (error) { 1270163953Srrs return (error); 1271163953Srrs } 1272163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1273166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1274163953Srrs struct sockaddr_in6 sin6; 1275163953Srrs 1276163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1277163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1278163953Srrs } 1279163953Srrs } 1280163953Srrs return error; 1281163953Srrs} 1282163953Srrs 1283163953Srrsstruct pr_usrreqs sctp6_usrreqs = { 1284163953Srrs .pru_abort = sctp6_abort, 1285163953Srrs .pru_accept = sctp_accept, 1286163953Srrs .pru_attach = sctp6_attach, 1287163953Srrs .pru_bind = sctp6_bind, 1288163953Srrs .pru_connect = sctp6_connect, 1289163953Srrs .pru_control = in6_control, 1290163953Srrs .pru_close = sctp6_close, 1291163953Srrs .pru_detach = sctp6_close, 1292163953Srrs .pru_sopoll = sopoll_generic, 1293178201Srrs .pru_flush = sctp_flush, 1294163953Srrs .pru_disconnect = sctp6_disconnect, 1295163953Srrs .pru_listen = sctp_listen, 1296163953Srrs .pru_peeraddr = sctp6_getpeeraddr, 1297163953Srrs .pru_send = sctp6_send, 1298163953Srrs .pru_shutdown = sctp_shutdown, 1299163953Srrs .pru_sockaddr = sctp6_in6getaddr, 1300163953Srrs .pru_sosend = sctp_sosend, 1301163953Srrs .pru_soreceive = sctp_soreceive 1302163953Srrs}; 1303