t40c term[] count fix
[minix.git] / commands / irdpd / irdpd.c
blob2061ee95b0ca60f69b1e8e3678ae0984d5116650
1 /* irdpd 1.10 - Internet router discovery protocol daemon.
2 * Author: Kees J. Bot
3 * 28 May 1994
4 * Activily solicitate or passively look for routers.
5 * Based heavily on its forerunners, the irdp_sol and rip2icmp daemons by
6 * Philip Homburg.
7 */
8 #define nil 0
9 #include <sys/types.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <limits.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #include <sys/ioctl.h>
21 #include <sys/asynchio.h>
22 #include <net/hton.h>
23 #include <net/netlib.h>
24 #include <net/gen/in.h>
25 #include <net/gen/ip_hdr.h>
26 #include <net/gen/icmp.h>
27 #include <net/gen/icmp_hdr.h>
28 #include <net/gen/ip_io.h>
29 #include <net/gen/inet.h>
30 #include <net/gen/netdb.h>
31 #include <net/gen/oneCsum.h>
32 #include <net/gen/socket.h>
33 #include <net/gen/udp.h>
34 #include <net/gen/udp_hdr.h>
35 #include <net/gen/udp_io.h>
37 #define MAX_SOLICITATIONS 3 /* # router solicitations. */
38 #define SOLICITATION_INTERVAL 3 /* Secs between solicitate retries. */
39 #define DEST_TO (10*60) /* 10 minutes */
40 #define NEW_ROUTE (5*60) /* 5 minutes */
41 #define DANGER (2*60) /* Nearing a advert timeout? */
42 #define DEAD_TO (24L*60*60) /* 24 hours */
43 #define DEAD_PREF 0x80000000L /* From RFC 1256 */
44 #define MaxAdvertisementInterval (DEST_TO/2) /* Chosen to jive with RIP */
45 #define AdvertisementLifetime DEST_TO
46 #define PRIO_OFF_DEF -1024
47 #define RIP_REPLY 2
49 /* It's now or never. */
50 #define IMMEDIATELY ((time_t) ((time_t) -1 < 0 ? LONG_MIN : 0))
51 #define NEVER ((time_t) ((time_t) -1 < 0 ? LONG_MAX : ULONG_MAX))
53 #if !__minix_vmd
54 /* Standard Minix needs to choose between router discovery and RIP info. */
55 int do_rdisc= 1;
56 int do_rip= 0;
57 #else
58 /* VMD Minix can do both at once. */
59 #define do_rdisc 1
60 #define do_rip 1
61 #endif
63 int rip_fd; /* For incoming RIP packet. */
64 int irdp_fd; /* Receive or transmit IRDP packets. */
66 char *udp_device; /* UDP device to use. */
67 char *ip_device; /* IP device to use. */
69 int priority_offset; /* Offset to make my routes less preferred. */
71 int bcast= 0; /* Broadcast adverts to all. */
72 int debug= 0;
74 char rip_buf[8192]; /* Incoming RIP packet buffer. */
75 char irdp_buf[1024]; /* IRDP buffer. */
77 typedef struct routeinfo
79 u8_t command;
80 u8_t version;
81 u16_t zero1;
82 struct routedata {
83 u16_t family;
84 u16_t zero2;
85 u32_t ip_addr;
86 u32_t zero3, zero4;
87 u32_t metric;
88 } data[1];
89 } routeinfo_t;
91 typedef struct table
93 ipaddr_t tab_gw;
94 i32_t tab_pref;
95 time_t tab_time;
96 } table_t;
98 table_t *table; /* Collected table of routing info. */
99 size_t table_size;
101 int sol_retries= MAX_SOLICITATIONS;
102 time_t next_sol= IMMEDIATELY;
103 time_t next_advert= NEVER;
104 time_t router_advert_valid= IMMEDIATELY;
105 time_t now;
107 void report(const char *label)
108 /* irdpd: /dev/hd0: Device went up in flames */
110 fprintf(stderr, "irdpd: %s: %s\n", label, strerror(errno));
113 void fatal(const char *label)
114 /* irdpd: /dev/house: Taking this with it */
116 report(label);
117 exit(1);
120 #if DEBUG
121 char *addr2name(ipaddr_t host)
122 /* Translate an IP address to a printable name. */
124 struct hostent *hostent;
126 hostent= gethostbyaddr((char *) &host, sizeof(host), AF_INET);
127 return hostent == nil ? inet_ntoa(host) : hostent->h_name;
129 #else
130 #define addr2name(host) inet_ntoa(host)
131 #endif
133 void print_table(void)
134 /* Show the collected routing table. */
136 int i;
137 table_t *ptab;
138 struct tm *tm;
140 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
141 if (ptab->tab_time < now - DEAD_TO) continue;
143 tm= localtime(&ptab->tab_time);
144 printf("%-40s %6ld %02d:%02d:%02d\n",
145 addr2name(ptab->tab_gw),
146 (long) ptab->tab_pref,
147 tm->tm_hour, tm->tm_min, tm->tm_sec);
151 void advertize(ipaddr_t host)
152 /* Send a router advert to a host. */
154 char *buf, *data;
155 ip_hdr_t *ip_hdr;
156 icmp_hdr_t *icmp_hdr;
157 int i;
158 table_t *ptab;
160 buf= malloc(sizeof(*ip_hdr) + offsetof(icmp_hdr_t, ih_dun.uhd_data)
161 + table_size * (sizeof(ipaddr_t) + sizeof(u32_t)));
162 if (buf == nil) fatal("heap error");
164 ip_hdr= (ip_hdr_t *) buf;
165 icmp_hdr= (icmp_hdr_t *) (ip_hdr + 1);
167 ip_hdr->ih_vers_ihl= 0x45;
168 ip_hdr->ih_dst= host;
170 icmp_hdr->ih_type= ICMP_TYPE_ROUTER_ADVER;
171 icmp_hdr->ih_code= 0;
172 icmp_hdr->ih_hun.ihh_ram.iram_na= 0;
173 icmp_hdr->ih_hun.ihh_ram.iram_aes= 2;
174 icmp_hdr->ih_hun.ihh_ram.iram_lt= htons(AdvertisementLifetime);
175 data= (char *) icmp_hdr->ih_dun.uhd_data;
177 /* Collect gateway entries from the table. */
178 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
179 if (ptab->tab_time < now - DEAD_TO) continue;
181 icmp_hdr->ih_hun.ihh_ram.iram_na++;
182 if (ptab->tab_time < now - DEST_TO) ptab->tab_pref= DEAD_PREF;
183 * (ipaddr_t *) data= ptab->tab_gw;
184 data+= sizeof(ipaddr_t);
185 * (i32_t *) data= htonl(ptab->tab_pref);
186 data+= sizeof(i32_t);
188 icmp_hdr->ih_chksum= 0;
189 icmp_hdr->ih_chksum= ~oneC_sum(0, icmp_hdr, data - (char *) icmp_hdr);
191 if (icmp_hdr->ih_hun.ihh_ram.iram_na > 0) {
192 /* Send routing info. */
194 if (debug) {
195 printf("Routing table send to %s:\n", addr2name(host));
196 print_table();
199 if (write(irdp_fd, buf, data - buf) < 0) {
200 (errno == EIO ? fatal : report)(ip_device);
203 free(buf);
206 void time_functions(void)
207 /* Perform time dependend functions: router solicitation, router advert. */
209 if (now >= next_sol) {
210 char buf[sizeof(ip_hdr_t) + 8];
211 ip_hdr_t *ip_hdr;
212 icmp_hdr_t *icmp_hdr;
214 if (sol_retries == 0) {
215 /* Stop soliciting. */
216 next_sol= NEVER;
217 #if !__minix_vmd
218 /* Switch to RIP if no router responded. */
219 if (table_size == 0) {
220 do_rip= 1;
221 do_rdisc= 0;
223 #endif
224 return;
227 /* Broadcast a router solicitation to find a router. */
228 ip_hdr= (ip_hdr_t *) buf;
229 icmp_hdr= (icmp_hdr_t *) (ip_hdr + 1);
231 ip_hdr->ih_vers_ihl= 0x45;
232 #ifdef __NBSD_LIBC
233 ip_hdr->ih_dst= htonl(0xFFFFFFFFL);
234 #else
235 ip_hdr->ih_dst= HTONL(0xFFFFFFFFL);
236 #endif
237 icmp_hdr->ih_type= ICMP_TYPE_ROUTE_SOL;
238 icmp_hdr->ih_code= 0;
239 icmp_hdr->ih_chksum= 0;
240 icmp_hdr->ih_hun.ihh_unused= 0;
241 icmp_hdr->ih_chksum= ~oneC_sum(0, icmp_hdr, 8);
243 if (debug) printf("Broadcasting router solicitation\n");
245 if (write(irdp_fd, buf, sizeof(buf)) < 0)
246 fatal("sending router solicitation failed");
248 /* Schedule the next packet. */
249 next_sol= now + SOLICITATION_INTERVAL;
251 sol_retries--;
254 if (now >= next_advert) {
255 /* Advertize routes to the local host (normally), or
256 * broadcast them (to keep bad hosts up.)
259 #ifdef __NBSD_LIBC
260 advertize(bcast ? htonl(0xFFFFFFFFL) : htonl(0x7F000001L));
261 #else
262 advertize(bcast ? HTONL(0xFFFFFFFFL) : HTONL(0x7F000001L));
263 #endif
264 next_advert= now + MaxAdvertisementInterval;
265 #if !__minix_vmd
266 /* Make sure we are listening to RIP now. */
267 do_rip= 1;
268 do_rdisc= 0;
269 #endif
273 void add_gateway(ipaddr_t host, i32_t pref)
274 /* Add a router with given address and preference to the routing table. */
276 table_t *oldest, *ptab;
277 int i;
279 /* Look for the host, or select the oldest entry. */
280 oldest= nil;
281 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
282 if (ptab->tab_gw == host) break;
284 if (oldest == nil || ptab->tab_time < oldest->tab_time)
285 oldest= ptab;
288 /* Don't evict the oldest if it is still valid. */
289 if (oldest != nil && oldest->tab_time >= now - DEST_TO) oldest= nil;
291 /* Expand the table? */
292 if (i == table_size && oldest == nil) {
293 table_size++;
294 table= realloc(table, table_size * sizeof(*table));
295 if (table == nil) fatal("heap error");
296 oldest= &table[table_size - 1];
299 if (oldest != nil) {
300 ptab= oldest;
301 ptab->tab_gw= host;
302 ptab->tab_pref= DEAD_PREF;
305 /* Replace an entry if the new one looks more promising. */
306 if (pref >= ptab->tab_pref || ptab->tab_time <= now - NEW_ROUTE) {
307 ptab->tab_pref= pref;
308 ptab->tab_time= now;
312 void rip_incoming(ssize_t n)
313 /* Use a RIP packet to add to the router table. (RIP packets are really for
314 * between routers, but often it is the only information around.)
317 udp_io_hdr_t *udp_io_hdr;
318 u32_t default_dist;
319 i32_t pref;
320 routeinfo_t *routeinfo;
321 struct routedata *data, *end;
323 /* We don't care about RIP packets when there are router adverts. */
324 if (now + MaxAdvertisementInterval < router_advert_valid) return;
326 udp_io_hdr= (udp_io_hdr_t *) rip_buf;
327 if (udp_io_hdr->uih_data_len != n - sizeof(*udp_io_hdr)) {
328 if (debug) printf("Bad sized route packet (discarded)\n");
329 return;
331 routeinfo= (routeinfo_t *) (rip_buf + sizeof(*udp_io_hdr)
332 + udp_io_hdr->uih_ip_opt_len);
334 if (routeinfo->command != RIP_REPLY) {
335 if (debug) {
336 printf("RIP-%d packet command %d ignored\n",
337 routeinfo->version, routeinfo->command);
339 return;
342 /* Look for a default route, the route to the gateway. */
343 end= (struct routedata *) (rip_buf + n);
344 default_dist= (u32_t) -1;
345 for (data= routeinfo->data; data < end; data++) {
346 if (ntohs(data->family) != AF_INET || data->ip_addr != 0)
347 continue;
348 default_dist= ntohl(data->metric);
349 if (default_dist >= 256) {
350 if (debug) {
351 printf("Strange metric %lu\n",
352 (unsigned long) default_dist);
356 pref= default_dist >= 256 ? 1 : 512 - default_dist;
357 pref+= priority_offset;
359 /* Add the gateway to the table with the calculated preference. */
360 add_gateway(udp_io_hdr->uih_src_addr, pref);
362 if (debug) {
363 printf("Routing table after RIP-%d packet from %s:\n",
364 routeinfo->version,
365 addr2name(udp_io_hdr->uih_src_addr));
366 print_table();
369 /* Start advertizing. */
370 if (next_advert == NEVER) next_advert= IMMEDIATELY;
373 void irdp_incoming(ssize_t n)
374 /* Look for router solicitations and router advertisements. The solicitations
375 * are probably from other irdpd daemons, we answer them if we do not expect
376 * a real router to answer. The advertisements cause this daemon to shut up.
379 ip_hdr_t *ip_hdr;
380 icmp_hdr_t *icmp_hdr;
381 int ip_hdr_len;
382 char *data;
383 int i;
384 int router;
385 ipaddr_t addr;
386 i32_t pref;
387 time_t valid;
389 ip_hdr= (ip_hdr_t *) irdp_buf;
390 ip_hdr_len= (ip_hdr->ih_vers_ihl & IH_IHL_MASK) << 2;
391 if (n < ip_hdr_len + 8) {
392 if (debug) printf("Bad sized ICMP (discarded)\n");
393 return;
396 icmp_hdr= (icmp_hdr_t *)(irdp_buf + ip_hdr_len);
398 /* Did I send this myself? */
399 if (ip_hdr->ih_src == ip_hdr->ih_dst) return;
400 if ((htonl(ip_hdr->ih_src) & 0xFF000000L) == 0x7F000000L) return;
402 if (icmp_hdr->ih_type != ICMP_TYPE_ROUTER_ADVER) return;
404 /* Incoming router advertisement, the kind of packet the TCP/IP task
405 * is very happy with. No need to solicit further.
407 sol_retries= 0;
409 /* Add router info to our table. Also see if the packet really came
410 * from a router. If so then we can go dormant for the lifetime of
411 * the ICMP.
413 router= 0;
414 data= (char *) icmp_hdr->ih_dun.uhd_data;
415 for (i= 0; i < icmp_hdr->ih_hun.ihh_ram.iram_na; i++) {
416 addr= * (ipaddr_t *) data;
417 data+= sizeof(ipaddr_t);
418 pref= htonl(* (i32_t *) data);
419 data+= sizeof(i32_t);
421 if (addr == ip_hdr->ih_src) {
422 /* The sender is in the routing table! */
423 router= 1;
425 add_gateway(addr, pref);
428 valid= now + ntohs(icmp_hdr->ih_hun.ihh_ram.iram_lt);
429 if (router) router_advert_valid= valid;
431 /* Restart advertizing close to the timeout of the advert. (No more
432 * irdpd adverts if the router stays alive.)
434 if (router || next_advert > valid - DANGER)
435 next_advert= valid - DANGER;
437 if (debug) {
438 printf("Routing table after advert received from %s:\n",
439 addr2name(ip_hdr->ih_src));
440 print_table();
441 if (router) {
442 struct tm *tm= localtime(&router_advert_valid);
443 printf(
444 "This router advert is valid until %02d:%02d:%02d\n",
445 tm->tm_hour, tm->tm_min, tm->tm_sec);
450 void sig_handler(int sig)
451 /* A signal changes the debug level. */
453 switch (sig) {
454 case SIGUSR1: debug++; break;
455 case SIGUSR2: debug= 0; break;
459 void usage(void)
461 fprintf(stderr,
462 "Usage: irdpd [-bd] [-U udp-device] [-I ip-device] [-o priority-offset]\n");
463 exit(1);
466 int main(int argc, char **argv)
468 int i;
469 struct servent *service;
470 udpport_t route_port;
471 nwio_udpopt_t udpopt;
472 nwio_ipopt_t ipopt;
473 asynchio_t asyn;
474 time_t timeout;
475 struct timeval tv;
476 struct sigaction sa;
477 char *offset_arg, *offset_end;
478 long arg;
480 udp_device= ip_device= nil;
481 offset_arg= nil;
483 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
484 char *p= argv[i] + 1;
486 if (p[0] == '-' && p[1] == 0) { i++; break; }
488 while (*p != 0) {
489 switch (*p++) {
490 case 'U':
491 if (udp_device != nil) usage();
492 if (*p == 0) {
493 if (++i == argc) usage();
494 p= argv[i];
496 udp_device= p;
497 p= "";
498 break;
499 case 'I':
500 if (ip_device != nil) usage();
501 if (*p == 0) {
502 if (++i == argc) usage();
503 p= argv[i];
505 ip_device= p;
506 p= "";
507 break;
508 case 'o':
509 if (offset_arg != nil) usage();
510 if (*p == 0) {
511 if (++i == argc) usage();
512 p= argv[i];
514 offset_arg= p;
515 p= "";
516 break;
517 case 'b':
518 bcast= 1;
519 break;
520 case 's':
521 /*obsolete*/
522 break;
523 case 'd':
524 debug= 1;
525 break;
526 default:
527 usage();
531 if (i != argc) usage();
533 /* Debug level signals. */
534 sa.sa_handler= sig_handler;
535 sigemptyset(&sa.sa_mask);
536 sa.sa_flags= 0;
537 sigaction(SIGUSR1, &sa, nil);
538 sigaction(SIGUSR2, &sa, nil);
540 if (udp_device == nil && (udp_device= getenv("UDP_DEVICE")) == nil)
541 udp_device= UDP_DEVICE;
543 if (ip_device == nil && (ip_device= getenv("IP_DEVICE")) == nil)
544 ip_device= IP_DEVICE;
546 if (offset_arg == nil) {
547 priority_offset= PRIO_OFF_DEF;
548 } else {
549 arg= strtol(offset_arg, &offset_end, 0);
550 if (*offset_end != 0 || (priority_offset= arg) != arg) usage();
553 if ((service= getservbyname("route", "udp")) == nil) {
554 fprintf(stderr,
555 "irdpd: unable to look up the port number for the 'route' service\n");
556 exit(1);
559 route_port= (udpport_t) service->s_port;
561 if ((rip_fd= open(udp_device, O_RDWR)) < 0) fatal(udp_device);
563 udpopt.nwuo_flags= NWUO_COPY | NWUO_LP_SET | NWUO_DI_LOC
564 | NWUO_EN_BROAD | NWUO_RP_SET | NWUO_RA_ANY | NWUO_RWDATALL
565 | NWUO_DI_IPOPT;
566 udpopt.nwuo_locport= route_port;
567 udpopt.nwuo_remport= route_port;
568 if (ioctl(rip_fd, NWIOSUDPOPT, &udpopt) < 0)
569 fatal("setting UDP options failed");
571 if ((irdp_fd= open(ip_device, O_RDWR)) < 0) fatal(ip_device);
573 ipopt.nwio_flags= NWIO_COPY | NWIO_EN_LOC | NWIO_EN_BROAD
574 | NWIO_REMANY | NWIO_PROTOSPEC
575 | NWIO_HDR_O_SPEC | NWIO_RWDATALL;
576 ipopt.nwio_tos= 0;
577 ipopt.nwio_ttl= 1;
578 ipopt.nwio_df= 0;
579 ipopt.nwio_hdropt.iho_opt_siz= 0;
580 #ifdef __NBSD_LIBC
581 ipopt.nwio_rem= htonl(0xFFFFFFFFL);
582 #else
583 ipopt.nwio_rem= HTONL(0xFFFFFFFFL);
584 #endif
585 ipopt.nwio_proto= IPPROTO_ICMP;
587 if (ioctl(irdp_fd, NWIOSIPOPT, &ipopt) < 0)
588 fatal("can't configure ICMP channel");
590 asyn_init(&asyn);
592 while (1) {
593 ssize_t r;
595 if (do_rip) {
596 /* Try a RIP read. */
597 r= asyn_read(&asyn, rip_fd, rip_buf, sizeof(rip_buf));
598 if (r < 0) {
599 if (errno == EIO) fatal(udp_device);
600 if (errno != EINPROGRESS) report(udp_device);
601 } else {
602 now= time(nil);
603 rip_incoming(r);
607 if (do_rdisc) {
608 /* Try an IRDP read. */
609 r= asyn_read(&asyn, irdp_fd, irdp_buf,
610 sizeof(irdp_buf));
611 if (r < 0) {
612 if (errno == EIO) fatal(ip_device);
613 if (errno != EINPROGRESS) report(ip_device);
614 } else {
615 now= time(nil);
616 irdp_incoming(r);
619 fflush(stdout);
621 /* Compute the next wakeup call. */
622 timeout= next_sol < next_advert ? next_sol : next_advert;
624 /* Wait for a RIP or IRDP packet or a timeout. */
625 tv.tv_sec= timeout;
626 tv.tv_usec= 0;
627 if (asyn_wait(&asyn, 0, timeout == NEVER ? nil : &tv) < 0) {
628 /* Timeout? */
629 if (errno != EINTR && errno != EAGAIN)
630 fatal("asyn_wait()");
631 now= time(nil);
632 time_functions();