]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b | 2 | |
9aa5d8ba | 3 | #include <netinet/in.h> |
f579559b | 4 | #include <linux/if.h> |
8f815e8b | 5 | #include <linux/if_arp.h> |
518cd6b5 | 6 | #include <linux/if_link.h> |
4cc7a82c | 7 | #include <unistd.h> |
f579559b | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
737f1405 YW |
10 | #include "bond.h" |
11 | #include "bridge.h" | |
1346b1f0 | 12 | #include "bus-util.h" |
27dfc982 | 13 | #include "dhcp-identifier.h" |
bd91b83e | 14 | #include "dhcp-lease-internal.h" |
686d13b9 | 15 | #include "env-file.h" |
4bb7cc82 | 16 | #include "ethtool-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
cf1d700d | 18 | #include "fileio.h" |
737f1405 | 19 | #include "ipvlan.h" |
ef118d00 | 20 | #include "missing_network.h" |
cf1d700d | 21 | #include "netlink-util.h" |
c6f7c917 | 22 | #include "network-internal.h" |
fb486c90 | 23 | #include "networkd-address-label.h" |
093e3533 | 24 | #include "networkd-address.h" |
3ddcbeea | 25 | #include "networkd-can.h" |
8fcf1d61 | 26 | #include "networkd-dhcp-server.h" |
ca5ad760 YW |
27 | #include "networkd-dhcp4.h" |
28 | #include "networkd-dhcp6.h" | |
ee446d57 | 29 | #include "networkd-fdb.h" |
ca5ad760 | 30 | #include "networkd-ipv4ll.h" |
a0e5c15d | 31 | #include "networkd-ipv6-proxy-ndp.h" |
6a1af3d4 YW |
32 | #include "networkd-link-bus.h" |
33 | #include "networkd-link.h" | |
8e1ad1ea | 34 | #include "networkd-lldp-tx.h" |
23f53b99 | 35 | #include "networkd-manager.h" |
dbf63196 | 36 | #include "networkd-mdb.h" |
1e7a0e21 | 37 | #include "networkd-ndisc.h" |
e4a71bf3 | 38 | #include "networkd-neighbor.h" |
75156ccb | 39 | #include "networkd-nexthop.h" |
518cd6b5 | 40 | #include "networkd-sriov.h" |
5e0534f1 | 41 | #include "networkd-sysctl.h" |
7465dd22 | 42 | #include "networkd-radv.h" |
bce67bbe | 43 | #include "networkd-routing-policy-rule.h" |
8d968fdd | 44 | #include "networkd-wifi.h" |
cf1d700d TG |
45 | #include "set.h" |
46 | #include "socket-util.h" | |
bf331d87 | 47 | #include "stat-util.h" |
15a5e950 | 48 | #include "stdio-util.h" |
8b43440b | 49 | #include "string-table.h" |
51517f9e | 50 | #include "strv.h" |
62e021a9 | 51 | #include "sysctl-util.h" |
34658df2 | 52 | #include "tc.h" |
e4de7287 | 53 | #include "tmpfile-util.h" |
299ad32d | 54 | #include "udev-util.h" |
cf1d700d | 55 | #include "util.h" |
737f1405 | 56 | #include "vrf.h" |
fc2f9534 | 57 | |
2d792895 | 58 | bool link_ipv4ll_enabled(Link *link, AddressFamily mask) { |
b9d74c40 | 59 | assert(link); |
910feb78 | 60 | assert((mask & ~(ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) == 0); |
b9d74c40 | 61 | |
78c958f8 TG |
62 | if (link->flags & IFF_LOOPBACK) |
63 | return false; | |
64 | ||
65 | if (!link->network) | |
66 | return false; | |
67 | ||
500b96eb | 68 | if (link->iftype == ARPHRD_CAN) |
c6ac3729 YW |
69 | return false; |
70 | ||
98d20a17 | 71 | if (STRPTR_IN_SET(link->kind, |
72 | "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti", | |
b0486c73 | 73 | "vti6", "nlmon", "xfrm", "bareudp")) |
a8f5bba6 JD |
74 | return false; |
75 | ||
f410d463 YW |
76 | /* L3 or L3S mode do not support ARP. */ |
77 | if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S)) | |
78 | return false; | |
79 | ||
f2bfcdb9 YW |
80 | if (link->network->bond) |
81 | return false; | |
82 | ||
910feb78 | 83 | return link->network->link_local & mask; |
8bc17bb3 SS |
84 | } |
85 | ||
f8f2f880 | 86 | bool link_ipv6ll_enabled(Link *link) { |
b9d74c40 LP |
87 | assert(link); |
88 | ||
fa709992 LP |
89 | if (!socket_ipv6_is_supported()) |
90 | return false; | |
91 | ||
d0d6a4cd TG |
92 | if (link->flags & IFF_LOOPBACK) |
93 | return false; | |
94 | ||
95 | if (!link->network) | |
96 | return false; | |
97 | ||
500b96eb | 98 | if (link->iftype == ARPHRD_CAN) |
c6ac3729 YW |
99 | return false; |
100 | ||
101 | if (STRPTR_IN_SET(link->kind, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon")) | |
a8f5bba6 JD |
102 | return false; |
103 | ||
f2bfcdb9 YW |
104 | if (link->network->bond) |
105 | return false; | |
106 | ||
e0ee46f2 | 107 | return link->network->link_local & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
108 | } |
109 | ||
5e0534f1 | 110 | bool link_ipv6_enabled(Link *link) { |
439689c6 SS |
111 | assert(link); |
112 | ||
113 | if (!socket_ipv6_is_supported()) | |
114 | return false; | |
115 | ||
b102cdca | 116 | if (link->network->bond) |
2b00a4e0 TY |
117 | return false; |
118 | ||
500b96eb | 119 | if (link->iftype == ARPHRD_CAN) |
af9ba57a YW |
120 | return false; |
121 | ||
4cef7fe3 | 122 | /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */ |
adfeee49 YW |
123 | if (link_ipv6ll_enabled(link)) |
124 | return true; | |
125 | ||
126 | if (network_has_static_ipv6_configurations(link->network)) | |
127 | return true; | |
128 | ||
129 | return false; | |
439689c6 SS |
130 | } |
131 | ||
34bf3c00 YW |
132 | static bool link_is_enslaved(Link *link) { |
133 | if (link->flags & IFF_SLAVE) | |
134 | /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */ | |
135 | return true; | |
136 | ||
34bf3c00 YW |
137 | if (!link->network) |
138 | return false; | |
139 | ||
bb262ef0 | 140 | if (link->master_ifindex > 0 && link->network->bridge) |
34bf3c00 YW |
141 | return true; |
142 | ||
bb262ef0 YW |
143 | /* TODO: add conditions for other netdevs. */ |
144 | ||
34bf3c00 YW |
145 | return false; |
146 | } | |
147 | ||
45e11abf YW |
148 | static void link_update_master_operstate(Link *link, NetDev *netdev) { |
149 | Link *master; | |
150 | ||
151 | if (!netdev) | |
152 | return; | |
153 | ||
7936917e YW |
154 | if (netdev->ifindex <= 0) |
155 | return; | |
156 | ||
45e11abf YW |
157 | if (link_get(link->manager, netdev->ifindex, &master) < 0) |
158 | return; | |
159 | ||
160 | link_update_operstate(master, true); | |
161 | } | |
162 | ||
163 | void link_update_operstate(Link *link, bool also_update_master) { | |
84de38c5 | 164 | LinkOperationalState operstate; |
1678fbb3 YW |
165 | LinkCarrierState carrier_state; |
166 | LinkAddressState address_state; | |
35c5a9ca | 167 | _cleanup_strv_free_ char **p = NULL; |
1678fbb3 | 168 | uint8_t scope = RT_SCOPE_NOWHERE; |
35c5a9ca | 169 | bool changed = false; |
1678fbb3 | 170 | Address *address; |
14153d1b | 171 | |
84de38c5 TG |
172 | assert(link); |
173 | ||
174 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
1678fbb3 | 175 | carrier_state = LINK_CARRIER_STATE_DORMANT; |
84de38c5 | 176 | else if (link_has_carrier(link)) { |
1678fbb3 YW |
177 | if (link_is_enslaved(link)) |
178 | carrier_state = LINK_CARRIER_STATE_ENSLAVED; | |
84de38c5 | 179 | else |
1678fbb3 | 180 | carrier_state = LINK_CARRIER_STATE_CARRIER; |
84de38c5 | 181 | } else if (link->flags & IFF_UP) |
1678fbb3 | 182 | carrier_state = LINK_CARRIER_STATE_NO_CARRIER; |
84de38c5 | 183 | else |
1678fbb3 | 184 | carrier_state = LINK_CARRIER_STATE_OFF; |
84de38c5 | 185 | |
1678fbb3 | 186 | if (carrier_state >= LINK_CARRIER_STATE_CARRIER) { |
959f65d3 YW |
187 | Link *slave; |
188 | ||
90e74a66 | 189 | SET_FOREACH(slave, link->slaves) { |
959f65d3 | 190 | link_update_operstate(slave, false); |
84de38c5 | 191 | |
1678fbb3 YW |
192 | if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER) |
193 | carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER; | |
84de38c5 | 194 | } |
959f65d3 | 195 | } |
84de38c5 | 196 | |
90e74a66 | 197 | SET_FOREACH(address, link->addresses) { |
1678fbb3 YW |
198 | if (!address_is_ready(address)) |
199 | continue; | |
84de38c5 | 200 | |
1678fbb3 YW |
201 | if (address->scope < scope) |
202 | scope = address->scope; | |
203 | } | |
84de38c5 | 204 | |
1678fbb3 | 205 | /* for operstate we also take foreign addresses into account */ |
90e74a66 | 206 | SET_FOREACH(address, link->addresses_foreign) { |
1678fbb3 YW |
207 | if (!address_is_ready(address)) |
208 | continue; | |
209 | ||
210 | if (address->scope < scope) | |
211 | scope = address->scope; | |
212 | } | |
213 | ||
214 | if (scope < RT_SCOPE_SITE) | |
215 | /* universally accessible addresses found */ | |
216 | address_state = LINK_ADDRESS_STATE_ROUTABLE; | |
217 | else if (scope < RT_SCOPE_HOST) | |
218 | /* only link or site local addresses found */ | |
219 | address_state = LINK_ADDRESS_STATE_DEGRADED; | |
84de38c5 | 220 | else |
1678fbb3 YW |
221 | /* no useful addresses found */ |
222 | address_state = LINK_ADDRESS_STATE_OFF; | |
223 | ||
224 | /* Mapping of address and carrier state vs operational state | |
225 | * carrier state | |
226 | * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved | |
227 | * ------------------------------------------------------------------------------ | |
228 | * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved | |
229 | * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved | |
230 | * routable | off | no-carrier | dormant | degraded-carrier | routable | routable | |
231 | */ | |
84de38c5 | 232 | |
1678fbb3 YW |
233 | if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF) |
234 | operstate = (LinkOperationalState) carrier_state; | |
235 | else if (address_state == LINK_ADDRESS_STATE_ROUTABLE) | |
236 | operstate = LINK_OPERSTATE_ROUTABLE; | |
237 | else if (carrier_state == LINK_CARRIER_STATE_CARRIER) | |
238 | operstate = LINK_OPERSTATE_DEGRADED; | |
239 | else | |
14153d1b YW |
240 | operstate = LINK_OPERSTATE_ENSLAVED; |
241 | ||
35c5a9ca YW |
242 | if (link->carrier_state != carrier_state) { |
243 | link->carrier_state = carrier_state; | |
244 | changed = true; | |
245 | if (strv_extend(&p, "CarrierState") < 0) | |
246 | log_oom(); | |
247 | } | |
959f65d3 | 248 | |
35c5a9ca YW |
249 | if (link->address_state != address_state) { |
250 | link->address_state = address_state; | |
251 | changed = true; | |
252 | if (strv_extend(&p, "AddressState") < 0) | |
253 | log_oom(); | |
959f65d3 YW |
254 | } |
255 | ||
84de38c5 TG |
256 | if (link->operstate != operstate) { |
257 | link->operstate = operstate; | |
35c5a9ca YW |
258 | changed = true; |
259 | if (strv_extend(&p, "OperationalState") < 0) | |
260 | log_oom(); | |
84de38c5 | 261 | } |
959f65d3 | 262 | |
35c5a9ca YW |
263 | if (p) |
264 | link_send_changed_strv(link, p); | |
265 | if (changed) | |
266 | link_dirty(link); | |
267 | ||
45e11abf YW |
268 | if (also_update_master && link->network) { |
269 | link_update_master_operstate(link, link->network->bond); | |
270 | link_update_master_operstate(link, link->network->bridge); | |
959f65d3 | 271 | } |
84de38c5 TG |
272 | } |
273 | ||
51d18171 TG |
274 | #define FLAG_STRING(string, flag, old, new) \ |
275 | (((old ^ new) & flag) \ | |
276 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
277 | : "") | |
278 | ||
bb262ef0 | 279 | static int link_update_flags(Link *link, sd_netlink_message *m, bool force_update_operstate) { |
51d18171 TG |
280 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; |
281 | uint8_t operstate; | |
282 | int r; | |
283 | ||
284 | assert(link); | |
285 | ||
286 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
287 | if (r < 0) |
288 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 | 289 | |
1c4baffc | 290 | r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate); |
51d18171 TG |
291 | if (r < 0) |
292 | /* if we got a message without operstate, take it to mean | |
293 | the state was unchanged */ | |
294 | operstate = link->kernel_operstate; | |
295 | ||
bb262ef0 | 296 | if (!force_update_operstate && (link->flags == flags) && (link->kernel_operstate == operstate)) |
51d18171 TG |
297 | return 0; |
298 | ||
299 | if (link->flags != flags) { | |
6a7a4e4d | 300 | log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", |
51d18171 TG |
301 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
302 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
303 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
304 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
305 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
306 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
307 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
308 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
309 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
310 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
311 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
312 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
313 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
314 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
315 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
316 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
317 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
318 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
319 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
320 | ||
321 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
322 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
323 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
324 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
325 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
326 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
327 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
328 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
329 | ||
330 | /* link flags are currently at most 18 bits, let's align to | |
331 | * printing 20 */ | |
332 | if (unknown_flags_added) | |
79008bdd | 333 | log_link_debug(link, |
6a7a4e4d | 334 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
335 | unknown_flags_added); |
336 | ||
337 | if (unknown_flags_removed) | |
79008bdd | 338 | log_link_debug(link, |
6a7a4e4d | 339 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
340 | unknown_flags_removed); |
341 | } | |
342 | ||
343 | link->flags = flags; | |
344 | link->kernel_operstate = operstate; | |
345 | ||
959f65d3 | 346 | link_update_operstate(link, true); |
51d18171 TG |
347 | |
348 | return 0; | |
349 | } | |
350 | ||
1c4baffc | 351 | static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { |
8e766630 | 352 | _cleanup_(link_unrefp) Link *link = NULL; |
6cad256d | 353 | const char *ifname, *kind = NULL; |
b710e6b6 | 354 | unsigned short iftype; |
572b21d9 YW |
355 | int r, ifindex; |
356 | uint16_t type; | |
f579559b | 357 | |
0c2f9b84 | 358 | assert(manager); |
505f8da7 | 359 | assert(message); |
f579559b TG |
360 | assert(ret); |
361 | ||
6cad256d TJ |
362 | /* check for link kind */ |
363 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); | |
364 | if (r == 0) { | |
db2f8a2e | 365 | (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind); |
6cad256d TJ |
366 | r = sd_netlink_message_exit_container(message); |
367 | if (r < 0) | |
368 | return r; | |
369 | } | |
370 | ||
1c4baffc | 371 | r = sd_netlink_message_get_type(message, &type); |
505f8da7 TG |
372 | if (r < 0) |
373 | return r; | |
374 | else if (type != RTM_NEWLINK) | |
375 | return -EINVAL; | |
376 | ||
377 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
378 | if (r < 0) | |
379 | return r; | |
380 | else if (ifindex <= 0) | |
381 | return -EINVAL; | |
382 | ||
b710e6b6 LP |
383 | r = sd_rtnl_message_link_get_type(message, &iftype); |
384 | if (r < 0) | |
385 | return r; | |
386 | ||
1c4baffc | 387 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname); |
505f8da7 TG |
388 | if (r < 0) |
389 | return r; | |
390 | ||
17f9c355 | 391 | link = new(Link, 1); |
f579559b TG |
392 | if (!link) |
393 | return -ENOMEM; | |
394 | ||
17f9c355 YW |
395 | *link = (Link) { |
396 | .n_ref = 1, | |
397 | .manager = manager, | |
398 | .state = LINK_STATE_PENDING, | |
17f9c355 YW |
399 | .ifindex = ifindex, |
400 | .iftype = iftype, | |
15761549 YW |
401 | |
402 | .n_dns = (unsigned) -1, | |
403 | .dns_default_route = -1, | |
404 | .llmnr = _RESOLVE_SUPPORT_INVALID, | |
405 | .mdns = _RESOLVE_SUPPORT_INVALID, | |
406 | .dnssec_mode = _DNSSEC_MODE_INVALID, | |
407 | .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID, | |
17f9c355 YW |
408 | }; |
409 | ||
505f8da7 TG |
410 | link->ifname = strdup(ifname); |
411 | if (!link->ifname) | |
412 | return -ENOMEM; | |
f579559b | 413 | |
6cad256d TJ |
414 | if (kind) { |
415 | link->kind = strdup(kind); | |
416 | if (!link->kind) | |
417 | return -ENOMEM; | |
418 | } | |
419 | ||
cbff7170 TJ |
420 | r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex); |
421 | if (r < 0) | |
422 | log_link_debug_errno(link, r, "New device has no master, continuing without"); | |
423 | ||
b8162cd2 | 424 | r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &link->hw_addr); |
512922f8 | 425 | if (r < 0) |
b8162cd2 | 426 | log_link_debug_errno(link, r, "Hardware address not found for new device, continuing without"); |
512922f8 | 427 | |
c643bda5 | 428 | r = ethtool_get_permanent_macaddr(&manager->ethtool_fd, link->ifname, &link->permanent_mac); |
4bb7cc82 YW |
429 | if (r < 0) |
430 | log_link_debug_errno(link, r, "Permanent MAC address not found for new device, continuing without: %m"); | |
431 | ||
c643bda5 YW |
432 | r = ethtool_get_driver(&manager->ethtool_fd, link->ifname, &link->driver); |
433 | if (r < 0) | |
434 | log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m"); | |
435 | ||
572b21d9 YW |
436 | r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &link->alternative_names); |
437 | if (r < 0 && r != -ENODATA) | |
438 | return r; | |
439 | ||
34437b4f | 440 | if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0) |
315db1a8 | 441 | return -ENOMEM; |
fe8db0c5 | 442 | |
34437b4f | 443 | if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0) |
68a8723c TG |
444 | return -ENOMEM; |
445 | ||
34437b4f | 446 | if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0) |
49699bac SS |
447 | return -ENOMEM; |
448 | ||
d5099efc | 449 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
450 | if (r < 0) |
451 | return r; | |
452 | ||
453 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
454 | if (r < 0) |
455 | return r; | |
456 | ||
bb262ef0 | 457 | r = link_update_flags(link, message, false); |
51d18171 TG |
458 | if (r < 0) |
459 | return r; | |
460 | ||
1cc6c93a | 461 | *ret = TAKE_PTR(link); |
f579559b TG |
462 | |
463 | return 0; | |
464 | } | |
465 | ||
15761549 YW |
466 | void link_ntp_settings_clear(Link *link) { |
467 | link->ntp = strv_free(link->ntp); | |
468 | } | |
469 | ||
470 | void link_dns_settings_clear(Link *link) { | |
e77bd3fd YW |
471 | if (link->n_dns != (unsigned) -1) |
472 | for (unsigned i = 0; i < link->n_dns; i++) | |
473 | in_addr_full_free(link->dns[i]); | |
15761549 YW |
474 | link->dns = mfree(link->dns); |
475 | link->n_dns = (unsigned) -1; | |
476 | ||
477 | link->search_domains = ordered_set_free_free(link->search_domains); | |
478 | link->route_domains = ordered_set_free_free(link->route_domains); | |
479 | ||
480 | link->dns_default_route = -1; | |
481 | link->llmnr = _RESOLVE_SUPPORT_INVALID; | |
482 | link->mdns = _RESOLVE_SUPPORT_INVALID; | |
483 | link->dnssec_mode = _DNSSEC_MODE_INVALID; | |
484 | link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID; | |
485 | ||
486 | link->dnssec_negative_trust_anchors = set_free_free(link->dnssec_negative_trust_anchors); | |
487 | } | |
488 | ||
1a6bb31f YW |
489 | static void link_free_engines(Link *link) { |
490 | if (!link) | |
491 | return; | |
492 | ||
493 | link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server); | |
494 | link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client); | |
495 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); | |
1c494872 | 496 | link->dhcp_acd = sd_ipv4acd_unref(link->dhcp_acd); |
1a6bb31f YW |
497 | |
498 | link->lldp = sd_lldp_unref(link->lldp); | |
1c494872 | 499 | link_lldp_emit_stop(link); |
1a6bb31f YW |
500 | |
501 | ndisc_flush(link); | |
502 | ||
503 | link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll); | |
504 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
1633c457 | 505 | link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease); |
1a6bb31f YW |
506 | link->ndisc = sd_ndisc_unref(link->ndisc); |
507 | link->radv = sd_radv_unref(link->radv); | |
7abe175c YW |
508 | |
509 | ipv4_dad_unref(link); | |
1a6bb31f YW |
510 | } |
511 | ||
8301aa0b | 512 | static Link *link_free(Link *link) { |
8301aa0b | 513 | assert(link); |
f579559b | 514 | |
15761549 YW |
515 | link_ntp_settings_clear(link); |
516 | link_dns_settings_clear(link); | |
517 | ||
8eec0b9d YW |
518 | link->routes = set_free(link->routes); |
519 | link->routes_foreign = set_free(link->routes_foreign); | |
6e537f62 YW |
520 | link->dhcp_routes = set_free(link->dhcp_routes); |
521 | link->dhcp_routes_old = set_free(link->dhcp_routes_old); | |
1633c457 YW |
522 | link->dhcp6_routes = set_free(link->dhcp6_routes); |
523 | link->dhcp6_routes_old = set_free(link->dhcp6_routes_old); | |
524 | link->dhcp6_pd_routes = set_free(link->dhcp6_pd_routes); | |
525 | link->dhcp6_pd_routes_old = set_free(link->dhcp6_pd_routes_old); | |
69203fba | 526 | link->ndisc_routes = set_free(link->ndisc_routes); |
adda1ed9 | 527 | |
8eec0b9d YW |
528 | link->nexthops = set_free(link->nexthops); |
529 | link->nexthops_foreign = set_free(link->nexthops_foreign); | |
c16c7808 | 530 | |
8eec0b9d YW |
531 | link->neighbors = set_free(link->neighbors); |
532 | link->neighbors_foreign = set_free(link->neighbors_foreign); | |
d1bdafd2 | 533 | |
8eec0b9d YW |
534 | link->addresses = set_free(link->addresses); |
535 | link->addresses_foreign = set_free(link->addresses_foreign); | |
aa651e88 | 536 | link->pool_addresses = set_free(link->pool_addresses); |
e5526518 | 537 | link->static_addresses = set_free(link->static_addresses); |
1633c457 YW |
538 | link->dhcp6_addresses = set_free(link->dhcp6_addresses); |
539 | link->dhcp6_addresses_old = set_free(link->dhcp6_addresses_old); | |
540 | link->dhcp6_pd_addresses = set_free(link->dhcp6_pd_addresses); | |
541 | link->dhcp6_pd_addresses_old = set_free(link->dhcp6_pd_addresses_old); | |
69203fba | 542 | link->ndisc_addresses = set_free(link->ndisc_addresses); |
adda1ed9 | 543 | |
1a6bb31f | 544 | link_free_engines(link); |
68a8723c | 545 | free(link->lease_file); |
49699bac SS |
546 | free(link->lldp_file); |
547 | ||
c166a070 | 548 | free(link->ifname); |
572b21d9 | 549 | strv_free(link->alternative_names); |
ceac4078 | 550 | free(link->kind); |
8d968fdd | 551 | free(link->ssid); |
c643bda5 | 552 | free(link->driver); |
6cad256d | 553 | |
db2f8a2e | 554 | (void) unlink(link->state_file); |
fe8db0c5 | 555 | free(link->state_file); |
c166a070 | 556 | |
51517f9e | 557 | sd_device_unref(link->sd_device); |
b5db00e5 | 558 | |
0d4ad91d | 559 | hashmap_free(link->bound_to_links); |
0d4ad91d AR |
560 | hashmap_free(link->bound_by_links); |
561 | ||
5f707e12 | 562 | set_free_with_destructor(link->slaves, link_unref); |
033295c1 | 563 | |
c9c908a6 YW |
564 | network_unref(link->network); |
565 | ||
8301aa0b | 566 | return mfree(link); |
14b746f7 TG |
567 | } |
568 | ||
8301aa0b | 569 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free); |
14b746f7 | 570 | |
11a7f229 TG |
571 | int link_get(Manager *m, int ifindex, Link **ret) { |
572 | Link *link; | |
11a7f229 TG |
573 | |
574 | assert(m); | |
e856ed00 | 575 | assert(ifindex > 0); |
11a7f229 TG |
576 | assert(ret); |
577 | ||
ae06ab10 | 578 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
579 | if (!link) |
580 | return -ENODEV; | |
581 | ||
582 | *ret = link; | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
af9ba57a | 587 | void link_set_state(Link *link, LinkState state) { |
e331e246 TG |
588 | assert(link); |
589 | ||
590 | if (link->state == state) | |
591 | return; | |
592 | ||
0beb9542 YW |
593 | log_link_debug(link, "State changed: %s -> %s", |
594 | link_state_to_string(link->state), | |
595 | link_state_to_string(state)); | |
596 | ||
e331e246 TG |
597 | link->state = state; |
598 | ||
599 | link_send_changed(link, "AdministrativeState", NULL); | |
e331e246 TG |
600 | } |
601 | ||
57bd6899 TG |
602 | static void link_enter_unmanaged(Link *link) { |
603 | assert(link); | |
604 | ||
e331e246 | 605 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 | 606 | |
84de38c5 | 607 | link_dirty(link); |
57bd6899 TG |
608 | } |
609 | ||
2a99eed0 | 610 | int link_stop_engines(Link *link, bool may_keep_dhcp) { |
111bb8f9 TG |
611 | int r = 0, k; |
612 | ||
613 | assert(link); | |
614 | assert(link->manager); | |
615 | assert(link->manager->event); | |
616 | ||
80060352 ZJS |
617 | bool keep_dhcp = may_keep_dhcp && |
618 | link->network && | |
619 | (link->manager->restarting || | |
620 | FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP_ON_STOP)); | |
621 | ||
84add3cd | 622 | if (!keep_dhcp) { |
111bb8f9 | 623 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d | 624 | if (k < 0) |
36c7d709 | 625 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m"); |
111bb8f9 TG |
626 | } |
627 | ||
84add3cd YW |
628 | k = sd_ipv4acd_stop(link->dhcp_acd); |
629 | if (k < 0) | |
630 | r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client for DHCPv4: %m"); | |
a7df5cae | 631 | |
2a99eed0 YW |
632 | k = sd_dhcp_server_stop(link->dhcp_server); |
633 | if (k < 0) | |
634 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 server: %m"); | |
635 | ||
9cc65242 YW |
636 | k = sd_lldp_stop(link->lldp); |
637 | if (k < 0) | |
638 | r = log_link_warning_errno(link, k, "Could not stop LLDP: %m"); | |
639 | ||
84add3cd YW |
640 | k = sd_ipv4ll_stop(link->ipv4ll); |
641 | if (k < 0) | |
642 | r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m"); | |
dd43110f | 643 | |
490ccbd5 | 644 | k = ipv4_dad_stop(link); |
2488e4d9 YW |
645 | if (k < 0) |
646 | r = log_link_warning_errno(link, k, "Could not stop IPv4 ACD client: %m"); | |
051e77ca | 647 | |
84add3cd YW |
648 | k = sd_dhcp6_client_stop(link->dhcp6_client); |
649 | if (k < 0) | |
650 | r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m"); | |
4138fb2c | 651 | |
2ffd6d73 YW |
652 | k = dhcp6_pd_remove(link); |
653 | if (k < 0) | |
654 | r = log_link_warning_errno(link, k, "Could not remove DHCPv6 PD addresses and routes: %m"); | |
1633c457 | 655 | |
84add3cd YW |
656 | k = sd_ndisc_stop(link->ndisc); |
657 | if (k < 0) | |
658 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m"); | |
4138fb2c | 659 | |
84add3cd YW |
660 | k = sd_radv_stop(link->radv); |
661 | if (k < 0) | |
662 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m"); | |
7465dd22 | 663 | |
7272b25e | 664 | link_lldp_emit_stop(link); |
111bb8f9 TG |
665 | return r; |
666 | } | |
667 | ||
b22d8a00 | 668 | void link_enter_failed(Link *link) { |
ef1ba606 | 669 | assert(link); |
f882c247 | 670 | |
370e9930 | 671 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
672 | return; |
673 | ||
6a7a4e4d | 674 | log_link_warning(link, "Failed"); |
449f7554 | 675 | |
e331e246 | 676 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 677 | |
2a99eed0 | 678 | (void) link_stop_engines(link, false); |
111bb8f9 | 679 | |
84de38c5 | 680 | link_dirty(link); |
f882c247 TG |
681 | } |
682 | ||
7033af49 YW |
683 | static int link_join_netdevs_after_configured(Link *link) { |
684 | NetDev *netdev; | |
7033af49 YW |
685 | int r; |
686 | ||
90e74a66 | 687 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) { |
7033af49 YW |
688 | if (netdev->ifindex > 0) |
689 | /* Assume already enslaved. */ | |
690 | continue; | |
691 | ||
692 | if (netdev_get_create_type(netdev) != NETDEV_CREATE_AFTER_CONFIGURED) | |
693 | continue; | |
694 | ||
695 | log_struct(LOG_DEBUG, | |
696 | LOG_LINK_INTERFACE(link), | |
697 | LOG_NETDEV_INTERFACE(netdev), | |
698 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); | |
699 | ||
700 | r = netdev_join(netdev, link, NULL); | |
701 | if (r < 0) | |
702 | return log_struct_errno(LOG_WARNING, r, | |
703 | LOG_LINK_INTERFACE(link), | |
704 | LOG_NETDEV_INTERFACE(netdev), | |
705 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); | |
706 | } | |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
e3a7b048 | 711 | static void link_enter_configured(Link *link) { |
dd43110f TG |
712 | assert(link); |
713 | assert(link->network); | |
e3a7b048 | 714 | |
289e6774 | 715 | if (link->state != LINK_STATE_CONFIGURING) |
e3a7b048 | 716 | return; |
dd43110f | 717 | |
e331e246 | 718 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f | 719 | |
7033af49 YW |
720 | (void) link_join_netdevs_after_configured(link); |
721 | ||
84de38c5 | 722 | link_dirty(link); |
dd43110f TG |
723 | } |
724 | ||
6accfd31 DA |
725 | void link_check_ready(Link *link) { |
726 | Address *a; | |
6accfd31 DA |
727 | |
728 | assert(link); | |
729 | ||
5f58af25 YW |
730 | if (link->state == LINK_STATE_CONFIGURED) |
731 | return; | |
732 | ||
733 | if (link->state != LINK_STATE_CONFIGURING) { | |
734 | log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state)); | |
6accfd31 | 735 | return; |
39373cb9 | 736 | } |
6accfd31 DA |
737 | |
738 | if (!link->network) | |
739 | return; | |
740 | ||
39373cb9 YW |
741 | if (!link->addresses_configured) { |
742 | log_link_debug(link, "%s(): static addresses are not configured.", __func__); | |
6accfd31 | 743 | return; |
39373cb9 | 744 | } |
6accfd31 | 745 | |
39373cb9 YW |
746 | if (!link->neighbors_configured) { |
747 | log_link_debug(link, "%s(): static neighbors are not configured.", __func__); | |
6accfd31 | 748 | return; |
39373cb9 | 749 | } |
6accfd31 | 750 | |
90e74a66 | 751 | SET_FOREACH(a, link->addresses) |
39373cb9 YW |
752 | if (!address_is_ready(a)) { |
753 | _cleanup_free_ char *str = NULL; | |
754 | ||
755 | (void) in_addr_to_string(a->family, &a->in_addr, &str); | |
756 | log_link_debug(link, "%s(): an address %s/%d is not ready.", __func__, strnull(str), a->prefixlen); | |
6aa5773b | 757 | return; |
39373cb9 | 758 | } |
6aa5773b | 759 | |
39373cb9 YW |
760 | if (!link->static_routes_configured) { |
761 | log_link_debug(link, "%s(): static routes are not configured.", __func__); | |
6accfd31 | 762 | return; |
39373cb9 | 763 | } |
6accfd31 | 764 | |
39373cb9 YW |
765 | if (!link->static_nexthops_configured) { |
766 | log_link_debug(link, "%s(): static nexthops are not configured.", __func__); | |
c16c7808 | 767 | return; |
39373cb9 | 768 | } |
c16c7808 | 769 | |
39373cb9 YW |
770 | if (!link->routing_policy_rules_configured) { |
771 | log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__); | |
6accfd31 | 772 | return; |
39373cb9 | 773 | } |
6accfd31 | 774 | |
39373cb9 YW |
775 | if (!link->tc_configured) { |
776 | log_link_debug(link, "%s(): traffic controls are not configured.", __func__); | |
4ecdcb07 | 777 | return; |
39373cb9 | 778 | } |
4ecdcb07 | 779 | |
39373cb9 YW |
780 | if (!link->sr_iov_configured) { |
781 | log_link_debug(link, "%s(): SR-IOV is not configured.", __func__); | |
518cd6b5 | 782 | return; |
39373cb9 | 783 | } |
518cd6b5 | 784 | |
1f241589 YW |
785 | if (!link->bridge_mdb_configured) { |
786 | log_link_debug(link, "%s(): Bridge MDB is not configured.", __func__); | |
787 | return; | |
788 | } | |
789 | ||
463797c1 | 790 | if (link_has_carrier(link) || !link->network->configure_without_carrier) { |
50550722 YW |
791 | bool has_ndisc_address = false; |
792 | NDiscAddress *n; | |
6accfd31 | 793 | |
1c09d84e | 794 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4) && !link->ipv4ll_address_configured) { |
39373cb9 | 795 | log_link_debug(link, "%s(): IPv4LL is not configured.", __func__); |
463797c1 | 796 | return; |
39373cb9 | 797 | } |
6accfd31 | 798 | |
463797c1 | 799 | if (link_ipv6ll_enabled(link) && |
39373cb9 YW |
800 | in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) { |
801 | log_link_debug(link, "%s(): IPv6LL is not configured.", __func__); | |
463797c1 | 802 | return; |
39373cb9 | 803 | } |
6accfd31 | 804 | |
90e74a66 | 805 | SET_FOREACH(n, link->ndisc_addresses) |
50550722 YW |
806 | if (!n->marked) { |
807 | has_ndisc_address = true; | |
808 | break; | |
809 | } | |
810 | ||
15797d6a | 811 | if ((link_dhcp4_enabled(link) || link_dhcp6_enabled(link)) && |
50550722 | 812 | !link->dhcp_address && set_isempty(link->dhcp6_addresses) && !has_ndisc_address && |
15797d6a YW |
813 | !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address_configured)) { |
814 | log_link_debug(link, "%s(): DHCP4 or DHCP6 is enabled but no dynamic address is assigned yet.", __func__); | |
659ad3a0 YW |
815 | return; |
816 | } | |
817 | ||
1633c457 | 818 | if (link_dhcp4_enabled(link) || link_dhcp6_enabled(link) || link_dhcp6_pd_is_enabled(link) || link_ipv6_accept_ra_enabled(link)) { |
39373cb9 YW |
819 | if (!link->dhcp4_configured && |
820 | !(link->dhcp6_address_configured && link->dhcp6_route_configured) && | |
821 | !(link->dhcp6_pd_address_configured && link->dhcp6_pd_route_configured) && | |
d98c546d | 822 | !(link->ndisc_addresses_configured && link->ndisc_routes_configured) && |
1c09d84e | 823 | !(link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4) && link->ipv4ll_address_configured)) { |
39373cb9 YW |
824 | /* When DHCP or RA is enabled, at least one protocol must provide an address, or |
825 | * an IPv4ll fallback address must be configured. */ | |
826 | log_link_debug(link, "%s(): dynamic addresses or routes are not configured.", __func__); | |
827 | return; | |
828 | } | |
829 | ||
d98c546d | 830 | log_link_debug(link, "%s(): dhcp4:%s dhcp6_addresses:%s dhcp_routes:%s dhcp_pd_addresses:%s dhcp_pd_routes:%s ndisc_addresses:%s ndisc_routes:%s", |
39373cb9 YW |
831 | __func__, |
832 | yes_no(link->dhcp4_configured), | |
833 | yes_no(link->dhcp6_address_configured), | |
834 | yes_no(link->dhcp6_route_configured), | |
835 | yes_no(link->dhcp6_pd_address_configured), | |
836 | yes_no(link->dhcp6_pd_route_configured), | |
d98c546d YW |
837 | yes_no(link->ndisc_addresses_configured), |
838 | yes_no(link->ndisc_routes_configured)); | |
39373cb9 | 839 | } |
463797c1 | 840 | } |
6accfd31 | 841 | |
5f58af25 | 842 | link_enter_configured(link); |
6accfd31 DA |
843 | |
844 | return; | |
845 | } | |
846 | ||
682c65b0 | 847 | static int link_set_static_configs(Link *link) { |
f882c247 TG |
848 | int r; |
849 | ||
850 | assert(link); | |
851 | assert(link->network); | |
f5be5601 | 852 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 853 | |
2428613f | 854 | /* Reset all *_configured flags we are configuring. */ |
9b966cee | 855 | link->request_static_addresses = false; |
2428613f | 856 | link->addresses_configured = false; |
6aa5773b | 857 | link->addresses_ready = false; |
2428613f YW |
858 | link->neighbors_configured = false; |
859 | link->static_routes_configured = false; | |
c16c7808 | 860 | link->static_nexthops_configured = false; |
2428613f YW |
861 | link->routing_policy_rules_configured = false; |
862 | ||
f6bb7ac5 TJ |
863 | r = link_set_bridge_fdb(link); |
864 | if (r < 0) | |
865 | return r; | |
866 | ||
1f241589 YW |
867 | r = link_set_bridge_mdb(link); |
868 | if (r < 0) | |
869 | return r; | |
870 | ||
58f1fe9a | 871 | r = link_set_neighbors(link); |
f3ef324d YW |
872 | if (r < 0) |
873 | return r; | |
e4a71bf3 | 874 | |
682c65b0 YW |
875 | r = link_set_addresses(link); |
876 | if (r < 0) | |
877 | return r; | |
bd6379ec | 878 | |
fe2bc17c YW |
879 | r = link_set_address_labels(link); |
880 | if (r < 0) | |
881 | return r; | |
f882c247 | 882 | |
e5526518 | 883 | /* now that we can figure out a default address for the dhcp server, start it */ |
5ae0fb7f YW |
884 | r = dhcp4_server_configure(link); |
885 | if (r < 0) | |
886 | return r; | |
d4cdbea5 | 887 | |
f882c247 TG |
888 | return 0; |
889 | } | |
890 | ||
3a390124 | 891 | static int link_configure_continue(Link *link); |
55dc8c4a | 892 | |
e16e4b3b DS |
893 | static int link_mac_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
894 | int r; | |
895 | ||
896 | assert(link); | |
897 | ||
898 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
899 | return 1; | |
900 | ||
901 | r = sd_netlink_message_get_errno(m); | |
902 | if (r < 0) | |
903 | log_link_message_warning_errno(link, m, r, "Could not set MAC address, ignoring"); | |
904 | else | |
905 | log_link_debug(link, "Setting MAC address done."); | |
906 | ||
907 | return 1; | |
908 | } | |
909 | ||
910 | static int link_set_mac(Link *link) { | |
911 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
912 | int r; | |
913 | ||
914 | assert(link); | |
915 | assert(link->network); | |
916 | assert(link->manager); | |
917 | assert(link->manager->rtnl); | |
918 | ||
919 | if (!link->network->mac) | |
920 | return 0; | |
921 | ||
922 | log_link_debug(link, "Setting MAC address"); | |
923 | ||
924 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
925 | if (r < 0) | |
926 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
927 | ||
928 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); | |
929 | if (r < 0) | |
930 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
931 | ||
932 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_mac_handler, | |
933 | link_netlink_destroy_callback, link); | |
934 | if (r < 0) | |
935 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
936 | ||
937 | link_ref(link); | |
938 | ||
939 | return 0; | |
940 | } | |
941 | ||
942 | static int link_nomaster_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
943 | int r; | |
944 | ||
945 | assert(link); | |
946 | ||
947 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
948 | return 1; | |
949 | ||
950 | r = sd_netlink_message_get_errno(m); | |
951 | if (r < 0) | |
952 | log_link_message_warning_errno(link, m, r, "Could not set nomaster, ignoring"); | |
953 | else | |
954 | log_link_debug(link, "Setting nomaster done."); | |
955 | ||
956 | return 1; | |
957 | } | |
958 | ||
959 | static int link_set_nomaster(Link *link) { | |
960 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
961 | int r; | |
962 | ||
963 | assert(link); | |
964 | assert(link->network); | |
965 | assert(link->manager); | |
966 | assert(link->manager->rtnl); | |
967 | ||
968 | /* set it free if not enslaved with networkd */ | |
969 | if (link->network->bridge || link->network->bond || link->network->vrf) | |
970 | return 0; | |
971 | ||
972 | log_link_debug(link, "Setting nomaster"); | |
973 | ||
974 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
975 | if (r < 0) | |
976 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
977 | ||
978 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); | |
979 | if (r < 0) | |
980 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
981 | ||
982 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_nomaster_handler, | |
983 | link_netlink_destroy_callback, link); | |
984 | if (r < 0) | |
985 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
986 | ||
987 | link_ref(link); | |
988 | ||
989 | return 0; | |
990 | } | |
991 | ||
302a796f | 992 | static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
4f882b2a TG |
993 | int r; |
994 | ||
995 | assert(m); | |
996 | assert(link); | |
997 | assert(link->ifname); | |
998 | ||
55dc8c4a YW |
999 | link->setting_mtu = false; |
1000 | ||
5da8149f | 1001 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
1002 | return 1; |
1003 | ||
1c4baffc | 1004 | r = sd_netlink_message_get_errno(m); |
4ff296b0 | 1005 | if (r < 0) |
5ecb131d | 1006 | log_link_message_warning_errno(link, m, r, "Could not set MTU, ignoring"); |
4ff296b0 YW |
1007 | else |
1008 | log_link_debug(link, "Setting MTU done."); | |
55dc8c4a | 1009 | |
4ff296b0 | 1010 | if (link->state == LINK_STATE_INITIALIZED) { |
3a390124 | 1011 | r = link_configure_continue(link); |
4ff296b0 YW |
1012 | if (r < 0) |
1013 | link_enter_failed(link); | |
1014 | } | |
4f882b2a TG |
1015 | |
1016 | return 1; | |
1017 | } | |
1018 | ||
933c70a0 | 1019 | int link_set_mtu(Link *link, uint32_t mtu) { |
4afd3348 | 1020 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f882b2a TG |
1021 | int r; |
1022 | ||
1023 | assert(link); | |
1024 | assert(link->manager); | |
1025 | assert(link->manager->rtnl); | |
1026 | ||
40288ece YW |
1027 | if (mtu == 0 || link->setting_mtu) |
1028 | return 0; | |
1029 | ||
933c70a0 | 1030 | if (link->mtu == mtu) |
ee493106 YW |
1031 | return 0; |
1032 | ||
6a7a4e4d | 1033 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 1034 | |
6a7a4e4d LP |
1035 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1036 | if (r < 0) | |
1037 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a | 1038 | |
44b598a1 | 1039 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes |
72c2500b YW |
1040 | * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ |
1041 | if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) { | |
44b598a1 SS |
1042 | |
1043 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
d236718c | 1044 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes"); |
44b598a1 | 1045 | |
72c2500b | 1046 | mtu = IPV6_MIN_MTU; |
44b598a1 SS |
1047 | } |
1048 | ||
1c4baffc | 1049 | r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu); |
6a7a4e4d LP |
1050 | if (r < 0) |
1051 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 1052 | |
302a796f YW |
1053 | r = netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler, |
1054 | link_netlink_destroy_callback, link); | |
6a7a4e4d LP |
1055 | if (r < 0) |
1056 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 1057 | |
ae941762 | 1058 | link_ref(link); |
1046bf9b | 1059 | link->setting_mtu = true; |
b226d99b | 1060 | |
4f882b2a TG |
1061 | return 0; |
1062 | } | |
1063 | ||
f6fcc1c2 YW |
1064 | static bool link_reduces_vlan_mtu(Link *link) { |
1065 | /* See netif_reduces_vlan_mtu() in kernel. */ | |
1066 | return streq_ptr(link->kind, "macsec"); | |
1067 | } | |
1068 | ||
1069 | static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link *link) { | |
1070 | uint32_t mtu = 0; | |
1071 | NetDev *dev; | |
f6fcc1c2 | 1072 | |
90e74a66 | 1073 | HASHMAP_FOREACH(dev, link->network->stacked_netdevs) |
f6fcc1c2 YW |
1074 | if (dev->kind == NETDEV_KIND_VLAN && dev->mtu > 0) |
1075 | /* See vlan_dev_change_mtu() in kernel. */ | |
1076 | mtu = MAX(mtu, link_reduces_vlan_mtu(link) ? dev->mtu + 4 : dev->mtu); | |
1077 | ||
1078 | else if (dev->kind == NETDEV_KIND_MACVLAN && dev->mtu > mtu) | |
1079 | /* See macvlan_change_mtu() in kernel. */ | |
1080 | mtu = dev->mtu; | |
1081 | ||
1082 | return mtu; | |
1083 | } | |
1084 | ||
1085 | static int link_configure_mtu(Link *link) { | |
1086 | uint32_t mtu; | |
1087 | ||
1088 | assert(link); | |
1089 | assert(link->network); | |
1090 | ||
1091 | if (link->network->mtu > 0) | |
1092 | return link_set_mtu(link, link->network->mtu); | |
1093 | ||
1094 | mtu = link_get_requested_mtu_by_stacked_netdevs(link); | |
1095 | if (link->mtu >= mtu) | |
1096 | return 0; | |
1097 | ||
1098 | log_link_notice(link, "Bumping MTU bytes from %"PRIu32" to %"PRIu32" because of stacked device. " | |
1099 | "If it is not desired, then please explicitly specify MTUBytes= setting.", | |
1100 | link->mtu, mtu); | |
1101 | ||
1102 | return link_set_mtu(link, mtu); | |
1103 | } | |
1104 | ||
302a796f | 1105 | static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
99d2baa2 SS |
1106 | int r; |
1107 | ||
1108 | assert(m); | |
1109 | assert(link); | |
1110 | assert(link->ifname); | |
1111 | ||
1112 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1113 | return 1; | |
1114 | ||
1115 | r = sd_netlink_message_get_errno(m); | |
1116 | if (r < 0) | |
5ecb131d | 1117 | log_link_message_warning_errno(link, m, r, "Could not set link flags, ignoring"); |
99d2baa2 SS |
1118 | |
1119 | return 1; | |
1120 | } | |
1121 | ||
1122 | static int link_set_flags(Link *link) { | |
1123 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1124 | unsigned ifi_change = 0; | |
1125 | unsigned ifi_flags = 0; | |
1126 | int r; | |
1127 | ||
1128 | assert(link); | |
1129 | assert(link->manager); | |
1130 | assert(link->manager->rtnl); | |
1131 | ||
1132 | if (link->flags & IFF_LOOPBACK) | |
1133 | return 0; | |
1134 | ||
1135 | if (!link->network) | |
1136 | return 0; | |
1137 | ||
866e6b7a | 1138 | if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0) |
99d2baa2 SS |
1139 | return 0; |
1140 | ||
1141 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1142 | if (r < 0) | |
1143 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1144 | ||
1145 | if (link->network->arp >= 0) { | |
1146 | ifi_change |= IFF_NOARP; | |
e6ebebbe SS |
1147 | SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0); |
1148 | } | |
1149 | ||
1150 | if (link->network->multicast >= 0) { | |
1151 | ifi_change |= IFF_MULTICAST; | |
1152 | SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast); | |
99d2baa2 SS |
1153 | } |
1154 | ||
866e6b7a SS |
1155 | if (link->network->allmulticast >= 0) { |
1156 | ifi_change |= IFF_ALLMULTI; | |
1157 | SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast); | |
1158 | } | |
1159 | ||
99d2baa2 SS |
1160 | r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change); |
1161 | if (r < 0) | |
1162 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1163 | ||
302a796f YW |
1164 | r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler, |
1165 | link_netlink_destroy_callback, link); | |
99d2baa2 SS |
1166 | if (r < 0) |
1167 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1168 | ||
1169 | link_ref(link); | |
1170 | ||
1171 | return 0; | |
1172 | } | |
1173 | ||
e7ab854c TG |
1174 | static int link_acquire_ipv6_conf(Link *link) { |
1175 | int r; | |
1176 | ||
1177 | assert(link); | |
1178 | ||
2ffd6d73 | 1179 | if (link->ndisc) { |
e7ab854c TG |
1180 | log_link_debug(link, "Discovering IPv6 routers"); |
1181 | ||
1e7a0e21 | 1182 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1183 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1184 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1185 | } | |
1186 | ||
086b8853 | 1187 | if (link->radv) { |
7465dd22 PF |
1188 | assert(link->radv); |
1189 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1190 | ||
1191 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
1192 | ||
fd3ef936 YW |
1193 | r = radv_emit_dns(link); |
1194 | if (r < 0) | |
1195 | return log_link_warning_errno(link, r, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m"); | |
1196 | ||
7465dd22 PF |
1197 | r = sd_radv_start(link->radv); |
1198 | if (r < 0 && r != -EBUSY) | |
1199 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); | |
1200 | } | |
1201 | ||
838d39af SS |
1202 | if (link_dhcp6_enabled(link) && IN_SET(link->network->dhcp6_without_ra, |
1203 | DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST, | |
1204 | DHCP6_CLIENT_START_MODE_SOLICIT)) { | |
cd305af1 SS |
1205 | assert(link->dhcp6_client); |
1206 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1207 | ||
838d39af | 1208 | r = dhcp6_request_address(link, link->network->dhcp6_without_ra == DHCP6_CLIENT_START_MODE_INFORMATION_REQUEST); |
cd305af1 | 1209 | if (r < 0 && r != -EBUSY) |
1633c457 | 1210 | return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m"); |
cd305af1 SS |
1211 | else |
1212 | log_link_debug(link, "Acquiring DHCPv6 lease"); | |
1213 | } | |
1214 | ||
1633c457 YW |
1215 | r = dhcp6_request_prefix_delegation(link); |
1216 | if (r < 0) | |
1217 | return log_link_warning_errno(link, r, "Failed to request DHCPv6 prefix delegation: %m"); | |
10752343 | 1218 | |
e7ab854c TG |
1219 | return 0; |
1220 | } | |
1221 | ||
6fc25497 | 1222 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1223 | int r; |
1224 | ||
1225 | assert(link); | |
ff254138 TG |
1226 | assert(link->manager); |
1227 | assert(link->manager->event); | |
1228 | ||
910feb78 | 1229 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4)) { |
eb34d4af | 1230 | assert(link->ipv4ll); |
ff254138 | 1231 | |
6a7a4e4d | 1232 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1233 | |
1234 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1235 | if (r < 0) |
1236 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1237 | } |
1238 | ||
2ffd6d73 | 1239 | if (link->dhcp_client) { |
6a7a4e4d | 1240 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1241 | |
5c1d3fc9 | 1242 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1243 | if (r < 0) |
1244 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1245 | } |
ff254138 | 1246 | |
6fc25497 SS |
1247 | return 0; |
1248 | } | |
1249 | ||
1250 | static int link_acquire_conf(Link *link) { | |
1251 | int r; | |
1252 | ||
1253 | assert(link); | |
1254 | ||
1255 | r = link_acquire_ipv4_conf(link); | |
1256 | if (r < 0) | |
1257 | return r; | |
1258 | ||
3f7cc080 | 1259 | if (!in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address)) { |
6fc25497 SS |
1260 | r = link_acquire_ipv6_conf(link); |
1261 | if (r < 0) | |
1262 | return r; | |
1263 | } | |
1264 | ||
2ffd6d73 YW |
1265 | r = link_lldp_emit_start(link); |
1266 | if (r < 0) | |
1267 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
8e1ad1ea | 1268 | |
ff254138 TG |
1269 | return 0; |
1270 | } | |
1271 | ||
a61bb41c | 1272 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1273 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1274 | ||
a61bb41c | 1275 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1276 | return true; |
1277 | ||
a61bb41c | 1278 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1279 | /* operstate may not be implemented, so fall back to flags */ |
b26ea308 YW |
1280 | if (FLAGS_SET(link->flags, IFF_LOWER_UP | IFF_RUNNING) && |
1281 | !FLAGS_SET(link->flags, IFF_DORMANT)) | |
deb2e523 TG |
1282 | return true; |
1283 | ||
1284 | return false; | |
1285 | } | |
1286 | ||
0e2fdb83 SS |
1287 | static int link_address_genmode_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
1288 | int r; | |
1289 | ||
1290 | assert(link); | |
1291 | ||
9524014e DS |
1292 | link->setting_genmode = false; |
1293 | ||
0e2fdb83 SS |
1294 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1295 | return 1; | |
1296 | ||
1297 | r = sd_netlink_message_get_errno(m); | |
1298 | if (r < 0) | |
5ecb131d | 1299 | log_link_message_warning_errno(link, m, r, "Could not set address genmode for interface, ignoring"); |
9524014e DS |
1300 | else |
1301 | log_link_debug(link, "Setting address genmode done."); | |
1302 | ||
1303 | if (link->state == LINK_STATE_INITIALIZED) { | |
1304 | r = link_configure_continue(link); | |
1305 | if (r < 0) | |
1306 | link_enter_failed(link); | |
1307 | } | |
0e2fdb83 SS |
1308 | |
1309 | return 1; | |
1310 | } | |
1311 | ||
1312 | static int link_configure_addrgen_mode(Link *link) { | |
1313 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1314 | uint8_t ipv6ll_mode; | |
1315 | int r; | |
1316 | ||
1317 | assert(link); | |
1318 | assert(link->network); | |
1319 | assert(link->manager); | |
1320 | assert(link->manager->rtnl); | |
1321 | ||
9524014e | 1322 | if (!socket_ipv6_is_supported() || link->setting_genmode) |
9f6e82e6 YW |
1323 | return 0; |
1324 | ||
0e2fdb83 SS |
1325 | log_link_debug(link, "Setting address genmode for link"); |
1326 | ||
1327 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1328 | if (r < 0) | |
1329 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1330 | ||
1331 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); | |
1332 | if (r < 0) | |
1333 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
1334 | ||
1335 | r = sd_netlink_message_open_container(req, AF_INET6); | |
1336 | if (r < 0) | |
1337 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
1338 | ||
1339 | if (!link_ipv6ll_enabled(link)) | |
1340 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
6f6296b9 | 1341 | else if (link->network->ipv6ll_address_gen_mode < 0) { |
a6f1848a SS |
1342 | r = sysctl_read_ip_property(AF_INET6, link->ifname, "stable_secret", NULL); |
1343 | if (r < 0) { | |
1344 | /* The file may not exist. And even if it exists, when stable_secret is unset, | |
1345 | * reading the file fails with EIO. */ | |
1346 | log_link_debug_errno(link, r, "Failed to read sysctl property stable_secret: %m"); | |
1347 | ||
1348 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1349 | } else | |
1350 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1351 | } else | |
6f6296b9 | 1352 | ipv6ll_mode = link->network->ipv6ll_address_gen_mode; |
0e2fdb83 SS |
1353 | |
1354 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); | |
1355 | if (r < 0) | |
1356 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
1357 | ||
1358 | r = sd_netlink_message_close_container(req); | |
1359 | if (r < 0) | |
1360 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); | |
1361 | ||
1362 | r = sd_netlink_message_close_container(req); | |
1363 | if (r < 0) | |
1364 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
1365 | ||
1366 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_address_genmode_handler, | |
1367 | link_netlink_destroy_callback, link); | |
1368 | if (r < 0) | |
1369 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1370 | ||
1371 | link_ref(link); | |
9524014e | 1372 | link->setting_genmode = true; |
0e2fdb83 SS |
1373 | |
1374 | return 0; | |
1375 | } | |
1376 | ||
302a796f | 1377 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
dd3efc09 TG |
1378 | int r; |
1379 | ||
1746cf2a TG |
1380 | assert(link); |
1381 | ||
5da8149f | 1382 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1383 | return 1; |
1384 | ||
1c4baffc | 1385 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d | 1386 | if (r < 0) |
26d6b214 | 1387 | /* we warn but don't fail the link, as it may be brought up later */ |
5ecb131d | 1388 | log_link_message_warning_errno(link, m, r, "Could not bring up interface"); |
45ad2c13 | 1389 | |
f882c247 TG |
1390 | return 1; |
1391 | } | |
1392 | ||
1cc84f3b | 1393 | static int link_up(Link *link) { |
4afd3348 | 1394 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
f579559b TG |
1395 | int r; |
1396 | ||
f882c247 | 1397 | assert(link); |
c106cc36 | 1398 | assert(link->network); |
f882c247 TG |
1399 | assert(link->manager); |
1400 | assert(link->manager->rtnl); | |
1401 | ||
6a7a4e4d | 1402 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1403 | |
6a7a4e4d LP |
1404 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1405 | if (r < 0) | |
1406 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1407 | |
5d4795f3 | 1408 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1409 | if (r < 0) |
1410 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1411 | |
302a796f YW |
1412 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, |
1413 | link_netlink_destroy_callback, link); | |
6a7a4e4d LP |
1414 | if (r < 0) |
1415 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1416 | |
b226d99b TG |
1417 | link_ref(link); |
1418 | ||
f882c247 TG |
1419 | return 0; |
1420 | } | |
1421 | ||
302a796f | 1422 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
0d4ad91d AR |
1423 | int r; |
1424 | ||
1425 | assert(link); | |
1426 | ||
1427 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1428 | return 1; | |
1429 | ||
1c4baffc | 1430 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1431 | if (r < 0) |
5ecb131d | 1432 | log_link_message_warning_errno(link, m, r, "Could not bring down interface"); |
0d4ad91d AR |
1433 | |
1434 | return 1; | |
1435 | } | |
1436 | ||
8e54db83 | 1437 | int link_down(Link *link, link_netlink_message_handler_t callback) { |
4afd3348 | 1438 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1439 | int r; |
1440 | ||
1441 | assert(link); | |
1442 | assert(link->manager); | |
1443 | assert(link->manager->rtnl); | |
1444 | ||
6a7a4e4d | 1445 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1446 | |
1447 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1448 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1449 | if (r < 0) |
1450 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1451 | |
1452 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1453 | if (r < 0) |
1454 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1455 | |
8e54db83 YW |
1456 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
1457 | callback ?: link_down_handler, | |
302a796f | 1458 | link_netlink_destroy_callback, link); |
6a7a4e4d LP |
1459 | if (r < 0) |
1460 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1461 | |
1462 | link_ref(link); | |
1463 | ||
1464 | return 0; | |
1465 | } | |
1466 | ||
89fe6535 SS |
1467 | static int link_group_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
1468 | int r; | |
1469 | ||
1470 | assert(link); | |
1471 | ||
1472 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1473 | return 1; | |
1474 | ||
1475 | r = sd_netlink_message_get_errno(m); | |
1476 | if (r < 0) | |
1477 | log_link_message_warning_errno(link, m, r, "Could not set group for the interface"); | |
1478 | ||
1479 | return 1; | |
1480 | } | |
1481 | ||
1482 | static int link_set_group(Link *link) { | |
1483 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1484 | int r; | |
1485 | ||
1486 | assert(link); | |
1487 | assert(link->network); | |
1488 | assert(link->manager); | |
1489 | assert(link->manager->rtnl); | |
1490 | ||
1491 | if (link->network->group <= 0) | |
1492 | return 0; | |
1493 | ||
1494 | log_link_debug(link, "Setting group"); | |
1495 | ||
1496 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1497 | if (r < 0) | |
1498 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1499 | ||
1500 | r = sd_netlink_message_append_u32(req, IFLA_GROUP, link->network->group); | |
1501 | if (r < 0) | |
1502 | return log_link_error_errno(link, r, "Could not set link group: %m"); | |
1503 | ||
1504 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_group_handler, | |
1505 | link_netlink_destroy_callback, link); | |
1506 | if (r < 0) | |
1507 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1508 | ||
1509 | link_ref(link); | |
1510 | ||
1511 | return 0; | |
1512 | } | |
1513 | ||
0d4ad91d AR |
1514 | static int link_handle_bound_to_list(Link *link) { |
1515 | Link *l; | |
0d4ad91d AR |
1516 | int r; |
1517 | bool required_up = false; | |
1518 | bool link_is_up = false; | |
1519 | ||
1520 | assert(link); | |
1521 | ||
1522 | if (hashmap_isempty(link->bound_to_links)) | |
1523 | return 0; | |
1524 | ||
1525 | if (link->flags & IFF_UP) | |
1526 | link_is_up = true; | |
1527 | ||
90e74a66 | 1528 | HASHMAP_FOREACH (l, link->bound_to_links) |
0d4ad91d AR |
1529 | if (link_has_carrier(l)) { |
1530 | required_up = true; | |
1531 | break; | |
1532 | } | |
1533 | ||
1534 | if (!required_up && link_is_up) { | |
8e54db83 | 1535 | r = link_down(link, NULL); |
0d4ad91d AR |
1536 | if (r < 0) |
1537 | return r; | |
1538 | } else if (required_up && !link_is_up) { | |
1539 | r = link_up(link); | |
1540 | if (r < 0) | |
1541 | return r; | |
1542 | } | |
1543 | ||
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | static int link_handle_bound_by_list(Link *link) { | |
0d4ad91d AR |
1548 | Link *l; |
1549 | int r; | |
1550 | ||
1551 | assert(link); | |
1552 | ||
1553 | if (hashmap_isempty(link->bound_by_links)) | |
1554 | return 0; | |
1555 | ||
90e74a66 | 1556 | HASHMAP_FOREACH (l, link->bound_by_links) { |
0d4ad91d AR |
1557 | r = link_handle_bound_to_list(l); |
1558 | if (r < 0) | |
1559 | return r; | |
1560 | } | |
1561 | ||
1562 | return 0; | |
1563 | } | |
1564 | ||
1565 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1566 | int r; | |
1567 | ||
1568 | assert(link); | |
1569 | assert(carrier); | |
1570 | ||
1571 | if (link == carrier) | |
1572 | return 0; | |
1573 | ||
1574 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1575 | return 0; | |
1576 | ||
1577 | r = hashmap_ensure_allocated(h, NULL); | |
1578 | if (r < 0) | |
1579 | return r; | |
1580 | ||
1581 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1582 | if (r < 0) | |
1583 | return r; | |
1584 | ||
1585 | return 0; | |
1586 | } | |
1587 | ||
1588 | static int link_new_bound_by_list(Link *link) { | |
1589 | Manager *m; | |
1590 | Link *carrier; | |
0d4ad91d AR |
1591 | int r; |
1592 | bool list_updated = false; | |
1593 | ||
1594 | assert(link); | |
1595 | assert(link->manager); | |
1596 | ||
1597 | m = link->manager; | |
1598 | ||
90e74a66 | 1599 | HASHMAP_FOREACH(carrier, m->links) { |
0d4ad91d AR |
1600 | if (!carrier->network) |
1601 | continue; | |
1602 | ||
1603 | if (strv_isempty(carrier->network->bind_carrier)) | |
1604 | continue; | |
1605 | ||
191a3f16 | 1606 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) { |
0d4ad91d AR |
1607 | r = link_put_carrier(link, carrier, &link->bound_by_links); |
1608 | if (r < 0) | |
1609 | return r; | |
1610 | ||
1611 | list_updated = true; | |
1612 | } | |
1613 | } | |
1614 | ||
1615 | if (list_updated) | |
84de38c5 | 1616 | link_dirty(link); |
0d4ad91d | 1617 | |
90e74a66 | 1618 | HASHMAP_FOREACH(carrier, link->bound_by_links) { |
0d4ad91d AR |
1619 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
1620 | if (r < 0) | |
1621 | return r; | |
1622 | ||
84de38c5 | 1623 | link_dirty(carrier); |
0d4ad91d AR |
1624 | } |
1625 | ||
1626 | return 0; | |
1627 | } | |
1628 | ||
1629 | static int link_new_bound_to_list(Link *link) { | |
1630 | Manager *m; | |
1631 | Link *carrier; | |
0d4ad91d AR |
1632 | int r; |
1633 | bool list_updated = false; | |
1634 | ||
1635 | assert(link); | |
1636 | assert(link->manager); | |
1637 | ||
1638 | if (!link->network) | |
1639 | return 0; | |
1640 | ||
1641 | if (strv_isempty(link->network->bind_carrier)) | |
1642 | return 0; | |
1643 | ||
1644 | m = link->manager; | |
1645 | ||
90e74a66 | 1646 | HASHMAP_FOREACH (carrier, m->links) { |
191a3f16 | 1647 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) { |
0d4ad91d AR |
1648 | r = link_put_carrier(link, carrier, &link->bound_to_links); |
1649 | if (r < 0) | |
1650 | return r; | |
1651 | ||
1652 | list_updated = true; | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | if (list_updated) | |
84de38c5 | 1657 | link_dirty(link); |
0d4ad91d | 1658 | |
90e74a66 | 1659 | HASHMAP_FOREACH (carrier, link->bound_to_links) { |
0d4ad91d AR |
1660 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); |
1661 | if (r < 0) | |
1662 | return r; | |
1663 | ||
84de38c5 | 1664 | link_dirty(carrier); |
0d4ad91d AR |
1665 | } |
1666 | ||
1667 | return 0; | |
1668 | } | |
1669 | ||
1670 | static int link_new_carrier_maps(Link *link) { | |
1671 | int r; | |
1672 | ||
1673 | r = link_new_bound_by_list(link); | |
1674 | if (r < 0) | |
1675 | return r; | |
1676 | ||
1677 | r = link_handle_bound_by_list(link); | |
1678 | if (r < 0) | |
1679 | return r; | |
1680 | ||
1681 | r = link_new_bound_to_list(link); | |
1682 | if (r < 0) | |
1683 | return r; | |
1684 | ||
1685 | r = link_handle_bound_to_list(link); | |
1686 | if (r < 0) | |
1687 | return r; | |
1688 | ||
1689 | return 0; | |
1690 | } | |
1691 | ||
1692 | static void link_free_bound_to_list(Link *link) { | |
1693 | Link *bound_to; | |
0d4ad91d | 1694 | |
90e74a66 | 1695 | HASHMAP_FOREACH (bound_to, link->bound_to_links) { |
0d4ad91d AR |
1696 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); |
1697 | ||
1698 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 1699 | link_dirty(bound_to); |
0d4ad91d AR |
1700 | } |
1701 | ||
1702 | return; | |
1703 | } | |
1704 | ||
1705 | static void link_free_bound_by_list(Link *link) { | |
1706 | Link *bound_by; | |
0d4ad91d | 1707 | |
90e74a66 | 1708 | HASHMAP_FOREACH (bound_by, link->bound_by_links) { |
0d4ad91d AR |
1709 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); |
1710 | ||
1711 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 1712 | link_dirty(bound_by); |
0d4ad91d AR |
1713 | link_handle_bound_to_list(bound_by); |
1714 | } | |
1715 | } | |
1716 | ||
1717 | return; | |
1718 | } | |
1719 | ||
1720 | static void link_free_carrier_maps(Link *link) { | |
1721 | bool list_updated = false; | |
1722 | ||
1723 | assert(link); | |
1724 | ||
1725 | if (!hashmap_isempty(link->bound_to_links)) { | |
1726 | link_free_bound_to_list(link); | |
1727 | list_updated = true; | |
1728 | } | |
1729 | ||
1730 | if (!hashmap_isempty(link->bound_by_links)) { | |
1731 | link_free_bound_by_list(link); | |
1732 | list_updated = true; | |
1733 | } | |
1734 | ||
1735 | if (list_updated) | |
84de38c5 | 1736 | link_dirty(link); |
0d4ad91d AR |
1737 | |
1738 | return; | |
1739 | } | |
1740 | ||
5f707e12 YW |
1741 | static int link_append_to_master(Link *link, NetDev *netdev) { |
1742 | Link *master; | |
1743 | int r; | |
1744 | ||
1745 | assert(link); | |
1746 | assert(netdev); | |
1747 | ||
1748 | r = link_get(link->manager, netdev->ifindex, &master); | |
1749 | if (r < 0) | |
1750 | return r; | |
1751 | ||
de7fef4b | 1752 | r = set_ensure_put(&master->slaves, NULL, link); |
38288f0b | 1753 | if (r <= 0) |
5f707e12 YW |
1754 | return r; |
1755 | ||
1756 | link_ref(link); | |
1757 | return 0; | |
1758 | } | |
1759 | ||
1760 | static void link_drop_from_master(Link *link, NetDev *netdev) { | |
1761 | Link *master; | |
1762 | ||
1763 | assert(link); | |
1764 | ||
1765 | if (!link->manager || !netdev) | |
1766 | return; | |
1767 | ||
1768 | if (link_get(link->manager, netdev->ifindex, &master) < 0) | |
1769 | return; | |
1770 | ||
1771 | link_unref(set_remove(master->slaves, link)); | |
1772 | } | |
1773 | ||
1774 | static void link_detach_from_manager(Link *link) { | |
1775 | if (!link || !link->manager) | |
1776 | return; | |
1777 | ||
1778 | link_unref(set_remove(link->manager->links_requesting_uuid, link)); | |
1779 | link_clean(link); | |
1780 | ||
1781 | /* The following must be called at last. */ | |
1782 | assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link); | |
1783 | link_unref(link); | |
1784 | } | |
1785 | ||
0d4ad91d AR |
1786 | void link_drop(Link *link) { |
1787 | if (!link || link->state == LINK_STATE_LINGER) | |
1788 | return; | |
1789 | ||
1790 | link_set_state(link, LINK_STATE_LINGER); | |
1791 | ||
1792 | link_free_carrier_maps(link); | |
1793 | ||
5f707e12 YW |
1794 | if (link->network) { |
1795 | link_drop_from_master(link, link->network->bridge); | |
1796 | link_drop_from_master(link, link->network->bond); | |
1797 | } | |
1798 | ||
6a7a4e4d | 1799 | log_link_debug(link, "Link removed"); |
0d4ad91d | 1800 | |
db2f8a2e | 1801 | (void) unlink(link->state_file); |
c4397d94 | 1802 | link_detach_from_manager(link); |
0d4ad91d AR |
1803 | } |
1804 | ||
3f265037 | 1805 | static int link_joined(Link *link) { |
f882c247 TG |
1806 | int r; |
1807 | ||
ef1ba606 | 1808 | assert(link); |
f5be5601 | 1809 | assert(link->network); |
dd3efc09 | 1810 | |
0d4ad91d AR |
1811 | if (!hashmap_isempty(link->bound_to_links)) { |
1812 | r = link_handle_bound_to_list(link); | |
1813 | if (r < 0) | |
1814 | return r; | |
1815 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
1816 | r = link_up(link); |
1817 | if (r < 0) { | |
1818 | link_enter_failed(link); | |
1819 | return r; | |
1820 | } | |
ef1ba606 | 1821 | } |
f882c247 | 1822 | |
9ed794a3 | 1823 | if (link->network->bridge) { |
e1853b00 | 1824 | r = link_set_bridge(link); |
6a7a4e4d LP |
1825 | if (r < 0) |
1826 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
45e11abf YW |
1827 | |
1828 | r = link_append_to_master(link, link->network->bridge); | |
1829 | if (r < 0) | |
1830 | log_link_error_errno(link, r, "Failed to add to bridge master's slave list: %m"); | |
e1853b00 SS |
1831 | } |
1832 | ||
eb64b435 | 1833 | if (link->network->bond) { |
7fcee284 | 1834 | r = link_set_bond(link); |
eb64b435 SS |
1835 | if (r < 0) |
1836 | log_link_error_errno(link, r, "Could not set bond message: %m"); | |
959f65d3 | 1837 | |
45e11abf | 1838 | r = link_append_to_master(link, link->network->bond); |
959f65d3 YW |
1839 | if (r < 0) |
1840 | log_link_error_errno(link, r, "Failed to add to bond master's slave list: %m"); | |
eb64b435 SS |
1841 | } |
1842 | ||
34d7f2c9 YW |
1843 | r = link_set_bridge_vlan(link); |
1844 | if (r < 0) | |
1845 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
13b498f9 | 1846 | |
c1835a42 RM |
1847 | /* Skip setting up addresses until it gets carrier, |
1848 | or it would try to set addresses twice, | |
1849 | which is bad for non-idempotent steps. */ | |
dad2d78e | 1850 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
c1835a42 RM |
1851 | return 0; |
1852 | ||
b425c3ae | 1853 | link_set_state(link, LINK_STATE_CONFIGURING); |
0e569a43 YW |
1854 | |
1855 | r = link_acquire_conf(link); | |
1856 | if (r < 0) | |
1857 | return r; | |
1858 | ||
682c65b0 | 1859 | return link_set_static_configs(link); |
02b59d57 TG |
1860 | } |
1861 | ||
302a796f | 1862 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
02b59d57 TG |
1863 | int r; |
1864 | ||
1746cf2a | 1865 | assert(link); |
ef1ba606 | 1866 | assert(link->network); |
34bf3c00 | 1867 | assert(link->enslaving > 0); |
02b59d57 | 1868 | |
313cefa1 | 1869 | link->enslaving--; |
52433f6b | 1870 | |
5da8149f | 1871 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
1872 | return 1; |
1873 | ||
1c4baffc | 1874 | r = sd_netlink_message_get_errno(m); |
856f962c | 1875 | if (r < 0 && r != -EEXIST) { |
5ecb131d | 1876 | log_link_message_warning_errno(link, m, r, "Could not join netdev"); |
ef1ba606 TG |
1877 | link_enter_failed(link); |
1878 | return 1; | |
4ff296b0 YW |
1879 | } |
1880 | ||
1881 | log_link_debug(link, "Joined netdev"); | |
02b59d57 | 1882 | |
34bf3c00 | 1883 | if (link->enslaving == 0) { |
4ff296b0 YW |
1884 | r = link_joined(link); |
1885 | if (r < 0) | |
1886 | link_enter_failed(link); | |
34bf3c00 | 1887 | } |
02b59d57 TG |
1888 | |
1889 | return 1; | |
1890 | } | |
1891 | ||
3f265037 | 1892 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 1893 | NetDev *netdev; |
02b59d57 TG |
1894 | int r; |
1895 | ||
1896 | assert(link); | |
1897 | assert(link->network); | |
bd08ce56 | 1898 | assert(link->state == LINK_STATE_INITIALIZED); |
02b59d57 | 1899 | |
289e6774 | 1900 | link_set_state(link, LINK_STATE_CONFIGURING); |
02b59d57 | 1901 | |
84de38c5 | 1902 | link_dirty(link); |
34bf3c00 | 1903 | link->enslaving = 0; |
02b59d57 | 1904 | |
d9c67ea1 | 1905 | if (link->network->bond) { |
cbff7170 TJ |
1906 | if (link->network->bond->state == NETDEV_STATE_READY && |
1907 | link->network->bond->ifindex == link->master_ifindex) | |
1908 | return link_joined(link); | |
1909 | ||
f2341e0a LP |
1910 | log_struct(LOG_DEBUG, |
1911 | LOG_LINK_INTERFACE(link), | |
1912 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 1913 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname)); |
f2341e0a | 1914 | |
34bf3c00 YW |
1915 | link->enslaving++; |
1916 | ||
f2341e0a | 1917 | r = netdev_join(link->network->bond, link, netdev_join_handler); |
52433f6b | 1918 | if (r < 0) { |
f2341e0a LP |
1919 | log_struct_errno(LOG_WARNING, r, |
1920 | LOG_LINK_INTERFACE(link), | |
1921 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 1922 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname)); |
52433f6b TG |
1923 | link_enter_failed(link); |
1924 | return r; | |
1925 | } | |
0ad6148e MO |
1926 | } |
1927 | ||
d9c67ea1 | 1928 | if (link->network->bridge) { |
f2341e0a LP |
1929 | log_struct(LOG_DEBUG, |
1930 | LOG_LINK_INTERFACE(link), | |
1931 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 1932 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname)); |
f2341e0a | 1933 | |
34bf3c00 YW |
1934 | link->enslaving++; |
1935 | ||
f2341e0a | 1936 | r = netdev_join(link->network->bridge, link, netdev_join_handler); |
0ad6148e | 1937 | if (r < 0) { |
f2341e0a LP |
1938 | log_struct_errno(LOG_WARNING, r, |
1939 | LOG_LINK_INTERFACE(link), | |
1940 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 1941 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname)); |
0ad6148e MO |
1942 | link_enter_failed(link); |
1943 | return r; | |
1944 | } | |
52433f6b TG |
1945 | } |
1946 | ||
6cb955c6 AR |
1947 | if (link->network->vrf) { |
1948 | log_struct(LOG_DEBUG, | |
1949 | LOG_LINK_INTERFACE(link), | |
1950 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 ZJS |
1951 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname)); |
1952 | ||
34bf3c00 YW |
1953 | link->enslaving++; |
1954 | ||
6cb955c6 AR |
1955 | r = netdev_join(link->network->vrf, link, netdev_join_handler); |
1956 | if (r < 0) { | |
1957 | log_struct_errno(LOG_WARNING, r, | |
1958 | LOG_LINK_INTERFACE(link), | |
1959 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 | 1960 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname)); |
6cb955c6 AR |
1961 | link_enter_failed(link); |
1962 | return r; | |
1963 | } | |
6cb955c6 AR |
1964 | } |
1965 | ||
90e74a66 | 1966 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) { |
7951dea2 | 1967 | |
2b6db913 YW |
1968 | if (netdev->ifindex > 0) |
1969 | /* Assume already enslaved. */ | |
a63e5daa | 1970 | continue; |
a63e5daa | 1971 | |
7033af49 YW |
1972 | if (netdev_get_create_type(netdev) != NETDEV_CREATE_STACKED) |
1973 | continue; | |
1974 | ||
f2341e0a LP |
1975 | log_struct(LOG_DEBUG, |
1976 | LOG_LINK_INTERFACE(link), | |
1977 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 1978 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); |
f2341e0a | 1979 | |
34bf3c00 YW |
1980 | link->enslaving++; |
1981 | ||
f2341e0a | 1982 | r = netdev_join(netdev, link, netdev_join_handler); |
7951dea2 | 1983 | if (r < 0) { |
f2341e0a LP |
1984 | log_struct_errno(LOG_WARNING, r, |
1985 | LOG_LINK_INTERFACE(link), | |
1986 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 1987 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); |
326cb406 SS |
1988 | link_enter_failed(link); |
1989 | return r; | |
1990 | } | |
326cb406 SS |
1991 | } |
1992 | ||
34bf3c00 YW |
1993 | if (link->enslaving == 0) |
1994 | return link_joined(link); | |
1995 | ||
ef1ba606 TG |
1996 | return 0; |
1997 | } | |
1998 | ||
5e5b137a | 1999 | static int link_drop_foreign_config(Link *link) { |
5e5b137a TG |
2000 | int r; |
2001 | ||
f8f2f880 YW |
2002 | r = link_drop_foreign_addresses(link); |
2003 | if (r < 0) | |
2004 | return r; | |
5e5b137a | 2005 | |
59048336 YW |
2006 | r = link_drop_foreign_neighbors(link); |
2007 | if (r < 0) | |
2008 | return r; | |
d1bdafd2 | 2009 | |
779804dd | 2010 | return link_drop_foreign_routes(link); |
5e5b137a TG |
2011 | } |
2012 | ||
3104883d | 2013 | static int link_drop_config(Link *link) { |
3104883d SS |
2014 | int r; |
2015 | ||
d7fbb9f5 YW |
2016 | r = link_drop_addresses(link); |
2017 | if (r < 0) | |
2018 | return r; | |
3104883d | 2019 | |
59048336 YW |
2020 | r = link_drop_neighbors(link); |
2021 | if (r < 0) | |
2022 | return r; | |
d1bdafd2 | 2023 | |
62f0ea5f YW |
2024 | r = link_drop_routes(link); |
2025 | if (r < 0) | |
2026 | return r; | |
3104883d | 2027 | |
c69305ff LP |
2028 | ndisc_flush(link); |
2029 | ||
3104883d SS |
2030 | return 0; |
2031 | } | |
2032 | ||
256c75fd | 2033 | int link_configure(Link *link) { |
4ecdcb07 YW |
2034 | int r; |
2035 | ||
ef1ba606 | 2036 | assert(link); |
b22d8a00 | 2037 | assert(link->network); |
bd08ce56 | 2038 | assert(link->state == LINK_STATE_INITIALIZED); |
a748b692 | 2039 | |
34658df2 | 2040 | r = link_configure_traffic_control(link); |
4ecdcb07 YW |
2041 | if (r < 0) |
2042 | return r; | |
2043 | ||
518cd6b5 SS |
2044 | r = link_configure_sr_iov(link); |
2045 | if (r < 0) | |
2046 | return r; | |
2047 | ||
500b96eb | 2048 | if (link->iftype == ARPHRD_CAN) |
93ea7750 | 2049 | return link_configure_can(link); |
92c918b0 | 2050 | |
5e0534f1 | 2051 | r = link_set_sysctl(link); |
b69c3180 SS |
2052 | if (r < 0) |
2053 | return r; | |
8749cbcd | 2054 | |
5e0534f1 | 2055 | r = link_set_ipv6_proxy_ndp_addresses(link); |
94d76d07 YW |
2056 | if (r < 0) |
2057 | return r; | |
2058 | ||
e16e4b3b DS |
2059 | r = link_set_mac(link); |
2060 | if (r < 0) | |
2061 | return r; | |
2062 | ||
2063 | r = link_set_nomaster(link); | |
2064 | if (r < 0) | |
2065 | return r; | |
2066 | ||
99d2baa2 SS |
2067 | r = link_set_flags(link); |
2068 | if (r < 0) | |
2069 | return r; | |
2070 | ||
89fe6535 SS |
2071 | r = link_set_group(link); |
2072 | if (r < 0) | |
2073 | return r; | |
2074 | ||
2ffd6d73 YW |
2075 | r = ipv4ll_configure(link); |
2076 | if (r < 0) | |
2077 | return r; | |
64b21ece | 2078 | |
2ffd6d73 YW |
2079 | r = dhcp4_configure(link); |
2080 | if (r < 0) | |
2081 | return r; | |
eb34d4af | 2082 | |
2ffd6d73 YW |
2083 | r = dhcp6_configure(link); |
2084 | if (r < 0) | |
2085 | return r; | |
f5a8c43f | 2086 | |
2ffd6d73 YW |
2087 | r = ndisc_configure(link); |
2088 | if (r < 0) | |
2089 | return r; | |
4138fb2c | 2090 | |
086b8853 YW |
2091 | r = radv_configure(link); |
2092 | if (r < 0) | |
2093 | return r; | |
7465dd22 | 2094 | |
2ffd6d73 YW |
2095 | r = link_lldp_rx_configure(link); |
2096 | if (r < 0) | |
2097 | return r; | |
ce43e484 | 2098 | |
f6fcc1c2 | 2099 | r = link_configure_mtu(link); |
40288ece YW |
2100 | if (r < 0) |
2101 | return r; | |
44b598a1 | 2102 | |
9f6e82e6 YW |
2103 | r = link_configure_addrgen_mode(link); |
2104 | if (r < 0) | |
2105 | return r; | |
0e2fdb83 | 2106 | |
3a390124 DS |
2107 | return link_configure_continue(link); |
2108 | } | |
2109 | ||
2110 | /* The configuration continues in this separate function, instead of | |
2111 | * including this in the above link_configure() function, for two | |
2112 | * reasons: | |
2113 | * 1) some devices reset the link when the mtu is set, which caused | |
2114 | * an infinite loop here in networkd; see: | |
2115 | * https://github.com/systemd/systemd/issues/6593 | |
2116 | * https://github.com/systemd/systemd/issues/9831 | |
2117 | * 2) if ipv6ll is disabled, then bringing the interface up must be | |
2118 | * delayed until after we get confirmation from the kernel that | |
2119 | * the addr_gen_mode parameter has been set (via netlink), see: | |
2120 | * https://github.com/systemd/systemd/issues/13882 | |
2121 | */ | |
2122 | static int link_configure_continue(Link *link) { | |
55dc8c4a YW |
2123 | int r; |
2124 | ||
2125 | assert(link); | |
2126 | assert(link->network); | |
bd08ce56 | 2127 | assert(link->state == LINK_STATE_INITIALIZED); |
55dc8c4a | 2128 | |
9524014e | 2129 | if (link->setting_mtu || link->setting_genmode) |
55dc8c4a YW |
2130 | return 0; |
2131 | ||
4c649652 DS |
2132 | /* Drop foreign config, but ignore loopback or critical devices. |
2133 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2134 | if (!(link->flags & IFF_LOOPBACK) && | |
2135 | link->network->keep_configuration != KEEP_CONFIGURATION_YES) { | |
2136 | r = link_drop_foreign_config(link); | |
2137 | if (r < 0) | |
2138 | return r; | |
2139 | } | |
2140 | ||
d236718c DS |
2141 | /* The kernel resets ipv6 mtu after changing device mtu; |
2142 | * we must set this here, after we've set device mtu */ | |
2143 | r = link_set_ipv6_mtu(link); | |
2144 | if (r < 0) | |
5e0534f1 | 2145 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface, ignoring: %m"); |
d236718c | 2146 | |
3f265037 | 2147 | return link_enter_join_netdev(link); |
505f8da7 TG |
2148 | } |
2149 | ||
572b21d9 | 2150 | static int link_reconfigure_internal(Link *link, sd_netlink_message *m, bool force) { |
ad932b15 YW |
2151 | Network *network; |
2152 | int r; | |
2153 | ||
572b21d9 YW |
2154 | if (m) { |
2155 | _cleanup_strv_free_ char **s = NULL; | |
2156 | ||
2157 | r = sd_netlink_message_get_errno(m); | |
2158 | if (r < 0) | |
2159 | return r; | |
2160 | ||
2161 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s); | |
2162 | if (r < 0 && r != -ENODATA) | |
2163 | return r; | |
2164 | ||
2165 | strv_free_and_replace(link->alternative_names, s); | |
2166 | } | |
2167 | ||
c643bda5 YW |
2168 | r = network_get(link->manager, link->iftype, link->sd_device, |
2169 | link->ifname, link->alternative_names, link->driver, | |
b8162cd2 | 2170 | &link->hw_addr.addr.ether, &link->permanent_mac, |
c643bda5 | 2171 | link->wlan_iftype, link->ssid, &link->bssid, &network); |
ad932b15 YW |
2172 | if (r == -ENOENT) { |
2173 | link_enter_unmanaged(link); | |
2174 | return 0; | |
2175 | } else if (r == 0 && network->unmanaged) { | |
2176 | link_enter_unmanaged(link); | |
2177 | return 0; | |
2178 | } else if (r < 0) | |
2179 | return r; | |
2180 | ||
99b8517c | 2181 | if (link->network == network && !force) |
ad932b15 YW |
2182 | return 0; |
2183 | ||
2184 | log_link_info(link, "Re-configuring with %s", network->filename); | |
2185 | ||
2186 | /* Dropping old .network file */ | |
2a99eed0 | 2187 | r = link_stop_engines(link, false); |
572b21d9 | 2188 | if (r < 0) |
ad932b15 | 2189 | return r; |
ad932b15 | 2190 | |
ad932b15 YW |
2191 | r = link_drop_config(link); |
2192 | if (r < 0) | |
2193 | return r; | |
2194 | ||
2c7b826d | 2195 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) { |
ad932b15 YW |
2196 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); |
2197 | r = link_drop_foreign_config(link); | |
2198 | if (r < 0) | |
2199 | return r; | |
2200 | } | |
2201 | ||
2202 | link_free_carrier_maps(link); | |
2203 | link_free_engines(link); | |
2204 | link->network = network_unref(link->network); | |
2205 | ||
2206 | /* Then, apply new .network file */ | |
2207 | r = network_apply(network, link); | |
2208 | if (r < 0) | |
2209 | return r; | |
2210 | ||
2211 | r = link_new_carrier_maps(link); | |
2212 | if (r < 0) | |
2213 | return r; | |
2214 | ||
2215 | link_set_state(link, LINK_STATE_INITIALIZED); | |
8ae7b8a1 | 2216 | link_dirty(link); |
ad932b15 YW |
2217 | |
2218 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, | |
2219 | * link_configure() is called later asynchronously. */ | |
2220 | r = link_configure_duid(link); | |
2221 | if (r <= 0) | |
2222 | return r; | |
2223 | ||
2224 | r = link_configure(link); | |
2225 | if (r < 0) | |
2226 | return r; | |
2227 | ||
2228 | return 0; | |
2229 | } | |
2230 | ||
572b21d9 YW |
2231 | static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
2232 | int r; | |
2233 | ||
2234 | r = link_reconfigure_internal(link, m, false); | |
2235 | if (r < 0) | |
2236 | link_enter_failed(link); | |
2237 | ||
2238 | return 1; | |
2239 | } | |
2240 | ||
2241 | static int link_force_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
2242 | int r; | |
2243 | ||
2244 | r = link_reconfigure_internal(link, m, true); | |
2245 | if (r < 0) | |
2246 | link_enter_failed(link); | |
2247 | ||
2248 | return 1; | |
2249 | } | |
2250 | ||
2251 | int link_reconfigure(Link *link, bool force) { | |
2252 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
2253 | int r; | |
2254 | ||
2c0d7ed3 YW |
2255 | if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER)) |
2256 | return 0; | |
2257 | ||
572b21d9 YW |
2258 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2259 | link->ifindex); | |
2260 | if (r < 0) | |
2261 | return r; | |
2262 | ||
2263 | r = netlink_call_async(link->manager->rtnl, NULL, req, | |
2264 | force ? link_force_reconfigure_handler : link_reconfigure_handler, | |
2265 | link_netlink_destroy_callback, link); | |
2266 | if (r < 0) | |
2267 | return r; | |
2268 | ||
2269 | link_ref(link); | |
2270 | ||
2271 | return 0; | |
2272 | } | |
2273 | ||
e6bf7774 | 2274 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 2275 | Network *network; |
505f8da7 TG |
2276 | int r; |
2277 | ||
2278 | assert(link); | |
2279 | assert(link->ifname); | |
2280 | assert(link->manager); | |
2281 | ||
bd08ce56 YW |
2282 | /* We may get called either from the asynchronous netlink callback, |
2283 | * or directly for link_add() if running in a container. See link_add(). */ | |
2284 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) | |
4232cf04 | 2285 | return 0; |
505f8da7 | 2286 | |
6a7a4e4d | 2287 | log_link_debug(link, "Link state is up-to-date"); |
bd08ce56 | 2288 | link_set_state(link, LINK_STATE_INITIALIZED); |
505f8da7 | 2289 | |
0d4ad91d AR |
2290 | r = link_new_bound_by_list(link); |
2291 | if (r < 0) | |
2292 | return r; | |
2293 | ||
2294 | r = link_handle_bound_by_list(link); | |
2295 | if (r < 0) | |
2296 | return r; | |
2297 | ||
c4a03a56 | 2298 | if (!link->network) { |
0894cfe4 | 2299 | r = wifi_get_info(link); |
8d968fdd YW |
2300 | if (r < 0) |
2301 | return r; | |
2302 | ||
c643bda5 YW |
2303 | r = network_get(link->manager, link->iftype, link->sd_device, |
2304 | link->ifname, link->alternative_names, link->driver, | |
b8162cd2 | 2305 | &link->hw_addr.addr.ether, &link->permanent_mac, |
c643bda5 | 2306 | link->wlan_iftype, link->ssid, &link->bssid, &network); |
c4a03a56 TG |
2307 | if (r == -ENOENT) { |
2308 | link_enter_unmanaged(link); | |
4232cf04 | 2309 | return 0; |
a09dc546 DM |
2310 | } else if (r == 0 && network->unmanaged) { |
2311 | link_enter_unmanaged(link); | |
2312 | return 0; | |
c4a03a56 TG |
2313 | } else if (r < 0) |
2314 | return r; | |
505f8da7 | 2315 | |
c4a03a56 TG |
2316 | if (link->flags & IFF_LOOPBACK) { |
2317 | if (network->link_local != ADDRESS_FAMILY_NO) | |
2318 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 2319 | |
c4a03a56 TG |
2320 | if (network->dhcp != ADDRESS_FAMILY_NO) |
2321 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 2322 | |
c4a03a56 TG |
2323 | if (network->dhcp_server) |
2324 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
2325 | } | |
bd2efe92 | 2326 | |
7d342c03 | 2327 | r = network_apply(network, link); |
c4a03a56 TG |
2328 | if (r < 0) |
2329 | return r; | |
2330 | } | |
505f8da7 | 2331 | |
0d4ad91d AR |
2332 | r = link_new_bound_to_list(link); |
2333 | if (r < 0) | |
2334 | return r; | |
2335 | ||
27dfc982 YW |
2336 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, |
2337 | * link_configure() is called later asynchronously. */ | |
2338 | r = link_configure_duid(link); | |
2339 | if (r <= 0) | |
2340 | return r; | |
2341 | ||
a748b692 TG |
2342 | r = link_configure(link); |
2343 | if (r < 0) | |
2344 | return r; | |
2345 | ||
4232cf04 | 2346 | return 0; |
505f8da7 TG |
2347 | } |
2348 | ||
302a796f | 2349 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
572b21d9 | 2350 | _cleanup_strv_free_ char **s = NULL; |
4ff296b0 YW |
2351 | int r; |
2352 | ||
572b21d9 YW |
2353 | r = sd_netlink_message_get_errno(m); |
2354 | if (r < 0) { | |
40681e5c | 2355 | log_link_warning_errno(link, r, "Failed to wait for the interface to be initialized: %m"); |
572b21d9 YW |
2356 | link_enter_failed(link); |
2357 | return 0; | |
2358 | } | |
2359 | ||
2360 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s); | |
2361 | if (r < 0 && r != -ENODATA) { | |
2362 | link_enter_failed(link); | |
2363 | return 0; | |
2364 | } | |
2365 | ||
2366 | strv_free_and_replace(link->alternative_names, s); | |
2367 | ||
4ff296b0 YW |
2368 | r = link_initialized_and_synced(link); |
2369 | if (r < 0) | |
2370 | link_enter_failed(link); | |
e6bf7774 YW |
2371 | return 1; |
2372 | } | |
2373 | ||
51517f9e | 2374 | int link_initialized(Link *link, sd_device *device) { |
4afd3348 | 2375 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
2376 | int r; |
2377 | ||
2378 | assert(link); | |
2379 | assert(link->manager); | |
2380 | assert(link->manager->rtnl); | |
2381 | assert(device); | |
2382 | ||
8434fd5c | 2383 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
2384 | return 0; |
2385 | ||
51517f9e | 2386 | if (link->sd_device) |
679b3605 TG |
2387 | return 0; |
2388 | ||
79008bdd | 2389 | log_link_debug(link, "udev initialized link"); |
bd08ce56 | 2390 | link_set_state(link, LINK_STATE_INITIALIZED); |
4f561e8e | 2391 | |
51517f9e | 2392 | link->sd_device = sd_device_ref(device); |
4f561e8e | 2393 | |
3c9b8860 TG |
2394 | /* udev has initialized the link, but we don't know if we have yet |
2395 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
2396 | * when it returns we know that the pending NEWLINKs have already been | |
2397 | * processed and that we are up-to-date */ | |
4f561e8e | 2398 | |
3c9b8860 TG |
2399 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2400 | link->ifindex); | |
4f561e8e TG |
2401 | if (r < 0) |
2402 | return r; | |
2403 | ||
302a796f YW |
2404 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler, |
2405 | link_netlink_destroy_callback, link); | |
4f561e8e TG |
2406 | if (r < 0) |
2407 | return r; | |
2408 | ||
5da8149f TG |
2409 | link_ref(link); |
2410 | ||
4f561e8e TG |
2411 | return 0; |
2412 | } | |
2413 | ||
c4a03a56 | 2414 | static int link_load(Link *link) { |
0bc70f1d TG |
2415 | _cleanup_free_ char *network_file = NULL, |
2416 | *addresses = NULL, | |
f703cc2c | 2417 | *routes = NULL, |
0bc70f1d TG |
2418 | *dhcp4_address = NULL, |
2419 | *ipv4ll_address = NULL; | |
c4a03a56 TG |
2420 | int r; |
2421 | ||
2422 | assert(link); | |
2423 | ||
aa8fbc74 | 2424 | r = parse_env_file(NULL, link->state_file, |
c4a03a56 TG |
2425 | "NETWORK_FILE", &network_file, |
2426 | "ADDRESSES", &addresses, | |
f703cc2c | 2427 | "ROUTES", &routes, |
0bc70f1d | 2428 | "DHCP4_ADDRESS", &dhcp4_address, |
13df9c39 | 2429 | "IPV4LL_ADDRESS", &ipv4ll_address); |
c4a03a56 TG |
2430 | if (r < 0 && r != -ENOENT) |
2431 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
2432 | ||
2433 | if (network_file) { | |
2434 | Network *network; | |
2435 | char *suffix; | |
2436 | ||
2437 | /* drop suffix */ | |
2438 | suffix = strrchr(network_file, '.'); | |
2439 | if (!suffix) { | |
2440 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
2441 | goto network_file_fail; | |
2442 | } | |
2443 | *suffix = '\0'; | |
2444 | ||
2445 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
2446 | if (r < 0) { | |
2447 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
2448 | goto network_file_fail; | |
2449 | } | |
2450 | ||
7d342c03 | 2451 | r = network_apply(network, link); |
c4a03a56 TG |
2452 | if (r < 0) |
2453 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
2454 | } | |
2455 | ||
2456 | network_file_fail: | |
2457 | ||
da4d3a61 YW |
2458 | r = link_deserialize_addresses(link, addresses); |
2459 | if (r < 0) | |
2460 | log_link_warning_errno(link, r, "Failed to load addresses from %s, ignoring: %m", link->state_file); | |
74544b4e | 2461 | |
731ff05b YW |
2462 | r = link_deserialize_routes(link, routes); |
2463 | if (r < 0) | |
2464 | log_link_warning_errno(link, r, "Failed to load routes from %s, ignoring: %m", link->state_file); | |
f703cc2c | 2465 | |
571eeba9 YW |
2466 | r = link_deserialize_dhcp4(link, dhcp4_address); |
2467 | if (r < 0) | |
2468 | log_link_warning_errno(link, r, "Failed to load DHCPv4 address from %s, ignoring: %m", link->state_file); | |
0bc70f1d | 2469 | |
daad60d1 YW |
2470 | r = link_deserialize_ipv4ll(link, ipv4ll_address); |
2471 | if (r < 0) | |
2472 | log_link_warning_errno(link, r, "Failed to load IPv4LL address from %s, ignoring: %m", link->state_file); | |
0bc70f1d | 2473 | |
c4a03a56 TG |
2474 | return 0; |
2475 | } | |
2476 | ||
1c4baffc | 2477 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
51517f9e | 2478 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
505f8da7 | 2479 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
51517f9e | 2480 | Link *link; |
5a937ea2 | 2481 | int r; |
505f8da7 TG |
2482 | |
2483 | assert(m); | |
fbbeb65a | 2484 | assert(m->rtnl); |
505f8da7 TG |
2485 | assert(message); |
2486 | assert(ret); | |
2487 | ||
2488 | r = link_new(m, message, ret); | |
2489 | if (r < 0) | |
2490 | return r; | |
2491 | ||
2492 | link = *ret; | |
2493 | ||
6a7a4e4d | 2494 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 2495 | |
c4a03a56 TG |
2496 | r = link_load(link); |
2497 | if (r < 0) | |
2498 | return r; | |
2499 | ||
bf331d87 YW |
2500 | if (path_is_read_only_fs("/sys") <= 0) { |
2501 | /* udev should be around */ | |
ae06ab10 | 2502 | sprintf(ifindex_str, "n%d", link->ifindex); |
51517f9e YW |
2503 | r = sd_device_new_from_device_id(&device, ifindex_str); |
2504 | if (r < 0) { | |
4d59e14f YW |
2505 | log_link_warning_errno(link, r, "Could not find device, waiting for device initialization: %m"); |
2506 | return 0; | |
5c416fc4 | 2507 | } |
505f8da7 | 2508 | |
5a937ea2 | 2509 | r = sd_device_get_is_initialized(device); |
51517f9e | 2510 | if (r < 0) { |
bf331d87 | 2511 | log_link_warning_errno(link, r, "Could not determine whether the device is initialized: %m"); |
51517f9e YW |
2512 | goto failed; |
2513 | } | |
5a937ea2 | 2514 | if (r == 0) { |
505f8da7 | 2515 | /* not yet ready */ |
79008bdd | 2516 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 2517 | return 0; |
3c4cb064 | 2518 | } |
505f8da7 | 2519 | |
299ad32d YW |
2520 | r = device_is_renaming(device); |
2521 | if (r < 0) { | |
bf331d87 | 2522 | log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m"); |
299ad32d YW |
2523 | goto failed; |
2524 | } | |
2525 | if (r > 0) { | |
bf331d87 | 2526 | log_link_debug(link, "Interface is being renamed, pending initialization."); |
299ad32d YW |
2527 | return 0; |
2528 | } | |
2529 | ||
4f561e8e TG |
2530 | r = link_initialized(link, device); |
2531 | if (r < 0) | |
5c416fc4 | 2532 | goto failed; |
4f561e8e | 2533 | } else { |
e6bf7774 | 2534 | r = link_initialized_and_synced(link); |
4f561e8e | 2535 | if (r < 0) |
5c416fc4 | 2536 | goto failed; |
4f561e8e | 2537 | } |
505f8da7 | 2538 | |
a748b692 | 2539 | return 0; |
5c416fc4 TG |
2540 | failed: |
2541 | link_enter_failed(link); | |
2542 | return r; | |
a748b692 TG |
2543 | } |
2544 | ||
c601ebf7 | 2545 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
2546 | int r; |
2547 | ||
2548 | assert(link); | |
2549 | ||
2550 | log_link_info(link, "Gained IPv6LL"); | |
2551 | ||
c601ebf7 | 2552 | link->ipv6ll_address = *address; |
e7ab854c TG |
2553 | link_check_ready(link); |
2554 | ||
b9ea3d2e | 2555 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
e7ab854c TG |
2556 | r = link_acquire_ipv6_conf(link); |
2557 | if (r < 0) { | |
2558 | link_enter_failed(link); | |
2559 | return r; | |
2560 | } | |
2561 | } | |
2562 | ||
2563 | return 0; | |
2564 | } | |
2565 | ||
9c0a72f9 TG |
2566 | static int link_carrier_gained(Link *link) { |
2567 | int r; | |
2568 | ||
2569 | assert(link); | |
2570 | ||
0894cfe4 | 2571 | r = wifi_get_info(link); |
8d968fdd YW |
2572 | if (r < 0) |
2573 | return r; | |
2574 | if (r > 0) { | |
572b21d9 YW |
2575 | r = link_reconfigure_internal(link, NULL, false); |
2576 | if (r < 0) { | |
2577 | link_enter_failed(link); | |
8d968fdd | 2578 | return r; |
572b21d9 | 2579 | } |
8d968fdd YW |
2580 | } |
2581 | ||
b9ea3d2e | 2582 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
9c0a72f9 TG |
2583 | r = link_acquire_conf(link); |
2584 | if (r < 0) { | |
2585 | link_enter_failed(link); | |
2586 | return r; | |
2587 | } | |
6fc25497 | 2588 | |
b425c3ae | 2589 | link_set_state(link, LINK_STATE_CONFIGURING); |
682c65b0 | 2590 | r = link_set_static_configs(link); |
6fc25497 SS |
2591 | if (r < 0) |
2592 | return r; | |
9c0a72f9 TG |
2593 | } |
2594 | ||
0d4ad91d AR |
2595 | r = link_handle_bound_by_list(link); |
2596 | if (r < 0) | |
2597 | return r; | |
2598 | ||
1f241589 YW |
2599 | if (!link->bridge_mdb_configured) { |
2600 | r = link_set_bridge_mdb(link); | |
2601 | if (r < 0) | |
2602 | return r; | |
2603 | } | |
2604 | ||
2605 | if (streq_ptr(link->kind, "bridge")) { | |
2606 | Link *slave; | |
2607 | ||
2608 | SET_FOREACH(slave, link->slaves) { | |
2609 | if (slave->bridge_mdb_configured) | |
2610 | continue; | |
2611 | ||
2612 | r = link_set_bridge_mdb(slave); | |
2613 | if (r < 0) | |
2614 | link_enter_failed(slave); | |
2615 | } | |
2616 | } | |
3db468ea | 2617 | |
9c0a72f9 TG |
2618 | return 0; |
2619 | } | |
2620 | ||
2621 | static int link_carrier_lost(Link *link) { | |
2622 | int r; | |
2623 | ||
2624 | assert(link); | |
2625 | ||
25e992ba | 2626 | if (link->network && link->network->ignore_carrier_loss) |
93b4dab5 SS |
2627 | return 0; |
2628 | ||
fbdb6605 | 2629 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
55dc8c4a YW |
2630 | * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the |
2631 | * configuration and stop the clients as well. */ | |
97e7fb39 SS |
2632 | if (link->setting_mtu) |
2633 | return 0; | |
2634 | ||
2a99eed0 | 2635 | r = link_stop_engines(link, false); |
9c0a72f9 TG |
2636 | if (r < 0) { |
2637 | link_enter_failed(link); | |
2638 | return r; | |
2639 | } | |
2640 | ||
3104883d SS |
2641 | r = link_drop_config(link); |
2642 | if (r < 0) | |
2643 | return r; | |
2644 | ||
2c7b826d | 2645 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) { |
f258e948 | 2646 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); |
c436d553 MM |
2647 | r = link_drop_foreign_config(link); |
2648 | if (r < 0) | |
2649 | return r; | |
2650 | } | |
3104883d | 2651 | |
0d4ad91d AR |
2652 | r = link_handle_bound_by_list(link); |
2653 | if (r < 0) | |
2654 | return r; | |
2655 | ||
9c0a72f9 TG |
2656 | return 0; |
2657 | } | |
2658 | ||
2659 | int link_carrier_reset(Link *link) { | |
2660 | int r; | |
2661 | ||
2662 | assert(link); | |
2663 | ||
2664 | if (link_has_carrier(link)) { | |
2665 | r = link_carrier_lost(link); | |
2666 | if (r < 0) | |
2667 | return r; | |
2668 | ||
2669 | r = link_carrier_gained(link); | |
2670 | if (r < 0) | |
2671 | return r; | |
2672 | ||
6a7a4e4d | 2673 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
2674 | } |
2675 | ||
2676 | return 0; | |
2677 | } | |
2678 | ||
d236718c DS |
2679 | /* This is called every time an interface admin state changes to up; |
2680 | * specifically, when IFF_UP flag changes from unset to set */ | |
2681 | static int link_admin_state_up(Link *link) { | |
2682 | int r; | |
2683 | ||
2684 | /* We set the ipv6 mtu after the device mtu, but the kernel resets | |
2685 | * ipv6 mtu on NETDEV_UP, so we need to reset it. The check for | |
2686 | * ipv6_mtu_set prevents this from trying to set it too early before | |
2687 | * the link->network has been setup; we only need to reset it | |
2688 | * here if we've already set it during normal initialization. */ | |
2689 | if (link->ipv6_mtu_set) { | |
2690 | r = link_set_ipv6_mtu(link); | |
2691 | if (r < 0) | |
2692 | return r; | |
2693 | } | |
2694 | ||
2695 | return 0; | |
2696 | } | |
2697 | ||
1c4baffc | 2698 | int link_update(Link *link, sd_netlink_message *m) { |
572b21d9 | 2699 | _cleanup_strv_free_ char **s = NULL; |
b8162cd2 | 2700 | hw_addr_data hw_addr; |
ca4e095a | 2701 | const char *ifname; |
afe7fd56 | 2702 | uint32_t mtu; |
d236718c | 2703 | bool had_carrier, carrier_gained, carrier_lost, link_was_admin_up; |
bb262ef0 | 2704 | int old_master, r; |
22936833 | 2705 | |
dd3efc09 | 2706 | assert(link); |
b8941f74 | 2707 | assert(link->ifname); |
22936833 TG |
2708 | assert(m); |
2709 | ||
7619683b | 2710 | if (link->state == LINK_STATE_LINGER) { |
5238e957 | 2711 | log_link_info(link, "Link re-added"); |
289e6774 | 2712 | link_set_state(link, LINK_STATE_CONFIGURING); |
0d4ad91d AR |
2713 | |
2714 | r = link_new_carrier_maps(link); | |
2715 | if (r < 0) | |
2716 | return r; | |
7619683b TG |
2717 | } |
2718 | ||
1c4baffc | 2719 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 2720 | if (r >= 0 && !streq(ifname, link->ifname)) { |
30de2b89 | 2721 | Manager *manager = link->manager; |
b8941f74 | 2722 | |
30de2b89 | 2723 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
0d4ad91d | 2724 | |
30de2b89 YW |
2725 | link_drop(link); |
2726 | r = link_add(manager, m, &link); | |
2727 | if (r < 0) | |
2728 | return r; | |
b8941f74 TG |
2729 | } |
2730 | ||
572b21d9 YW |
2731 | r = sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &s); |
2732 | if (r >= 0) | |
2733 | strv_free_and_replace(link->alternative_names, s); | |
2734 | ||
1c4baffc | 2735 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
2736 | if (r >= 0 && mtu > 0) { |
2737 | link->mtu = mtu; | |
4e964aa0 | 2738 | if (link->original_mtu == 0) { |
afe7fd56 | 2739 | link->original_mtu = mtu; |
6a7a4e4d | 2740 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
2741 | } |
2742 | ||
2743 | if (link->dhcp_client) { | |
3c9b8860 TG |
2744 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
2745 | link->mtu); | |
fc95c359 YW |
2746 | if (r < 0) |
2747 | return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); | |
afe7fd56 | 2748 | } |
7465dd22 PF |
2749 | |
2750 | if (link->radv) { | |
2751 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
2752 | if (r < 0) | |
2753 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
2754 | } | |
9842de0d | 2755 | } |
69629de9 | 2756 | |
e9189a1f TG |
2757 | /* The kernel may broadcast NEWLINK messages without the MAC address |
2758 | set, simply ignore them. */ | |
b8162cd2 TR |
2759 | r = netlink_message_read_hw_addr(m, IFLA_ADDRESS, &hw_addr); |
2760 | if (r >= 0 && (link->hw_addr.length != hw_addr.length || | |
2761 | memcmp(link->hw_addr.addr.bytes, hw_addr.addr.bytes, hw_addr.length) != 0)) { | |
807667f7 | 2762 | |
b8162cd2 | 2763 | memcpy(link->hw_addr.addr.bytes, hw_addr.addr.bytes, hw_addr.length); |
807667f7 | 2764 | |
b8162cd2 | 2765 | log_link_debug(link, "Gained new hardware address: %s", HW_ADDR_TO_STR(&hw_addr)); |
807667f7 | 2766 | |
a3adb4a6 YW |
2767 | r = ipv4ll_update_mac(link); |
2768 | if (r < 0) | |
2769 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
807667f7 | 2770 | |
d947f7f9 YW |
2771 | r = dhcp4_update_mac(link); |
2772 | if (r < 0) | |
2773 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 2774 | |
eebba6dc YW |
2775 | r = dhcp6_update_mac(link); |
2776 | if (r < 0) | |
2777 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
7465dd22 | 2778 | |
d0c4275c | 2779 | r = radv_update_mac(link); |
be9363cc YW |
2780 | if (r < 0) |
2781 | return log_link_warning_errno(link, r, "Could not update MAC address for Router Advertisement: %m"); | |
807667f7 YW |
2782 | |
2783 | if (link->ndisc) { | |
b8162cd2 | 2784 | r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.addr.ether); |
807667f7 YW |
2785 | if (r < 0) |
2786 | return log_link_warning_errno(link, r, "Could not update MAC for NDisc: %m"); | |
2787 | } | |
d93d655c YW |
2788 | |
2789 | r = ipv4_dad_update_mac(link); | |
2790 | if (r < 0) | |
2791 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m"); | |
4f882b2a TG |
2792 | } |
2793 | ||
bb262ef0 YW |
2794 | old_master = link->master_ifindex; |
2795 | (void) sd_netlink_message_read_u32(m, IFLA_MASTER, (uint32_t *) &link->master_ifindex); | |
2796 | ||
d236718c | 2797 | link_was_admin_up = link->flags & IFF_UP; |
a61bb41c TG |
2798 | had_carrier = link_has_carrier(link); |
2799 | ||
bb262ef0 | 2800 | r = link_update_flags(link, m, old_master != link->master_ifindex); |
a61bb41c TG |
2801 | if (r < 0) |
2802 | return r; | |
2803 | ||
d236718c DS |
2804 | if (!link_was_admin_up && (link->flags & IFF_UP)) { |
2805 | log_link_info(link, "Link UP"); | |
2806 | ||
2807 | r = link_admin_state_up(link); | |
2808 | if (r < 0) | |
2809 | return r; | |
2810 | } else if (link_was_admin_up && !(link->flags & IFF_UP)) | |
2811 | log_link_info(link, "Link DOWN"); | |
2812 | ||
273eec24 LP |
2813 | r = link_update_lldp(link); |
2814 | if (r < 0) | |
2815 | return r; | |
2816 | ||
a61bb41c TG |
2817 | carrier_gained = !had_carrier && link_has_carrier(link); |
2818 | carrier_lost = had_carrier && !link_has_carrier(link); | |
2819 | ||
2820 | if (carrier_gained) { | |
6a7a4e4d | 2821 | log_link_info(link, "Gained carrier"); |
a61bb41c | 2822 | |
9c0a72f9 TG |
2823 | r = link_carrier_gained(link); |
2824 | if (r < 0) | |
2825 | return r; | |
a61bb41c | 2826 | } else if (carrier_lost) { |
6a7a4e4d | 2827 | log_link_info(link, "Lost carrier"); |
a61bb41c | 2828 | |
9c0a72f9 TG |
2829 | r = link_carrier_lost(link); |
2830 | if (r < 0) | |
a61bb41c | 2831 | return r; |
a61bb41c TG |
2832 | } |
2833 | ||
2834 | return 0; | |
dd3efc09 | 2835 | } |
fe8db0c5 | 2836 | |
b295beea LP |
2837 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
2838 | bool space = false; | |
b295beea LP |
2839 | Link *link; |
2840 | ||
2841 | assert(f); | |
2842 | assert(prefix); | |
2843 | ||
2844 | if (hashmap_isempty(h)) | |
2845 | return; | |
2846 | ||
0d536673 | 2847 | fputs(prefix, f); |
90e74a66 | 2848 | HASHMAP_FOREACH(link, h) { |
b295beea | 2849 | if (space) |
0d536673 | 2850 | fputc(' ', f); |
b295beea LP |
2851 | |
2852 | fprintf(f, "%i", link->ifindex); | |
2853 | space = true; | |
2854 | } | |
2855 | ||
0d536673 | 2856 | fputc('\n', f); |
b295beea LP |
2857 | } |
2858 | ||
64581765 | 2859 | static void link_save_dns(Link *link, FILE *f, struct in_addr_full **dns, unsigned n_dns, bool *space) { |
dddc8d1e | 2860 | for (unsigned j = 0; j < n_dns; j++) { |
e77bd3fd | 2861 | const char *str; |
15761549 | 2862 | |
64581765 YW |
2863 | if (dns[j]->ifindex != 0 && dns[j]->ifindex != link->ifindex) |
2864 | continue; | |
2865 | ||
e77bd3fd YW |
2866 | str = in_addr_full_to_string(dns[j]); |
2867 | if (!str) | |
15761549 | 2868 | continue; |
15761549 YW |
2869 | |
2870 | if (*space) | |
2871 | fputc(' ', f); | |
e77bd3fd | 2872 | fputs(str, f); |
15761549 YW |
2873 | *space = true; |
2874 | } | |
2875 | } | |
2876 | ||
d5e172d2 ZJS |
2877 | static void serialize_addresses( |
2878 | FILE *f, | |
2879 | const char *lvalue, | |
2880 | bool *space, | |
2881 | char **addresses, | |
2882 | sd_dhcp_lease *lease, | |
2883 | bool conditional, | |
ddb82ec2 | 2884 | sd_dhcp_lease_server_type what, |
d5e172d2 ZJS |
2885 | sd_dhcp6_lease *lease6, |
2886 | bool conditional6, | |
2887 | int (*lease6_get_addr)(sd_dhcp6_lease*, const struct in6_addr**), | |
2888 | int (*lease6_get_fqdn)(sd_dhcp6_lease*, char ***)) { | |
2889 | int r; | |
2890 | ||
2891 | bool _space = false; | |
2892 | if (!space) | |
2893 | space = &_space; | |
2894 | ||
2895 | if (lvalue) | |
2896 | fprintf(f, "%s=", lvalue); | |
2897 | fputstrv(f, addresses, NULL, space); | |
2898 | ||
2899 | if (lease && conditional) { | |
2900 | const struct in_addr *lease_addresses; | |
2901 | ||
2902 | r = sd_dhcp_lease_get_servers(lease, what, &lease_addresses); | |
2903 | if (r > 0) | |
d8bff5cc | 2904 | serialize_in_addrs(f, lease_addresses, r, space, in4_addr_is_non_local); |
d5e172d2 ZJS |
2905 | } |
2906 | ||
2907 | if (lease6 && conditional6 && lease6_get_addr) { | |
2908 | const struct in6_addr *in6_addrs; | |
2909 | ||
2910 | r = lease6_get_addr(lease6, &in6_addrs); | |
d8bff5cc ZJS |
2911 | if (r > 0) |
2912 | serialize_in6_addrs(f, in6_addrs, r, space); | |
d5e172d2 ZJS |
2913 | } |
2914 | ||
2915 | if (lease6 && conditional6 && lease6_get_fqdn) { | |
2916 | char **in6_hosts; | |
2917 | ||
2918 | r = lease6_get_fqdn(lease6, &in6_hosts); | |
2919 | if (r > 0) | |
2920 | fputstrv(f, in6_hosts, NULL, space); | |
2921 | } | |
2922 | ||
2923 | if (lvalue) | |
2924 | fputc('\n', f); | |
2925 | } | |
2926 | ||
fe8db0c5 | 2927 | int link_save(Link *link) { |
d361b373 | 2928 | const char *admin_state, *oper_state, *carrier_state, *address_state; |
68a8723c | 2929 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 TG |
2930 | _cleanup_fclose_ FILE *f = NULL; |
2931 | int r; | |
2932 | ||
2933 | assert(link); | |
2934 | assert(link->state_file); | |
68a8723c | 2935 | assert(link->lease_file); |
bbf7c048 TG |
2936 | assert(link->manager); |
2937 | ||
370e9930 | 2938 | if (link->state == LINK_STATE_LINGER) { |
6990fb6b | 2939 | (void) unlink(link->state_file); |
370e9930 TG |
2940 | return 0; |
2941 | } | |
2942 | ||
34437b4f LP |
2943 | link_lldp_save(link); |
2944 | ||
deb2e523 TG |
2945 | admin_state = link_state_to_string(link->state); |
2946 | assert(admin_state); | |
2947 | ||
e375dcde TG |
2948 | oper_state = link_operstate_to_string(link->operstate); |
2949 | assert(oper_state); | |
deb2e523 | 2950 | |
ac999bf0 YW |
2951 | carrier_state = link_carrier_state_to_string(link->carrier_state); |
2952 | assert(carrier_state); | |
2953 | ||
2954 | address_state = link_address_state_to_string(link->address_state); | |
2955 | assert(address_state); | |
2956 | ||
fe8db0c5 TG |
2957 | r = fopen_temporary(link->state_file, &f, &temp_path); |
2958 | if (r < 0) | |
6a7a4e4d | 2959 | goto fail; |
fe8db0c5 | 2960 | |
5512a963 | 2961 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
2962 | |
2963 | fprintf(f, | |
2964 | "# This is private data. Do not parse.\n" | |
deb2e523 | 2965 | "ADMIN_STATE=%s\n" |
ac999bf0 YW |
2966 | "OPER_STATE=%s\n" |
2967 | "CARRIER_STATE=%s\n" | |
2968 | "ADDRESS_STATE=%s\n", | |
2969 | admin_state, oper_state, carrier_state, address_state); | |
fe8db0c5 | 2970 | |
bcb7a07e | 2971 | if (link->network) { |
fe0e16db YW |
2972 | char **dhcp6_domains = NULL, **dhcp_domains = NULL; |
2973 | const char *dhcp_domainname = NULL, *p; | |
fe0e16db | 2974 | bool space; |
07bdc70d | 2975 | |
c1a38904 MTL |
2976 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
2977 | yes_no(link->network->required_for_online)); | |
2978 | ||
d5e172d2 ZJS |
2979 | LinkOperationalStateRange st = link->network->required_operstate_for_online; |
2980 | fprintf(f, "REQUIRED_OPER_STATE_FOR_ONLINE=%s%s%s\n", | |
2981 | strempty(link_operstate_to_string(st.min)), | |
2982 | st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? ":" : "", | |
2983 | st.max != LINK_OPERSTATE_RANGE_DEFAULT.max ? strempty(link_operstate_to_string(st.max)) : ""); | |
4ac77d63 | 2984 | |
adc5b2e2 TG |
2985 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
2986 | ||
d5e172d2 ZJS |
2987 | /************************************************************/ |
2988 | ||
0d536673 | 2989 | fputs("DNS=", f); |
ea352b40 | 2990 | space = false; |
15761549 | 2991 | if (link->n_dns != (unsigned) -1) |
64581765 | 2992 | link_save_dns(link, f, link->dns, link->n_dns, &space); |
15761549 | 2993 | else |
64581765 | 2994 | link_save_dns(link, f, link->network->dns, link->network->n_dns, &space); |
d5314fff | 2995 | |
d5e172d2 ZJS |
2996 | serialize_addresses(f, NULL, &space, |
2997 | NULL, | |
2998 | link->dhcp_lease, | |
2999 | link->network->dhcp_use_dns, | |
ddb82ec2 | 3000 | SD_DHCP_LEASE_DNS, |
1633c457 | 3001 | link->dhcp6_lease, |
d5e172d2 ZJS |
3002 | link->network->dhcp6_use_dns, |
3003 | sd_dhcp6_lease_get_dns, | |
3004 | NULL); | |
1e7a0e21 | 3005 | |
a8c10331 | 3006 | /* Make sure to flush out old entries before we use the NDisc data */ |
1e7a0e21 LP |
3007 | ndisc_vacuum(link); |
3008 | ||
b296797f | 3009 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
3010 | NDiscRDNSS *dd; |
3011 | ||
90e74a66 | 3012 | SET_FOREACH(dd, link->ndisc_rdnss) |
d8bff5cc | 3013 | serialize_in6_addrs(f, &dd->address, 1, &space); |
299d578f SS |
3014 | } |
3015 | ||
3016 | fputc('\n', f); | |
3017 | ||
d5e172d2 ZJS |
3018 | /************************************************************/ |
3019 | ||
3020 | serialize_addresses(f, "NTP", NULL, | |
3021 | link->ntp ?: link->network->ntp, | |
3022 | link->dhcp_lease, | |
3023 | link->network->dhcp_use_ntp, | |
ddb82ec2 | 3024 | SD_DHCP_LEASE_NTP, |
1633c457 | 3025 | link->dhcp6_lease, |
d5e172d2 ZJS |
3026 | link->network->dhcp6_use_ntp, |
3027 | sd_dhcp6_lease_get_ntp_addrs, | |
3028 | sd_dhcp6_lease_get_ntp_fqdn); | |
3029 | ||
3030 | serialize_addresses(f, "SIP", NULL, | |
2a71d57f | 3031 | NULL, |
d5e172d2 ZJS |
3032 | link->dhcp_lease, |
3033 | link->network->dhcp_use_sip, | |
ddb82ec2 | 3034 | SD_DHCP_LEASE_SIP, |
2a71d57f | 3035 | NULL, false, NULL, NULL); |
d5e172d2 ZJS |
3036 | |
3037 | /************************************************************/ | |
bd8f6538 | 3038 | |
b2a81c0b | 3039 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 3040 | if (link->dhcp_lease) { |
b2a81c0b | 3041 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
3042 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
3043 | } | |
1633c457 YW |
3044 | if (link->dhcp6_lease) |
3045 | (void) sd_dhcp6_lease_get_domains(link->dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3046 | } |
3047 | ||
fe0e16db YW |
3048 | fputs("DOMAINS=", f); |
3049 | space = false; | |
90e74a66 | 3050 | ORDERED_SET_FOREACH(p, link->search_domains ?: link->network->search_domains) |
fe0e16db | 3051 | fputs_with_space(f, p, NULL, &space); |
07bdc70d | 3052 | |
1e7a0e21 | 3053 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
1e7a0e21 LP |
3054 | if (dhcp_domainname) |
3055 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3056 | if (dhcp_domains) |
3057 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3058 | if (dhcp6_domains) |
3059 | fputstrv(f, dhcp6_domains, NULL, &space); | |
a0cd6da5 YW |
3060 | } |
3061 | ||
3062 | if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_YES) { | |
3063 | NDiscDNSSL *dd; | |
1e7a0e21 | 3064 | |
90e74a66 | 3065 | SET_FOREACH(dd, link->ndisc_dnssl) |
1e7a0e21 LP |
3066 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); |
3067 | } | |
b2a81c0b | 3068 | |
0d536673 | 3069 | fputc('\n', f); |
6192b846 | 3070 | |
d5e172d2 ZJS |
3071 | /************************************************************/ |
3072 | ||
fe0e16db YW |
3073 | fputs("ROUTE_DOMAINS=", f); |
3074 | space = false; | |
90e74a66 | 3075 | ORDERED_SET_FOREACH(p, link->route_domains ?: link->network->route_domains) |
fe0e16db | 3076 | fputs_with_space(f, p, NULL, &space); |
b2a81c0b | 3077 | |
1e7a0e21 | 3078 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
1e7a0e21 LP |
3079 | if (dhcp_domainname) |
3080 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3081 | if (dhcp_domains) |
3082 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3083 | if (dhcp6_domains) |
3084 | fputstrv(f, dhcp6_domains, NULL, &space); | |
a0cd6da5 YW |
3085 | } |
3086 | ||
3087 | if (link->network->ipv6_accept_ra_use_domains == DHCP_USE_DOMAINS_ROUTE) { | |
3088 | NDiscDNSSL *dd; | |
1e7a0e21 | 3089 | |
90e74a66 | 3090 | SET_FOREACH(dd, link->ndisc_dnssl) |
1e7a0e21 LP |
3091 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); |
3092 | } | |
b2a81c0b | 3093 | |
0d536673 | 3094 | fputc('\n', f); |
67272d15 | 3095 | |
d5e172d2 ZJS |
3096 | /************************************************************/ |
3097 | ||
3c9b8860 | 3098 | fprintf(f, "LLMNR=%s\n", |
15761549 | 3099 | resolve_support_to_string(link->llmnr >= 0 ? link->llmnr : link->network->llmnr)); |
d5e172d2 ZJS |
3100 | |
3101 | /************************************************************/ | |
3102 | ||
aaa297d4 | 3103 | fprintf(f, "MDNS=%s\n", |
15761549 | 3104 | resolve_support_to_string(link->mdns >= 0 ? link->mdns : link->network->mdns)); |
15761549 | 3105 | |
d5e172d2 ZJS |
3106 | /************************************************************/ |
3107 | ||
3108 | int dns_default_route = | |
3109 | link->dns_default_route >= 0 ? link->dns_default_route : | |
3110 | link->network->dns_default_route; | |
3111 | if (dns_default_route >= 0) | |
3112 | fprintf(f, "DNS_DEFAULT_ROUTE=%s\n", yes_no(dns_default_route)); | |
3113 | ||
3114 | /************************************************************/ | |
3115 | ||
3116 | DnsOverTlsMode dns_over_tls_mode = | |
3117 | link->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID ? link->dns_over_tls_mode : | |
3118 | link->network->dns_over_tls_mode; | |
3119 | if (dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) | |
3120 | fprintf(f, "DNS_OVER_TLS=%s\n", dns_over_tls_mode_to_string(dns_over_tls_mode)); | |
3121 | ||
3122 | /************************************************************/ | |
3123 | ||
3124 | DnssecMode dnssec_mode = | |
3125 | link->dnssec_mode != _DNSSEC_MODE_INVALID ? link->dnssec_mode : | |
3126 | link->network->dnssec_mode; | |
3127 | if (dnssec_mode != _DNSSEC_MODE_INVALID) | |
3128 | fprintf(f, "DNSSEC=%s\n", dnssec_mode_to_string(dnssec_mode)); | |
3129 | ||
3130 | /************************************************************/ | |
3131 | ||
3132 | Set *nta_anchors = link->dnssec_negative_trust_anchors; | |
3133 | if (set_isempty(nta_anchors)) | |
3134 | nta_anchors = link->network->dnssec_negative_trust_anchors; | |
3135 | ||
3136 | if (!set_isempty(nta_anchors)) { | |
8a516214 LP |
3137 | const char *n; |
3138 | ||
0d536673 | 3139 | fputs("DNSSEC_NTA=", f); |
8a516214 | 3140 | space = false; |
90e74a66 | 3141 | SET_FOREACH(n, nta_anchors) |
d390f8ef | 3142 | fputs_with_space(f, n, NULL, &space); |
0d536673 | 3143 | fputc('\n', f); |
8a516214 LP |
3144 | } |
3145 | ||
d5e172d2 ZJS |
3146 | /************************************************************/ |
3147 | ||
3b31e2b7 YW |
3148 | r = link_serialize_addresses(link, f); |
3149 | if (r < 0) | |
3150 | goto fail; | |
c1eb9872 | 3151 | |
d5e172d2 ZJS |
3152 | /************************************************************/ |
3153 | ||
56519412 YW |
3154 | r = link_serialize_routes(link, f); |
3155 | if (r < 0) | |
3156 | goto fail; | |
bcb7a07e | 3157 | } |
7374f9d8 | 3158 | |
b295beea LP |
3159 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3160 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3161 | |
8eb9058d | 3162 | if (link->dhcp_lease) { |
bd91b83e | 3163 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3164 | if (r < 0) |
c2d6bd61 | 3165 | goto fail; |
fe8db0c5 | 3166 | |
7374f9d8 | 3167 | fprintf(f, |
b0e39c82 TG |
3168 | "DHCP_LEASE=%s\n", |
3169 | link->lease_file); | |
deb2e523 | 3170 | } else |
6990fb6b | 3171 | (void) unlink(link->lease_file); |
fe8db0c5 | 3172 | |
ca21a19a YW |
3173 | r = link_serialize_ipv4ll(link, f); |
3174 | if (r < 0) | |
3175 | goto fail; | |
0bc70f1d | 3176 | |
5460bde5 YW |
3177 | r = link_serialize_dhcp6_client(link, f); |
3178 | if (r < 0) | |
3179 | goto fail; | |
18d8a2cf | 3180 | |
c2d6bd61 LP |
3181 | r = fflush_and_check(f); |
3182 | if (r < 0) | |
3183 | goto fail; | |
fe8db0c5 | 3184 | |
c2d6bd61 | 3185 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3186 | r = -errno; |
c2d6bd61 | 3187 | goto fail; |
fe8db0c5 TG |
3188 | } |
3189 | ||
c2d6bd61 | 3190 | return 0; |
dacd6cee | 3191 | |
c2d6bd61 | 3192 | fail: |
6a7a4e4d | 3193 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3194 | if (temp_path) |
3195 | (void) unlink(temp_path); | |
3196 | ||
dacd6cee | 3197 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3198 | } |
3199 | ||
84de38c5 TG |
3200 | /* The serialized state in /run is no longer up-to-date. */ |
3201 | void link_dirty(Link *link) { | |
3202 | int r; | |
3203 | ||
3204 | assert(link); | |
3205 | ||
0c241a37 SS |
3206 | /* mark manager dirty as link is dirty */ |
3207 | manager_dirty(link->manager); | |
3208 | ||
de7fef4b | 3209 | r = set_ensure_put(&link->manager->dirty_links, NULL, link); |
0c241a37 | 3210 | if (r <= 0) |
de7fef4b | 3211 | /* Ignore allocation errors and don't take another ref if the link was already dirty */ |
84de38c5 | 3212 | return; |
84de38c5 TG |
3213 | link_ref(link); |
3214 | } | |
3215 | ||
3216 | /* The serialized state in /run is up-to-date */ | |
3217 | void link_clean(Link *link) { | |
3218 | assert(link); | |
3219 | assert(link->manager); | |
3220 | ||
1046bf9b | 3221 | link_unref(set_remove(link->manager->dirty_links, link)); |
84de38c5 TG |
3222 | } |
3223 | ||
c2a65950 YW |
3224 | int link_save_and_clean(Link *link) { |
3225 | int r; | |
3226 | ||
3227 | r = link_save(link); | |
3228 | if (r < 0) | |
3229 | return r; | |
3230 | ||
3231 | link_clean(link); | |
3232 | return 0; | |
3233 | } | |
3234 | ||
fe8db0c5 | 3235 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 3236 | [LINK_STATE_PENDING] = "pending", |
bd08ce56 | 3237 | [LINK_STATE_INITIALIZED] = "initialized", |
289e6774 | 3238 | [LINK_STATE_CONFIGURING] = "configuring", |
fe8db0c5 | 3239 | [LINK_STATE_CONFIGURED] = "configured", |
57bd6899 | 3240 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 3241 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 3242 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
3243 | }; |
3244 | ||
3245 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
5ecb131d YW |
3246 | |
3247 | int log_link_message_full_errno(Link *link, sd_netlink_message *m, int level, int err, const char *msg) { | |
3248 | const char *err_msg = NULL; | |
3249 | ||
3250 | (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg); | |
d157714b YW |
3251 | return log_link_full_errno(link, level, err, |
3252 | "%s: %s%s%s%m", | |
3253 | msg, | |
3254 | strempty(err_msg), | |
3255 | err_msg && !endswith(err_msg, ".") ? "." : "", | |
3256 | err_msg ? " " : ""); | |
5ecb131d | 3257 | } |