-
Notifications
You must be signed in to change notification settings - Fork 1
/
dhclient.c
2949 lines (2531 loc) · 74.2 KB
/
dhclient.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
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
/* $OpenBSD: dhclient.c,v 1.405 2017/03/08 20:54:30 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <[email protected]>
* Copyright (c) 1995, 1996, 1997, 1998, 1999
* The Internet Software Consortium. 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.
* 3. Neither the name of The Internet Software Consortium 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 BY THE INTERNET SOFTWARE CONSORTIUM AND
* CONTRIBUTORS ``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 INTERNET SOFTWARE CONSORTIUM OR
* CONTRIBUTORS 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.
*
* This software has been written for the Internet Software Consortium
* by Ted Lemon <[email protected]> in cooperation with Vixie
* Enterprises. To learn more about the Internet Software Consortium,
* see ``http://www.vix.com/isc''. To learn more about Vixie
* Enterprises, see ``http://www.vix.com''.
*
* This client was substantially modified and enhanced by Elliot Poger
* for use on Linux while he was working on the MosquitoNet project at
* Stanford.
*
* The current version owes much to Elliot's Linux enhancements, but
* was substantially reorganized and partially rewritten by Ted Lemon
* so as to use the same networking framework that the Internet Software
* Consortium DHCP server uses. Much system-specific configuration code
* was moved into a shell script so that as support for more operating
* systems is added, it will not be necessary to port and maintain
* system-specific configuration code to these operating systems - instead,
* the shell script can invoke the native tools to accomplish the same
* purpose.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <sys/queue.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <imsg.h>
#include <limits.h>
#include <paths.h>
#include <poll.h>
#include <pwd.h>
#include <resolv.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "dhcp.h"
#include "dhcpd.h"
#include "log.h"
#include "privsep.h"
char *path_dhclient_conf = _PATH_DHCLIENT_CONF;
char *path_dhclient_db = NULL;
char path_option_db[PATH_MAX];
int log_perror = 1;
int nullfd = -1;
int daemonize = 1;
int unknown_ok = 1;
int routefd = -1;
volatile sig_atomic_t quit;
struct in_addr deleting;
struct in_addr adding;
const struct in_addr inaddr_any = { INADDR_ANY };
const struct in_addr inaddr_broadcast = { INADDR_BROADCAST };
struct client_config *config;
struct imsgbuf *unpriv_ibuf;
void sighdlr(int);
int findproto(char *, int);
struct sockaddr *get_ifa(char *, int);
void usage(void);
int res_hnok(const char *dn);
int res_hnok_list(const char *dn);
int addressinuse(struct interface_info *, struct in_addr, char *);
void fork_privchld(struct interface_info *, int, int);
void get_ifname(struct interface_info *, char *);
char *resolv_conf_contents(struct interface_info *ifi,
struct option_data *, struct option_data *,
struct option_data *);
void write_resolv_conf(u_int8_t *, size_t);
void write_option_db(u_int8_t *, size_t);
struct client_lease *apply_defaults(struct client_lease *);
struct client_lease *clone_lease(struct client_lease *);
void apply_ignore_list(char *);
void add_direct_route(struct in_addr, struct in_addr, struct in_addr);
void add_default_route(struct in_addr, struct in_addr);
void add_static_routes(struct option_data *, struct in_addr);
void add_classless_static_routes(struct option_data *, struct in_addr);
int compare_lease(struct client_lease *, struct client_lease *);
void set_lease_times(struct client_lease *);
void state_preboot(void *);
void state_reboot(void *);
void state_init(void *);
void state_selecting(void *);
void state_bound(void *);
void state_panic(void *);
void send_discover(void *);
void send_request(void *);
void send_decline(void *);
void bind_lease(struct interface_info *);
void make_discover(struct interface_info *, struct client_lease *);
void make_request(struct interface_info *, struct client_lease *);
void make_decline(struct interface_info *, struct client_lease *);
void rewrite_client_leases(struct interface_info *);
void rewrite_option_db(struct interface_info *, struct client_lease *,
struct client_lease *);
char *lease_as_string(struct interface_info *, char *, struct client_lease *);
struct client_lease *packet_to_lease(struct interface_info *, struct in_addr,
struct option_data *);
void go_daemon(void);
int rdaemon(int);
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
static FILE *leaseFile;
void
sighdlr(int sig)
{
quit = sig;
}
int
findproto(char *cp, int n)
{
struct sockaddr *sa;
unsigned int i;
if (n == 0)
return -1;
for (i = 1; i; i <<= 1) {
if (i & n) {
sa = (struct sockaddr *)cp;
switch (i) {
case RTA_IFA:
case RTA_DST:
case RTA_GATEWAY:
case RTA_NETMASK:
if (sa->sa_family == AF_INET)
return AF_INET;
if (sa->sa_family == AF_INET6)
return AF_INET6;
break;
case RTA_IFP:
break;
}
ADVANCE(cp, sa);
}
}
return (-1);
}
struct sockaddr *
get_ifa(char *cp, int n)
{
struct sockaddr *sa;
unsigned int i;
if (n == 0)
return (NULL);
for (i = 1; i; i <<= 1)
if (i & n) {
sa = (struct sockaddr *)cp;
if (i == RTA_IFA)
return (sa);
ADVANCE(cp, sa);
}
return (NULL);
}
void
routehandler(struct interface_info *ifi)
{
char ntoabuf[INET_ADDRSTRLEN];
struct in_addr a, b;
struct sockaddr *sa;
struct ifa_msghdr *ifam;
struct client_state *client = ifi->client;
struct ether_addr hw;
struct rt_msghdr *rtm;
struct if_msghdr *ifm;
struct if_announcemsghdr *ifan;
char *errmsg, *rtmmsg;
ssize_t n;
int linkstat, rslt;
rtmmsg = calloc(1, 2048);
if (rtmmsg == NULL)
fatalx("No memory for rtmmsg");
do {
n = read(routefd, rtmmsg, 2048);
} while (n == -1 && errno == EINTR);
rtm = (struct rt_msghdr *)rtmmsg;
if (n < sizeof(rtm->rtm_msglen) || n < rtm->rtm_msglen ||
rtm->rtm_version != RTM_VERSION)
goto done;
switch (rtm->rtm_type) {
case RTM_NEWADDR:
ifam = (struct ifa_msghdr *)rtm;
if (ifam->ifam_index != ifi->index)
break;
if (findproto((char *)ifam + ifam->ifam_hdrlen,
ifam->ifam_addrs) != AF_INET)
break;
sa = get_ifa((char *)ifam + ifam->ifam_hdrlen,
ifam->ifam_addrs);
if (sa == NULL)
goto done;
memcpy(&a, &((struct sockaddr_in *)sa)->sin_addr, sizeof(a));
if (a.s_addr == INADDR_ANY)
break;
/*
* If we are in the process of adding a new address, ignore
* messages generated by that process.
*/
if (a.s_addr == adding.s_addr) {
adding.s_addr = INADDR_ANY;
log_info("bound to %s -- renewal in %lld seconds.",
inet_ntoa(client->active->address),
(long long)(client->active->renewal -
time(NULL)));
client->flags |= IS_RESPONSIBLE;
go_daemon();
break;
}
if ((client->flags & IS_RESPONSIBLE) == 0)
/* We're not responsible yet! */
break;
if (adding.s_addr != INADDR_ANY) {
strlcpy(ntoabuf, inet_ntoa(a), sizeof(ntoabuf));
rslt = asprintf(&errmsg, "%s, not %s, added to %s",
ntoabuf, inet_ntoa(adding), ifi->name);
} else
rslt = asprintf(&errmsg, "%s added to %s",
inet_ntoa(a), ifi->name);
goto die;
case RTM_DELADDR:
ifam = (struct ifa_msghdr *)rtm;
if (ifam->ifam_index != ifi->index)
break;
if (findproto((char *)ifam + ifam->ifam_hdrlen,
ifam->ifam_addrs) != AF_INET)
break;
sa = get_ifa((char *)ifam + ifam->ifam_hdrlen,
ifam->ifam_addrs);
if (sa == NULL)
goto done;
memcpy(&a, &((struct sockaddr_in *)sa)->sin_addr, sizeof(a));
if (a.s_addr == INADDR_ANY)
break;
/*
* If we are in the process of deleting an address, ignore
* messages generated by that process.
*/
if (a.s_addr == deleting.s_addr) {
deleting.s_addr = INADDR_ANY;
break;
}
if ((client->flags & IS_RESPONSIBLE) == 0)
/* We're not responsible yet! */
break;
if (adding.s_addr == INADDR_ANY && client->active &&
a.s_addr == client->active->address.s_addr) {
/* Tell the priv process active_addr is gone. */
log_warnx("Active address (%s) deleted; exiting",
inet_ntoa(client->active->address));
memset(&b, 0, sizeof(b));
add_address(b, b);
/* No need to write resolv.conf now. */
client->flags &= ~IS_RESPONSIBLE;
quit = INTERNALSIG;
break;
}
if (deleting.s_addr != INADDR_ANY) {
strlcpy(ntoabuf, inet_ntoa(a), sizeof(ntoabuf));
rslt = asprintf(&errmsg, "%s, not %s, deleted from %s",
ntoabuf, inet_ntoa(deleting), ifi->name);
} else
rslt = asprintf(&errmsg, "%s deleted from %s",
inet_ntoa(a), ifi->name);
goto die;
case RTM_DESYNC:
log_warnx("route socket buffer overflow");
break;
case RTM_IFINFO:
ifm = (struct if_msghdr *)rtm;
if (ifm->ifm_index != ifi->index)
break;
if ((rtm->rtm_flags & RTF_UP) == 0) {
rslt = asprintf(&errmsg, "%s down", ifi->name);
goto die;
}
if (ifi->flags & IFI_VALID_LLADDR) {
memcpy(&hw, &ifi->hw_address, sizeof(hw));
get_hw_address(ifi);
if (memcmp(&hw, &ifi->hw_address, sizeof(hw))) {
log_warnx("LLADDR changed; restarting");
ifi->flags |= IFI_NEW_LLADDR;
quit = SIGHUP;
goto done;
}
}
linkstat = interface_status(ifi);
if (linkstat != ifi->linkstat) {
#ifdef DEBUG
log_debug("link state %s -> %s",
ifi->linkstat ? "up" : "down",
linkstat ? "up" : "down");
#endif /* DEBUG */
ifi->linkstat = linkstat;
if (ifi->linkstat) {
if (client->state == S_PREBOOT) {
state_preboot(ifi);
get_hw_address(ifi);
} else {
client->state = S_REBOOTING;
state_reboot(ifi);
}
} else {
/* Let monitoring programs see link loss. */
if (strlen(path_option_db))
write_option_db("", 0);
/* No need to wait for anything but link. */
cancel_timeout();
}
}
break;
case RTM_IFANNOUNCE:
ifan = (struct if_announcemsghdr *)rtm;
if (ifan->ifan_what == IFAN_DEPARTURE &&
ifan->ifan_index == ifi->index) {
rslt = asprintf(&errmsg, "%s departured", ifi->name);
goto die;
}
break;
default:
break;
}
/* Something has happened. Try to write out the resolv.conf. */
if (client->active && client->active->resolv_conf &&
client->flags & IS_RESPONSIBLE)
write_resolv_conf(client->active->resolv_conf,
strlen(client->active->resolv_conf));
done:
free(rtmmsg);
return;
die:
if (rslt == -1)
fatalx("no memory for errmsg");
fatalx("%s", errmsg);
}
char **saved_argv;
int sock;
int
main(int argc, char *argv[])
{
struct interface_info *ifi;
struct option_data *opt;
struct ifreq ifr;
struct ieee80211_nwid nwid;
struct stat sb;
int ch, fd, socket_fd[2];
struct passwd *pw;
char *ignore_list = NULL;
ssize_t tailn;
int rtfilter, tailfd, q_flag, d_flag;
saved_argv = argv;
if (isatty(STDERR_FILENO))
log_perror = 1; /* log to stderr until daemonized */
else
log_perror = 0; /* can't log to stderr */
log_init(log_perror, LOG_DAEMON);
log_setverbose(1);
q_flag = d_flag = 0;
while ((ch = getopt(argc, argv, "c:di:l:L:qu")) != -1)
switch (ch) {
case 'c':
path_dhclient_conf = optarg;
break;
case 'd':
d_flag = 1;
break;
case 'i':
ignore_list = optarg;
break;
case 'l':
path_dhclient_db = optarg;
if (lstat(path_dhclient_db, &sb) != -1) {
if (!S_ISREG(sb.st_mode))
fatalx("'%s' is not a regular file",
path_dhclient_db);
}
break;
case 'L':
strlcat(path_option_db, optarg, PATH_MAX);
if (lstat(path_option_db, &sb) != -1) {
if (!S_ISREG(sb.st_mode))
fatalx("'%s' is not a regular file",
path_option_db);
}
break;
case 'q':
q_flag = 1;
break;
case 'u':
unknown_ok = 0;
break;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc != 1 || (q_flag && d_flag))
usage();
if (d_flag)
daemonize = 0;
if (q_flag)
log_perror = 0;
log_init(log_perror, LOG_DAEMON);
ifi = calloc(1, sizeof(struct interface_info));
if (ifi == NULL)
fatalx("ifi calloc");
get_ifname(ifi, argv[0]);
ifi->index = if_nametoindex(ifi->name);
if (ifi->index == 0)
fatalx("%s: no such interface", ifi->name);
get_hw_address(ifi);
tzset();
/* Get the ssid if present. */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
fatalx("Can't create socket to get ssid");
memset(&ifr, 0, sizeof(ifr));
memset(&nwid, 0, sizeof(nwid));
ifr.ifr_data = (caddr_t)&nwid;
strlcpy(ifr.ifr_name, ifi->name, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCG80211NWID, (caddr_t)&ifr) == 0) {
memset(ifi->ssid, 0, sizeof(ifi->ssid));
memcpy(ifi->ssid, nwid.i_nwid, nwid.i_len);
}
/* Put us into the correct rdomain */
ifi->rdomain = get_rdomain(ifi->name);
if (setrtable(ifi->rdomain) == -1)
fatal("setting routing table to %u", ifi->rdomain);
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
PF_UNSPEC, socket_fd) == -1)
fatal("socketpair");
if ((nullfd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1)
fatal("cannot open %s", _PATH_DEVNULL);
fork_privchld(ifi, socket_fd[0], socket_fd[1]);
close(socket_fd[0]);
if ((unpriv_ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
fatalx("no memory for unpriv_ibuf");
imsg_init(unpriv_ibuf, socket_fd[1]);
config = calloc(1, sizeof(struct client_config));
if (config == NULL)
fatalx("config calloc");
TAILQ_INIT(&config->reject_list);
ifi->client = calloc(1, sizeof(struct client_state));
if (ifi->client == NULL)
fatalx("client calloc");
TAILQ_INIT(&ifi->client->leases);
TAILQ_INIT(&ifi->client->offered_leases);
read_client_conf(ifi);
/*
* Set default client identifier, if needed, *before* reading
* the leases file! Changes to the lladdr will trigger a restart
* and go through here again.
*
* Check both len && data so
*
* send dhcp-client-identifier "";
*
* can be used to suppress sending the default client
* identifier.
*/
opt = &config->send_options[DHO_DHCP_CLIENT_IDENTIFIER];
if (opt->len == 0 && opt->data == NULL) {
opt->data = calloc(1, ETHER_ADDR_LEN + 1);
if (opt->data == NULL)
fatalx("no memory for default client identifier");
opt->data[0] = HTYPE_ETHER;
memcpy(&opt->data[1], ifi->hw_address.ether_addr_octet,
ETHER_ADDR_LEN);
opt->len = ETHER_ADDR_LEN + 1;
}
if ((pw = getpwnam("_dhcp")) == NULL)
fatalx("no such user: _dhcp");
if (path_dhclient_db == NULL && asprintf(&path_dhclient_db, "%s.%s",
_PATH_DHCLIENT_DB, ifi->name) == -1)
fatalx("asprintf");
/* 2nd stage (post fork) config setup. */
if (ignore_list)
apply_ignore_list(ignore_list);
tailfd = open("/etc/resolv.conf.tail", O_RDONLY);
if (tailfd == -1) {
if (errno != ENOENT)
fatal("Cannot open /etc/resolv.conf.tail");
} else if (fstat(tailfd, &sb) == -1) {
fatal("Cannot stat /etc/resolv.conf.tail");
} else {
if (sb.st_size > 0 && sb.st_size < SIZE_MAX) {
config->resolv_tail = calloc(1, sb.st_size + 1);
if (config->resolv_tail == NULL) {
fatalx("no memory for resolv.conf.tail "
"contents");
}
tailn = read(tailfd, config->resolv_tail, sb.st_size);
if (tailn == -1)
fatal("Couldn't read resolv.conf.tail");
else if (tailn == 0)
fatalx("Got no data from resolv.conf.tail");
else if (tailn != sb.st_size)
fatalx("Short read of resolv.conf.tail");
}
close(tailfd);
}
if ((fd = open(path_dhclient_db,
O_RDONLY|O_EXLOCK|O_CREAT|O_NOFOLLOW, 0640)) == -1)
fatal("can't open and lock %s", path_dhclient_db);
read_client_leases(ifi);
if ((leaseFile = fopen(path_dhclient_db, "w")) == NULL)
fatal("can't open %s", path_dhclient_db);
rewrite_client_leases(ifi);
close(fd);
/*
* Do the initial status check and possible force up before creating
* the routing socket. If we bounce the interface down and up while
* the routing socket is listening, the RTM_IFINFO message with the
* RTF_UP flag reset will cause premature exit.
*/
ifi->linkstat = interface_status(ifi);
if (ifi->linkstat == 0)
interface_link_forceup(ifi->name);
if ((routefd = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
fatal("socket(PF_ROUTE, SOCK_RAW)");
rtfilter = ROUTE_FILTER(RTM_NEWADDR) | ROUTE_FILTER(RTM_DELADDR) |
ROUTE_FILTER(RTM_IFINFO) | ROUTE_FILTER(RTM_IFANNOUNCE);
if (setsockopt(routefd, PF_ROUTE, ROUTE_MSGFILTER,
&rtfilter, sizeof(rtfilter)) == -1)
fatal("setsockopt(ROUTE_MSGFILTER)");
if (setsockopt(routefd, AF_ROUTE, ROUTE_TABLEFILTER, &ifi->rdomain,
sizeof(ifi->rdomain)) == -1)
fatal("setsockopt(ROUTE_TABLEFILTER)");
/* Register the interface. */
if_register_receive(ifi);
if_register_send(ifi);
if (chroot(_PATH_VAREMPTY) == -1)
fatalx("chroot");
if (chdir("/") == -1)
fatalx("chdir(\"/\")");
if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
fatalx("setresgid");
if (setgroups(1, &pw->pw_gid) == -1)
fatalx("setgroups");
if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatalx("setresuid");
endpwent();
if (daemonize) {
if (pledge("stdio inet dns route proc", NULL) == -1)
fatalx("pledge");
} else {
if (pledge("stdio inet dns route", NULL) == -1)
fatalx("pledge");
}
setproctitle("%s", ifi->name);
time(&ifi->client->startup_time);
if (ifi->linkstat) {
ifi->client->state = S_REBOOTING;
state_reboot(ifi);
} else {
ifi->client->state = S_PREBOOT;
state_preboot(ifi);
}
dispatch(ifi);
/* not reached */
return (0);
}
void
usage(void)
{
extern char *__progname;
fprintf(stderr,
"usage: %s [-d | -q] [-u] [-c file] [-i options] [-L file] "
"[-l file] interface\n", __progname);
exit(1);
}
void
state_preboot(void *xifi)
{
struct interface_info *ifi = xifi;
struct client_state *client = ifi->client;
static int preamble;
time_t cur_time;
int interval;
time(&cur_time);
interval = (int)(cur_time - client->startup_time);
ifi->linkstat = interface_status(ifi);
if (log_perror && interval > 3) {
if (!preamble && !ifi->linkstat) {
fprintf(stderr, "%s: no link ....", ifi->name);
preamble = 1;
}
if (preamble) {
if (ifi->linkstat)
fprintf(stderr, " got link\n");
else if (interval > config->link_timeout)
fprintf(stderr, " sleeping\n");
else
fprintf(stderr, ".");
fflush(stderr);
}
}
if (ifi->linkstat) {
client->state = S_REBOOTING;
set_timeout_interval(1, state_reboot, ifi);
} else {
if (interval > config->link_timeout)
go_daemon();
client->state = S_PREBOOT;
set_timeout_interval(1, state_preboot, ifi);
}
}
/*
* Called when the interface link becomes active.
*/
void
state_reboot(void *xifi)
{
struct interface_info *ifi = xifi;
struct client_state *client = ifi->client;
char ifname[IF_NAMESIZE];
struct client_lease *lp;
time_t cur_time;
int i;
cancel_timeout();
deleting.s_addr = INADDR_ANY;
adding.s_addr = INADDR_ANY;
time(&cur_time);
if (client->active) {
if (client->active->expiry <= cur_time)
client->active = NULL;
else if (addressinuse(ifi, client->active->address, ifname) &&
strncmp(ifname, ifi->name, IF_NAMESIZE) != 0)
client->active = NULL;
}
/* Run through the list of leases and see if one can be used. */
i = DHO_DHCP_CLIENT_IDENTIFIER;
TAILQ_FOREACH(lp, &client->leases, next) {
if (strcmp(lp->ssid, ifi->ssid) != 0)
continue;
if ((lp->options[i].len != 0) && ((lp->options[i].len !=
config->send_options[i].len) ||
memcmp(lp->options[i].data, config->send_options[i].data,
lp->options[i].len)))
continue;
if (addressinuse(ifi, lp->address, ifname) &&
strncmp(ifname, ifi->name, IF_NAMESIZE) != 0)
continue;
if (client->active || lp->is_static)
break;
if (lp->expiry > cur_time) {
client->active = lp;
break;
}
}
/* If we don't remember an active lease, go straight to INIT. */
if (!client->active || client->active->is_bootp) {
client->state = S_INIT;
state_init(ifi);
return;
}
client->xid = arc4random();
make_request(ifi, client->active);
client->destination.s_addr = INADDR_BROADCAST;
client->first_sending = time(NULL);
client->interval = 0;
send_request(ifi);
}
/*
* Called when a lease has completely expired and we've been unable to
* renew it.
*/
void
state_init(void *xifi)
{
struct interface_info *ifi = xifi;
struct client_state *client = ifi->client;
client->xid = arc4random();
make_discover(ifi, client->active);
client->destination.s_addr = INADDR_BROADCAST;
client->state = S_SELECTING;
client->first_sending = time(NULL);
client->interval = 0;
send_discover(ifi);
}
/*
* Called when one or more DHCPOFFER packets have been received and a
* configurable period of time has passed.
*/
void
state_selecting(void *xifi)
{
struct interface_info *ifi = xifi;
struct client_state *client = ifi->client;
struct client_lease *lease, *picked;
cancel_timeout();
/* Take the first valid DHCPOFFER. */
TAILQ_FOREACH_SAFE(picked, &client->offered_leases, next, lease) {
if (picked->is_invalid == 0) {
TAILQ_REMOVE(&client->offered_leases, picked, next);
break;
}
}
/* DECLINE the rest of the offers. */
while (!TAILQ_EMPTY(&client->offered_leases)) {
lease = TAILQ_FIRST(&client->offered_leases);
TAILQ_REMOVE(&client->offered_leases, lease, next);
make_decline(ifi, lease);
send_decline(ifi);
free_client_lease(lease);
}
if (!picked) {
state_panic(ifi);
return;
}
/* If it was a BOOTREPLY, we can just take the lease right now. */
if (!picked->options[DHO_DHCP_MESSAGE_TYPE].len) {
struct option_data *option;
client->new = picked;
/*
* Set (unsigned 32 bit) options
*
* DHO_DHCP_LEASE_TIME (12000 seconds),
* DHO_RENEWAL_TIME (8000 seconds)
* DHO_REBINDING_TIME (10000 seconds)
*
* so bind_lease() can set the lease times. Note that the
* values must be big-endian.
*/
option = &client->new->options[DHO_DHCP_LEASE_TIME];
option->data = malloc(4);
if (option->data) {
option->len = 4;
memcpy(option->data, "\x00\x00\x2e\xe0", 4);
}
option = &client->new->options[DHO_DHCP_RENEWAL_TIME];
option->data = malloc(4);
if (option->data) {
option->len = 4;
memcpy(option->data, "\x00\x00\x1f\x40", 4);
}
option = &client->new->options[DHO_DHCP_REBINDING_TIME];
option->data = malloc(4);
if (option->data) {
option->len = 4;
memcpy(option->data, "\x00\x00\x27\x10", 4);
}
client->state = S_REQUESTING;
bind_lease(ifi);
return;
}
client->destination.s_addr = INADDR_BROADCAST;
client->state = S_REQUESTING;
client->first_sending = time(NULL);
client->interval = 0;
/*
* Make a DHCPREQUEST packet from the lease we picked. Keep
* the current xid, as all offers should have had the same
* one.
*/
make_request(ifi, picked);
/* Toss the lease we picked - we'll get it back in a DHCPACK. */
free_client_lease(picked);
send_request(ifi);
}
void
dhcpack(struct interface_info *ifi, struct in_addr client_addr,
struct option_data *options, char *info)
{
struct client_state *client = ifi->client;
struct client_lease *lease;
if (client->state != S_REBOOTING &&
client->state != S_REQUESTING &&
client->state != S_RENEWING &&
client->state != S_REBINDING) {
#ifdef DEBUG
log_debug("Unexpected %s. State #%d", info, client->state);
#endif /* DEBUG */
return;
}
log_info("%s", info);
lease = packet_to_lease(ifi, client_addr, options);
if (lease->is_invalid) {
log_info("Unsatisfactory %s", info);
make_decline(ifi, lease);
send_decline(ifi);
free_client_lease(lease);
client->state = S_INIT;
state_init(ifi);
return;
}
client->new = lease;
memcpy(client->new->ssid, ifi->ssid, sizeof(client->new->ssid));
/* Stop resending DHCPREQUEST. */
cancel_timeout();
bind_lease(ifi);
}
void
bind_lease(struct interface_info *ifi)
{
struct client_state *client = ifi->client;
struct in_addr gateway, mask;
struct option_data *opt;
struct option_data *options;
struct client_lease *lease, *pl;
time_t cur_time;
int seen;
/*
* Clear out any old resolv_conf in case the lease has been here
* before (e.g. static lease).
*/
free(client->new->resolv_conf);
client->new->resolv_conf = NULL;
lease = apply_defaults(client->new);
options = lease->options;
set_lease_times(lease);
client->new->expiry = lease->expiry;
client->new->renewal = lease->renewal;
client->new->rebind = lease->rebind;
/*
* A duplicate lease once we are responsible & S_RENEWING means we
* don't need to change the interface, routing table or resolv.conf.
*/
if ((client->flags & IS_RESPONSIBLE) && client->state == S_RENEWING &&
compare_lease(client->active, client->new) == 0) {
client->new->resolv_conf = client->active->resolv_conf;