/
dhcpcd.c
2697 lines (2401 loc) · 61.3 KB
/
dhcpcd.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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* dhcpcd - DHCP client daemon
* Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
* 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 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 AUTHOR 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.
*/
static const char dhcpcd_copyright[] = "Copyright (c) 2006-2023 Roy Marples";
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <time.h>
#include "config.h"
#include "arp.h"
#include "common.h"
#include "control.h"
#include "dev.h"
#include "dhcp-common.h"
#include "dhcpcd.h"
#include "dhcp.h"
#include "dhcp6.h"
#include "duid.h"
#include "eloop.h"
#include "if.h"
#include "if-options.h"
#include "ipv4.h"
#include "ipv4ll.h"
#include "ipv6.h"
#include "ipv6nd.h"
#include "logerr.h"
#include "privsep.h"
#include "script.h"
#ifdef HAVE_CAPSICUM
#include <sys/capsicum.h>
#endif
#ifdef HAVE_OPENSSL
#include <openssl/crypto.h>
#endif
#ifdef HAVE_UTIL_H
#include <util.h>
#endif
#ifdef USE_SIGNALS
const int dhcpcd_signals[] = {
SIGTERM,
SIGINT,
SIGALRM,
SIGHUP,
SIGUSR1,
SIGUSR2,
SIGCHLD,
};
const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals);
const int dhcpcd_signals_ignore[] = {
SIGPIPE,
};
const size_t dhcpcd_signals_ignore_len = __arraycount(dhcpcd_signals_ignore);
#endif
const char *dhcpcd_default_script = SCRIPT;
static void
usage(void)
{
printf("usage: "PACKAGE"\t[-146ABbDdEGgHJKLMNPpqTV]\n"
"\t\t[-C, --nohook hook] [-c, --script script]\n"
"\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
"\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
"\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n"
"\t\t[-l, --leasetime seconds] [-m, --metric metric]\n"
"\t\t[-O, --nooption option] [-o, --option option]\n"
"\t\t[-Q, --require option] [-r, --request address]\n"
"\t\t[-S, --static value]\n"
"\t\t[-s, --inform address[/cidr[/broadcast_address]]]\n [--inform6]"
"\t\t[-t, --timeout seconds] [-u, --userclass class]\n"
"\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n"
"\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n"
"\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
"\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n"
" "PACKAGE"\t-n, --rebind [interface]\n"
" "PACKAGE"\t-k, --release [interface]\n"
" "PACKAGE"\t-U, --dumplease interface\n"
" "PACKAGE"\t--version\n"
" "PACKAGE"\t-x, --exit [interface]\n");
}
static void
free_globals(struct dhcpcd_ctx *ctx)
{
struct dhcp_opt *opt;
if (ctx->ifac) {
for (; ctx->ifac > 0; ctx->ifac--)
free(ctx->ifav[ctx->ifac - 1]);
free(ctx->ifav);
ctx->ifav = NULL;
}
if (ctx->ifdc) {
for (; ctx->ifdc > 0; ctx->ifdc--)
free(ctx->ifdv[ctx->ifdc - 1]);
free(ctx->ifdv);
ctx->ifdv = NULL;
}
if (ctx->ifcc) {
for (; ctx->ifcc > 0; ctx->ifcc--)
free(ctx->ifcv[ctx->ifcc - 1]);
free(ctx->ifcv);
ctx->ifcv = NULL;
}
#ifdef INET
if (ctx->dhcp_opts) {
for (opt = ctx->dhcp_opts;
ctx->dhcp_opts_len > 0;
opt++, ctx->dhcp_opts_len--)
free_dhcp_opt_embenc(opt);
free(ctx->dhcp_opts);
ctx->dhcp_opts = NULL;
}
#endif
#ifdef INET6
if (ctx->nd_opts) {
for (opt = ctx->nd_opts;
ctx->nd_opts_len > 0;
opt++, ctx->nd_opts_len--)
free_dhcp_opt_embenc(opt);
free(ctx->nd_opts);
ctx->nd_opts = NULL;
}
#ifdef DHCP6
if (ctx->dhcp6_opts) {
for (opt = ctx->dhcp6_opts;
ctx->dhcp6_opts_len > 0;
opt++, ctx->dhcp6_opts_len--)
free_dhcp_opt_embenc(opt);
free(ctx->dhcp6_opts);
ctx->dhcp6_opts = NULL;
}
#endif
#endif
if (ctx->vivso) {
for (opt = ctx->vivso;
ctx->vivso_len > 0;
opt++, ctx->vivso_len--)
free_dhcp_opt_embenc(opt);
free(ctx->vivso);
ctx->vivso = NULL;
}
}
static void
handle_exit_timeout(void *arg)
{
struct dhcpcd_ctx *ctx;
ctx = arg;
logerrx("timed out");
if (!(ctx->options & DHCPCD_MANAGER)) {
struct interface *ifp;
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
if (ifp->active == IF_ACTIVE_USER)
script_runreason(ifp, "STOPPED");
}
eloop_exit(ctx->eloop, EXIT_FAILURE);
return;
}
ctx->options |= DHCPCD_NOWAITIP;
dhcpcd_daemonise(ctx);
}
static const char *
dhcpcd_af(int af)
{
switch (af) {
case AF_UNSPEC:
return "IP";
case AF_INET:
return "IPv4";
case AF_INET6:
return "IPv6";
default:
return NULL;
}
}
int
dhcpcd_ifafwaiting(const struct interface *ifp)
{
unsigned long long opts;
bool foundany = false;
if (ifp->active != IF_ACTIVE_USER)
return AF_MAX;
#define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
opts = ifp->options->options;
#ifdef INET
if (opts & DHCPCD_WAITIP4 ||
(opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
{
bool foundaddr = ipv4_hasaddr(ifp);
if (opts & DHCPCD_WAITIP4 && !foundaddr)
return AF_INET;
if (foundaddr)
foundany = true;
}
#endif
#ifdef INET6
if (opts & DHCPCD_WAITIP6 ||
(opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
{
bool foundaddr = ipv6_hasaddr(ifp);
if (opts & DHCPCD_WAITIP6 && !foundaddr)
return AF_INET6;
if (foundaddr)
foundany = true;
}
#endif
if (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL) && !foundany)
return AF_UNSPEC;
return AF_MAX;
}
int
dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx)
{
unsigned long long opts;
const struct interface *ifp;
int af;
if (!(ctx->options & DHCPCD_WAITOPTS))
return AF_MAX;
opts = ctx->options;
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
#ifdef INET
if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) &&
ipv4_hasaddr(ifp))
opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4);
#endif
#ifdef INET6
if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) &&
ipv6_hasaddr(ifp))
opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6);
#endif
if (!(opts & DHCPCD_WAITOPTS))
break;
}
if (opts & DHCPCD_WAITIP)
af = AF_UNSPEC;
else if (opts & DHCPCD_WAITIP4)
af = AF_INET;
else if (opts & DHCPCD_WAITIP6)
af = AF_INET6;
else
return AF_MAX;
return af;
}
static int
dhcpcd_ipwaited(struct dhcpcd_ctx *ctx)
{
struct interface *ifp;
int af;
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
logdebugx("%s: waiting for an %s address",
ifp->name, dhcpcd_af(af));
return 0;
}
}
if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) {
logdebugx("waiting for an %s address",
dhcpcd_af(af));
return 0;
}
return 1;
}
#ifndef THERE_IS_NO_FORK
void
dhcpcd_daemonised(struct dhcpcd_ctx *ctx)
{
unsigned int logopts = loggetopts();
/*
* Stop writing to stderr.
* On the happy path, only the manager process writes to stderr,
* so this just stops wasting fprintf calls to nowhere.
*/
logopts &= ~LOGERR_ERR;
logsetopts(logopts);
/*
* We need to do something with stdout/stderr to avoid SIGPIPE.
* We know that stdin is already mapped to /dev/null.
* TODO: Capture script output and log it to the logfile and/or syslog.
*/
dup2(STDIN_FILENO, STDOUT_FILENO);
dup2(STDIN_FILENO, STDERR_FILENO);
ctx->options |= DHCPCD_DAEMONISED;
}
#endif
/* Returns the pid of the child, otherwise 0. */
void
dhcpcd_daemonise(struct dhcpcd_ctx *ctx)
{
#ifdef THERE_IS_NO_FORK
eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
errno = ENOSYS;
return;
#else
int exit_code;
if (ctx->options & DHCPCD_DAEMONISE &&
!(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP)))
{
if (!dhcpcd_ipwaited(ctx))
return;
}
if (ctx->options & DHCPCD_ONESHOT) {
loginfox("exiting due to oneshot");
eloop_exit(ctx->eloop, EXIT_SUCCESS);
return;
}
eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
if (ctx->options & DHCPCD_DAEMONISED ||
!(ctx->options & DHCPCD_DAEMONISE))
return;
#ifdef PRIVSEP
if (IN_PRIVSEP(ctx))
ps_daemonised(ctx);
else
#endif
dhcpcd_daemonised(ctx);
eloop_event_delete(ctx->eloop, ctx->fork_fd);
exit_code = EXIT_SUCCESS;
if (write(ctx->fork_fd, &exit_code, sizeof(exit_code)) == -1)
logerr(__func__);
close(ctx->fork_fd);
ctx->fork_fd = -1;
#endif
}
static void
dhcpcd_drop(struct interface *ifp, int stop)
{
#ifdef DHCP6
dhcp6_drop(ifp, stop ? NULL : "EXPIRE6");
#endif
#ifdef INET6
ipv6nd_drop(ifp);
ipv6_drop(ifp);
#endif
#ifdef IPV4LL
ipv4ll_drop(ifp);
#endif
#ifdef INET
dhcp_drop(ifp, stop ? "STOP" : "EXPIRE");
#endif
#ifdef ARP
arp_drop(ifp);
#endif
#if !defined(DHCP6) && !defined(DHCP)
UNUSED(stop);
#endif
}
static void
stop_interface(struct interface *ifp, const char *reason)
{
struct dhcpcd_ctx *ctx;
ctx = ifp->ctx;
loginfox("%s: removing interface", ifp->name);
ifp->options->options |= DHCPCD_STOPPING;
dhcpcd_drop(ifp, 1);
script_runreason(ifp, reason == NULL ? "STOPPED" : reason);
/* Delete all timeouts for the interfaces */
eloop_q_timeout_delete(ctx->eloop, ELOOP_QUEUE_ALL, NULL, ifp);
/* De-activate the interface */
ifp->active = IF_INACTIVE;
ifp->options->options &= ~DHCPCD_STOPPING;
if (!(ctx->options & (DHCPCD_MANAGER | DHCPCD_TEST)))
eloop_exit(ctx->eloop, EXIT_FAILURE);
}
static void
configure_interface1(struct interface *ifp)
{
struct if_options *ifo = ifp->options;
/* Do any platform specific configuration */
if_conf(ifp);
/* If we want to release a lease, we can't really persist the
* address either. */
if (ifo->options & DHCPCD_RELEASE)
ifo->options &= ~DHCPCD_PERSISTENT;
if (ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) {
ifo->options &= ~DHCPCD_ARP;
if (!(ifp->flags & IFF_MULTICAST))
ifo->options &= ~DHCPCD_IPV6RS;
if (!(ifo->options & (DHCPCD_INFORM | DHCPCD_WANTDHCP)))
ifo->options |= DHCPCD_STATIC;
}
if (ifo->metric != -1)
ifp->metric = (unsigned int)ifo->metric;
#ifdef INET6
/* We want to setup INET6 on the interface as soon as possible. */
if (ifp->active == IF_ACTIVE_USER &&
ifo->options & DHCPCD_IPV6 &&
!(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST)))
{
/* If not doing any DHCP, disable the RDNSS requirement. */
if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6)))
ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
if_setup_inet6(ifp);
}
#endif
if (!(ifo->options & DHCPCD_IAID)) {
/*
* An IAID is for identifying a unqiue interface within
* the client. It is 4 bytes long. Working out a default
* value is problematic.
*
* Interface name and number are not stable
* between different OS's. Some OS's also cannot make
* up their mind what the interface should be called
* (yes, udev, I'm looking at you).
* Also, the name could be longer than 4 bytes.
* Also, with pluggable interfaces the name and index
* could easily get swapped per actual interface.
*
* The MAC address is 6 bytes long, the final 3
* being unique to the manufacturer and the initial 3
* being unique to the organisation which makes it.
* We could use the last 4 bytes of the MAC address
* as the IAID as it's the most stable part given the
* above, but equally it's not guaranteed to be
* unique.
*
* Given the above, and our need to reliably work
* between reboots without persitent storage,
* generating the IAID from the MAC address is the only
* logical default.
* Saying that, if a VLANID has been specified then we
* can use that. It's possible that different interfaces
* can have the same VLANID, but this is no worse than
* generating the IAID from the duplicate MAC address.
*
* dhclient uses the last 4 bytes of the MAC address.
* dibbler uses an increamenting counter.
* wide-dhcpv6 uses 0 or a configured value.
* odhcp6c uses 1.
* Windows 7 uses the first 3 bytes of the MAC address
* and an unknown byte.
* dhcpcd-6.1.0 and earlier used the interface name,
* falling back to interface index if name > 4.
*/
if (ifp->vlanid != 0) {
uint32_t vlanid;
/* Maximal VLANID is 4095, so prefix with 0xff
* so we don't conflict with an interface index. */
vlanid = htonl(ifp->vlanid | 0xff000000);
memcpy(ifo->iaid, &vlanid, sizeof(vlanid));
} else if (ifo->options & DHCPCD_ANONYMOUS)
memset(ifo->iaid, 0, sizeof(ifo->iaid));
else if (ifp->hwlen >= sizeof(ifo->iaid)) {
memcpy(ifo->iaid,
ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
sizeof(ifo->iaid));
} else {
uint32_t len;
len = (uint32_t)strlen(ifp->name);
if (len <= sizeof(ifo->iaid)) {
memcpy(ifo->iaid, ifp->name, len);
if (len < sizeof(ifo->iaid))
memset(ifo->iaid + len, 0,
sizeof(ifo->iaid) - len);
} else {
/* IAID is the same size as a uint32_t */
len = htonl(ifp->index);
memcpy(ifo->iaid, &len, sizeof(ifo->iaid));
}
}
ifo->options |= DHCPCD_IAID;
}
#ifdef DHCP6
if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 &&
ifp->name[0] != '\0')
{
ifo->ia = malloc(sizeof(*ifo->ia));
if (ifo->ia == NULL)
logerr(__func__);
else {
ifo->ia_len = 1;
ifo->ia->ia_type = D6_OPTION_IA_NA;
memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid));
memset(&ifo->ia->addr, 0, sizeof(ifo->ia->addr));
#ifndef SMALL
ifo->ia->sla = NULL;
ifo->ia->sla_len = 0;
#endif
}
} else {
size_t i;
for (i = 0; i < ifo->ia_len; i++) {
if (!ifo->ia[i].iaid_set) {
memcpy(&ifo->ia[i].iaid, ifo->iaid,
sizeof(ifo->ia[i].iaid));
ifo->ia[i].iaid_set = 1;
}
}
}
#endif
/* If root is network mounted, we don't want to kill the connection
* if the DHCP server goes the way of the dodo OR dhcpcd is rebooting
* and the lease file has expired. */
if (is_root_local() == 0)
ifo->options |= DHCPCD_LASTLEASE_EXTEND;
}
int
dhcpcd_selectprofile(struct interface *ifp, const char *profile)
{
struct if_options *ifo;
char pssid[PROFILE_LEN];
if (ifp->ssid_len) {
ssize_t r;
r = print_string(pssid, sizeof(pssid), OT_ESCSTRING,
ifp->ssid, ifp->ssid_len);
if (r == -1) {
logerr(__func__);
pssid[0] = '\0';
}
} else
pssid[0] = '\0';
ifo = read_config(ifp->ctx, ifp->name, pssid, profile);
if (ifo == NULL) {
logdebugx("%s: no profile %s", ifp->name, profile);
return -1;
}
if (profile != NULL) {
strlcpy(ifp->profile, profile, sizeof(ifp->profile));
loginfox("%s: selected profile %s", ifp->name, profile);
} else
*ifp->profile = '\0';
free_options(ifp->ctx, ifp->options);
ifp->options = ifo;
if (profile) {
add_options(ifp->ctx, ifp->name, ifp->options,
ifp->ctx->argc, ifp->ctx->argv);
configure_interface1(ifp);
}
return 1;
}
static void
configure_interface(struct interface *ifp, int argc, char **argv,
unsigned long long options)
{
time_t old;
old = ifp->options ? ifp->options->mtime : 0;
dhcpcd_selectprofile(ifp, NULL);
if (ifp->options == NULL) {
/* dhcpcd cannot continue with this interface. */
ifp->active = IF_INACTIVE;
return;
}
add_options(ifp->ctx, ifp->name, ifp->options, argc, argv);
ifp->options->options |= options;
configure_interface1(ifp);
/* If the mtime has changed drop any old lease */
if (old != 0 && ifp->options->mtime != old) {
logwarnx("%s: config file changed, expiring leases",
ifp->name);
dhcpcd_drop(ifp, 0);
}
}
static void
dhcpcd_initstate1(struct interface *ifp, int argc, char **argv,
unsigned long long options)
{
struct if_options *ifo;
configure_interface(ifp, argc, argv, options);
if (!ifp->active)
return;
ifo = ifp->options;
ifo->options |= options;
#ifdef INET6
if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == -1) {
logerr(__func__);
ifo->options &= ~DHCPCD_IPV6;
}
#endif
}
static void
dhcpcd_initstate(struct interface *ifp, unsigned long long options)
{
dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options);
}
static void
dhcpcd_reportssid(struct interface *ifp)
{
char pssid[IF_SSIDLEN * 4];
if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
ifp->ssid, ifp->ssid_len) == -1)
{
logerr(__func__);
return;
}
loginfox("%s: connected to Access Point: %s", ifp->name, pssid);
}
static void
dhcpcd_nocarrier_roaming(struct interface *ifp)
{
loginfox("%s: carrier lost - roaming", ifp->name);
#ifdef ARP
arp_drop(ifp);
#endif
#ifdef INET
dhcp_abort(ifp);
#endif
#ifdef DHCP6
dhcp6_abort(ifp);
#endif
rt_build(ifp->ctx, AF_UNSPEC);
script_runreason(ifp, "NOCARRIER_ROAMING");
}
void
dhcpcd_handlecarrier(struct interface *ifp, int carrier, unsigned int flags)
{
bool was_link_up = if_is_link_up(ifp);
bool was_roaming = if_roaming(ifp);
ifp->carrier = carrier;
ifp->flags = flags;
if (!if_is_link_up(ifp)) {
if (!ifp->active || (!was_link_up && !was_roaming))
return;
/*
* If the interface is roaming (generally on wireless)
* then while we are not up, we are not down either.
* Preserve the network state until we either disconnect
* or re-connect.
*/
if (!ifp->options->randomise_hwaddr && if_roaming(ifp)) {
dhcpcd_nocarrier_roaming(ifp);
return;
}
loginfox("%s: carrier lost", ifp->name);
script_runreason(ifp, "NOCARRIER");
dhcpcd_drop(ifp, 0);
if (ifp->options->randomise_hwaddr) {
bool is_up = ifp->flags & IFF_UP;
if (is_up)
if_down(ifp);
if (if_randomisemac(ifp) == -1 && errno != ENXIO)
logerr(__func__);
if (is_up)
if_up(ifp);
}
return;
}
/*
* At this point carrier is NOT DOWN and we have IFF_UP.
* We should treat LINK_UNKNOWN as up as the driver may not support
* link state changes.
* The consideration of any other information about carrier should
* be handled in the OS specific if_carrier() function.
*/
if (was_link_up)
return;
if (ifp->active) {
if (carrier == LINK_UNKNOWN)
loginfox("%s: carrier unknown, assuming up", ifp->name);
else
loginfox("%s: carrier acquired", ifp->name);
}
#if !defined(__linux__) && !defined(__NetBSD__)
/* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
* hardware address changes so we have to go
* through the disovery process to work it out. */
dhcpcd_handleinterface(ifp->ctx, 0, ifp->name);
#endif
if (ifp->wireless) {
uint8_t ossid[IF_SSIDLEN];
size_t olen;
olen = ifp->ssid_len;
memcpy(ossid, ifp->ssid, ifp->ssid_len);
if_getssid(ifp);
/* If we changed SSID network, drop leases */
if ((ifp->ssid_len != olen ||
memcmp(ifp->ssid, ossid, ifp->ssid_len)) && ifp->active)
{
dhcpcd_reportssid(ifp);
dhcpcd_drop(ifp, 0);
#ifdef IPV4LL
ipv4ll_reset(ifp);
#endif
}
}
if (!ifp->active)
return;
dhcpcd_initstate(ifp, 0);
script_runreason(ifp, "CARRIER");
#ifdef INET6
/* Set any IPv6 Routers we remembered to expire faster than they
* would normally as we maybe on a new network. */
ipv6nd_startexpire(ifp);
#ifdef IPV6_MANAGETEMPADDR
/* RFC4941 Section 3.5 */
ipv6_regentempaddrs(ifp);
#endif
#endif
dhcpcd_startinterface(ifp);
}
static void
warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid)
{
struct interface *ifn;
#ifdef INET6
size_t i;
struct if_ia *ia;
#endif
TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
if (ifn == ifp || !ifn->active)
continue;
if (ifn->options->options & DHCPCD_ANONYMOUS)
continue;
if (ia_type == 0 &&
memcmp(ifn->options->iaid, iaid,
sizeof(ifn->options->iaid)) == 0)
break;
#ifdef INET6
for (i = 0; i < ifn->options->ia_len; i++) {
ia = &ifn->options->ia[i];
if (ia->ia_type == ia_type &&
memcmp(ia->iaid, iaid, sizeof(ia->iaid)) == 0)
break;
}
#endif
}
/* This is only a problem if the interfaces are on the same network. */
if (ifn)
logerrx("%s: IAID conflicts with one assigned to %s",
ifp->name, ifn->name);
}
static void
dhcpcd_initduid(struct dhcpcd_ctx *ctx, struct interface *ifp)
{
char buf[DUID_LEN * 3];
if (ctx->duid != NULL) {
if (ifp == NULL)
goto log;
return;
}
duid_init(ctx, ifp);
if (ctx->duid == NULL)
return;
log:
loginfox("DUID %s",
hwaddr_ntoa(ctx->duid, ctx->duid_len, buf, sizeof(buf)));
}
void
dhcpcd_startinterface(void *arg)
{
struct interface *ifp = arg;
struct if_options *ifo = ifp->options;
if (ifo->options & DHCPCD_LINK && !if_is_link_up(ifp)) {
loginfox("%s: waiting for carrier", ifp->name);
return;
}
if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6) &&
!(ifo->options & DHCPCD_ANONYMOUS))
{
char buf[sizeof(ifo->iaid) * 3];
#ifdef INET6
size_t i;
struct if_ia *ia;
#endif
/* Try and init DUID from the interface hardware address */
dhcpcd_initduid(ifp->ctx, ifp);
/* Report IAIDs */
loginfox("%s: IAID %s", ifp->name,
hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid),
buf, sizeof(buf)));
warn_iaid_conflict(ifp, 0, ifo->iaid);
#ifdef INET6
for (i = 0; i < ifo->ia_len; i++) {
ia = &ifo->ia[i];
if (memcmp(ifo->iaid, ia->iaid, sizeof(ifo->iaid))) {
loginfox("%s: IA type %u IAID %s",
ifp->name, ia->ia_type,
hwaddr_ntoa(ia->iaid, sizeof(ia->iaid),
buf, sizeof(buf)));
warn_iaid_conflict(ifp, ia->ia_type, ia->iaid);
}
}
#endif
}
#ifdef INET6
if (ifo->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) {
logerr("%s: ipv6_start", ifp->name);
ifo->options &= ~DHCPCD_IPV6;
}
if (ifo->options & DHCPCD_IPV6) {
if (ifp->active == IF_ACTIVE_USER) {
ipv6_startstatic(ifp);
if (ifo->options & DHCPCD_IPV6RS)
ipv6nd_startrs(ifp);
}
#ifdef DHCP6
/* DHCPv6 could be turned off, but the interface
* is still delegated to. */
if (ifp->active)
dhcp6_find_delegates(ifp);
if (ifo->options & DHCPCD_DHCP6) {
if (ifp->active == IF_ACTIVE_USER) {
enum DH6S d6_state;
if (ifo->options & DHCPCD_IA_FORCED)
d6_state = DH6S_INIT;
else if (ifo->options & DHCPCD_INFORM6)
d6_state = DH6S_INFORM;
else
d6_state = DH6S_CONFIRM;
if (dhcp6_start(ifp, d6_state) == -1)
logerr("%s: dhcp6_start", ifp->name);
}
}
#endif
}
#endif
#ifdef INET
if (ifo->options & DHCPCD_IPV4 && ifp->active == IF_ACTIVE_USER) {
/* Ensure we have an IPv4 state before starting DHCP */
if (ipv4_getstate(ifp) != NULL)
dhcp_start(ifp);
}
#endif
}
static void
dhcpcd_prestartinterface(void *arg)
{
struct interface *ifp = arg;
struct dhcpcd_ctx *ctx = ifp->ctx;
bool randmac_down;
if (ifp->carrier <= LINK_DOWN &&
ifp->options->randomise_hwaddr &&
ifp->flags & IFF_UP)
{
if_down(ifp);
randmac_down = true;
} else
randmac_down = false;
if ((!(ctx->options & DHCPCD_MANAGER) ||
ifp->options->options & DHCPCD_IF_UP || randmac_down) &&
!(ifp->flags & IFF_UP))
{
if (ifp->options->randomise_hwaddr &&
if_randomisemac(ifp) == -1)
logerr(__func__);
if (if_up(ifp) == -1)
logerr(__func__);
}
dhcpcd_startinterface(ifp);
}