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