input.c revision 1.12 1 /* $NetBSD: input.c,v 1.12 1995/05/24 15:22:55 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.12 1995/05/24 15:22:55 christos Exp $";
41 #endif
42 #endif /* not lint */
43
44 /*
45 * Routing Table Management Daemon
46 */
47 #include "defs.h"
48 #include <sys/syslog.h>
49
50 /*
51 * Process a newly received packet.
52 */
53 void
54 rip_input(from, rip, size)
55 struct sockaddr *from;
56 register struct rip *rip;
57 int size;
58 {
59 register struct rt_entry *rt;
60 register struct netinfo *n;
61 register struct interface *ifp;
62 struct interface *if_ifwithdstaddr();
63 int count, changes = 0;
64 register struct afswitch *afp;
65 static struct sockaddr badfrom, badfrom2;
66 char buf1[256], buf2[256];
67
68 ifp = 0;
69 TRACE_INPUT(ifp, from, (char *)rip, size);
70 if (from->sa_family >= af_max ||
71 (afp = &afswitch[from->sa_family])->af_hash == (int (*)())0) {
72 syslog(LOG_INFO,
73 "\"from\" address in unsupported address family (%d), cmd %d\n",
74 from->sa_family, rip->rip_cmd);
75 return;
76 }
77 if (rip->rip_vers == 0) {
78 syslog(LOG_ERR,
79 "RIP version 0 packet received from %s! (cmd %d)",
80 (*afswitch[from->sa_family].af_format)(from, buf1,
81 sizeof(buf1)),
82 rip->rip_cmd);
83 return;
84 }
85 switch (rip->rip_cmd) {
86
87 case RIPCMD_REQUEST:
88 n = rip->rip_nets;
89 count = size - ((char *)n - (char *)rip);
90 if (count < sizeof (struct netinfo))
91 return;
92 for (; count > 0; n++) {
93 if (count < sizeof (struct netinfo))
94 break;
95 count -= sizeof (struct netinfo);
96
97 #if BSD < 198810
98 if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
99 n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
100 #else
101 #define osa(x) ((struct osockaddr *)(&(x)))
102 n->rip_dst.sa_family =
103 ntohs(osa(n->rip_dst)->sa_family);
104 n->rip_dst.sa_len = sizeof(n->rip_dst);
105 #endif
106 n->rip_metric = ntohl(n->rip_metric);
107 /*
108 * A single entry with sa_family == AF_UNSPEC and
109 * metric ``infinity'' means ``all routes''.
110 * We respond to routers only if we are acting
111 * as a supplier, or to anyone other than a router
112 * (eg, query).
113 */
114 if (n->rip_dst.sa_family == AF_UNSPEC &&
115 n->rip_metric == HOPCNT_INFINITY && count == 0) {
116 if (supplier || (*afp->af_portmatch)(from) == 0)
117 supply(from, 0, 0, 0);
118 return;
119 }
120 if (n->rip_dst.sa_family < af_max &&
121 afswitch[n->rip_dst.sa_family].af_hash)
122 rt = rtlookup(&n->rip_dst);
123 else
124 rt = 0;
125 #define min(a, b) (a < b ? a : b)
126 n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
127 min(rt->rt_metric + 1, HOPCNT_INFINITY);
128 #if BSD < 198810
129 if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
130 n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
131 #else
132 osa(n->rip_dst)->sa_family =
133 htons(n->rip_dst.sa_family);
134 #endif
135 n->rip_metric = htonl(n->rip_metric);
136 }
137 rip->rip_cmd = RIPCMD_RESPONSE;
138 memcpy(packet, rip, size);
139 (*afp->af_output)(s, 0, from, size);
140 return;
141
142 case RIPCMD_TRACEON:
143 case RIPCMD_TRACEOFF:
144 /* verify message came from a privileged port */
145 if ((*afp->af_portcheck)(from) == 0)
146 return;
147 if ((ifp = if_iflookup(from)) == 0) {
148 syslog(LOG_ERR, "trace command from unknown router, %s",
149 (*afswitch[from->sa_family].af_format)(from, buf1,
150 sizeof(buf1)));
151 return;
152 }
153
154 if ((ifp->int_flags &
155 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_REMOTE)) == 0) {
156 syslog(LOG_ERR,
157 "trace command from router %s, with bad flags %x",
158 (*afswitch[from->sa_family].af_format)(from, buf1,
159 sizeof(buf1)),
160 ifp->int_flags);
161 return;
162 }
163
164 if ((ifp->int_flags & IFF_PASSIVE) != 0) {
165 syslog(LOG_ERR,
166 "trace command from %s on an active interface",
167 (*afswitch[from->sa_family].af_format)(from, buf1,
168 sizeof(buf1)));
169 return;
170 }
171
172 ((char *)rip)[size] = '\0';
173 if (rip->rip_cmd == RIPCMD_TRACEON)
174 traceon(rip->rip_tracefile);
175 else
176 traceoff();
177 return;
178
179 case RIPCMD_RESPONSE:
180 /* verify message came from a router */
181 if ((*afp->af_portmatch)(from) == 0)
182 return;
183 (*afp->af_canon)(from);
184 /* are we talking to ourselves? */
185 ifp = if_ifwithaddr(from);
186 if (ifp) {
187 if (ifp->int_flags & IFF_PASSIVE) {
188 syslog(LOG_ERR,
189 "bogus input (from passive interface, %s)",
190 (*afswitch[from->sa_family].af_format)(from,
191 buf1, sizeof(buf1)));
192 return;
193 }
194 rt = rtfind(from);
195 if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) &&
196 rt->rt_metric >= ifp->int_metric)
197 addrouteforif(ifp);
198 else
199 rt->rt_timer = 0;
200 return;
201 }
202 /*
203 * Update timer for interface on which the packet arrived.
204 * If from other end of a point-to-point link that isn't
205 * in the routing tables, (re-)add the route.
206 */
207 if ((rt = rtfind(from)) &&
208 (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
209 rt->rt_timer = 0;
210 else if ((ifp = if_ifwithdstaddr(from)) &&
211 (rt == 0 || rt->rt_metric >= ifp->int_metric))
212 addrouteforif(ifp);
213 /*
214 * "Authenticate" router from which message originated.
215 * We accept routing packets from routers directly connected
216 * via broadcast or point-to-point networks,
217 * and from those listed in /etc/gateways.
218 */
219 if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
220 (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
221 ifp->int_flags & IFF_PASSIVE) {
222 if (memcmp(from, &badfrom, sizeof(badfrom)) != 0) {
223 syslog(LOG_ERR,
224 "packet from unknown router, %s",
225 (*afswitch[from->sa_family].af_format)(from,
226 buf1, sizeof(buf1)));
227 badfrom = *from;
228 }
229 return;
230 }
231 size -= 4 * sizeof (char);
232 n = rip->rip_nets;
233 for (; size > 0; size -= sizeof (struct netinfo), n++) {
234 if (size < sizeof (struct netinfo))
235 break;
236 #if BSD < 198810
237 if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
238 n->rip_dst.sa_family =
239 ntohs(n->rip_dst.sa_family);
240 #else
241 n->rip_dst.sa_family =
242 ntohs(osa(n->rip_dst)->sa_family);
243 n->rip_dst.sa_len = sizeof(n->rip_dst);
244 #endif
245 n->rip_metric = ntohl(n->rip_metric);
246 if (n->rip_dst.sa_family >= af_max ||
247 (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
248 (int (*)())0) {
249 syslog(LOG_INFO,
250 "route in unsupported address family (%d), from %s (af %d)\n",
251 n->rip_dst.sa_family,
252 (*afswitch[from->sa_family].af_format)(from,
253 buf1, sizeof(buf1)),
254 from->sa_family);
255 continue;
256 }
257 if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
258 syslog(LOG_DEBUG,
259 "bad host %s in route from %s (af %d)\n",
260 (*afswitch[n->rip_dst.sa_family].af_format)(
261 &n->rip_dst, buf1, sizeof(buf1)),
262 (*afswitch[from->sa_family].af_format)(from,
263 buf2, sizeof(buf2)),
264 from->sa_family);
265 continue;
266 }
267 if (n->rip_metric == 0 ||
268 (unsigned) n->rip_metric > HOPCNT_INFINITY) {
269 if (memcmp(from, &badfrom2,
270 sizeof(badfrom2)) != 0) {
271 syslog(LOG_ERR,
272 "bad metric (%d) from %s\n",
273 n->rip_metric,
274 (*afswitch[from->sa_family].af_format)(from,
275 buf1, sizeof(buf1)));
276 badfrom2 = *from;
277 }
278 continue;
279 }
280 /*
281 * Adjust metric according to incoming interface.
282 */
283 if ((unsigned) n->rip_metric < HOPCNT_INFINITY)
284 n->rip_metric += ifp->int_metric;
285 if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
286 n->rip_metric = HOPCNT_INFINITY;
287 rt = rtlookup(&n->rip_dst);
288 if (rt == 0 ||
289 (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
290 (RTS_INTERNAL|RTS_INTERFACE)) {
291 /*
292 * If we're hearing a logical network route
293 * back from a peer to which we sent it,
294 * ignore it.
295 */
296 if (rt && rt->rt_state & RTS_SUBNET &&
297 (*afp->af_sendroute)(rt, from))
298 continue;
299 if ((unsigned)n->rip_metric < HOPCNT_INFINITY) {
300 /*
301 * Look for an equivalent route that
302 * includes this one before adding
303 * this route.
304 */
305 rt = rtfind(&n->rip_dst);
306 if (rt && equal(from, &rt->rt_router))
307 continue;
308 rtadd(&n->rip_dst, from, n->rip_metric, 0);
309 changes++;
310 }
311 continue;
312 }
313
314 /*
315 * Update if from gateway and different,
316 * shorter, or equivalent but old route
317 * is getting stale.
318 */
319 if (equal(from, &rt->rt_router)) {
320 if (n->rip_metric != rt->rt_metric) {
321 rtchange(rt, from, 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, from, 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 extern long random();
357
358 if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME &&
359 timercmp(&nextbcast, &now, <)) {
360 if (traceactions)
361 fprintf(ftrace, "send dynamic update\n");
362 toall(supply, RTS_CHANGED, ifp);
363 lastbcast = now;
364 needupdate = 0;
365 nextbcast.tv_sec = 0;
366 } else {
367 needupdate++;
368 if (traceactions)
369 fprintf(ftrace, "delay dynamic update\n");
370 }
371 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
372 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
373
374 if (nextbcast.tv_sec == 0) {
375 delay = RANDOMDELAY();
376 if (traceactions)
377 fprintf(ftrace,
378 "inhibit dynamic update for %d usec\n",
379 delay);
380 nextbcast.tv_sec = delay / 1000000;
381 nextbcast.tv_usec = delay % 1000000;
382 timeradd(&nextbcast, &now, &nextbcast);
383 /*
384 * If the next possibly dynamic update
385 * is within MIN_WAITTIME of the next full update,
386 * force the delay past the full update,
387 * or we might send a dynamic update just before
388 * the full update.
389 */
390 if (nextbcast.tv_sec > lastfullupdate.tv_sec +
391 SUPPLY_INTERVAL - MIN_WAITTIME)
392 nextbcast.tv_sec = lastfullupdate.tv_sec +
393 SUPPLY_INTERVAL + 1;
394 }
395 }
396 }
397