input.c revision 1.11 1 1.11 cgd /* $NetBSD: input.c,v 1.11 1995/04/24 13:24:30 cgd 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.11 cgd static char rcsid[] = "$NetBSD: input.c,v 1.11 1995/04/24 13:24:30 cgd 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.1 cgd
67 1.1 cgd ifp = 0;
68 1.11 cgd TRACE_INPUT(ifp, from, (char *)rip, size);
69 1.1 cgd if (from->sa_family >= af_max ||
70 1.1 cgd (afp = &afswitch[from->sa_family])->af_hash == (int (*)())0) {
71 1.1 cgd syslog(LOG_INFO,
72 1.1 cgd "\"from\" address in unsupported address family (%d), cmd %d\n",
73 1.1 cgd from->sa_family, rip->rip_cmd);
74 1.1 cgd return;
75 1.1 cgd }
76 1.1 cgd if (rip->rip_vers == 0) {
77 1.1 cgd syslog(LOG_ERR,
78 1.1 cgd "RIP version 0 packet received from %s! (cmd %d)",
79 1.1 cgd (*afswitch[from->sa_family].af_format)(from), rip->rip_cmd);
80 1.1 cgd return;
81 1.1 cgd }
82 1.1 cgd switch (rip->rip_cmd) {
83 1.1 cgd
84 1.1 cgd case RIPCMD_REQUEST:
85 1.1 cgd n = rip->rip_nets;
86 1.1 cgd count = size - ((char *)n - (char *)rip);
87 1.1 cgd if (count < sizeof (struct netinfo))
88 1.1 cgd return;
89 1.1 cgd for (; count > 0; n++) {
90 1.1 cgd if (count < sizeof (struct netinfo))
91 1.1 cgd break;
92 1.1 cgd count -= sizeof (struct netinfo);
93 1.1 cgd
94 1.1 cgd #if BSD < 198810
95 1.1 cgd if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
96 1.1 cgd n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
97 1.1 cgd #else
98 1.1 cgd #define osa(x) ((struct osockaddr *)(&(x)))
99 1.1 cgd n->rip_dst.sa_family =
100 1.1 cgd ntohs(osa(n->rip_dst)->sa_family);
101 1.1 cgd n->rip_dst.sa_len = sizeof(n->rip_dst);
102 1.1 cgd #endif
103 1.1 cgd n->rip_metric = ntohl(n->rip_metric);
104 1.1 cgd /*
105 1.1 cgd * A single entry with sa_family == AF_UNSPEC and
106 1.1 cgd * metric ``infinity'' means ``all routes''.
107 1.1 cgd * We respond to routers only if we are acting
108 1.1 cgd * as a supplier, or to anyone other than a router
109 1.1 cgd * (eg, query).
110 1.1 cgd */
111 1.1 cgd if (n->rip_dst.sa_family == AF_UNSPEC &&
112 1.1 cgd n->rip_metric == HOPCNT_INFINITY && count == 0) {
113 1.1 cgd if (supplier || (*afp->af_portmatch)(from) == 0)
114 1.1 cgd supply(from, 0, 0, 0);
115 1.1 cgd return;
116 1.1 cgd }
117 1.1 cgd if (n->rip_dst.sa_family < af_max &&
118 1.1 cgd afswitch[n->rip_dst.sa_family].af_hash)
119 1.1 cgd rt = rtlookup(&n->rip_dst);
120 1.1 cgd else
121 1.1 cgd rt = 0;
122 1.1 cgd #define min(a, b) (a < b ? a : b)
123 1.1 cgd n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
124 1.1 cgd min(rt->rt_metric + 1, HOPCNT_INFINITY);
125 1.1 cgd #if BSD < 198810
126 1.1 cgd if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
127 1.1 cgd n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
128 1.1 cgd #else
129 1.1 cgd osa(n->rip_dst)->sa_family =
130 1.1 cgd htons(n->rip_dst.sa_family);
131 1.1 cgd #endif
132 1.1 cgd n->rip_metric = htonl(n->rip_metric);
133 1.1 cgd }
134 1.1 cgd rip->rip_cmd = RIPCMD_RESPONSE;
135 1.6 mycroft memcpy(packet, rip, size);
136 1.1 cgd (*afp->af_output)(s, 0, from, size);
137 1.1 cgd return;
138 1.1 cgd
139 1.1 cgd case RIPCMD_TRACEON:
140 1.1 cgd case RIPCMD_TRACEOFF:
141 1.1 cgd /* verify message came from a privileged port */
142 1.1 cgd if ((*afp->af_portcheck)(from) == 0)
143 1.1 cgd return;
144 1.1 cgd if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
145 1.1 cgd (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
146 1.1 cgd ifp->int_flags & IFF_PASSIVE) {
147 1.1 cgd syslog(LOG_ERR, "trace command from unknown router, %s",
148 1.1 cgd (*afswitch[from->sa_family].af_format)(from));
149 1.1 cgd return;
150 1.1 cgd }
151 1.1 cgd ((char *)rip)[size] = '\0';
152 1.1 cgd if (rip->rip_cmd == RIPCMD_TRACEON)
153 1.1 cgd traceon(rip->rip_tracefile);
154 1.1 cgd else
155 1.1 cgd traceoff();
156 1.1 cgd return;
157 1.1 cgd
158 1.1 cgd case RIPCMD_RESPONSE:
159 1.1 cgd /* verify message came from a router */
160 1.1 cgd if ((*afp->af_portmatch)(from) == 0)
161 1.1 cgd return;
162 1.1 cgd (*afp->af_canon)(from);
163 1.1 cgd /* are we talking to ourselves? */
164 1.1 cgd ifp = if_ifwithaddr(from);
165 1.1 cgd if (ifp) {
166 1.1 cgd if (ifp->int_flags & IFF_PASSIVE) {
167 1.1 cgd syslog(LOG_ERR,
168 1.1 cgd "bogus input (from passive interface, %s)",
169 1.1 cgd (*afswitch[from->sa_family].af_format)(from));
170 1.1 cgd return;
171 1.1 cgd }
172 1.1 cgd rt = rtfind(from);
173 1.1 cgd if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) &&
174 1.1 cgd rt->rt_metric >= ifp->int_metric)
175 1.1 cgd addrouteforif(ifp);
176 1.1 cgd else
177 1.1 cgd rt->rt_timer = 0;
178 1.1 cgd return;
179 1.1 cgd }
180 1.1 cgd /*
181 1.1 cgd * Update timer for interface on which the packet arrived.
182 1.1 cgd * If from other end of a point-to-point link that isn't
183 1.1 cgd * in the routing tables, (re-)add the route.
184 1.1 cgd */
185 1.1 cgd if ((rt = rtfind(from)) &&
186 1.1 cgd (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
187 1.1 cgd rt->rt_timer = 0;
188 1.1 cgd else if ((ifp = if_ifwithdstaddr(from)) &&
189 1.1 cgd (rt == 0 || rt->rt_metric >= ifp->int_metric))
190 1.1 cgd addrouteforif(ifp);
191 1.1 cgd /*
192 1.1 cgd * "Authenticate" router from which message originated.
193 1.1 cgd * We accept routing packets from routers directly connected
194 1.1 cgd * via broadcast or point-to-point networks,
195 1.1 cgd * and from those listed in /etc/gateways.
196 1.1 cgd */
197 1.1 cgd if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
198 1.1 cgd (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
199 1.1 cgd ifp->int_flags & IFF_PASSIVE) {
200 1.7 mycroft if (memcmp(from, &badfrom, sizeof(badfrom)) != 0) {
201 1.1 cgd syslog(LOG_ERR,
202 1.1 cgd "packet from unknown router, %s",
203 1.1 cgd (*afswitch[from->sa_family].af_format)(from));
204 1.1 cgd badfrom = *from;
205 1.1 cgd }
206 1.1 cgd return;
207 1.1 cgd }
208 1.1 cgd size -= 4 * sizeof (char);
209 1.1 cgd n = rip->rip_nets;
210 1.1 cgd for (; size > 0; size -= sizeof (struct netinfo), n++) {
211 1.1 cgd if (size < sizeof (struct netinfo))
212 1.1 cgd break;
213 1.1 cgd #if BSD < 198810
214 1.1 cgd if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
215 1.1 cgd n->rip_dst.sa_family =
216 1.1 cgd ntohs(n->rip_dst.sa_family);
217 1.1 cgd #else
218 1.1 cgd n->rip_dst.sa_family =
219 1.1 cgd ntohs(osa(n->rip_dst)->sa_family);
220 1.1 cgd n->rip_dst.sa_len = sizeof(n->rip_dst);
221 1.1 cgd #endif
222 1.1 cgd n->rip_metric = ntohl(n->rip_metric);
223 1.1 cgd if (n->rip_dst.sa_family >= af_max ||
224 1.1 cgd (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
225 1.1 cgd (int (*)())0) {
226 1.1 cgd syslog(LOG_INFO,
227 1.1 cgd "route in unsupported address family (%d), from %s (af %d)\n",
228 1.1 cgd n->rip_dst.sa_family,
229 1.1 cgd (*afswitch[from->sa_family].af_format)(from),
230 1.1 cgd from->sa_family);
231 1.1 cgd continue;
232 1.1 cgd }
233 1.1 cgd if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
234 1.1 cgd syslog(LOG_DEBUG,
235 1.1 cgd "bad host in route from %s (af %d)\n",
236 1.1 cgd (*afswitch[from->sa_family].af_format)(from),
237 1.1 cgd from->sa_family);
238 1.1 cgd continue;
239 1.1 cgd }
240 1.1 cgd if (n->rip_metric == 0 ||
241 1.1 cgd (unsigned) n->rip_metric > HOPCNT_INFINITY) {
242 1.7 mycroft if (memcmp(from, &badfrom2,
243 1.1 cgd sizeof(badfrom2)) != 0) {
244 1.1 cgd syslog(LOG_ERR,
245 1.1 cgd "bad metric (%d) from %s\n",
246 1.1 cgd n->rip_metric,
247 1.1 cgd (*afswitch[from->sa_family].af_format)(from));
248 1.1 cgd badfrom2 = *from;
249 1.1 cgd }
250 1.1 cgd continue;
251 1.1 cgd }
252 1.1 cgd /*
253 1.1 cgd * Adjust metric according to incoming interface.
254 1.1 cgd */
255 1.1 cgd if ((unsigned) n->rip_metric < HOPCNT_INFINITY)
256 1.1 cgd n->rip_metric += ifp->int_metric;
257 1.1 cgd if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
258 1.1 cgd n->rip_metric = HOPCNT_INFINITY;
259 1.1 cgd rt = rtlookup(&n->rip_dst);
260 1.1 cgd if (rt == 0 ||
261 1.1 cgd (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
262 1.1 cgd (RTS_INTERNAL|RTS_INTERFACE)) {
263 1.1 cgd /*
264 1.1 cgd * If we're hearing a logical network route
265 1.1 cgd * back from a peer to which we sent it,
266 1.1 cgd * ignore it.
267 1.1 cgd */
268 1.1 cgd if (rt && rt->rt_state & RTS_SUBNET &&
269 1.1 cgd (*afp->af_sendroute)(rt, from))
270 1.1 cgd continue;
271 1.1 cgd if ((unsigned)n->rip_metric < HOPCNT_INFINITY) {
272 1.1 cgd /*
273 1.1 cgd * Look for an equivalent route that
274 1.1 cgd * includes this one before adding
275 1.1 cgd * this route.
276 1.1 cgd */
277 1.1 cgd rt = rtfind(&n->rip_dst);
278 1.1 cgd if (rt && equal(from, &rt->rt_router))
279 1.1 cgd continue;
280 1.1 cgd rtadd(&n->rip_dst, from, n->rip_metric, 0);
281 1.1 cgd changes++;
282 1.1 cgd }
283 1.1 cgd continue;
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd /*
287 1.1 cgd * Update if from gateway and different,
288 1.1 cgd * shorter, or equivalent but old route
289 1.1 cgd * is getting stale.
290 1.1 cgd */
291 1.1 cgd if (equal(from, &rt->rt_router)) {
292 1.1 cgd if (n->rip_metric != rt->rt_metric) {
293 1.1 cgd rtchange(rt, from, n->rip_metric);
294 1.1 cgd changes++;
295 1.1 cgd rt->rt_timer = 0;
296 1.1 cgd if (rt->rt_metric >= HOPCNT_INFINITY)
297 1.1 cgd rt->rt_timer =
298 1.1 cgd GARBAGE_TIME - EXPIRE_TIME;
299 1.1 cgd } else if (rt->rt_metric < HOPCNT_INFINITY)
300 1.1 cgd rt->rt_timer = 0;
301 1.1 cgd } else if ((unsigned) n->rip_metric < rt->rt_metric ||
302 1.1 cgd (rt->rt_metric == n->rip_metric &&
303 1.1 cgd rt->rt_timer > (EXPIRE_TIME/2) &&
304 1.1 cgd (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
305 1.1 cgd rtchange(rt, from, n->rip_metric);
306 1.1 cgd changes++;
307 1.1 cgd rt->rt_timer = 0;
308 1.1 cgd }
309 1.1 cgd }
310 1.1 cgd break;
311 1.1 cgd }
312 1.1 cgd
313 1.1 cgd /*
314 1.1 cgd * If changes have occurred, and if we have not sent a broadcast
315 1.1 cgd * recently, send a dynamic update. This update is sent only
316 1.1 cgd * on interfaces other than the one on which we received notice
317 1.1 cgd * of the change. If we are within MIN_WAITTIME of a full update,
318 1.1 cgd * don't bother sending; if we just sent a dynamic update
319 1.1 cgd * and set a timer (nextbcast), delay until that time.
320 1.1 cgd * If we just sent a full update, delay the dynamic update.
321 1.1 cgd * Set a timer for a randomized value to suppress additional
322 1.1 cgd * dynamic updates until it expires; if we delayed sending
323 1.1 cgd * the current changes, set needupdate.
324 1.1 cgd */
325 1.1 cgd if (changes && supplier &&
326 1.1 cgd now.tv_sec - lastfullupdate.tv_sec < SUPPLY_INTERVAL-MAX_WAITTIME) {
327 1.1 cgd u_long delay;
328 1.1 cgd extern long random();
329 1.1 cgd
330 1.1 cgd if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME &&
331 1.1 cgd timercmp(&nextbcast, &now, <)) {
332 1.1 cgd if (traceactions)
333 1.1 cgd fprintf(ftrace, "send dynamic update\n");
334 1.1 cgd toall(supply, RTS_CHANGED, ifp);
335 1.1 cgd lastbcast = now;
336 1.1 cgd needupdate = 0;
337 1.1 cgd nextbcast.tv_sec = 0;
338 1.1 cgd } else {
339 1.1 cgd needupdate++;
340 1.1 cgd if (traceactions)
341 1.1 cgd fprintf(ftrace, "delay dynamic update\n");
342 1.1 cgd }
343 1.1 cgd #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
344 1.1 cgd (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
345 1.1 cgd
346 1.1 cgd if (nextbcast.tv_sec == 0) {
347 1.1 cgd delay = RANDOMDELAY();
348 1.1 cgd if (traceactions)
349 1.1 cgd fprintf(ftrace,
350 1.1 cgd "inhibit dynamic update for %d usec\n",
351 1.1 cgd delay);
352 1.1 cgd nextbcast.tv_sec = delay / 1000000;
353 1.1 cgd nextbcast.tv_usec = delay % 1000000;
354 1.10 mycroft timeradd(&nextbcast, &now, &nextbcast);
355 1.1 cgd /*
356 1.1 cgd * If the next possibly dynamic update
357 1.1 cgd * is within MIN_WAITTIME of the next full update,
358 1.1 cgd * force the delay past the full update,
359 1.1 cgd * or we might send a dynamic update just before
360 1.1 cgd * the full update.
361 1.1 cgd */
362 1.1 cgd if (nextbcast.tv_sec > lastfullupdate.tv_sec +
363 1.1 cgd SUPPLY_INTERVAL - MIN_WAITTIME)
364 1.1 cgd nextbcast.tv_sec = lastfullupdate.tv_sec +
365 1.1 cgd SUPPLY_INTERVAL + 1;
366 1.1 cgd }
367 1.1 cgd }
368 1.1 cgd }
369