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>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_packet.h>
29 #include <linux/filter.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
33 #include "iputils_common.h"
45 char *tftp_dir = "/etc/tftpboot";
47 extern int ether_ntohost(char *name, unsigned char *ea);
48 void usage(void) __attribute__((noreturn));
55 unsigned char lladdr[16];
57 struct ifaddr *ifa_list;
70 struct rarp_map *next;
75 unsigned char lladdr[16];
83 " rarpd [options] [interface]\n"
85 " -A listen also arp messages\n"
86 " -a listen on all the interfaces\n"
87 " -b <dir> tftpd boot directory\n"
89 " -e /etc/ethers markup alone is fine\n"
91 " -V print version and exit\n"
92 "\nFor more details see rarpd(8).\n"
104 struct ifreq *ifrp, *ifend;
108 struct ifreq ibuf[256];
110 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
111 syslog(LOG_ERR, "socket: %s", strerror(errno));
115 ifc.ifc_len = sizeof ibuf;
116 ifc.ifc_buf = (char *)ibuf;
117 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
118 ifc.ifc_len < (int)sizeof(struct ifreq)) {
119 syslog(LOG_ERR, "SIOCGIFCONF: %s", strerror(errno));
124 while ((ifl = ifl_list) != NULL) {
125 while ((ifa = ifl->ifa_list) != NULL) {
126 ifl->ifa_list = ifa->next;
129 ifl_list = ifl->next;
133 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
134 for (ifrp = ibuf; ifrp < ifend; ifrp++) {
139 if (ifrp->ifr_addr.sa_family != AF_INET)
141 addr = ((struct sockaddr_in*)&ifrp->ifr_addr)->sin_addr.s_addr;
144 if (ioctl(fd, SIOCGIFINDEX, ifrp)) {
145 syslog(LOG_ERR, "ioctl(SIOCGIFNAME): %s", strerror(errno));
148 if (ifidx && ifrp->ifr_ifindex != ifidx)
150 for (ifl = ifl_list; ifl; ifl = ifl->next)
151 if (ifl->index == ifrp->ifr_ifindex)
155 int index = ifrp->ifr_ifindex;
157 if (ioctl(fd, SIOCGIFHWADDR, ifrp)) {
158 syslog(LOG_ERR, "ioctl(SIOCGIFHWADDR): %s", strerror(errno));
162 ifl = (struct iflink*)malloc(sizeof(*ifl));
165 memset(ifl, 0, sizeof(*ifl));
166 ifl->next = ifl_list;
169 ifl->hatype = ifrp->ifr_hwaddr.sa_family;
170 memcpy(ifl->lladdr, ifrp->ifr_hwaddr.sa_data, 14);
171 strncpy(ifl->name, ifrp->ifr_name, IFNAMSIZ);
172 p = strchr(ifl->name, ':');
176 syslog(LOG_INFO, "link %s", ifl->name);
178 if (ioctl(fd, SIOCGIFNETMASK, ifrp)) {
179 syslog(LOG_ERR, "ioctl(SIOCGIFMASK): %s", strerror(errno));
182 mask = ((struct sockaddr_in*)&ifrp->ifr_netmask)->sin_addr.s_addr;
183 if (ioctl(fd, SIOCGIFDSTADDR, ifrp)) {
184 syslog(LOG_ERR, "ioctl(SIOCGIFDSTADDR): %s", strerror(errno));
187 prefix = ((struct sockaddr_in*)&ifrp->ifr_dstaddr)->sin_addr.s_addr;
188 for (ifa = ifl->ifa_list; ifa; ifa = ifa->next) {
189 if (ifa->local == addr &&
190 ifa->prefix == prefix &&
195 if (mask == 0 || prefix == 0)
197 ifa = (struct ifaddr*)malloc(sizeof(*ifa));
198 memset(ifa, 0, sizeof(*ifa));
200 ifa->prefix = prefix;
202 ifa->next = ifl->ifa_list;
208 for (i=32; i>=0; i--) {
209 if (htonl(m) == mask)
213 if (addr == prefix) {
214 syslog(LOG_INFO, " addr %s/%d on %s\n",
215 inet_ntoa(*(struct in_addr*)&addr), i, ifl->name);
218 sprintf(tmpa, "%s", inet_ntoa(*(struct in_addr*)&addr));
219 syslog(LOG_INFO, " addr %s %s/%d on %s\n", tmpa,
220 inet_ntoa(*(struct in_addr*)&prefix), i, ifl->name);
233 int bootable(uint32_t addr)
239 sprintf(name, "%08X", (uint32_t)ntohl(addr));
240 d = opendir(tftp_dir);
242 syslog(LOG_ERR, "opendir: %s", strerror(errno));
245 while ((dent = readdir(d)) != NULL) {
246 if (strncmp(dent->d_name, name, 8) == 0)
253 struct ifaddr *select_ipaddr(int ifindex, uint32_t *sel_addr, uint32_t **alist)
261 for (ifl=ifl_list; ifl; ifl=ifl->next)
262 if (ifl->index == ifindex)
264 if (ifl == NULL && !retry) {
272 for (i=0; alist[i]; i++) {
273 uint32_t addr = *(alist[i]);
274 for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
275 if (!((ifa->prefix^addr)&ifa->mask)) {
280 if (ifa == NULL && retry==0) {
286 if (i==1 && allow_offlink) {
287 *sel_addr = *(alist[0]);
288 return ifl->ifa_list;
290 syslog(LOG_ERR, "Off-link request on %s", ifl->name);
294 struct rarp_map *rarp_lookup(int ifindex, int hatype,
295 int halen, unsigned char *lladdr)
299 for (r=rarp_db; r; r=r->next) {
300 if (r->arp_type != hatype && r->arp_type != -1)
302 if (r->lladdr_len != halen)
304 if (r->ifindex != ifindex && r->ifindex != 0)
306 if (memcmp(r->lladdr, lladdr, halen) == 0)
311 if (hatype == ARPHRD_ETHER && halen == 6) {
315 static struct rarp_map emap = {
316 .arp_type = ARPHRD_ETHER,
320 if (ether_ntohost(ename, lladdr) != 0 ||
321 (hp = gethostbyname(ename)) == NULL) {
323 syslog(LOG_INFO, "not found in /etc/ethers");
326 if (hp->h_addrtype != AF_INET) {
327 syslog(LOG_ERR, "no IP address");
330 ifa = select_ipaddr(ifindex, &emap.ipaddr, (uint32_t **)hp->h_addr_list);
332 memcpy(emap.lladdr, lladdr, 6);
333 if (only_ethers || bootable(emap.ipaddr))
336 syslog(LOG_INFO, "not bootable");
343 static int load_arp_bpflet(int fd)
345 static struct sock_filter insns[] = {
346 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
347 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ARPOP_RREQUEST, 0, 1),
348 BPF_STMT(BPF_RET|BPF_K, 1024),
349 BPF_STMT(BPF_RET|BPF_K, 0),
351 static struct sock_fprog filter = {
352 sizeof insns / sizeof(insns[0]),
356 return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
359 int put_mylladdr(unsigned char **ptr_p, int ifindex, int alen)
363 for (ifl=ifl_list; ifl; ifl = ifl->next)
364 if (ifl->index == ifindex)
370 memcpy(*ptr_p, ifl->lladdr, alen);
375 int put_myipaddr(unsigned char **ptr_p, int ifindex, uint32_t hisipaddr)
381 for (ifl=ifl_list; ifl; ifl = ifl->next)
382 if (ifl->index == ifindex)
388 for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
389 if (!((ifa->prefix^hisipaddr)&ifa->mask)) {
394 memcpy(*ptr_p, &laddr, 4);
399 void arp_advise(int ifindex, unsigned char *lladdr, int lllen, uint32_t ipaddr)
403 struct sockaddr_in *sin;
406 for (ifl=ifl_list; ifl; ifl = ifl->next)
407 if (ifl->index == ifindex)
413 fd = socket(AF_INET, SOCK_DGRAM, 0);
414 memset(&req, 0, sizeof(req));
415 req.arp_flags = ATF_COM;
416 sin = (struct sockaddr_in *)&req.arp_pa;
417 sin->sin_family = AF_INET;
418 sin->sin_addr.s_addr = ipaddr;
419 req.arp_ha.sa_family = ifl->hatype;
420 memcpy(req.arp_ha.sa_data, lladdr, lllen);
421 memcpy(req.arp_dev, ifl->name, IFNAMSIZ);
423 if (ioctl(fd, SIOCSARP, &req))
424 syslog(LOG_ERR, "SIOCSARP: %s", strerror(errno));
428 void serve_it(int fd)
430 unsigned char buf[1024];
431 struct sockaddr_ll sll = { 0 };
432 socklen_t sll_len = sizeof(sll);
433 struct arphdr *a = (struct arphdr*)buf;
434 struct rarp_map *rmap;
438 n = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*)&sll, &sll_len);
440 if (errno != EINTR && errno != EAGAIN)
441 syslog(LOG_ERR, "recvfrom: %s", strerror(errno));
445 /* Do not accept packets for other hosts and our own ones */
446 if (sll.sll_pkttype != PACKET_BROADCAST &&
447 sll.sll_pkttype != PACKET_MULTICAST &&
448 sll.sll_pkttype != PACKET_HOST)
451 if (ifidx && sll.sll_ifindex != ifidx)
454 if ((size_t)n<sizeof(*a)) {
455 syslog(LOG_ERR, "truncated arp packet; len=%zu", n);
459 /* Accept only RARP requests */
460 if (a->ar_op != htons(ARPOP_RREQUEST))
467 for (i=0; i<sll.sll_halen; i++) {
469 sprintf(p, ":%02x", sll.sll_addr[i]);
472 sprintf(p, "%02x", sll.sll_addr[i]);
475 syslog(LOG_INFO, "RARP request from %s on if%d", tmpbuf, sll.sll_ifindex);
480 /* 1. IP only -> pln==4 */
481 if (a->ar_pln != 4) {
482 syslog(LOG_ERR, "interesting rarp_req plen=%d", a->ar_pln);
485 /* 2. ARP protocol must be IP */
486 if (a->ar_pro != htons(ETH_P_IP)) {
487 syslog(LOG_ERR, "rarp protocol is not IP %04x", ntohs(a->ar_pro));
490 /* 3. ARP types must match */
491 if (htons(sll.sll_hatype) != a->ar_hrd) {
492 switch (sll.sll_hatype) {
494 if (a->ar_hrd == htons(ARPHRD_ETHER) ||
495 a->ar_hrd == htons(ARPHRD_IEEE802))
499 syslog(LOG_ERR, "rarp htype mismatch");
503 /* 3. LL address lengths must be equal */
504 if (a->ar_hln != sll.sll_halen) {
505 syslog(LOG_ERR, "rarp hlen mismatch");
508 /* 4. Check packet length */
509 if (sizeof(*a) + 2*4 + 2*a->ar_hln > (size_t) n) {
510 syslog(LOG_ERR, "truncated rarp request; len=%zu", n);
513 /* 5. Silly check: if this guy set different source
514 addresses in MAC header and in ARP, he is insane
516 if (memcmp(sll.sll_addr, a+1, sll.sll_halen)) {
517 syslog(LOG_ERR, "this guy set different his lladdrs in arp and header");
520 /* End of sanity checks */
522 /* Lookup requested target in our database */
523 rmap = rarp_lookup(sll.sll_ifindex, sll.sll_hatype,
524 sll.sll_halen, (unsigned char*)(a+1) + sll.sll_halen + 4);
528 /* Prepare reply. It is almost ready, we only
529 replace ARP packet type, put our lladdr and
530 IP address to source fields,
531 and fill target IP address.
533 a->ar_op = htons(ARPOP_RREPLY);
534 ptr = (unsigned char*)(a+1);
535 if (put_mylladdr(&ptr, sll.sll_ifindex, rmap->lladdr_len))
537 if (put_myipaddr(&ptr, sll.sll_ifindex, rmap->ipaddr))
539 /* It is already filled */
540 ptr += rmap->lladdr_len;
541 memcpy(ptr, &rmap->ipaddr, 4);
544 /* Update our ARP cache. Probably, this guy
545 will not able to make ARP (if it is broken)
547 arp_advise(sll.sll_ifindex, rmap->lladdr, rmap->lladdr_len, rmap->ipaddr);
549 /* Sendto is blocking, but with 5sec timeout */
551 sendto(fd, buf, ptr - buf, 0, (struct sockaddr*)&sll, sizeof(sll));
555 void catch_signal(int sig, void (*handler)(int))
559 memset(&sa, 0, sizeof(sa));
560 sa.sa_handler = handler;
561 sigaction(sig, &sa, NULL);
564 void sig_alarm(int signo __attribute__((__unused__)))
568 void sig_hup(int signo __attribute__((__unused__)))
573 int main(int argc, char **argv)
575 struct pollfd pset[2];
579 atexit(close_stdout);
581 while ((opt = getopt(argc, argv, "aAb:dvoeV")) != EOF) {
611 printf(IPUTILS_VERSION("rarpd"));
620 ifname = argv[optind];
624 pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
628 memset(&ifr, 0, sizeof(ifr));
629 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
630 if (ioctl(pset[0].fd, SIOCGIFINDEX, &ifr)) {
631 error(0, errno, "ioctl(SIOCGIFINDEX)");
634 ifidx = ifr.ifr_ifindex;
639 pset[1].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
640 if (pset[1].fd >= 0) {
641 load_arp_bpflet(pset[1].fd);
646 if (pset[1].fd >= 0) {
647 struct sockaddr_ll sll;
648 memset(&sll, 0, sizeof(sll));
649 sll.sll_family = AF_PACKET;
650 sll.sll_protocol = htons(ETH_P_ARP);
651 sll.sll_ifindex = all_ifaces ? 0 : ifidx;
652 if (bind(pset[1].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
658 if (pset[0].fd >= 0) {
659 struct sockaddr_ll sll;
660 memset(&sll, 0, sizeof(sll));
661 sll.sll_family = AF_PACKET;
662 sll.sll_protocol = htons(ETH_P_RARP);
663 sll.sll_ifindex = all_ifaces ? 0 : ifidx;
664 if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
669 if (pset[0].fd < 0) {
674 error(1, errno, "failed to bind any socket");
677 if (daemon(0, 0) < 0)
678 error(1, errno, "failed to daemon()");
681 openlog("rarpd", LOG_PID | LOG_CONS, LOG_DAEMON);
682 catch_signal(SIGALRM, sig_alarm);
683 catch_signal(SIGHUP, sig_hup);
693 #define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP)
694 pset[0].events = EVENTS;
696 pset[1].events = EVENTS;
699 i = poll(pset, psize, -1);
701 if (errno != EINTR && i<0) {
702 syslog(LOG_ERR, "poll returned some crap: %s\n", strerror(errno));
707 for (i=0; i<psize; i++) {
708 if (pset[i].revents&EVENTS)
709 serve_it(pset[i].fd);