]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/input.c
e6d2626264f51390b616d022f61babdb6fbbdb8a
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
56 * @(#)defs.h 8.1 (Berkeley) 6/5/93
61 * Routing Table Management Daemon
64 #include <sys/syslog.h>
67 * Process a newly received packet.
70 rip_input(from
, rip
, size
)
71 struct sockaddr
*from
;
72 register struct rip
*rip
;
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
;
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) {
88 "\"from\" address in unsupported address family (%d), cmd %d\n",
89 from
->sa_family
, rip
->rip_cmd
);
92 if (rip
->rip_vers
== 0) {
94 "RIP version 0 packet received from %s! (cmd %d)",
95 (*afswitch
[from
->sa_family
].af_format
)(from
), rip
->rip_cmd
);
98 switch (rip
->rip_cmd
) {
102 count
= size
- ((char *)n
- (char *)rip
);
103 if (count
< sizeof (struct netinfo
))
105 for (; count
> 0; n
++) {
106 if (count
< sizeof (struct netinfo
))
108 count
-= sizeof (struct netinfo
);
109 n
->rip_metric
= ntohl(n
->rip_metric
);
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
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);
123 if (n
->rip_dst
.sa_family
< af_max
&&
124 afswitch
[n
->rip_dst
.sa_family
].af_hash
)
125 rt
= rtlookup(&n
->rip_dst
);
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
);
131 n
->rip_metric
= htonl(n
->rip_metric
);
133 rip
->rip_cmd
= RIPCMD_RESPONSE
;
134 memmove(packet
, rip
, size
);
135 (*afp
->af_output
)(s
, 0, from
, size
);
139 case RIPCMD_TRACEOFF
:
140 /* verify message came from a privileged port */
141 if ((*afp
->af_portcheck
)(from
) == 0)
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
));
150 ((char *)rip
)[size
] = '\0';
151 if (rip
->rip_cmd
== RIPCMD_TRACEON
)
152 traceon(rip
->rip_tracefile
);
157 case RIPCMD_RESPONSE
:
158 /* verify message came from a router */
159 if ((*afp
->af_portmatch
)(from
) == 0)
161 (*afp
->af_canon
)(from
);
162 /* are we talking to ourselves? */
163 ifp
= if_ifwithaddr(from
);
165 if (ifp
->int_flags
& IFF_PASSIVE
) {
167 "bogus input (from passive interface, %s)",
168 (*afswitch
[from
->sa_family
].af_format
)(from
));
172 if (rt
== 0 || ((rt
->rt_state
& RTS_INTERFACE
) == 0) &&
173 rt
->rt_metric
>= ifp
->int_metric
)
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.
184 if ((rt
= rtfind(from
)) &&
185 (rt
->rt_state
& (RTS_INTERFACE
| RTS_REMOTE
)))
187 else if ((ifp
= if_ifwithdstaddr(from
)) &&
188 (rt
== 0 || rt
->rt_metric
>= ifp
->int_metric
))
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.
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) {
201 "packet from unknown router, %s",
202 (*afswitch
[from
->sa_family
].af_format
)(from
));
207 size
-= 4 * sizeof (char);
209 for (; size
> 0; size
-= sizeof (struct netinfo
), n
++) {
210 if (size
< sizeof (struct netinfo
))
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
==
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
),
223 if (((*afp
->af_checkhost
)(&n
->rip_dst
)) == 0) {
225 "bad host in route from %s (af %d)\n",
226 (*afswitch
[from
->sa_family
].af_format
)(from
),
230 if (n
->rip_metric
== 0 ||
231 (unsigned) n
->rip_metric
> HOPCNT_INFINITY
) {
232 if (memcmp(from
, &badfrom2
,
233 sizeof(badfrom2
)) != 0) {
235 "bad metric (%d) from %s\n",
237 (*afswitch
[from
->sa_family
].af_format
)(from
));
243 * Adjust metric according to incoming interface.
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
);
251 (rt
->rt_state
& (RTS_INTERNAL
|RTS_INTERFACE
)) ==
252 (RTS_INTERNAL
|RTS_INTERFACE
)) {
254 * If we're hearing a logical network route
255 * back from a peer to which we sent it,
258 if (rt
&& rt
->rt_state
& RTS_SUBNET
&&
259 (*afp
->af_sendroute
)(rt
, from
))
261 if ((unsigned)n
->rip_metric
< HOPCNT_INFINITY
) {
263 * Look for an equivalent route that
264 * includes this one before adding
267 rt
= rtfind(&n
->rip_dst
);
268 if (rt
&& equal(from
, &rt
->rt_router
))
270 rtadd(&n
->rip_dst
, from
, n
->rip_metric
, 0);
277 * Update if from gateway and different,
278 * shorter, or equivalent but old route
281 if (equal(from
, &rt
->rt_router
)) {
282 if (n
->rip_metric
!= rt
->rt_metric
) {
283 rtchange(rt
, from
, n
->rip_metric
);
286 if (rt
->rt_metric
>= HOPCNT_INFINITY
)
288 GARBAGE_TIME
- EXPIRE_TIME
;
289 } else if (rt
->rt_metric
< HOPCNT_INFINITY
)
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
);
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.
315 if (changes
&& supplier
&&
316 now
.tv_sec
- lastfullupdate
.tv_sec
< SUPPLY_INTERVAL
-MAX_WAITTIME
) {
318 extern long random();
320 if (now
.tv_sec
- lastbcast
.tv_sec
>= MIN_WAITTIME
&&
321 timercmp(&nextbcast
, &now
, <)) {
323 fprintf(ftrace
, "send dynamic update\n");
324 toall((int (*)())supply
, RTS_CHANGED
, ifp
);
327 nextbcast
.tv_sec
= 0;
331 fprintf(ftrace
, "delay dynamic update\n");
333 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
334 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
336 if (nextbcast
.tv_sec
== 0) {
337 delay
= RANDOMDELAY();
340 "inhibit dynamic update for %lu usec\n",
342 nextbcast
.tv_sec
= delay
/ 1000000;
343 nextbcast
.tv_usec
= delay
% 1000000;
344 timevaladd(&nextbcast
, &now
);
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
352 if (nextbcast
.tv_sec
> lastfullupdate
.tv_sec
+
353 SUPPLY_INTERVAL
- MIN_WAITTIME
)
354 nextbcast
.tv_sec
= lastfullupdate
.tv_sec
+