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>
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
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))
55 /* Standard Minix needs to choose between router discovery and RIP info. */
59 /* VMD Minix can do both at once. */
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. */
75 char rip_buf
[8192]; /* Incoming RIP packet buffer. */
76 char irdp_buf
[1024]; /* IRDP buffer. */
78 typedef struct routeinfo
99 table_t
*table
; /* Collected table of routing info. */
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
;
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 */
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
;
131 #define addr2name(host) inet_ntoa(*(struct in_addr *)&(host))
134 void print_table(void)
135 /* Show the collected routing table. */
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. */
157 icmp_hdr_t
*icmp_hdr
;
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. */
196 printf("Routing table send to %s:\n", addr2name(host
));
200 if (write(irdp_fd
, buf
, data
- buf
) < 0) {
201 (errno
== EIO
? fatal
: report
)(ip_device
);
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];
213 icmp_hdr_t
*icmp_hdr
;
215 if (sol_retries
== 0) {
216 /* Stop soliciting. */
219 /* Switch to RIP if no router responded. */
220 if (table_size
== 0) {
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
;
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
;
259 /* Make sure we are listening to RIP now. */
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
;
272 /* Look for the host, or select the oldest entry. */
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
)
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
) {
287 table
= realloc(table
, table_size
* sizeof(*table
));
288 if (table
== nil
) fatal("heap error");
289 oldest
= &table
[table_size
- 1];
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
;
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
;
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");
324 routeinfo
= (routeinfo_t
*) (rip_buf
+ sizeof(*udp_io_hdr
)
325 + udp_io_hdr
->uih_ip_opt_len
);
327 if (routeinfo
->command
!= RIP_REPLY
) {
329 printf("RIP-%d packet command %d ignored\n",
330 routeinfo
->version
, routeinfo
->command
);
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)
341 default_dist
= ntohl(data
->metric
);
342 if (default_dist
>= 256) {
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
);
356 printf("Routing table after RIP-%d packet from %s:\n",
358 addr2name(udp_io_hdr
->uih_src_addr
));
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.
373 icmp_hdr_t
*icmp_hdr
;
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");
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.
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
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! */
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
;
431 printf("Routing table after advert received from %s:\n",
432 addr2name(ip_hdr
->ih_src
));
435 struct tm
*tm
= localtime(&router_advert_valid
);
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. */
447 case SIGUSR1
: debug
++; break;
448 case SIGUSR2
: debug
= 0; break;
455 "Usage: irdpd [-bd] [-U udp-device] [-I ip-device] [-o priority-offset]\n");
459 int main(int argc
, char **argv
)
462 struct servent
*service
;
463 udpport_t route_port
;
464 nwio_udpopt_t udpopt
;
470 char *offset_arg
, *offset_end
;
473 udp_device
= ip_device
= 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; }
484 if (udp_device
!= nil
) usage();
486 if (++i
== argc
) usage();
493 if (ip_device
!= nil
) usage();
495 if (++i
== argc
) usage();
502 if (offset_arg
!= nil
) usage();
504 if (++i
== argc
) usage();
524 if (i
!= argc
) usage();
526 /* Debug level signals. */
527 sa
.sa_handler
= sig_handler
;
528 sigemptyset(&sa
.sa_mask
);
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
;
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
) {
548 "irdpd: unable to look up the port number for the 'route' service\n");
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
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
;
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");
585 /* Try a RIP read. */
586 r
= asyn_read(&asyn
, rip_fd
, rip_buf
, sizeof(rip_buf
));
588 if (errno
== EIO
) fatal(udp_device
);
589 if (errno
!= EINPROGRESS
) report(udp_device
);
597 /* Try an IRDP read. */
598 r
= asyn_read(&asyn
, irdp_fd
, irdp_buf
,
601 if (errno
== EIO
) fatal(ip_device
);
602 if (errno
!= EINPROGRESS
) report(ip_device
);
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. */
616 if (asyn_wait(&asyn
, 0, timeout
== NEVER
? nil
: &tv
) < 0) {
618 if (errno
!= EINTR
&& errno
!= EAGAIN
)
619 fatal("asyn_wait()");