xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_heartbeat.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/systm.h>
31*0Sstevel@tonic-gate #include <sys/stream.h>
32*0Sstevel@tonic-gate #include <sys/cmn_err.h>
33*0Sstevel@tonic-gate #include <sys/strsubr.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <netinet/in.h>
36*0Sstevel@tonic-gate #include <netinet/ip6.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <inet/common.h>
39*0Sstevel@tonic-gate #include <inet/ip.h>
40*0Sstevel@tonic-gate #include <inet/mib2.h>
41*0Sstevel@tonic-gate #include "sctp_impl.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate void
44*0Sstevel@tonic-gate sctp_return_heartbeat(sctp_t *sctp, sctp_chunk_hdr_t *hbcp, mblk_t *mp)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	mblk_t *smp;
47*0Sstevel@tonic-gate 	sctp_chunk_hdr_t *cp;
48*0Sstevel@tonic-gate 	ipha_t *iniph;
49*0Sstevel@tonic-gate 	ip6_t *inip6h;
50*0Sstevel@tonic-gate 	int isv4;
51*0Sstevel@tonic-gate 	in6_addr_t addr;
52*0Sstevel@tonic-gate 	sctp_faddr_t *fp;
53*0Sstevel@tonic-gate 	uint16_t len;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	/* Update the faddr for the src addr */
58*0Sstevel@tonic-gate 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
59*0Sstevel@tonic-gate 	if (isv4) {
60*0Sstevel@tonic-gate 		iniph = (ipha_t *)mp->b_rptr;
61*0Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(iniph->ipha_src, &addr);
62*0Sstevel@tonic-gate 	} else {
63*0Sstevel@tonic-gate 		inip6h = (ip6_t *)mp->b_rptr;
64*0Sstevel@tonic-gate 		addr = inip6h->ip6_src;
65*0Sstevel@tonic-gate 	}
66*0Sstevel@tonic-gate 	fp = sctp_lookup_faddr(sctp, &addr);
67*0Sstevel@tonic-gate 	ASSERT(fp != NULL);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	dprint(3, ("sctp_return_heartbeat: %p got hb from %x:%x:%x:%x\n",
70*0Sstevel@tonic-gate 	    sctp, SCTP_PRINTADDR(addr)));
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	/*
73*0Sstevel@tonic-gate 	 * XXX It's really tempting to reuse the heartbeat mblk. But
74*0Sstevel@tonic-gate 	 * this complicates processing in sctp_dispatch (i.e. it will
75*0Sstevel@tonic-gate 	 * screw up sctp_next_chunk since we will set the chunk
76*0Sstevel@tonic-gate 	 * header's length into network byte-order), and if we ever
77*0Sstevel@tonic-gate 	 * encounter a heartbeat bundled with other chunks...
78*0Sstevel@tonic-gate 	 * So we take the slower-but-safe route.
79*0Sstevel@tonic-gate 	 */
80*0Sstevel@tonic-gate 	len = ntohs(hbcp->sch_len);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	/* Create an IP header, returning to the src addr from the heartbt */
83*0Sstevel@tonic-gate 	smp = sctp_make_mp(sctp, fp, len);
84*0Sstevel@tonic-gate 	if (smp == NULL) {
85*0Sstevel@tonic-gate 		return;
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	cp = (sctp_chunk_hdr_t *)smp->b_wptr;
89*0Sstevel@tonic-gate 	cp->sch_id = CHUNK_HEARTBEAT_ACK;
90*0Sstevel@tonic-gate 	cp->sch_flags = 0;
91*0Sstevel@tonic-gate 	cp->sch_len = htons(len);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/* Copy the information field from the heartbeat */
94*0Sstevel@tonic-gate 	bcopy((void *)(hbcp + 1), (void *)(cp + 1), len - sizeof (*cp));
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	smp->b_wptr += len;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	sctp_set_iplen(sctp, smp);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
101*0Sstevel@tonic-gate 	sctp_add_sendq(sctp, smp);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate  * The data section of the heartbeat contains a time field (lbolt64),
106*0Sstevel@tonic-gate  * a 64 bit secret, followed by the v6 (possible a v4mapped) address this
107*0Sstevel@tonic-gate  * heartbeat was sent to.  No byte-ordering is done, since the heartbeat
108*0Sstevel@tonic-gate  * is not interpreted by the peer.
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate void
111*0Sstevel@tonic-gate sctp_send_heartbeat(sctp_t *sctp, sctp_faddr_t *fp)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	sctp_chunk_hdr_t *cp;
114*0Sstevel@tonic-gate 	sctp_parm_hdr_t *hpp;
115*0Sstevel@tonic-gate 	int64_t *t;
116*0Sstevel@tonic-gate 	int64_t now;
117*0Sstevel@tonic-gate 	in6_addr_t *a;
118*0Sstevel@tonic-gate 	mblk_t *hbmp;
119*0Sstevel@tonic-gate 	size_t hblen;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	dprint(3, ("sctp_send_heartbeat: to %x:%x:%x:%x from %x:%x:%x:%x\n",
122*0Sstevel@tonic-gate 	    SCTP_PRINTADDR(fp->faddr), SCTP_PRINTADDR(fp->saddr)));
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	hblen = sizeof (*cp) +
125*0Sstevel@tonic-gate 		sizeof (*hpp) +
126*0Sstevel@tonic-gate 		sizeof (*t) +
127*0Sstevel@tonic-gate 		sizeof (fp->hb_secret) +
128*0Sstevel@tonic-gate 		sizeof (fp->faddr);
129*0Sstevel@tonic-gate 	hbmp = sctp_make_mp(sctp, fp, hblen);
130*0Sstevel@tonic-gate 	if (hbmp == NULL)
131*0Sstevel@tonic-gate 		return;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	cp = (sctp_chunk_hdr_t *)hbmp->b_wptr;
134*0Sstevel@tonic-gate 	cp->sch_id = CHUNK_HEARTBEAT;
135*0Sstevel@tonic-gate 	cp->sch_flags = 0;
136*0Sstevel@tonic-gate 	cp->sch_len = hblen;
137*0Sstevel@tonic-gate 	cp->sch_len = htons(cp->sch_len);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	hpp = (sctp_parm_hdr_t *)(cp + 1);
140*0Sstevel@tonic-gate 	hpp->sph_type = htons(PARM_HBINFO);
141*0Sstevel@tonic-gate 	hpp->sph_len = hblen - sizeof (*cp);
142*0Sstevel@tonic-gate 	hpp->sph_len = htons(hpp->sph_len);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/*
145*0Sstevel@tonic-gate 	 * Timestamp
146*0Sstevel@tonic-gate 	 *
147*0Sstevel@tonic-gate 	 * Copy the current time to the heartbeat and we can use it to
148*0Sstevel@tonic-gate 	 * calculate the RTT when we get it back in the heartbeat ACK.
149*0Sstevel@tonic-gate 	 */
150*0Sstevel@tonic-gate 	now = lbolt64;
151*0Sstevel@tonic-gate 	t = (int64_t *)(hpp + 1);
152*0Sstevel@tonic-gate 	bcopy(&now, t, sizeof (now));
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * Secret
156*0Sstevel@tonic-gate 	 *
157*0Sstevel@tonic-gate 	 * The per peer address secret is used to make sure that the heartbeat
158*0Sstevel@tonic-gate 	 * ack is really in response to our heartbeat.  This prevents blind
159*0Sstevel@tonic-gate 	 * spoofing of heartbeat ack to fake the validity of an address.
160*0Sstevel@tonic-gate 	 */
161*0Sstevel@tonic-gate 	t++;
162*0Sstevel@tonic-gate 	bcopy(&fp->hb_secret, t, sizeof (uint64_t));
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	/*
165*0Sstevel@tonic-gate 	 * Peer address
166*0Sstevel@tonic-gate 	 *
167*0Sstevel@tonic-gate 	 * The peer address is used to associate the heartbeat ack with
168*0Sstevel@tonic-gate 	 * the correct peer address.  The reason is that the peer is
169*0Sstevel@tonic-gate 	 * multihomed so that it may not use the same address as source
170*0Sstevel@tonic-gate 	 * in response to our heartbeat.
171*0Sstevel@tonic-gate 	 */
172*0Sstevel@tonic-gate 	a = (in6_addr_t *)(t + 1);
173*0Sstevel@tonic-gate 	bcopy(&fp->faddr, a, sizeof (*a));
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	hbmp->b_wptr += hblen;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	sctp_set_iplen(sctp, hbmp);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	/* Update the faddr's info */
180*0Sstevel@tonic-gate 	fp->lastactive = now;
181*0Sstevel@tonic-gate 	fp->hb_pending = B_TRUE;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
184*0Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpTimHeartBeatProbe);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	sctp_add_sendq(sctp, hbmp);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate  * Call right after any address change to validate peer addresses.
191*0Sstevel@tonic-gate  */
192*0Sstevel@tonic-gate void
193*0Sstevel@tonic-gate sctp_validate_peer(sctp_t *sctp)
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate 	sctp_faddr_t	*fp;
196*0Sstevel@tonic-gate 	int		cnt;
197*0Sstevel@tonic-gate 	int64_t		now;
198*0Sstevel@tonic-gate 	int64_t		earliest_expiry;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	now = lbolt64;
201*0Sstevel@tonic-gate 	earliest_expiry = 0;
202*0Sstevel@tonic-gate 	cnt = sctp_maxburst;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/*
205*0Sstevel@tonic-gate 	 * Loop thru the list looking for unconfirmed addresses and
206*0Sstevel@tonic-gate 	 * send a heartbeat.  But we should only send at most sctp_maxburst
207*0Sstevel@tonic-gate 	 * heartbeats.
208*0Sstevel@tonic-gate 	 */
209*0Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
210*0Sstevel@tonic-gate 		/* No need to validate unreachable address. */
211*0Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_UNREACH)
212*0Sstevel@tonic-gate 			continue;
213*0Sstevel@tonic-gate 		if (fp->state == SCTP_FADDRS_UNCONFIRMED) {
214*0Sstevel@tonic-gate 			if (cnt-- > 0) {
215*0Sstevel@tonic-gate 				fp->hb_expiry = now + fp->rto;
216*0Sstevel@tonic-gate 				sctp_send_heartbeat(sctp, fp);
217*0Sstevel@tonic-gate 			} else {
218*0Sstevel@tonic-gate 				/*
219*0Sstevel@tonic-gate 				 * If we cannot send now, be more aggressive
220*0Sstevel@tonic-gate 				 * and try again about half of RTO.  Note that
221*0Sstevel@tonic-gate 				 * all the unsent probes are set to expire at
222*0Sstevel@tonic-gate 				 * the same time.
223*0Sstevel@tonic-gate 				 */
224*0Sstevel@tonic-gate 				fp->hb_expiry = now +
225*0Sstevel@tonic-gate 				    (sctp->sctp_rto_initial >> 1);
226*0Sstevel@tonic-gate 			}
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 		/* Find the earliest heartbeat expiry time for ALL fps. */
229*0Sstevel@tonic-gate 		if (fp->hb_interval != 0 && (earliest_expiry == 0 ||
230*0Sstevel@tonic-gate 		    fp->hb_expiry < earliest_expiry)) {
231*0Sstevel@tonic-gate 			earliest_expiry = fp->hb_expiry;
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	/* We use heartbeat timer for autoclose. */
235*0Sstevel@tonic-gate 	if (sctp->sctp_autoclose != 0) {
236*0Sstevel@tonic-gate 		int64_t expire;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 		expire = sctp->sctp_active + sctp->sctp_autoclose;
239*0Sstevel@tonic-gate 		if (earliest_expiry == 0 || expire < earliest_expiry)
240*0Sstevel@tonic-gate 			earliest_expiry = expire;
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/*
244*0Sstevel@tonic-gate 	 * Set the timer to fire for the earliest heartbeat unless
245*0Sstevel@tonic-gate 	 * heartbeat is disabled for all addresses.
246*0Sstevel@tonic-gate 	 */
247*0Sstevel@tonic-gate 	if (earliest_expiry != 0) {
248*0Sstevel@tonic-gate 		earliest_expiry -= now;
249*0Sstevel@tonic-gate 		if (earliest_expiry < 0)
250*0Sstevel@tonic-gate 			earliest_expiry = 1;
251*0Sstevel@tonic-gate 		sctp_timer(sctp, sctp->sctp_heartbeat_mp, earliest_expiry);
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate /*
256*0Sstevel@tonic-gate  * Process an incoming heartbeat ack.  When sending a heartbeat, we
257*0Sstevel@tonic-gate  * put the timestamp, a secret and the peer address the heartbeat is
258*0Sstevel@tonic-gate  * sent in the data part of the heartbeat.  We will extract this info
259*0Sstevel@tonic-gate  * and verify that this heartbeat ack is valid.
260*0Sstevel@tonic-gate  */
261*0Sstevel@tonic-gate void
262*0Sstevel@tonic-gate sctp_process_heartbeat(sctp_t *sctp, sctp_chunk_hdr_t *cp)
263*0Sstevel@tonic-gate {
264*0Sstevel@tonic-gate 	int64_t *sentp, sent;
265*0Sstevel@tonic-gate 	uint64_t secret;
266*0Sstevel@tonic-gate 	in6_addr_t addr;
267*0Sstevel@tonic-gate 	sctp_faddr_t *fp;
268*0Sstevel@tonic-gate 	sctp_parm_hdr_t *hpp;
269*0Sstevel@tonic-gate 	int64_t now;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	/* Sanity checks */
274*0Sstevel@tonic-gate 	ASSERT(OK_32PTR(cp));
275*0Sstevel@tonic-gate 	if (ntohs(cp->sch_len) < (sizeof (*cp) + sizeof (*hpp) +
276*0Sstevel@tonic-gate 	    sizeof (sent) + sizeof (secret) + sizeof (addr))) {
277*0Sstevel@tonic-gate 		/* drop it */
278*0Sstevel@tonic-gate 		dprint(2, ("sctp_process_heartbeat: malformed ack %p\n",
279*0Sstevel@tonic-gate 		    sctp));
280*0Sstevel@tonic-gate 		return;
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	hpp = (sctp_parm_hdr_t *)(cp + 1);
284*0Sstevel@tonic-gate 	if (ntohs(hpp->sph_type) != PARM_HBINFO ||
285*0Sstevel@tonic-gate 	    ntohs(hpp->sph_len) != (ntohs(cp->sch_len) - sizeof (*cp))) {
286*0Sstevel@tonic-gate 		dprint(2,
287*0Sstevel@tonic-gate 		    ("sctp_process_heartbeat: malformed param in ack %p\n",
288*0Sstevel@tonic-gate 		    sctp));
289*0Sstevel@tonic-gate 		return;
290*0Sstevel@tonic-gate 	}
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	/*
293*0Sstevel@tonic-gate 	 * Pull out the time sent from the ack.
294*0Sstevel@tonic-gate 	 * SCTP is 32-bit aligned, so copy 64 bit quantity.  Since we
295*0Sstevel@tonic-gate 	 * put it in, it should be in our byte order.
296*0Sstevel@tonic-gate 	 */
297*0Sstevel@tonic-gate 	sentp = (int64_t *)(hpp + 1);
298*0Sstevel@tonic-gate 	bcopy(sentp, &sent, sizeof (sent));
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/* Grab the secret to make sure that this heartbeat is valid */
301*0Sstevel@tonic-gate 	bcopy(++sentp, &secret, sizeof (secret));
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	/* Next, verify the address to make sure that it is the right one. */
304*0Sstevel@tonic-gate 	bcopy(++sentp, &addr, sizeof (addr));
305*0Sstevel@tonic-gate 	fp = sctp_lookup_faddr(sctp, &addr);
306*0Sstevel@tonic-gate 	if (fp == NULL) {
307*0Sstevel@tonic-gate 		dprint(2, ("sctp_process_heartbeat: invalid faddr (sctp=%p)\n",
308*0Sstevel@tonic-gate 		    sctp));
309*0Sstevel@tonic-gate 		return;
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 	if (secret != fp->hb_secret) {
312*0Sstevel@tonic-gate 		dprint(2,
313*0Sstevel@tonic-gate 		    ("sctp_process_heartbeat: invalid secret in ack %p\n",
314*0Sstevel@tonic-gate 		    sctp));
315*0Sstevel@tonic-gate 		return;
316*0Sstevel@tonic-gate 	}
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	/* This address is now confirmed and alive. */
319*0Sstevel@tonic-gate 	sctp_faddr_alive(sctp, fp);
320*0Sstevel@tonic-gate 	now = lbolt64;
321*0Sstevel@tonic-gate 	sctp_update_rtt(sctp, fp, now - sent);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	/*
324*0Sstevel@tonic-gate 	 * Note that the heartbeat timer should still be running, we don't
325*0Sstevel@tonic-gate 	 * reset it to avoid going through the whole list of peer addresses
326*0Sstevel@tonic-gate 	 * for each heartbeat ack as we probably are in interrupt context.
327*0Sstevel@tonic-gate 	 */
328*0Sstevel@tonic-gate 	fp->hb_expiry = now + SET_HB_INTVL(fp);
329*0Sstevel@tonic-gate }
330