forked from adamlaska/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetworkd-dhcp4.c
More file actions
1791 lines (1413 loc) · 64.5 KB
/
networkd-dhcp4.c
File metadata and controls
1791 lines (1413 loc) · 64.5 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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <netinet/in.h>
#include <netinet/ip.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include "alloc-util.h"
#include "dhcp-client-internal.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "parse-util.h"
#include "network-internal.h"
#include "networkd-address.h"
#include "networkd-dhcp-prefix-delegation.h"
#include "networkd-dhcp4.h"
#include "networkd-ipv4acd.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-setlink.h"
#include "networkd-state-file.h"
#include "string-table.h"
#include "strv.h"
#include "sysctl-util.h"
static int dhcp4_request_address_and_routes(Link *link, bool announce);
void network_adjust_dhcp4(Network *network) {
assert(network);
if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4))
return;
if (network->dhcp_use_gateway < 0)
network->dhcp_use_gateway = network->dhcp_use_routes;
/* RFC7844 section 3.: MAY contain the Client Identifier option
* Section 3.5: clients MUST use client identifiers based solely on the link-layer address
* NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer
* if this option is not sent */
if (network->dhcp_anonymize &&
network->dhcp_client_identifier >= 0 &&
network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) {
log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.",
network->filename);
network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC;
}
if (network->dhcp_client_identifier < 0)
network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : DHCP_CLIENT_ID_DUID;
}
static int dhcp4_remove_address_and_routes(Link *link, bool only_marked) {
Address *address;
Route *route;
int k, r = 0;
assert(link);
SET_FOREACH(route, link->routes) {
if (route->source != NETWORK_CONFIG_SOURCE_DHCP4)
continue;
if (only_marked && !route_is_marked(route))
continue;
k = route_remove(route);
if (k < 0)
r = k;
route_cancel_request(route, link);
}
SET_FOREACH(address, link->addresses) {
if (address->source != NETWORK_CONFIG_SOURCE_DHCP4)
continue;
if (only_marked && !address_is_marked(address))
continue;
k = address_remove(address);
if (k < 0)
r = k;
address_cancel_request(address);
}
return r;
}
static int dhcp4_address_get(Link *link, Address **ret) {
Address *address;
assert(link);
SET_FOREACH(address, link->addresses) {
if (address->source != NETWORK_CONFIG_SOURCE_DHCP4)
continue;
if (address_is_marked(address))
continue;
if (ret)
*ret = address;
return 0;
}
return -ENOENT;
}
static int dhcp4_address_ready_callback(Address *address) {
assert(address);
assert(address->link);
/* Do not call this again. */
address->callback = NULL;
return dhcp4_check_ready(address->link);
}
int dhcp4_check_ready(Link *link) {
Address *address;
int r;
assert(link);
if (link->dhcp4_messages > 0) {
log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__);
return 0;
}
if (dhcp4_address_get(link, &address) < 0) {
log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__);
return 0;
}
if (!address_is_ready(address)) {
log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__);
address->callback = dhcp4_address_ready_callback;
return 0;
}
link->dhcp4_configured = true;
log_link_debug(link, "DHCPv4 address and routes set.");
/* New address and routes are configured now. Let's release old lease. */
r = dhcp4_remove_address_and_routes(link, /* only_marked = */ true);
if (r < 0)
return r;
r = sd_ipv4ll_stop(link->ipv4ll);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address: %m");
link_check_ready(link);
return 0;
}
static int dhcp4_retry(Link *link) {
int r;
assert(link);
r = dhcp4_remove_address_and_routes(link, /* only_marked = */ false);
if (r < 0)
return r;
r = link_request_static_nexthops(link, true);
if (r < 0)
return r;
r = link_request_static_routes(link, true);
if (r < 0)
return r;
return dhcp4_request_address_and_routes(link, false);
}
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
int r;
assert(m);
assert(link);
r = sd_netlink_message_get_errno(m);
if (r == -ENETUNREACH && !link->dhcp4_route_retrying) {
/* It seems kernel does not support that the prefix route cannot be configured with
* route table. Let's once drop the config and reconfigure them later. */
log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later");
link->dhcp4_route_failed = true;
link->manager->dhcp4_prefix_root_cannot_set_table = true;
} else if (r < 0 && r != -EEXIST) {
log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route");
link_enter_failed(link);
return 1;
}
if (link->dhcp4_messages == 0 && link->dhcp4_route_failed) {
link->dhcp4_route_failed = false;
link->dhcp4_route_retrying = true;
r = dhcp4_retry(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
r = dhcp4_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static int dhcp4_request_route(Route *in, Link *link) {
_cleanup_(route_freep) Route *route = in;
struct in_addr server;
Route *existing;
int r;
assert(route);
assert(link);
assert(link->dhcp_lease);
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server);
if (r < 0)
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
route->source = NETWORK_CONFIG_SOURCE_DHCP4;
route->provider.in = server;
route->family = AF_INET;
if (!route->protocol_set)
route->protocol = RTPROT_DHCP;
if (!route->priority_set)
route->priority = link->network->dhcp_route_metric;
if (!route->table_set)
route->table = link_get_dhcp4_route_table(link);
if (route->mtu == 0)
route->mtu = link->network->dhcp_route_mtu;
if (route_get(NULL, link, route, &existing) < 0) /* This is a new route. */
link->dhcp4_configured = false;
else
route_unmark(existing);
return link_request_route(link, TAKE_PTR(route), true, &link->dhcp4_messages,
dhcp4_route_handler, NULL);
}
static bool link_prefixroute(Link *link) {
return !link->network->dhcp_route_table_set ||
link->network->dhcp_route_table == RT_TABLE_MAIN ||
link->manager->dhcp4_prefix_root_cannot_set_table;
}
static int dhcp4_request_prefix_route(Link *link) {
_cleanup_(route_freep) Route *route = NULL;
struct in_addr address, netmask;
int r;
assert(link);
assert(link->dhcp_lease);
if (link_prefixroute(link))
/* When true, the route will be created by kernel. See dhcp4_update_address(). */
return 0;
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
route->dst.in.s_addr = address.s_addr & netmask.s_addr;
route->dst_prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
route->prefsrc.in = address;
route->scope = RT_SCOPE_LINK;
return dhcp4_request_route(TAKE_PTR(route), link);
}
static int dhcp4_request_route_to_gateway(Link *link, const struct in_addr *gw) {
_cleanup_(route_freep) Route *route = NULL;
struct in_addr address;
int r;
assert(link);
assert(link->dhcp_lease);
assert(gw);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
route->dst.in = *gw;
route->dst_prefixlen = 32;
route->prefsrc.in = address;
route->scope = RT_SCOPE_LINK;
return dhcp4_request_route(TAKE_PTR(route), link);
}
static int dhcp4_request_route_auto(
Route *in,
Link *link,
const struct in_addr *gw) {
_cleanup_(route_freep) Route *route = in;
struct in_addr address, netmask, prefix;
uint8_t prefixlen;
int r;
assert(route);
assert(link);
assert(link->dhcp_lease);
assert(gw);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
if (r < 0)
return r;
prefix.s_addr = address.s_addr & netmask.s_addr;
prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
if (in4_addr_is_localhost(&route->dst.in)) {
if (in4_addr_is_set(gw))
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, "
"ignoring gateway address "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
route->scope = RT_SCOPE_HOST;
route->gw_family = AF_UNSPEC;
route->gw = IN_ADDR_NULL;
route->prefsrc = IN_ADDR_NULL;
} else if (in4_addr_equal(&route->dst.in, &address)) {
if (in4_addr_is_set(gw))
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, "
"ignoring gateway address "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
route->scope = RT_SCOPE_HOST;
route->gw_family = AF_UNSPEC;
route->gw = IN_ADDR_NULL;
route->prefsrc.in = address;
} else if (route->dst_prefixlen >= prefixlen &&
(route->dst.in.s_addr & netmask.s_addr) == prefix.s_addr) {
if (in4_addr_is_set(gw))
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is in the assigned network "
IPV4_ADDRESS_FMT_STR"/%u, ignoring gateway address "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen,
IPV4_ADDRESS_FMT_VAL(prefix), prefixlen,
IPV4_ADDRESS_FMT_VAL(*gw));
route->scope = RT_SCOPE_LINK;
route->gw_family = AF_UNSPEC;
route->gw = IN_ADDR_NULL;
route->prefsrc.in = address;
} else {
if (in4_addr_is_null(gw)) {
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network "
IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, ignoring.",
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen,
IPV4_ADDRESS_FMT_VAL(prefix), prefixlen);
return 0;
}
r = dhcp4_request_route_to_gateway(link, gw);
if (r < 0)
return r;
route->scope = RT_SCOPE_UNIVERSE;
route->gw_family = AF_INET;
route->gw.in = *gw;
route->prefsrc.in = address;
}
return dhcp4_request_route(TAKE_PTR(route), link);
}
static int dhcp4_request_static_routes(Link *link, struct in_addr *ret_default_gw) {
_cleanup_free_ sd_dhcp_route **static_routes = NULL, **classless_routes = NULL;
size_t n_static_routes, n_classless_routes, n;
struct in_addr default_gw = {};
sd_dhcp_route **routes;
int r;
assert(link);
assert(link->dhcp_lease);
assert(ret_default_gw);
r = sd_dhcp_lease_get_static_routes(link->dhcp_lease, &static_routes);
if (r == -ENODATA)
n_static_routes = 0;
else if (r < 0)
return r;
else
n_static_routes = r;
r = sd_dhcp_lease_get_classless_routes(link->dhcp_lease, &classless_routes);
if (r == -ENODATA)
n_classless_routes = 0;
else if (r < 0)
return r;
else
n_classless_routes = r;
if (n_classless_routes == 0 && n_static_routes == 0) {
log_link_debug(link, "DHCP: No static routes received from DHCP server.");
return 0;
}
/* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
* the DHCP client MUST ignore the Static Routes option. */
if (n_classless_routes > 0 && n_static_routes > 0)
log_link_debug(link, "Classless static routes received from DHCP server: ignoring static-route option");
if (!link->network->dhcp_use_routes) {
/* Even if UseRoutes=no, try to find default gateway to make semi-static routes and
* routes to DNS or NTP servers can be configured in later steps. */
for (size_t i = 0; i < n_classless_routes; i++) {
struct in_addr dst;
uint8_t prefixlen;
r = sd_dhcp_route_get_destination(classless_routes[i], &dst);
if (r < 0)
return r;
if (in4_addr_is_set(&dst))
continue;
r = sd_dhcp_route_get_destination_prefix_length(classless_routes[i], &prefixlen);
if (r < 0)
return r;
if (prefixlen != 0)
continue;
r = sd_dhcp_route_get_gateway(classless_routes[i], ret_default_gw);
if (r < 0)
return r;
break;
}
/* Do not return 1 here, to ensure the router option can override the default gateway
* that was found. */
return 0;
}
if (n_classless_routes > 0) {
n = n_classless_routes;
routes = classless_routes;
} else if (n_static_routes > 0){
n = n_static_routes;
routes = static_routes;
} else
assert_not_reached();
for (size_t i = 0; i < n; i++) {
_cleanup_(route_freep) Route *route = NULL;
struct in_addr gw;
r = route_new(&route);
if (r < 0)
return r;
route->gw_family = AF_INET;
r = sd_dhcp_route_get_gateway(routes[i], &gw);
if (r < 0)
return r;
r = sd_dhcp_route_get_destination(routes[i], &route->dst.in);
if (r < 0)
return r;
r = sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen);
if (r < 0)
return r;
/* When classless static routes are provided, then router option will be ignored. To
* use the default gateway later in other routes, e.g., routes to dns servers, here we
* need to find the default gateway in the classless static routes. */
if (n_classless_routes > 0 &&
in4_addr_is_null(&route->dst.in) && route->dst_prefixlen == 0 &&
in4_addr_is_null(&default_gw))
default_gw = gw;
r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw);
if (r < 0)
return r;
}
*ret_default_gw = default_gw;
return n_classless_routes > 0;
}
static int dhcp4_request_gateway(Link *link, struct in_addr *gw) {
_cleanup_(route_freep) Route *route = NULL;
const struct in_addr *router;
struct in_addr address;
int r;
assert(link);
assert(link->dhcp_lease);
assert(gw);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
if (IN_SET(r, 0, -ENODATA)) {
log_link_debug(link, "DHCP: No gateway received from DHCP server.");
return 0;
}
if (r < 0)
return r;
if (in4_addr_is_null(&router[0])) {
log_link_debug(link, "DHCP: Received gateway address is null.");
return 0;
}
if (!link->network->dhcp_use_gateway) {
/* When no classless static route is provided, even if UseGateway=no, use the gateway
* address to configure semi-static routes or routes to DNS or NTP servers. Note, if
* neither UseRoutes= nor UseGateway= is disabled, use the default gateway in classless
* static routes if provided (in that case, in4_addr_is_null(gw) below is true). */
if (in4_addr_is_null(gw))
*gw = router[0];
return 0;
}
/* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host
* so that we can route no matter the netmask or existing kernel route tables. */
r = dhcp4_request_route_to_gateway(link, &router[0]);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
/* Next, add a default gateway. */
route->gw_family = AF_INET;
route->gw.in = router[0];
route->prefsrc.in = address;
r = dhcp4_request_route(TAKE_PTR(route), link);
if (r < 0)
return r;
/* When no classless static route is provided, or UseRoutes=no, then use the router address to
* configure semi-static routes and routes to DNS or NTP servers in later steps. */
*gw = router[0];
return 0;
}
static int dhcp4_request_semi_static_routes(Link *link, const struct in_addr *gw) {
Route *rt;
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
assert(gw);
if (in4_addr_is_null(gw))
return 0;
HASHMAP_FOREACH(rt, link->network->routes_by_section) {
_cleanup_(route_freep) Route *route = NULL;
if (!rt->gateway_from_dhcp_or_ra)
continue;
if (rt->gw_family != AF_INET)
continue;
r = dhcp4_request_route_to_gateway(link, gw);
if (r < 0)
return r;
r = route_dup(rt, &route);
if (r < 0)
return r;
route->gw.in = *gw;
r = dhcp4_request_route(TAKE_PTR(route), link);
if (r < 0)
return r;
}
return 0;
}
static int dhcp4_request_routes_to_servers(
Link *link,
const struct in_addr *servers,
size_t n_servers,
const struct in_addr *gw) {
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
assert(servers || n_servers == 0);
assert(gw);
for (size_t i = 0; i < n_servers; i++) {
_cleanup_(route_freep) Route *route = NULL;
if (in4_addr_is_null(&servers[i]))
continue;
r = route_new(&route);
if (r < 0)
return r;
route->dst.in = servers[i];
route->dst_prefixlen = 32;
r = dhcp4_request_route_auto(TAKE_PTR(route), link, gw);
if (r < 0)
return r;
}
return 0;
}
static int dhcp4_request_routes_to_dns(Link *link, const struct in_addr *gw) {
const struct in_addr *dns;
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
assert(gw);
if (!link->network->dhcp_use_dns ||
!link->network->dhcp_routes_to_dns)
return 0;
r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
if (IN_SET(r, 0, -ENODATA))
return 0;
if (r < 0)
return r;
return dhcp4_request_routes_to_servers(link, dns, r, gw);
}
static int dhcp4_request_routes_to_ntp(Link *link, const struct in_addr *gw) {
const struct in_addr *ntp;
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
assert(gw);
if (!link->network->dhcp_use_ntp ||
!link->network->dhcp_routes_to_ntp)
return 0;
r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
if (IN_SET(r, 0, -ENODATA))
return 0;
if (r < 0)
return r;
return dhcp4_request_routes_to_servers(link, ntp, r, gw);
}
static int dhcp4_request_routes(Link *link) {
struct in_addr gw = {};
int r;
assert(link);
assert(link->dhcp_lease);
r = dhcp4_request_prefix_route(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m");
r = dhcp4_request_static_routes(link, &gw);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m");
if (r == 0) {
/* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
* a Router option, the DHCP client MUST ignore the Router option. */
r = dhcp4_request_gateway(link, &gw);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request gateway: %m");
}
r = dhcp4_request_semi_static_routes(link, &gw);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m");
r = dhcp4_request_routes_to_dns(link, &gw);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m");
r = dhcp4_request_routes_to_ntp(link, &gw);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m");
return 0;
}
static int dhcp_reset_mtu(Link *link) {
int r;
assert(link);
if (!link->network->dhcp_use_mtu)
return 0;
r = link_request_to_set_mtu(link, link->original_mtu);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not queue request to reset MTU: %m");
return 0;
}
static int dhcp_reset_hostname(Link *link) {
const char *hostname;
int r;
assert(link);
if (!link->network->dhcp_use_hostname)
return 0;
hostname = link->network->dhcp_hostname;
if (!hostname)
(void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
if (!hostname)
return 0;
/* If a hostname was set due to the lease, then unset it now. */
r = manager_set_hostname(link->manager, NULL);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
return 0;
}
int dhcp4_lease_lost(Link *link) {
int k, r = 0;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
log_link_info(link, "DHCP lease lost");
link->dhcp4_configured = false;
if (link->network->dhcp_use_6rd &&
dhcp4_lease_has_pd_prefix(link->dhcp_lease))
dhcp4_pd_prefix_lost(link);
k = dhcp4_remove_address_and_routes(link, /* only_marked = */ false);
if (k < 0)
r = k;
k = dhcp_reset_mtu(link);
if (k < 0)
r = k;
k = dhcp_reset_hostname(link);
if (k < 0)
r = k;
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
link_dirty(link);
/* If one of the above failed. Do not request nexthops and routes. */
if (r < 0)
return r;
r = link_request_static_nexthops(link, true);
if (r < 0)
return r;
return link_request_static_routes(link, true);
}
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
int r;
assert(link);
r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCPv4 address");
if (r <= 0)
return r;
r = dhcp4_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static int dhcp4_request_address(Link *link, bool announce) {
_cleanup_(address_freep) Address *addr = NULL;
struct in_addr address, netmask, server;
unsigned prefixlen;
Address *existing;
usec_t lifetime_usec;
int r;
assert(link);
assert(link->network);
assert(link->dhcp_lease);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no address: %m");
r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server);
if (r < 0)
return log_link_debug_errno(link, r, "DHCP error: failed to get DHCP server IP address: %m");
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
uint32_t lifetime_sec;
r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime_sec);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m");
lifetime_usec = usec_add(lifetime_sec * USEC_PER_SEC, now(CLOCK_BOOTTIME));
} else
lifetime_usec = USEC_INFINITY;
prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
if (announce) {
const struct in_addr *router;
r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
if (r < 0 && r != -ENODATA)
return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
if (r > 0 && in4_addr_is_set(&router[0]))
log_struct(LOG_INFO,
LOG_LINK_INTERFACE(link),
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u, gateway "IPV4_ADDRESS_FMT_STR" acquired from "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(address),
prefixlen,
IPV4_ADDRESS_FMT_VAL(router[0]),
IPV4_ADDRESS_FMT_VAL(server)),
"ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
"PREFIXLEN=%u", prefixlen,
"GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]));
else
log_struct(LOG_INFO,
LOG_LINK_INTERFACE(link),
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u acquired from "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(address),
prefixlen,
IPV4_ADDRESS_FMT_VAL(server)),
"ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
"PREFIXLEN=%u", prefixlen);
}
r = address_new(&addr);
if (r < 0)
return log_oom();
addr->source = NETWORK_CONFIG_SOURCE_DHCP4;
addr->provider.in = server;
addr->family = AF_INET;
addr->in_addr.in.s_addr = address.s_addr;
addr->lifetime_preferred_usec = lifetime_usec;
addr->lifetime_valid_usec = lifetime_usec;
addr->prefixlen = prefixlen;
address_set_broadcast(addr, link);
SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link));
addr->route_metric = link->network->dhcp_route_metric;
addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO;
r = free_and_strdup_warn(&addr->label, link->network->dhcp_label);
if (r < 0)
return r;
if (address_get(link, addr, &existing) < 0) /* The address is new. */
link->dhcp4_configured = false;
else
address_unmark(existing);
r = link_request_address(link, TAKE_PTR(addr), true, &link->dhcp4_messages,
dhcp4_address_handler, NULL);
if (r < 0)
return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m");
return 0;
}
static int dhcp4_request_address_and_routes(Link *link, bool announce) {
int r;
assert(link);
link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP4, NULL);
link_mark_routes(link, NETWORK_CONFIG_SOURCE_DHCP4, NULL);
r = dhcp4_request_address(link, announce);
if (r < 0)
return r;
r = dhcp4_request_routes(link);
if (r < 0)
return r;
if (!link->dhcp4_configured) {
link_set_state(link, LINK_STATE_CONFIGURING);
link_check_ready(link);
}
return 0;
}
static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
_cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *old_lease = NULL;
sd_dhcp_lease *lease;
int r;
assert(link);
assert(link->network);
assert(client);
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
old_lease = TAKE_PTR(link->dhcp_lease);
link->dhcp_lease = sd_dhcp_lease_ref(lease);
link_dirty(link);
if (link->network->dhcp_use_6rd) {
if (dhcp4_lease_has_pd_prefix(link->dhcp_lease)) {
r = dhcp4_pd_prefix_acquired(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to process 6rd option: %m");
} else if (dhcp4_lease_has_pd_prefix(old_lease))
dhcp4_pd_prefix_lost(link);
}
return dhcp4_request_address_and_routes(link, false);
}
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
sd_dhcp_lease *lease;
int r;
assert(client);
assert(link);
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: No lease: %m");
sd_dhcp_lease_unref(link->dhcp_lease);
link->dhcp_lease = sd_dhcp_lease_ref(lease);
link_dirty(link);
if (link->network->dhcp_use_mtu) {