forked from iputils/iputils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rarpd.c
712 lines (632 loc) · 15.1 KB
/
rarpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
* rarpd.c RARP daemon.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*/
#include <stdio.h>
#include <syslog.h>
#include <dirent.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <poll.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_packet.h>
#include <linux/filter.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include "iputils_common.h"
int do_reload = 1;
int debug;
int verbose;
int ifidx;
int allow_offlink;
int only_ethers;
int all_ifaces;
int listen_arp;
char *ifname;
char *tftp_dir = "/etc/tftpboot";
extern int ether_ntohost(char *name, unsigned char *ea);
void usage(void) __attribute__((noreturn));
struct iflink
{
struct iflink *next;
int index;
int hatype;
unsigned char lladdr[16];
char name[IFNAMSIZ];
struct ifaddr *ifa_list;
} *ifl_list;
struct ifaddr
{
struct ifaddr *next;
uint32_t prefix;
uint32_t mask;
uint32_t local;
};
struct rarp_map
{
struct rarp_map *next;
int ifindex;
int arp_type;
int lladdr_len;
unsigned char lladdr[16];
uint32_t ipaddr;
} *rarp_db;
void usage(void)
{
fprintf(stderr,
"\nUsage:\n"
" rarpd [options] [interface]\n"
"\nOptions:\n"
" -A listen also arp messages\n"
" -a listen on all the interfaces\n"
" -b <dir> tftpd boot directory\n"
" -d debug mode\n"
" -e /etc/ethers markup alone is fine\n"
" -v verbose mode\n"
" -V print version and exit\n"
"\nFor more details see rarpd(8).\n"
);
exit(1);
}
void load_db(void)
{
}
void load_if(void)
{
int fd;
struct ifreq *ifrp, *ifend;
struct iflink *ifl;
struct ifaddr *ifa;
struct ifconf ifc;
struct ifreq ibuf[256];
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
syslog(LOG_ERR, "socket: %s", strerror(errno));
return;
}
ifc.ifc_len = sizeof ibuf;
ifc.ifc_buf = (char *)ibuf;
if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
ifc.ifc_len < (int)sizeof(struct ifreq)) {
syslog(LOG_ERR, "SIOCGIFCONF: %s", strerror(errno));
close(fd);
return;
}
while ((ifl = ifl_list) != NULL) {
while ((ifa = ifl->ifa_list) != NULL) {
ifl->ifa_list = ifa->next;
free(ifa);
}
ifl_list = ifl->next;
free(ifl);
}
ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
for (ifrp = ibuf; ifrp < ifend; ifrp++) {
uint32_t addr;
uint32_t mask;
uint32_t prefix;
if (ifrp->ifr_addr.sa_family != AF_INET)
continue;
addr = ((struct sockaddr_in*)&ifrp->ifr_addr)->sin_addr.s_addr;
if (addr == 0)
continue;
if (ioctl(fd, SIOCGIFINDEX, ifrp)) {
syslog(LOG_ERR, "ioctl(SIOCGIFNAME): %s", strerror(errno));
continue;
}
if (ifidx && ifrp->ifr_ifindex != ifidx)
continue;
for (ifl = ifl_list; ifl; ifl = ifl->next)
if (ifl->index == ifrp->ifr_ifindex)
break;
if (ifl == NULL) {
char *p;
int index = ifrp->ifr_ifindex;
if (ioctl(fd, SIOCGIFHWADDR, ifrp)) {
syslog(LOG_ERR, "ioctl(SIOCGIFHWADDR): %s", strerror(errno));
continue;
}
ifl = (struct iflink*)malloc(sizeof(*ifl));
if (ifl == NULL)
continue;
memset(ifl, 0, sizeof(*ifl));
ifl->next = ifl_list;
ifl_list = ifl;
ifl->index = index;
ifl->hatype = ifrp->ifr_hwaddr.sa_family;
memcpy(ifl->lladdr, ifrp->ifr_hwaddr.sa_data, 14);
strncpy(ifl->name, ifrp->ifr_name, IFNAMSIZ);
p = strchr(ifl->name, ':');
if (p)
*p = 0;
if (verbose)
syslog(LOG_INFO, "link %s", ifl->name);
}
if (ioctl(fd, SIOCGIFNETMASK, ifrp)) {
syslog(LOG_ERR, "ioctl(SIOCGIFMASK): %s", strerror(errno));
continue;
}
mask = ((struct sockaddr_in*)&ifrp->ifr_netmask)->sin_addr.s_addr;
if (ioctl(fd, SIOCGIFDSTADDR, ifrp)) {
syslog(LOG_ERR, "ioctl(SIOCGIFDSTADDR): %s", strerror(errno));
continue;
}
prefix = ((struct sockaddr_in*)&ifrp->ifr_dstaddr)->sin_addr.s_addr;
for (ifa = ifl->ifa_list; ifa; ifa = ifa->next) {
if (ifa->local == addr &&
ifa->prefix == prefix &&
ifa->mask == mask)
break;
}
if (ifa == NULL) {
if (mask == 0 || prefix == 0)
continue;
ifa = (struct ifaddr*)malloc(sizeof(*ifa));
memset(ifa, 0, sizeof(*ifa));
ifa->local = addr;
ifa->prefix = prefix;
ifa->mask = mask;
ifa->next = ifl->ifa_list;
ifl->ifa_list = ifa;
if (verbose) {
int i;
uint32_t m = ~0U;
for (i=32; i>=0; i--) {
if (htonl(m) == mask)
break;
m <<= 1;
}
if (addr == prefix) {
syslog(LOG_INFO, " addr %s/%d on %s\n",
inet_ntoa(*(struct in_addr*)&addr), i, ifl->name);
} else {
char tmpa[64];
sprintf(tmpa, "%s", inet_ntoa(*(struct in_addr*)&addr));
syslog(LOG_INFO, " addr %s %s/%d on %s\n", tmpa,
inet_ntoa(*(struct in_addr*)&prefix), i, ifl->name);
}
}
}
}
}
void configure(void)
{
load_if();
load_db();
}
int bootable(uint32_t addr)
{
struct dirent *dent;
DIR *d;
char name[9];
sprintf(name, "%08X", (uint32_t)ntohl(addr));
d = opendir(tftp_dir);
if (d == NULL) {
syslog(LOG_ERR, "opendir: %s", strerror(errno));
return 0;
}
while ((dent = readdir(d)) != NULL) {
if (strncmp(dent->d_name, name, 8) == 0)
break;
}
closedir(d);
return dent != NULL;
}
struct ifaddr *select_ipaddr(int ifindex, uint32_t *sel_addr, uint32_t **alist)
{
struct iflink *ifl;
struct ifaddr *ifa;
int retry = 0;
int i;
retry:
for (ifl=ifl_list; ifl; ifl=ifl->next)
if (ifl->index == ifindex)
break;
if (ifl == NULL && !retry) {
retry++;
load_if();
goto retry;
}
if (ifl == NULL)
return NULL;
for (i=0; alist[i]; i++) {
uint32_t addr = *(alist[i]);
for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
if (!((ifa->prefix^addr)&ifa->mask)) {
*sel_addr = addr;
return ifa;
}
}
if (ifa == NULL && retry==0) {
retry++;
load_if();
goto retry;
}
}
if (i==1 && allow_offlink) {
*sel_addr = *(alist[0]);
return ifl->ifa_list;
}
syslog(LOG_ERR, "Off-link request on %s", ifl->name);
return NULL;
}
struct rarp_map *rarp_lookup(int ifindex, int hatype,
int halen, unsigned char *lladdr)
{
struct rarp_map *r;
for (r=rarp_db; r; r=r->next) {
if (r->arp_type != hatype && r->arp_type != -1)
continue;
if (r->lladdr_len != halen)
continue;
if (r->ifindex != ifindex && r->ifindex != 0)
continue;
if (memcmp(r->lladdr, lladdr, halen) == 0)
break;
}
if (r == NULL) {
if (hatype == ARPHRD_ETHER && halen == 6) {
struct ifaddr *ifa;
struct hostent *hp;
char ename[256];
static struct rarp_map emap = {
.arp_type = ARPHRD_ETHER,
.lladdr_len = 6
};
if (ether_ntohost(ename, lladdr) != 0 ||
(hp = gethostbyname(ename)) == NULL) {
if (verbose)
syslog(LOG_INFO, "not found in /etc/ethers");
return NULL;
}
if (hp->h_addrtype != AF_INET) {
syslog(LOG_ERR, "no IP address");
return NULL;
}
ifa = select_ipaddr(ifindex, &emap.ipaddr, (uint32_t **)hp->h_addr_list);
if (ifa) {
memcpy(emap.lladdr, lladdr, 6);
if (only_ethers || bootable(emap.ipaddr))
return &emap;
if (verbose)
syslog(LOG_INFO, "not bootable");
}
}
}
return r;
}
static int load_arp_bpflet(int fd)
{
static struct sock_filter insns[] = {
BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ARPOP_RREQUEST, 0, 1),
BPF_STMT(BPF_RET|BPF_K, 1024),
BPF_STMT(BPF_RET|BPF_K, 0),
};
static struct sock_fprog filter = {
sizeof insns / sizeof(insns[0]),
insns
};
return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
}
int put_mylladdr(unsigned char **ptr_p, int ifindex, int alen)
{
struct iflink *ifl;
for (ifl=ifl_list; ifl; ifl = ifl->next)
if (ifl->index == ifindex)
break;
if (ifl==NULL)
return -1;
memcpy(*ptr_p, ifl->lladdr, alen);
*ptr_p += alen;
return 0;
}
int put_myipaddr(unsigned char **ptr_p, int ifindex, uint32_t hisipaddr)
{
uint32_t laddr = 0;
struct iflink *ifl;
struct ifaddr *ifa;
for (ifl=ifl_list; ifl; ifl = ifl->next)
if (ifl->index == ifindex)
break;
if (ifl==NULL)
return -1;
for (ifa=ifl->ifa_list; ifa; ifa=ifa->next) {
if (!((ifa->prefix^hisipaddr)&ifa->mask)) {
laddr = ifa->local;
break;
}
}
memcpy(*ptr_p, &laddr, 4);
*ptr_p += 4;
return 0;
}
void arp_advise(int ifindex, unsigned char *lladdr, int lllen, uint32_t ipaddr)
{
int fd;
struct arpreq req;
struct sockaddr_in *sin;
struct iflink *ifl;
for (ifl=ifl_list; ifl; ifl = ifl->next)
if (ifl->index == ifindex)
break;
if (ifl == NULL)
return;
fd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&req, 0, sizeof(req));
req.arp_flags = ATF_COM;
sin = (struct sockaddr_in *)&req.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = ipaddr;
req.arp_ha.sa_family = ifl->hatype;
memcpy(req.arp_ha.sa_data, lladdr, lllen);
memcpy(req.arp_dev, ifl->name, IFNAMSIZ);
if (ioctl(fd, SIOCSARP, &req))
syslog(LOG_ERR, "SIOCSARP: %s", strerror(errno));
close(fd);
}
void serve_it(int fd)
{
unsigned char buf[1024];
struct sockaddr_ll sll = { 0 };
socklen_t sll_len = sizeof(sll);
struct arphdr *a = (struct arphdr*)buf;
struct rarp_map *rmap;
unsigned char *ptr;
ssize_t n;
n = recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*)&sll, &sll_len);
if (n<0) {
if (errno != EINTR && errno != EAGAIN)
syslog(LOG_ERR, "recvfrom: %s", strerror(errno));
return;
}
/* Do not accept packets for other hosts and our own ones */
if (sll.sll_pkttype != PACKET_BROADCAST &&
sll.sll_pkttype != PACKET_MULTICAST &&
sll.sll_pkttype != PACKET_HOST)
return;
if (ifidx && sll.sll_ifindex != ifidx)
return;
if ((size_t)n<sizeof(*a)) {
syslog(LOG_ERR, "truncated arp packet; len=%zu", n);
return;
}
/* Accept only RARP requests */
if (a->ar_op != htons(ARPOP_RREQUEST))
return;
if (verbose) {
int i;
char tmpbuf[16*3];
char *p = tmpbuf;
for (i=0; i<sll.sll_halen; i++) {
if (i) {
sprintf(p, ":%02x", sll.sll_addr[i]);
p++;
} else
sprintf(p, "%02x", sll.sll_addr[i]);
p += 2;
}
syslog(LOG_INFO, "RARP request from %s on if%d", tmpbuf, sll.sll_ifindex);
}
/* Sanity checks */
/* 1. IP only -> pln==4 */
if (a->ar_pln != 4) {
syslog(LOG_ERR, "interesting rarp_req plen=%d", a->ar_pln);
return;
}
/* 2. ARP protocol must be IP */
if (a->ar_pro != htons(ETH_P_IP)) {
syslog(LOG_ERR, "rarp protocol is not IP %04x", ntohs(a->ar_pro));
return;
}
/* 3. ARP types must match */
if (htons(sll.sll_hatype) != a->ar_hrd) {
switch (sll.sll_hatype) {
case ARPHRD_FDDI:
if (a->ar_hrd == htons(ARPHRD_ETHER) ||
a->ar_hrd == htons(ARPHRD_IEEE802))
break;
/* fallthrough */
default:
syslog(LOG_ERR, "rarp htype mismatch");
return;
}
}
/* 3. LL address lengths must be equal */
if (a->ar_hln != sll.sll_halen) {
syslog(LOG_ERR, "rarp hlen mismatch");
return;
}
/* 4. Check packet length */
if (sizeof(*a) + 2*4 + 2*a->ar_hln > (size_t) n) {
syslog(LOG_ERR, "truncated rarp request; len=%zu", n);
return;
}
/* 5. Silly check: if this guy set different source
addresses in MAC header and in ARP, he is insane
*/
if (memcmp(sll.sll_addr, a+1, sll.sll_halen)) {
syslog(LOG_ERR, "this guy set different his lladdrs in arp and header");
return;
}
/* End of sanity checks */
/* Lookup requested target in our database */
rmap = rarp_lookup(sll.sll_ifindex, sll.sll_hatype,
sll.sll_halen, (unsigned char*)(a+1) + sll.sll_halen + 4);
if (rmap == NULL)
return;
/* Prepare reply. It is almost ready, we only
replace ARP packet type, put our lladdr and
IP address to source fields,
and fill target IP address.
*/
a->ar_op = htons(ARPOP_RREPLY);
ptr = (unsigned char*)(a+1);
if (put_mylladdr(&ptr, sll.sll_ifindex, rmap->lladdr_len))
return;
if (put_myipaddr(&ptr, sll.sll_ifindex, rmap->ipaddr))
return;
/* It is already filled */
ptr += rmap->lladdr_len;
memcpy(ptr, &rmap->ipaddr, 4);
ptr += 4;
/* Update our ARP cache. Probably, this guy
will not able to make ARP (if it is broken)
*/
arp_advise(sll.sll_ifindex, rmap->lladdr, rmap->lladdr_len, rmap->ipaddr);
/* Sendto is blocking, but with 5sec timeout */
alarm(5);
sendto(fd, buf, ptr - buf, 0, (struct sockaddr*)&sll, sizeof(sll));
alarm(0);
}
void catch_signal(int sig, void (*handler)(int))
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sigaction(sig, &sa, NULL);
}
void sig_alarm(int signo __attribute__((__unused__)))
{
}
void sig_hup(int signo __attribute__((__unused__)))
{
do_reload = 1;
}
int main(int argc, char **argv)
{
struct pollfd pset[2];
int psize;
int opt;
atexit(close_stdout);
opterr = 0;
while ((opt = getopt(argc, argv, "aAb:dvoeV")) != EOF) {
switch (opt) {
case 'a':
++all_ifaces;
break;
case 'A':
++listen_arp;
break;
case 'd':
++debug;
break;
case 'v':
++verbose;
break;
case 'o':
++allow_offlink;
break;
case 'e':
++only_ethers;
break;
case 'b':
tftp_dir = optarg;
break;
case 'V':
printf(IPUTILS_VERSION("rarpd"));
return 0;
default:
usage();
}
}
if (argc > optind) {
if (argc > optind+1)
usage();
ifname = argv[optind];
}
psize = 1;
pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (ifname) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(pset[0].fd, SIOCGIFINDEX, &ifr)) {
error(0, errno, "ioctl(SIOCGIFINDEX)");
usage();
}
ifidx = ifr.ifr_ifindex;
}
pset[1].fd = -1;
if (listen_arp) {
pset[1].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
if (pset[1].fd >= 0) {
load_arp_bpflet(pset[1].fd);
psize = 1;
}
}
if (pset[1].fd >= 0) {
struct sockaddr_ll sll;
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_ARP);
sll.sll_ifindex = all_ifaces ? 0 : ifidx;
if (bind(pset[1].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
close(pset[1].fd);
pset[1].fd = -1;
psize = 1;
}
}
if (pset[0].fd >= 0) {
struct sockaddr_ll sll;
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_RARP);
sll.sll_ifindex = all_ifaces ? 0 : ifidx;
if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
close(pset[0].fd);
pset[0].fd = -1;
}
}
if (pset[0].fd < 0) {
pset[0] = pset[1];
psize--;
}
if (psize == 0)
error(1, errno, "failed to bind any socket");
if (!debug) {
if (daemon(0, 0) < 0)
error(1, errno, "failed to daemon()");
}
openlog("rarpd", LOG_PID | LOG_CONS, LOG_DAEMON);
catch_signal(SIGALRM, sig_alarm);
catch_signal(SIGHUP, sig_hup);
for (;;) {
int i;
if (do_reload) {
configure();
do_reload = 0;
}
#define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP)
pset[0].events = EVENTS;
pset[0].revents = 0;
pset[1].events = EVENTS;
pset[1].revents = 0;
i = poll(pset, psize, -1);
if (i <= 0) {
if (errno != EINTR && i<0) {
syslog(LOG_ERR, "poll returned some crap: %s\n", strerror(errno));
sleep(10);
}
continue;
}
for (i=0; i<psize; i++) {
if (pset[i].revents&EVENTS)
serve_it(pset[i].fd);
}
}
}