]>
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 * "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.
69 rip_input(from
, rip
, size
)
70 struct sockaddr
*from
;
71 register struct rip
*rip
;
74 register struct rt_entry
*rt
;
75 register struct netinfo
*n
;
76 register struct interface
*ifp
;
77 struct interface
*if_ifwithdstaddr();
78 int count
, changes
= 0;
79 register struct afswitch
*afp
;
80 static struct sockaddr badfrom
, badfrom2
;
83 TRACE_INPUT(ifp
, from
, (char *)rip
, size
);
84 if (from
->sa_family
>= af_max
||
85 (afp
= &afswitch
[from
->sa_family
])->af_hash
== (int (*)())0) {
87 "\"from\" address in unsupported address family (%d), cmd %d\n",
88 from
->sa_family
, rip
->rip_cmd
);
91 if (rip
->rip_vers
== 0) {
93 "RIP version 0 packet received from %s! (cmd %d)",
94 (*afswitch
[from
->sa_family
].af_format
)(from
), rip
->rip_cmd
);
97 switch (rip
->rip_cmd
) {
101 count
= size
- ((char *)n
- (char *)rip
);
102 if (count
< sizeof (struct netinfo
))
104 for (; count
> 0; n
++) {
105 if (count
< sizeof (struct netinfo
))
107 count
-= sizeof (struct netinfo
);
110 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
111 n
->rip_dst
.sa_family
= ntohs(n
->rip_dst
.sa_family
);
113 #define osa(x) ((struct osockaddr *)(&(x)))
114 n
->rip_dst
.sa_family
=
115 ntohs(osa(n
->rip_dst
)->sa_family
);
116 n
->rip_dst
.sa_len
= sizeof(n
->rip_dst
);
118 n
->rip_metric
= ntohl(n
->rip_metric
);
120 * A single entry with sa_family == AF_UNSPEC and
121 * metric ``infinity'' means ``all routes''.
122 * We respond to routers only if we are acting
123 * as a supplier, or to anyone other than a router
126 if (n
->rip_dst
.sa_family
== AF_UNSPEC
&&
127 n
->rip_metric
== HOPCNT_INFINITY
&& count
== 0) {
128 if (supplier
|| (*afp
->af_portmatch
)(from
) == 0)
129 supply(from
, 0, 0, 0);
132 if (n
->rip_dst
.sa_family
< af_max
&&
133 afswitch
[n
->rip_dst
.sa_family
].af_hash
)
134 rt
= rtlookup(&n
->rip_dst
);
137 #define min(a, b) (a < b ? a : b)
138 n
->rip_metric
= rt
== 0 ? HOPCNT_INFINITY
:
139 min(rt
->rt_metric
+ 1, HOPCNT_INFINITY
);
141 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
142 n
->rip_dst
.sa_family
= htons(n
->rip_dst
.sa_family
);
144 osa(n
->rip_dst
)->sa_family
=
145 htons(n
->rip_dst
.sa_family
);
147 n
->rip_metric
= htonl(n
->rip_metric
);
149 rip
->rip_cmd
= RIPCMD_RESPONSE
;
150 memmove(packet
, rip
, size
);
151 (*afp
->af_output
)(s
, 0, from
, size
);
155 case RIPCMD_TRACEOFF
:
156 /* verify message came from a privileged port */
157 if ((*afp
->af_portcheck
)(from
) == 0)
159 if ((ifp
= if_iflookup(from
)) == 0 || (ifp
->int_flags
&
160 (IFF_BROADCAST
| IFF_POINTOPOINT
| IFF_REMOTE
)) == 0 ||
161 ifp
->int_flags
& IFF_PASSIVE
) {
162 syslog(LOG_ERR
, "trace command from unknown router, %s",
163 (*afswitch
[from
->sa_family
].af_format
)(from
));
166 ((char *)rip
)[size
] = '\0';
167 if (rip
->rip_cmd
== RIPCMD_TRACEON
)
168 traceon(rip
->rip_tracefile
);
173 case RIPCMD_RESPONSE
:
174 /* verify message came from a router */
175 if ((*afp
->af_portmatch
)(from
) == 0)
177 (*afp
->af_canon
)(from
);
178 /* are we talking to ourselves? */
179 ifp
= if_ifwithaddr(from
);
181 if (ifp
->int_flags
& IFF_PASSIVE
) {
183 "bogus input (from passive interface, %s)",
184 (*afswitch
[from
->sa_family
].af_format
)(from
));
188 if (rt
== 0 || ((rt
->rt_state
& RTS_INTERFACE
) == 0) &&
189 rt
->rt_metric
>= ifp
->int_metric
)
196 * Update timer for interface on which the packet arrived.
197 * If from other end of a point-to-point link that isn't
198 * in the routing tables, (re-)add the route.
200 if ((rt
= rtfind(from
)) &&
201 (rt
->rt_state
& (RTS_INTERFACE
| RTS_REMOTE
)))
203 else if ((ifp
= if_ifwithdstaddr(from
)) &&
204 (rt
== 0 || rt
->rt_metric
>= ifp
->int_metric
))
207 * "Authenticate" router from which message originated.
208 * We accept routing packets from routers directly connected
209 * via broadcast or point-to-point networks,
210 * and from those listed in /etc/gateways.
212 if ((ifp
= if_iflookup(from
)) == 0 || (ifp
->int_flags
&
213 (IFF_BROADCAST
| IFF_POINTOPOINT
| IFF_REMOTE
)) == 0 ||
214 ifp
->int_flags
& IFF_PASSIVE
) {
215 if (memcmp(from
, &badfrom
, sizeof(badfrom
)) != 0) {
217 "packet from unknown router, %s",
218 (*afswitch
[from
->sa_family
].af_format
)(from
));
223 size
-= 4 * sizeof (char);
225 for (; size
> 0; size
-= sizeof (struct netinfo
), n
++) {
226 if (size
< sizeof (struct netinfo
))
229 if (sizeof(n
->rip_dst
.sa_family
) > 1) /* XXX */
230 n
->rip_dst
.sa_family
=
231 ntohs(n
->rip_dst
.sa_family
);
233 n
->rip_dst
.sa_family
=
234 ntohs(osa(n
->rip_dst
)->sa_family
);
235 n
->rip_dst
.sa_len
= sizeof(n
->rip_dst
);
237 n
->rip_metric
= ntohl(n
->rip_metric
);
238 if (n
->rip_dst
.sa_family
>= af_max
||
239 (afp
= &afswitch
[n
->rip_dst
.sa_family
])->af_hash
==
242 "route in unsupported address family (%d), from %s (af %d)\n",
243 n
->rip_dst
.sa_family
,
244 (*afswitch
[from
->sa_family
].af_format
)(from
),
248 if (((*afp
->af_checkhost
)(&n
->rip_dst
)) == 0) {
250 "bad host in route from %s (af %d)\n",
251 (*afswitch
[from
->sa_family
].af_format
)(from
),
255 if (n
->rip_metric
== 0 ||
256 (unsigned) n
->rip_metric
> HOPCNT_INFINITY
) {
257 if (memcmp(from
, &badfrom2
,
258 sizeof(badfrom2
)) != 0) {
260 "bad metric (%d) from %s\n",
262 (*afswitch
[from
->sa_family
].af_format
)(from
));
268 * Adjust metric according to incoming interface.
270 if ((unsigned) n
->rip_metric
< HOPCNT_INFINITY
)
271 n
->rip_metric
+= ifp
->int_metric
;
272 if ((unsigned) n
->rip_metric
> HOPCNT_INFINITY
)
273 n
->rip_metric
= HOPCNT_INFINITY
;
274 rt
= rtlookup(&n
->rip_dst
);
276 (rt
->rt_state
& (RTS_INTERNAL
|RTS_INTERFACE
)) ==
277 (RTS_INTERNAL
|RTS_INTERFACE
)) {
279 * If we're hearing a logical network route
280 * back from a peer to which we sent it,
283 if (rt
&& rt
->rt_state
& RTS_SUBNET
&&
284 (*afp
->af_sendroute
)(rt
, from
))
286 if ((unsigned)n
->rip_metric
< HOPCNT_INFINITY
) {
288 * Look for an equivalent route that
289 * includes this one before adding
292 rt
= rtfind(&n
->rip_dst
);
293 if (rt
&& equal(from
, &rt
->rt_router
))
295 rtadd(&n
->rip_dst
, from
, n
->rip_metric
, 0);
302 * Update if from gateway and different,
303 * shorter, or equivalent but old route
306 if (equal(from
, &rt
->rt_router
)) {
307 if (n
->rip_metric
!= rt
->rt_metric
) {
308 rtchange(rt
, from
, n
->rip_metric
);
311 if (rt
->rt_metric
>= HOPCNT_INFINITY
)
313 GARBAGE_TIME
- EXPIRE_TIME
;
314 } else if (rt
->rt_metric
< HOPCNT_INFINITY
)
316 } else if ((unsigned) n
->rip_metric
< rt
->rt_metric
||
317 (rt
->rt_metric
== n
->rip_metric
&&
318 rt
->rt_timer
> (EXPIRE_TIME
/2) &&
319 (unsigned) n
->rip_metric
< HOPCNT_INFINITY
)) {
320 rtchange(rt
, from
, n
->rip_metric
);
329 * If changes have occurred, and if we have not sent a broadcast
330 * recently, send a dynamic update. This update is sent only
331 * on interfaces other than the one on which we received notice
332 * of the change. If we are within MIN_WAITTIME of a full update,
333 * don't bother sending; if we just sent a dynamic update
334 * and set a timer (nextbcast), delay until that time.
335 * If we just sent a full update, delay the dynamic update.
336 * Set a timer for a randomized value to suppress additional
337 * dynamic updates until it expires; if we delayed sending
338 * the current changes, set needupdate.
340 if (changes
&& supplier
&&
341 now
.tv_sec
- lastfullupdate
.tv_sec
< SUPPLY_INTERVAL
-MAX_WAITTIME
) {
343 extern long random();
345 if (now
.tv_sec
- lastbcast
.tv_sec
>= MIN_WAITTIME
&&
346 timercmp(&nextbcast
, &now
, <)) {
348 fprintf(ftrace
, "send dynamic update\n");
349 toall(supply
, RTS_CHANGED
, ifp
);
352 nextbcast
.tv_sec
= 0;
356 fprintf(ftrace
, "delay dynamic update\n");
358 #define RANDOMDELAY() (MIN_WAITTIME * 1000000 + \
359 (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
361 if (nextbcast
.tv_sec
== 0) {
362 delay
= RANDOMDELAY();
365 "inhibit dynamic update for %d usec\n",
367 nextbcast
.tv_sec
= delay
/ 1000000;
368 nextbcast
.tv_usec
= delay
% 1000000;
369 timevaladd(&nextbcast
, &now
);
371 * If the next possibly dynamic update
372 * is within MIN_WAITTIME of the next full update,
373 * force the delay past the full update,
374 * or we might send a dynamic update just before
377 if (nextbcast
.tv_sec
> lastfullupdate
.tv_sec
+
378 SUPPLY_INTERVAL
- MIN_WAITTIME
)
379 nextbcast
.tv_sec
= lastfullupdate
.tv_sec
+