make temp files the correct modes in all cases
[unix-history] / usr / src / sbin / routed / routed.c
CommitLineData
7c0bf99b 1#ifndef lint
98c1da1f 2static char sccsid[] = "@(#)routed.c 4.10 %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
28struct nlist nl[] = {
29#define N_IFNET 0
30 { "_ifnet" },
31 0,
32};
33
34struct sockaddr_in myaddr = { AF_INET, IPPORT_ROUTESERVER };
35
36int s;
77df73ca 37int kmem = -1;
7c0bf99b
SL
38int supplier; /* process should supply updates */
39int initializing; /* stem off broadcast() calls */
b7fcdb03 40int install = 1; /* if 1 call kernel */
77df73ca
SL
41int lookforinterfaces = 1;
42int performnlist = 1;
7c0bf99b
SL
43int timeval;
44int timer();
45int cleanup();
46int trace = 0;
47
48char packet[MAXPACKETSIZE];
49
3cec0c76
SL
50struct in_addr if_makeaddr();
51struct ifnet *if_ifwithaddr(), *if_ifwithnet();
7c0bf99b 52extern char *malloc();
c5f8709e 53extern int errno, exit();
7c0bf99b
SL
54
55main(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
76again:
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 */
126getothers()
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 */
159timer()
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);
168again:
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 245struct 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 */
264getinterfaces()
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 */
359request()
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 */
373broadcast(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 388sendall()
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
396again:
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 */
418supplyall()
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
426again:
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 */
448supply(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;
460again:
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 */
489rip_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 */
523rip_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)) {
567 rt = rtlookup(from);
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
619rtinit()
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 */
632struct rt_entry *
633rtlookup(dst)
634 struct sockaddr *dst;
635{
636 register struct rt_entry *rt;
3cec0c76 637 register struct rthash *rh;
7c0bf99b
SL
638 register int hash, (*match)();
639 struct afhash h;
640 int af = dst->sa_family, doinghost = 1;
641
642 if (af >= AF_MAX)
643 return (0);
644 (*afswitch[af].af_hash)(dst, &h);
645 hash = h.afh_hosthash;
646 rh = &hosthash[hash % ROUTEHASHSIZ];
647
648again:
649 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
650 if (rt->rt_hash != hash)
651 continue;
652 if (doinghost) {
653 if (equal(&rt->rt_dst, dst))
654 return (rt);
655 } else {
656 if (rt->rt_dst.sa_family == af &&
657 (*match)(&rt->rt_dst, dst))
658 return (rt);
659 }
660 }
661 if (doinghost) {
662 doinghost = 0;
663 hash = h.afh_nethash;
664 match = afswitch[af].af_netmatch;
665 rh = &nethash[hash % ROUTEHASHSIZ];
666 goto again;
667 }
668 return (0);
669}
670
7c0bf99b
SL
671/*
672 * Add a new entry.
673 */
674rtadd(dst, gate, metric)
675 struct sockaddr *dst, *gate;
676 short metric;
677{
678 struct afhash h;
679 register struct rt_entry *rt;
3cec0c76 680 struct rthash *rh;
7c0bf99b
SL
681 int af = dst->sa_family, flags, hash;
682
683 if (af >= AF_MAX)
684 return;
685 (*afswitch[af].af_hash)(dst, &h);
686 flags = (*afswitch[af].af_checkhost)(dst) ? RTF_HOST : 0;
687 if (flags & RTF_HOST) {
688 hash = h.afh_hosthash;
689 rh = &hosthash[hash % ROUTEHASHSIZ];
690 } else {
691 hash = h.afh_nethash;
692 rh = &nethash[hash % ROUTEHASHSIZ];
693 }
694 rt = (struct rt_entry *)malloc(sizeof (*rt));
695 if (rt == 0)
696 return;
697 rt->rt_hash = hash;
698 rt->rt_dst = *dst;
699 rt->rt_gateway = *gate;
700 rt->rt_metric = metric;
701 rt->rt_timer = 0;
702 rt->rt_flags = RTF_UP | flags;
e6b5ed24 703 rt->rt_state = 0;
7c0bf99b
SL
704 rt->rt_ifp = if_ifwithnet(&rt->rt_gateway);
705 if (metric == 0)
706 rt->rt_flags |= RTF_DIRECT;
707 insque(rt, rh);
77df73ca 708 log("add", rt);
7c0bf99b
SL
709 if (initializing)
710 return;
711 if (supplier)
712 broadcast(rt);
713 if (install) {
e6b5ed24 714 rt->rt_state |= RTS_ADDRT;
7c0bf99b
SL
715 rt->rt_retry = EXPIRE_TIME/TIMER_RATE;
716 }
717}
718
719/*
720 * Look to see if a change to an existing entry
721 * is warranted; if so, make it.
722 */
723rtchange(rt, gate, metric)
724 struct rt_entry *rt;
725 struct sockaddr *gate;
726 short metric;
727{
728 int change = 0;
729
730 if (!equal(&rt->rt_gateway, gate)) {
731 rt->rt_gateway = *gate;
732 change++;
733 }
734
735 /*
736 * If the hop count has changed, adjust
737 * the flags in the routing table entry accordingly.
738 */
739 if (metric != rt->rt_metric) {
740 if (rt->rt_metric == 0)
741 rt->rt_flags &= ~RTF_DIRECT;
742 rt->rt_metric = metric;
743 if (metric >= HOPCNT_INFINITY)
744 rt->rt_flags &= ~RTF_UP;
745 else
746 rt->rt_flags |= RTF_UP;
747 change++;
748 }
749
750 if (!change)
751 return;
752 if (supplier)
753 broadcast(rt);
77df73ca 754 log("change", rt);
7c0bf99b 755 if (install) {
e6b5ed24 756 rt->rt_state |= RTS_CHGRT;
7c0bf99b
SL
757 rt->rt_retry = EXPIRE_TIME/TIMER_RATE;
758 }
759}
760
761/*
762 * Delete a routing table entry.
763 */
764rtdelete(rt)
765 struct rt_entry *rt;
766{
77df73ca 767 log("delete", rt);
7c0bf99b 768 if (install)
3cec0c76 769 if (ioctl(s, SIOCDELRT, (char *)&rt->rt_rt) &&
7c0bf99b 770 errno == EBUSY)
e6b5ed24 771 rt->rt_state |= RTS_DELRT;
7c0bf99b
SL
772 remque(rt);
773 free((char *)rt);
774}
775
7c0bf99b
SL
776log(operation, rt)
777 char *operation;
778 struct rt_entry *rt;
779{
780 time_t t = time(0);
781 struct sockaddr_in *dst, *gate;
e6b5ed24 782 static struct bits {
7c0bf99b
SL
783 int t_bits;
784 char *t_name;
e6b5ed24 785 } flagbits[] = {
7c0bf99b
SL
786 { RTF_UP, "UP" },
787 { RTF_DIRECT, "DIRECT" },
788 { RTF_HOST, "HOST" },
e6b5ed24
SL
789 { 0 }
790 }, statebits[] = {
791 { RTS_DELRT, "DELETE" },
792 { RTS_CHGRT, "CHANGE" },
98c1da1f 793 { RTS_HIDDEN, "HIDDEN" },
7c0bf99b
SL
794 { 0 }
795 };
e6b5ed24 796 register struct bits *p;
7c0bf99b
SL
797 register int first;
798 char *cp;
799
77df73ca
SL
800 if (trace == 0)
801 return;
7c0bf99b
SL
802 printf("%s ", operation);
803 dst = (struct sockaddr_in *)&rt->rt_dst;
804 gate = (struct sockaddr_in *)&rt->rt_gateway;
e6b5ed24 805 printf("dst %x, router %x, metric %d, flags",
7c0bf99b 806 dst->sin_addr, gate->sin_addr, rt->rt_metric);
e6b5ed24
SL
807 cp = " %s";
808 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
7c0bf99b
SL
809 if ((rt->rt_flags & p->t_bits) == 0)
810 continue;
811 printf(cp, p->t_name);
812 if (first) {
813 cp = "|%s";
814 first = 0;
815 }
816 }
e6b5ed24
SL
817 printf(" state");
818 cp = " %s";
819 for (first = 1, p = statebits; p->t_bits > 0; p++) {
820 if ((rt->rt_state & p->t_bits) == 0)
821 continue;
822 printf(cp, p->t_name);
823 if (first) {
824 cp = "|%s";
825 first = 0;
826 }
827 }
7c0bf99b
SL
828 putchar('\n');
829}
77df73ca 830
3cec0c76
SL
831/*
832 * Find the interface with address "addr".
833 */
77df73ca
SL
834struct ifnet *
835if_ifwithaddr(addr)
836 struct sockaddr *addr;
837{
838 register struct ifnet *ifp;
839
840#define same(a1, a2) \
841 (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
842 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
843 if (ifp->if_addr.sa_family != addr->sa_family)
844 continue;
845 if (same(&ifp->if_addr, addr))
846 break;
847 if ((ifp->if_flags & IFF_BROADCAST) &&
848 same(&ifp->if_broadaddr, addr))
849 break;
850 }
851 return (ifp);
852#undef same
853}
854
3cec0c76
SL
855/*
856 * Find the interface with network imbedded in
857 * the sockaddr "addr". Must use per-af routine
858 * look for match.
859 */
77df73ca
SL
860struct ifnet *
861if_ifwithnet(addr)
862 register struct sockaddr *addr;
863{
864 register struct ifnet *ifp;
865 register int af = addr->sa_family;
866 register int (*netmatch)();
867
868 if (af >= AF_MAX)
869 return (0);
870 netmatch = afswitch[af].af_netmatch;
871 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
872 if (af != ifp->if_addr.sa_family)
873 continue;
874 if ((*netmatch)(addr, &ifp->if_addr))
875 break;
876 }
877 return (ifp);
878}
879
3cec0c76
SL
880/*
881 * Formulate an Internet address.
882 */
77df73ca
SL
883struct in_addr
884if_makeaddr(net, host)
885 int net, host;
886{
887 u_long addr;
888
889 if (net < 128)
890 addr = (net << 24) | host;
891 else if (net < 65536)
892 addr = (net << 16) | host;
893 else
894 addr = (net << 8) | host;
895#ifdef vax
896 addr = htonl(addr);
897#endif
898 return (*(struct in_addr *)&addr);
899}