]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
a2c93a76 A |
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. | |
b7080c8e A |
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, | |
a2c93a76 A |
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." | |
b7080c8e A |
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 | */ | |
b8dff150 | 69 | void |
b7080c8e A |
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); | |
b7080c8e A |
109 | n->rip_metric = ntohl(n->rip_metric); |
110 | /* | |
111 | * A single entry with sa_family == AF_UNSPEC and | |
112 | * metric ``infinity'' means ``all routes''. | |
113 | * We respond to routers only if we are acting | |
114 | * as a supplier, or to anyone other than a router | |
115 | * (eg, query). | |
116 | */ | |
117 | if (n->rip_dst.sa_family == AF_UNSPEC && | |
118 | n->rip_metric == HOPCNT_INFINITY && count == 0) { | |
119 | if (supplier || (*afp->af_portmatch)(from) == 0) | |
120 | supply(from, 0, 0, 0); | |
121 | return; | |
122 | } | |
123 | if (n->rip_dst.sa_family < af_max && | |
124 | afswitch[n->rip_dst.sa_family].af_hash) | |
125 | rt = rtlookup(&n->rip_dst); | |
126 | else | |
127 | rt = 0; | |
128 | #define min(a, b) (a < b ? a : b) | |
129 | n->rip_metric = rt == 0 ? HOPCNT_INFINITY : | |
130 | min(rt->rt_metric + 1, HOPCNT_INFINITY); | |
b7080c8e A |
131 | n->rip_metric = htonl(n->rip_metric); |
132 | } | |
133 | rip->rip_cmd = RIPCMD_RESPONSE; | |
134 | memmove(packet, rip, size); | |
135 | (*afp->af_output)(s, 0, from, size); | |
136 | return; | |
137 | ||
138 | case RIPCMD_TRACEON: | |
139 | case RIPCMD_TRACEOFF: | |
140 | /* verify message came from a privileged port */ | |
141 | if ((*afp->af_portcheck)(from) == 0) | |
142 | return; | |
143 | if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags & | |
144 | (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 || | |
145 | ifp->int_flags & IFF_PASSIVE) { | |
146 | syslog(LOG_ERR, "trace command from unknown router, %s", | |
147 | (*afswitch[from->sa_family].af_format)(from)); | |
148 | return; | |
149 | } | |
150 | ((char *)rip)[size] = '\0'; | |
151 | if (rip->rip_cmd == RIPCMD_TRACEON) | |
152 | traceon(rip->rip_tracefile); | |
153 | else | |
154 | traceoff(); | |
155 | return; | |
156 | ||
157 | case RIPCMD_RESPONSE: | |
158 | /* verify message came from a router */ | |
159 | if ((*afp->af_portmatch)(from) == 0) | |
160 | return; | |
161 | (*afp->af_canon)(from); | |
162 | /* are we talking to ourselves? */ | |
163 | ifp = if_ifwithaddr(from); | |
164 | if (ifp) { | |
165 | if (ifp->int_flags & IFF_PASSIVE) { | |
166 | syslog(LOG_ERR, | |
167 | "bogus input (from passive interface, %s)", | |
168 | (*afswitch[from->sa_family].af_format)(from)); | |
169 | return; | |
170 | } | |
171 | rt = rtfind(from); | |
172 | if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) && | |
173 | rt->rt_metric >= ifp->int_metric) | |
174 | addrouteforif(ifp); | |
175 | else | |
176 | rt->rt_timer = 0; | |
177 | return; | |
178 | } | |
179 | /* | |
180 | * Update timer for interface on which the packet arrived. | |
181 | * If from other end of a point-to-point link that isn't | |
182 | * in the routing tables, (re-)add the route. | |
183 | */ | |
184 | if ((rt = rtfind(from)) && | |
185 | (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE))) | |
186 | rt->rt_timer = 0; | |
187 | else if ((ifp = if_ifwithdstaddr(from)) && | |
188 | (rt == 0 || rt->rt_metric >= ifp->int_metric)) | |
189 | addrouteforif(ifp); | |
190 | /* | |
191 | * "Authenticate" router from which message originated. | |
192 | * We accept routing packets from routers directly connected | |
193 | * via broadcast or point-to-point networks, | |
194 | * and from those listed in /etc/gateways. | |
195 | */ | |
196 | if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags & | |
197 | (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 || | |
198 | ifp->int_flags & IFF_PASSIVE) { | |
199 | if (memcmp(from, &badfrom, sizeof(badfrom)) != 0) { | |
200 | syslog(LOG_ERR, | |
201 | "packet from unknown router, %s", | |
202 | (*afswitch[from->sa_family].af_format)(from)); | |
203 | badfrom = *from; | |
204 | } | |
205 | return; | |
206 | } | |
207 | size -= 4 * sizeof (char); | |
208 | n = rip->rip_nets; | |
209 | for (; size > 0; size -= sizeof (struct netinfo), n++) { | |
210 | if (size < sizeof (struct netinfo)) | |
211 | break; | |
b7080c8e A |
212 | n->rip_metric = ntohl(n->rip_metric); |
213 | if (n->rip_dst.sa_family >= af_max || | |
214 | (afp = &afswitch[n->rip_dst.sa_family])->af_hash == | |
215 | (int (*)())0) { | |
216 | syslog(LOG_INFO, | |
217 | "route in unsupported address family (%d), from %s (af %d)\n", | |
218 | n->rip_dst.sa_family, | |
219 | (*afswitch[from->sa_family].af_format)(from), | |
220 | from->sa_family); | |
221 | continue; | |
222 | } | |
223 | if (((*afp->af_checkhost)(&n->rip_dst)) == 0) { | |
224 | syslog(LOG_DEBUG, | |
225 | "bad host in route from %s (af %d)\n", | |
226 | (*afswitch[from->sa_family].af_format)(from), | |
227 | from->sa_family); | |
228 | continue; | |
229 | } | |
230 | if (n->rip_metric == 0 || | |
231 | (unsigned) n->rip_metric > HOPCNT_INFINITY) { | |
232 | if (memcmp(from, &badfrom2, | |
233 | sizeof(badfrom2)) != 0) { | |
234 | syslog(LOG_ERR, | |
235 | "bad metric (%d) from %s\n", | |
236 | n->rip_metric, | |
237 | (*afswitch[from->sa_family].af_format)(from)); | |
238 | badfrom2 = *from; | |
239 | } | |
240 | continue; | |
241 | } | |
242 | /* | |
243 | * Adjust metric according to incoming interface. | |
244 | */ | |
245 | if ((unsigned) n->rip_metric < HOPCNT_INFINITY) | |
246 | n->rip_metric += ifp->int_metric; | |
247 | if ((unsigned) n->rip_metric > HOPCNT_INFINITY) | |
248 | n->rip_metric = HOPCNT_INFINITY; | |
249 | rt = rtlookup(&n->rip_dst); | |
250 | if (rt == 0 || | |
251 | (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) == | |
252 | (RTS_INTERNAL|RTS_INTERFACE)) { | |
253 | /* | |
254 | * If we're hearing a logical network route | |
255 | * back from a peer to which we sent it, | |
256 | * ignore it. | |
257 | */ | |
258 | if (rt && rt->rt_state & RTS_SUBNET && | |
259 | (*afp->af_sendroute)(rt, from)) | |
260 | continue; | |
261 | if ((unsigned)n->rip_metric < HOPCNT_INFINITY) { | |
262 | /* | |
263 | * Look for an equivalent route that | |
264 | * includes this one before adding | |
265 | * this route. | |
266 | */ | |
267 | rt = rtfind(&n->rip_dst); | |
268 | if (rt && equal(from, &rt->rt_router)) | |
269 | continue; | |
270 | rtadd(&n->rip_dst, from, n->rip_metric, 0); | |
271 | changes++; | |
272 | } | |
273 | continue; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Update if from gateway and different, | |
278 | * shorter, or equivalent but old route | |
279 | * is getting stale. | |
280 | */ | |
281 | if (equal(from, &rt->rt_router)) { | |
282 | if (n->rip_metric != rt->rt_metric) { | |
283 | rtchange(rt, from, n->rip_metric); | |
284 | changes++; | |
285 | rt->rt_timer = 0; | |
286 | if (rt->rt_metric >= HOPCNT_INFINITY) | |
287 | rt->rt_timer = | |
288 | GARBAGE_TIME - EXPIRE_TIME; | |
289 | } else if (rt->rt_metric < HOPCNT_INFINITY) | |
290 | rt->rt_timer = 0; | |
291 | } else if ((unsigned) n->rip_metric < rt->rt_metric || | |
292 | (rt->rt_metric == n->rip_metric && | |
293 | rt->rt_timer > (EXPIRE_TIME/2) && | |
294 | (unsigned) n->rip_metric < HOPCNT_INFINITY)) { | |
295 | rtchange(rt, from, n->rip_metric); | |
296 | changes++; | |
297 | rt->rt_timer = 0; | |
298 | } | |
299 | } | |
300 | break; | |
301 | } | |
302 | ||
303 | /* | |
304 | * If changes have occurred, and if we have not sent a broadcast | |
305 | * recently, send a dynamic update. This update is sent only | |
306 | * on interfaces other than the one on which we received notice | |
307 | * of the change. If we are within MIN_WAITTIME of a full update, | |
308 | * don't bother sending; if we just sent a dynamic update | |
309 | * and set a timer (nextbcast), delay until that time. | |
310 | * If we just sent a full update, delay the dynamic update. | |
311 | * Set a timer for a randomized value to suppress additional | |
312 | * dynamic updates until it expires; if we delayed sending | |
313 | * the current changes, set needupdate. | |
314 | */ | |
315 | if (changes && supplier && | |
316 | now.tv_sec - lastfullupdate.tv_sec < SUPPLY_INTERVAL-MAX_WAITTIME) { | |
317 | u_long delay; | |
318 | extern long random(); | |
319 | ||
320 | if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME && | |
321 | timercmp(&nextbcast, &now, <)) { | |
322 | if (traceactions) | |
323 | fprintf(ftrace, "send dynamic update\n"); | |
b8dff150 | 324 | toall((int (*)())supply, RTS_CHANGED, ifp); |
b7080c8e A |
325 | lastbcast = now; |
326 | needupdate = 0; | |
327 | nextbcast.tv_sec = 0; | |
328 | } else { | |
329 | needupdate++; | |
330 | if (traceactions) | |
331 | fprintf(ftrace, "delay dynamic update\n"); | |
332 | } | |
333 | #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \ | |
334 | (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000)) | |
335 | ||
336 | if (nextbcast.tv_sec == 0) { | |
337 | delay = RANDOMDELAY(); | |
338 | if (traceactions) | |
339 | fprintf(ftrace, | |
b8dff150 | 340 | "inhibit dynamic update for %lu usec\n", |
b7080c8e A |
341 | delay); |
342 | nextbcast.tv_sec = delay / 1000000; | |
343 | nextbcast.tv_usec = delay % 1000000; | |
344 | timevaladd(&nextbcast, &now); | |
345 | /* | |
346 | * If the next possibly dynamic update | |
347 | * is within MIN_WAITTIME of the next full update, | |
348 | * force the delay past the full update, | |
349 | * or we might send a dynamic update just before | |
350 | * the full update. | |
351 | */ | |
352 | if (nextbcast.tv_sec > lastfullupdate.tv_sec + | |
353 | SUPPLY_INTERVAL - MIN_WAITTIME) | |
354 | nextbcast.tv_sec = lastfullupdate.tv_sec + | |
355 | SUPPLY_INTERVAL + 1; | |
356 | } | |
357 | } | |
358 | } |