1/*	$FreeBSD$	*/
2/*	$KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $	*/
3
4/*-
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Copyright (C) 1998 WIDE Project.
8 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the project nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/queue.h>
37#include <sys/socket.h>
38
39#include <net/if.h>
40#include <net/if_dl.h>
41#include <netinet/in.h>
42
43#include <unistd.h>
44#include <syslog.h>
45#include <stdlib.h>
46#include <string.h>
47#include <search.h>
48#include <time.h>
49#include <netdb.h>
50
51#include "rtadvd.h"
52#include "timer_subr.h"
53#include "timer.h"
54
55struct rtadvd_timer_head_t ra_timer =
56    TAILQ_HEAD_INITIALIZER(ra_timer);
57static struct timespec tm_limit;
58static struct timespec tm_max;
59
60void
61rtadvd_timer_init(void)
62{
63	/* Generate maximum time in timespec. */
64	tm_limit.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
65	tm_limit.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
66	tm_max = tm_limit;
67	TAILQ_INIT(&ra_timer);
68}
69
70void
71rtadvd_update_timeout_handler(void)
72{
73	struct ifinfo *ifi;
74
75	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
76		switch (ifi->ifi_state) {
77		case IFI_STATE_CONFIGURED:
78		case IFI_STATE_TRANSITIVE:
79			if (ifi->ifi_ra_timer != NULL)
80				continue;
81
82			syslog(LOG_DEBUG, "<%s> add timer for %s (idx=%d)",
83			    __func__, ifi->ifi_ifname, ifi->ifi_ifindex);
84			ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
85			    ra_timer_update, ifi, ifi);
86			ra_timer_update((void *)ifi,
87			    &ifi->ifi_ra_timer->rat_tm);
88			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
89			    ifi->ifi_ra_timer);
90			break;
91		case IFI_STATE_UNCONFIGURED:
92			if (ifi->ifi_ra_timer == NULL)
93				continue;
94
95			syslog(LOG_DEBUG,
96			    "<%s> remove timer for %s (idx=%d)", __func__,
97			    ifi->ifi_ifname, ifi->ifi_ifindex);
98			rtadvd_remove_timer(ifi->ifi_ra_timer);
99			ifi->ifi_ra_timer = NULL;
100			break;
101		}
102	}
103
104	return;
105}
106
107struct rtadvd_timer *
108rtadvd_add_timer(struct rtadvd_timer *(*timeout)(void *),
109    void (*update)(void *, struct timespec *),
110    void *timeodata, void *updatedata)
111{
112	struct rtadvd_timer *rat;
113
114	if (timeout == NULL) {
115		syslog(LOG_ERR,
116		    "<%s> timeout function unspecified", __func__);
117		exit(1);
118	}
119
120	rat = malloc(sizeof(*rat));
121	if (rat == NULL) {
122		syslog(LOG_ERR,
123		    "<%s> can't allocate memory", __func__);
124		exit(1);
125	}
126	memset(rat, 0, sizeof(*rat));
127
128	rat->rat_expire = timeout;
129	rat->rat_update = update;
130	rat->rat_expire_data = timeodata;
131	rat->rat_update_data = updatedata;
132	rat->rat_tm = tm_max;
133
134	/* link into chain */
135	TAILQ_INSERT_TAIL(&ra_timer, rat, rat_next);
136
137	return (rat);
138}
139
140void
141rtadvd_remove_timer(struct rtadvd_timer *rat)
142{
143
144	if (rat == NULL)
145		return;
146
147	TAILQ_REMOVE(&ra_timer, rat, rat_next);
148	free(rat);
149}
150
151/*
152 * Check expiration for each timer. If a timer expires,
153 * call the expire function for the timer and update the timer.
154 * Return the next interval for select() call.
155 */
156struct timespec *
157rtadvd_check_timer(void)
158{
159	static struct timespec returnval;
160	struct timespec now;
161	struct rtadvd_timer *rat;
162
163	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
164	tm_max = tm_limit;
165	TAILQ_FOREACH(rat, &ra_timer, rat_next) {
166		if (TS_CMP(&rat->rat_tm, &now, <=)) {
167			if (((*rat->rat_expire)(rat->rat_expire_data) == NULL))
168				continue; /* the timer was removed */
169			if (rat->rat_update)
170				(*rat->rat_update)(rat->rat_update_data, &rat->rat_tm);
171			TS_ADD(&rat->rat_tm, &now, &rat->rat_tm);
172		}
173		if (TS_CMP(&rat->rat_tm, &tm_max, <))
174			tm_max = rat->rat_tm;
175	}
176	if (TS_CMP(&tm_max, &tm_limit, ==)) {
177		/* no need to timeout */
178		return (NULL);
179	} else if (TS_CMP(&tm_max, &now, <)) {
180		/* this may occur when the interval is too small */
181		returnval.tv_sec = returnval.tv_nsec = 0;
182	} else
183		TS_SUB(&tm_max, &now, &returnval);
184	return (&returnval);
185}
186
187void
188rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *rat)
189{
190	struct timespec now;
191
192	/* reset the timer */
193	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
194	TS_ADD(&now, tm, &rat->rat_tm);
195
196	/* update the next expiration time */
197	if (TS_CMP(&rat->rat_tm, &tm_max, <))
198		tm_max = rat->rat_tm;
199
200	return;
201}
202