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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
/*
* Copyright (c) 1990, 1991, 1992, 1993, 1996
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#if 0
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1990, 1991, 1992, 1993, 1996\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* rarpd - Reverse ARP Daemon
*
* Usage: rarpd -a [ -dfsv ] [-t directory] [ hostname ]
* rarpd [ -dfsv ] [-t directory] interface [ hostname ]
*
* 'hostname' is optional solely for backwards compatibility with Sun's rarpd.
* Currently, the argument is ignored.
*/
#include <sys/param.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#if defined(SUNOS4) || defined(__FreeBSD__) /* XXX */
#define HAVE_DIRENT_H
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#else
#include <sys/dir.h>
#endif
/* Cast a struct sockaddr to a structaddr_in */
#define SATOSIN(sa) ((struct sockaddr_in *)(sa))
#ifndef TFTP_DIR
#define TFTP_DIR "/tftpboot"
#endif
#if BSD >= 199200
#define ARPSECS (20 * 60) /* as per code in netinet/if_ether.c */
#define REVARP_REQUEST ARPOP_REVREQUEST
#define REVARP_REPLY ARPOP_REVREPLY
#endif
#ifndef ETHERTYPE_REVARP
#define ETHERTYPE_REVARP 0x8035
#define REVARP_REQUEST 3
#define REVARP_REPLY 4
#endif
/*
* Map field names in ether_arp struct. What a pain in the neck.
*/
#ifdef SUNOS3
#undef arp_sha
#undef arp_spa
#undef arp_tha
#undef arp_tpa
#define arp_sha arp_xsha
#define arp_spa arp_xspa
#define arp_tha arp_xtha
#define arp_tpa arp_xtpa
#endif
/*
* The structure for each interface.
*/
struct if_info {
struct if_info *ii_next;
int ii_fd; /* BPF file descriptor */
in_addr_t ii_ipaddr; /* IP address of this interface */
in_addr_t ii_netmask; /* subnet or net mask */
u_char ii_eaddr[6]; /* Ethernet address of this interface */
char ii_ifname[sizeof(((struct ifreq *)0)->ifr_name) + 1];
};
/*
* The list of all interfaces that are being listened to. rarp_loop()
* "selects" on the descriptors in this list.
*/
struct if_info *iflist;
int verbose; /* verbose messages */
int s; /* inet datagram socket */
const char *tftp_dir = TFTP_DIR; /* tftp directory */
int dflag; /* messages to stdout/stderr, not syslog(3) */
int sflag; /* ignore /tftpboot */
static u_char zero[6];
static int bpf_open(void);
static in_addr_t choose_ipaddr(in_addr_t **, in_addr_t, in_addr_t);
static char *eatoa(u_char *);
static int expand_syslog_m(const char *fmt, char **newfmt);
static void init(char *);
static void init_one(struct ifreq *, char *);
static char *intoa(in_addr_t);
static in_addr_t ipaddrtonetmask(in_addr_t);
static void logmsg(int, const char *, ...) __printflike(2, 3);
static int rarp_bootable(in_addr_t);
static int rarp_check(u_char *, u_int);
static void rarp_loop(void);
static int rarp_open(char *);
static void rarp_process(struct if_info *, u_char *, u_int);
static void rarp_reply(struct if_info *, struct ether_header *,
in_addr_t, u_int);
static void update_arptab(u_char *, in_addr_t);
static void usage(void);
int
main(int argc, char *argv[])
{
int op;
char *ifname, *hostname, *name;
int aflag = 0; /* listen on "all" interfaces */
int fflag = 0; /* don't fork */
if ((name = strrchr(argv[0], '/')) != NULL)
++name;
else
name = argv[0];
if (*name == '-')
++name;
/*
* All error reporting is done through syslog, unless -d is specified
*/
openlog(name, LOG_PID | LOG_CONS, LOG_DAEMON);
opterr = 0;
while ((op = getopt(argc, argv, "adfst:v")) != -1) {
switch (op) {
case 'a':
++aflag;
break;
case 'd':
++dflag;
break;
case 'f':
++fflag;
break;
case 's':
++sflag;
break;
case 't':
tftp_dir = optarg;
break;
case 'v':
++verbose;
break;
default:
usage();
/* NOTREACHED */
}
}
ifname = argv[optind++];
hostname = ifname ? argv[optind] : NULL;
if ((aflag && ifname) || (!aflag && ifname == NULL))
usage();
if (aflag)
init(NULL);
else
init(ifname);
if (!fflag) {
if (daemon(0,0)) {
logmsg(LOG_ERR, "cannot fork");
exit(1);
}
}
rarp_loop();
return(0);
}
/*
* Add to the interface list.
*/
void
init_one(struct ifreq *ifrp, char *target)
{
struct if_info *ii;
struct sockaddr_dl *ll;
int family;
struct ifreq ifr;
family = ifrp->ifr_addr.sa_family;
switch (family) {
case AF_INET:
#if BSD >= 199100
case AF_LINK:
#endif
(void)strncpy(ifr.ifr_name, ifrp->ifr_name,
sizeof(ifrp->ifr_name));
if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) == -1) {
logmsg(LOG_ERR,
"SIOCGIFFLAGS: %.*s: %m",
(int)sizeof(ifrp->ifr_name), ifrp->ifr_name);
exit(1);
}
if ((ifr.ifr_flags & IFF_UP) == 0 ||
(ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0)
return;
break;
default:
return;
}
/* Don't bother going any further if not the target interface */
if (target != NULL &&
strncmp(ifrp->ifr_name, target, sizeof(ifrp->ifr_name)) != 0)
return;
/* Look for interface in list */
for (ii = iflist; ii != NULL; ii = ii->ii_next)
if (strncmp(ifrp->ifr_name, ii->ii_ifname,
sizeof(ifrp->ifr_name)) == 0)
break;
/* Allocate a new one if not found */
if (ii == NULL) {
ii = (struct if_info *)malloc(sizeof(*ii));
if (ii == NULL) {
logmsg(LOG_ERR, "malloc: %m");
exit(1);
}
bzero(ii, sizeof(*ii));
ii->ii_fd = -1;
(void)strncpy(ii->ii_ifname, ifrp->ifr_name,
sizeof(ifrp->ifr_name));
ii->ii_ifname[sizeof(ii->ii_ifname) - 1] = '\0';
ii->ii_next = iflist;
iflist = ii;
}
switch (family) {
case AF_INET:
if (ioctl(s, SIOCGIFADDR, (char *)&ifr) == -1) {
logmsg(LOG_ERR, "ipaddr SIOCGIFADDR: %s: %m",
ii->ii_ifname);
exit(1);
}
ii->ii_ipaddr = SATOSIN(&ifr.ifr_addr)->sin_addr.s_addr;
if (ioctl(s, SIOCGIFNETMASK, (char *)&ifr) == -1) {
logmsg(LOG_ERR, "SIOCGIFNETMASK: %m");
exit(1);
}
ii->ii_netmask = SATOSIN(&ifr.ifr_addr)->sin_addr.s_addr;
if (ii->ii_netmask == 0)
ii->ii_netmask = ipaddrtonetmask(ii->ii_ipaddr);
if (ii->ii_fd < 0) {
ii->ii_fd = rarp_open(ii->ii_ifname);
#if BSD < 199100
/* Use BPF descriptor to get ethernet address. */
if (ioctl(ii->ii_fd, SIOCGIFADDR, (char *)&ifr) == -1) {
logmsg(LOG_ERR, "eaddr SIOCGIFADDR: %s: %m",
ii->ii_ifname);
exit(1);
}
bcopy(&ifr.ifr_addr.sa_data[0], ii->ii_eaddr, 6);
#endif
}
break;
#if BSD >= 199100
case AF_LINK:
ll = (struct sockaddr_dl *)&ifrp->ifr_addr;
if (ll->sdl_type == IFT_ETHER)
bcopy(LLADDR(ll), ii->ii_eaddr, 6);
break;
#endif
}
}
/*
* Initialize all "candidate" interfaces that are in the system
* configuration list. A "candidate" is up, not loopback and not
* point to point.
*/
void
init(char *target)
{
u_int n;
struct ifreq *ifrp, *ifend;
struct if_info *ii, *nii, *lii;
struct ifconf ifc;
struct ifreq ibuf[16];
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
logmsg(LOG_ERR, "socket: %m");
exit(1);
}
ifc.ifc_len = sizeof ibuf;
ifc.ifc_buf = (caddr_t)ibuf;
if ((ioctl(s, SIOCGIFCONF, (char *)&ifc) == -1) ||
((u_int)ifc.ifc_len < sizeof(struct ifreq))) {
logmsg(LOG_ERR, "SIOCGIFCONF: %m");
exit(1);
}
ifrp = ibuf;
ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
while (ifrp < ifend) {
init_one(ifrp, target);
#if BSD >= 199100
n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
if (n < sizeof(*ifrp))
n = sizeof(*ifrp);
ifrp = (struct ifreq *)((char *)ifrp + n);
#else
++ifrp;
#endif
}
/* Throw away incomplete interfaces */
lii = NULL;
for (ii = iflist; ii != NULL; ii = nii) {
nii = ii->ii_next;
if (ii->ii_ipaddr == 0 ||
bcmp(ii->ii_eaddr, zero, 6) == 0) {
if (lii == NULL)
iflist = nii;
else
lii->ii_next = nii;
if (ii->ii_fd >= 0)
close(ii->ii_fd);
free(ii);
continue;
}
lii = ii;
}
/* Verbose stuff */
if (verbose)
for (ii = iflist; ii != NULL; ii = ii->ii_next)
logmsg(LOG_DEBUG, "%s %s 0x%08x %s",
ii->ii_ifname, intoa(ntohl(ii->ii_ipaddr)),
(in_addr_t)ntohl(ii->ii_netmask), eatoa(ii->ii_eaddr));
}
void
usage(void)
{
(void)fprintf(stderr, "usage: rarpd [-adfsv] [-t directory] [interface]\n");
exit(1);
}
int
bpf_open(void)
{
int fd;
int n = 0;
char device[sizeof "/dev/bpf000"];
/*
* Go through all the minors and find one that isn't in use.
*/
do {
(void)sprintf(device, "/dev/bpf%d", n++);
fd = open(device, O_RDWR);
} while ((fd == -1) && (errno == EBUSY));
if (fd == -1) {
logmsg(LOG_ERR, "%s: %m", device);
exit(1);
}
return fd;
}
/*
* Open a BPF file and attach it to the interface named 'device'.
* Set immediate mode, and set a filter that accepts only RARP requests.
*/
int
rarp_open(char *device)
{
int fd;
struct ifreq ifr;
u_int dlt;
int immediate;
static struct bpf_insn insns[] = {
BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 12),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETHERTYPE_REVARP, 0, 3),
BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 20),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, REVARP_REQUEST, 0, 1),
BPF_STMT(BPF_RET|BPF_K, sizeof(struct ether_arp) +
sizeof(struct ether_header)),
BPF_STMT(BPF_RET|BPF_K, 0),
};
static struct bpf_program filter = {
sizeof insns / sizeof(insns[0]),
insns
};
fd = bpf_open();
/*
* Set immediate mode so packets are processed as they arrive.
*/
immediate = 1;
if (ioctl(fd, BIOCIMMEDIATE, &immediate) == -1) {
logmsg(LOG_ERR, "BIOCIMMEDIATE: %m");
exit(1);
}
(void)strncpy(ifr.ifr_name, device, sizeof ifr.ifr_name);
if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) == -1) {
logmsg(LOG_ERR, "BIOCSETIF: %m");
exit(1);
}
/*
* Check that the data link layer is an Ethernet; this code won't
* work with anything else.
*/
if (ioctl(fd, BIOCGDLT, (caddr_t)&dlt) == -1) {
logmsg(LOG_ERR, "BIOCGDLT: %m");
exit(1);
}
if (dlt != DLT_EN10MB) {
logmsg(LOG_ERR, "%s is not an ethernet", device);
exit(1);
}
/*
* Set filter program.
*/
if (ioctl(fd, BIOCSETF, (caddr_t)&filter) == -1) {
logmsg(LOG_ERR, "BIOCSETF: %m");
exit(1);
}
return fd;
}
/*
* Perform various sanity checks on the RARP request packet. Return
* false on failure and log the reason.
*/
int
rarp_check(u_char *p, u_int len)
{
struct ether_header *ep = (struct ether_header *)p;
struct ether_arp *ap = (struct ether_arp *)(p + sizeof(*ep));
if (len < sizeof(*ep) + sizeof(*ap)) {
logmsg(LOG_ERR, "truncated request, got %u, expected %lu",
len, (u_long)(sizeof(*ep) + sizeof(*ap)));
return 0;
}
/*
* XXX This test might be better off broken out...
*/
if (ntohs(ep->ether_type) != ETHERTYPE_REVARP ||
ntohs(ap->arp_hrd) != ARPHRD_ETHER ||
ntohs(ap->arp_op) != REVARP_REQUEST ||
ntohs(ap->arp_pro) != ETHERTYPE_IP ||
ap->arp_hln != 6 || ap->arp_pln != 4) {
logmsg(LOG_DEBUG, "request fails sanity check");
return 0;
}
if (bcmp((char *)&ep->ether_shost, (char *)&ap->arp_sha, 6) != 0) {
logmsg(LOG_DEBUG, "ether/arp sender address mismatch");
return 0;
}
if (bcmp((char *)&ap->arp_sha, (char *)&ap->arp_tha, 6) != 0) {
logmsg(LOG_DEBUG, "ether/arp target address mismatch");
return 0;
}
return 1;
}
#ifndef FD_SETSIZE
#define FD_SET(n, fdp) ((fdp)->fds_bits[0] |= (1 << (n)))
#define FD_ISSET(n, fdp) ((fdp)->fds_bits[0] & (1 << (n)))
#define FD_ZERO(fdp) ((fdp)->fds_bits[0] = 0)
#endif
/*
* Loop indefinitely listening for RARP requests on the
* interfaces in 'iflist'.
*/
void
rarp_loop(void)
{
u_char *buf, *bp, *ep;
int cc, fd;
fd_set fds, listeners;
int bufsize, maxfd = 0;
struct if_info *ii;
if (iflist == NULL) {
logmsg(LOG_ERR, "no interfaces");
exit(1);
}
if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) == -1) {
logmsg(LOG_ERR, "BIOCGBLEN: %m");
exit(1);
}
buf = malloc(bufsize);
if (buf == NULL) {
logmsg(LOG_ERR, "malloc: %m");
exit(1);
}
while (1) {
/*
* Find the highest numbered file descriptor for select().
* Initialize the set of descriptors to listen to.
*/
FD_ZERO(&fds);
for (ii = iflist; ii != NULL; ii = ii->ii_next) {
FD_SET(ii->ii_fd, &fds);
if (ii->ii_fd > maxfd)
maxfd = ii->ii_fd;
}
listeners = fds;
if (select(maxfd + 1, &listeners, NULL, NULL, NULL) == -1) {
/* Don't choke when we get ptraced */
if (errno == EINTR)
continue;
logmsg(LOG_ERR, "select: %m");
exit(1);
}
for (ii = iflist; ii != NULL; ii = ii->ii_next) {
fd = ii->ii_fd;
if (!FD_ISSET(fd, &listeners))
continue;
again:
cc = read(fd, (char *)buf, bufsize);
/* Don't choke when we get ptraced */
if ((cc == -1) && (errno == EINTR))
goto again;
#if defined(SUNOS3) || defined(SUNOS4)
/*
* Due to a SunOS bug, after 2^31 bytes, the
* file offset overflows and read fails with
* EINVAL. The lseek() to 0 will fix things.
*/
if (cc == -1) {
if (errno == EINVAL &&
(long)(tell(fd) + bufsize) < 0) {
(void)lseek(fd, 0, 0);
goto again;
}
logmsg(LOG_ERR, "read: %m");
exit(1);
}
#endif
/* Loop through the packet(s) */
#define bhp ((struct bpf_hdr *)bp)
bp = buf;
ep = bp + cc;
while (bp < ep) {
u_int caplen, hdrlen;
caplen = bhp->bh_caplen;
hdrlen = bhp->bh_hdrlen;
if (rarp_check(bp + hdrlen, caplen))
rarp_process(ii, bp + hdrlen, caplen);
bp += BPF_WORDALIGN(hdrlen + caplen);
}
}
}
#undef bhp
}
/*
* True if this server can boot the host whose IP address is 'addr'.
* This check is made by looking in the tftp directory for the
* configuration file.
*/
int
rarp_bootable(in_addr_t addr)
{
#ifdef HAVE_DIRENT_H
struct dirent *dent;
#else
struct direct *dent;
#endif
DIR *d;
char ipname[9];
static DIR *dd = NULL;
(void)sprintf(ipname, "%08X", (in_addr_t)ntohl(addr));
/*
* If directory is already open, rewind it. Otherwise, open it.
*/
if ((d = dd) != NULL)
rewinddir(d);
else {
if (chdir(tftp_dir) == -1) {
logmsg(LOG_ERR, "chdir: %s: %m", tftp_dir);
exit(1);
}
d = opendir(".");
if (d == NULL) {
logmsg(LOG_ERR, "opendir: %m");
exit(1);
}
dd = d;
}
while ((dent = readdir(d)) != NULL)
if (strncmp(dent->d_name, ipname, 8) == 0)
return 1;
return 0;
}
/*
* Given a list of IP addresses, 'alist', return the first address that
* is on network 'net'; 'netmask' is a mask indicating the network portion
* of the address.
*/
in_addr_t
choose_ipaddr(in_addr_t **alist, in_addr_t net, in_addr_t netmask)
{
for (; *alist; ++alist)
if ((**alist & netmask) == net)
return **alist;
return 0;
}
/*
* Answer the RARP request in 'pkt', on the interface 'ii'. 'pkt' has
* already been checked for validity. The reply is overlaid on the request.
*/
void
rarp_process(struct if_info *ii, u_char *pkt, u_int len)
{
struct ether_header *ep;
struct hostent *hp;
in_addr_t target_ipaddr;
char ename[256];
ep = (struct ether_header *)pkt;
/* should this be arp_tha? */
if (ether_ntohost(ename, (struct ether_addr *)&ep->ether_shost) != 0) {
logmsg(LOG_ERR, "cannot map %s to name",
eatoa(ep->ether_shost));
return;
}
if ((hp = gethostbyname(ename)) == NULL) {
logmsg(LOG_ERR, "cannot map %s to IP address", ename);
return;
}
/*
* Choose correct address from list.
*/
if (hp->h_addrtype != AF_INET) {
logmsg(LOG_ERR, "cannot handle non IP addresses for %s",
ename);
return;
}
target_ipaddr = choose_ipaddr((in_addr_t **)hp->h_addr_list,
ii->ii_ipaddr & ii->ii_netmask,
ii->ii_netmask);
if (target_ipaddr == 0) {
logmsg(LOG_ERR, "cannot find %s on net %s",
ename, intoa(ntohl(ii->ii_ipaddr & ii->ii_netmask)));
return;
}
if (sflag || rarp_bootable(target_ipaddr))
rarp_reply(ii, ep, target_ipaddr, len);
else if (verbose > 1)
logmsg(LOG_INFO, "%s %s at %s DENIED (not bootable)",
ii->ii_ifname,
eatoa(ep->ether_shost),
intoa(ntohl(target_ipaddr)));
}
/*
* Poke the kernel arp tables with the ethernet/ip address combinataion
* given. When processing a reply, we must do this so that the booting
* host (i.e. the guy running rarpd), won't try to ARP for the hardware
* address of the guy being booted (he cannot answer the ARP).
*/
#if BSD >= 199200
struct sockaddr_inarp sin_inarp = {
sizeof(struct sockaddr_inarp), AF_INET, 0,
{0},
{0},
0, 0
};
struct sockaddr_dl sin_dl = {
sizeof(struct sockaddr_dl), AF_LINK, 0, IFT_ETHER, 0, 6,
0, ""
};
struct {
struct rt_msghdr rthdr;
char rtspace[512];
} rtmsg;
void
update_arptab(u_char *ep, in_addr_t ipaddr)
{
int cc;
struct sockaddr_inarp *ar, *ar2;
struct sockaddr_dl *ll, *ll2;
struct rt_msghdr *rt;
int xtype, xindex;
static pid_t pid;
int r;
static int seq;
r = socket(PF_ROUTE, SOCK_RAW, 0);
if (r == -1) {
logmsg(LOG_ERR, "raw route socket: %m");
exit(1);
}
pid = getpid();
ar = &sin_inarp;
ar->sin_addr.s_addr = ipaddr;
ll = &sin_dl;
bcopy(ep, LLADDR(ll), 6);
/* Get the type and interface index */
rt = &rtmsg.rthdr;
bzero(rt, sizeof(rtmsg));
rt->rtm_version = RTM_VERSION;
rt->rtm_addrs = RTA_DST;
rt->rtm_type = RTM_GET;
rt->rtm_seq = ++seq;
ar2 = (struct sockaddr_inarp *)rtmsg.rtspace;
bcopy(ar, ar2, sizeof(*ar));
rt->rtm_msglen = sizeof(*rt) + sizeof(*ar);
errno = 0;
if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != ESRCH)) {
logmsg(LOG_ERR, "rtmsg get write: %m");
close(r);
return;
}
do {
cc = read(r, rt, sizeof(rtmsg));
} while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid));
if (cc == -1) {
logmsg(LOG_ERR, "rtmsg get read: %m");
close(r);
return;
}
ll2 = (struct sockaddr_dl *)((u_char *)ar2 + ar2->sin_len);
if (ll2->sdl_family != AF_LINK) {
/*
* XXX I think this means the ip address is not on a
* directly connected network (the family is AF_INET in
* this case).
*/
logmsg(LOG_ERR, "bogus link family (%d) wrong net for %08X?\n",
ll2->sdl_family, ipaddr);
close(r);
return;
}
xtype = ll2->sdl_type;
xindex = ll2->sdl_index;
/* Set the new arp entry */
bzero(rt, sizeof(rtmsg));
rt->rtm_version = RTM_VERSION;
rt->rtm_addrs = RTA_DST | RTA_GATEWAY;
rt->rtm_inits = RTV_EXPIRE;
rt->rtm_rmx.rmx_expire = time(0) + ARPSECS;
rt->rtm_flags = RTF_HOST | RTF_STATIC;
rt->rtm_type = RTM_ADD;
rt->rtm_seq = ++seq;
bcopy(ar, ar2, sizeof(*ar));
ll2 = (struct sockaddr_dl *)((u_char *)ar2 + sizeof(*ar2));
bcopy(ll, ll2, sizeof(*ll));
ll2->sdl_type = xtype;
ll2->sdl_index = xindex;
rt->rtm_msglen = sizeof(*rt) + sizeof(*ar2) + sizeof(*ll2);
errno = 0;
if ((write(r, rt, rt->rtm_msglen) == -1) && (errno != EEXIST)) {
logmsg(LOG_ERR, "rtmsg add write: %m");
close(r);
return;
}
do {
cc = read(r, rt, sizeof(rtmsg));
} while (cc > 0 && (rt->rtm_seq != seq || rt->rtm_pid != pid));
close(r);
if (cc == -1) {
logmsg(LOG_ERR, "rtmsg add read: %m");
return;
}
}
#else
void
update_arptab(u_char *ep, in_addr_t ipaddr)
{
struct arpreq request;
struct sockaddr_in *sin;
request.arp_flags = 0;
sin = (struct sockaddr_in *)&request.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = ipaddr;
request.arp_ha.sa_family = AF_UNSPEC;
bcopy((char *)ep, (char *)request.arp_ha.sa_data, 6);
if (ioctl(s, SIOCSARP, (caddr_t)&request) == -1)
logmsg(LOG_ERR, "SIOCSARP: %m");
}
#endif
/*
* Build a reverse ARP packet and sent it out on the interface.
* 'ep' points to a valid REVARP_REQUEST. The REVARP_REPLY is built
* on top of the request, then written to the network.
*
* RFC 903 defines the ether_arp fields as follows. The following comments
* are taken (more or less) straight from this document.
*
* REVARP_REQUEST
*
* arp_sha is the hardware address of the sender of the packet.
* arp_spa is undefined.
* arp_tha is the 'target' hardware address.
* In the case where the sender wishes to determine his own
* protocol address, this, like arp_sha, will be the hardware
* address of the sender.
* arp_tpa is undefined.
*
* REVARP_REPLY
*
* arp_sha is the hardware address of the responder (the sender of the
* reply packet).
* arp_spa is the protocol address of the responder (see the note below).
* arp_tha is the hardware address of the target, and should be the same as
* that which was given in the request.
* arp_tpa is the protocol address of the target, that is, the desired address.
*
* Note that the requirement that arp_spa be filled in with the responder's
* protocol is purely for convenience. For instance, if a system were to use
* both ARP and RARP, then the inclusion of the valid protocol-hardware
* address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
* ARP request.
*/
void
rarp_reply(struct if_info *ii, struct ether_header *ep, in_addr_t ipaddr,
u_int len)
{
u_int n;
struct ether_arp *ap = (struct ether_arp *)(ep + 1);
update_arptab((u_char *)&ap->arp_sha, ipaddr);
/*
* Build the rarp reply by modifying the rarp request in place.
*/
ap->arp_op = htons(REVARP_REPLY);
#ifdef BROKEN_BPF
ep->ether_type = ETHERTYPE_REVARP;
#endif
bcopy((char *)&ap->arp_sha, (char *)&ep->ether_dhost, 6);
bcopy((char *)ii->ii_eaddr, (char *)&ep->ether_shost, 6);
bcopy((char *)ii->ii_eaddr, (char *)&ap->arp_sha, 6);
bcopy((char *)&ipaddr, (char *)ap->arp_tpa, 4);
/* Target hardware is unchanged. */
bcopy((char *)&ii->ii_ipaddr, (char *)ap->arp_spa, 4);
/* Zero possible garbage after packet. */
bzero((char *)ep + (sizeof(*ep) + sizeof(*ap)),
len - (sizeof(*ep) + sizeof(*ap)));
n = write(ii->ii_fd, (char *)ep, len);
if (n != len)
logmsg(LOG_ERR, "write: only %d of %d bytes written", n, len);
if (verbose)
logmsg(LOG_INFO, "%s %s at %s REPLIED", ii->ii_ifname,
eatoa(ap->arp_tha),
intoa(ntohl(ipaddr)));
}
/*
* Get the netmask of an IP address. This routine is used if
* SIOCGIFNETMASK doesn't work.
*/
in_addr_t
ipaddrtonetmask(in_addr_t addr)
{
addr = ntohl(addr);
if (IN_CLASSA(addr))
return htonl(IN_CLASSA_NET);
if (IN_CLASSB(addr))
return htonl(IN_CLASSB_NET);
if (IN_CLASSC(addr))
return htonl(IN_CLASSC_NET);
logmsg(LOG_DEBUG, "unknown IP address class: %08X", addr);
return htonl(0xffffffff);
}
/*
* A faster replacement for inet_ntoa().
*/
char *
intoa(in_addr_t addr)
{
char *cp;
u_int byte;
int n;
static char buf[sizeof(".xxx.xxx.xxx.xxx")];
cp = &buf[sizeof buf];
*--cp = '\0';
n = 4;
do {
byte = addr & 0xff;
*--cp = byte % 10 + '0';
byte /= 10;
if (byte > 0) {
*--cp = byte % 10 + '0';
byte /= 10;
if (byte > 0)
*--cp = byte + '0';
}
*--cp = '.';
addr >>= 8;
} while (--n > 0);
return cp + 1;
}
char *
eatoa(u_char *ea)
{
static char buf[sizeof("xx:xx:xx:xx:xx:xx")];
(void)sprintf(buf, "%x:%x:%x:%x:%x:%x",
ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
return (buf);
}
void
logmsg(int pri, const char *fmt, ...)
{
va_list v;
FILE *fp;
char *newfmt;
va_start(v, fmt);
if (dflag) {
if (pri == LOG_ERR)
fp = stderr;
else
fp = stdout;
if (expand_syslog_m(fmt, &newfmt) == -1) {
vfprintf(fp, fmt, v);
} else {
vfprintf(fp, newfmt, v);
free(newfmt);
}
fputs("\n", fp);
fflush(fp);
} else {
vsyslog(pri, fmt, v);
}
va_end(v);
}
int
expand_syslog_m(const char *fmt, char **newfmt) {
const char *str, *m;
char *p, *np;
p = strdup("");
str = fmt;
while ((m = strstr(str, "%m")) != NULL) {
asprintf(&np, "%s%.*s%s", p, (int)(m - str),
str, strerror(errno));
free(p);
if (np == NULL) {
errno = ENOMEM;
return (-1);
}
p = np;
str = m + 2;
}
if (*str != '\0') {
asprintf(&np, "%s%s", p, str);
free(p);
if (np == NULL) {
errno = ENOMEM;
return (-1);
}
p = np;
}
*newfmt = p;
return (0);
}
|