]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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,
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
57 * @(#)defs.h 8.1 (Berkeley) 6/5/93
62 * Routing Table Management Daemon
65 #include <sys/syslog.h>
68 * 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
);
111 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
112 n
->rip_dst
.sa_family
= ntohs(n
->rip_dst
.sa_family
);
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
);
119 n
->rip_metric
= ntohl(n
->rip_metric
);
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
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);
133 if (n
->rip_dst
.sa_family
< af_max
&&
134 afswitch
[n
->rip_dst
.sa_family
].af_hash
)
135 rt
= rtlookup(&n
->rip_dst
);
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
);
142 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
143 n
->rip_dst
.sa_family
= htons(n
->rip_dst
.sa_family
);
145 osa(n
->rip_dst
)->sa_family
=
146 htons(n
->rip_dst
.sa_family
);
148 n
->rip_metric
= htonl(n
->rip_metric
);
150 rip
->rip_cmd
= RIPCMD_RESPONSE
;
151 memmove(packet
, rip
, size
);
152 (*afp
->af_output
)(s
, 0, from
, size
);
156 case RIPCMD_TRACEOFF
:
157 /* verify message came from a privileged port */
158 if ((*afp
->af_portcheck
)(from
) == 0)
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
));
167 ((char *)rip
)[size
] = '\0';
168 if (rip
->rip_cmd
== RIPCMD_TRACEON
)
169 traceon(rip
->rip_tracefile
);
174 case RIPCMD_RESPONSE
:
175 /* verify message came from a router */
176 if ((*afp
->af_portmatch
)(from
) == 0)
178 (*afp
->af_canon
)(from
);
179 /* are we talking to ourselves? */
180 ifp
= if_ifwithaddr(from
);
182 if (ifp
->int_flags
& IFF_PASSIVE
) {
184 "bogus input (from passive interface, %s)",
185 (*afswitch
[from
->sa_family
].af_format
)(from
));
189 if (rt
== 0 || ((rt
->rt_state
& RTS_INTERFACE
) == 0) &&
190 rt
->rt_metric
>= ifp
->int_metric
)
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.
201 if ((rt
= rtfind(from
)) &&
202 (rt
->rt_state
& (RTS_INTERFACE
| RTS_REMOTE
)))
204 else if ((ifp
= if_ifwithdstaddr(from
)) &&
205 (rt
== 0 || rt
->rt_metric
>= ifp
->int_metric
))
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.
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) {
218 "packet from unknown router, %s",
219 (*afswitch
[from
->sa_family
].af_format
)(from
));
224 size
-= 4 * sizeof (char);
226 for (; size
> 0; size
-= sizeof (struct netinfo
), n
++) {
227 if (size
< sizeof (struct netinfo
))
230 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
231 n
->rip_dst
.sa_family
=
232 ntohs(n
->rip_dst
.sa_family
);
234 n
->rip_dst
.sa_family
=
235 ntohs(osa(n
->rip_dst
)->sa_family
);
236 n
->rip_dst
.sa_len
= sizeof(n
->rip_dst
);
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
==
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
),
249 if (((*afp
->af_checkhost
)(&n
->rip_dst
)) == 0) {
251 "bad host in route from %s (af %d)\n",
252 (*afswitch
[from
->sa_family
].af_format
)(from
),
256 if (n
->rip_metric
== 0 ||
257 (unsigned) n
->rip_metric
> HOPCNT_INFINITY
) {
258 if (memcmp(from
, &badfrom2
,
259 sizeof(badfrom2
)) != 0) {
261 "bad metric (%d) from %s\n",
263 (*afswitch
[from
->sa_family
].af_format
)(from
));
269 * Adjust metric according to incoming interface.
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
);
277 (rt
->rt_state
& (RTS_INTERNAL
|RTS_INTERFACE
)) ==
278 (RTS_INTERNAL
|RTS_INTERFACE
)) {
280 * If we're hearing a logical network route
281 * back from a peer to which we sent it,
284 if (rt
&& rt
->rt_state
& RTS_SUBNET
&&
285 (*afp
->af_sendroute
)(rt
, from
))
287 if ((unsigned)n
->rip_metric
< HOPCNT_INFINITY
) {
289 * Look for an equivalent route that
290 * includes this one before adding
293 rt
= rtfind(&n
->rip_dst
);
294 if (rt
&& equal(from
, &rt
->rt_router
))
296 rtadd(&n
->rip_dst
, from
, n
->rip_metric
, 0);
303 * Update if from gateway and different,
304 * shorter, or equivalent but old route
307 if (equal(from
, &rt
->rt_router
)) {
308 if (n
->rip_metric
!= rt
->rt_metric
) {
309 rtchange(rt
, from
, n
->rip_metric
);
312 if (rt
->rt_metric
>= HOPCNT_INFINITY
)
314 GARBAGE_TIME
- EXPIRE_TIME
;
315 } else if (rt
->rt_metric
< HOPCNT_INFINITY
)
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
);
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.
341 if (changes
&& supplier
&&
342 now
.tv_sec
- lastfullupdate
.tv_sec
< SUPPLY_INTERVAL
-MAX_WAITTIME
) {
344 extern long random();
346 if (now
.tv_sec
- lastbcast
.tv_sec
>= MIN_WAITTIME
&&
347 timercmp(&nextbcast
, &now
, <)) {
349 fprintf(ftrace
, "send dynamic update\n");
350 toall(supply
, RTS_CHANGED
, ifp
);
353 nextbcast
.tv_sec
= 0;
357 fprintf(ftrace
, "delay dynamic update\n");
359 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
360 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
362 if (nextbcast
.tv_sec
== 0) {
363 delay
= RANDOMDELAY();
366 "inhibit dynamic update for %d usec\n",
368 nextbcast
.tv_sec
= delay
/ 1000000;
369 nextbcast
.tv_usec
= delay
% 1000000;
370 timevaladd(&nextbcast
, &now
);
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
378 if (nextbcast
.tv_sec
> lastfullupdate
.tv_sec
+
379 SUPPLY_INTERVAL
- MIN_WAITTIME
)
380 nextbcast
.tv_sec
= lastfullupdate
.tv_sec
+