Merge from vendor branch AWK:
[dragonfly.git] / usr.sbin / IPXrouted / timer.c
1 /*
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (c) 1995 John Hay.  All rights reserved.
6  *
7  * This file includes significant work done at Cornell University by
8  * Bill Nesheim.  That work included by permission.
9  *
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.
25  *
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.
37  *
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 $
40  *
41  * @(#)timer.c  8.1 (Berkeley) 6/5/93
42  */
43
44 /*
45  * Routing Table Management Daemon
46  */
47 #include "defs.h"
48 #include <unistd.h>
49 #include <stdlib.h>
50
51 int     timeval = -TIMER_RATE;
52
53 /*
54  * Timer routine.  Performs routing information supply
55  * duties and manages timers on routing and SAP table entries.
56  */
57 void
58 timer(void)
59 {
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;
66
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;
74
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;
80                                 /*
81                                  * If a clone expire free it and mark the
82                                  * main route RTS_CHANGED.
83                                  */
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;
96                                         }
97                                 }
98                         }
99                         /*
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.
108                          */
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;
115                         }
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;
121                         }
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);
137                         }
138                 }
139         }
140         if (ripbroadcast)
141                 toall(supply, NULL, 0);
142
143         /* 
144          * Now do the SAP stuff.
145          */
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;
151                                 /*
152                                  * If a clone expire free it and mark the
153                                  * main sap entry RTS_CHANGED.
154                                  */
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;
167                                         }
168                                 }
169                         }
170                         sap->timer += TIMER_RATE;
171                         if (sap->timer >= EXPIRE_TIME) {
172                                 sap->sap.hops = htons(HOPCNT_INFINITY);
173                                 sap->state |= RTS_CHANGED;
174                         }
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;
180                         }
181                         /*
182                          * XXX sap_sndmsg on RTS_CHANGED
183                          */
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
198                         }
199                 }
200         }
201         if (sapbroadcast)
202                 sap_supply_toall(0);
203         if (ftrace && sapbroadcast)
204                 dumpsaptable(ftrace, sap_head);
205 }
206
207 /*
208  * On hangup, let everyone know we're going away.
209  */
210 void
211 hup(void)
212 {
213         struct rthash *rh;
214         struct rt_entry *rt;
215         struct sap_hash *sh;
216         struct sap_entry *sap;
217
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;
223                 }
224                 toall(supply, NULL, 0);
225
226                 /*
227                  * Now for SAP.
228                  */
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);
233                 }
234                 if (dosap)
235                         sap_supply_toall(0);
236         }
237         exit(1);
238 }