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