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