/
rarpd.c
726 lines (605 loc) · 15.9 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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
* Copyright (c) 2012, 2017 Andreas Fett.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include "dispatcher.h"
#include "link.h"
#include "netlink.h"
#include "sighandler.h"
#include "xlog.h"
#define PIDFILE "/var/run/rarpd.pid"
enum {
LISTEN_ALL = 1 << 0,
DEBUG_MODE = 1 << 1,
FOREGROUND = 1 << 2,
LOG_REQ = 1 << 3,
BOOT_FILE = 1 << 4,
};
struct rarpd {
struct nl_ctx nl_ctx;
struct link_array links;
struct dispatcher dispatcher;
struct fd_handler sighandler;
unsigned int opts;
char **ifname;
};
bool in_argv(const char *name, char **ifname)
{
for (; *ifname != NULL; ++ifname) {
if (strcmp(name, *ifname) == 0) {
return true;
}
}
return false;
}
void add_link(struct nl_link *nl_link, void *aux)
{
struct rarpd *rarpd = (struct rarpd *) aux;
if (!(rarpd->opts & LISTEN_ALL) && !in_argv(nl_link->ifname, rarpd->ifname)) {
XLOG_DEBUG("skipping %s: not found in arguments", nl_link->ifname);
return;
}
if (nl_link->iftype != ARPHRD_ETHER) {
XLOG_DEBUG("skipping %s: no ethernet", nl_link->ifname);
return;
}
if (!(nl_link->ifflags & IFF_RUNNING)) {
XLOG_DEBUG("skipping %s: link is down", nl_link->ifname);
return;
}
XLOG_DEBUG("adding link %s: %s",
nl_link->ifname, ether_ntoa(nl_link->ifaddr));
struct link *link = link_array_add(&rarpd->links);
link->ifindex = nl_link->ifindex;
snprintf(link->name, sizeof(link->name), "%s", nl_link->ifname);
memcpy(&link->ether_addr, nl_link->ifaddr, sizeof(struct ether_addr));
link->in_addr = 0;
}
bool link_add_addr(struct link* link, void *aux)
{
struct nl_addr *nl_addr = (struct nl_addr *) aux;
if (link->ifindex != nl_addr->ifindex) {
return true;
}
char addrstr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &nl_addr->ifaddr, addrstr, sizeof(addrstr));
if (link->in_addr != 0) {
char used[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &link->in_addr, used, sizeof(used));
XLOG_WARNING("ignoring address %s for link %s using %s",
addrstr, link->name, used);
return true;
}
XLOG_DEBUG("using address %s for link %s", addrstr, link->name);
link->in_addr = nl_addr->ifaddr;
return true;
}
void add_addr(struct nl_addr *addr, void *aux)
{
struct link_array *links = (struct link_array *) aux;
link_array_foreach(links, link_add_addr, addr);
}
int get_addresses(struct rarpd *rarpd)
{
if (nl_list_addr(&rarpd->nl_ctx) != 0) {
return -1;
}
struct nl_cb cb;
nl_init_addr_cb(&cb, add_addr, &rarpd->links);
return nl_receive(&rarpd->nl_ctx, &cb);
}
bool has_addr(struct link *link, void *aux)
{
(void) aux;
return link->in_addr != 0;
}
int get_links(struct rarpd *rarpd)
{
if (nl_list_links(&rarpd->nl_ctx) != 0) {
return -1;
}
struct nl_cb cb;
nl_init_link_cb(&cb, add_link, rarpd);
return nl_receive(&rarpd->nl_ctx, &cb);
}
int set_promisc(int fd, size_t ifindex)
{
struct packet_mreq mreq;
memset(&mreq, 0, sizeof(struct packet_mreq));
mreq.mr_ifindex = ifindex;
mreq.mr_type = PACKET_MR_PROMISC;
int ret = setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
&mreq, sizeof(struct packet_mreq));
if (ret != 0) {
XLOG_ERR("error setting promisc mode on link %zu: %s",
ifindex, strerror(errno));
return -1;
}
return 0;
}
int do_bind(int fd, size_t ifindex)
{
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
addr.sll_family = PF_PACKET;
addr.sll_ifindex = ifindex;
int ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_ll));
if (ret == -1) {
XLOG_ERR("error binding socket on %zu: %s",
ifindex, strerror(errno));
return -1;
}
return 0;
}
int open_socket()
{
int fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_RARP));
if (fd < 0) {
XLOG_ERR("error opening socket %s", strerror(errno));
return -1;
}
if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
XLOG_ERR("error setting socket nonblocking %s",
strerror(errno));
close(fd);
return -1;
}
return fd;
}
enum dispatch_action
rarp_handler(int fd, short events, void *aux);
bool setup_link(struct link *link, void *aux)
{
int fd = open_socket();
if (fd < 0) {
goto err;
}
if (do_bind(fd, link->ifindex) != 0) {
goto err;
}
if (set_promisc(fd, link->ifindex) != 0) {
goto err;
}
struct dispatcher *dispatcher = (struct dispatcher *) aux;
link->handler = dispatcher_watch(
dispatcher, fd, rarp_handler, link);
dispatcher_flags(&link->handler, POLLIN);
return true;
err:
close(fd);
XLOG_ERR("error setting up %s", link->name);
return false;
}
void dump_packet(char *buf, size_t size)
{
for (size_t i = 0; i < size; ++i) {
fprintf(stderr, "%02x ", (unsigned char) buf[i]);
if (((i+1) % 16) == 0) {
fprintf(stderr, "%s", "\n");
}
}
}
bool check_frame(struct sockaddr_ll *addr, struct link *link)
{
if (ntohs(addr->sll_protocol) != ETH_P_RARP) {
XLOG_WARNING(
"frame check failed: no RARP packet: sll_protocol=0x%x",
ntohs(addr->sll_protocol));
return false;
}
if (addr->sll_ifindex != link->ifindex) {
XLOG_WARNING("frame check failed: wrong interface");
return false;
}
if (addr->sll_hatype != ARPHRD_ETHER) {
XLOG_WARNING("frame check failed: no ethernet");
return false;
}
if (addr->sll_pkttype != PACKET_BROADCAST) {
XLOG_WARNING("frame check failed: no broadcast");
return false;
}
if (addr->sll_halen != sizeof(struct ether_addr)) {
XLOG_WARNING("frame check failed: bad source address length");
return false;
}
return true;
}
bool check_request(const struct ether_arp *req, struct sockaddr_ll *addr)
{
if (ntohs(req->ea_hdr.ar_hrd) != ARPHRD_ETHER) {
XLOG_WARNING("check request: invalid hardware address");
return false;
}
if (ntohs(req->ea_hdr.ar_pro) != ETHERTYPE_IP) {
XLOG_WARNING("check request: invalid ethertype");
return false;
}
if (req->ea_hdr.ar_hln != ETH_ALEN) {
XLOG_WARNING("check request: invalid hardware address length");
return false;
}
if (req->ea_hdr.ar_pln != sizeof(in_addr_t)) {
XLOG_WARNING("check request: invalid protocol address length");
return false;
}
if (ntohs(req->ea_hdr.ar_op) != ARPOP_RREQUEST) {
XLOG_WARNING("check request: invalid rarp opcode");
return false;
}
if (memcmp(req->arp_sha, addr->sll_addr, ETH_ALEN) != 0) {
XLOG_WARNING("check request: spoofed src addr");
return false;
}
XLOG_DEBUG("rarp request for %s from %s",
ether_ntoa((struct ether_addr*)&req->arp_sha),
ether_ntoa((struct ether_addr*)&req->arp_tha));
return true;
}
ssize_t read_request(int fd, struct sockaddr_ll *addr, char *buf, size_t size)
{
socklen_t addrlen = sizeof(struct sockaddr_ll);
ssize_t ret = recvfrom(fd, buf, size, 0, (struct sockaddr *)addr, &addrlen);
if (ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
XLOG_ERR("read error on fd %i: %s", fd, strerror(errno));
return -1;
}
XLOG_DEBUG("read %zi octets", ret);
return ret;
}
/*
This may (and will) block.
Use local /etc/ethers and /etc/hosts
to avoid NIS and DNS queries.
*/
int resolve(struct ether_addr *addr, struct in_addr *in_addr)
{
char hostname[4096];
memset(hostname, 0, sizeof(hostname));
/*
This is not really safe, at the time of writing glibc uses
a 1024 byte buffer for the answer internally ... We could
parse /etc/ethers by ourselves but then we would loose
nsswitch support. Alas RARP support without NIS is no fun :-(
*/
int ret = ether_ntohost(hostname, addr);
if (ret != 0) {
XLOG_DEBUG("failed to lookup %s", ether_ntoa(addr));
return -1;
}
XLOG_DEBUG("lookup for %s returned %s", ether_ntoa(addr), hostname);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_flags = AI_ADDRCONFIG;
struct addrinfo *ai = NULL;
ret = getaddrinfo(hostname, NULL, &hints, &ai);
if (ret != 0) {
XLOG_DEBUG("could not resolve '%s': %s\n", hostname,
ret == EAI_SYSTEM ? strerror(errno) : gai_strerror(ret));
return -1;
}
struct sockaddr_in *sa = (struct sockaddr_in *)(ai->ai_addr);
memcpy(in_addr, &(sa->sin_addr), sizeof(struct in_addr));
return 0;
}
void create_reply(struct ether_arp *reply, struct in_addr *ip, struct link *link)
{
reply->ea_hdr.ar_op = htons(ARPOP_RREPLY);
memcpy(&reply->arp_tha, &reply->arp_sha, sizeof(reply->arp_tha));
memcpy(&reply->arp_tpa, ip, sizeof(reply->arp_tpa));
memcpy(&reply->arp_sha, &link->ether_addr, sizeof(reply->arp_sha));
memcpy(&reply->arp_spa, &link->in_addr, sizeof(reply->arp_spa));
}
/* look for a "bootable" file which we can serve via tftp
* The file must start with the hex encoded ip address
* and be placed in /tftpboot.
* An ss4 that is assigned an address of 192.168.77.17
* for example will try to boot a file named C0A84D11.SUN4M
*/
bool is_bootable(struct in_addr in)
{
const char bootdir[] = "/tftpboot";
char name[9];
snprintf(name, sizeof(name), "%08X", ntohl(*(uint32_t*)(&in)));
XLOG_DEBUG("looking for file matching %s in %s", name, bootdir);
DIR *dir = opendir(bootdir);
if (dir == NULL) {
return false;
}
struct dirent *entry;
do {
entry = readdir(dir);
} while (entry != NULL && strncmp(name, entry->d_name, 8) != 0);
closedir(dir);
return entry != NULL;
}
void handle_request(int fd, struct link *link, bool check_bootable)
{
memset(link->buf, 0, sizeof(link->buf));
memset(&link->src, 0, sizeof(link->src));
ssize_t size = read_request(fd,
&link->src, link->buf, sizeof(link->buf));
if (size <= 0) {
return;
}
if (!check_frame(&link->src, link)) {
return;
}
if ((size_t) size < sizeof(struct ether_arp)) {
XLOG_WARNING("request to short");
return;
}
struct ether_arp *arp_req = (struct ether_arp *)link->buf;
if (!check_request(arp_req, &link->src)) {
return;
}
struct in_addr ip;
memset(&ip, 0, sizeof(in_addr_t));
int ret = resolve((struct ether_addr*)&arp_req->arp_tha, &ip);
if (ret != 0) {
return;
}
XLOG_DEBUG("found address: %s", inet_ntoa(ip));
if (check_bootable == true && !is_bootable(ip)) {
return;
}
create_reply(arp_req, &ip, link);
dispatcher_flags(&link->handler, POLLOUT);
}
int send_reply(int fd, struct link *link)
{
ssize_t ret = sendto(fd, link->buf, sizeof(struct ether_arp), 0,
(struct sockaddr *)&link->src, sizeof(struct sockaddr_ll));
if (ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
XLOG_ERR("write error on fd %i: %s", fd, strerror(errno));
return -1;
}
XLOG_DEBUG("send %zi octets on %s", ret, link->name);
dispatcher_flags(&link->handler, POLLIN);
return 0;
}
enum dispatch_action
rarp_handler(int fd, short events, void *aux)
{
struct link *link = (struct link*) aux;
XLOG_DEBUG("received poll event for %s", link->name);
if (events & POLLIN) {
handle_request(fd, link, false);
} else if (events & POLLOUT) {
send_reply(fd, link);
} else {
XLOG_DEBUG("poll error on %s", link->name);
}
return DISPATCH_CONTINUE;
}
enum dispatch_action
signal_handler(int fd, short events, void *aux)
{
(void) events;
(void) aux;
int ret;
int signo = 0;
do {
ret = read(fd, &signo, sizeof(signo));
} while (ret < 0 && errno == EINTR);
XLOG_INFO("caught signal: %s terminating", strsignal(signo));
return DISPATCH_ABORT;
}
void rarpd_init(struct rarpd *rarpd)
{
memset(rarpd, 0, sizeof(struct rarpd));
rarpd->nl_ctx.fd = -1;
dispatcher_init(&rarpd->dispatcher);
}
int rarpd_init_signals(struct rarpd *rarpd)
{
int signalfd = install_signal_fd();
if (signalfd < 0) {
return -1;
}
rarpd->sighandler = dispatcher_watch(
&rarpd->dispatcher, signalfd, signal_handler, NULL);
dispatcher_flags(&rarpd->sighandler, POLLIN);
return 0;
}
int write_pidfile()
{
int fd = open(PIDFILE, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0644);
if (fd < 0) {
XLOG_ERR("could not open pidfile: %s", strerror(errno));
return -1;
}
FILE *pidfile = fdopen(fd, "w");
if (pidfile == NULL) {
XLOG_ERR("could not fdopen pidfile: %s", strerror(errno));
return -1;
}
fprintf(pidfile, "%u", getpid());
fclose(pidfile);
return 0;
}
void usage(const char* msg)
{
if (msg != NULL) {
fprintf(stderr, "%s\n", msg);
}
fprintf(stderr, "Usage: rarpd [-adflt] interface ...\n");
}
int parse_options(struct rarpd* rarpd, int argc, char *argv[])
{
for (;;) {
int opt = getopt(argc, argv, "adflt");
switch (opt) {
case -1:
return 0;
case 'a':
rarpd->opts |= LISTEN_ALL;
break;
case 'd':
rarpd->opts |= (DEBUG_MODE|FOREGROUND);
break;
case 'f':
rarpd->opts |= FOREGROUND;
break;
case 'l':
rarpd->opts |= LOG_REQ;
break;
case 't':
rarpd->opts |= BOOT_FILE;
break;
default:
usage(NULL);
return -1;
}
}
}
int parse_args(struct rarpd* rarpd, char *argv[])
{
if (rarpd->opts & LISTEN_ALL) {
if (argv[optind] != NULL) {
usage("found extra arguments, but -a given");
return -1;
}
return 0;
}
if (argv[optind] == NULL) {
usage("no interfaces specified, use -a for all interfaces");
return -1;
}
rarpd->ifname = &argv[optind];
return 0;
}
int find_interfaces(struct rarpd *rarpd)
{
if (nl_open(&rarpd->nl_ctx) != 0) {
return -1;
}
if (get_links(rarpd) != 0) {
return -1;
}
if (get_addresses(rarpd) != 0) {
return -1;
}
nl_close(&rarpd->nl_ctx);
return 0;
}
int daemonize()
{
pid_t pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
if (write_pidfile() != 0) {
return -1;
}
int ret = setsid();
if (ret == -1) {
XLOG_ERR("failed to set session id: %s", strerror(errno));
unlink(PIDFILE);
return -1;
}
for (int fd = getdtablesize(); fd >= 0; --fd) {
close(fd);
}
chdir("/");
int fd = open("/dev/null", O_RDWR);
dup(fd);
dup(fd);
return 0;
}
void cleanup_rarpd(struct rarpd *rarpd)
{
unlink(PIDFILE);
link_array_free(&rarpd->links);
dispatcher_cleanup(&rarpd->dispatcher);
}
void init_syslog(struct rarpd *rarpd)
{
int logopt = LOG_PID;
if (rarpd->opts & DEBUG_MODE) {
logopt |= LOG_PERROR;
}
openlog("rarpd", logopt, LOG_DAEMON);
if (!(rarpd->opts & DEBUG_MODE)) {
setlogmask(LOG_UPTO(LOG_INFO));
}
}
int rarpd(int argc, char *argv[])
{
struct rarpd rarpd;
rarpd_init(&rarpd);
if (parse_options(&rarpd, argc, argv) != 0) {
return EXIT_FAILURE;
}
if (parse_args(&rarpd, argv) != 0) {
return EXIT_FAILURE;
}
if (!(rarpd.opts & FOREGROUND) && daemonize() != 0) {
return EXIT_FAILURE;
}
init_syslog(&rarpd);
if (rarpd_init_signals(&rarpd) != 0) {
return EXIT_FAILURE;
}
if (find_interfaces(&rarpd) != 0) {
return EXIT_FAILURE;
}
link_array_filter(&rarpd.links, has_addr, NULL);
if (rarpd.links.count == 0) {
XLOG_ERR("no usable links found");
return EXIT_FAILURE;
}
if (!link_array_foreach(&rarpd.links, setup_link, &rarpd.dispatcher)) {
cleanup_rarpd(&rarpd);
return EXIT_FAILURE;
}
dispatcher_run(&rarpd.dispatcher);
cleanup_rarpd(&rarpd);
return EXIT_SUCCESS;
}