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