Repository navigation
Expand file tree
/
Copy pathdhcpd.c
More file actions
1421 lines (1247 loc) · 36.6 KB
/
Copy pathdhcpd.c
File metadata and controls
1421 lines (1247 loc) · 36.6 KB
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
/* dhcpd 1.15 - Dynamic Host Configuration Protocol daemon.
* Author: Kees J. Bot
* 11 Jun 1999
*/
#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <configfile.h>
#include <sys/ioctl.h>
#include <sys/asynchio.h>
#include <net/hton.h>
#include <net/gen/socket.h>
#include <netdb.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <net/gen/ether.h>
#include <net/gen/if_ether.h>
#include <net/gen/eth_hdr.h>
#include <net/gen/ip_hdr.h>
#include <net/gen/udp.h>
#include <net/gen/udp_hdr.h>
#include <net/gen/udp_io.h>
#include <net/gen/dhcp.h>
#include "arp.h"
#define EXTERN
#include "dhcpd.h"
char *configfile= PATH_DHCPCONF;
char *poolfile= PATH_DHCPPOOL;
static char *cachefile= PATH_DHCPCACHE;
static int qflag; /* True if printing cached DHCP data. */
static int aflag, rflag; /* True if adding or deleting pool addresses. */
#define BCAST_IP htonl(0xFFFFFFFFUL)
/* We try to play with up to this many networks. */
#define N_NETS 32
static unsigned n_nets; /* Actual number of networks. */
int lwip;
void report(const char *label)
{
static FILE *logfp;
if(!logfp)
logfp = fopen("/usr/log/dhcp.log", "w");
if(logfp)
fprintf(logfp, "%s: %s: %s\n", program, label, strerror(errno));
}
void fatal(const char *label)
{
report(label);
exit(1);
}
void *allocate(size_t size)
{
void *mem;
if ((mem= malloc(size)) == nil) fatal("Can't allocate memory");
return mem;
}
/* Choose a DHCP xid based on the start time and network number. Not really
* random, but we don't have anything more random than the clock anyway.
*/
#define XID(np) htonl(((u32_t) (np)->start << 8) | (np)->n)
static network_t *network[N_NETS];
int ifname2if(const char *name)
{
/* Translate an interface name to a number, -1 if bad. */
char *end;
unsigned long n;
if (*name++ != 'i' || *name++ != 'p') return -1;
n= strtoul(name, &end, 10);
if (end == name || *end != 0) return -1;
if (n >= N_NETS) return -1;
return n;
}
network_t *if2net(int n)
{
/* Translate an interface number to a network struct. */
int i;
for (i= 0; i < n_nets; i++) {
if (network[i]->n == n) return network[i];
}
return nil;
}
static ipaddr_t defaultmask(ipaddr_t ip)
{
/* Compute netmask by the oldfashioned Class rules. */
if (B(&ip)[0] < 0x80) return htonl(0xFF000000UL); /* Class A. */
if (B(&ip)[0] < 0xC0) return htonl(0xFFFF0000UL); /* Class B. */
if (B(&ip)[0] < 0xE0) return htonl(0xFFFFFF00UL); /* Class C. */
return htonl(0xFFFFFFFFUL); /* Multicast? Shouldn't happen... */
}
#define POOL_MAGIC htonl(0x81F85D00UL)
typedef struct pool { /* Dynamic pool entry. */
u32_t magic; /* Pool file magic number. */
ipaddr_t ip; /* IP address. */
u32_t expire; /* When does/did the lease expire? */
u8_t len; /* Client ID length. */
u8_t unused[19]; /* Space for extensions. */
u8_t clid[CLID_MAX]; /* Client ID of current/last user. */
} pool_t;
static int openpool(int mode)
{
/* Open the dynamic pool and lock it, return fd on success or -1. */
int fd;
struct flock lck;
if ((fd= open(poolfile, mode, 0644)) < 0) {
if (errno != ENOENT) fatal(poolfile);
return -1;
}
if (mode != O_RDONLY) {
lck.l_type= F_WRLCK;
lck.l_whence= SEEK_SET;
lck.l_start= 0;
lck.l_len= 0;
if (fcntl(fd, F_SETLKW, &lck) < 0) fatal(poolfile);
}
return fd;
}
static int readpool(int fd, pool_t *entry)
{
/* Read one pool table entry, return true unless EOF. */
ssize_t r;
if ((r= read(fd, entry, sizeof(*entry))) < 0) fatal(poolfile);
if (r == 0) return 0;
if (r != sizeof(*entry) || entry->magic != POOL_MAGIC) {
fprintf(stderr, "%s: %s: Pool table is corrupt\n",
program, poolfile);
close(fd);
return 0;
}
return 1;
}
static void writepool(int fd, pool_t *entry)
{
/* (Over)write a pool table entry. */
if (write(fd, entry, sizeof(*entry)) < 0
|| (entry->expire > now && fsync(fd) < 0)
) {
fatal(poolfile);
}
}
static ipaddr_t findpool(u8_t *client, size_t len, ipaddr_t ifip)
{
/* Look for a client ID in the dynamic address pool within the same network
* as 'ifip'. Select an unused one for a new client if necessary. Return
* 0 if nothing is available, otherwise the IP address we can offer.
*/
int fd, found;
pool_t entry, oldest;
dhcp_t dhcp;
u8_t *pmask;
ipaddr_t mask;
/* Any information available on the network the client is at? */
if (!makedhcp(&dhcp, nil, 0, nil, 0, ifip, ifip, nil)) return 0;
if ((fd= openpool(O_RDWR)) < 0) return 0;
(void) gettag(&dhcp, DHCP_TAG_NETMASK, &pmask, nil);
memcpy(&mask, pmask, sizeof(mask));
oldest.expire= NEVER;
while ((found= readpool(fd, &entry))) {
/* Deleted entry? */
if (entry.ip == 0) continue;
/* Correct network? */
if (((entry.ip ^ ifip) & mask) != 0) continue;
/* Client present? */
if (entry.len == len && memcmp(entry.clid, client, len) == 0) break;
/* Oldest candidate for a new lease? */
entry.expire= ntohl(entry.expire);
if (entry.expire < oldest.expire) oldest= entry;
}
close(fd);
if (found) return entry.ip;
if (oldest.expire <= now) return oldest.ip;
return 0;
}
static int commitpool(ipaddr_t ip, u8_t *client, size_t len, time_t expire)
{
/* Commit a new binding to stable storage, return true on success. */
int fd;
pool_t entry;
if ((fd= openpool(O_RDWR)) < 0) return 0;
do {
if (!readpool(fd, &entry)) {
close(fd);
return 0;
}
} while (entry.ip != ip);
entry.expire= htonl(expire);
entry.len= len;
memcpy(entry.clid, client, len);
if (lseek(fd, -(off_t)sizeof(entry), SEEK_CUR) == -1) fatal(poolfile);
writepool(fd, &entry);
close(fd);
return 1;
}
static void updatepool(int add, const char *name)
{
/* Add a new IP address to the dynamic pool. */
ipaddr_t ip;
int fd, i;
pool_t entry;
struct hostent *he;
off_t off, off0;
if ((he= gethostbyname(name)) == nil || he->h_addrtype != AF_INET) {
fprintf(stderr, "%s: %s: Unknown host\n", program, name);
exit(1);
}
for (i= 0; he->h_addr_list[i] != nil; i++) {}
if (i != 1) {
fprintf(stderr, "%s: %s has %d addresses\n", program, name, i);
exit(1);
}
memcpy(&ip, he->h_addr_list[0], sizeof(ip));
if ((fd= openpool(O_RDWR|O_CREAT)) < 0) fatal(poolfile);
off= 0;
off0= -1;
while (readpool(fd, &entry)) {
if (add) {
if (entry.ip == ip) {
fprintf(stderr, "%s: %s: %s is already present\n",
program, poolfile, name);
exit(1);
}
if (entry.ip == 0 && off0 == -1) off0= off;
} else {
if (entry.ip == ip) {
memset(&entry, 0, sizeof(entry));
entry.magic= POOL_MAGIC;
entry.ip= 0;
if (lseek(fd, off, SEEK_SET) == -1) fatal(poolfile);
writepool(fd, &entry);
}
}
off+= sizeof(entry);
}
if (add) {
if (off0 != -1 && lseek(fd, off0, SEEK_SET) == -1) fatal(poolfile);
memset(&entry, 0, sizeof(entry));
entry.magic= POOL_MAGIC;
entry.ip= ip;
writepool(fd, &entry);
}
close(fd);
}
static void cachedhcp(int n, dhcp_t *dp)
{
/* Store a DHCP packet in a cache where those who care can find it. */
static int inited;
FILE *fp;
int fd;
int mode;
if (test > 0) return;
if (!inited) {
/* First time, clear store and also save my pid. */
if ((fp= fopen(PATH_DHCPPID, "w")) != nil) {
if (fprintf(fp, "%d\n", getpid()) == EOF || fclose(fp) == EOF) {
fatal(PATH_DHCPPID);
}
}
inited= 1;
mode= O_WRONLY | O_CREAT | O_TRUNC;
} else {
mode= O_WRONLY;
}
dp->xid= htonl(now); /* To tell how old this data is. */
if ((fd= open(cachefile, mode, 0666)) < 0
|| lseek(fd, (off_t) n * sizeof(*dp), SEEK_SET) == -1
|| write(fd, dp, sizeof(*dp)) < 0
|| close(fd) < 0
) {
if (errno != ENOENT) fatal(cachefile);
}
}
static void printdata(void)
{
/* Show the contents of the cache and the dynamic pool. */
int fd;
dhcp_t d;
ssize_t r;
int i;
pool_t entry;
unsigned long expire;
char delta[3*sizeof(u32_t)];
initdhcpconf();
if ((fd= open(cachefile, O_RDONLY)) < 0) fatal(cachefile);
i= 0;
while ((r= read(fd, &d, sizeof(d))) == sizeof(d)) {
if (d.yiaddr != 0) {
printf("DHCP data for network %d:\n", i);
printdhcp(&d);
}
i++;
}
if (r < 0) fatal(cachefile);
close(fd);
if ((fd= openpool(O_RDONLY)) >= 0) {
printf("Dynamic address pool since %ld:\n", (long) now);
while (readpool(fd, &entry)) {
if (entry.ip == 0) continue;
expire= ntohl(entry.expire);
if (expire == 0) {
strcpy(delta, "unused");
} else
if (expire == 0xFFFFFFFFUL) {
strcpy(delta, "infinite");
} else
if (expire < now) {
sprintf(delta, "-%lu", now - expire);
} else {
sprintf(delta, "+%lu", expire - now);
}
printf("\t%-15s %8s ", inet_ntoa(entry.ip), delta);
for (i= 0; i < entry.len; i++) {
printf("%02X", entry.clid[i]);
}
fputc('\n', stdout);
}
close(fd);
}
}
static udpport_t portbyname(const char *name)
{
struct servent *se;
if ((se= getservbyname(name, "udp")) == nil) {
fprintf(stderr, "%s: Unknown port \"%s\"\n", program, name);
exit(1);
}
return se->s_port;
}
static int sendpacket(network_t *np, void *data, size_t len)
{
/* Send out a packet using a filedescriptor that is probably in async mode,
* so first dup() a sync version, then write. Return true on success.
*/
int fd;
ssize_t r;
if ((fd= dup(np->fdp->fd)) < 0) fatal("Can't dup()");
if ((r= write(fd, data, len)) < 0) {
report(np->fdp->device);
sleep(10);
}
close(fd);
return r >= 0;
}
static size_t servdhcp(network_t *np, buf_t *bp, size_t dlen)
{
buf_t *abp= nil;
ipaddr_t cip, ifip;
u8_t defclid[1+sizeof(bp->dhcp->chaddr)];
u8_t *pdata, *client, *class, *server, *reqip, *lease;
u32_t expire;
size_t len, cilen, calen;
int type, dyn;
u8_t atype;
static char NAKMESS[] = "IP address requested isn't yours";
if (test > 0) return 0;
/* The IP address of the interface close to the client. */
ifip= bp->dhcp->giaddr != 0 ? bp->dhcp->giaddr : np->ip;
/* All kinds of DHCP tags. */
if (gettag(bp->dhcp, DHCP_TAG_TYPE, &pdata, nil)) {
type= *pdata;
} else {
type= -1; /* BOOTP? */
}
if (!gettag(bp->dhcp, DHCP_TAG_CLIENTID, &client, &cilen)) {
defclid[0]= bp->dhcp->htype;
memcpy(defclid+1, bp->dhcp->chaddr, bp->dhcp->hlen);
client= defclid;
cilen= 1+bp->dhcp->hlen;
}
if (!gettag(bp->dhcp, DHCP_TAG_CLASSID, &class, &calen)) {
calen= 0;
}
if (!gettag(bp->dhcp, DHCP_TAG_SERVERID, &server, nil)) {
server= B(&np->ip);
}
if (!gettag(bp->dhcp, DHCP_TAG_REQIP, &reqip, nil)) {
reqip= nil;
}
/* I'm a server? Then see if I know this client. */
if ((np->flags & NF_SERVING)
&& bp->dhcp->op == DHCP_BOOTREQUEST
&& between(1, bp->dhcp->hlen, sizeof(bp->dhcp->chaddr))
&& (server == nil || memcmp(server, &np->ip, sizeof(np->ip)) == 0)
) {
get_buf(&abp);
/* Is the client in my tables? */
(void) makedhcp(abp->dhcp, class, calen, client, cilen, 0, ifip, nil);
cip= abp->dhcp->yiaddr;
dyn= 0;
/* If not, do we have a dynamic address? */
if (cip == 0 && (cip= findpool(client, cilen, ifip)) != 0) dyn= 1;
if (type == DHCP_INFORM) {
/* The client already has an address, it just wants information.
* We only answer if we could answer a normal request (cip != 0),
* unless configured to answer anyone.
*/
if (cip != 0 || (np->flags & NF_INFORM)) cip= bp->dhcp->ciaddr;
}
if (cip == 0 || !makedhcp(abp->dhcp, class, calen,
client, cilen, cip, ifip, nil)) {
put_buf(&abp);
}
if (abp != nil) {
if (gettag(abp->dhcp, DHCP_TAG_LEASE, &lease, nil)) {
memcpy(&expire, lease, sizeof(expire));
expire= now + ntohl(expire);
if (expire < now) expire= 0xFFFFFFFFUL;
} else {
if (dyn) {
/* A dynamic address must have a lease. */
fprintf(stderr, "%s: No lease set for address %s\n",
program, inet_ntoa(cip));
exit(1);
}
lease= nil;
expire= 0xFFFFFFFFUL;
}
/* What does our client want, and what do we say? */
switch (type) {
case DHCP_DISCOVER:
atype= DHCP_OFFER;
/* Assign this address for a short moment. */
if (dyn && !commitpool(cip, client, cilen, now + DELTA_FAST)) {
put_buf(&abp);
}
break;
case -1:/* BOOTP */
case DHCP_REQUEST:
case DHCP_INFORM:
atype= DHCP_ACK;
/* The address wanted must be the address we offer. */
if ((reqip != nil && memcmp(reqip, &cip, sizeof(cip)) != 0)
|| (bp->dhcp->ciaddr != 0 && bp->dhcp->ciaddr != cip)
) {
atype= DHCP_NAK;
} else
if (dyn && type == DHCP_REQUEST) {
/* Assign this address for the duration of the lease. */
if (!commitpool(cip, client, cilen, expire)) put_buf(&abp);
}
break;
case DHCP_DECLINE:
/* Our client doesn't want the offered address! */
if (dyn
&& reqip != nil
&& memcmp(reqip, &cip, sizeof(cip)) == 0
) {
int i;
fprintf(stderr, "%s: ", program);
for (i= 0; i < cilen; i++) {
fprintf(stderr, "%02X", client[i]);
}
fprintf(stderr, " declines %s", inet_ntoa(cip));
if (gettag(bp->dhcp, DHCP_TAG_MESSAGE, &pdata, &len)) {
fprintf(stderr, " saying: \"%.*s\"", (int)len, pdata);
}
fputc('\n', stderr);
/* Disable address for the duration of the lease. */
(void) commitpool(cip, nil, 0, expire);
}
put_buf(&abp);
break;
case DHCP_RELEASE:
/* Our client is nice enough to return its address. */
if (dyn) (void) commitpool(cip, client, cilen, now);
put_buf(&abp);
break;
default: /* Anything else is ignored. */
put_buf(&abp);
}
}
if (abp != nil) {
/* Finish the return packet. */
abp->dhcp->htype= bp->dhcp->htype;
abp->dhcp->hlen= bp->dhcp->hlen;
abp->dhcp->hops= 0;
abp->dhcp->xid= bp->dhcp->xid;
abp->dhcp->secs= 0;
abp->dhcp->flags= bp->dhcp->flags;
abp->dhcp->ciaddr= 0;
abp->dhcp->yiaddr= atype == DHCP_NAK ? 0 : cip;
if (atype == DHCP_NAK) abp->dhcp->siaddr= 0;
abp->dhcp->giaddr= bp->dhcp->giaddr;
memcpy(abp->dhcp->chaddr,bp->dhcp->chaddr,sizeof(bp->dhcp->chaddr));
settag(abp->dhcp, DHCP_TAG_SERVERID, &np->ip, sizeof(np->ip));
if (lease == nil) {
/* No lease specified? Then give an infinite lease. */
settag(abp->dhcp, DHCP_TAG_LEASE, &expire, sizeof(expire));
}
if (type == DHCP_INFORM) {
/* Oops, this one has a fixed address, so no lease business. */
abp->dhcp->yiaddr= 0;
settag(abp->dhcp, DHCP_TAG_LEASE, nil, 0);
settag(abp->dhcp, DHCP_TAG_RENEWAL, nil, 0);
settag(abp->dhcp, DHCP_TAG_REBINDING, nil, 0);
}
if (atype == DHCP_NAK) {
/* A NAK doesn't need much. */
memset(abp->dhcp->sname, 0, sizeof(abp->dhcp->sname));
memset(abp->dhcp->file, 0, sizeof(abp->dhcp->file));
memset(abp->dhcp->options, 255, sizeof(abp->dhcp->options));
settag(abp->dhcp, DHCP_TAG_MESSAGE, NAKMESS, sizeof(NAKMESS));
}
settag(abp->dhcp, DHCP_TAG_TYPE, &atype, sizeof(atype));
/* Figure out where to send this to. */
abp->udpio->uih_src_addr= np->ip;
abp->udpio->uih_src_port= port_server;
if (bp->dhcp->giaddr != 0) {
abp->udpio->uih_dst_addr= bp->dhcp->giaddr;
abp->udpio->uih_dst_port= port_server;
} else
if (bp->dhcp->flags & DHCP_FLAGS_BCAST) {
abp->udpio->uih_dst_addr= BCAST_IP;
abp->udpio->uih_dst_port= port_client;
} else
if (bp->udpio->uih_src_addr != 0
&& bp->udpio->uih_dst_addr == np->ip
) {
abp->udpio->uih_dst_addr= bp->udpio->uih_src_addr;
abp->udpio->uih_dst_port= port_client;
} else {
abp->udpio->uih_dst_addr= BCAST_IP;
abp->udpio->uih_dst_port= port_client;
}
abp->udpio->uih_ip_opt_len= 0;
abp->udpio->uih_data_len= sizeof(dhcp_t);
/* Copy the packet to the input buffer, and return the new size. */
memcpy(bp->buf, abp->buf, sizeof(bp->buf));
put_buf(&abp);
return sizeof(udp_io_hdr_t) + sizeof(dhcp_t);
}
}
/* I'm a relay? If it is a not already a relayed request then relay. */
if ((np->flags & NF_RELAYING)
&& bp->dhcp->op == DHCP_BOOTREQUEST
&& bp->dhcp->giaddr == 0
) {
bp->dhcp->giaddr= np->ip;
bp->udpio->uih_src_addr= np->ip;
bp->udpio->uih_src_port= port_server;
bp->udpio->uih_dst_addr= np->server;
bp->udpio->uih_dst_port= port_server;
return dlen;
}
/* I'm a relay? If the server sends a reply to me then relay back. */
if ((np->flags & NF_RELAYING)
&& bp->dhcp->op == DHCP_BOOTREPLY
&& bp->dhcp->giaddr == np->ip
) {
bp->dhcp->giaddr= 0;
bp->udpio->uih_src_addr= np->ip;
bp->udpio->uih_src_port= port_server;
bp->udpio->uih_dst_addr= BCAST_IP;
bp->udpio->uih_dst_port= port_client;
return dlen;
}
/* Don't know what to do otherwise, so doing nothing seems wise. */
return 0;
}
static void onsig(int sig)
{
switch (sig) {
case SIGUSR1: debug++; break;
case SIGUSR2: debug= 0; break;
}
}
static void usage(void)
{
fprintf(stderr,
"Usage: %s [-qar] [-t[L]] [-d[L]] [-f config] [-c cache] [-p pool] [host ...]\n",
program);
exit(1);
}
int main(int argc, char **argv)
{
int i;
network_t *np;
struct sigaction sa;
ssize_t r;
buf_t *bp;
static struct timeval eventtv;
main:
n_nets = 0;
r = -1;
bp = nil;
program= argv[0];
start= now= time(nil);
debug= 0;
i= 1;
while (i < argc && argv[i][0] == '-') {
char *opt= argv[i++]+1;
if (opt[0] == '-' && opt[1] == 0) break; /* -- */
if (strcmp(opt, "-lwip") == 0) {
lwip = 1;
continue;
}
while (*opt != 0) switch (*opt++) {
case 'f':
if (*opt == 0) {
if (i == argc) usage();
opt= argv[i++];
}
configfile= opt;
opt= "";
break;
case 'c':
if (*opt == 0) {
if (i == argc) usage();
opt= argv[i++];
}
cachefile= opt;
opt= "";
break;
case 'p':
if (*opt == 0) {
if (i == argc) usage();
opt= argv[i++];
}
poolfile= opt;
opt= "";
break;
case 't':
test= 1;
if (between('0', *opt, '9')) test= strtoul(opt, &opt, 10);
break;
case 'd':
debug= 1;
if (between('0', *opt, '9')) debug= strtoul(opt, &opt, 10);
break;
case 'q':
qflag= 1;
break;
case 'a':
aflag= 1;
break;
case 'r':
rflag= 1;
break;
default:
usage();
}
}
if (aflag + rflag + qflag > 1) usage();
if (aflag || rflag) {
/* Add or remove addresses from the dynamic pool. */
while (i < argc) updatepool(aflag, argv[i++]);
exit(0);
}
if (i != argc) usage();
if (qflag) {
/* Only show the contents of the cache and dynamic pool to the user. */
printdata();
exit(0);
}
/* BOOTP ports. */
port_server= portbyname("bootps");
port_client= portbyname("bootpc");
sa.sa_handler= onsig;
sigemptyset(&sa.sa_mask);
sa.sa_flags= 0;
sigaction(SIGUSR1, &sa, nil);
sigaction(SIGUSR2, &sa, nil);
/* Initial configuration. */
for (i= 0; i < N_NETS; i++) {
int fd;
ipaddr_t ip, mask;
/* Is there something there? */
if ((fd= open(ipdev(i), O_RDWR|O_NONBLOCK)) < 0) {
if (errno != ENOENT && errno != ENODEV && errno != ENXIO) {
fatal(ipdev(i));
}
continue;
}
close(fd);
network[n_nets++]= np= newnetwork();
np->n= i;
/* Ethernet? */
if (lwip) {
np->type = NT_ETHERNET;
} else if (opendev(np, FT_ETHERNET, 1)) {
np->type= B(&np->eth)[0] != 'Z' ? NT_ETHERNET : NT_SINK;
if (debug >= 1) {
printf("%s: Ethernet address is %s%s\n",
np->fdp->device, ether_ntoa(&np->eth),
np->type == NT_SINK ? " (sink)" : "");
}
closedev(np, FT_ETHERNET);
}
/* Only true Ethernets worry about DHCP. */
if (np->type != NT_ETHERNET) np->renew= np->rebind= np->lease= NEVER;
}
/* Try to find my interfaces in the DHCP table. */
for (i= 0; i < n_nets; i++) {
ipaddr_t cip;
u8_t clid[1+DHCP_HLEN_ETH];
size_t cilen;
np= network[i];
if (np->flags & NF_BOUND) continue;
if (np->type == NT_IP) {
cilen= 0;
} else {
ether2clid(clid, &np->eth);
cilen= 1+DHCP_HLEN_ETH;
}
/* Try to find an Ethernet address, or the IP address of an already
* configured network. If we have data we get an IP address back.
*/
get_buf(&bp);
(void) makedhcp(bp->dhcp, (u8_t *) "Minix", 5,
clid, cilen, np->ip, 0, np);
cip= bp->dhcp->yiaddr;
/* Gather information on the interface. */
if (cip != 0
&& makedhcp(bp->dhcp, (u8_t *) "Minix", 5,
clid, cilen, cip, cip, np)
&& test < 2
) {
u8_t *pdata;
u16_t mtu;
cachedhcp(np->n, bp->dhcp);
np->ip= cip;
(void) gettag(bp->dhcp, DHCP_TAG_NETMASK, &pdata, nil);
memcpy(&np->mask, pdata, sizeof(np->mask));
if (gettag(bp->dhcp, DHCP_TAG_GATEWAY, &pdata, nil)) {
memcpy(&np->gateway, pdata, sizeof(np->gateway));
} else {
np->gateway= 0;
}
if (gettag(bp->dhcp, DHCP_TAG_IPMTU, &pdata, nil)) {
memcpy(&mtu, pdata, sizeof(mtu));
mtu= ntohs(mtu);
} else {
mtu= 0;
}
set_ipconf(ipdev(np->n), np->ip, np->mask, mtu);
if (debug >= 1) {
printf("%s: IP address is %s\n",
ipdev(np->n), cidr_ntoa(np->ip, np->mask));
}
np->flags |= NF_BOUND;
np->renew= np->rebind= np->lease= NEVER;
np->sol_ct= N_SOLICITS;
np->solicit= 0;
/* Other (previous) interfaces may have been defined. */
i= 0;
}
put_buf(&bp);
}
for (;;) {
now= time(nil);
event= NEVER;
/* Is it time to request/renew a lease? */
for (i= 0; i < n_nets; i++) {
np= network[i];
if (np->renew <= now) {
u8_t type;
static u8_t taglist[] = {
DHCP_TAG_NETMASK, DHCP_TAG_GATEWAY, DHCP_TAG_DNS,
DHCP_TAG_HOSTNAME
};
u8_t ethclid[1+DHCP_HLEN_ETH];
/* We may have lost our binding or even our lease. */
if (np->rebind <= now) np->server= BCAST_IP;
if (np->lease <= now) {
if (np->flags & NF_BOUND) closedev(np, FT_ALL);
if ((np->flags & (NF_BOUND | NF_POSSESSIVE)) == NF_BOUND) {
set_ipconf(ipdev(np->n), np->ip= 0, np->mask= 0, 0);
if (debug >= 1) {
printf("%s: Interface disabled (lease expired)\n",
ipdev(np->n));
}
}
np->flags &= ~NF_BOUND;
}
/* See if we can open the network we need to send on. */
if (!(np->flags & NF_BOUND)) {
if (!opendev(np, FT_ETHERNET, 1)) continue;
} else {
if (!opendev(np, FT_BOOTPC, 1)) continue;
}
if (!(np->flags & NF_NEGOTIATING)) {
/* We need to start querying a DHCP server. */
np->start= now;
np->delta= DELTA_FIRST;
np->flags |= NF_NEGOTIATING;
}
/* Fill in a DHCP query packet. */
get_buf(&bp);
dhcp_init(bp->dhcp);
bp->dhcp->op= DHCP_BOOTREQUEST;
bp->dhcp->htype= DHCP_HTYPE_ETH;
bp->dhcp->hlen= DHCP_HLEN_ETH;
bp->dhcp->xid= XID(np);
bp->dhcp->secs= htons(now - np->start > 0xFFFF
? 0xFFFF : now - np->start);
memcpy(bp->dhcp->chaddr, &np->eth, sizeof(np->eth));
if (np->lease <= now) {
/* First time, or my old server is unresponsive. */
type= DHCP_DISCOVER;
} else {
/* Request an offered address or renew an address. */
type= DHCP_REQUEST;
if (np->flags & NF_BOUND) {
/* A renewal, I claim my current address. */
bp->dhcp->ciaddr= np->ip;
} else {
/* Nicely ask for the address just offered. */
settag(bp->dhcp, DHCP_TAG_REQIP, &np->ip,
sizeof(np->ip));
settag(bp->dhcp, DHCP_TAG_SERVERID, &np->server,
sizeof(np->server));
}
}
settag(bp->dhcp, DHCP_TAG_TYPE, &type, 1);
/* My client ID. Simply use the default. */
ether2clid(ethclid, &np->eth);
settag(bp->dhcp, DHCP_TAG_CLIENTID, ethclid, sizeof(ethclid));
/* The Class ID may serve to recognize Minix hosts. */
settag(bp->dhcp, DHCP_TAG_CLASSID, "Minix", 5);
/* The few tags that Minix can make good use of. */
settag(bp->dhcp, DHCP_TAG_REQPAR, taglist, sizeof(taglist));
/* Some weird sites use a hostname, not a client ID. */
if (np->hostname != nil) {
settag(bp->dhcp, DHCP_TAG_HOSTNAME,
np->hostname, strlen(np->hostname));
}
bp->udpio->uih_src_addr= np->ip;
bp->udpio->uih_dst_addr= np->server;
bp->udpio->uih_src_port= port_client;
bp->udpio->uih_dst_port= port_server;
bp->udpio->uih_ip_opt_len= 0;
bp->udpio->uih_data_len= sizeof(dhcp_t);
if (!(np->flags & NF_BOUND)) {
/* Rebind over Ethernet. */
udp2ether(bp, np);
if (sendpacket(np, (lwip ? bp->ip : (void *) bp->eth),
(lwip ? 0 : sizeof(eth_hdr_t)) + sizeof(ip_hdr_t)
+ sizeof(udp_hdr_t) + sizeof(dhcp_t))) {
if (debug >= 1) {
printf("%s: Broadcast DHCP %s\n",
np->fdp->device, dhcptypename(type));
if (debug >= 2) printdhcp(bp->dhcp);
}
}
} else {
/* Renew over UDP. */
if (sendpacket(np, bp->udpio, sizeof(udp_io_hdr_t)
+ sizeof(dhcp_t))) {
if (debug >= 1) {
printf("%s: Sent DHCP %s to %s\n",
np->fdp->device,
dhcptypename(type),
inet_ntoa(np->server));
if (debug >= 2) printdhcp(bp->dhcp);
}
}
}
put_buf(&bp);
/* When to continue querying a DHCP server? */
if (np->flags & NF_BOUND) {
/* Still bound, keep halving time till next event. */
time_t e, d;
e= now < np->rebind ? np->rebind : np->lease;
d= (e - now) / 2;
if (d < DELTA_SLOW) d= DELTA_SLOW;