4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Extricated from iputils by Nick Bowler (2023-07).
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_packet.h>
31 #include <linux/filter.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
49 char *tftp_dir = "/etc/tftpboot";
51 static const char *progname = "rarpd";
53 extern int ether_ntohost(char *name, unsigned char *ea);
60 unsigned char lladdr[16];
62 struct ifaddr *ifa_list;
75 struct rarp_map *next;
80 unsigned char lladdr[16];
84 static void print_usage(FILE *f)
86 fprintf(f, "Usage: %s [options] [interface]\n", progname);
88 fprintf(f, "Try %s -H for more information.\n", progname);
91 static void print_help(void)
97 puts(" -A listen also arp messages");
98 puts(" -a listen on all the interfaces");
99 puts(" -b <dir> tftpd boot directory");
100 puts(" -d debug mode");
101 puts(" -e /etc/ethers markup alone is fine");
102 puts(" -v verbose mode");
103 puts(" -V print version and then exit");
104 puts(" -H print this message and then exit");
107 puts("For more information, see the rarpd(8) man page.");
110 static void print_version(void)
112 printf("rarpd (%s) %s\n", PACKAGE, PACKAGE_VERSION);
113 puts("License GPLv2+: GNU GPL version 2 or any later version");
114 puts("This is free software: you are free to change and redistribute it.");
115 puts("There is NO WARRANTY, to the extent permitted by law.");
125 struct ifreq *ifrp, *ifend;
129 struct ifreq ibuf[256];
131 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
132 syslog(LOG_ERR, "socket: %s", strerror(errno));
136 ifc.ifc_len = sizeof ibuf;
137 ifc.ifc_buf = (char *)ibuf;
138 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
139 ifc.ifc_len < (int)sizeof(struct ifreq)) {
140 syslog(LOG_ERR, "SIOCGIFCONF: %s", strerror(errno));
145 while ((ifl = ifl_list) != NULL) {
146 while ((ifa = ifl->ifa_list) != NULL) {
147 ifl->ifa_list = ifa->next;
150 ifl_list = ifl->next;
154 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
155 for (ifrp = ibuf; ifrp < ifend; ifrp++) {
160 if (ifrp->ifr_addr.sa_family != AF_INET)
162 addr = ((struct sockaddr_in*)&ifrp->ifr_addr)->sin_addr.s_addr;
165 if (ioctl(fd, SIOCGIFINDEX, ifrp)) {
166 syslog(LOG_ERR, "ioctl(SIOCGIFNAME): %s", strerror(errno));
169 if (ifidx && ifrp->ifr_ifindex != ifidx)
171 for (ifl = ifl_list; ifl; ifl = ifl->next)
172 if (ifl->index == ifrp->ifr_ifindex)
176 int index = ifrp->ifr_ifindex;
178 if (ioctl(fd, SIOCGIFHWADDR, ifrp)) {
179 syslog(LOG_ERR, "ioctl(SIOCGIFHWADDR): %s", strerror(errno));
183 ifl = (struct iflink*)malloc(sizeof(*ifl));
186 memset(ifl, 0, sizeof(*ifl));
187 ifl->next = ifl_list;
190 ifl->hatype = ifrp->ifr_hwaddr.sa_family;
191 memcpy(ifl->lladdr, ifrp->ifr_hwaddr.sa_data, 14);
192 strncpy(ifl->name, ifrp->ifr_name, IFNAMSIZ);
193 p = strchr(ifl->name, ':');
197 syslog(LOG_INFO, "link %s", ifl->name);
199 if (ioctl(fd, SIOCGIFNETMASK, ifrp)) {
200 syslog(LOG_ERR, "ioctl(SIOCGIFMASK): %s", strerror(errno));
203 mask = ((struct sockaddr_in*)&ifrp->ifr_netmask)->sin_addr.s_addr;
204 if (ioctl(fd, SIOCGIFDSTADDR, ifrp)) {
205 syslog(LOG_ERR, "ioctl(SIOCGIFDSTADDR): %s", strerror(errno));
208 prefix = ((struct sockaddr_in*)&ifrp->ifr_dstaddr)->sin_addr.s_addr;
209 for (ifa = ifl->ifa_list; ifa; ifa = ifa->next) {
210 if (ifa->local == addr &&
211 ifa->prefix == prefix &&
216 if (mask == 0 || prefix == 0)
218 ifa = (struct ifaddr*)malloc(sizeof(*ifa));
219 memset(ifa, 0, sizeof(*ifa));
221 ifa->prefix = prefix;
223 ifa->next = ifl->ifa_list;
229 for (i=32; i>=0; i--) {
230 if (htonl(m) == mask)
234 if (addr == prefix) {
235 syslog(LOG_INFO, " addr %s/%d on %s\n",
236 inet_ntoa(*(struct in_addr*)&addr), i, ifl->name);
239 sprintf(tmpa, "%s", inet_ntoa(*(struct in_addr*)&addr));
240 syslog(LOG_INFO, " addr %s %s/%d on %s\n", tmpa,
241 inet_ntoa(*(struct in_addr*)&prefix), i, ifl->name);
254 int bootable(uint32_t addr)
260 sprintf(name, "%08X", (uint32_t)ntohl(addr));
261 d = opendir(tftp_dir);
263 syslog(LOG_ERR, "opendir: %s", strerror(errno));
266 while ((dent = readdir(d)) != NULL) {
267 if (strncmp(dent->d_name, name, 8) == 0)
274 struct ifaddr *select_ipaddr(int ifindex, uint32_t *sel_addr, uint32_t **alist)
282 for (ifl=ifl_list; ifl; ifl=ifl->next)
283 if (ifl->index == ifindex)
285 if (ifl == NULL && !retry) {
293 for (i=0; alist[i]; i++) {
294 uint32_t addr = *(alist[i]);
295 for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
296 if (!((ifa->prefix^addr)&ifa->mask)) {
301 if (ifa == NULL && retry==0) {
307 if (i==1 && allow_offlink) {
308 *sel_addr = *(alist[0]);
309 return ifl->ifa_list;
311 syslog(LOG_ERR, "Off-link request on %s", ifl->name);
315 struct rarp_map *rarp_lookup(int ifindex, int hatype,
316 int halen, unsigned char *lladdr)
320 for (r=rarp_db; r; r=r->next) {
321 if (r->arp_type != hatype && r->arp_type != -1)
323 if (r->lladdr_len != halen)
325 if (r->ifindex != ifindex && r->ifindex != 0)
327 if (memcmp(r->lladdr, lladdr, halen) == 0)
332 if (hatype == ARPHRD_ETHER && halen == 6) {
336 static struct rarp_map emap = {
337 .arp_type = ARPHRD_ETHER,
341 if (ether_ntohost(ename, lladdr) != 0 ||
342 (hp = gethostbyname(ename)) == NULL) {
344 syslog(LOG_INFO, "not found in /etc/ethers");
347 if (hp->h_addrtype != AF_INET) {
348 syslog(LOG_ERR, "no IP address");
351 ifa = select_ipaddr(ifindex, &emap.ipaddr, (uint32_t **)hp->h_addr_list);
353 memcpy(emap.lladdr, lladdr, 6);
354 if (only_ethers || bootable(emap.ipaddr))
357 syslog(LOG_INFO, "not bootable");
364 static int load_arp_bpflet(int fd)
366 static struct sock_filter insns[] = {
367 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
368 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ARPOP_RREQUEST, 0, 1),
369 BPF_STMT(BPF_RET|BPF_K, 1024),
370 BPF_STMT(BPF_RET|BPF_K, 0),
372 static struct sock_fprog filter = {
373 sizeof insns / sizeof(insns[0]),
377 return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
380 int put_mylladdr(unsigned char **ptr_p, int ifindex, int alen)
384 for (ifl=ifl_list; ifl; ifl = ifl->next)
385 if (ifl->index == ifindex)
391 memcpy(*ptr_p, ifl->lladdr, alen);
396 int put_myipaddr(unsigned char **ptr_p, int ifindex, uint32_t hisipaddr)
402 for (ifl=ifl_list; ifl; ifl = ifl->next)
403 if (ifl->index == ifindex)
409 for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
410 if (!((ifa->prefix^hisipaddr)&ifa->mask)) {
415 memcpy(*ptr_p, &laddr, 4);
420 void arp_advise(int ifindex, unsigned char *lladdr, int lllen, uint32_t ipaddr)
424 struct sockaddr_in *sin;
427 for (ifl=ifl_list; ifl; ifl = ifl->next)
428 if (ifl->index == ifindex)
434 fd = socket(AF_INET, SOCK_DGRAM, 0);
435 memset(&req, 0, sizeof(req));
436 req.arp_flags = ATF_COM;
437 sin = (struct sockaddr_in *)&req.arp_pa;
438 sin->sin_family = AF_INET;
439 sin->sin_addr.s_addr = ipaddr;
440 req.arp_ha.sa_family = ifl->hatype;
441 memcpy(req.arp_ha.sa_data, lladdr, lllen);
442 memcpy(req.arp_dev, ifl->name, IFNAMSIZ);
444 if (ioctl(fd, SIOCSARP, &req))
445 syslog(LOG_ERR, "SIOCSARP: %s", strerror(errno));
449 void serve_it(int fd)
451 unsigned char buf[1024];
452 struct sockaddr_ll sll = { 0 };
453 socklen_t sll_len = sizeof(sll);
454 struct arphdr *a = (struct arphdr*)buf;
455 struct rarp_map *rmap;
459 n = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*)&sll, &sll_len);
461 if (errno != EINTR && errno != EAGAIN)
462 syslog(LOG_ERR, "recvfrom: %s", strerror(errno));
466 /* Do not accept packets for other hosts and our own ones */
467 if (sll.sll_pkttype != PACKET_BROADCAST &&
468 sll.sll_pkttype != PACKET_MULTICAST &&
469 sll.sll_pkttype != PACKET_HOST)
472 if (ifidx && sll.sll_ifindex != ifidx)
475 if ((size_t)n<sizeof(*a)) {
476 syslog(LOG_ERR, "truncated arp packet; len=%zu", n);
480 /* Accept only RARP requests */
481 if (a->ar_op != htons(ARPOP_RREQUEST))
488 for (i=0; i<sll.sll_halen; i++) {
490 sprintf(p, ":%02x", sll.sll_addr[i]);
493 sprintf(p, "%02x", sll.sll_addr[i]);
496 syslog(LOG_INFO, "RARP request from %s on if%d", tmpbuf, sll.sll_ifindex);
501 /* 1. IP only -> pln==4 */
502 if (a->ar_pln != 4) {
503 syslog(LOG_ERR, "interesting rarp_req plen=%d", a->ar_pln);
506 /* 2. ARP protocol must be IP */
507 if (a->ar_pro != htons(ETH_P_IP)) {
508 syslog(LOG_ERR, "rarp protocol is not IP %04x", ntohs(a->ar_pro));
511 /* 3. ARP types must match */
512 if (htons(sll.sll_hatype) != a->ar_hrd) {
513 switch (sll.sll_hatype) {
515 if (a->ar_hrd == htons(ARPHRD_ETHER) ||
516 a->ar_hrd == htons(ARPHRD_IEEE802))
520 syslog(LOG_ERR, "rarp htype mismatch");
524 /* 3. LL address lengths must be equal */
525 if (a->ar_hln != sll.sll_halen) {
526 syslog(LOG_ERR, "rarp hlen mismatch");
529 /* 4. Check packet length */
530 if (sizeof(*a) + 2*4 + 2*a->ar_hln > (size_t) n) {
531 syslog(LOG_ERR, "truncated rarp request; len=%zu", n);
534 /* 5. Silly check: if this guy set different source
535 addresses in MAC header and in ARP, he is insane
537 if (memcmp(sll.sll_addr, a+1, sll.sll_halen)) {
538 syslog(LOG_ERR, "this guy set different his lladdrs in arp and header");
541 /* End of sanity checks */
543 /* Lookup requested target in our database */
544 rmap = rarp_lookup(sll.sll_ifindex, sll.sll_hatype,
545 sll.sll_halen, (unsigned char*)(a+1) + sll.sll_halen + 4);
549 /* Prepare reply. It is almost ready, we only
550 replace ARP packet type, put our lladdr and
551 IP address to source fields,
552 and fill target IP address.
554 a->ar_op = htons(ARPOP_RREPLY);
555 ptr = (unsigned char*)(a+1);
556 if (put_mylladdr(&ptr, sll.sll_ifindex, rmap->lladdr_len))
558 if (put_myipaddr(&ptr, sll.sll_ifindex, rmap->ipaddr))
560 /* It is already filled */
561 ptr += rmap->lladdr_len;
562 memcpy(ptr, &rmap->ipaddr, 4);
565 /* Update our ARP cache. Probably, this guy
566 will not able to make ARP (if it is broken)
568 arp_advise(sll.sll_ifindex, rmap->lladdr, rmap->lladdr_len, rmap->ipaddr);
570 /* Sendto is blocking, but with 5sec timeout */
572 sendto(fd, buf, ptr - buf, 0, (struct sockaddr*)&sll, sizeof(sll));
576 void catch_signal(int sig, void (*handler)(int))
580 memset(&sa, 0, sizeof(sa));
581 sa.sa_handler = handler;
582 sigaction(sig, &sa, NULL);
585 void sig_alarm(int signo __attribute__((__unused__)))
589 void sig_hup(int signo __attribute__((__unused__)))
594 int main(int argc, char **argv)
596 struct pollfd pset[2];
604 while ((opt = getopt(argc, argv, "aAb:dvoeVH")) != EOF) {
645 if (argc > optind+1) {
649 ifname = argv[optind];
653 pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
657 memset(&ifr, 0, sizeof(ifr));
658 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
659 if (ioctl(pset[0].fd, SIOCGIFINDEX, &ifr)) {
660 error(0, errno, "ioctl(SIOCGIFINDEX)");
663 ifidx = ifr.ifr_ifindex;
668 pset[1].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
669 if (pset[1].fd >= 0) {
670 load_arp_bpflet(pset[1].fd);
675 if (pset[1].fd >= 0) {
676 struct sockaddr_ll sll;
677 memset(&sll, 0, sizeof(sll));
678 sll.sll_family = AF_PACKET;
679 sll.sll_protocol = htons(ETH_P_ARP);
680 sll.sll_ifindex = all_ifaces ? 0 : ifidx;
681 if (bind(pset[1].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
687 if (pset[0].fd >= 0) {
688 struct sockaddr_ll sll;
689 memset(&sll, 0, sizeof(sll));
690 sll.sll_family = AF_PACKET;
691 sll.sll_protocol = htons(ETH_P_RARP);
692 sll.sll_ifindex = all_ifaces ? 0 : ifidx;
693 if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
698 if (pset[0].fd < 0) {
703 error(1, errno, "failed to bind any socket");
706 if (daemon(0, 0) < 0)
707 error(1, errno, "failed to daemon()");
710 openlog("rarpd", LOG_PID | LOG_CONS, LOG_DAEMON);
711 catch_signal(SIGALRM, sig_alarm);
712 catch_signal(SIGHUP, sig_hup);
722 #define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP)
723 pset[0].events = EVENTS;
725 pset[1].events = EVENTS;
728 i = poll(pset, psize, -1);
730 if (errno != EINTR && i<0) {
731 syslog(LOG_ERR, "poll returned some crap: %s\n", strerror(errno));
736 for (i=0; i<psize; i++) {
737 if (pset[i].revents&EVENTS)
738 serve_it(pset[i].fd);