Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / usr.sbin / IPXrouted / timer.c
blob83309354a9d5cdc5d56e9e546ccbac38234c30e8
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1995 John Hay. All rights reserved.
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim. That work included by permission.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
38 * $FreeBSD: src/usr.sbin/IPXrouted/timer.c,v 1.5 1999/08/28 01:15:06 peter Exp $
39 * $DragonFly: src/usr.sbin/IPXrouted/timer.c,v 1.3 2004/03/11 09:38:59 hmp Exp $
41 * @(#)timer.c 8.1 (Berkeley) 6/5/93
45 * Routing Table Management Daemon
47 #include "defs.h"
48 #include <unistd.h>
49 #include <stdlib.h>
51 int timeval = -TIMER_RATE;
54 * Timer routine. Performs routing information supply
55 * duties and manages timers on routing and SAP table entries.
57 void
58 timer(void)
60 struct rthash *rh;
61 struct rt_entry *rt;
62 struct sap_hash *sh;
63 struct sap_entry *sap;
64 struct sap_hash *sap_base = sap_head;
65 int timetobroadcast, ripbroadcast, sapbroadcast;
67 timeval += TIMER_RATE;
68 if (lookforinterfaces && (timeval % CHECK_INTERVAL) == 0)
69 ifinit();
70 timetobroadcast = supplier && (timeval % SUPPLY_INTERVAL) == 0;
71 ripbroadcast = supplier && timetobroadcast &&
72 (timeval % RIP_INTERVAL) == 0;
73 sapbroadcast = timetobroadcast && dosap && !ripbroadcast;
75 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++) {
76 rt = rh->rt_forw;
77 for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
78 if (rt->rt_clone) {
79 struct rt_entry *trt, *prt;
81 * If a clone expire free it and mark the
82 * main route RTS_CHANGED.
84 prt = rt;
85 trt = rt->rt_clone;
86 while (trt) {
87 trt->rt_timer += TIMER_RATE;
88 if (trt->rt_timer >= EXPIRE_TIME) {
89 prt->rt_clone = trt->rt_clone;
90 free((char *)trt);
91 trt = prt->rt_clone;
92 rt->rt_state |= RTS_CHANGED;
93 } else {
94 prt = trt;
95 trt = prt->rt_clone;
100 * We don't advance time on a routing entry for
101 * a passive gateway or that for our only interface.
102 * The latter is excused because we don't act as
103 * a routing information supplier and hence would
104 * time it out. This is fair as if it's down
105 * we're cut off from the world anyway and it's
106 * not likely we'll grow any new hardware in
107 * the mean time.
109 if (!(rt->rt_state & RTS_PASSIVE) &&
110 !(rt->rt_state & RTS_INTERFACE))
111 rt->rt_timer += TIMER_RATE;
112 if (rt->rt_timer >= EXPIRE_TIME) {
113 rt->rt_metric = HOPCNT_INFINITY;
114 rt->rt_state |= RTS_CHANGED;
116 if (rt->rt_timer >= GARBAGE_TIME) {
117 rt = rt->rt_back;
118 /* Perhaps we should send a REQUEST for this route? */
119 rtdelete(rt->rt_forw);
120 continue;
122 if (rt->rt_state & RTS_CHANGED) {
123 rt->rt_state &= ~RTS_CHANGED;
124 /* don't send extraneous packets */
125 if (!supplier || ripbroadcast)
126 continue;
127 if ((rt->rt_metric + 1) == HOPCNT_INFINITY)
128 continue;
129 msg->rip_cmd = htons(RIPCMD_RESPONSE);
130 msg->rip_nets[0].rip_dst =
131 (satoipx_addr(rt->rt_dst)).x_net;
132 msg->rip_nets[0].rip_metric =
133 htons(min(rt->rt_metric+1, HOPCNT_INFINITY));
134 msg->rip_nets[0].rip_ticks =
135 htons(rt->rt_ticks + 1);
136 toall(sndmsg, rt, 0);
140 if (ripbroadcast)
141 toall(supply, NULL, 0);
144 * Now do the SAP stuff.
146 for (sh = sap_base; sh < &sap_base[SAPHASHSIZ]; sh++) {
147 sap = sh->forw;
148 for (; sap != (struct sap_entry *)sh; sap = sap->forw) {
149 if (sap->clone) {
150 struct sap_entry *tsap, *psap;
152 * If a clone expire free it and mark the
153 * main sap entry RTS_CHANGED.
155 psap = sap;
156 tsap = sap->clone;
157 while (tsap) {
158 tsap->timer += TIMER_RATE;
159 if (tsap->timer >= EXPIRE_TIME) {
160 psap->clone = tsap->clone;
161 free((char *)tsap);
162 tsap = psap->clone;
163 sap->state |= RTS_CHANGED;
164 } else {
165 psap = tsap;
166 tsap = psap->clone;
170 sap->timer += TIMER_RATE;
171 if (sap->timer >= EXPIRE_TIME) {
172 sap->sap.hops = htons(HOPCNT_INFINITY);
173 sap->state |= RTS_CHANGED;
175 if (sap->timer >= GARBAGE_TIME) {
176 sap = sap->back;
177 /* Perhaps we should send a REQUEST for this route? */
178 sap_delete(sap->forw);
179 continue;
182 * XXX sap_sndmsg on RTS_CHANGED
184 if (sap->state & RTS_CHANGED) {
185 sap->state &= ~RTS_CHANGED;
186 #ifdef notyet
187 /* don't send extraneous packets */
188 if (!supplier || sapbroadcast)
189 continue;
190 if ((ntohs(sap->sap.hops) + 1) == HOPCNT_INFINITY)
191 continue;
192 sap_msg->sap_cmd = htons(SAP_RESP);
193 sap_msg->sap[0] = sap->sap;
194 sap_msg->sap[0].hops =
195 htons(min(sap->sap.hops+1, HOPCNT_INFINITY));
196 toall(sapsndmsg, rt, 0);
197 #endif
201 if (sapbroadcast)
202 sap_supply_toall(0);
203 if (ftrace && sapbroadcast)
204 dumpsaptable(ftrace, sap_head);
208 * On hangup, let everyone know we're going away.
210 void
211 hup(void)
213 struct rthash *rh;
214 struct rt_entry *rt;
215 struct sap_hash *sh;
216 struct sap_entry *sap;
218 if (supplier) {
219 for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++) {
220 rt = rh->rt_forw;
221 for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw)
222 rt->rt_metric = HOPCNT_INFINITY;
224 toall(supply, NULL, 0);
227 * Now for SAP.
229 for (sh = sap_head; sh < &sap_head[SAPHASHSIZ]; sh++) {
230 sap = sh->forw;
231 for (; sap != (struct sap_entry *)sh; sap = sap->forw)
232 sap->sap.hops = htons(HOPCNT_INFINITY);
234 if (dosap)
235 sap_supply_toall(0);
237 exit(1);