sctp6_usrreq.c revision 211944
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 211944 2010-08-28 17:59:51Z tuexen $"); 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? */ 417211944Stuexen if ((stcb->asoc.sctp_cmt_on_off == 1) && 418211944Stuexen (stcb->asoc.sctp_cmt_pf > 0)) { 419172091Srrs net->dest_state &= ~SCTP_ADDR_PF; 420172091Srrs SCTPDBG(SCTP_DEBUG_TIMER4, "Destination %p moved from PF to unreachable.\n", 421172091Srrs net); 422172091Srrs } 423172091Srrs net->error_count = net->failure_threshold + 1; 424172091Srrs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 425172091Srrs stcb, SCTP_FAILED_THRESHOLD, 426172091Srrs (void *)net, SCTP_SO_NOT_LOCKED); 427172091Srrs } 428172091Srrs SCTP_TCB_UNLOCK(stcb); 429172091Srrs } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) || 430172091Srrs (icmph->icmp6_code == ICMP_UNREACH_PORT)) { 431172091Srrs /* 432172091Srrs * Here the peer is either playing tricks on us, including 433172091Srrs * an address that belongs to someone who does not support 434172091Srrs * SCTP OR was a userland implementation that shutdown and 435172091Srrs * now is dead. In either case treat it like a OOTB abort 436172091Srrs * with no TCB 437172091Srrs */ 438172091Srrs reason = SCTP_PEER_FAULTY; 439172091Srrs sctp_abort_notification(stcb, reason, SCTP_SO_NOT_LOCKED); 440172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 441172091Srrs so = SCTP_INP_SO(inp); 442172091Srrs atomic_add_int(&stcb->asoc.refcnt, 1); 443172091Srrs SCTP_TCB_UNLOCK(stcb); 444172091Srrs SCTP_SOCKET_LOCK(so, 1); 445172091Srrs SCTP_TCB_LOCK(stcb); 446172091Srrs atomic_subtract_int(&stcb->asoc.refcnt, 1); 447172091Srrs#endif 448172091Srrs (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 449172091Srrs#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 450172091Srrs SCTP_SOCKET_UNLOCK(so, 1); 451172091Srrs /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */ 452172091Srrs#endif 453172091Srrs /* no need to unlock here, since the TCB is gone */ 454172091Srrs } else { 455172091Srrs SCTP_TCB_UNLOCK(stcb); 456172091Srrs } 457172091Srrs} 458172091Srrs 459172091Srrs 460172091Srrs 461163953Srrsvoid 462171259Sdelphijsctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d) 463163953Srrs{ 464163953Srrs struct sctphdr sh; 465163953Srrs struct ip6ctlparam *ip6cp = NULL; 466167598Srrs uint32_t vrf_id; 467163953Srrs 468167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 469167598Srrs 470163953Srrs if (pktdst->sa_family != AF_INET6 || 471163953Srrs pktdst->sa_len != sizeof(struct sockaddr_in6)) 472163953Srrs return; 473163953Srrs 474163953Srrs if ((unsigned)cmd >= PRC_NCMDS) 475163953Srrs return; 476163953Srrs if (PRC_IS_REDIRECT(cmd)) { 477163953Srrs d = NULL; 478163953Srrs } else if (inet6ctlerrmap[cmd] == 0) { 479163953Srrs return; 480163953Srrs } 481163953Srrs /* if the parameter is from icmp6, decode it. */ 482163953Srrs if (d != NULL) { 483163953Srrs ip6cp = (struct ip6ctlparam *)d; 484163953Srrs } else { 485163953Srrs ip6cp = (struct ip6ctlparam *)NULL; 486163953Srrs } 487163953Srrs 488163953Srrs if (ip6cp) { 489163953Srrs /* 490163953Srrs * XXX: We assume that when IPV6 is non NULL, M and OFF are 491163953Srrs * valid. 492163953Srrs */ 493163953Srrs /* check if we can safely examine src and dst ports */ 494163953Srrs struct sctp_inpcb *inp = NULL; 495163953Srrs struct sctp_tcb *stcb = NULL; 496163953Srrs struct sctp_nets *net = NULL; 497163953Srrs struct sockaddr_in6 final; 498163953Srrs 499165647Srrs if (ip6cp->ip6c_m == NULL) 500163953Srrs return; 501163953Srrs 502163953Srrs bzero(&sh, sizeof(sh)); 503163953Srrs bzero(&final, sizeof(final)); 504163953Srrs inp = NULL; 505163953Srrs net = NULL; 506163953Srrs m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh), 507163953Srrs (caddr_t)&sh); 508163953Srrs ip6cp->ip6c_src->sin6_port = sh.src_port; 509163953Srrs final.sin6_len = sizeof(final); 510163953Srrs final.sin6_family = AF_INET6; 511163953Srrs final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr; 512163953Srrs final.sin6_port = sh.dest_port; 513163953Srrs stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src, 514163953Srrs (struct sockaddr *)&final, 515167598Srrs &inp, &net, 1, vrf_id); 516163953Srrs /* inp's ref-count increased && stcb locked */ 517163953Srrs if (stcb != NULL && inp && (inp->sctp_socket != NULL)) { 518163953Srrs if (cmd == PRC_MSGSIZE) { 519163953Srrs sctp6_notify_mbuf(inp, 520163953Srrs ip6cp->ip6c_icmp6, 521163953Srrs &sh, 522163953Srrs stcb, 523163953Srrs net); 524163953Srrs /* inp's ref-count reduced && stcb unlocked */ 525163953Srrs } else { 526172091Srrs sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh, 527163953Srrs (struct sockaddr *)&final, 528163953Srrs stcb, net); 529163953Srrs /* inp's ref-count reduced && stcb unlocked */ 530163953Srrs } 531163953Srrs } else { 532163953Srrs if (PRC_IS_REDIRECT(cmd) && inp) { 533163953Srrs in6_rtchange((struct in6pcb *)inp, 534163953Srrs inet6ctlerrmap[cmd]); 535163953Srrs } 536163953Srrs if (inp) { 537163953Srrs /* reduce inp's ref-count */ 538163953Srrs SCTP_INP_WLOCK(inp); 539163953Srrs SCTP_INP_DECR_REF(inp); 540163953Srrs SCTP_INP_WUNLOCK(inp); 541163953Srrs } 542163953Srrs if (stcb) 543163953Srrs SCTP_TCB_UNLOCK(stcb); 544163953Srrs } 545163953Srrs } 546163953Srrs} 547163953Srrs 548163953Srrs/* 549163953Srrs * this routine can probably be collasped into the one in sctp_userreq.c 550163953Srrs * since they do the same thing and now we lookup with a sockaddr 551163953Srrs */ 552163953Srrsstatic int 553163953Srrssctp6_getcred(SYSCTL_HANDLER_ARGS) 554163953Srrs{ 555164085Srrs struct xucred xuc; 556163953Srrs struct sockaddr_in6 addrs[2]; 557163953Srrs struct sctp_inpcb *inp; 558163953Srrs struct sctp_nets *net; 559163953Srrs struct sctp_tcb *stcb; 560164085Srrs int error; 561167598Srrs uint32_t vrf_id; 562163953Srrs 563167598Srrs vrf_id = SCTP_DEFAULT_VRFID; 564167598Srrs 565170587Srwatson error = priv_check(req->td, PRIV_NETINET_GETCRED); 566163953Srrs if (error) 567163953Srrs return (error); 568163953Srrs 569171943Srrs if (req->newlen != sizeof(addrs)) { 570171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 571163953Srrs return (EINVAL); 572171943Srrs } 573171943Srrs if (req->oldlen != sizeof(struct ucred)) { 574171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 575163953Srrs return (EINVAL); 576171943Srrs } 577163953Srrs error = SYSCTL_IN(req, addrs, sizeof(addrs)); 578163953Srrs if (error) 579163953Srrs return (error); 580163953Srrs 581163953Srrs stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]), 582163953Srrs sin6tosa(&addrs[1]), 583167598Srrs &inp, &net, 1, vrf_id); 584163953Srrs if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 585164085Srrs if ((inp != NULL) && (stcb == NULL)) { 586164085Srrs /* reduce ref-count */ 587163953Srrs SCTP_INP_WLOCK(inp); 588163953Srrs SCTP_INP_DECR_REF(inp); 589164085Srrs goto cred_can_cont; 590163953Srrs } 591171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 592164085Srrs error = ENOENT; 593163953Srrs goto out; 594163953Srrs } 595163953Srrs SCTP_TCB_UNLOCK(stcb); 596164085Srrs /* 597164085Srrs * We use the write lock here, only since in the error leg we need 598164085Srrs * it. If we used RLOCK, then we would have to 599164085Srrs * wlock/decr/unlock/rlock. Which in theory could create a hole. 600164085Srrs * Better to use higher wlock. 601164085Srrs */ 602164085Srrs SCTP_INP_WLOCK(inp); 603164085Srrscred_can_cont: 604164085Srrs error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 605164085Srrs if (error) { 606164085Srrs SCTP_INP_WUNLOCK(inp); 607164085Srrs goto out; 608164085Srrs } 609164085Srrs cru2x(inp->sctp_socket->so_cred, &xuc); 610164085Srrs SCTP_INP_WUNLOCK(inp); 611164085Srrs error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 612163953Srrsout: 613163953Srrs return (error); 614163953Srrs} 615163953Srrs 616163953SrrsSYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 617163953Srrs 0, 0, 618163953Srrs sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 619163953Srrs 620163953Srrs 621163953Srrs/* This is the same as the sctp_abort() could be made common */ 622163953Srrsstatic void 623163953Srrssctp6_abort(struct socket *so) 624163953Srrs{ 625163953Srrs struct sctp_inpcb *inp; 626163953Srrs uint32_t flags; 627163953Srrs 628163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 629172091Srrs if (inp == 0) { 630172091Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 631163953Srrs return; 632172091Srrs } 633163953Srrssctp_must_try_again: 634163953Srrs flags = inp->sctp_flags; 635163953Srrs#ifdef SCTP_LOG_CLOSING 636163953Srrs sctp_log_closing(inp, NULL, 17); 637163953Srrs#endif 638163953Srrs if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 639163953Srrs (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 640163953Srrs#ifdef SCTP_LOG_CLOSING 641163953Srrs sctp_log_closing(inp, NULL, 16); 642163953Srrs#endif 643169380Srrs sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 644169380Srrs SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 645163953Srrs SOCK_LOCK(so); 646167695Srrs SCTP_SB_CLEAR(so->so_snd); 647163953Srrs /* 648163953Srrs * same for the rcv ones, they are only here for the 649163953Srrs * accounting/select. 650163953Srrs */ 651167695Srrs SCTP_SB_CLEAR(so->so_rcv); 652167695Srrs /* Now null out the reference, we are completely detached. */ 653163953Srrs so->so_pcb = NULL; 654163953Srrs SOCK_UNLOCK(so); 655163953Srrs } else { 656163953Srrs flags = inp->sctp_flags; 657163953Srrs if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 658163953Srrs goto sctp_must_try_again; 659163953Srrs } 660163953Srrs } 661163953Srrs return; 662163953Srrs} 663163953Srrs 664163953Srrsstatic int 665163953Srrssctp6_attach(struct socket *so, int proto, struct thread *p) 666163953Srrs{ 667163953Srrs struct in6pcb *inp6; 668166086Srrs int error; 669163953Srrs struct sctp_inpcb *inp; 670170205Srrs uint32_t vrf_id = SCTP_DEFAULT_VRFID; 671163953Srrs 672163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 673171943Srrs if (inp != NULL) { 674171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 675163953Srrs return EINVAL; 676171943Srrs } 677163953Srrs if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 678179783Srrs error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 679163953Srrs if (error) 680163953Srrs return error; 681163953Srrs } 682170205Srrs error = sctp_inpcb_alloc(so, vrf_id); 683163953Srrs if (error) 684163953Srrs return error; 685163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 686170205Srrs SCTP_INP_WLOCK(inp); 687163953Srrs inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 688163953Srrs inp6 = (struct in6pcb *)inp; 689163953Srrs 690163953Srrs inp6->inp_vflag |= INP_IPV6; 691163953Srrs inp6->in6p_hops = -1; /* use kernel default */ 692163953Srrs inp6->in6p_cksum = -1; /* just to be sure */ 693163953Srrs#ifdef INET 694163953Srrs /* 695163953Srrs * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 696163953Srrs * socket as well, because the socket may be bound to an IPv6 697163953Srrs * wildcard address, which may match an IPv4-mapped IPv6 address. 698163953Srrs */ 699197288Srrs inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 700163953Srrs#endif 701163953Srrs /* 702163953Srrs * Hmm what about the IPSEC stuff that is missing here but in 703163953Srrs * sctp_attach()? 704163953Srrs */ 705170205Srrs SCTP_INP_WUNLOCK(inp); 706163953Srrs return 0; 707163953Srrs} 708163953Srrs 709163953Srrsstatic int 710163953Srrssctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 711163953Srrs{ 712163953Srrs struct sctp_inpcb *inp; 713163953Srrs struct in6pcb *inp6; 714166086Srrs int error; 715163953Srrs 716163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 717171943Srrs if (inp == 0) { 718171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 719163953Srrs return EINVAL; 720171943Srrs } 721170056Srrs if (addr) { 722170056Srrs if ((addr->sa_family == AF_INET6) && 723170056Srrs (addr->sa_len != sizeof(struct sockaddr_in6))) { 724171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 725170056Srrs return EINVAL; 726170056Srrs } 727170056Srrs if ((addr->sa_family == AF_INET) && 728170056Srrs (addr->sa_len != sizeof(struct sockaddr_in))) { 729171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 730170056Srrs return EINVAL; 731170056Srrs } 732170056Srrs } 733163953Srrs inp6 = (struct in6pcb *)inp; 734163953Srrs inp6->inp_vflag &= ~INP_IPV4; 735163953Srrs inp6->inp_vflag |= INP_IPV6; 736166023Srrs if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { 737163953Srrs if (addr->sa_family == AF_INET) { 738163953Srrs /* binding v4 addr to v6 socket, so reset flags */ 739163953Srrs inp6->inp_vflag |= INP_IPV4; 740163953Srrs inp6->inp_vflag &= ~INP_IPV6; 741163953Srrs } else { 742163953Srrs struct sockaddr_in6 *sin6_p; 743163953Srrs 744163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 745163953Srrs 746163953Srrs if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 747163953Srrs inp6->inp_vflag |= INP_IPV4; 748163953Srrs } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 749163953Srrs struct sockaddr_in sin; 750163953Srrs 751163953Srrs in6_sin6_2_sin(&sin, sin6_p); 752163953Srrs inp6->inp_vflag |= INP_IPV4; 753163953Srrs inp6->inp_vflag &= ~INP_IPV6; 754171572Srrs error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); 755163953Srrs return error; 756163953Srrs } 757163953Srrs } 758163953Srrs } else if (addr != NULL) { 759163953Srrs /* IPV6_V6ONLY socket */ 760163953Srrs if (addr->sa_family == AF_INET) { 761163953Srrs /* can't bind v4 addr to v6 only socket! */ 762171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 763163953Srrs return EINVAL; 764163953Srrs } else { 765163953Srrs struct sockaddr_in6 *sin6_p; 766163953Srrs 767163953Srrs sin6_p = (struct sockaddr_in6 *)addr; 768163953Srrs 769172091Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 770163953Srrs /* can't bind v4-mapped addrs either! */ 771163953Srrs /* NOTE: we don't support SIIT */ 772171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 773172091Srrs return EINVAL; 774172091Srrs } 775163953Srrs } 776163953Srrs } 777171572Srrs error = sctp_inpcb_bind(so, addr, NULL, p); 778163953Srrs return error; 779163953Srrs} 780163953Srrs 781163953Srrs 782163953Srrsstatic void 783163953Srrssctp6_close(struct socket *so) 784163953Srrs{ 785171990Srrs sctp_close(so); 786163953Srrs} 787163953Srrs 788167598Srrs/* This could be made common with sctp_detach() since they are identical */ 789163953Srrs 790168709Srrsstatic 791168709Srrsint 792163953Srrssctp6_disconnect(struct socket *so) 793163953Srrs{ 794171990Srrs return (sctp_disconnect(so)); 795163953Srrs} 796163953Srrs 797168709Srrs 798163953Srrsint 799163953Srrssctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 800163953Srrs struct mbuf *control, struct thread *p); 801163953Srrs 802163953Srrs 803163953Srrsstatic int 804163953Srrssctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 805163953Srrs struct mbuf *control, struct thread *p) 806163953Srrs{ 807163953Srrs struct sctp_inpcb *inp; 808163953Srrs struct inpcb *in_inp; 809163953Srrs struct in6pcb *inp6; 810163953Srrs 811163953Srrs#ifdef INET 812163953Srrs struct sockaddr_in6 *sin6; 813163953Srrs 814163953Srrs#endif /* INET */ 815163953Srrs /* No SPL needed since sctp_output does this */ 816163953Srrs 817163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 818163953Srrs if (inp == NULL) { 819163953Srrs if (control) { 820169352Srrs SCTP_RELEASE_PKT(control); 821163953Srrs control = NULL; 822163953Srrs } 823169352Srrs SCTP_RELEASE_PKT(m); 824171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 825163953Srrs return EINVAL; 826163953Srrs } 827163953Srrs in_inp = (struct inpcb *)inp; 828163953Srrs inp6 = (struct in6pcb *)inp; 829163953Srrs /* 830163953Srrs * For the TCP model we may get a NULL addr, if we are a connected 831163953Srrs * socket thats ok. 832163953Srrs */ 833163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 834163953Srrs (addr == NULL)) { 835163953Srrs goto connected_type; 836163953Srrs } 837163953Srrs if (addr == NULL) { 838169352Srrs SCTP_RELEASE_PKT(m); 839163953Srrs if (control) { 840169352Srrs SCTP_RELEASE_PKT(control); 841163953Srrs control = NULL; 842163953Srrs } 843171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); 844163953Srrs return (EDESTADDRREQ); 845163953Srrs } 846163953Srrs#ifdef INET 847163953Srrs sin6 = (struct sockaddr_in6 *)addr; 848166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 849163953Srrs /* 850163953Srrs * if IPV6_V6ONLY flag, we discard datagrams destined to a 851163953Srrs * v4 addr or v4-mapped addr 852163953Srrs */ 853163953Srrs if (addr->sa_family == AF_INET) { 854171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 855163953Srrs return EINVAL; 856163953Srrs } 857163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 858171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 859163953Srrs return EINVAL; 860163953Srrs } 861163953Srrs } 862163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 863197288Srrs if (!MODULE_GLOBAL(ip6_v6only)) { 864163953Srrs struct sockaddr_in sin; 865163953Srrs 866163953Srrs /* convert v4-mapped into v4 addr and send */ 867163953Srrs in6_sin6_2_sin(&sin, sin6); 868163953Srrs return sctp_sendm(so, flags, m, (struct sockaddr *)&sin, 869163953Srrs control, p); 870163953Srrs } else { 871163953Srrs /* mapped addresses aren't enabled */ 872171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 873163953Srrs return EINVAL; 874163953Srrs } 875163953Srrs } 876163953Srrs#endif /* INET */ 877163953Srrsconnected_type: 878163953Srrs /* now what about control */ 879163953Srrs if (control) { 880163953Srrs if (inp->control) { 881169420Srrs SCTP_PRINTF("huh? control set?\n"); 882169352Srrs SCTP_RELEASE_PKT(inp->control); 883163953Srrs inp->control = NULL; 884163953Srrs } 885163953Srrs inp->control = control; 886163953Srrs } 887163953Srrs /* Place the data */ 888163953Srrs if (inp->pkt) { 889165647Srrs SCTP_BUF_NEXT(inp->pkt_last) = m; 890163953Srrs inp->pkt_last = m; 891163953Srrs } else { 892163953Srrs inp->pkt_last = inp->pkt = m; 893163953Srrs } 894163953Srrs if ( 895163953Srrs /* FreeBSD and MacOSX uses a flag passed */ 896163953Srrs ((flags & PRUS_MORETOCOME) == 0) 897163953Srrs ) { 898163953Srrs /* 899163953Srrs * note with the current version this code will only be used 900163953Srrs * by OpenBSD, NetBSD and FreeBSD have methods for 901163953Srrs * re-defining sosend() to use sctp_sosend(). One can 902163953Srrs * optionaly switch back to this code (by changing back the 903163953Srrs * defininitions but this is not advisable. 904163953Srrs */ 905163953Srrs int ret; 906163953Srrs 907163953Srrs ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 908163953Srrs inp->pkt = NULL; 909163953Srrs inp->control = NULL; 910163953Srrs return (ret); 911163953Srrs } else { 912163953Srrs return (0); 913163953Srrs } 914163953Srrs} 915163953Srrs 916163953Srrsstatic int 917163953Srrssctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 918163953Srrs{ 919167598Srrs uint32_t vrf_id; 920163953Srrs int error = 0; 921163953Srrs struct sctp_inpcb *inp; 922163953Srrs struct in6pcb *inp6; 923163953Srrs struct sctp_tcb *stcb; 924163953Srrs 925163953Srrs#ifdef INET 926163953Srrs struct sockaddr_in6 *sin6; 927163953Srrs struct sockaddr_storage ss; 928163953Srrs 929163953Srrs#endif /* INET */ 930163953Srrs 931163953Srrs inp6 = (struct in6pcb *)so->so_pcb; 932163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 933163953Srrs if (inp == 0) { 934171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 935163953Srrs return (ECONNRESET); /* I made the same as TCP since we are 936163953Srrs * not setup? */ 937163953Srrs } 938170056Srrs if (addr == NULL) { 939171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 940170056Srrs return (EINVAL); 941170056Srrs } 942170056Srrs if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) { 943171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 944170056Srrs return (EINVAL); 945170056Srrs } 946170056Srrs if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) { 947171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 948170056Srrs return (EINVAL); 949170056Srrs } 950168299Srrs vrf_id = inp->def_vrf_id; 951163953Srrs SCTP_ASOC_CREATE_LOCK(inp); 952163953Srrs SCTP_INP_RLOCK(inp); 953163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 954163953Srrs SCTP_PCB_FLAGS_UNBOUND) { 955163953Srrs /* Bind a ephemeral port */ 956163953Srrs SCTP_INP_RUNLOCK(inp); 957163953Srrs error = sctp6_bind(so, NULL, p); 958163953Srrs if (error) { 959163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 960163953Srrs 961163953Srrs return (error); 962163953Srrs } 963163953Srrs SCTP_INP_RLOCK(inp); 964163953Srrs } 965163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 966163953Srrs (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 967163953Srrs /* We are already connected AND the TCP model */ 968163953Srrs SCTP_INP_RUNLOCK(inp); 969163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 970171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); 971163953Srrs return (EADDRINUSE); 972163953Srrs } 973163953Srrs#ifdef INET 974163953Srrs sin6 = (struct sockaddr_in6 *)addr; 975166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 976163953Srrs /* 977163953Srrs * if IPV6_V6ONLY flag, ignore connections destined to a v4 978163953Srrs * addr or v4-mapped addr 979163953Srrs */ 980163953Srrs if (addr->sa_family == AF_INET) { 981163953Srrs SCTP_INP_RUNLOCK(inp); 982163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 983171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 984163953Srrs return EINVAL; 985163953Srrs } 986163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 987163953Srrs SCTP_INP_RUNLOCK(inp); 988163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 989171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 990163953Srrs return EINVAL; 991163953Srrs } 992163953Srrs } 993163953Srrs if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 994197288Srrs if (!MODULE_GLOBAL(ip6_v6only)) { 995163953Srrs /* convert v4-mapped into v4 addr */ 996163953Srrs in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6); 997163953Srrs addr = (struct sockaddr *)&ss; 998163953Srrs } else { 999163953Srrs /* mapped addresses aren't enabled */ 1000163953Srrs SCTP_INP_RUNLOCK(inp); 1001163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1002171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1003163953Srrs return EINVAL; 1004163953Srrs } 1005163953Srrs } else 1006163953Srrs#endif /* INET */ 1007163953Srrs addr = addr; /* for true v6 address case */ 1008163953Srrs 1009163953Srrs /* Now do we connect? */ 1010163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1011163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1012169420Srrs if (stcb) { 1013163953Srrs SCTP_TCB_UNLOCK(stcb); 1014169420Srrs } 1015163953Srrs SCTP_INP_RUNLOCK(inp); 1016163953Srrs } else { 1017163953Srrs SCTP_INP_RUNLOCK(inp); 1018163953Srrs SCTP_INP_WLOCK(inp); 1019163953Srrs SCTP_INP_INCR_REF(inp); 1020163953Srrs SCTP_INP_WUNLOCK(inp); 1021163953Srrs stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 1022163953Srrs if (stcb == NULL) { 1023163953Srrs SCTP_INP_WLOCK(inp); 1024163953Srrs SCTP_INP_DECR_REF(inp); 1025163953Srrs SCTP_INP_WUNLOCK(inp); 1026163953Srrs } 1027163953Srrs } 1028163953Srrs 1029163953Srrs if (stcb != NULL) { 1030163953Srrs /* Already have or am bring up an association */ 1031163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1032163953Srrs SCTP_TCB_UNLOCK(stcb); 1033171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); 1034163953Srrs return (EALREADY); 1035163953Srrs } 1036163953Srrs /* We are GOOD to go */ 1037206137Stuexen stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p); 1038163953Srrs SCTP_ASOC_CREATE_UNLOCK(inp); 1039163953Srrs if (stcb == NULL) { 1040163953Srrs /* Gak! no memory */ 1041163953Srrs return (error); 1042163953Srrs } 1043163953Srrs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 1044163953Srrs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 1045163953Srrs /* Set the connected flag so we can queue data */ 1046163953Srrs soisconnecting(so); 1047163953Srrs } 1048163953Srrs stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 1049169420Srrs (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 1050163953Srrs 1051163953Srrs /* initialize authentication parameters for the assoc */ 1052163953Srrs sctp_initialize_auth_params(inp, stcb); 1053163953Srrs 1054172090Srrs sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 1055163953Srrs SCTP_TCB_UNLOCK(stcb); 1056163953Srrs return error; 1057163953Srrs} 1058163953Srrs 1059163953Srrsstatic int 1060163953Srrssctp6_getaddr(struct socket *so, struct sockaddr **addr) 1061163953Srrs{ 1062163953Srrs struct sockaddr_in6 *sin6; 1063163953Srrs struct sctp_inpcb *inp; 1064167598Srrs uint32_t vrf_id; 1065167598Srrs struct sctp_ifa *sctp_ifa; 1066163953Srrs 1067163953Srrs int error; 1068163953Srrs 1069163953Srrs /* 1070163953Srrs * Do the malloc first in case it blocks. 1071163953Srrs */ 1072163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1073211944Stuexen if (sin6 == NULL) 1074211944Stuexen return ENOMEM; 1075163953Srrs sin6->sin6_family = AF_INET6; 1076163953Srrs sin6->sin6_len = sizeof(*sin6); 1077163953Srrs 1078163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1079163953Srrs if (inp == NULL) { 1080163953Srrs SCTP_FREE_SONAME(sin6); 1081171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1082163953Srrs return ECONNRESET; 1083163953Srrs } 1084163953Srrs SCTP_INP_RLOCK(inp); 1085163953Srrs sin6->sin6_port = inp->sctp_lport; 1086163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 1087163953Srrs /* For the bound all case you get back 0 */ 1088163953Srrs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 1089163953Srrs struct sctp_tcb *stcb; 1090163953Srrs struct sockaddr_in6 *sin_a6; 1091163953Srrs struct sctp_nets *net; 1092163953Srrs int fnd; 1093163953Srrs 1094163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1095163953Srrs if (stcb == NULL) { 1096163953Srrs goto notConn6; 1097163953Srrs } 1098163953Srrs fnd = 0; 1099163953Srrs sin_a6 = NULL; 1100163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1101163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1102164085Srrs if (sin_a6 == NULL) 1103164085Srrs /* this will make coverity happy */ 1104164085Srrs continue; 1105164085Srrs 1106163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1107163953Srrs fnd = 1; 1108163953Srrs break; 1109163953Srrs } 1110163953Srrs } 1111163953Srrs if ((!fnd) || (sin_a6 == NULL)) { 1112163953Srrs /* punt */ 1113163953Srrs goto notConn6; 1114163953Srrs } 1115168299Srrs vrf_id = inp->def_vrf_id; 1116168299Srrs sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id); 1117167598Srrs if (sctp_ifa) { 1118167598Srrs sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 1119167598Srrs } 1120163953Srrs } else { 1121163953Srrs /* For the bound all case you get back 0 */ 1122163953Srrs notConn6: 1123163953Srrs memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1124163953Srrs } 1125163953Srrs } else { 1126163953Srrs /* Take the first IPv6 address in the list */ 1127163953Srrs struct sctp_laddr *laddr; 1128163953Srrs int fnd = 0; 1129163953Srrs 1130163953Srrs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1131167598Srrs if (laddr->ifa->address.sa.sa_family == AF_INET6) { 1132163953Srrs struct sockaddr_in6 *sin_a; 1133163953Srrs 1134167598Srrs sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; 1135163953Srrs sin6->sin6_addr = sin_a->sin6_addr; 1136163953Srrs fnd = 1; 1137163953Srrs break; 1138163953Srrs } 1139163953Srrs } 1140163953Srrs if (!fnd) { 1141163953Srrs SCTP_FREE_SONAME(sin6); 1142163953Srrs SCTP_INP_RUNLOCK(inp); 1143171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1144163953Srrs return ENOENT; 1145163953Srrs } 1146163953Srrs } 1147163953Srrs SCTP_INP_RUNLOCK(inp); 1148163953Srrs /* Scoping things for v6 */ 1149164085Srrs if ((error = sa6_recoverscope(sin6)) != 0) { 1150164085Srrs SCTP_FREE_SONAME(sin6); 1151163953Srrs return (error); 1152164085Srrs } 1153163953Srrs (*addr) = (struct sockaddr *)sin6; 1154163953Srrs return (0); 1155163953Srrs} 1156163953Srrs 1157163953Srrsstatic int 1158163953Srrssctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1159163953Srrs{ 1160163953Srrs struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr; 1161163953Srrs int fnd; 1162163953Srrs struct sockaddr_in6 *sin_a6; 1163163953Srrs struct sctp_inpcb *inp; 1164163953Srrs struct sctp_tcb *stcb; 1165163953Srrs struct sctp_nets *net; 1166163953Srrs 1167163953Srrs int error; 1168163953Srrs 1169163953Srrs /* 1170163953Srrs * Do the malloc first in case it blocks. 1171163953Srrs */ 1172163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1173163953Srrs if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) { 1174163953Srrs /* UDP type and listeners will drop out here */ 1175171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); 1176163953Srrs return (ENOTCONN); 1177163953Srrs } 1178163953Srrs SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1179211944Stuexen if (sin6 == NULL) 1180211944Stuexen return (ENOMEM); 1181163953Srrs sin6->sin6_family = AF_INET6; 1182163953Srrs sin6->sin6_len = sizeof(*sin6); 1183163953Srrs 1184163953Srrs /* We must recapture incase we blocked */ 1185163953Srrs inp = (struct sctp_inpcb *)so->so_pcb; 1186163953Srrs if (inp == NULL) { 1187163953Srrs SCTP_FREE_SONAME(sin6); 1188171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1189163953Srrs return ECONNRESET; 1190163953Srrs } 1191163953Srrs SCTP_INP_RLOCK(inp); 1192163953Srrs stcb = LIST_FIRST(&inp->sctp_asoc_list); 1193169420Srrs if (stcb) { 1194163953Srrs SCTP_TCB_LOCK(stcb); 1195169420Srrs } 1196163953Srrs SCTP_INP_RUNLOCK(inp); 1197163953Srrs if (stcb == NULL) { 1198163953Srrs SCTP_FREE_SONAME(sin6); 1199171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1200163953Srrs return ECONNRESET; 1201163953Srrs } 1202163953Srrs fnd = 0; 1203163953Srrs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1204163953Srrs sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1205163953Srrs if (sin_a6->sin6_family == AF_INET6) { 1206163953Srrs fnd = 1; 1207163953Srrs sin6->sin6_port = stcb->rport; 1208163953Srrs sin6->sin6_addr = sin_a6->sin6_addr; 1209163953Srrs break; 1210163953Srrs } 1211163953Srrs } 1212163953Srrs SCTP_TCB_UNLOCK(stcb); 1213163953Srrs if (!fnd) { 1214163953Srrs /* No IPv4 address */ 1215163953Srrs SCTP_FREE_SONAME(sin6); 1216171943Srrs SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1217163953Srrs return ENOENT; 1218163953Srrs } 1219163953Srrs if ((error = sa6_recoverscope(sin6)) != 0) 1220163953Srrs return (error); 1221163953Srrs *addr = (struct sockaddr *)sin6; 1222163953Srrs return (0); 1223163953Srrs} 1224163953Srrs 1225163953Srrsstatic int 1226163953Srrssctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1227163953Srrs{ 1228163953Srrs struct sockaddr *addr; 1229163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1230166086Srrs int error; 1231163953Srrs 1232171943Srrs if (inp6 == NULL) { 1233171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1234163953Srrs return EINVAL; 1235171943Srrs } 1236163953Srrs /* allow v6 addresses precedence */ 1237163953Srrs error = sctp6_getaddr(so, nam); 1238163953Srrs if (error) { 1239163953Srrs /* try v4 next if v6 failed */ 1240163953Srrs error = sctp_ingetaddr(so, nam); 1241163953Srrs if (error) { 1242163953Srrs return (error); 1243163953Srrs } 1244163953Srrs addr = *nam; 1245163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1246166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1247163953Srrs struct sockaddr_in6 sin6; 1248163953Srrs 1249163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1250163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1251168709Srrs 1252163953Srrs } 1253163953Srrs } 1254163953Srrs return (error); 1255163953Srrs} 1256163953Srrs 1257163953Srrs 1258163953Srrsstatic int 1259163953Srrssctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1260163953Srrs{ 1261163953Srrs struct sockaddr *addr = *nam; 1262163953Srrs struct in6pcb *inp6 = sotoin6pcb(so); 1263166086Srrs int error; 1264163953Srrs 1265171943Srrs if (inp6 == NULL) { 1266171943Srrs SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1267163953Srrs return EINVAL; 1268171943Srrs } 1269163953Srrs /* allow v6 addresses precedence */ 1270163953Srrs error = sctp6_peeraddr(so, nam); 1271163953Srrs if (error) { 1272163953Srrs /* try v4 next if v6 failed */ 1273163953Srrs error = sctp_peeraddr(so, nam); 1274163953Srrs if (error) { 1275163953Srrs return (error); 1276163953Srrs } 1277163953Srrs /* if I'm V6ONLY, convert it to v4-mapped */ 1278166023Srrs if (SCTP_IPV6_V6ONLY(inp6)) { 1279163953Srrs struct sockaddr_in6 sin6; 1280163953Srrs 1281163953Srrs in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6); 1282163953Srrs memcpy(addr, &sin6, sizeof(struct sockaddr_in6)); 1283163953Srrs } 1284163953Srrs } 1285163953Srrs return error; 1286163953Srrs} 1287163953Srrs 1288163953Srrsstruct pr_usrreqs sctp6_usrreqs = { 1289163953Srrs .pru_abort = sctp6_abort, 1290163953Srrs .pru_accept = sctp_accept, 1291163953Srrs .pru_attach = sctp6_attach, 1292163953Srrs .pru_bind = sctp6_bind, 1293163953Srrs .pru_connect = sctp6_connect, 1294163953Srrs .pru_control = in6_control, 1295163953Srrs .pru_close = sctp6_close, 1296163953Srrs .pru_detach = sctp6_close, 1297163953Srrs .pru_sopoll = sopoll_generic, 1298178201Srrs .pru_flush = sctp_flush, 1299163953Srrs .pru_disconnect = sctp6_disconnect, 1300163953Srrs .pru_listen = sctp_listen, 1301163953Srrs .pru_peeraddr = sctp6_getpeeraddr, 1302163953Srrs .pru_send = sctp6_send, 1303163953Srrs .pru_shutdown = sctp_shutdown, 1304163953Srrs .pru_sockaddr = sctp6_in6getaddr, 1305163953Srrs .pru_sosend = sctp_sosend, 1306163953Srrs .pru_soreceive = sctp_soreceive 1307163953Srrs}; 1308