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