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