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