]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/input.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1983, 1988, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
55 * @(#)defs.h 8.1 (Berkeley) 6/5/93
60 * Routing Table Management Daemon
63 #include <sys/syslog.h>
66 * Process a newly received packet.
68 rip_input(from
, rip
, size
)
69 struct sockaddr
*from
;
70 register struct rip
*rip
;
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
;
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) {
86 "\"from\" address in unsupported address family (%d), cmd %d\n",
87 from
->sa_family
, rip
->rip_cmd
);
90 if (rip
->rip_vers
== 0) {
92 "RIP version 0 packet received from %s! (cmd %d)",
93 (*afswitch
[from
->sa_family
].af_format
)(from
), rip
->rip_cmd
);
96 switch (rip
->rip_cmd
) {
100 count
= size
- ((char *)n
- (char *)rip
);
101 if (count
< sizeof (struct netinfo
))
103 for (; count
> 0; n
++) {
104 if (count
< sizeof (struct netinfo
))
106 count
-= sizeof (struct netinfo
);
109 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
110 n
->rip_dst
.sa_family
= ntohs(n
->rip_dst
.sa_family
);
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
);
117 n
->rip_metric
= ntohl(n
->rip_metric
);
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
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);
131 if (n
->rip_dst
.sa_family
< af_max
&&
132 afswitch
[n
->rip_dst
.sa_family
].af_hash
)
133 rt
= rtlookup(&n
->rip_dst
);
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
);
140 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
141 n
->rip_dst
.sa_family
= htons(n
->rip_dst
.sa_family
);
143 osa(n
->rip_dst
)->sa_family
=
144 htons(n
->rip_dst
.sa_family
);
146 n
->rip_metric
= htonl(n
->rip_metric
);
148 rip
->rip_cmd
= RIPCMD_RESPONSE
;
149 memmove(packet
, rip
, size
);
150 (*afp
->af_output
)(s
, 0, from
, size
);
154 case RIPCMD_TRACEOFF
:
155 /* verify message came from a privileged port */
156 if ((*afp
->af_portcheck
)(from
) == 0)
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
));
165 ((char *)rip
)[size
] = '\0';
166 if (rip
->rip_cmd
== RIPCMD_TRACEON
)
167 traceon(rip
->rip_tracefile
);
172 case RIPCMD_RESPONSE
:
173 /* verify message came from a router */
174 if ((*afp
->af_portmatch
)(from
) == 0)
176 (*afp
->af_canon
)(from
);
177 /* are we talking to ourselves? */
178 ifp
= if_ifwithaddr(from
);
180 if (ifp
->int_flags
& IFF_PASSIVE
) {
182 "bogus input (from passive interface, %s)",
183 (*afswitch
[from
->sa_family
].af_format
)(from
));
187 if (rt
== 0 || ((rt
->rt_state
& RTS_INTERFACE
) == 0) &&
188 rt
->rt_metric
>= ifp
->int_metric
)
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.
199 if ((rt
= rtfind(from
)) &&
200 (rt
->rt_state
& (RTS_INTERFACE
| RTS_REMOTE
)))
202 else if ((ifp
= if_ifwithdstaddr(from
)) &&
203 (rt
== 0 || rt
->rt_metric
>= ifp
->int_metric
))
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.
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) {
216 "packet from unknown router, %s",
217 (*afswitch
[from
->sa_family
].af_format
)(from
));
222 size
-= 4 * sizeof (char);
224 for (; size
> 0; size
-= sizeof (struct netinfo
), n
++) {
225 if (size
< sizeof (struct netinfo
))
228 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
229 n
->rip_dst
.sa_family
=
230 ntohs(n
->rip_dst
.sa_family
);
232 n
->rip_dst
.sa_family
=
233 ntohs(osa(n
->rip_dst
)->sa_family
);
234 n
->rip_dst
.sa_len
= sizeof(n
->rip_dst
);
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
==
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
),
247 if (((*afp
->af_checkhost
)(&n
->rip_dst
)) == 0) {
249 "bad host in route from %s (af %d)\n",
250 (*afswitch
[from
->sa_family
].af_format
)(from
),
254 if (n
->rip_metric
== 0 ||
255 (unsigned) n
->rip_metric
> HOPCNT_INFINITY
) {
256 if (memcmp(from
, &badfrom2
,
257 sizeof(badfrom2
)) != 0) {
259 "bad metric (%d) from %s\n",
261 (*afswitch
[from
->sa_family
].af_format
)(from
));
267 * Adjust metric according to incoming interface.
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
);
275 (rt
->rt_state
& (RTS_INTERNAL
|RTS_INTERFACE
)) ==
276 (RTS_INTERNAL
|RTS_INTERFACE
)) {
278 * If we're hearing a logical network route
279 * back from a peer to which we sent it,
282 if (rt
&& rt
->rt_state
& RTS_SUBNET
&&
283 (*afp
->af_sendroute
)(rt
, from
))
285 if ((unsigned)n
->rip_metric
< HOPCNT_INFINITY
) {
287 * Look for an equivalent route that
288 * includes this one before adding
291 rt
= rtfind(&n
->rip_dst
);
292 if (rt
&& equal(from
, &rt
->rt_router
))
294 rtadd(&n
->rip_dst
, from
, n
->rip_metric
, 0);
301 * Update if from gateway and different,
302 * shorter, or equivalent but old route
305 if (equal(from
, &rt
->rt_router
)) {
306 if (n
->rip_metric
!= rt
->rt_metric
) {
307 rtchange(rt
, from
, n
->rip_metric
);
310 if (rt
->rt_metric
>= HOPCNT_INFINITY
)
312 GARBAGE_TIME
- EXPIRE_TIME
;
313 } else if (rt
->rt_metric
< HOPCNT_INFINITY
)
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
);
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.
339 if (changes
&& supplier
&&
340 now
.tv_sec
- lastfullupdate
.tv_sec
< SUPPLY_INTERVAL
-MAX_WAITTIME
) {
342 extern long random();
344 if (now
.tv_sec
- lastbcast
.tv_sec
>= MIN_WAITTIME
&&
345 timercmp(&nextbcast
, &now
, <)) {
347 fprintf(ftrace
, "send dynamic update\n");
348 toall(supply
, RTS_CHANGED
, ifp
);
351 nextbcast
.tv_sec
= 0;
355 fprintf(ftrace
, "delay dynamic update\n");
357 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
358 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
360 if (nextbcast
.tv_sec
== 0) {
361 delay
= RANDOMDELAY();
364 "inhibit dynamic update for %d usec\n",
366 nextbcast
.tv_sec
= delay
/ 1000000;
367 nextbcast
.tv_usec
= delay
% 1000000;
368 timevaladd(&nextbcast
, &now
);
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
376 if (nextbcast
.tv_sec
> lastfullupdate
.tv_sec
+
377 SUPPLY_INTERVAL
- MIN_WAITTIME
)
378 nextbcast
.tv_sec
= lastfullupdate
.tv_sec
+