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