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