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