xref: /freebsd/usr.sbin/rtadvd/timer.c (revision be5a2c70b1c8c243a9c22e16d60335b56c9983ee)
1fa19f9beSHajimu UMEMOTO /*	$KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $	*/
2b26e03e9SKris Kennaway 
3*8a16b7a1SPedro F. Giffuni /*-
4*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni  *
69a4365d0SYoshinobu Inoue  * Copyright (C) 1998 WIDE Project.
737241896SHiroki Sato  * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
89a4365d0SYoshinobu Inoue  * All rights reserved.
99a4365d0SYoshinobu Inoue  *
109a4365d0SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
119a4365d0SYoshinobu Inoue  * modification, are permitted provided that the following conditions
129a4365d0SYoshinobu Inoue  * are met:
139a4365d0SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
149a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
159a4365d0SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
169a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
179a4365d0SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
189a4365d0SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
199a4365d0SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
209a4365d0SYoshinobu Inoue  *    without specific prior written permission.
219a4365d0SYoshinobu Inoue  *
229a4365d0SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
239a4365d0SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249a4365d0SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259a4365d0SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
269a4365d0SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279a4365d0SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289a4365d0SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299a4365d0SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309a4365d0SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319a4365d0SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329a4365d0SYoshinobu Inoue  * SUCH DAMAGE.
339a4365d0SYoshinobu Inoue  */
349a4365d0SYoshinobu Inoue 
35db82af41SHiroki Sato #include <sys/queue.h>
3637241896SHiroki Sato #include <sys/socket.h>
3737241896SHiroki Sato 
3837241896SHiroki Sato #include <net/if.h>
3937241896SHiroki Sato #include <net/if_dl.h>
4037241896SHiroki Sato #include <netinet/in.h>
419a4365d0SYoshinobu Inoue 
429a4365d0SYoshinobu Inoue #include <unistd.h>
439a4365d0SYoshinobu Inoue #include <syslog.h>
449a4365d0SYoshinobu Inoue #include <stdlib.h>
459a4365d0SYoshinobu Inoue #include <string.h>
4672286081SSUZUKI Shinsuke #include <search.h>
477d26db17SHiroki Sato #include <time.h>
4837241896SHiroki Sato #include <netdb.h>
499a4365d0SYoshinobu Inoue 
5037241896SHiroki Sato #include "rtadvd.h"
5137241896SHiroki Sato #include "timer_subr.h"
5237241896SHiroki Sato #include "timer.h"
539a4365d0SYoshinobu Inoue 
54db82af41SHiroki Sato struct rtadvd_timer_head_t ra_timer =
55db82af41SHiroki Sato     TAILQ_HEAD_INITIALIZER(ra_timer);
567d26db17SHiroki Sato static struct timespec tm_limit;
577d26db17SHiroki Sato static struct timespec tm_max;
589a4365d0SYoshinobu Inoue 
599a4365d0SYoshinobu Inoue void
rtadvd_timer_init(void)60db82af41SHiroki Sato rtadvd_timer_init(void)
619a4365d0SYoshinobu Inoue {
627d26db17SHiroki Sato 	/* Generate maximum time in timespec. */
6337743840SHiroki Sato 	tm_limit.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
6437743840SHiroki Sato 	tm_limit.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
65db82af41SHiroki Sato 	tm_max = tm_limit;
66db82af41SHiroki Sato 	TAILQ_INIT(&ra_timer);
679a4365d0SYoshinobu Inoue }
689a4365d0SYoshinobu Inoue 
6937241896SHiroki Sato void
rtadvd_update_timeout_handler(void)7037241896SHiroki Sato rtadvd_update_timeout_handler(void)
7137241896SHiroki Sato {
7237241896SHiroki Sato 	struct ifinfo *ifi;
7337241896SHiroki Sato 
7437241896SHiroki Sato 	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
7537241896SHiroki Sato 		switch (ifi->ifi_state) {
7637241896SHiroki Sato 		case IFI_STATE_CONFIGURED:
7737241896SHiroki Sato 		case IFI_STATE_TRANSITIVE:
7837241896SHiroki Sato 			if (ifi->ifi_ra_timer != NULL)
7937241896SHiroki Sato 				continue;
8037241896SHiroki Sato 
8137241896SHiroki Sato 			syslog(LOG_DEBUG, "<%s> add timer for %s (idx=%d)",
8237241896SHiroki Sato 			    __func__, ifi->ifi_ifname, ifi->ifi_ifindex);
8337241896SHiroki Sato 			ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
8437241896SHiroki Sato 			    ra_timer_update, ifi, ifi);
8537241896SHiroki Sato 			ra_timer_update((void *)ifi,
8637241896SHiroki Sato 			    &ifi->ifi_ra_timer->rat_tm);
8737241896SHiroki Sato 			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
8837241896SHiroki Sato 			    ifi->ifi_ra_timer);
8937241896SHiroki Sato 			break;
9037241896SHiroki Sato 		case IFI_STATE_UNCONFIGURED:
9137241896SHiroki Sato 			if (ifi->ifi_ra_timer == NULL)
9237241896SHiroki Sato 				continue;
9337241896SHiroki Sato 
9437241896SHiroki Sato 			syslog(LOG_DEBUG,
9537241896SHiroki Sato 			    "<%s> remove timer for %s (idx=%d)", __func__,
9637241896SHiroki Sato 			    ifi->ifi_ifname, ifi->ifi_ifindex);
9737241896SHiroki Sato 			rtadvd_remove_timer(ifi->ifi_ra_timer);
9837241896SHiroki Sato 			ifi->ifi_ra_timer = NULL;
9937241896SHiroki Sato 			break;
10037241896SHiroki Sato 		}
10137241896SHiroki Sato 	}
10237241896SHiroki Sato }
10337241896SHiroki Sato 
1049a4365d0SYoshinobu Inoue struct rtadvd_timer *
rtadvd_add_timer(struct rtadvd_timer * (* timeout)(void *),void (* update)(void *,struct timespec *),void * timeodata,void * updatedata)105784bddbcSKevin Lo rtadvd_add_timer(struct rtadvd_timer *(*timeout)(void *),
1067d26db17SHiroki Sato     void (*update)(void *, struct timespec *),
1079a4365d0SYoshinobu Inoue     void *timeodata, void *updatedata)
1089a4365d0SYoshinobu Inoue {
109db82af41SHiroki Sato 	struct rtadvd_timer *rat;
1109a4365d0SYoshinobu Inoue 
1119a4365d0SYoshinobu Inoue 	if (timeout == NULL) {
1129a4365d0SYoshinobu Inoue 		syslog(LOG_ERR,
113fa19f9beSHajimu UMEMOTO 		    "<%s> timeout function unspecified", __func__);
1149a4365d0SYoshinobu Inoue 		exit(1);
1159a4365d0SYoshinobu Inoue 	}
116db82af41SHiroki Sato 
117db82af41SHiroki Sato 	rat = malloc(sizeof(*rat));
118db82af41SHiroki Sato 	if (rat == NULL) {
119db82af41SHiroki Sato 		syslog(LOG_ERR,
120db82af41SHiroki Sato 		    "<%s> can't allocate memory", __func__);
121db82af41SHiroki Sato 		exit(1);
122db82af41SHiroki Sato 	}
123db82af41SHiroki Sato 	memset(rat, 0, sizeof(*rat));
124db82af41SHiroki Sato 
125db82af41SHiroki Sato 	rat->rat_expire = timeout;
126db82af41SHiroki Sato 	rat->rat_update = update;
127db82af41SHiroki Sato 	rat->rat_expire_data = timeodata;
128db82af41SHiroki Sato 	rat->rat_update_data = updatedata;
129db82af41SHiroki Sato 	rat->rat_tm = tm_max;
1309a4365d0SYoshinobu Inoue 
1319a4365d0SYoshinobu Inoue 	/* link into chain */
132db82af41SHiroki Sato 	TAILQ_INSERT_TAIL(&ra_timer, rat, rat_next);
1339a4365d0SYoshinobu Inoue 
134db82af41SHiroki Sato 	return (rat);
1359a4365d0SYoshinobu Inoue }
1369a4365d0SYoshinobu Inoue 
1379a4365d0SYoshinobu Inoue void
rtadvd_remove_timer(struct rtadvd_timer * rat)138db82af41SHiroki Sato rtadvd_remove_timer(struct rtadvd_timer *rat)
139b26e03e9SKris Kennaway {
140db82af41SHiroki Sato 
141db82af41SHiroki Sato 	if (rat == NULL)
142db82af41SHiroki Sato 		return;
143db82af41SHiroki Sato 
144db82af41SHiroki Sato 	TAILQ_REMOVE(&ra_timer, rat, rat_next);
145db82af41SHiroki Sato 	free(rat);
146b26e03e9SKris Kennaway }
147b26e03e9SKris Kennaway 
1489a4365d0SYoshinobu Inoue /*
1490a5f3ef4SHajimu UMEMOTO  * Check expiration for each timer. If a timer expires,
1509a4365d0SYoshinobu Inoue  * call the expire function for the timer and update the timer.
1519a4365d0SYoshinobu Inoue  * Return the next interval for select() call.
1529a4365d0SYoshinobu Inoue  */
1537d26db17SHiroki Sato struct timespec *
rtadvd_check_timer(void)154db82af41SHiroki Sato rtadvd_check_timer(void)
1559a4365d0SYoshinobu Inoue {
1567d26db17SHiroki Sato 	static struct timespec returnval;
1577d26db17SHiroki Sato 	struct timespec now;
158db82af41SHiroki Sato 	struct rtadvd_timer *rat;
1599a4365d0SYoshinobu Inoue 
1607d26db17SHiroki Sato 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
161db82af41SHiroki Sato 	tm_max = tm_limit;
162db82af41SHiroki Sato 	TAILQ_FOREACH(rat, &ra_timer, rat_next) {
1637d26db17SHiroki Sato 		if (TS_CMP(&rat->rat_tm, &now, <=)) {
164db82af41SHiroki Sato 			if (((*rat->rat_expire)(rat->rat_expire_data) == NULL))
1650a5f3ef4SHajimu UMEMOTO 				continue; /* the timer was removed */
166db82af41SHiroki Sato 			if (rat->rat_update)
167db82af41SHiroki Sato 				(*rat->rat_update)(rat->rat_update_data, &rat->rat_tm);
1687d26db17SHiroki Sato 			TS_ADD(&rat->rat_tm, &now, &rat->rat_tm);
1699a4365d0SYoshinobu Inoue 		}
1707d26db17SHiroki Sato 		if (TS_CMP(&rat->rat_tm, &tm_max, <))
171db82af41SHiroki Sato 			tm_max = rat->rat_tm;
1729a4365d0SYoshinobu Inoue 	}
1737d26db17SHiroki Sato 	if (TS_CMP(&tm_max, &tm_limit, ==)) {
174b26e03e9SKris Kennaway 		/* no need to timeout */
175b26e03e9SKris Kennaway 		return (NULL);
1767d26db17SHiroki Sato 	} else if (TS_CMP(&tm_max, &now, <)) {
1779a4365d0SYoshinobu Inoue 		/* this may occur when the interval is too small */
1787d26db17SHiroki Sato 		returnval.tv_sec = returnval.tv_nsec = 0;
179fa19f9beSHajimu UMEMOTO 	} else
1807d26db17SHiroki Sato 		TS_SUB(&tm_max, &now, &returnval);
1819a4365d0SYoshinobu Inoue 	return (&returnval);
1829a4365d0SYoshinobu Inoue }
1839a4365d0SYoshinobu Inoue 
18437241896SHiroki Sato void
rtadvd_set_timer(struct timespec * tm,struct rtadvd_timer * rat)1857d26db17SHiroki Sato rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *rat)
1869a4365d0SYoshinobu Inoue {
1877d26db17SHiroki Sato 	struct timespec now;
1889a4365d0SYoshinobu Inoue 
18937241896SHiroki Sato 	/* reset the timer */
1907d26db17SHiroki Sato 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1917d26db17SHiroki Sato 	TS_ADD(&now, tm, &rat->rat_tm);
1929a4365d0SYoshinobu Inoue 
19337241896SHiroki Sato 	/* update the next expiration time */
1947d26db17SHiroki Sato 	if (TS_CMP(&rat->rat_tm, &tm_max, <))
19537241896SHiroki Sato 		tm_max = rat->rat_tm;
1969a4365d0SYoshinobu Inoue }
197