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>
35 #include "iputils_common.h"
47 char *tftp_dir = "/etc/tftpboot";
49 extern int ether_ntohost(char *name, unsigned char *ea);
50 void usage(void) __attribute__((noreturn));
57 unsigned char lladdr[16];
59 struct ifaddr *ifa_list;
72 struct rarp_map *next;
77 unsigned char lladdr[16];
85 " rarpd [options] [interface]\n"
87 " -A listen also arp messages\n"
88 " -a listen on all the interfaces\n"
89 " -b <dir> tftpd boot directory\n"
91 " -e /etc/ethers markup alone is fine\n"
93 " -V print version and exit\n"
94 "\nFor more details see rarpd(8).\n"
99 static void print_version(void)
101 printf("rarpd (%s) %s\n", PACKAGE, PACKAGE_VERSION);
102 puts("License GPLv2+: GNU GPL version 2 or any later version");
103 puts("This is free software: you are free to change and redistribute it.");
104 puts("There is NO WARRANTY, to the extent permitted by law.");
114 struct ifreq *ifrp, *ifend;
118 struct ifreq ibuf[256];
120 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
121 syslog(LOG_ERR, "socket: %s", strerror(errno));
125 ifc.ifc_len = sizeof ibuf;
126 ifc.ifc_buf = (char *)ibuf;
127 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
128 ifc.ifc_len < (int)sizeof(struct ifreq)) {
129 syslog(LOG_ERR, "SIOCGIFCONF: %s", strerror(errno));
134 while ((ifl = ifl_list) != NULL) {
135 while ((ifa = ifl->ifa_list) != NULL) {
136 ifl->ifa_list = ifa->next;
139 ifl_list = ifl->next;
143 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
144 for (ifrp = ibuf; ifrp < ifend; ifrp++) {
149 if (ifrp->ifr_addr.sa_family != AF_INET)
151 addr = ((struct sockaddr_in*)&ifrp->ifr_addr)->sin_addr.s_addr;
154 if (ioctl(fd, SIOCGIFINDEX, ifrp)) {
155 syslog(LOG_ERR, "ioctl(SIOCGIFNAME): %s", strerror(errno));
158 if (ifidx && ifrp->ifr_ifindex != ifidx)
160 for (ifl = ifl_list; ifl; ifl = ifl->next)
161 if (ifl->index == ifrp->ifr_ifindex)
165 int index = ifrp->ifr_ifindex;
167 if (ioctl(fd, SIOCGIFHWADDR, ifrp)) {
168 syslog(LOG_ERR, "ioctl(SIOCGIFHWADDR): %s", strerror(errno));
172 ifl = (struct iflink*)malloc(sizeof(*ifl));
175 memset(ifl, 0, sizeof(*ifl));
176 ifl->next = ifl_list;
179 ifl->hatype = ifrp->ifr_hwaddr.sa_family;
180 memcpy(ifl->lladdr, ifrp->ifr_hwaddr.sa_data, 14);
181 strncpy(ifl->name, ifrp->ifr_name, IFNAMSIZ);
182 p = strchr(ifl->name, ':');
186 syslog(LOG_INFO, "link %s", ifl->name);
188 if (ioctl(fd, SIOCGIFNETMASK, ifrp)) {
189 syslog(LOG_ERR, "ioctl(SIOCGIFMASK): %s", strerror(errno));
192 mask = ((struct sockaddr_in*)&ifrp->ifr_netmask)->sin_addr.s_addr;
193 if (ioctl(fd, SIOCGIFDSTADDR, ifrp)) {
194 syslog(LOG_ERR, "ioctl(SIOCGIFDSTADDR): %s", strerror(errno));
197 prefix = ((struct sockaddr_in*)&ifrp->ifr_dstaddr)->sin_addr.s_addr;
198 for (ifa = ifl->ifa_list; ifa; ifa = ifa->next) {
199 if (ifa->local == addr &&
200 ifa->prefix == prefix &&
205 if (mask == 0 || prefix == 0)
207 ifa = (struct ifaddr*)malloc(sizeof(*ifa));
208 memset(ifa, 0, sizeof(*ifa));
210 ifa->prefix = prefix;
212 ifa->next = ifl->ifa_list;
218 for (i=32; i>=0; i--) {
219 if (htonl(m) == mask)
223 if (addr == prefix) {
224 syslog(LOG_INFO, " addr %s/%d on %s\n",
225 inet_ntoa(*(struct in_addr*)&addr), i, ifl->name);
228 sprintf(tmpa, "%s", inet_ntoa(*(struct in_addr*)&addr));
229 syslog(LOG_INFO, " addr %s %s/%d on %s\n", tmpa,
230 inet_ntoa(*(struct in_addr*)&prefix), i, ifl->name);
243 int bootable(uint32_t addr)
249 sprintf(name, "%08X", (uint32_t)ntohl(addr));
250 d = opendir(tftp_dir);
252 syslog(LOG_ERR, "opendir: %s", strerror(errno));
255 while ((dent = readdir(d)) != NULL) {
256 if (strncmp(dent->d_name, name, 8) == 0)
263 struct ifaddr *select_ipaddr(int ifindex, uint32_t *sel_addr, uint32_t **alist)
271 for (ifl=ifl_list; ifl; ifl=ifl->next)
272 if (ifl->index == ifindex)
274 if (ifl == NULL && !retry) {
282 for (i=0; alist[i]; i++) {
283 uint32_t addr = *(alist[i]);
284 for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
285 if (!((ifa->prefix^addr)&ifa->mask)) {
290 if (ifa == NULL && retry==0) {
296 if (i==1 && allow_offlink) {
297 *sel_addr = *(alist[0]);
298 return ifl->ifa_list;
300 syslog(LOG_ERR, "Off-link request on %s", ifl->name);
304 struct rarp_map *rarp_lookup(int ifindex, int hatype,
305 int halen, unsigned char *lladdr)
309 for (r=rarp_db; r; r=r->next) {
310 if (r->arp_type != hatype && r->arp_type != -1)
312 if (r->lladdr_len != halen)
314 if (r->ifindex != ifindex && r->ifindex != 0)
316 if (memcmp(r->lladdr, lladdr, halen) == 0)
321 if (hatype == ARPHRD_ETHER && halen == 6) {
325 static struct rarp_map emap = {
326 .arp_type = ARPHRD_ETHER,
330 if (ether_ntohost(ename, lladdr) != 0 ||
331 (hp = gethostbyname(ename)) == NULL) {
333 syslog(LOG_INFO, "not found in /etc/ethers");
336 if (hp->h_addrtype != AF_INET) {
337 syslog(LOG_ERR, "no IP address");
340 ifa = select_ipaddr(ifindex, &emap.ipaddr, (uint32_t **)hp->h_addr_list);
342 memcpy(emap.lladdr, lladdr, 6);
343 if (only_ethers || bootable(emap.ipaddr))
346 syslog(LOG_INFO, "not bootable");
353 static int load_arp_bpflet(int fd)
355 static struct sock_filter insns[] = {
356 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
357 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ARPOP_RREQUEST, 0, 1),
358 BPF_STMT(BPF_RET|BPF_K, 1024),
359 BPF_STMT(BPF_RET|BPF_K, 0),
361 static struct sock_fprog filter = {
362 sizeof insns / sizeof(insns[0]),
366 return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
369 int put_mylladdr(unsigned char **ptr_p, int ifindex, int alen)
373 for (ifl=ifl_list; ifl; ifl = ifl->next)
374 if (ifl->index == ifindex)
380 memcpy(*ptr_p, ifl->lladdr, alen);
385 int put_myipaddr(unsigned char **ptr_p, int ifindex, uint32_t hisipaddr)
391 for (ifl=ifl_list; ifl; ifl = ifl->next)
392 if (ifl->index == ifindex)
398 for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
399 if (!((ifa->prefix^hisipaddr)&ifa->mask)) {
404 memcpy(*ptr_p, &laddr, 4);
409 void arp_advise(int ifindex, unsigned char *lladdr, int lllen, uint32_t ipaddr)
413 struct sockaddr_in *sin;
416 for (ifl=ifl_list; ifl; ifl = ifl->next)
417 if (ifl->index == ifindex)
423 fd = socket(AF_INET, SOCK_DGRAM, 0);
424 memset(&req, 0, sizeof(req));
425 req.arp_flags = ATF_COM;
426 sin = (struct sockaddr_in *)&req.arp_pa;
427 sin->sin_family = AF_INET;
428 sin->sin_addr.s_addr = ipaddr;
429 req.arp_ha.sa_family = ifl->hatype;
430 memcpy(req.arp_ha.sa_data, lladdr, lllen);
431 memcpy(req.arp_dev, ifl->name, IFNAMSIZ);
433 if (ioctl(fd, SIOCSARP, &req))
434 syslog(LOG_ERR, "SIOCSARP: %s", strerror(errno));
438 void serve_it(int fd)
440 unsigned char buf[1024];
441 struct sockaddr_ll sll = { 0 };
442 socklen_t sll_len = sizeof(sll);
443 struct arphdr *a = (struct arphdr*)buf;
444 struct rarp_map *rmap;
448 n = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*)&sll, &sll_len);
450 if (errno != EINTR && errno != EAGAIN)
451 syslog(LOG_ERR, "recvfrom: %s", strerror(errno));
455 /* Do not accept packets for other hosts and our own ones */
456 if (sll.sll_pkttype != PACKET_BROADCAST &&
457 sll.sll_pkttype != PACKET_MULTICAST &&
458 sll.sll_pkttype != PACKET_HOST)
461 if (ifidx && sll.sll_ifindex != ifidx)
464 if ((size_t)n<sizeof(*a)) {
465 syslog(LOG_ERR, "truncated arp packet; len=%zu", n);
469 /* Accept only RARP requests */
470 if (a->ar_op != htons(ARPOP_RREQUEST))
477 for (i=0; i<sll.sll_halen; i++) {
479 sprintf(p, ":%02x", sll.sll_addr[i]);
482 sprintf(p, "%02x", sll.sll_addr[i]);
485 syslog(LOG_INFO, "RARP request from %s on if%d", tmpbuf, sll.sll_ifindex);
490 /* 1. IP only -> pln==4 */
491 if (a->ar_pln != 4) {
492 syslog(LOG_ERR, "interesting rarp_req plen=%d", a->ar_pln);
495 /* 2. ARP protocol must be IP */
496 if (a->ar_pro != htons(ETH_P_IP)) {
497 syslog(LOG_ERR, "rarp protocol is not IP %04x", ntohs(a->ar_pro));
500 /* 3. ARP types must match */
501 if (htons(sll.sll_hatype) != a->ar_hrd) {
502 switch (sll.sll_hatype) {
504 if (a->ar_hrd == htons(ARPHRD_ETHER) ||
505 a->ar_hrd == htons(ARPHRD_IEEE802))
509 syslog(LOG_ERR, "rarp htype mismatch");
513 /* 3. LL address lengths must be equal */
514 if (a->ar_hln != sll.sll_halen) {
515 syslog(LOG_ERR, "rarp hlen mismatch");
518 /* 4. Check packet length */
519 if (sizeof(*a) + 2*4 + 2*a->ar_hln > (size_t) n) {
520 syslog(LOG_ERR, "truncated rarp request; len=%zu", n);
523 /* 5. Silly check: if this guy set different source
524 addresses in MAC header and in ARP, he is insane
526 if (memcmp(sll.sll_addr, a+1, sll.sll_halen)) {
527 syslog(LOG_ERR, "this guy set different his lladdrs in arp and header");
530 /* End of sanity checks */
532 /* Lookup requested target in our database */
533 rmap = rarp_lookup(sll.sll_ifindex, sll.sll_hatype,
534 sll.sll_halen, (unsigned char*)(a+1) + sll.sll_halen + 4);
538 /* Prepare reply. It is almost ready, we only
539 replace ARP packet type, put our lladdr and
540 IP address to source fields,
541 and fill target IP address.
543 a->ar_op = htons(ARPOP_RREPLY);
544 ptr = (unsigned char*)(a+1);
545 if (put_mylladdr(&ptr, sll.sll_ifindex, rmap->lladdr_len))
547 if (put_myipaddr(&ptr, sll.sll_ifindex, rmap->ipaddr))
549 /* It is already filled */
550 ptr += rmap->lladdr_len;
551 memcpy(ptr, &rmap->ipaddr, 4);
554 /* Update our ARP cache. Probably, this guy
555 will not able to make ARP (if it is broken)
557 arp_advise(sll.sll_ifindex, rmap->lladdr, rmap->lladdr_len, rmap->ipaddr);
559 /* Sendto is blocking, but with 5sec timeout */
561 sendto(fd, buf, ptr - buf, 0, (struct sockaddr*)&sll, sizeof(sll));
565 void catch_signal(int sig, void (*handler)(int))
569 memset(&sa, 0, sizeof(sa));
570 sa.sa_handler = handler;
571 sigaction(sig, &sa, NULL);
574 void sig_alarm(int signo __attribute__((__unused__)))
578 void sig_hup(int signo __attribute__((__unused__)))
583 int main(int argc, char **argv)
585 struct pollfd pset[2];
589 atexit(close_stdout);
591 while ((opt = getopt(argc, argv, "aAb:dvoeV")) != EOF) {
630 ifname = argv[optind];
634 pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
638 memset(&ifr, 0, sizeof(ifr));
639 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
640 if (ioctl(pset[0].fd, SIOCGIFINDEX, &ifr)) {
641 error(0, errno, "ioctl(SIOCGIFINDEX)");
644 ifidx = ifr.ifr_ifindex;
649 pset[1].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
650 if (pset[1].fd >= 0) {
651 load_arp_bpflet(pset[1].fd);
656 if (pset[1].fd >= 0) {
657 struct sockaddr_ll sll;
658 memset(&sll, 0, sizeof(sll));
659 sll.sll_family = AF_PACKET;
660 sll.sll_protocol = htons(ETH_P_ARP);
661 sll.sll_ifindex = all_ifaces ? 0 : ifidx;
662 if (bind(pset[1].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
668 if (pset[0].fd >= 0) {
669 struct sockaddr_ll sll;
670 memset(&sll, 0, sizeof(sll));
671 sll.sll_family = AF_PACKET;
672 sll.sll_protocol = htons(ETH_P_RARP);
673 sll.sll_ifindex = all_ifaces ? 0 : ifidx;
674 if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
679 if (pset[0].fd < 0) {
684 error(1, errno, "failed to bind any socket");
687 if (daemon(0, 0) < 0)
688 error(1, errno, "failed to daemon()");
691 openlog("rarpd", LOG_PID | LOG_CONS, LOG_DAEMON);
692 catch_signal(SIGALRM, sig_alarm);
693 catch_signal(SIGHUP, sig_hup);
703 #define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP)
704 pset[0].events = EVENTS;
706 pset[1].events = EVENTS;
709 i = poll(pset, psize, -1);
711 if (errno != EINTR && i<0) {
712 syslog(LOG_ERR, "poll returned some crap: %s\n", strerror(errno));
717 for (i=0; i<psize; i++) {
718 if (pset[i].revents&EVENTS)
719 serve_it(pset[i].fd);