input.c revision 1.16 1 /* $NetBSD: input.c,v 1.16 1995/07/13 23:20:10 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 6/5/93";
39 #else
40 static char rcsid[] = "$NetBSD: input.c,v 1.16 1995/07/13 23:20:10 christos Exp $";
41 #endif
42 #endif /* not lint */
43
44 /*
45 * Routing Table Management Daemon
46 */
47 #include "defs.h"
48 #include <syslog.h>
49
50
51 /*
52 * "Authenticate" router from which message originated.
53 * We accept routing packets from routers directly connected
54 * via broadcast or point-to-point networks,
55 * and from those listed in /etc/gateways.
56 */
57 static struct interface *
58 rip_verify(from)
59 struct sockaddr *from;
60 {
61 struct interface *ifp;
62 char buf[256];
63
64 if ((ifp = if_iflookup(from)) == 0) {
65 syslog(LOG_ERR, "trace command from unknown router, %s",
66 (*afswitch[from->sa_family].af_format)(from, buf,
67 sizeof(buf)));
68 return NULL;
69 }
70
71 if ((ifp->int_flags &
72 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_REMOTE)) == 0) {
73 syslog(LOG_ERR,
74 "trace command from router %s, with bad flags %x",
75 (*afswitch[from->sa_family].af_format)(from, buf,
76 sizeof(buf)),
77 ifp->int_flags);
78 return NULL;
79 }
80
81 if ((ifp->int_flags & IFF_PASSIVE) != 0) {
82 syslog(LOG_ERR,
83 "trace command from %s on an active interface",
84 (*afswitch[from->sa_family].af_format)(from, buf,
85 sizeof(buf)));
86 return NULL;
87 }
88
89 return ifp;
90 }
91
92
93 /*
94 * Process a newly received packet.
95 */
96 void
97 rip_input(from, rip, size)
98 struct sockaddr *from;
99 register struct rip *rip;
100 int size;
101 {
102 register struct rt_entry *rt;
103 register struct netinfo *n;
104 register struct interface *ifp;
105 struct sockaddr dst, gateway, netmask;
106 int count, changes = 0;
107 register struct afswitch *afp;
108 static struct sockaddr badfrom;
109 char buf1[256], buf2[256];
110
111 ifp = 0;
112 TRACE_INPUT(ifp, from, (char *)rip, size);
113 if (from->sa_family >= af_max ||
114 (afp = &afswitch[from->sa_family])->af_hash == NULL) {
115 syslog(LOG_INFO,
116 "\"from\" address in unsupported address family (%d), cmd %d\n",
117 from->sa_family, rip->rip_cmd);
118 return;
119 }
120 if (rip->rip_vers == 0) {
121 syslog(LOG_ERR,
122 "RIP version 0 packet received from %s! (cmd %d)",
123 (*afswitch[from->sa_family].af_format)(from, buf1,
124 sizeof(buf1)),
125 rip->rip_cmd);
126 return;
127 }
128
129 switch (rip->rip_cmd) {
130
131 case RIPCMD_REQUEST:
132 n = rip->rip_nets;
133 count = size - ((char *)n - (char *)rip);
134 if (count < sizeof (struct netinfo))
135 return;
136 for (; count > 0; n++) {
137 if (count < sizeof (struct netinfo))
138 break;
139 count -= sizeof (struct netinfo);
140
141 n->rip_metric = ntohl(n->rip_metric);
142 n->rip_family = ntohs(n->rip_family);
143 /*
144 * A single entry with sa_family == AF_UNSPEC and
145 * metric ``infinity'' means ``all routes''.
146 * We respond to routers only if we are acting
147 * as a supplier, or to anyone other than a router
148 * (eg, query).
149 */
150 if (n->rip_family == AF_UNSPEC &&
151 n->rip_metric == HOPCNT_INFINITY && count == 0) {
152 if (supplier || (*afp->af_portmatch)(from) == 0)
153 supply(from, 0, 0, 0);
154 return;
155 }
156 if (n->rip_family < af_max &&
157 afswitch[n->rip_family].af_hash) {
158 if (!(*afswitch[n->rip_family].af_get)(
159 DESTINATION, n, &dst))
160 return;
161 rt = rtlookup(&dst);
162 }
163 else
164 rt = 0;
165 #define min(a, b) (a < b ? a : b)
166 n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
167 min(rt->rt_metric + 1, HOPCNT_INFINITY);
168 n->rip_metric = htonl(n->rip_metric);
169 }
170 rip->rip_cmd = RIPCMD_RESPONSE;
171 memcpy(packet, rip, size);
172 (*afp->af_output)(s, 0, from, size);
173 return;
174
175 case RIPCMD_TRACEON:
176 case RIPCMD_TRACEOFF:
177 /* verify message came from a privileged port */
178 if ((*afp->af_portcheck)(from) == 0)
179 return;
180
181 if ((ifp = rip_verify(from)) == NULL)
182 return;
183
184 ((char *)rip)[size] = '\0';
185 if (rip->rip_cmd == RIPCMD_TRACEON)
186 traceon(rip->rip_tracefile);
187 else
188 traceoff();
189 return;
190
191 case RIPCMD_RESPONSE:
192 /* verify message came from a router */
193 if ((*afp->af_portmatch)(from) == 0)
194 return;
195 (*afp->af_canon)(from);
196 /* are we talking to ourselves? */
197 ifp = if_ifwithaddr(from);
198 if (ifp) {
199 if (ifp->int_flags & IFF_PASSIVE) {
200 syslog(LOG_ERR,
201 "bogus input (from passive interface, %s)",
202 (*afswitch[from->sa_family].af_format)(from,
203 buf1, sizeof(buf1)));
204 return;
205 }
206 rt = rtfind(from);
207 if (rt == 0 || (((rt->rt_state & RTS_INTERFACE) == 0) &&
208 rt->rt_metric >= ifp->int_metric))
209 addrouteforif(ifp);
210 else
211 rt->rt_timer = 0;
212 return;
213 }
214 /*
215 * Update timer for interface on which the packet arrived.
216 * If from other end of a point-to-point link that isn't
217 * in the routing tables, (re-)add the route.
218 */
219 if ((rt = rtfind(from)) &&
220 (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
221 rt->rt_timer = 0;
222 else if ((ifp = if_ifwithdstaddr(from)) &&
223 (rt == 0 || rt->rt_metric >= ifp->int_metric))
224 addrouteforif(ifp);
225
226 if ((ifp = rip_verify(from)) == NULL)
227 return;
228
229 size -= 4 * sizeof (char);
230 n = rip->rip_nets;
231 for (; size > 0; size -= sizeof (struct netinfo), n++) {
232 if (size < sizeof (struct netinfo))
233 break;
234 n->rip_metric = ntohl(n->rip_metric);
235 n->rip_family = ntohs(n->rip_family);
236 if (!(*afswitch[n->rip_family].af_get)(DESTINATION, n,
237 &dst))
238 continue;
239 if (!(*afswitch[n->rip_family].af_get)(NETMASK,
240 n, &netmask))
241 memset(&netmask, 0, sizeof(netmask));
242 if (!(*afswitch[n->rip_family].af_get)(GATEWAY,
243 n, &gateway))
244 memcpy(&gateway, from, sizeof(gateway));
245 if (dst.sa_family >= af_max ||
246 (afp = &afswitch[dst.sa_family])->af_hash == NULL) {
247 syslog(LOG_INFO,
248 "route in unsupported address family (%d), from %s (af %d)\n",
249 dst.sa_family,
250 (*afswitch[from->sa_family].af_format)(from,
251 buf1, sizeof(buf1)),
252 from->sa_family);
253 continue;
254 }
255 if (((*afp->af_checkhost)(&dst)) == 0) {
256 syslog(LOG_DEBUG,
257 "bad host %s in route from %s (af %d)\n",
258 (*afswitch[dst.sa_family].af_format)(
259 &dst, buf1, sizeof(buf1)),
260 (*afswitch[from->sa_family].af_format)(from,
261 buf2, sizeof(buf2)),
262 from->sa_family);
263 continue;
264 }
265 if (n->rip_metric == 0 ||
266 (unsigned) n->rip_metric > HOPCNT_INFINITY) {
267 if (memcmp(from, &badfrom,
268 sizeof(badfrom)) != 0) {
269 syslog(LOG_ERR,
270 "bad metric (%d) from %s\n",
271 n->rip_metric,
272 (*afswitch[from->sa_family].af_format)(from,
273 buf1, sizeof(buf1)));
274 badfrom = *from;
275 }
276 continue;
277 }
278 /*
279 * Adjust metric according to incoming interface.
280 */
281 if ((unsigned) n->rip_metric < HOPCNT_INFINITY)
282 n->rip_metric += ifp->int_metric;
283 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
284 n->rip_metric = HOPCNT_INFINITY;
285 rt = rtlookup(&dst);
286 if (rt == 0 ||
287 (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
288 (RTS_INTERNAL|RTS_INTERFACE)) {
289 /*
290 * If we're hearing a logical network route
291 * back from a peer to which we sent it,
292 * ignore it.
293 */
294 if (rt && rt->rt_state & RTS_SUBNET &&
295 (*afp->af_sendroute)(rt, from))
296 continue;
297 if ((unsigned)n->rip_metric < HOPCNT_INFINITY) {
298 /*
299 * Look for an equivalent route that
300 * includes this one before adding
301 * this route.
302 */
303 rt = rtfind(&dst);
304 if (rt && equal(&gateway, &rt->rt_router))
305 continue;
306 rtadd(&dst, &gateway, &netmask,
307 n->rip_metric, 0);
308 changes++;
309 }
310 continue;
311 }
312
313 /*
314 * Update if from gateway and different,
315 * shorter, or equivalent but old route
316 * is getting stale.
317 */
318 if (equal(&gateway, &rt->rt_router)) {
319 if (n->rip_metric != rt->rt_metric) {
320 rtchange(rt, &gateway,
321 &netmask, n->rip_metric);
322 changes++;
323 rt->rt_timer = 0;
324 if (rt->rt_metric >= HOPCNT_INFINITY)
325 rt->rt_timer =
326 GARBAGE_TIME - EXPIRE_TIME;
327 } else if (rt->rt_metric < HOPCNT_INFINITY)
328 rt->rt_timer = 0;
329 } else if ((unsigned) n->rip_metric < rt->rt_metric ||
330 (rt->rt_metric == n->rip_metric &&
331 rt->rt_timer > (EXPIRE_TIME/2) &&
332 (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
333 rtchange(rt, &gateway, &netmask, n->rip_metric);
334 changes++;
335 rt->rt_timer = 0;
336 }
337 }
338 break;
339 }
340
341 /*
342 * If changes have occurred, and if we have not sent a broadcast
343 * recently, send a dynamic update. This update is sent only
344 * on interfaces other than the one on which we received notice
345 * of the change. If we are within MIN_WAITTIME of a full update,
346 * don't bother sending; if we just sent a dynamic update
347 * and set a timer (nextbcast), delay until that time.
348 * If we just sent a full update, delay the dynamic update.
349 * Set a timer for a randomized value to suppress additional
350 * dynamic updates until it expires; if we delayed sending
351 * the current changes, set needupdate.
352 */
353 if (changes && supplier &&
354 now.tv_sec - lastfullupdate.tv_sec < SUPPLY_INTERVAL-MAX_WAITTIME) {
355 u_long delay;
356
357 if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME &&
358 timercmp(&nextbcast, &now, <)) {
359 if (traceactions)
360 fprintf(ftrace, "send dynamic update\n");
361 toall(supply, RTS_CHANGED, ifp);
362 lastbcast = now;
363 needupdate = 0;
364 nextbcast.tv_sec = 0;
365 } else {
366 needupdate++;
367 if (traceactions)
368 fprintf(ftrace, "delay dynamic update\n");
369 }
370 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
371 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
372
373 if (nextbcast.tv_sec == 0) {
374 delay = RANDOMDELAY();
375 if (traceactions)
376 fprintf(ftrace,
377 "inhibit dynamic update for %d usec\n",
378 delay);
379 nextbcast.tv_sec = delay / 1000000;
380 nextbcast.tv_usec = delay % 1000000;
381 timeradd(&nextbcast, &now, &nextbcast);
382 /*
383 * If the next possibly dynamic update
384 * is within MIN_WAITTIME of the next full update,
385 * force the delay past the full update,
386 * or we might send a dynamic update just before
387 * the full update.
388 */
389 if (nextbcast.tv_sec > lastfullupdate.tv_sec +
390 SUPPLY_INTERVAL - MIN_WAITTIME)
391 nextbcast.tv_sec = lastfullupdate.tv_sec +
392 SUPPLY_INTERVAL + 1;
393 }
394 }
395 }
396