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