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