10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51676Sjpk * Common Development and Distribution License (the "License"). 61676Sjpk * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211735Skcpoon 220Sstevel@tonic-gate /* 23*12869SKacheong.Poon@Sun.COM * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <sys/types.h> 270Sstevel@tonic-gate #include <sys/systm.h> 280Sstevel@tonic-gate #include <sys/stream.h> 290Sstevel@tonic-gate #include <sys/cmn_err.h> 300Sstevel@tonic-gate #include <sys/strsubr.h> 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <netinet/in.h> 330Sstevel@tonic-gate #include <netinet/ip6.h> 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <inet/common.h> 360Sstevel@tonic-gate #include <inet/ip.h> 370Sstevel@tonic-gate #include <inet/mib2.h> 383448Sdh155122 #include <inet/ipclassifier.h> 390Sstevel@tonic-gate #include "sctp_impl.h" 400Sstevel@tonic-gate 410Sstevel@tonic-gate void 420Sstevel@tonic-gate sctp_return_heartbeat(sctp_t *sctp, sctp_chunk_hdr_t *hbcp, mblk_t *mp) 430Sstevel@tonic-gate { 440Sstevel@tonic-gate mblk_t *smp; 450Sstevel@tonic-gate sctp_chunk_hdr_t *cp; 460Sstevel@tonic-gate ipha_t *iniph; 470Sstevel@tonic-gate ip6_t *inip6h; 480Sstevel@tonic-gate int isv4; 490Sstevel@tonic-gate in6_addr_t addr; 500Sstevel@tonic-gate sctp_faddr_t *fp; 510Sstevel@tonic-gate uint16_t len; 523448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 530Sstevel@tonic-gate 540Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_ibchunks); 550Sstevel@tonic-gate 560Sstevel@tonic-gate /* Update the faddr for the src addr */ 570Sstevel@tonic-gate isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); 580Sstevel@tonic-gate if (isv4) { 590Sstevel@tonic-gate iniph = (ipha_t *)mp->b_rptr; 600Sstevel@tonic-gate IN6_IPADDR_TO_V4MAPPED(iniph->ipha_src, &addr); 610Sstevel@tonic-gate } else { 620Sstevel@tonic-gate inip6h = (ip6_t *)mp->b_rptr; 630Sstevel@tonic-gate addr = inip6h->ip6_src; 640Sstevel@tonic-gate } 650Sstevel@tonic-gate fp = sctp_lookup_faddr(sctp, &addr); 6611042SErik.Nordmark@Sun.COM /* If the source address is bogus we silently drop the packet */ 6711042SErik.Nordmark@Sun.COM if (fp == NULL) { 6811042SErik.Nordmark@Sun.COM dprint(1, 6911042SErik.Nordmark@Sun.COM ("sctp_return_heartbeat: %p bogus hb from %x:%x:%x:%x\n", 7011042SErik.Nordmark@Sun.COM (void *)sctp, SCTP_PRINTADDR(addr))); 7111042SErik.Nordmark@Sun.COM SCTP_KSTAT(sctps, sctp_return_hb_failed); 7211042SErik.Nordmark@Sun.COM return; 7311042SErik.Nordmark@Sun.COM } 740Sstevel@tonic-gate dprint(3, ("sctp_return_heartbeat: %p got hb from %x:%x:%x:%x\n", 751676Sjpk (void *)sctp, SCTP_PRINTADDR(addr))); 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * XXX It's really tempting to reuse the heartbeat mblk. But 790Sstevel@tonic-gate * this complicates processing in sctp_dispatch (i.e. it will 800Sstevel@tonic-gate * screw up sctp_next_chunk since we will set the chunk 810Sstevel@tonic-gate * header's length into network byte-order), and if we ever 820Sstevel@tonic-gate * encounter a heartbeat bundled with other chunks... 830Sstevel@tonic-gate * So we take the slower-but-safe route. 840Sstevel@tonic-gate */ 850Sstevel@tonic-gate len = ntohs(hbcp->sch_len); 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* Create an IP header, returning to the src addr from the heartbt */ 880Sstevel@tonic-gate smp = sctp_make_mp(sctp, fp, len); 890Sstevel@tonic-gate if (smp == NULL) { 903448Sdh155122 SCTP_KSTAT(sctps, sctp_return_hb_failed); 910Sstevel@tonic-gate return; 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate cp = (sctp_chunk_hdr_t *)smp->b_wptr; 950Sstevel@tonic-gate cp->sch_id = CHUNK_HEARTBEAT_ACK; 960Sstevel@tonic-gate cp->sch_flags = 0; 970Sstevel@tonic-gate cp->sch_len = htons(len); 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* Copy the information field from the heartbeat */ 1000Sstevel@tonic-gate bcopy((void *)(hbcp + 1), (void *)(cp + 1), len - sizeof (*cp)); 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate smp->b_wptr += len; 1030Sstevel@tonic-gate 10411042SErik.Nordmark@Sun.COM BUMP_LOCAL(sctp->sctp_obchunks); 1050Sstevel@tonic-gate 10611042SErik.Nordmark@Sun.COM sctp_set_iplen(sctp, smp, fp->ixa); 10711042SErik.Nordmark@Sun.COM (void) conn_ip_output(smp, fp->ixa); 10811042SErik.Nordmark@Sun.COM BUMP_LOCAL(sctp->sctp_opkts); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * The data section of the heartbeat contains a time field (lbolt64), 1130Sstevel@tonic-gate * a 64 bit secret, followed by the v6 (possible a v4mapped) address this 1140Sstevel@tonic-gate * heartbeat was sent to. No byte-ordering is done, since the heartbeat 1150Sstevel@tonic-gate * is not interpreted by the peer. 1160Sstevel@tonic-gate */ 1170Sstevel@tonic-gate void 1180Sstevel@tonic-gate sctp_send_heartbeat(sctp_t *sctp, sctp_faddr_t *fp) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate sctp_chunk_hdr_t *cp; 1210Sstevel@tonic-gate sctp_parm_hdr_t *hpp; 1220Sstevel@tonic-gate int64_t *t; 1230Sstevel@tonic-gate int64_t now; 1240Sstevel@tonic-gate in6_addr_t *a; 1250Sstevel@tonic-gate mblk_t *hbmp; 1260Sstevel@tonic-gate size_t hblen; 1273448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate dprint(3, ("sctp_send_heartbeat: to %x:%x:%x:%x from %x:%x:%x:%x\n", 1300Sstevel@tonic-gate SCTP_PRINTADDR(fp->faddr), SCTP_PRINTADDR(fp->saddr))); 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate hblen = sizeof (*cp) + 13311042SErik.Nordmark@Sun.COM sizeof (*hpp) + 13411042SErik.Nordmark@Sun.COM sizeof (*t) + 13511042SErik.Nordmark@Sun.COM sizeof (fp->hb_secret) + 13611042SErik.Nordmark@Sun.COM sizeof (fp->faddr); 1370Sstevel@tonic-gate hbmp = sctp_make_mp(sctp, fp, hblen); 1381735Skcpoon if (hbmp == NULL) { 1393448Sdh155122 SCTP_KSTAT(sctps, sctp_send_hb_failed); 1400Sstevel@tonic-gate return; 1411735Skcpoon } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate cp = (sctp_chunk_hdr_t *)hbmp->b_wptr; 1440Sstevel@tonic-gate cp->sch_id = CHUNK_HEARTBEAT; 1450Sstevel@tonic-gate cp->sch_flags = 0; 1460Sstevel@tonic-gate cp->sch_len = hblen; 1470Sstevel@tonic-gate cp->sch_len = htons(cp->sch_len); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate hpp = (sctp_parm_hdr_t *)(cp + 1); 1500Sstevel@tonic-gate hpp->sph_type = htons(PARM_HBINFO); 1510Sstevel@tonic-gate hpp->sph_len = hblen - sizeof (*cp); 1520Sstevel@tonic-gate hpp->sph_len = htons(hpp->sph_len); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * Timestamp 1560Sstevel@tonic-gate * 1570Sstevel@tonic-gate * Copy the current time to the heartbeat and we can use it to 1580Sstevel@tonic-gate * calculate the RTT when we get it back in the heartbeat ACK. 1590Sstevel@tonic-gate */ 16011066Srafael.vanoni@sun.com now = ddi_get_lbolt64(); 1610Sstevel@tonic-gate t = (int64_t *)(hpp + 1); 1620Sstevel@tonic-gate bcopy(&now, t, sizeof (now)); 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate /* 1650Sstevel@tonic-gate * Secret 1660Sstevel@tonic-gate * 1670Sstevel@tonic-gate * The per peer address secret is used to make sure that the heartbeat 1680Sstevel@tonic-gate * ack is really in response to our heartbeat. This prevents blind 1690Sstevel@tonic-gate * spoofing of heartbeat ack to fake the validity of an address. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate t++; 1720Sstevel@tonic-gate bcopy(&fp->hb_secret, t, sizeof (uint64_t)); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* 1750Sstevel@tonic-gate * Peer address 1760Sstevel@tonic-gate * 1770Sstevel@tonic-gate * The peer address is used to associate the heartbeat ack with 1780Sstevel@tonic-gate * the correct peer address. The reason is that the peer is 1790Sstevel@tonic-gate * multihomed so that it may not use the same address as source 1800Sstevel@tonic-gate * in response to our heartbeat. 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate a = (in6_addr_t *)(t + 1); 1830Sstevel@tonic-gate bcopy(&fp->faddr, a, sizeof (*a)); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate hbmp->b_wptr += hblen; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* Update the faddr's info */ 1880Sstevel@tonic-gate fp->lastactive = now; 1890Sstevel@tonic-gate fp->hb_pending = B_TRUE; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_obchunks); 192*12869SKacheong.Poon@Sun.COM SCTPS_BUMP_MIB(sctps, sctpTimHeartBeatProbe); 1930Sstevel@tonic-gate 19411042SErik.Nordmark@Sun.COM sctp_set_iplen(sctp, hbmp, fp->ixa); 19511042SErik.Nordmark@Sun.COM (void) conn_ip_output(hbmp, fp->ixa); 19611042SErik.Nordmark@Sun.COM BUMP_LOCAL(sctp->sctp_opkts); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Call right after any address change to validate peer addresses. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate void 2030Sstevel@tonic-gate sctp_validate_peer(sctp_t *sctp) 2040Sstevel@tonic-gate { 2050Sstevel@tonic-gate sctp_faddr_t *fp; 2060Sstevel@tonic-gate int cnt; 2070Sstevel@tonic-gate int64_t now; 2080Sstevel@tonic-gate int64_t earliest_expiry; 2093448Sdh155122 sctp_stack_t *sctps = sctp->sctp_sctps; 2100Sstevel@tonic-gate 21111066Srafael.vanoni@sun.com now = ddi_get_lbolt64(); 2120Sstevel@tonic-gate earliest_expiry = 0; 2133448Sdh155122 cnt = sctps->sctps_maxburst; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate /* 2160Sstevel@tonic-gate * Loop thru the list looking for unconfirmed addresses and 2170Sstevel@tonic-gate * send a heartbeat. But we should only send at most sctp_maxburst 2180Sstevel@tonic-gate * heartbeats. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) { 2210Sstevel@tonic-gate /* No need to validate unreachable address. */ 2220Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_UNREACH) 2230Sstevel@tonic-gate continue; 2240Sstevel@tonic-gate if (fp->state == SCTP_FADDRS_UNCONFIRMED) { 2250Sstevel@tonic-gate if (cnt-- > 0) { 2260Sstevel@tonic-gate fp->hb_expiry = now + fp->rto; 2270Sstevel@tonic-gate sctp_send_heartbeat(sctp, fp); 2280Sstevel@tonic-gate } else { 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * If we cannot send now, be more aggressive 2310Sstevel@tonic-gate * and try again about half of RTO. Note that 2320Sstevel@tonic-gate * all the unsent probes are set to expire at 2330Sstevel@tonic-gate * the same time. 2340Sstevel@tonic-gate */ 2350Sstevel@tonic-gate fp->hb_expiry = now + 2360Sstevel@tonic-gate (sctp->sctp_rto_initial >> 1); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate /* Find the earliest heartbeat expiry time for ALL fps. */ 2400Sstevel@tonic-gate if (fp->hb_interval != 0 && (earliest_expiry == 0 || 2410Sstevel@tonic-gate fp->hb_expiry < earliest_expiry)) { 2420Sstevel@tonic-gate earliest_expiry = fp->hb_expiry; 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate /* We use heartbeat timer for autoclose. */ 2460Sstevel@tonic-gate if (sctp->sctp_autoclose != 0) { 2470Sstevel@tonic-gate int64_t expire; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate expire = sctp->sctp_active + sctp->sctp_autoclose; 2500Sstevel@tonic-gate if (earliest_expiry == 0 || expire < earliest_expiry) 2510Sstevel@tonic-gate earliest_expiry = expire; 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * Set the timer to fire for the earliest heartbeat unless 2560Sstevel@tonic-gate * heartbeat is disabled for all addresses. 2570Sstevel@tonic-gate */ 2580Sstevel@tonic-gate if (earliest_expiry != 0) { 2590Sstevel@tonic-gate earliest_expiry -= now; 2600Sstevel@tonic-gate if (earliest_expiry < 0) 2610Sstevel@tonic-gate earliest_expiry = 1; 2620Sstevel@tonic-gate sctp_timer(sctp, sctp->sctp_heartbeat_mp, earliest_expiry); 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * Process an incoming heartbeat ack. When sending a heartbeat, we 2680Sstevel@tonic-gate * put the timestamp, a secret and the peer address the heartbeat is 2690Sstevel@tonic-gate * sent in the data part of the heartbeat. We will extract this info 2700Sstevel@tonic-gate * and verify that this heartbeat ack is valid. 2710Sstevel@tonic-gate */ 2720Sstevel@tonic-gate void 2730Sstevel@tonic-gate sctp_process_heartbeat(sctp_t *sctp, sctp_chunk_hdr_t *cp) 2740Sstevel@tonic-gate { 2750Sstevel@tonic-gate int64_t *sentp, sent; 2760Sstevel@tonic-gate uint64_t secret; 2770Sstevel@tonic-gate in6_addr_t addr; 2780Sstevel@tonic-gate sctp_faddr_t *fp; 2790Sstevel@tonic-gate sctp_parm_hdr_t *hpp; 2800Sstevel@tonic-gate int64_t now; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate BUMP_LOCAL(sctp->sctp_ibchunks); 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* Sanity checks */ 2850Sstevel@tonic-gate ASSERT(OK_32PTR(cp)); 2860Sstevel@tonic-gate if (ntohs(cp->sch_len) < (sizeof (*cp) + sizeof (*hpp) + 2870Sstevel@tonic-gate sizeof (sent) + sizeof (secret) + sizeof (addr))) { 2880Sstevel@tonic-gate /* drop it */ 2890Sstevel@tonic-gate dprint(2, ("sctp_process_heartbeat: malformed ack %p\n", 2901676Sjpk (void *)sctp)); 2910Sstevel@tonic-gate return; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate hpp = (sctp_parm_hdr_t *)(cp + 1); 2950Sstevel@tonic-gate if (ntohs(hpp->sph_type) != PARM_HBINFO || 2960Sstevel@tonic-gate ntohs(hpp->sph_len) != (ntohs(cp->sch_len) - sizeof (*cp))) { 2970Sstevel@tonic-gate dprint(2, 2980Sstevel@tonic-gate ("sctp_process_heartbeat: malformed param in ack %p\n", 2991676Sjpk (void *)sctp)); 3000Sstevel@tonic-gate return; 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * Pull out the time sent from the ack. 3050Sstevel@tonic-gate * SCTP is 32-bit aligned, so copy 64 bit quantity. Since we 3060Sstevel@tonic-gate * put it in, it should be in our byte order. 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate sentp = (int64_t *)(hpp + 1); 3090Sstevel@tonic-gate bcopy(sentp, &sent, sizeof (sent)); 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate /* Grab the secret to make sure that this heartbeat is valid */ 3120Sstevel@tonic-gate bcopy(++sentp, &secret, sizeof (secret)); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* Next, verify the address to make sure that it is the right one. */ 3150Sstevel@tonic-gate bcopy(++sentp, &addr, sizeof (addr)); 3160Sstevel@tonic-gate fp = sctp_lookup_faddr(sctp, &addr); 3170Sstevel@tonic-gate if (fp == NULL) { 3180Sstevel@tonic-gate dprint(2, ("sctp_process_heartbeat: invalid faddr (sctp=%p)\n", 3191676Sjpk (void *)sctp)); 3200Sstevel@tonic-gate return; 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate if (secret != fp->hb_secret) { 3230Sstevel@tonic-gate dprint(2, 3240Sstevel@tonic-gate ("sctp_process_heartbeat: invalid secret in ack %p\n", 3251676Sjpk (void *)sctp)); 3260Sstevel@tonic-gate return; 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate /* This address is now confirmed and alive. */ 3300Sstevel@tonic-gate sctp_faddr_alive(sctp, fp); 33111066Srafael.vanoni@sun.com now = ddi_get_lbolt64(); 3320Sstevel@tonic-gate sctp_update_rtt(sctp, fp, now - sent); 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate /* 3350Sstevel@tonic-gate * Note that the heartbeat timer should still be running, we don't 3360Sstevel@tonic-gate * reset it to avoid going through the whole list of peer addresses 3370Sstevel@tonic-gate * for each heartbeat ack as we probably are in interrupt context. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate fp->hb_expiry = now + SET_HB_INTVL(fp); 3400Sstevel@tonic-gate } 341