Commit | Line | Data |
---|---|---|
7c0bf99b | 1 | #ifndef lint |
eae14a37 | 2 | static char sccsid[] = "@(#)routed.c 4.11 %G%"; |
7c0bf99b SL |
3 | #endif |
4 | ||
3cec0c76 SL |
5 | /* |
6 | * Routing Table Management Daemon | |
7 | */ | |
8 | #include <sys/types.h> | |
7c0bf99b SL |
9 | #include <sys/ioctl.h> |
10 | #include <sys/socket.h> | |
11 | #include <net/in.h> | |
7c0bf99b SL |
12 | #include <net/if.h> |
13 | #include <errno.h> | |
14 | #include <stdio.h> | |
15 | #include <nlist.h> | |
16 | #include <signal.h> | |
17 | #include "rip.h" | |
18 | #include "router.h" | |
19 | ||
20 | #define LOOPBACKNET 0177 | |
21 | /* casts to keep lint happy */ | |
22 | #define insque(q,p) _insque((caddr_t)q,(caddr_t)p) | |
23 | #define remque(q) _remque((caddr_t)q) | |
24 | #define equal(a1, a2) \ | |
25 | (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) | |
77df73ca | 26 | #define min(a,b) ((a)>(b)?(b):(a)) |
7c0bf99b SL |
27 | |
28 | struct nlist nl[] = { | |
29 | #define N_IFNET 0 | |
30 | { "_ifnet" }, | |
31 | 0, | |
32 | }; | |
33 | ||
34 | struct sockaddr_in myaddr = { AF_INET, IPPORT_ROUTESERVER }; | |
35 | ||
36 | int s; | |
77df73ca | 37 | int kmem = -1; |
7c0bf99b SL |
38 | int supplier; /* process should supply updates */ |
39 | int initializing; /* stem off broadcast() calls */ | |
b7fcdb03 | 40 | int install = 1; /* if 1 call kernel */ |
77df73ca SL |
41 | int lookforinterfaces = 1; |
42 | int performnlist = 1; | |
7c0bf99b SL |
43 | int timeval; |
44 | int timer(); | |
45 | int cleanup(); | |
46 | int trace = 0; | |
47 | ||
48 | char packet[MAXPACKETSIZE]; | |
49 | ||
3cec0c76 SL |
50 | struct in_addr if_makeaddr(); |
51 | struct ifnet *if_ifwithaddr(), *if_ifwithnet(); | |
7c0bf99b | 52 | extern char *malloc(); |
c5f8709e | 53 | extern int errno, exit(); |
7c0bf99b SL |
54 | |
55 | main(argc, argv) | |
56 | int argc; | |
57 | char *argv[]; | |
58 | { | |
59 | int cc; | |
60 | struct sockaddr from; | |
61 | ||
62 | { int t = open("/dev/tty", 2); | |
63 | if (t >= 0) { | |
64 | ioctl(t, TIOCNOTTY, 0); | |
65 | close(t); | |
66 | } | |
67 | } | |
68 | if (trace) { | |
77df73ca SL |
69 | (void) freopen("/etc/routerlog", "a", stdout); |
70 | (void) dup2(fileno(stdout), 2); | |
7c0bf99b | 71 | setbuf(stdout, NULL); |
7c0bf99b | 72 | } |
e6b5ed24 | 73 | #ifdef vax || pdp11 |
7c0bf99b SL |
74 | myaddr.sin_port = htons(myaddr.sin_port); |
75 | #endif | |
76 | again: | |
77 | s = socket(SOCK_DGRAM, 0, &myaddr, 0); | |
78 | if (s < 0) { | |
79 | perror("socket"); | |
80 | sleep(30); | |
81 | goto again; | |
82 | } | |
83 | rtinit(); | |
84 | getothers(); | |
77df73ca | 85 | initializing = 1; |
7c0bf99b | 86 | getinterfaces(); |
77df73ca | 87 | initializing = 0; |
7c0bf99b SL |
88 | request(); |
89 | ||
90 | argv++, argc--; | |
91 | while (argc > 0) { | |
92 | if (strcmp(*argv, "-s") == 0) | |
77df73ca | 93 | supplier = 1; |
7c0bf99b SL |
94 | else if (strcmp(*argv, "-q") == 0) |
95 | supplier = 0; | |
96 | argv++, argc--; | |
97 | } | |
98 | sigset(SIGALRM, timer); | |
77df73ca | 99 | timer(); |
7c0bf99b SL |
100 | |
101 | /* | |
102 | * Listen for routing packets | |
103 | */ | |
104 | for (;;) { | |
105 | cc = receive(s, &from, packet, sizeof (packet)); | |
106 | if (cc <= 0) { | |
107 | if (cc < 0 && errno != EINTR) | |
108 | perror("receive"); | |
109 | continue; | |
110 | } | |
111 | sighold(SIGALRM); | |
112 | rip_input(&from, cc); | |
113 | sigrelse(SIGALRM); | |
114 | } | |
115 | } | |
116 | ||
117 | /* | |
118 | * Look in a file for any gateways we should configure | |
119 | * outside the directly connected ones. This is a kludge, | |
120 | * but until we can find out about gateways on the "other side" | |
121 | * of the ARPANET using GGP, it's a must. | |
122 | * | |
123 | * We don't really know the distance to the gateway, so we | |
124 | * assume it's a neighbor. | |
125 | */ | |
126 | getothers() | |
127 | { | |
128 | struct sockaddr_in dst, gate; | |
129 | FILE *fp = fopen("/etc/gateways", "r"); | |
130 | struct rt_entry *rt; | |
131 | ||
132 | if (fp == NULL) | |
133 | return; | |
134 | bzero((char *)&dst, sizeof (dst)); | |
135 | bzero((char *)&gate, sizeof (gate)); | |
136 | dst.sin_family = AF_INET; | |
137 | gate.sin_family = AF_INET; | |
138 | while (fscanf(fp, "%x %x", &dst.sin_addr.s_addr, | |
139 | &gate.sin_addr.s_addr) != EOF) { | |
140 | rtadd((struct sockaddr *)&dst, (struct sockaddr *)&gate, 1); | |
141 | rt = rtlookup((struct sockaddr *)&dst); | |
142 | if (rt) | |
98c1da1f | 143 | rt->rt_state |= RTS_HIDDEN; |
7c0bf99b SL |
144 | } |
145 | fclose(fp); | |
146 | } | |
147 | ||
77df73ca SL |
148 | /* |
149 | * Timer routine: | |
150 | * | |
151 | * o handle timers on table entries, | |
152 | * o invalidate entries which haven't been updated in a while, | |
153 | * o delete entries which are too old, | |
154 | * o retry ioctl's which weren't successful the first | |
155 | * time due to the kernel entry being busy | |
156 | * o if we're an internetwork router, supply routing updates | |
157 | * periodically | |
158 | */ | |
159 | timer() | |
7c0bf99b | 160 | { |
3cec0c76 | 161 | register struct rthash *rh; |
77df73ca | 162 | register struct rt_entry *rt; |
3cec0c76 | 163 | struct rthash *base = hosthash; |
77df73ca | 164 | int doinghost = 1; |
7c0bf99b | 165 | |
77df73ca SL |
166 | if (trace) |
167 | printf(">>> time %d >>>\n", timeval); | |
168 | again: | |
169 | for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) { | |
170 | rt = rh->rt_forw; | |
171 | for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { | |
7c0bf99b | 172 | |
77df73ca SL |
173 | /* |
174 | * If the host is indicated to be | |
98c1da1f | 175 | * "hidden" (i.e. it's one we got |
77df73ca SL |
176 | * from the initialization file), |
177 | * don't time out it's entry. | |
178 | */ | |
98c1da1f | 179 | if ((rt->rt_state & RTS_HIDDEN) == 0) |
e929cf48 | 180 | rt->rt_timer += TIMER_RATE; |
77df73ca | 181 | log("", rt); |
7c0bf99b | 182 | |
77df73ca SL |
183 | /* |
184 | * If the entry should be deleted | |
185 | * attempt to do so and reclaim space. | |
186 | */ | |
187 | if (rt->rt_timer >= GARBAGE_TIME || | |
e6b5ed24 | 188 | (rt->rt_state & RTS_DELRT)) { |
77df73ca | 189 | rt = rt->rt_back; |
e6b5ed24 | 190 | rtdelete(rt->rt_forw); |
77df73ca SL |
191 | continue; |
192 | } | |
7c0bf99b | 193 | |
77df73ca SL |
194 | /* |
195 | * If we haven't heard from the router | |
196 | * in a long time indicate the route is | |
197 | * hard to reach. | |
198 | */ | |
199 | if (rt->rt_timer >= EXPIRE_TIME) | |
200 | rt->rt_metric = HOPCNT_INFINITY; | |
e6b5ed24 | 201 | if (rt->rt_state & RTS_CHGRT) |
3cec0c76 | 202 | if (!ioctl(s, SIOCCHGRT,(char *)&rt->rt_rt) || |
77df73ca | 203 | --rt->rt_retry == 0) |
e6b5ed24 | 204 | rt->rt_state &= ~RTS_CHGRT; |
7c0bf99b | 205 | |
77df73ca SL |
206 | /* |
207 | * Try to add the route to the kernel tables. | |
208 | * If this fails because the entry already exists | |
209 | * (perhaps because someone manually added it) | |
210 | * change the add to a change. If the operation | |
211 | * fails otherwise (likely because the entry is | |
212 | * in use), retry the operation a few more times. | |
213 | */ | |
e6b5ed24 | 214 | if (rt->rt_state & RTS_ADDRT) { |
3cec0c76 | 215 | if (!ioctl(s, SIOCADDRT,(char *)&rt->rt_rt)) { |
77df73ca | 216 | if (errno == EEXIST) { |
e6b5ed24 SL |
217 | rt->rt_state &= ~RTS_ADDRT; |
218 | rt->rt_state |= RTS_CHGRT; | |
77df73ca SL |
219 | rt->rt_retry = |
220 | (EXPIRE_TIME/TIMER_RATE); | |
221 | continue; | |
222 | } | |
223 | if (--rt->rt_retry) | |
224 | continue; | |
225 | } | |
e6b5ed24 | 226 | rt->rt_state &= ~RTS_ADDRT; |
77df73ca SL |
227 | } |
228 | } | |
229 | } | |
230 | if (doinghost) { | |
231 | doinghost = 0; | |
232 | base = nethash; | |
233 | goto again; | |
234 | } | |
235 | timeval += TIMER_RATE; | |
236 | if (lookforinterfaces && (timeval % CHECK_INTERVAL) == 0) | |
237 | getinterfaces(); | |
238 | if (supplier && (timeval % SUPPLY_INTERVAL) == 0) | |
239 | supplyall(); | |
240 | if (trace) | |
241 | printf("<<< time %d <<<\n", timeval); | |
242 | alarm(TIMER_RATE); | |
7c0bf99b SL |
243 | } |
244 | ||
3cec0c76 | 245 | struct ifnet *ifnet; |
7c0bf99b SL |
246 | /* |
247 | * Find the network interfaces attached to this machine. | |
77df73ca | 248 | * The info is used to: |
7c0bf99b SL |
249 | * |
250 | * (1) initialize the routing tables, as done by the kernel. | |
251 | * (2) ignore incoming packets we send. | |
252 | * (3) figure out broadcast capability and addresses. | |
253 | * (4) figure out if we're an internetwork gateway. | |
254 | * | |
255 | * We don't handle anything but Internet addresses. | |
3cec0c76 SL |
256 | * |
257 | * Note: this routine may be called periodically to | |
258 | * scan for new interfaces. In fact, the timer routine | |
259 | * does so based on the flag lookforinterfaces. The | |
260 | * flag performnlist is set whenever something odd occurs | |
261 | * while scanning the kernel; this is likely to occur | |
262 | * if /vmunix isn't up to date (e.g. someone booted /ovmunix). | |
7c0bf99b SL |
263 | */ |
264 | getinterfaces() | |
265 | { | |
77df73ca SL |
266 | struct ifnet *ifp; |
267 | struct ifnet ifstruct, *next; | |
7c0bf99b | 268 | struct sockaddr_in net; |
77df73ca | 269 | register struct sockaddr *dst; |
7c0bf99b SL |
270 | int nets; |
271 | ||
77df73ca SL |
272 | if (performnlist) { |
273 | nlist("/vmunix", nl); | |
274 | if (nl[N_IFNET].n_value == 0) { | |
275 | performnlist++; | |
276 | printf("ifnet: not in namelist\n"); | |
277 | return; | |
278 | } | |
279 | performnlist = 0; | |
7c0bf99b | 280 | } |
7c0bf99b | 281 | if (kmem < 0) { |
77df73ca SL |
282 | kmem = open("/dev/kmem", 0); |
283 | if (kmem < 0) { | |
284 | perror("/dev/kmem"); | |
285 | return; | |
286 | } | |
287 | } | |
288 | if (lseek(kmem, (long)nl[N_IFNET].n_value, 0) == -1 || | |
289 | read(kmem, (char *)&ifp, sizeof (ifp)) != sizeof (ifp)) { | |
290 | performnlist = 1; | |
291 | return; | |
7c0bf99b | 292 | } |
7c0bf99b SL |
293 | bzero((char *)&net, sizeof (net)); |
294 | net.sin_family = AF_INET; | |
71efe4b8 | 295 | lookforinterfaces = 0; |
7c0bf99b | 296 | nets = 0; |
77df73ca SL |
297 | while (ifp) { |
298 | if (lseek(kmem, (long)ifp, 0) == -1 || | |
299 | read(kmem, (char *)&ifstruct, sizeof (ifstruct)) != | |
300 | sizeof (ifstruct)) { | |
7c0bf99b | 301 | perror("read"); |
71efe4b8 | 302 | lookforinterfaces = performnlist = 1; |
7c0bf99b SL |
303 | break; |
304 | } | |
77df73ca SL |
305 | ifp = &ifstruct; |
306 | if ((ifp->if_flags & IFF_UP) == 0) { | |
71efe4b8 | 307 | lookforinterfaces = 1; |
77df73ca SL |
308 | skip: |
309 | ifp = ifp->if_next; | |
310 | continue; | |
311 | } | |
312 | if (ifp->if_addr.sa_family != AF_INET) | |
7c0bf99b | 313 | goto skip; |
77df73ca SL |
314 | /* ignore software loopback networks. */ |
315 | if (ifp->if_net == LOOPBACKNET) | |
7c0bf99b | 316 | goto skip; |
77df73ca SL |
317 | /* check if we already know about this one */ |
318 | if (if_ifwithaddr(&ifstruct.if_addr)) { | |
319 | nets++; | |
7c0bf99b SL |
320 | goto skip; |
321 | } | |
77df73ca SL |
322 | ifp = (struct ifnet *)malloc(sizeof (struct ifnet)); |
323 | if (ifp == 0) { | |
324 | printf("routed: out of memory\n"); | |
325 | break; | |
326 | } | |
327 | bcopy((char *)&ifstruct, (char *)ifp, sizeof (struct ifnet)); | |
7c0bf99b SL |
328 | |
329 | /* | |
77df73ca SL |
330 | * Count the # of directly connected networks |
331 | * and point to point links which aren't looped | |
332 | * back to ourself. This is used below to | |
333 | * decide if we should be a routing "supplier". | |
7c0bf99b | 334 | */ |
77df73ca SL |
335 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0 || |
336 | if_ifwithaddr(&ifp->if_dstaddr) == 0) | |
337 | nets++; | |
338 | ||
339 | if (ifp->if_flags & IFF_POINTOPOINT) | |
340 | dst = &ifp->if_dstaddr; | |
341 | else { | |
342 | net.sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); | |
343 | dst = (struct sockaddr *)&net; | |
344 | } | |
345 | next = ifp->if_next; | |
346 | ifp->if_next = ifnet; | |
347 | ifnet = ifp; | |
348 | if (rtlookup(dst) == 0) | |
349 | rtadd(dst, &ifp->if_addr, 0); | |
350 | ifp = next; | |
7c0bf99b | 351 | } |
7c0bf99b SL |
352 | supplier = nets > 1; |
353 | } | |
354 | ||
355 | /* | |
356 | * Send a request message to all directly | |
357 | * connected hosts and networks. | |
358 | */ | |
359 | request() | |
360 | { | |
77df73ca | 361 | register struct rip *msg = (struct rip *)packet; |
7c0bf99b | 362 | |
77df73ca SL |
363 | msg->rip_cmd = RIPCMD_REQUEST; |
364 | msg->rip_nets[0].rip_dst.sa_family = AF_UNSPEC; | |
365 | msg->rip_nets[0].rip_metric = HOPCNT_INFINITY; | |
366 | sendall(); | |
7c0bf99b SL |
367 | } |
368 | ||
369 | /* | |
370 | * Broadcast a new, or modified, routing table entry | |
371 | * to all directly connected hosts and networks. | |
372 | */ | |
373 | broadcast(entry) | |
374 | struct rt_entry *entry; | |
77df73ca SL |
375 | { |
376 | struct rip *msg = (struct rip *)packet; | |
377 | ||
378 | log("broadcast", entry); | |
379 | msg->rip_cmd = RIPCMD_RESPONSE; | |
380 | msg->rip_nets[0].rip_dst = entry->rt_dst; | |
381 | msg->rip_nets[0].rip_metric = min(entry->rt_metric+1, HOPCNT_INFINITY); | |
382 | sendall(); | |
383 | } | |
384 | ||
3cec0c76 SL |
385 | /* |
386 | * Send "packet" to all neighbors. | |
387 | */ | |
77df73ca | 388 | sendall() |
7c0bf99b | 389 | { |
3cec0c76 | 390 | register struct rthash *rh; |
7c0bf99b SL |
391 | register struct rt_entry *rt; |
392 | register struct sockaddr *dst; | |
3cec0c76 | 393 | struct rthash *base = hosthash; |
7c0bf99b | 394 | int doinghost = 1; |
7c0bf99b SL |
395 | |
396 | again: | |
397 | for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) | |
398 | for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { | |
98c1da1f | 399 | if ((rt->rt_state & RTS_HIDDEN) || rt->rt_metric > 0) |
7c0bf99b SL |
400 | continue; |
401 | if (rt->rt_ifp && (rt->rt_ifp->if_flags & IFF_BROADCAST)) | |
402 | dst = &rt->rt_ifp->if_broadaddr; | |
403 | else | |
404 | dst = &rt->rt_gateway; | |
405 | (*afswitch[dst->sa_family].af_output)(dst, sizeof (struct rip)); | |
406 | } | |
407 | if (doinghost) { | |
7c0bf99b | 408 | base = nethash; |
77df73ca | 409 | doinghost = 0; |
7c0bf99b SL |
410 | goto again; |
411 | } | |
412 | } | |
413 | ||
414 | /* | |
415 | * Supply all directly connected neighbors with the | |
416 | * current state of the routing tables. | |
417 | */ | |
418 | supplyall() | |
419 | { | |
420 | register struct rt_entry *rt; | |
3cec0c76 | 421 | register struct rthash *rh; |
7c0bf99b | 422 | register struct sockaddr *dst; |
3cec0c76 | 423 | struct rthash *base = hosthash; |
7c0bf99b SL |
424 | int doinghost = 1; |
425 | ||
426 | again: | |
427 | for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) | |
428 | for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { | |
98c1da1f | 429 | if ((rt->rt_state & RTS_HIDDEN) || rt->rt_metric > 0) |
7c0bf99b SL |
430 | continue; |
431 | if (rt->rt_ifp && (rt->rt_ifp->if_flags & IFF_BROADCAST)) | |
432 | dst = &rt->rt_ifp->if_broadaddr; | |
433 | else | |
434 | dst = &rt->rt_gateway; | |
77df73ca | 435 | log("supply", rt); |
7c0bf99b SL |
436 | supply(dst); |
437 | } | |
438 | if (doinghost) { | |
439 | base = nethash; | |
440 | doinghost = 0; | |
441 | goto again; | |
442 | } | |
443 | } | |
444 | ||
445 | /* | |
446 | * Supply routing information to target "sa". | |
447 | */ | |
448 | supply(sa) | |
449 | struct sockaddr *sa; | |
450 | { | |
451 | struct rip *msg = (struct rip *)packet; | |
452 | struct netinfo *n = msg->rip_nets; | |
3cec0c76 | 453 | register struct rthash *rh; |
7c0bf99b | 454 | register struct rt_entry *rt; |
3cec0c76 | 455 | struct rthash *base = hosthash; |
77df73ca | 456 | int doinghost = 1, size; |
7c0bf99b SL |
457 | int (*output)() = afswitch[sa->sa_family].af_output; |
458 | ||
459 | msg->rip_cmd = RIPCMD_RESPONSE; | |
460 | again: | |
461 | for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) | |
462 | for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { | |
463 | ||
464 | /* | |
465 | * Flush packet out if not enough room for | |
466 | * another routing table entry. | |
467 | */ | |
77df73ca SL |
468 | size = (char *)n - packet; |
469 | if (size > MAXPACKETSIZE - sizeof (struct netinfo)) { | |
470 | (*output)(sa, size); | |
7c0bf99b SL |
471 | n = msg->rip_nets; |
472 | } | |
473 | n->rip_dst = rt->rt_dst; | |
77df73ca SL |
474 | n->rip_metric = min(rt->rt_metric + 1, HOPCNT_INFINITY); |
475 | n++; | |
7c0bf99b SL |
476 | } |
477 | if (doinghost) { | |
478 | doinghost = 0; | |
479 | base = nethash; | |
480 | goto again; | |
481 | } | |
77df73ca SL |
482 | if (n != msg->rip_nets) |
483 | (*output)(sa, (char *)n - packet); | |
7c0bf99b SL |
484 | } |
485 | ||
486 | /* | |
487 | * Respond to a routing info request. | |
488 | */ | |
489 | rip_respond(from, size) | |
490 | struct sockaddr *from; | |
491 | int size; | |
492 | { | |
493 | register struct rip *msg = (struct rip *)packet; | |
494 | struct netinfo *np = msg->rip_nets; | |
495 | struct rt_entry *rt; | |
496 | int newsize = 0; | |
497 | ||
77df73ca | 498 | size -= 4 * sizeof (char); |
7c0bf99b SL |
499 | while (size > 0) { |
500 | if (size < sizeof (struct netinfo)) | |
501 | break; | |
502 | size -= sizeof (struct netinfo); | |
503 | if (np->rip_dst.sa_family == AF_UNSPEC && | |
504 | np->rip_metric == HOPCNT_INFINITY && size == 0) { | |
505 | supply(from); | |
506 | return; | |
507 | } | |
508 | rt = rtlookup(&np->rip_dst); | |
77df73ca SL |
509 | np->rip_metric = rt == 0 ? |
510 | HOPCNT_INFINITY : min(rt->rt_metric+1, HOPCNT_INFINITY); | |
7c0bf99b SL |
511 | np++, newsize += sizeof (struct netinfo); |
512 | } | |
513 | if (newsize > 0) { | |
514 | msg->rip_cmd = RIPCMD_RESPONSE; | |
515 | newsize += sizeof (int); | |
516 | (*afswitch[from->sa_family].af_output)(from, newsize); | |
517 | } | |
518 | } | |
519 | ||
520 | /* | |
521 | * Handle an incoming routing packet. | |
522 | */ | |
523 | rip_input(from, size) | |
524 | struct sockaddr *from; | |
525 | int size; | |
526 | { | |
527 | register struct rip *msg = (struct rip *)packet; | |
528 | struct rt_entry *rt; | |
529 | struct netinfo *n; | |
530 | ||
e6b5ed24 | 531 | switch (msg->rip_cmd) { |
7c0bf99b | 532 | |
e6b5ed24 | 533 | default: |
7c0bf99b | 534 | return; |
e6b5ed24 SL |
535 | |
536 | case RIPCMD_REQUEST: | |
7c0bf99b SL |
537 | rip_respond(from, size); |
538 | return; | |
e6b5ed24 SL |
539 | |
540 | case RIPCMD_RESPONSE: | |
541 | /* verify message came from a priviledged port */ | |
542 | if ((*afswitch[from->sa_family].af_portmatch)(from) == 0) | |
543 | return; | |
544 | break; | |
7c0bf99b SL |
545 | } |
546 | ||
547 | /* | |
548 | * Process updates. | |
549 | * Extraneous information like Internet ports | |
550 | * must first be purged from the sender's address for | |
551 | * pattern matching below. | |
552 | */ | |
553 | (*afswitch[from->sa_family].af_canon)(from); | |
554 | if (trace) | |
3cec0c76 SL |
555 | printf("input from %x\n", |
556 | ((struct sockaddr_in *)from)->sin_addr); | |
7c0bf99b SL |
557 | /* |
558 | * If response packet is from ourselves, use it only | |
559 | * to reset timer on entry. Otherwise, we'd believe | |
560 | * it as gospel (since it comes from the router) and | |
561 | * unknowingly update the metric to show the outgoing | |
562 | * cost (higher than our real cost). I guess the protocol | |
563 | * spec doesn't address this because Xerox Ethernets | |
564 | * don't hear their own broadcasts? | |
565 | */ | |
566 | if (if_ifwithaddr(from)) { | |
eae14a37 | 567 | rt = rtfind(from); |
7c0bf99b SL |
568 | if (rt) |
569 | rt->rt_timer = 0; | |
570 | return; | |
571 | } | |
77df73ca | 572 | size -= 4 * sizeof (char); |
7c0bf99b SL |
573 | n = msg->rip_nets; |
574 | for (; size > 0; size -= sizeof (struct netinfo), n++) { | |
575 | if (size < sizeof (struct netinfo)) | |
576 | break; | |
577 | if (trace) | |
3cec0c76 SL |
578 | printf("dst %x hc %d...", |
579 | ((struct sockaddr_in *)&n->rip_dst)->sin_addr, | |
7c0bf99b SL |
580 | n->rip_metric); |
581 | rt = rtlookup(&n->rip_dst); | |
582 | ||
583 | /* | |
584 | * Unknown entry, add it to the tables only if | |
585 | * its interesting. | |
586 | */ | |
587 | if (rt == 0) { | |
588 | if (n->rip_metric < HOPCNT_INFINITY) | |
589 | rtadd(&n->rip_dst, from, n->rip_metric); | |
590 | if (trace) | |
591 | printf("new\n"); | |
592 | continue; | |
593 | } | |
594 | ||
595 | if (trace) | |
596 | printf("ours: gate %x hc %d timer %d\n", | |
3cec0c76 SL |
597 | ((struct sockaddr_in *)&rt->rt_gateway)->sin_addr, |
598 | rt->rt_metric, rt->rt_timer); | |
7c0bf99b SL |
599 | /* |
600 | * Update the entry if one of the following is true: | |
601 | * | |
602 | * (1) The update came directly from the gateway. | |
603 | * (2) A shorter path is provided. | |
604 | * (3) The entry hasn't been updated in a while | |
77df73ca SL |
605 | * and a path of equivalent cost is offered |
606 | * (with the cost finite). | |
7c0bf99b SL |
607 | */ |
608 | if (equal(from, &rt->rt_gateway) || | |
609 | rt->rt_metric > n->rip_metric || | |
610 | (rt->rt_timer > (EXPIRE_TIME/2) && | |
77df73ca SL |
611 | rt->rt_metric == n->rip_metric && |
612 | rt->rt_metric < HOPCNT_INFINITY)) { | |
7c0bf99b SL |
613 | rtchange(rt, from, n->rip_metric); |
614 | rt->rt_timer = 0; | |
615 | } | |
616 | } | |
617 | } | |
618 | ||
3cec0c76 SL |
619 | rtinit() |
620 | { | |
621 | register struct rthash *rh; | |
622 | ||
623 | for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++) | |
624 | rh->rt_forw = rh->rt_back = (struct rt_entry *)rh; | |
625 | for (rh = hosthash; rh < &hosthash[ROUTEHASHSIZ]; rh++) | |
626 | rh->rt_forw = rh->rt_back = (struct rt_entry *)rh; | |
627 | } | |
628 | ||
7c0bf99b SL |
629 | /* |
630 | * Lookup an entry to the appropriate dstination. | |
631 | */ | |
632 | struct rt_entry * | |
633 | rtlookup(dst) | |
634 | struct sockaddr *dst; | |
635 | { | |
636 | register struct rt_entry *rt; | |
3cec0c76 | 637 | register struct rthash *rh; |
eae14a37 | 638 | register int hash; |
7c0bf99b | 639 | struct afhash h; |
eae14a37 SL |
640 | int doinghost = 1; |
641 | ||
642 | if (dst->sa_family >= AF_MAX) | |
643 | return (0); | |
644 | (*afswitch[dst->sa_family].af_hash)(dst, &h); | |
645 | hash = h.afh_hosthash; | |
646 | rh = &hosthash[hash % ROUTEHASHSIZ]; | |
647 | again: | |
648 | for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { | |
649 | if (rt->rt_hash != hash) | |
650 | continue; | |
651 | if (equal(&rt->rt_dst, dst)) | |
652 | return (rt); | |
653 | } | |
654 | if (doinghost) { | |
655 | doinghost = 0; | |
656 | hash = h.afh_nethash; | |
657 | rh = &nethash[hash % ROUTEHASHSIZ]; | |
658 | goto again; | |
659 | } | |
660 | return (0); | |
661 | } | |
662 | ||
663 | /* | |
664 | * Find an entry based on address "dst", as the kernel | |
665 | * does in selecting routes. This means we look first | |
666 | * for a point to point link, settling for a route to | |
667 | * the destination network if the former fails. | |
668 | */ | |
669 | struct rt_entry * | |
670 | rtfind(dst) | |
671 | struct sockaddr *dst; | |
672 | { | |
673 | register struct rt_entry *rt; | |
674 | register struct rthash *rh; | |
675 | register int hash; | |
676 | struct afhash h; | |
677 | int af = dst->sa_family; | |
678 | int doinghost = 1, (*match)(); | |
7c0bf99b SL |
679 | |
680 | if (af >= AF_MAX) | |
681 | return (0); | |
682 | (*afswitch[af].af_hash)(dst, &h); | |
683 | hash = h.afh_hosthash; | |
684 | rh = &hosthash[hash % ROUTEHASHSIZ]; | |
685 | ||
686 | again: | |
687 | for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) { | |
688 | if (rt->rt_hash != hash) | |
689 | continue; | |
690 | if (doinghost) { | |
691 | if (equal(&rt->rt_dst, dst)) | |
692 | return (rt); | |
693 | } else { | |
694 | if (rt->rt_dst.sa_family == af && | |
695 | (*match)(&rt->rt_dst, dst)) | |
696 | return (rt); | |
697 | } | |
698 | } | |
699 | if (doinghost) { | |
700 | doinghost = 0; | |
701 | hash = h.afh_nethash; | |
7c0bf99b | 702 | rh = &nethash[hash % ROUTEHASHSIZ]; |
eae14a37 | 703 | match = afswitch[af].af_netmatch; |
7c0bf99b SL |
704 | goto again; |
705 | } | |
706 | return (0); | |
707 | } | |
708 | ||
7c0bf99b SL |
709 | /* |
710 | * Add a new entry. | |
711 | */ | |
712 | rtadd(dst, gate, metric) | |
713 | struct sockaddr *dst, *gate; | |
714 | short metric; | |
715 | { | |
716 | struct afhash h; | |
717 | register struct rt_entry *rt; | |
3cec0c76 | 718 | struct rthash *rh; |
7c0bf99b SL |
719 | int af = dst->sa_family, flags, hash; |
720 | ||
721 | if (af >= AF_MAX) | |
722 | return; | |
723 | (*afswitch[af].af_hash)(dst, &h); | |
724 | flags = (*afswitch[af].af_checkhost)(dst) ? RTF_HOST : 0; | |
725 | if (flags & RTF_HOST) { | |
726 | hash = h.afh_hosthash; | |
727 | rh = &hosthash[hash % ROUTEHASHSIZ]; | |
728 | } else { | |
729 | hash = h.afh_nethash; | |
730 | rh = &nethash[hash % ROUTEHASHSIZ]; | |
731 | } | |
732 | rt = (struct rt_entry *)malloc(sizeof (*rt)); | |
733 | if (rt == 0) | |
734 | return; | |
735 | rt->rt_hash = hash; | |
736 | rt->rt_dst = *dst; | |
737 | rt->rt_gateway = *gate; | |
738 | rt->rt_metric = metric; | |
739 | rt->rt_timer = 0; | |
740 | rt->rt_flags = RTF_UP | flags; | |
e6b5ed24 | 741 | rt->rt_state = 0; |
7c0bf99b SL |
742 | rt->rt_ifp = if_ifwithnet(&rt->rt_gateway); |
743 | if (metric == 0) | |
744 | rt->rt_flags |= RTF_DIRECT; | |
745 | insque(rt, rh); | |
77df73ca | 746 | log("add", rt); |
7c0bf99b SL |
747 | if (initializing) |
748 | return; | |
749 | if (supplier) | |
750 | broadcast(rt); | |
751 | if (install) { | |
e6b5ed24 | 752 | rt->rt_state |= RTS_ADDRT; |
7c0bf99b SL |
753 | rt->rt_retry = EXPIRE_TIME/TIMER_RATE; |
754 | } | |
755 | } | |
756 | ||
757 | /* | |
758 | * Look to see if a change to an existing entry | |
759 | * is warranted; if so, make it. | |
760 | */ | |
761 | rtchange(rt, gate, metric) | |
762 | struct rt_entry *rt; | |
763 | struct sockaddr *gate; | |
764 | short metric; | |
765 | { | |
766 | int change = 0; | |
767 | ||
768 | if (!equal(&rt->rt_gateway, gate)) { | |
769 | rt->rt_gateway = *gate; | |
770 | change++; | |
771 | } | |
772 | ||
773 | /* | |
774 | * If the hop count has changed, adjust | |
775 | * the flags in the routing table entry accordingly. | |
776 | */ | |
777 | if (metric != rt->rt_metric) { | |
778 | if (rt->rt_metric == 0) | |
779 | rt->rt_flags &= ~RTF_DIRECT; | |
780 | rt->rt_metric = metric; | |
781 | if (metric >= HOPCNT_INFINITY) | |
782 | rt->rt_flags &= ~RTF_UP; | |
783 | else | |
784 | rt->rt_flags |= RTF_UP; | |
785 | change++; | |
786 | } | |
787 | ||
788 | if (!change) | |
789 | return; | |
790 | if (supplier) | |
791 | broadcast(rt); | |
77df73ca | 792 | log("change", rt); |
7c0bf99b | 793 | if (install) { |
e6b5ed24 | 794 | rt->rt_state |= RTS_CHGRT; |
7c0bf99b SL |
795 | rt->rt_retry = EXPIRE_TIME/TIMER_RATE; |
796 | } | |
797 | } | |
798 | ||
799 | /* | |
800 | * Delete a routing table entry. | |
801 | */ | |
802 | rtdelete(rt) | |
803 | struct rt_entry *rt; | |
804 | { | |
77df73ca | 805 | log("delete", rt); |
7c0bf99b | 806 | if (install) |
3cec0c76 | 807 | if (ioctl(s, SIOCDELRT, (char *)&rt->rt_rt) && |
7c0bf99b | 808 | errno == EBUSY) |
e6b5ed24 | 809 | rt->rt_state |= RTS_DELRT; |
7c0bf99b SL |
810 | remque(rt); |
811 | free((char *)rt); | |
812 | } | |
813 | ||
7c0bf99b SL |
814 | log(operation, rt) |
815 | char *operation; | |
816 | struct rt_entry *rt; | |
817 | { | |
818 | time_t t = time(0); | |
819 | struct sockaddr_in *dst, *gate; | |
e6b5ed24 | 820 | static struct bits { |
7c0bf99b SL |
821 | int t_bits; |
822 | char *t_name; | |
e6b5ed24 | 823 | } flagbits[] = { |
7c0bf99b SL |
824 | { RTF_UP, "UP" }, |
825 | { RTF_DIRECT, "DIRECT" }, | |
826 | { RTF_HOST, "HOST" }, | |
e6b5ed24 SL |
827 | { 0 } |
828 | }, statebits[] = { | |
829 | { RTS_DELRT, "DELETE" }, | |
830 | { RTS_CHGRT, "CHANGE" }, | |
98c1da1f | 831 | { RTS_HIDDEN, "HIDDEN" }, |
7c0bf99b SL |
832 | { 0 } |
833 | }; | |
e6b5ed24 | 834 | register struct bits *p; |
7c0bf99b SL |
835 | register int first; |
836 | char *cp; | |
837 | ||
77df73ca SL |
838 | if (trace == 0) |
839 | return; | |
7c0bf99b SL |
840 | printf("%s ", operation); |
841 | dst = (struct sockaddr_in *)&rt->rt_dst; | |
842 | gate = (struct sockaddr_in *)&rt->rt_gateway; | |
e6b5ed24 | 843 | printf("dst %x, router %x, metric %d, flags", |
7c0bf99b | 844 | dst->sin_addr, gate->sin_addr, rt->rt_metric); |
e6b5ed24 SL |
845 | cp = " %s"; |
846 | for (first = 1, p = flagbits; p->t_bits > 0; p++) { | |
7c0bf99b SL |
847 | if ((rt->rt_flags & p->t_bits) == 0) |
848 | continue; | |
849 | printf(cp, p->t_name); | |
850 | if (first) { | |
851 | cp = "|%s"; | |
852 | first = 0; | |
853 | } | |
854 | } | |
e6b5ed24 SL |
855 | printf(" state"); |
856 | cp = " %s"; | |
857 | for (first = 1, p = statebits; p->t_bits > 0; p++) { | |
858 | if ((rt->rt_state & p->t_bits) == 0) | |
859 | continue; | |
860 | printf(cp, p->t_name); | |
861 | if (first) { | |
862 | cp = "|%s"; | |
863 | first = 0; | |
864 | } | |
865 | } | |
7c0bf99b SL |
866 | putchar('\n'); |
867 | } | |
77df73ca | 868 | |
3cec0c76 SL |
869 | /* |
870 | * Find the interface with address "addr". | |
871 | */ | |
77df73ca SL |
872 | struct ifnet * |
873 | if_ifwithaddr(addr) | |
874 | struct sockaddr *addr; | |
875 | { | |
876 | register struct ifnet *ifp; | |
877 | ||
878 | #define same(a1, a2) \ | |
879 | (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0) | |
880 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { | |
881 | if (ifp->if_addr.sa_family != addr->sa_family) | |
882 | continue; | |
883 | if (same(&ifp->if_addr, addr)) | |
884 | break; | |
885 | if ((ifp->if_flags & IFF_BROADCAST) && | |
886 | same(&ifp->if_broadaddr, addr)) | |
887 | break; | |
888 | } | |
889 | return (ifp); | |
890 | #undef same | |
891 | } | |
892 | ||
3cec0c76 SL |
893 | /* |
894 | * Find the interface with network imbedded in | |
895 | * the sockaddr "addr". Must use per-af routine | |
896 | * look for match. | |
897 | */ | |
77df73ca SL |
898 | struct ifnet * |
899 | if_ifwithnet(addr) | |
900 | register struct sockaddr *addr; | |
901 | { | |
902 | register struct ifnet *ifp; | |
903 | register int af = addr->sa_family; | |
904 | register int (*netmatch)(); | |
905 | ||
906 | if (af >= AF_MAX) | |
907 | return (0); | |
908 | netmatch = afswitch[af].af_netmatch; | |
909 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { | |
910 | if (af != ifp->if_addr.sa_family) | |
911 | continue; | |
912 | if ((*netmatch)(addr, &ifp->if_addr)) | |
913 | break; | |
914 | } | |
915 | return (ifp); | |
916 | } | |
917 | ||
3cec0c76 SL |
918 | /* |
919 | * Formulate an Internet address. | |
920 | */ | |
77df73ca SL |
921 | struct in_addr |
922 | if_makeaddr(net, host) | |
923 | int net, host; | |
924 | { | |
925 | u_long addr; | |
926 | ||
927 | if (net < 128) | |
928 | addr = (net << 24) | host; | |
929 | else if (net < 65536) | |
930 | addr = (net << 16) | host; | |
931 | else | |
932 | addr = (net << 8) | host; | |
933 | #ifdef vax | |
934 | addr = htonl(addr); | |
935 | #endif | |
936 | return (*(struct in_addr *)&addr); | |
937 | } |