forked from adamlaska/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetworkd-link.c
More file actions
2760 lines (2127 loc) · 94.9 KB
/
networkd-link.c
File metadata and controls
2760 lines (2127 loc) · 94.9 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 <net/if.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_link.h>
#include <linux/netdevice.h>
#include <sys/socket.h>
#include <unistd.h>
#include "alloc-util.h"
#include "arphrd-util.h"
#include "batadv.h"
#include "bond.h"
#include "bridge.h"
#include "bus-util.h"
#include "device-private.h"
#include "device-util.h"
#include "dhcp-identifier.h"
#include "dhcp-lease-internal.h"
#include "env-file.h"
#include "ethtool-util.h"
#include "event-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "ipvlan.h"
#include "missing_network.h"
#include "netlink-util.h"
#include "network-internal.h"
#include "networkd-address-label.h"
#include "networkd-address.h"
#include "networkd-bridge-fdb.h"
#include "networkd-bridge-mdb.h"
#include "networkd-can.h"
#include "networkd-dhcp-prefix-delegation.h"
#include "networkd-dhcp-server.h"
#include "networkd-dhcp4.h"
#include "networkd-dhcp6.h"
#include "networkd-ipv4acd.h"
#include "networkd-ipv4ll.h"
#include "networkd-ipv6-proxy-ndp.h"
#include "networkd-link-bus.h"
#include "networkd-link.h"
#include "networkd-lldp-tx.h"
#include "networkd-manager.h"
#include "networkd-ndisc.h"
#include "networkd-neighbor.h"
#include "networkd-nexthop.h"
#include "networkd-queue.h"
#include "networkd-radv.h"
#include "networkd-route.h"
#include "networkd-routing-policy-rule.h"
#include "networkd-setlink.h"
#include "networkd-sriov.h"
#include "networkd-state-file.h"
#include "networkd-sysctl.h"
#include "set.h"
#include "socket-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "strv.h"
#include "tc.h"
#include "tmpfile-util.h"
#include "udev-util.h"
#include "util.h"
#include "vrf.h"
bool link_ipv4ll_enabled(Link *link) {
assert(link);
if (link->flags & IFF_LOOPBACK)
return false;
if (!link->network)
return false;
if (link->iftype == ARPHRD_CAN)
return false;
if (link->hw_addr.length != ETH_ALEN)
return false;
if (ether_addr_is_null(&link->hw_addr.ether))
return false;
/* ARPHRD_INFINIBAND seems to potentially support IPv4LL.
* But currently sd-ipv4ll and sd-ipv4acd only support ARPHRD_ETHER. */
if (link->iftype != ARPHRD_ETHER)
return false;
if (streq_ptr(link->kind, "vrf"))
return false;
/* L3 or L3S mode do not support ARP. */
if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S))
return false;
if (link->network->bond)
return false;
return link->network->link_local & ADDRESS_FAMILY_IPV4;
}
bool link_ipv6_enabled(Link *link) {
assert(link);
if (!socket_ipv6_is_supported())
return false;
if (link->network->bond)
return false;
if (link->iftype == ARPHRD_CAN)
return false;
/* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
if (link_ipv6ll_enabled(link))
return true;
if (network_has_static_ipv6_configurations(link->network))
return true;
return false;
}
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
assert(link);
if (!link->network) {
if (!allow_unmanaged)
return false;
return link_has_carrier(link);
}
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
return false;
if (!link->network->configure_without_carrier) {
if (link->set_flags_messages > 0)
return false;
if (!link_has_carrier(link))
return false;
}
if (link->set_link_messages > 0)
return false;
if (!link->activated)
return false;
return true;
}
void link_ntp_settings_clear(Link *link) {
link->ntp = strv_free(link->ntp);
}
void link_dns_settings_clear(Link *link) {
if (link->n_dns != UINT_MAX)
for (unsigned i = 0; i < link->n_dns; i++)
in_addr_full_free(link->dns[i]);
link->dns = mfree(link->dns);
link->n_dns = UINT_MAX;
link->search_domains = ordered_set_free(link->search_domains);
link->route_domains = ordered_set_free(link->route_domains);
link->dns_default_route = -1;
link->llmnr = _RESOLVE_SUPPORT_INVALID;
link->mdns = _RESOLVE_SUPPORT_INVALID;
link->dnssec_mode = _DNSSEC_MODE_INVALID;
link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors);
}
static void link_free_engines(Link *link) {
if (!link)
return;
link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
ndisc_flush(link);
link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
link->ndisc = sd_ndisc_unref(link->ndisc);
link->radv = sd_radv_unref(link->radv);
}
static Link *link_free(Link *link) {
assert(link);
link_ntp_settings_clear(link);
link_dns_settings_clear(link);
link->routes = set_free(link->routes);
link->nexthops = set_free(link->nexthops);
link->neighbors = set_free(link->neighbors);
link->addresses = set_free(link->addresses);
link->qdiscs = set_free(link->qdiscs);
link->tclasses = set_free(link->tclasses);
link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
link_free_engines(link);
free(link->ifname);
strv_free(link->alternative_names);
free(link->kind);
free(link->ssid);
free(link->previous_ssid);
free(link->driver);
unlink_and_free(link->lease_file);
unlink_and_free(link->lldp_file);
unlink_and_free(link->state_file);
sd_device_unref(link->sd_device);
netdev_unref(link->netdev);
hashmap_free(link->bound_to_links);
hashmap_free(link->bound_by_links);
set_free_with_destructor(link->slaves, link_unref);
network_unref(link->network);
sd_event_source_disable_unref(link->carrier_lost_timer);
return mfree(link);
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
Link *link;
assert(m);
assert(ifindex > 0);
link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
if (!link)
return -ENODEV;
if (ret)
*ret = link;
return 0;
}
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
Link *link;
assert(m);
assert(ifname);
link = hashmap_get(m->links_by_name, ifname);
if (!link)
return -ENODEV;
if (ret)
*ret = link;
return 0;
}
int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
Link *link;
assert(m);
assert(hw_addr);
link = hashmap_get(m->links_by_hw_addr, hw_addr);
if (!link)
return -ENODEV;
if (ret)
*ret = link;
return 0;
}
int link_get_master(Link *link, Link **ret) {
assert(link);
assert(link->manager);
assert(ret);
if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
return -ENODEV;
return link_get_by_index(link->manager, link->master_ifindex, ret);
}
void link_set_state(Link *link, LinkState state) {
assert(link);
if (link->state == state)
return;
log_link_debug(link, "State changed: %s -> %s",
link_state_to_string(link->state),
link_state_to_string(state));
link->state = state;
link_send_changed(link, "AdministrativeState", NULL);
link_dirty(link);
}
int link_stop_engines(Link *link, bool may_keep_dhcp) {
int r = 0, k;
assert(link);
assert(link->manager);
assert(link->manager->event);
bool keep_dhcp = may_keep_dhcp &&
link->network &&
!link->network->dhcp_send_decline && /* IPv4 ACD for the DHCPv4 address is running. */
(link->manager->restarting ||
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP));
if (!keep_dhcp) {
k = sd_dhcp_client_stop(link->dhcp_client);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m");
}
k = sd_dhcp_server_stop(link->dhcp_server);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop DHCPv4 server: %m");
k = sd_lldp_rx_stop(link->lldp_rx);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop LLDP Rx: %m");
k = sd_lldp_tx_stop(link->lldp_tx);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop LLDP Tx: %m");
k = sd_ipv4ll_stop(link->ipv4ll);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m");
k = ipv4acd_stop(link);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m");
k = sd_dhcp6_client_stop(link->dhcp6_client);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m");
k = dhcp_pd_remove(link, /* only_marked = */ false);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not remove DHCPv6 PD addresses and routes: %m");
k = sd_ndisc_stop(link->ndisc);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m");
ndisc_flush(link);
k = sd_radv_stop(link->radv);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m");
return r;
}
void link_enter_failed(Link *link) {
assert(link);
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return;
log_link_warning(link, "Failed");
link_set_state(link, LINK_STATE_FAILED);
(void) link_stop_engines(link, false);
}
void link_check_ready(Link *link) {
Address *a;
assert(link);
if (link->state == LINK_STATE_CONFIGURED)
return;
if (link->state != LINK_STATE_CONFIGURING)
return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
if (!link->network)
return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
if (!link->tc_configured)
return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
if (link->set_link_messages > 0)
return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
if (!link->activated)
return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
if (link->iftype == ARPHRD_CAN) {
/* let's shortcut things for CAN which doesn't need most of checks below. */
link_set_state(link, LINK_STATE_CONFIGURED);
return;
}
if (!link->stacked_netdevs_created)
return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
if (!link->static_addresses_configured)
return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
SET_FOREACH(a, link->addresses)
if (!address_is_ready(a)) {
_cleanup_free_ char *str = NULL;
(void) in_addr_prefix_to_string(a->family, &a->in_addr, a->prefixlen, &str);
return (void) log_link_debug(link, "%s(): address %s is not ready.", __func__, strna(str));
}
if (!link->static_address_labels_configured)
return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
if (!link->static_bridge_fdb_configured)
return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
if (!link->static_bridge_mdb_configured)
return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
if (!link->static_ipv6_proxy_ndp_configured)
return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
if (!link->static_neighbors_configured)
return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
if (!link->static_nexthops_configured)
return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
if (!link->static_routes_configured)
return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
if (!link->static_routing_policy_rules_configured)
return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
if (!link->sr_iov_configured)
return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
/* IPv6LL is assigned after the link gains its carrier. */
if (!link->network->configure_without_carrier &&
link_ipv6ll_enabled(link) &&
!in6_addr_is_set(&link->ipv6ll_address))
return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
bool has_dynamic_address = false;
SET_FOREACH(a, link->addresses) {
if (address_is_marked(a))
continue;
if (!address_exists(a))
continue;
if (IN_SET(a->source,
NETWORK_CONFIG_SOURCE_IPV4LL,
NETWORK_CONFIG_SOURCE_DHCP4,
NETWORK_CONFIG_SOURCE_DHCP6,
NETWORK_CONFIG_SOURCE_DHCP_PD,
NETWORK_CONFIG_SOURCE_NDISC)) {
has_dynamic_address = true;
break;
}
}
if ((link_ipv4ll_enabled(link) || link_dhcp4_enabled(link) || link_dhcp6_with_address_enabled(link) ||
(link_dhcp_pd_is_enabled(link) && link->network->dhcp_pd_assign)) && !has_dynamic_address)
/* When DHCP[46] or IPv4LL is enabled, at least one address is acquired by them. */
return (void) log_link_debug(link, "%s(): DHCPv4, DHCPv6, DHCP-PD or IPv4LL is enabled but no dynamic address is assigned yet.", __func__);
/* Ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
if (link_ipv4ll_enabled(link) || link_dhcp4_enabled(link) ||
link_dhcp6_enabled(link) || link_dhcp_pd_is_enabled(link) ||
(!link->network->configure_without_carrier && link_ipv6_accept_ra_enabled(link))) {
if (!link->ipv4ll_address_configured && !link->dhcp4_configured &&
!link->dhcp6_configured && !link->dhcp_pd_configured && !link->ndisc_configured)
/* When DHCP[46], NDisc, or IPv4LL is enabled, at least one protocol must be finished. */
return (void) log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__);
log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
__func__,
yes_no(link->ipv4ll_address_configured),
yes_no(link->dhcp4_configured),
yes_no(link->dhcp6_configured),
yes_no(link->dhcp_pd_configured),
yes_no(link->ndisc_configured));
}
link_set_state(link, LINK_STATE_CONFIGURED);
}
static int link_request_static_configs(Link *link) {
int r;
assert(link);
assert(link->network);
assert(link->state != _LINK_STATE_INVALID);
r = link_request_static_addresses(link);
if (r < 0)
return r;
r = link_request_static_address_labels(link);
if (r < 0)
return r;
r = link_request_static_bridge_fdb(link);
if (r < 0)
return r;
r = link_request_static_bridge_mdb(link);
if (r < 0)
return r;
r = link_request_static_ipv6_proxy_ndp_addresses(link);
if (r < 0)
return r;
r = link_request_static_neighbors(link);
if (r < 0)
return r;
r = link_request_static_nexthops(link, false);
if (r < 0)
return r;
r = link_request_static_routes(link, false);
if (r < 0)
return r;
r = link_request_static_routing_policy_rules(link);
if (r < 0)
return r;
return 0;
}
static int link_request_stacked_netdevs(Link *link) {
NetDev *netdev;
int r;
assert(link);
link->stacked_netdevs_created = false;
HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
r = link_request_stacked_netdev(link, netdev);
if (r < 0)
return r;
}
if (link->create_stacked_netdev_messages == 0) {
link->stacked_netdevs_created = true;
link_check_ready(link);
}
return 0;
}
static int link_acquire_dynamic_ipv6_conf(Link *link) {
int r;
assert(link);
r = radv_start(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
r = ndisc_start(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
r = dhcp6_start(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
return 0;
}
static int link_acquire_dynamic_ipv4_conf(Link *link) {
int r;
assert(link);
assert(link->manager);
assert(link->manager->event);
if (link->dhcp_client) {
r = dhcp4_start(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
log_link_debug(link, "Acquiring DHCPv4 lease.");
} else if (link->ipv4ll) {
r = sd_ipv4ll_start(link->ipv4ll);
if (r < 0)
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
log_link_debug(link, "Acquiring IPv4 link-local address.");
}
if (link->dhcp_server) {
r = sd_dhcp_server_start(link->dhcp_server);
if (r < 0)
return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
}
r = ipv4acd_start(link);
if (r < 0)
return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
return 0;
}
static int link_acquire_dynamic_conf(Link *link) {
int r;
assert(link);
assert(link->network);
r = link_acquire_dynamic_ipv4_conf(link);
if (r < 0)
return r;
if (in6_addr_is_set(&link->ipv6ll_address)) {
r = link_acquire_dynamic_ipv6_conf(link);
if (r < 0)
return r;
}
if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
/* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
* configured, and RADV may not be configured yet here. Only acquire subnet prefix when
* RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
* below will be called in radv_start(). */
r = dhcp_request_prefix_delegation(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
}
if (link->lldp_tx) {
r = sd_lldp_tx_start(link->lldp_tx);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
}
if (link->lldp_rx) {
r = sd_lldp_rx_start(link->lldp_rx);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
}
return 0;
}
int link_ipv6ll_gained(Link *link) {
int r;
assert(link);
log_link_info(link, "Gained IPv6LL");
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
return 0;
r = link_acquire_dynamic_ipv6_conf(link);
if (r < 0)
return r;
link_check_ready(link);
return 0;
}
int link_handle_bound_to_list(Link *link) {
bool required_up = false;
bool link_is_up = false;
Link *l;
assert(link);
/* If at least one interface in bound_to_links has carrier, then make this interface up.
* If all interfaces in bound_to_links do not, then make this interface down. */
if (hashmap_isempty(link->bound_to_links))
return 0;
if (link->flags & IFF_UP)
link_is_up = true;
HASHMAP_FOREACH(l, link->bound_to_links)
if (link_has_carrier(l)) {
required_up = true;
break;
}
if (!required_up && link_is_up)
return link_request_to_bring_up_or_down(link, /* up = */ false);
if (required_up && !link_is_up)
return link_request_to_bring_up_or_down(link, /* up = */ true);
return 0;
}
static int link_handle_bound_by_list(Link *link) {
Link *l;
int r;
assert(link);
/* Update up or down state of interfaces which depend on this interface's carrier state. */
if (hashmap_isempty(link->bound_by_links))
return 0;
HASHMAP_FOREACH(l, link->bound_by_links) {
r = link_handle_bound_to_list(l);
if (r < 0)
return r;
}
return 0;
}
static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
int r;
assert(link);
assert(carrier);
if (link == carrier)
return 0;
if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
return 0;
r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
if (r < 0)
return r;
link_dirty(link);
return 0;
}
static int link_new_bound_by_list(Link *link) {
Manager *m;
Link *carrier;
int r;
assert(link);
assert(link->manager);
m = link->manager;
HASHMAP_FOREACH(carrier, m->links_by_index) {
if (!carrier->network)
continue;
if (strv_isempty(carrier->network->bind_carrier))
continue;
if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
r = link_put_carrier(link, carrier, &link->bound_by_links);
if (r < 0)
return r;
}
}
HASHMAP_FOREACH(carrier, link->bound_by_links) {
r = link_put_carrier(carrier, link, &carrier->bound_to_links);
if (r < 0)
return r;
}
return 0;
}
static int link_new_bound_to_list(Link *link) {
Manager *m;
Link *carrier;
int r;
assert(link);
assert(link->manager);
if (!link->network)
return 0;
if (strv_isempty(link->network->bind_carrier))
return 0;
m = link->manager;
HASHMAP_FOREACH(carrier, m->links_by_index) {
if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
r = link_put_carrier(link, carrier, &link->bound_to_links);
if (r < 0)
return r;
}
}
HASHMAP_FOREACH(carrier, link->bound_to_links) {
r = link_put_carrier(carrier, link, &carrier->bound_by_links);
if (r < 0)
return r;
}
return 0;
}
static void link_free_bound_to_list(Link *link) {
bool updated = false;
Link *bound_to;
assert(link);
while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
updated = true;
if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
link_dirty(bound_to);
}
if (updated)
link_dirty(link);
}
static void link_free_bound_by_list(Link *link) {
bool updated = false;
Link *bound_by;
assert(link);
while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
updated = true;
if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
link_dirty(bound_by);
link_handle_bound_to_list(bound_by);
}
}
if (updated)
link_dirty(link);
}
static int link_append_to_master(Link *link) {
Link *master;
int r;
assert(link);
/* - The link may have no master.
* - RTM_NEWLINK message about master interface may not be received yet. */
if (link_get_master(link, &master) < 0)
return 0;
r = set_ensure_put(&master->slaves, NULL, link);
if (r <= 0)
return r;
link_ref(link);
return 0;
}
static void link_drop_from_master(Link *link) {
Link *master;
assert(link);
if (!link->manager)
return;
if (link_get_master(link, &master) < 0)
return;
link_unref(set_remove(master->slaves, link));
}
static void link_drop_requests(Link *link) {
Request *req;
assert(link);
assert(link->manager);
ORDERED_SET_FOREACH(req, link->manager->request_queue)
if (req->link == link)
request_detach(link->manager, req);
}
static Link *link_drop(Link *link) {
if (!link)
return NULL;
assert(link->manager);
link_set_state(link, LINK_STATE_LINGER);
/* Drop all references from other links and manager. Note that async netlink calls may have
* references to the link, and they will be dropped when we receive replies. */
link_drop_requests(link);
link_free_bound_to_list(link);
link_free_bound_by_list(link);
link_drop_from_master(link);
if (link->state_file)
(void) unlink(link->state_file);
link_clean(link);
STRV_FOREACH(n, link->alternative_names)
hashmap_remove(link->manager->links_by_name, *n);
hashmap_remove(link->manager->links_by_name, link->ifname);
/* bonding master and its slaves have the same hardware address. */
hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
/* The following must be called at last. */
assert_se(hashmap_remove(link->manager->links_by_index, INT_TO_PTR(link->ifindex)) == link);
return link_unref(link);
}
static int link_drop_foreign_config(Link *link) {
int k, r;
assert(link);
assert(link->manager);
/* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
* to remove loopback address or addresses used for root NFS. */
if (IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
return 0;
if (FLAGS_SET(link->flags, IFF_LOOPBACK))
return 0;
if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
return 0;
r = link_drop_foreign_routes(link);
k = link_drop_foreign_nexthops(link);
if (k < 0 && r >= 0)
r = k;
k = link_drop_foreign_addresses(link);
if (k < 0 && r >= 0)
r = k;
k = link_drop_foreign_neighbors(link);
if (k < 0 && r >= 0)
r = k;
k = manager_drop_foreign_routing_policy_rules(link->manager);
if (k < 0 && r >= 0)
r = k;
return r;
}
static int link_drop_managed_config(Link *link) {
int k, r;
assert(link);
assert(link->manager);
r = link_drop_managed_routes(link);
k = link_drop_managed_nexthops(link);
if (k < 0 && r >= 0)
r = k;
k = link_drop_managed_addresses(link);
if (k < 0 && r >= 0)
r = k;
k = link_drop_managed_neighbors(link);