1 /* irdpd 1.10 - Internet router discovery protocol daemon.
4 * Activily solicitate or passively look for routers.
5 * Based heavily on its forerunners, the irdp_sol and rip2icmp daemons by
20 #include <sys/ioctl.h>
21 #include <sys/asynchio.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
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))
54 /* Standard Minix needs to choose between router discovery and RIP info. */
58 /* VMD Minix can do both at once. */
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. */
74 char rip_buf
[8192]; /* Incoming RIP packet buffer. */
75 char irdp_buf
[1024]; /* IRDP buffer. */
77 typedef struct routeinfo
98 table_t
*table
; /* Collected table of routing info. */
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
;
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 */
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
;
130 #define addr2name(host) inet_ntoa(host)
133 void print_table(void)
134 /* Show the collected routing table. */
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. */
156 icmp_hdr_t
*icmp_hdr
;
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. */
195 printf("Routing table send to %s:\n", addr2name(host
));
199 if (write(irdp_fd
, buf
, data
- buf
) < 0) {
200 (errno
== EIO
? fatal
: report
)(ip_device
);
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];
212 icmp_hdr_t
*icmp_hdr
;
214 if (sol_retries
== 0) {
215 /* Stop soliciting. */
218 /* Switch to RIP if no router responded. */
219 if (table_size
== 0) {
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;
233 ip_hdr
->ih_dst
= htonl(0xFFFFFFFFL
);
235 ip_hdr
->ih_dst
= HTONL(0xFFFFFFFFL
);
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
;
254 if (now
>= next_advert
) {
255 /* Advertize routes to the local host (normally), or
256 * broadcast them (to keep bad hosts up.)
260 advertize(bcast
? htonl(0xFFFFFFFFL
) : htonl(0x7F000001L
));
262 advertize(bcast
? HTONL(0xFFFFFFFFL
) : HTONL(0x7F000001L
));
264 next_advert
= now
+ MaxAdvertisementInterval
;
266 /* Make sure we are listening to RIP now. */
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
;
279 /* Look for the host, or select the oldest entry. */
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
)
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
) {
294 table
= realloc(table
, table_size
* sizeof(*table
));
295 if (table
== nil
) fatal("heap error");
296 oldest
= &table
[table_size
- 1];
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
;
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
;
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");
331 routeinfo
= (routeinfo_t
*) (rip_buf
+ sizeof(*udp_io_hdr
)
332 + udp_io_hdr
->uih_ip_opt_len
);
334 if (routeinfo
->command
!= RIP_REPLY
) {
336 printf("RIP-%d packet command %d ignored\n",
337 routeinfo
->version
, routeinfo
->command
);
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)
348 default_dist
= ntohl(data
->metric
);
349 if (default_dist
>= 256) {
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
);
363 printf("Routing table after RIP-%d packet from %s:\n",
365 addr2name(udp_io_hdr
->uih_src_addr
));
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.
380 icmp_hdr_t
*icmp_hdr
;
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");
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.
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
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! */
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
;
438 printf("Routing table after advert received from %s:\n",
439 addr2name(ip_hdr
->ih_src
));
442 struct tm
*tm
= localtime(&router_advert_valid
);
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. */
454 case SIGUSR1
: debug
++; break;
455 case SIGUSR2
: debug
= 0; break;
462 "Usage: irdpd [-bd] [-U udp-device] [-I ip-device] [-o priority-offset]\n");
466 int main(int argc
, char **argv
)
469 struct servent
*service
;
470 udpport_t route_port
;
471 nwio_udpopt_t udpopt
;
477 char *offset_arg
, *offset_end
;
480 udp_device
= ip_device
= 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; }
491 if (udp_device
!= nil
) usage();
493 if (++i
== argc
) usage();
500 if (ip_device
!= nil
) usage();
502 if (++i
== argc
) usage();
509 if (offset_arg
!= nil
) usage();
511 if (++i
== argc
) usage();
531 if (i
!= argc
) usage();
533 /* Debug level signals. */
534 sa
.sa_handler
= sig_handler
;
535 sigemptyset(&sa
.sa_mask
);
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
;
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
) {
555 "irdpd: unable to look up the port number for the 'route' service\n");
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
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
;
579 ipopt
.nwio_hdropt
.iho_opt_siz
= 0;
581 ipopt
.nwio_rem
= htonl(0xFFFFFFFFL
);
583 ipopt
.nwio_rem
= HTONL(0xFFFFFFFFL
);
585 ipopt
.nwio_proto
= IPPROTO_ICMP
;
587 if (ioctl(irdp_fd
, NWIOSIPOPT
, &ipopt
) < 0)
588 fatal("can't configure ICMP channel");
596 /* Try a RIP read. */
597 r
= asyn_read(&asyn
, rip_fd
, rip_buf
, sizeof(rip_buf
));
599 if (errno
== EIO
) fatal(udp_device
);
600 if (errno
!= EINPROGRESS
) report(udp_device
);
608 /* Try an IRDP read. */
609 r
= asyn_read(&asyn
, irdp_fd
, irdp_buf
,
612 if (errno
== EIO
) fatal(ip_device
);
613 if (errno
!= EINPROGRESS
) report(ip_device
);
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. */
627 if (asyn_wait(&asyn
, 0, timeout
== NEVER
? nil
: &tv
) < 0) {
629 if (errno
!= EINTR
&& errno
!= EAGAIN
)
630 fatal("asyn_wait()");