source: trunk/minix/commands/simple/irdpd.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 15.0 KB
Line 
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>
36
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
48
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))
52
53#if !__minix_vmd
54/* Standard Minix needs to choose between router discovery and RIP info. */
55int do_rdisc= 1;
56int 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
62
63int rip_fd; /* For incoming RIP packet. */
64int irdp_fd; /* Receive or transmit IRDP packets. */
65
66char *udp_device; /* UDP device to use. */
67char *ip_device; /* IP device to use. */
68
69int priority_offset; /* Offset to make my routes less preferred. */
70
71int bcast= 0; /* Broadcast adverts to all. */
72int debug= 0;
73
74char rip_buf[8192]; /* Incoming RIP packet buffer. */
75char irdp_buf[1024]; /* IRDP buffer. */
76
77typedef struct routeinfo
78{
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;
90
91typedef struct table
92{
93 ipaddr_t tab_gw;
94 i32_t tab_pref;
95 time_t tab_time;
96} table_t;
97
98table_t *table; /* Collected table of routing info. */
99size_t table_size;
100
101int sol_retries= MAX_SOLICITATIONS;
102time_t next_sol= IMMEDIATELY;
103time_t next_advert= NEVER;
104time_t router_advert_valid= IMMEDIATELY;
105time_t now;
106
107void report(const char *label)
108/* irdpd: /dev/hd0: Device went up in flames */
109{
110 fprintf(stderr, "irdpd: %s: %s\n", label, strerror(errno));
111}
112
113void fatal(const char *label)
114/* irdpd: /dev/house: Taking this with it */
115{
116 report(label);
117 exit(1);
118}
119
120#if DEBUG
121char *addr2name(ipaddr_t host)
122/* Translate an IP address to a printable name. */
123{
124 struct hostent *hostent;
125
126 hostent= gethostbyaddr((char *) &host, sizeof(host), AF_INET);
127 return hostent == nil ? inet_ntoa(host) : hostent->h_name;
128}
129#else
130#define addr2name(host) inet_ntoa(host)
131#endif
132
133void print_table(void)
134/* Show the collected routing table. */
135{
136 int i;
137 table_t *ptab;
138 struct tm *tm;
139
140 for (i= 0, ptab= table; i < table_size; i++, ptab++) {
141 if (ptab->tab_time < now - DEAD_TO) continue;
142
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);
148 }
149}
150
151void advertize(ipaddr_t host)
152/* Send a router advert to a host. */
153{
154 char *buf, *data;
155 ip_hdr_t *ip_hdr;
156 icmp_hdr_t *icmp_hdr;
157 int i;
158 table_t *ptab;
159
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");
163
164 ip_hdr= (ip_hdr_t *) buf;
165 icmp_hdr= (icmp_hdr_t *) (ip_hdr + 1);
166
167 ip_hdr->ih_vers_ihl= 0x45;
168 ip_hdr->ih_dst= host;
169
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;
176
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;
180
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);
187 }
188 icmp_hdr->ih_chksum= 0;
189 icmp_hdr->ih_chksum= ~oneC_sum(0, icmp_hdr, data - (char *) icmp_hdr);
190
191 if (icmp_hdr->ih_hun.ihh_ram.iram_na > 0) {
192 /* Send routing info. */
193
194 if (debug) {
195 printf("Routing table send to %s:\n", addr2name(host));
196 print_table();
197 }
198
199 if (write(irdp_fd, buf, data - buf) < 0) {
200 (errno == EIO ? fatal : report)(ip_device);
201 }
202 }
203 free(buf);
204}
205
206void time_functions(void)
207/* Perform time dependend functions: router solicitation, router advert. */
208{
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;
213
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;
222 }
223#endif
224 return;
225 }
226
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);
230
231 ip_hdr->ih_vers_ihl= 0x45;
232 ip_hdr->ih_dst= HTONL(0xFFFFFFFFL);
233
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);
239
240 if (debug) printf("Broadcasting router solicitation\n");
241
242 if (write(irdp_fd, buf, sizeof(buf)) < 0)
243 fatal("sending router solicitation failed");
244
245 /* Schedule the next packet. */
246 next_sol= now + SOLICITATION_INTERVAL;
247
248 sol_retries--;
249 }
250
251 if (now >= next_advert) {
252 /* Advertize routes to the local host (normally), or
253 * broadcast them (to keep bad hosts up.)
254 */
255
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
263 }
264}
265
266void add_gateway(ipaddr_t host, i32_t pref)
267/* Add a router with given address and preference to the routing table. */
268{
269 table_t *oldest, *ptab;
270 int i;
271
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;
276
277 if (oldest == nil || ptab->tab_time < oldest->tab_time)
278 oldest= ptab;
279 }
280
281 /* Don't evict the oldest if it is still valid. */
282 if (oldest != nil && oldest->tab_time >= now - DEST_TO) oldest= nil;
283
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];
290 }
291
292 if (oldest != nil) {
293 ptab= oldest;
294 ptab->tab_gw= host;
295 ptab->tab_pref= DEAD_PREF;
296 }
297
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;
302 }
303}
304
305void 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.)
308 */
309{
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;
315
316 /* We don't care about RIP packets when there are router adverts. */
317 if (now + MaxAdvertisementInterval < router_advert_valid) return;
318
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;
323 }
324 routeinfo= (routeinfo_t *) (rip_buf + sizeof(*udp_io_hdr)
325 + udp_io_hdr->uih_ip_opt_len);
326
327 if (routeinfo->command != RIP_REPLY) {
328 if (debug) {
329 printf("RIP-%d packet command %d ignored\n",
330 routeinfo->version, routeinfo->command);
331 }
332 return;
333 }
334
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);
346 }
347 }
348 }
349 pref= default_dist >= 256 ? 1 : 512 - default_dist;
350 pref+= priority_offset;
351
352 /* Add the gateway to the table with the calculated preference. */
353 add_gateway(udp_io_hdr->uih_src_addr, pref);
354
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();
360 }
361
362 /* Start advertizing. */
363 if (next_advert == NEVER) next_advert= IMMEDIATELY;
364}
365
366void 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.
370 */
371{
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;
381
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;
387 }
388
389 icmp_hdr= (icmp_hdr_t *)(irdp_buf + ip_hdr_len);
390
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;
394
395 if (icmp_hdr->ih_type != ICMP_TYPE_ROUTER_ADVER) return;
396
397 /* Incoming router advertisement, the kind of packet the TCP/IP task
398 * is very happy with. No need to solicit further.
399 */
400 sol_retries= 0;
401
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.
405 */
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);
413
414 if (addr == ip_hdr->ih_src) {
415 /* The sender is in the routing table! */
416 router= 1;
417 }
418 add_gateway(addr, pref);
419 }
420
421 valid= now + ntohs(icmp_hdr->ih_hun.ihh_ram.iram_lt);
422 if (router) router_advert_valid= valid;
423
424 /* Restart advertizing close to the timeout of the advert. (No more
425 * irdpd adverts if the router stays alive.)
426 */
427 if (router || next_advert > valid - DANGER)
428 next_advert= valid - DANGER;
429
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);
439 }
440 }
441}
442
443void sig_handler(int sig)
444/* A signal changes the debug level. */
445{
446 switch (sig) {
447 case SIGUSR1: debug++; break;
448 case SIGUSR2: debug= 0; break;
449 }
450}
451
452void usage(void)
453{
454 fprintf(stderr,
455"Usage: irdpd [-bd] [-U udp-device] [-I ip-device] [-o priority-offset]\n");
456 exit(1);
457}
458
459int main(int argc, char **argv)
460{
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;
472
473 udp_device= ip_device= nil;
474 offset_arg= nil;
475
476 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
477 char *p= argv[i] + 1;
478
479 if (p[0] == '-' && p[1] == 0) { i++; break; }
480
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];
488 }
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];
497 }
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];
506 }
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();
521 }
522 }
523 }
524 if (i != argc) usage();
525
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);
532
533 if (udp_device == nil && (udp_device= getenv("UDP_DEVICE")) == nil)
534 udp_device= UDP_DEVICE;
535
536 if (ip_device == nil && (ip_device= getenv("IP_DEVICE")) == nil)
537 ip_device= IP_DEVICE;
538
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();
544 }
545
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);
550 }
551
552 route_port= (udpport_t) service->s_port;
553
554 if ((rip_fd= open(udp_device, O_RDWR)) < 0) fatal(udp_device);
555
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");
563
564 if ((irdp_fd= open(ip_device, O_RDWR)) < 0) fatal(ip_device);
565
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;
575
576 if (ioctl(irdp_fd, NWIOSIPOPT, &ipopt) < 0)
577 fatal("can't configure ICMP channel");
578
579 asyn_init(&asyn);
580
581 while (1) {
582 ssize_t r;
583
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);
593 }
594 }
595
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);
606 }
607 }
608 fflush(stdout);
609
610 /* Compute the next wakeup call. */
611 timeout= next_sol < next_advert ? next_sol : next_advert;
612
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();
622 }
623 }
624}
Note: See TracBrowser for help on using the repository browser.