Improve the process for GNU tools
[minix3.git] / minix / commands / irdpd / irdpd.c
blobfbd88c5858ccdd5606b7c9a39b34bd356dfff31e
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 <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>
36 #include <arpa/inet.h>
38 #define MAX_SOLICITATIONS 3 /* # router solicitations. */
39 #define SOLICITATION_INTERVAL 3 /* Secs between solicitate retries. */
40 #define DEST_TO (10*60) /* 10 minutes */
41 #define NEW_ROUTE (5*60) /* 5 minutes */
42 #define DANGER (2*60) /* Nearing a advert timeout? */
43 #define DEAD_TO (24L*60*60) /* 24 hours */
44 #define DEAD_PREF 0x80000000L /* From RFC 1256 */
45 #define MaxAdvertisementInterval (DEST_TO/2) /* Chosen to jive with RIP */
46 #define AdvertisementLifetime DEST_TO
47 #define PRIO_OFF_DEF -1024
48 #define RIP_REPLY 2
50 /* It's now or never. */
51 #define IMMEDIATELY ((time_t) ((time_t) -1 < 0 ? LONG_MIN : 0))
52 #define NEVER ((time_t) ((time_t) -1 < 0 ? LONG_MAX : ULONG_MAX))
54 #if !__minix_vmd
55 /* Standard Minix needs to choose between router discovery and RIP info. */
56 int do_rdisc= 1;
57 int do_rip= 0;
58 #else
59 /* VMD Minix can do both at once. */
60 #define do_rdisc 1
61 #define do_rip 1
62 #endif
64 int rip_fd; /* For incoming RIP packet. */
65 int irdp_fd; /* Receive or transmit IRDP packets. */
67 char *udp_device; /* UDP device to use. */
68 char *ip_device; /* IP device to use. */
70 int priority_offset; /* Offset to make my routes less preferred. */
72 int bcast= 0; /* Broadcast adverts to all. */
73 int debug= 0;
75 char rip_buf[8192]; /* Incoming RIP packet buffer. */
76 char irdp_buf[1024]; /* IRDP buffer. */
78 typedef struct routeinfo
80 u8_t command;
81 u8_t version;
82 u16_t zero1;
83 struct routedata {
84 u16_t family;
85 u16_t zero2;
86 u32_t ip_addr;
87 u32_t zero3, zero4;
88 u32_t metric;
89 } data[1];
90 } routeinfo_t;
92 typedef struct table
94 ipaddr_t tab_gw;
95 i32_t tab_pref;
96 time_t tab_time;
97 } table_t;
99 table_t *table; /* Collected table of routing info. */
100 size_t table_size;
102 int sol_retries= MAX_SOLICITATIONS;
103 time_t next_sol= IMMEDIATELY;
104 time_t next_advert= NEVER;
105 time_t router_advert_valid= IMMEDIATELY;
106 time_t now;
108 void report(const char *label)
109 /* irdpd: /dev/hd0: Device went up in flames */
111 fprintf(stderr, "irdpd: %s: %s\n", label, strerror(errno));
114 void fatal(const char *label)
115 /* irdpd: /dev/house: Taking this with it */
117 report(label);
118 exit(1);
121 #if DEBUG
122 char *addr2name(ipaddr_t host)
123 /* Translate an IP address to a printable name. */
125 struct hostent *hostent;
127 hostent= gethostbyaddr((char *) &host, sizeof(host), AF_INET);
128 return hostent == nil ? inet_ntoa(host) : hostent->h_name;
130 #else
131 #define addr2name(host) inet_ntoa(*(struct in_addr *)&(host))
132 #endif
134 void print_table(void)
135 /* Show the collected routing table. */
137 int i;
138 table_t *ptab;
139 struct tm *tm;
141 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
142 if (ptab->tab_time < now - DEAD_TO) continue;
144 tm= localtime(&ptab->tab_time);
145 printf("%-40s %6ld %02d:%02d:%02d\n",
146 addr2name(ptab->tab_gw),
147 (long) ptab->tab_pref,
148 tm->tm_hour, tm->tm_min, tm->tm_sec);
152 void advertize(ipaddr_t host)
153 /* Send a router advert to a host. */
155 char *buf, *data;
156 ip_hdr_t *ip_hdr;
157 icmp_hdr_t *icmp_hdr;
158 int i;
159 table_t *ptab;
161 buf= malloc(sizeof(*ip_hdr) + offsetof(icmp_hdr_t, ih_dun.uhd_data)
162 + table_size * (sizeof(ipaddr_t) + sizeof(u32_t)));
163 if (buf == nil) fatal("heap error");
165 ip_hdr= (ip_hdr_t *) buf;
166 icmp_hdr= (icmp_hdr_t *) (ip_hdr + 1);
168 ip_hdr->ih_vers_ihl= 0x45;
169 ip_hdr->ih_dst= host;
171 icmp_hdr->ih_type= ICMP_TYPE_ROUTER_ADVER;
172 icmp_hdr->ih_code= 0;
173 icmp_hdr->ih_hun.ihh_ram.iram_na= 0;
174 icmp_hdr->ih_hun.ihh_ram.iram_aes= 2;
175 icmp_hdr->ih_hun.ihh_ram.iram_lt= htons(AdvertisementLifetime);
176 data= (char *) icmp_hdr->ih_dun.uhd_data;
178 /* Collect gateway entries from the table. */
179 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
180 if (ptab->tab_time < now - DEAD_TO) continue;
182 icmp_hdr->ih_hun.ihh_ram.iram_na++;
183 if (ptab->tab_time < now - DEST_TO) ptab->tab_pref= DEAD_PREF;
184 * (ipaddr_t *) data= ptab->tab_gw;
185 data+= sizeof(ipaddr_t);
186 * (i32_t *) data= htonl(ptab->tab_pref);
187 data+= sizeof(i32_t);
189 icmp_hdr->ih_chksum= 0;
190 icmp_hdr->ih_chksum= ~oneC_sum(0, icmp_hdr, data - (char *) icmp_hdr);
192 if (icmp_hdr->ih_hun.ihh_ram.iram_na > 0) {
193 /* Send routing info. */
195 if (debug) {
196 printf("Routing table send to %s:\n", addr2name(host));
197 print_table();
200 if (write(irdp_fd, buf, data - buf) < 0) {
201 (errno == EIO ? fatal : report)(ip_device);
204 free(buf);
207 void time_functions(void)
208 /* Perform time dependend functions: router solicitation, router advert. */
210 if (now >= next_sol) {
211 char buf[sizeof(ip_hdr_t) + 8];
212 ip_hdr_t *ip_hdr;
213 icmp_hdr_t *icmp_hdr;
215 if (sol_retries == 0) {
216 /* Stop soliciting. */
217 next_sol= NEVER;
218 #if !__minix_vmd
219 /* Switch to RIP if no router responded. */
220 if (table_size == 0) {
221 do_rip= 1;
222 do_rdisc= 0;
224 #endif
225 return;
228 /* Broadcast a router solicitation to find a router. */
229 ip_hdr= (ip_hdr_t *) buf;
230 icmp_hdr= (icmp_hdr_t *) (ip_hdr + 1);
232 ip_hdr->ih_vers_ihl= 0x45;
233 ip_hdr->ih_dst= htonl(0xFFFFFFFFL);
234 icmp_hdr->ih_type= ICMP_TYPE_ROUTE_SOL;
235 icmp_hdr->ih_code= 0;
236 icmp_hdr->ih_chksum= 0;
237 icmp_hdr->ih_hun.ihh_unused= 0;
238 icmp_hdr->ih_chksum= ~oneC_sum(0, icmp_hdr, 8);
240 if (debug) printf("Broadcasting router solicitation\n");
242 if (write(irdp_fd, buf, sizeof(buf)) < 0)
243 fatal("sending router solicitation failed");
245 /* Schedule the next packet. */
246 next_sol= now + SOLICITATION_INTERVAL;
248 sol_retries--;
251 if (now >= next_advert) {
252 /* Advertize routes to the local host (normally), or
253 * broadcast them (to keep bad hosts up.)
256 advertize(bcast ? htonl(0xFFFFFFFFL) : htonl(0x7F000001L));
257 next_advert= now + MaxAdvertisementInterval;
258 #if !__minix_vmd
259 /* Make sure we are listening to RIP now. */
260 do_rip= 1;
261 do_rdisc= 0;
262 #endif
266 void add_gateway(ipaddr_t host, i32_t pref)
267 /* Add a router with given address and preference to the routing table. */
269 table_t *oldest, *ptab;
270 int i;
272 /* Look for the host, or select the oldest entry. */
273 oldest= nil;
274 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
275 if (ptab->tab_gw == host) break;
277 if (oldest == nil || ptab->tab_time < oldest->tab_time)
278 oldest= ptab;
281 /* Don't evict the oldest if it is still valid. */
282 if (oldest != nil && oldest->tab_time >= now - DEST_TO) oldest= nil;
284 /* Expand the table? */
285 if (i == table_size && oldest == nil) {
286 table_size++;
287 table= realloc(table, table_size * sizeof(*table));
288 if (table == nil) fatal("heap error");
289 oldest= &table[table_size - 1];
292 if (oldest != nil) {
293 ptab= oldest;
294 ptab->tab_gw= host;
295 ptab->tab_pref= DEAD_PREF;
298 /* Replace an entry if the new one looks more promising. */
299 if (pref >= ptab->tab_pref || ptab->tab_time <= now - NEW_ROUTE) {
300 ptab->tab_pref= pref;
301 ptab->tab_time= now;
305 void rip_incoming(ssize_t n)
306 /* Use a RIP packet to add to the router table. (RIP packets are really for
307 * between routers, but often it is the only information around.)
310 udp_io_hdr_t *udp_io_hdr;
311 u32_t default_dist;
312 i32_t pref;
313 routeinfo_t *routeinfo;
314 struct routedata *data, *end;
316 /* We don't care about RIP packets when there are router adverts. */
317 if (now + MaxAdvertisementInterval < router_advert_valid) return;
319 udp_io_hdr= (udp_io_hdr_t *) rip_buf;
320 if (udp_io_hdr->uih_data_len != n - sizeof(*udp_io_hdr)) {
321 if (debug) printf("Bad sized route packet (discarded)\n");
322 return;
324 routeinfo= (routeinfo_t *) (rip_buf + sizeof(*udp_io_hdr)
325 + udp_io_hdr->uih_ip_opt_len);
327 if (routeinfo->command != RIP_REPLY) {
328 if (debug) {
329 printf("RIP-%d packet command %d ignored\n",
330 routeinfo->version, routeinfo->command);
332 return;
335 /* Look for a default route, the route to the gateway. */
336 end= (struct routedata *) (rip_buf + n);
337 default_dist= (u32_t) -1;
338 for (data= routeinfo->data; data < end; data++) {
339 if (ntohs(data->family) != AF_INET || data->ip_addr != 0)
340 continue;
341 default_dist= ntohl(data->metric);
342 if (default_dist >= 256) {
343 if (debug) {
344 printf("Strange metric %lu\n",
345 (unsigned long) default_dist);
349 pref= default_dist >= 256 ? 1 : 512 - default_dist;
350 pref+= priority_offset;
352 /* Add the gateway to the table with the calculated preference. */
353 add_gateway(udp_io_hdr->uih_src_addr, pref);
355 if (debug) {
356 printf("Routing table after RIP-%d packet from %s:\n",
357 routeinfo->version,
358 addr2name(udp_io_hdr->uih_src_addr));
359 print_table();
362 /* Start advertizing. */
363 if (next_advert == NEVER) next_advert= IMMEDIATELY;
366 void irdp_incoming(ssize_t n)
367 /* Look for router solicitations and router advertisements. The solicitations
368 * are probably from other irdpd daemons, we answer them if we do not expect
369 * a real router to answer. The advertisements cause this daemon to shut up.
372 ip_hdr_t *ip_hdr;
373 icmp_hdr_t *icmp_hdr;
374 int ip_hdr_len;
375 char *data;
376 int i;
377 int router;
378 ipaddr_t addr;
379 i32_t pref;
380 time_t valid;
382 ip_hdr= (ip_hdr_t *) irdp_buf;
383 ip_hdr_len= (ip_hdr->ih_vers_ihl & IH_IHL_MASK) << 2;
384 if (n < ip_hdr_len + 8) {
385 if (debug) printf("Bad sized ICMP (discarded)\n");
386 return;
389 icmp_hdr= (icmp_hdr_t *)(irdp_buf + ip_hdr_len);
391 /* Did I send this myself? */
392 if (ip_hdr->ih_src == ip_hdr->ih_dst) return;
393 if ((htonl(ip_hdr->ih_src) & 0xFF000000L) == 0x7F000000L) return;
395 if (icmp_hdr->ih_type != ICMP_TYPE_ROUTER_ADVER) return;
397 /* Incoming router advertisement, the kind of packet the TCP/IP task
398 * is very happy with. No need to solicit further.
400 sol_retries= 0;
402 /* Add router info to our table. Also see if the packet really came
403 * from a router. If so then we can go dormant for the lifetime of
404 * the ICMP.
406 router= 0;
407 data= (char *) icmp_hdr->ih_dun.uhd_data;
408 for (i= 0; i < icmp_hdr->ih_hun.ihh_ram.iram_na; i++) {
409 addr= * (ipaddr_t *) data;
410 data+= sizeof(ipaddr_t);
411 pref= htonl(* (i32_t *) data);
412 data+= sizeof(i32_t);
414 if (addr == ip_hdr->ih_src) {
415 /* The sender is in the routing table! */
416 router= 1;
418 add_gateway(addr, pref);
421 valid= now + ntohs(icmp_hdr->ih_hun.ihh_ram.iram_lt);
422 if (router) router_advert_valid= valid;
424 /* Restart advertizing close to the timeout of the advert. (No more
425 * irdpd adverts if the router stays alive.)
427 if (router || next_advert > valid - DANGER)
428 next_advert= valid - DANGER;
430 if (debug) {
431 printf("Routing table after advert received from %s:\n",
432 addr2name(ip_hdr->ih_src));
433 print_table();
434 if (router) {
435 struct tm *tm= localtime(&router_advert_valid);
436 printf(
437 "This router advert is valid until %02d:%02d:%02d\n",
438 tm->tm_hour, tm->tm_min, tm->tm_sec);
443 void sig_handler(int sig)
444 /* A signal changes the debug level. */
446 switch (sig) {
447 case SIGUSR1: debug++; break;
448 case SIGUSR2: debug= 0; break;
452 void usage(void)
454 fprintf(stderr,
455 "Usage: irdpd [-bd] [-U udp-device] [-I ip-device] [-o priority-offset]\n");
456 exit(1);
459 int main(int argc, char **argv)
461 int i;
462 struct servent *service;
463 udpport_t route_port;
464 nwio_udpopt_t udpopt;
465 nwio_ipopt_t ipopt;
466 asynchio_t asyn;
467 time_t timeout;
468 struct timeval tv;
469 struct sigaction sa;
470 char *offset_arg, *offset_end;
471 long arg;
473 udp_device= ip_device= nil;
474 offset_arg= nil;
476 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
477 char *p= argv[i] + 1;
479 if (p[0] == '-' && p[1] == 0) { i++; break; }
481 while (*p != 0) {
482 switch (*p++) {
483 case 'U':
484 if (udp_device != nil) usage();
485 if (*p == 0) {
486 if (++i == argc) usage();
487 p= argv[i];
489 udp_device= p;
490 p= "";
491 break;
492 case 'I':
493 if (ip_device != nil) usage();
494 if (*p == 0) {
495 if (++i == argc) usage();
496 p= argv[i];
498 ip_device= p;
499 p= "";
500 break;
501 case 'o':
502 if (offset_arg != nil) usage();
503 if (*p == 0) {
504 if (++i == argc) usage();
505 p= argv[i];
507 offset_arg= p;
508 p= "";
509 break;
510 case 'b':
511 bcast= 1;
512 break;
513 case 's':
514 /*obsolete*/
515 break;
516 case 'd':
517 debug= 1;
518 break;
519 default:
520 usage();
524 if (i != argc) usage();
526 /* Debug level signals. */
527 sa.sa_handler= sig_handler;
528 sigemptyset(&sa.sa_mask);
529 sa.sa_flags= 0;
530 sigaction(SIGUSR1, &sa, nil);
531 sigaction(SIGUSR2, &sa, nil);
533 if (udp_device == nil && (udp_device= getenv("UDP_DEVICE")) == nil)
534 udp_device= UDP_DEVICE;
536 if (ip_device == nil && (ip_device= getenv("IP_DEVICE")) == nil)
537 ip_device= IP_DEVICE;
539 if (offset_arg == nil) {
540 priority_offset= PRIO_OFF_DEF;
541 } else {
542 arg= strtol(offset_arg, &offset_end, 0);
543 if (*offset_end != 0 || (priority_offset= arg) != arg) usage();
546 if ((service= getservbyname("route", "udp")) == nil) {
547 fprintf(stderr,
548 "irdpd: unable to look up the port number for the 'route' service\n");
549 exit(1);
552 route_port= (udpport_t) service->s_port;
554 if ((rip_fd= open(udp_device, O_RDWR)) < 0) fatal(udp_device);
556 udpopt.nwuo_flags= NWUO_COPY | NWUO_LP_SET | NWUO_DI_LOC
557 | NWUO_EN_BROAD | NWUO_RP_SET | NWUO_RA_ANY | NWUO_RWDATALL
558 | NWUO_DI_IPOPT;
559 udpopt.nwuo_locport= route_port;
560 udpopt.nwuo_remport= route_port;
561 if (ioctl(rip_fd, NWIOSUDPOPT, &udpopt) < 0)
562 fatal("setting UDP options failed");
564 if ((irdp_fd= open(ip_device, O_RDWR)) < 0) fatal(ip_device);
566 ipopt.nwio_flags= NWIO_COPY | NWIO_EN_LOC | NWIO_EN_BROAD
567 | NWIO_REMANY | NWIO_PROTOSPEC
568 | NWIO_HDR_O_SPEC | NWIO_RWDATALL;
569 ipopt.nwio_tos= 0;
570 ipopt.nwio_ttl= 1;
571 ipopt.nwio_df= 0;
572 ipopt.nwio_hdropt.iho_opt_siz= 0;
573 ipopt.nwio_rem= htonl(0xFFFFFFFFL);
574 ipopt.nwio_proto= IPPROTO_ICMP;
576 if (ioctl(irdp_fd, NWIOSIPOPT, &ipopt) < 0)
577 fatal("can't configure ICMP channel");
579 asyn_init(&asyn);
581 while (1) {
582 ssize_t r;
584 if (do_rip) {
585 /* Try a RIP read. */
586 r= asyn_read(&asyn, rip_fd, rip_buf, sizeof(rip_buf));
587 if (r < 0) {
588 if (errno == EIO) fatal(udp_device);
589 if (errno != EINPROGRESS) report(udp_device);
590 } else {
591 now= time(nil);
592 rip_incoming(r);
596 if (do_rdisc) {
597 /* Try an IRDP read. */
598 r= asyn_read(&asyn, irdp_fd, irdp_buf,
599 sizeof(irdp_buf));
600 if (r < 0) {
601 if (errno == EIO) fatal(ip_device);
602 if (errno != EINPROGRESS) report(ip_device);
603 } else {
604 now= time(nil);
605 irdp_incoming(r);
608 fflush(stdout);
610 /* Compute the next wakeup call. */
611 timeout= next_sol < next_advert ? next_sol : next_advert;
613 /* Wait for a RIP or IRDP packet or a timeout. */
614 tv.tv_sec= timeout;
615 tv.tv_usec= 0;
616 if (asyn_wait(&asyn, 0, timeout == NEVER ? nil : &tv) < 0) {
617 /* Timeout? */
618 if (errno != EINTR && errno != EAGAIN)
619 fatal("asyn_wait()");
620 now= time(nil);
621 time_functions();