input.c revision 1.15 1 /* $NetBSD: input.c,v 1.15 1995/06/20 22:27:50 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.15 1995/06/20 22:27:50 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 (dst.sa_family < af_max &&
157 afswitch[dst.sa_family].af_hash) {
158 (*afswitch[n->rip_family].af_get)(DESTINATION,
159 n, &dst);
160 rt = rtlookup(&dst);
161 }
162 else
163 rt = 0;
164 #define min(a, b) (a < b ? a : b)
165 n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
166 min(rt->rt_metric + 1, HOPCNT_INFINITY);
167 n->rip_metric = htonl(n->rip_metric);
168 }
169 rip->rip_cmd = RIPCMD_RESPONSE;
170 memcpy(packet, rip, size);
171 (*afp->af_output)(s, 0, from, size);
172 return;
173
174 case RIPCMD_TRACEON:
175 case RIPCMD_TRACEOFF:
176 /* verify message came from a privileged port */
177 if ((*afp->af_portcheck)(from) == 0)
178 return;
179
180 if ((ifp = rip_verify(from)) == NULL)
181 return;
182
183 ((char *)rip)[size] = '\0';
184 if (rip->rip_cmd == RIPCMD_TRACEON)
185 traceon(rip->rip_tracefile);
186 else
187 traceoff();
188 return;
189
190 case RIPCMD_RESPONSE:
191 /* verify message came from a router */
192 if ((*afp->af_portmatch)(from) == 0)
193 return;
194 (*afp->af_canon)(from);
195 /* are we talking to ourselves? */
196 ifp = if_ifwithaddr(from);
197 if (ifp) {
198 if (ifp->int_flags & IFF_PASSIVE) {
199 syslog(LOG_ERR,
200 "bogus input (from passive interface, %s)",
201 (*afswitch[from->sa_family].af_format)(from,
202 buf1, sizeof(buf1)));
203 return;
204 }
205 rt = rtfind(from);
206 if (rt == 0 || (((rt->rt_state & RTS_INTERFACE) == 0) &&
207 rt->rt_metric >= ifp->int_metric))
208 addrouteforif(ifp);
209 else
210 rt->rt_timer = 0;
211 return;
212 }
213 /*
214 * Update timer for interface on which the packet arrived.
215 * If from other end of a point-to-point link that isn't
216 * in the routing tables, (re-)add the route.
217 */
218 if ((rt = rtfind(from)) &&
219 (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
220 rt->rt_timer = 0;
221 else if ((ifp = if_ifwithdstaddr(from)) &&
222 (rt == 0 || rt->rt_metric >= ifp->int_metric))
223 addrouteforif(ifp);
224
225 if ((ifp = rip_verify(from)) == NULL)
226 return;
227
228 size -= 4 * sizeof (char);
229 n = rip->rip_nets;
230 for (; size > 0; size -= sizeof (struct netinfo), n++) {
231 if (size < sizeof (struct netinfo))
232 break;
233 n->rip_metric = ntohl(n->rip_metric);
234 n->rip_family = ntohs(n->rip_family);
235 if (!(*afswitch[n->rip_family].af_get)(DESTINATION, n,
236 &dst))
237 continue;
238 if (!(*afswitch[n->rip_family].af_get)(NETMASK,
239 n, &netmask))
240 memset(&netmask, 0, sizeof(netmask));
241 if (!(*afswitch[n->rip_family].af_get)(GATEWAY,
242 n, &gateway))
243 memcpy(&gateway, from, sizeof(gateway));
244 if (dst.sa_family >= af_max ||
245 (afp = &afswitch[dst.sa_family])->af_hash == NULL) {
246 syslog(LOG_INFO,
247 "route in unsupported address family (%d), from %s (af %d)\n",
248 dst.sa_family,
249 (*afswitch[from->sa_family].af_format)(from,
250 buf1, sizeof(buf1)),
251 from->sa_family);
252 continue;
253 }
254 if (((*afp->af_checkhost)(&dst)) == 0) {
255 syslog(LOG_DEBUG,
256 "bad host %s in route from %s (af %d)\n",
257 (*afswitch[dst.sa_family].af_format)(
258 &dst, buf1, sizeof(buf1)),
259 (*afswitch[from->sa_family].af_format)(from,
260 buf2, sizeof(buf2)),
261 from->sa_family);
262 continue;
263 }
264 if (n->rip_metric == 0 ||
265 (unsigned) n->rip_metric > HOPCNT_INFINITY) {
266 if (memcmp(from, &badfrom,
267 sizeof(badfrom)) != 0) {
268 syslog(LOG_ERR,
269 "bad metric (%d) from %s\n",
270 n->rip_metric,
271 (*afswitch[from->sa_family].af_format)(from,
272 buf1, sizeof(buf1)));
273 badfrom = *from;
274 }
275 continue;
276 }
277 /*
278 * Adjust metric according to incoming interface.
279 */
280 if ((unsigned) n->rip_metric < HOPCNT_INFINITY)
281 n->rip_metric += ifp->int_metric;
282 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
283 n->rip_metric = HOPCNT_INFINITY;
284 rt = rtlookup(&dst);
285 if (rt == 0 ||
286 (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
287 (RTS_INTERNAL|RTS_INTERFACE)) {
288 /*
289 * If we're hearing a logical network route
290 * back from a peer to which we sent it,
291 * ignore it.
292 */
293 if (rt && rt->rt_state & RTS_SUBNET &&
294 (*afp->af_sendroute)(rt, from))
295 continue;
296 if ((unsigned)n->rip_metric < HOPCNT_INFINITY) {
297 /*
298 * Look for an equivalent route that
299 * includes this one before adding
300 * this route.
301 */
302 rt = rtfind(&dst);
303 if (rt && equal(&gateway, &rt->rt_router))
304 continue;
305 rtadd(&dst, &gateway, &netmask,
306 n->rip_metric, 0);
307 changes++;
308 }
309 continue;
310 }
311
312 /*
313 * Update if from gateway and different,
314 * shorter, or equivalent but old route
315 * is getting stale.
316 */
317 if (equal(&gateway, &rt->rt_router)) {
318 if (n->rip_metric != rt->rt_metric) {
319 rtchange(rt, &gateway,
320 &netmask, n->rip_metric);
321 changes++;
322 rt->rt_timer = 0;
323 if (rt->rt_metric >= HOPCNT_INFINITY)
324 rt->rt_timer =
325 GARBAGE_TIME - EXPIRE_TIME;
326 } else if (rt->rt_metric < HOPCNT_INFINITY)
327 rt->rt_timer = 0;
328 } else if ((unsigned) n->rip_metric < rt->rt_metric ||
329 (rt->rt_metric == n->rip_metric &&
330 rt->rt_timer > (EXPIRE_TIME/2) &&
331 (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
332 rtchange(rt, &gateway, &netmask, n->rip_metric);
333 changes++;
334 rt->rt_timer = 0;
335 }
336 }
337 break;
338 }
339
340 /*
341 * If changes have occurred, and if we have not sent a broadcast
342 * recently, send a dynamic update. This update is sent only
343 * on interfaces other than the one on which we received notice
344 * of the change. If we are within MIN_WAITTIME of a full update,
345 * don't bother sending; if we just sent a dynamic update
346 * and set a timer (nextbcast), delay until that time.
347 * If we just sent a full update, delay the dynamic update.
348 * Set a timer for a randomized value to suppress additional
349 * dynamic updates until it expires; if we delayed sending
350 * the current changes, set needupdate.
351 */
352 if (changes && supplier &&
353 now.tv_sec - lastfullupdate.tv_sec < SUPPLY_INTERVAL-MAX_WAITTIME) {
354 u_long delay;
355
356 if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME &&
357 timercmp(&nextbcast, &now, <)) {
358 if (traceactions)
359 fprintf(ftrace, "send dynamic update\n");
360 toall(supply, RTS_CHANGED, ifp);
361 lastbcast = now;
362 needupdate = 0;
363 nextbcast.tv_sec = 0;
364 } else {
365 needupdate++;
366 if (traceactions)
367 fprintf(ftrace, "delay dynamic update\n");
368 }
369 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
370 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
371
372 if (nextbcast.tv_sec == 0) {
373 delay = RANDOMDELAY();
374 if (traceactions)
375 fprintf(ftrace,
376 "inhibit dynamic update for %d usec\n",
377 delay);
378 nextbcast.tv_sec = delay / 1000000;
379 nextbcast.tv_usec = delay % 1000000;
380 timeradd(&nextbcast, &now, &nextbcast);
381 /*
382 * If the next possibly dynamic update
383 * is within MIN_WAITTIME of the next full update,
384 * force the delay past the full update,
385 * or we might send a dynamic update just before
386 * the full update.
387 */
388 if (nextbcast.tv_sec > lastfullupdate.tv_sec +
389 SUPPLY_INTERVAL - MIN_WAITTIME)
390 nextbcast.tv_sec = lastfullupdate.tv_sec +
391 SUPPLY_INTERVAL + 1;
392 }
393 }
394 }
395