xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_heartbeat.c (revision 1676:37f4a3e2bd99)
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
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * 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  */
210Sstevel@tonic-gate /*
22*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/strsubr.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <netinet/ip6.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <inet/common.h>
380Sstevel@tonic-gate #include <inet/ip.h>
390Sstevel@tonic-gate #include <inet/mib2.h>
400Sstevel@tonic-gate #include "sctp_impl.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate void
430Sstevel@tonic-gate sctp_return_heartbeat(sctp_t *sctp, sctp_chunk_hdr_t *hbcp, mblk_t *mp)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	mblk_t *smp;
460Sstevel@tonic-gate 	sctp_chunk_hdr_t *cp;
470Sstevel@tonic-gate 	ipha_t *iniph;
480Sstevel@tonic-gate 	ip6_t *inip6h;
490Sstevel@tonic-gate 	int isv4;
500Sstevel@tonic-gate 	in6_addr_t addr;
510Sstevel@tonic-gate 	sctp_faddr_t *fp;
520Sstevel@tonic-gate 	uint16_t len;
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);
660Sstevel@tonic-gate 	ASSERT(fp != NULL);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	dprint(3, ("sctp_return_heartbeat: %p got hb from %x:%x:%x:%x\n",
69*1676Sjpk 	    (void *)sctp, SCTP_PRINTADDR(addr)));
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * XXX It's really tempting to reuse the heartbeat mblk. But
730Sstevel@tonic-gate 	 * this complicates processing in sctp_dispatch (i.e. it will
740Sstevel@tonic-gate 	 * screw up sctp_next_chunk since we will set the chunk
750Sstevel@tonic-gate 	 * header's length into network byte-order), and if we ever
760Sstevel@tonic-gate 	 * encounter a heartbeat bundled with other chunks...
770Sstevel@tonic-gate 	 * So we take the slower-but-safe route.
780Sstevel@tonic-gate 	 */
790Sstevel@tonic-gate 	len = ntohs(hbcp->sch_len);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	/* Create an IP header, returning to the src addr from the heartbt */
820Sstevel@tonic-gate 	smp = sctp_make_mp(sctp, fp, len);
830Sstevel@tonic-gate 	if (smp == NULL) {
840Sstevel@tonic-gate 		return;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	cp = (sctp_chunk_hdr_t *)smp->b_wptr;
880Sstevel@tonic-gate 	cp->sch_id = CHUNK_HEARTBEAT_ACK;
890Sstevel@tonic-gate 	cp->sch_flags = 0;
900Sstevel@tonic-gate 	cp->sch_len = htons(len);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/* Copy the information field from the heartbeat */
930Sstevel@tonic-gate 	bcopy((void *)(hbcp + 1), (void *)(cp + 1), len - sizeof (*cp));
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	smp->b_wptr += len;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	sctp_set_iplen(sctp, smp);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
1000Sstevel@tonic-gate 	sctp_add_sendq(sctp, smp);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * The data section of the heartbeat contains a time field (lbolt64),
1050Sstevel@tonic-gate  * a 64 bit secret, followed by the v6 (possible a v4mapped) address this
1060Sstevel@tonic-gate  * heartbeat was sent to.  No byte-ordering is done, since the heartbeat
1070Sstevel@tonic-gate  * is not interpreted by the peer.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate void
1100Sstevel@tonic-gate sctp_send_heartbeat(sctp_t *sctp, sctp_faddr_t *fp)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	sctp_chunk_hdr_t *cp;
1130Sstevel@tonic-gate 	sctp_parm_hdr_t *hpp;
1140Sstevel@tonic-gate 	int64_t *t;
1150Sstevel@tonic-gate 	int64_t now;
1160Sstevel@tonic-gate 	in6_addr_t *a;
1170Sstevel@tonic-gate 	mblk_t *hbmp;
1180Sstevel@tonic-gate 	size_t hblen;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	dprint(3, ("sctp_send_heartbeat: to %x:%x:%x:%x from %x:%x:%x:%x\n",
1210Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), SCTP_PRINTADDR(fp->saddr)));
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	hblen = sizeof (*cp) +
1240Sstevel@tonic-gate 		sizeof (*hpp) +
1250Sstevel@tonic-gate 		sizeof (*t) +
1260Sstevel@tonic-gate 		sizeof (fp->hb_secret) +
1270Sstevel@tonic-gate 		sizeof (fp->faddr);
1280Sstevel@tonic-gate 	hbmp = sctp_make_mp(sctp, fp, hblen);
1290Sstevel@tonic-gate 	if (hbmp == NULL)
1300Sstevel@tonic-gate 		return;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	cp = (sctp_chunk_hdr_t *)hbmp->b_wptr;
1330Sstevel@tonic-gate 	cp->sch_id = CHUNK_HEARTBEAT;
1340Sstevel@tonic-gate 	cp->sch_flags = 0;
1350Sstevel@tonic-gate 	cp->sch_len = hblen;
1360Sstevel@tonic-gate 	cp->sch_len = htons(cp->sch_len);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	hpp = (sctp_parm_hdr_t *)(cp + 1);
1390Sstevel@tonic-gate 	hpp->sph_type = htons(PARM_HBINFO);
1400Sstevel@tonic-gate 	hpp->sph_len = hblen - sizeof (*cp);
1410Sstevel@tonic-gate 	hpp->sph_len = htons(hpp->sph_len);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/*
1440Sstevel@tonic-gate 	 * Timestamp
1450Sstevel@tonic-gate 	 *
1460Sstevel@tonic-gate 	 * Copy the current time to the heartbeat and we can use it to
1470Sstevel@tonic-gate 	 * calculate the RTT when we get it back in the heartbeat ACK.
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	now = lbolt64;
1500Sstevel@tonic-gate 	t = (int64_t *)(hpp + 1);
1510Sstevel@tonic-gate 	bcopy(&now, t, sizeof (now));
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * Secret
1550Sstevel@tonic-gate 	 *
1560Sstevel@tonic-gate 	 * The per peer address secret is used to make sure that the heartbeat
1570Sstevel@tonic-gate 	 * ack is really in response to our heartbeat.  This prevents blind
1580Sstevel@tonic-gate 	 * spoofing of heartbeat ack to fake the validity of an address.
1590Sstevel@tonic-gate 	 */
1600Sstevel@tonic-gate 	t++;
1610Sstevel@tonic-gate 	bcopy(&fp->hb_secret, t, sizeof (uint64_t));
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/*
1640Sstevel@tonic-gate 	 * Peer address
1650Sstevel@tonic-gate 	 *
1660Sstevel@tonic-gate 	 * The peer address is used to associate the heartbeat ack with
1670Sstevel@tonic-gate 	 * the correct peer address.  The reason is that the peer is
1680Sstevel@tonic-gate 	 * multihomed so that it may not use the same address as source
1690Sstevel@tonic-gate 	 * in response to our heartbeat.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	a = (in6_addr_t *)(t + 1);
1720Sstevel@tonic-gate 	bcopy(&fp->faddr, a, sizeof (*a));
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	hbmp->b_wptr += hblen;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	sctp_set_iplen(sctp, hbmp);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/* Update the faddr's info */
1790Sstevel@tonic-gate 	fp->lastactive = now;
1800Sstevel@tonic-gate 	fp->hb_pending = B_TRUE;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
1830Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpTimHeartBeatProbe);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	sctp_add_sendq(sctp, hbmp);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Call right after any address change to validate peer addresses.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate void
1920Sstevel@tonic-gate sctp_validate_peer(sctp_t *sctp)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	sctp_faddr_t	*fp;
1950Sstevel@tonic-gate 	int		cnt;
1960Sstevel@tonic-gate 	int64_t		now;
1970Sstevel@tonic-gate 	int64_t		earliest_expiry;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	now = lbolt64;
2000Sstevel@tonic-gate 	earliest_expiry = 0;
2010Sstevel@tonic-gate 	cnt = sctp_maxburst;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/*
2040Sstevel@tonic-gate 	 * Loop thru the list looking for unconfirmed addresses and
2050Sstevel@tonic-gate 	 * send a heartbeat.  But we should only send at most sctp_maxburst
2060Sstevel@tonic-gate 	 * heartbeats.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
2090Sstevel@tonic-gate 		/* No need to validate unreachable address. */
2100Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_UNREACH)
2110Sstevel@tonic-gate 			continue;
2120Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_UNCONFIRMED) {
2130Sstevel@tonic-gate 			if (cnt-- > 0) {
2140Sstevel@tonic-gate 				fp->hb_expiry = now + fp->rto;
2150Sstevel@tonic-gate 				sctp_send_heartbeat(sctp, fp);
2160Sstevel@tonic-gate 			} else {
2170Sstevel@tonic-gate 				/*
2180Sstevel@tonic-gate 				 * If we cannot send now, be more aggressive
2190Sstevel@tonic-gate 				 * and try again about half of RTO.  Note that
2200Sstevel@tonic-gate 				 * all the unsent probes are set to expire at
2210Sstevel@tonic-gate 				 * the same time.
2220Sstevel@tonic-gate 				 */
2230Sstevel@tonic-gate 				fp->hb_expiry = now +
2240Sstevel@tonic-gate 				    (sctp->sctp_rto_initial >> 1);
2250Sstevel@tonic-gate 			}
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 		/* Find the earliest heartbeat expiry time for ALL fps. */
2280Sstevel@tonic-gate 		if (fp->hb_interval != 0 && (earliest_expiry == 0 ||
2290Sstevel@tonic-gate 		    fp->hb_expiry < earliest_expiry)) {
2300Sstevel@tonic-gate 			earliest_expiry = fp->hb_expiry;
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	/* We use heartbeat timer for autoclose. */
2340Sstevel@tonic-gate 	if (sctp->sctp_autoclose != 0) {
2350Sstevel@tonic-gate 		int64_t expire;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		expire = sctp->sctp_active + sctp->sctp_autoclose;
2380Sstevel@tonic-gate 		if (earliest_expiry == 0 || expire < earliest_expiry)
2390Sstevel@tonic-gate 			earliest_expiry = expire;
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Set the timer to fire for the earliest heartbeat unless
2440Sstevel@tonic-gate 	 * heartbeat is disabled for all addresses.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	if (earliest_expiry != 0) {
2470Sstevel@tonic-gate 		earliest_expiry -= now;
2480Sstevel@tonic-gate 		if (earliest_expiry < 0)
2490Sstevel@tonic-gate 			earliest_expiry = 1;
2500Sstevel@tonic-gate 		sctp_timer(sctp, sctp->sctp_heartbeat_mp, earliest_expiry);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * Process an incoming heartbeat ack.  When sending a heartbeat, we
2560Sstevel@tonic-gate  * put the timestamp, a secret and the peer address the heartbeat is
2570Sstevel@tonic-gate  * sent in the data part of the heartbeat.  We will extract this info
2580Sstevel@tonic-gate  * and verify that this heartbeat ack is valid.
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate void
2610Sstevel@tonic-gate sctp_process_heartbeat(sctp_t *sctp, sctp_chunk_hdr_t *cp)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	int64_t *sentp, sent;
2640Sstevel@tonic-gate 	uint64_t secret;
2650Sstevel@tonic-gate 	in6_addr_t addr;
2660Sstevel@tonic-gate 	sctp_faddr_t *fp;
2670Sstevel@tonic-gate 	sctp_parm_hdr_t *hpp;
2680Sstevel@tonic-gate 	int64_t now;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/* Sanity checks */
2730Sstevel@tonic-gate 	ASSERT(OK_32PTR(cp));
2740Sstevel@tonic-gate 	if (ntohs(cp->sch_len) < (sizeof (*cp) + sizeof (*hpp) +
2750Sstevel@tonic-gate 	    sizeof (sent) + sizeof (secret) + sizeof (addr))) {
2760Sstevel@tonic-gate 		/* drop it */
2770Sstevel@tonic-gate 		dprint(2, ("sctp_process_heartbeat: malformed ack %p\n",
278*1676Sjpk 		    (void *)sctp));
2790Sstevel@tonic-gate 		return;
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	hpp = (sctp_parm_hdr_t *)(cp + 1);
2830Sstevel@tonic-gate 	if (ntohs(hpp->sph_type) != PARM_HBINFO ||
2840Sstevel@tonic-gate 	    ntohs(hpp->sph_len) != (ntohs(cp->sch_len) - sizeof (*cp))) {
2850Sstevel@tonic-gate 		dprint(2,
2860Sstevel@tonic-gate 		    ("sctp_process_heartbeat: malformed param in ack %p\n",
287*1676Sjpk 		    (void *)sctp));
2880Sstevel@tonic-gate 		return;
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/*
2920Sstevel@tonic-gate 	 * Pull out the time sent from the ack.
2930Sstevel@tonic-gate 	 * SCTP is 32-bit aligned, so copy 64 bit quantity.  Since we
2940Sstevel@tonic-gate 	 * put it in, it should be in our byte order.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	sentp = (int64_t *)(hpp + 1);
2970Sstevel@tonic-gate 	bcopy(sentp, &sent, sizeof (sent));
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	/* Grab the secret to make sure that this heartbeat is valid */
3000Sstevel@tonic-gate 	bcopy(++sentp, &secret, sizeof (secret));
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/* Next, verify the address to make sure that it is the right one. */
3030Sstevel@tonic-gate 	bcopy(++sentp, &addr, sizeof (addr));
3040Sstevel@tonic-gate 	fp = sctp_lookup_faddr(sctp, &addr);
3050Sstevel@tonic-gate 	if (fp == NULL) {
3060Sstevel@tonic-gate 		dprint(2, ("sctp_process_heartbeat: invalid faddr (sctp=%p)\n",
307*1676Sjpk 		    (void *)sctp));
3080Sstevel@tonic-gate 		return;
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 	if (secret != fp->hb_secret) {
3110Sstevel@tonic-gate 		dprint(2,
3120Sstevel@tonic-gate 		    ("sctp_process_heartbeat: invalid secret in ack %p\n",
313*1676Sjpk 		    (void *)sctp));
3140Sstevel@tonic-gate 		return;
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/* This address is now confirmed and alive. */
3180Sstevel@tonic-gate 	sctp_faddr_alive(sctp, fp);
3190Sstevel@tonic-gate 	now = lbolt64;
3200Sstevel@tonic-gate 	sctp_update_rtt(sctp, fp, now - sent);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Note that the heartbeat timer should still be running, we don't
3240Sstevel@tonic-gate 	 * reset it to avoid going through the whole list of peer addresses
3250Sstevel@tonic-gate 	 * for each heartbeat ack as we probably are in interrupt context.
3260Sstevel@tonic-gate 	 */
3270Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
3280Sstevel@tonic-gate }
329