]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b TG |
2 | |
3 | #include <netinet/ether.h> | |
4 | #include <linux/if.h> | |
06828bb6 | 5 | #include <linux/can/netlink.h> |
4cc7a82c | 6 | #include <unistd.h> |
0d536673 | 7 | #include <stdio_ext.h> |
f579559b | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
1346b1f0 | 10 | #include "bus-util.h" |
27dfc982 | 11 | #include "dhcp-identifier.h" |
bd91b83e | 12 | #include "dhcp-lease-internal.h" |
3ffd4af2 | 13 | #include "fd-util.h" |
cf1d700d TG |
14 | #include "fileio.h" |
15 | #include "netlink-util.h" | |
c6f7c917 | 16 | #include "network-internal.h" |
a0e5c15d | 17 | #include "networkd-ipv6-proxy-ndp.h" |
8e1ad1ea | 18 | #include "networkd-lldp-tx.h" |
23f53b99 | 19 | #include "networkd-manager.h" |
1e7a0e21 | 20 | #include "networkd-ndisc.h" |
7465dd22 | 21 | #include "networkd-radv.h" |
bce67bbe | 22 | #include "networkd-routing-policy-rule.h" |
cf1d700d TG |
23 | #include "set.h" |
24 | #include "socket-util.h" | |
15a5e950 | 25 | #include "stdio-util.h" |
8b43440b | 26 | #include "string-table.h" |
51517f9e | 27 | #include "strv.h" |
cf1d700d TG |
28 | #include "util.h" |
29 | #include "virt.h" | |
fc2f9534 | 30 | |
f24648a6 YW |
31 | DUID* link_get_duid(Link *link) { |
32 | if (link->network->duid.type != _DUID_TYPE_INVALID) | |
33 | return &link->network->duid; | |
34 | else | |
35 | return &link->manager->duid; | |
36 | } | |
37 | ||
b9d74c40 LP |
38 | static bool link_dhcp6_enabled(Link *link) { |
39 | assert(link); | |
40 | ||
fa709992 LP |
41 | if (!socket_ipv6_is_supported()) |
42 | return false; | |
43 | ||
78c958f8 TG |
44 | if (link->flags & IFF_LOOPBACK) |
45 | return false; | |
46 | ||
47 | if (!link->network) | |
48 | return false; | |
49 | ||
e0ee46f2 | 50 | return link->network->dhcp & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
51 | } |
52 | ||
b9d74c40 LP |
53 | static bool link_dhcp4_enabled(Link *link) { |
54 | assert(link); | |
55 | ||
78c958f8 TG |
56 | if (link->flags & IFF_LOOPBACK) |
57 | return false; | |
58 | ||
59 | if (!link->network) | |
60 | return false; | |
61 | ||
e0ee46f2 | 62 | return link->network->dhcp & ADDRESS_FAMILY_IPV4; |
78c958f8 TG |
63 | } |
64 | ||
b9d74c40 LP |
65 | static bool link_dhcp4_server_enabled(Link *link) { |
66 | assert(link); | |
67 | ||
78c958f8 TG |
68 | if (link->flags & IFF_LOOPBACK) |
69 | return false; | |
70 | ||
71 | if (!link->network) | |
72 | return false; | |
73 | ||
74 | return link->network->dhcp_server; | |
75 | } | |
76 | ||
b9d74c40 LP |
77 | static bool link_ipv4ll_enabled(Link *link) { |
78 | assert(link); | |
79 | ||
78c958f8 TG |
80 | if (link->flags & IFF_LOOPBACK) |
81 | return false; | |
82 | ||
83 | if (!link->network) | |
84 | return false; | |
85 | ||
a8f5bba6 JD |
86 | if (streq_ptr(link->kind, "wireguard")) |
87 | return false; | |
88 | ||
e0ee46f2 | 89 | return link->network->link_local & ADDRESS_FAMILY_IPV4; |
d0d6a4cd TG |
90 | } |
91 | ||
b9d74c40 LP |
92 | static bool link_ipv6ll_enabled(Link *link) { |
93 | assert(link); | |
94 | ||
fa709992 LP |
95 | if (!socket_ipv6_is_supported()) |
96 | return false; | |
97 | ||
d0d6a4cd TG |
98 | if (link->flags & IFF_LOOPBACK) |
99 | return false; | |
100 | ||
101 | if (!link->network) | |
102 | return false; | |
103 | ||
a8f5bba6 JD |
104 | if (streq_ptr(link->kind, "wireguard")) |
105 | return false; | |
106 | ||
e0ee46f2 | 107 | return link->network->link_local & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
108 | } |
109 | ||
439689c6 SS |
110 | static bool link_ipv6_enabled(Link *link) { |
111 | assert(link); | |
112 | ||
113 | if (!socket_ipv6_is_supported()) | |
114 | return false; | |
115 | ||
2b00a4e0 TY |
116 | if (link->network->bridge) |
117 | return false; | |
118 | ||
4cef7fe3 TY |
119 | /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */ |
120 | return link_ipv6ll_enabled(link) || network_has_static_ipv6_addresses(link->network); | |
439689c6 SS |
121 | } |
122 | ||
7465dd22 PF |
123 | static bool link_radv_enabled(Link *link) { |
124 | assert(link); | |
125 | ||
126 | if (!link_ipv6ll_enabled(link)) | |
127 | return false; | |
128 | ||
55a7c78b | 129 | return link->network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE; |
7465dd22 PF |
130 | } |
131 | ||
8e1ad1ea | 132 | static bool link_lldp_rx_enabled(Link *link) { |
b710e6b6 LP |
133 | assert(link); |
134 | ||
ce43e484 SS |
135 | if (link->flags & IFF_LOOPBACK) |
136 | return false; | |
137 | ||
b710e6b6 LP |
138 | if (link->iftype != ARPHRD_ETHER) |
139 | return false; | |
140 | ||
ce43e484 SS |
141 | if (!link->network) |
142 | return false; | |
143 | ||
764febc2 SS |
144 | /* LLDP should be handled on bridge slaves as those have a direct |
145 | * connection to their peers not on the bridge master. Linux doesn't | |
146 | * even (by default) forward lldp packets to the bridge master.*/ | |
147 | if (streq_ptr("bridge", link->kind)) | |
ce43e484 SS |
148 | return false; |
149 | ||
34437b4f | 150 | return link->network->lldp_mode != LLDP_MODE_NO; |
ce43e484 SS |
151 | } |
152 | ||
7272b25e | 153 | static bool link_lldp_emit_enabled(Link *link) { |
8e1ad1ea LP |
154 | assert(link); |
155 | ||
156 | if (link->flags & IFF_LOOPBACK) | |
157 | return false; | |
158 | ||
159 | if (link->iftype != ARPHRD_ETHER) | |
160 | return false; | |
161 | ||
162 | if (!link->network) | |
163 | return false; | |
164 | ||
7272b25e | 165 | return link->network->lldp_emit != LLDP_EMIT_NO; |
8e1ad1ea LP |
166 | } |
167 | ||
769d324c | 168 | static bool link_ipv4_forward_enabled(Link *link) { |
b9d74c40 LP |
169 | assert(link); |
170 | ||
5a8bcb67 LP |
171 | if (link->flags & IFF_LOOPBACK) |
172 | return false; | |
173 | ||
174 | if (!link->network) | |
175 | return false; | |
176 | ||
765afd5c LP |
177 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
178 | return false; | |
179 | ||
e0ee46f2 | 180 | return link->network->ip_forward & ADDRESS_FAMILY_IPV4; |
769d324c LP |
181 | } |
182 | ||
183 | static bool link_ipv6_forward_enabled(Link *link) { | |
b9d74c40 | 184 | assert(link); |
765afd5c LP |
185 | |
186 | if (!socket_ipv6_is_supported()) | |
187 | return false; | |
188 | ||
769d324c LP |
189 | if (link->flags & IFF_LOOPBACK) |
190 | return false; | |
191 | ||
192 | if (!link->network) | |
193 | return false; | |
194 | ||
765afd5c LP |
195 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
196 | return false; | |
197 | ||
e0ee46f2 | 198 | return link->network->ip_forward & ADDRESS_FAMILY_IPV6; |
5a8bcb67 LP |
199 | } |
200 | ||
23d8b221 SS |
201 | static bool link_proxy_arp_enabled(Link *link) { |
202 | assert(link); | |
203 | ||
204 | if (link->flags & IFF_LOOPBACK) | |
205 | return false; | |
206 | ||
207 | if (!link->network) | |
208 | return false; | |
209 | ||
210 | if (link->network->proxy_arp < 0) | |
211 | return false; | |
212 | ||
213 | return true; | |
214 | } | |
215 | ||
b9d74c40 LP |
216 | static bool link_ipv6_accept_ra_enabled(Link *link) { |
217 | assert(link); | |
218 | ||
fa709992 LP |
219 | if (!socket_ipv6_is_supported()) |
220 | return false; | |
221 | ||
f5a8c43f TG |
222 | if (link->flags & IFF_LOOPBACK) |
223 | return false; | |
224 | ||
225 | if (!link->network) | |
226 | return false; | |
227 | ||
702c979f SS |
228 | if (!link_ipv6ll_enabled(link)) |
229 | return false; | |
230 | ||
f5a8c43f TG |
231 | /* If unset use system default (enabled if local forwarding is disabled. |
232 | * disabled if local forwarding is enabled). | |
233 | * If set, ignore or enforce RA independent of local forwarding state. | |
234 | */ | |
235 | if (link->network->ipv6_accept_ra < 0) | |
236 | /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */ | |
237 | return !link_ipv6_forward_enabled(link); | |
238 | else if (link->network->ipv6_accept_ra > 0) | |
239 | /* accept RA even if ip_forward is enabled */ | |
240 | return true; | |
241 | else | |
242 | /* ignore RA */ | |
243 | return false; | |
244 | } | |
245 | ||
1f0d9695 | 246 | static IPv6PrivacyExtensions link_ipv6_privacy_extensions(Link *link) { |
fa709992 | 247 | assert(link); |
d68e2e59 LP |
248 | |
249 | if (!socket_ipv6_is_supported()) | |
250 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; | |
251 | ||
49092e22 | 252 | if (link->flags & IFF_LOOPBACK) |
1f0d9695 | 253 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
254 | |
255 | if (!link->network) | |
1f0d9695 | 256 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
257 | |
258 | return link->network->ipv6_privacy_extensions; | |
259 | } | |
260 | ||
439689c6 SS |
261 | static int link_enable_ipv6(Link *link) { |
262 | const char *p = NULL; | |
263 | bool disabled; | |
264 | int r; | |
265 | ||
266 | if (link->flags & IFF_LOOPBACK) | |
267 | return 0; | |
268 | ||
269 | disabled = !link_ipv6_enabled(link); | |
270 | ||
271 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/disable_ipv6"); | |
272 | ||
273 | r = write_string_file(p, one_zero(disabled), WRITE_STRING_FILE_VERIFY_ON_FAILURE); | |
274 | if (r < 0) | |
b106c586 ZJS |
275 | log_link_warning_errno(link, r, "Cannot %s IPv6 for interface %s: %m", |
276 | enable_disable(!disabled), link->ifname); | |
2d37cd53 | 277 | else |
b106c586 | 278 | log_link_info(link, "IPv6 successfully %sd", enable_disable(!disabled)); |
439689c6 SS |
279 | |
280 | return 0; | |
281 | } | |
282 | ||
84de38c5 TG |
283 | void link_update_operstate(Link *link) { |
284 | LinkOperationalState operstate; | |
285 | assert(link); | |
286 | ||
287 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
288 | operstate = LINK_OPERSTATE_DORMANT; | |
289 | else if (link_has_carrier(link)) { | |
290 | Address *address; | |
291 | uint8_t scope = RT_SCOPE_NOWHERE; | |
292 | Iterator i; | |
293 | ||
294 | /* if we have carrier, check what addresses we have */ | |
295 | SET_FOREACH(address, link->addresses, i) { | |
296 | if (!address_is_ready(address)) | |
297 | continue; | |
298 | ||
299 | if (address->scope < scope) | |
300 | scope = address->scope; | |
301 | } | |
302 | ||
303 | /* for operstate we also take foreign addresses into account */ | |
304 | SET_FOREACH(address, link->addresses_foreign, i) { | |
305 | if (!address_is_ready(address)) | |
306 | continue; | |
307 | ||
308 | if (address->scope < scope) | |
309 | scope = address->scope; | |
310 | } | |
311 | ||
312 | if (scope < RT_SCOPE_SITE) | |
313 | /* universally accessible addresses found */ | |
314 | operstate = LINK_OPERSTATE_ROUTABLE; | |
315 | else if (scope < RT_SCOPE_HOST) | |
316 | /* only link or site local addresses found */ | |
317 | operstate = LINK_OPERSTATE_DEGRADED; | |
318 | else | |
319 | /* no useful addresses found */ | |
320 | operstate = LINK_OPERSTATE_CARRIER; | |
321 | } else if (link->flags & IFF_UP) | |
322 | operstate = LINK_OPERSTATE_NO_CARRIER; | |
323 | else | |
324 | operstate = LINK_OPERSTATE_OFF; | |
325 | ||
326 | if (link->operstate != operstate) { | |
327 | link->operstate = operstate; | |
328 | link_send_changed(link, "OperationalState", NULL); | |
329 | link_dirty(link); | |
84de38c5 TG |
330 | } |
331 | } | |
332 | ||
51d18171 TG |
333 | #define FLAG_STRING(string, flag, old, new) \ |
334 | (((old ^ new) & flag) \ | |
335 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
336 | : "") | |
337 | ||
1c4baffc | 338 | static int link_update_flags(Link *link, sd_netlink_message *m) { |
51d18171 TG |
339 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; |
340 | uint8_t operstate; | |
341 | int r; | |
342 | ||
343 | assert(link); | |
344 | ||
345 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
346 | if (r < 0) |
347 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 | 348 | |
1c4baffc | 349 | r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate); |
51d18171 TG |
350 | if (r < 0) |
351 | /* if we got a message without operstate, take it to mean | |
352 | the state was unchanged */ | |
353 | operstate = link->kernel_operstate; | |
354 | ||
355 | if ((link->flags == flags) && (link->kernel_operstate == operstate)) | |
356 | return 0; | |
357 | ||
358 | if (link->flags != flags) { | |
6a7a4e4d | 359 | 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 |
360 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
361 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
362 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
363 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
364 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
365 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
366 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
367 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
368 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
369 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
370 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
371 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
372 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
373 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
374 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
375 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
376 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
377 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
378 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
379 | ||
380 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
381 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
382 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
383 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
384 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
385 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
386 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
387 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
388 | ||
389 | /* link flags are currently at most 18 bits, let's align to | |
390 | * printing 20 */ | |
391 | if (unknown_flags_added) | |
79008bdd | 392 | log_link_debug(link, |
6a7a4e4d | 393 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
394 | unknown_flags_added); |
395 | ||
396 | if (unknown_flags_removed) | |
79008bdd | 397 | log_link_debug(link, |
6a7a4e4d | 398 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
399 | unknown_flags_removed); |
400 | } | |
401 | ||
402 | link->flags = flags; | |
403 | link->kernel_operstate = operstate; | |
404 | ||
84de38c5 | 405 | link_update_operstate(link); |
51d18171 TG |
406 | |
407 | return 0; | |
408 | } | |
409 | ||
1046bf9b YW |
410 | DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref); |
411 | ||
1c4baffc | 412 | static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { |
8e766630 | 413 | _cleanup_(link_unrefp) Link *link = NULL; |
505f8da7 | 414 | uint16_t type; |
6cad256d | 415 | const char *ifname, *kind = NULL; |
505f8da7 | 416 | int r, ifindex; |
b710e6b6 | 417 | unsigned short iftype; |
f579559b | 418 | |
0c2f9b84 | 419 | assert(manager); |
505f8da7 | 420 | assert(message); |
f579559b TG |
421 | assert(ret); |
422 | ||
6cad256d TJ |
423 | /* check for link kind */ |
424 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); | |
425 | if (r == 0) { | |
db2f8a2e | 426 | (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind); |
6cad256d TJ |
427 | r = sd_netlink_message_exit_container(message); |
428 | if (r < 0) | |
429 | return r; | |
430 | } | |
431 | ||
1c4baffc | 432 | r = sd_netlink_message_get_type(message, &type); |
505f8da7 TG |
433 | if (r < 0) |
434 | return r; | |
435 | else if (type != RTM_NEWLINK) | |
436 | return -EINVAL; | |
437 | ||
438 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
439 | if (r < 0) | |
440 | return r; | |
441 | else if (ifindex <= 0) | |
442 | return -EINVAL; | |
443 | ||
b710e6b6 LP |
444 | r = sd_rtnl_message_link_get_type(message, &iftype); |
445 | if (r < 0) | |
446 | return r; | |
447 | ||
1c4baffc | 448 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname); |
505f8da7 TG |
449 | if (r < 0) |
450 | return r; | |
451 | ||
f579559b TG |
452 | link = new0(Link, 1); |
453 | if (!link) | |
454 | return -ENOMEM; | |
455 | ||
14b746f7 | 456 | link->n_ref = 1; |
5a3eb5a7 | 457 | link->manager = manager; |
8434fd5c | 458 | link->state = LINK_STATE_PENDING; |
be3a09b7 | 459 | link->rtnl_extended_attrs = true; |
505f8da7 | 460 | link->ifindex = ifindex; |
b710e6b6 | 461 | link->iftype = iftype; |
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 | ||
1c4baffc | 472 | r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
512922f8 | 473 | if (r < 0) |
34437b4f | 474 | log_link_debug_errno(link, r, "MAC address not found for new device, continuing without"); |
512922f8 | 475 | |
34437b4f | 476 | if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0) |
315db1a8 | 477 | return -ENOMEM; |
fe8db0c5 | 478 | |
34437b4f | 479 | if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0) |
68a8723c TG |
480 | return -ENOMEM; |
481 | ||
34437b4f | 482 | if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0) |
49699bac SS |
483 | return -ENOMEM; |
484 | ||
d5099efc | 485 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
486 | if (r < 0) |
487 | return r; | |
488 | ||
489 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
490 | if (r < 0) |
491 | return r; | |
492 | ||
51d18171 TG |
493 | r = link_update_flags(link, message); |
494 | if (r < 0) | |
495 | return r; | |
496 | ||
1cc6c93a | 497 | *ret = TAKE_PTR(link); |
f579559b TG |
498 | |
499 | return 0; | |
500 | } | |
501 | ||
8301aa0b | 502 | static Link *link_free(Link *link) { |
428fd0a7 | 503 | Address *address; |
0d4ad91d | 504 | Link *carrier; |
ddfc4f6e | 505 | Route *route; |
bce67bbe | 506 | Iterator i; |
428fd0a7 | 507 | |
8301aa0b | 508 | assert(link); |
f579559b | 509 | |
ddfc4f6e YW |
510 | while ((route = set_first(link->routes))) |
511 | route_free(route); | |
512 | ||
513 | while ((route = set_first(link->routes_foreign))) | |
514 | route_free(route); | |
515 | ||
516 | link->routes = set_free(link->routes); | |
517 | link->routes_foreign = set_free(link->routes_foreign); | |
518 | ||
0ade014c YW |
519 | while ((address = set_first(link->addresses))) |
520 | address_free(address); | |
cf1d700d | 521 | |
0ade014c YW |
522 | while ((address = set_first(link->addresses_foreign))) |
523 | address_free(address); | |
adda1ed9 | 524 | |
4701725c | 525 | link->addresses = set_free(link->addresses); |
4701725c | 526 | link->addresses_foreign = set_free(link->addresses_foreign); |
adda1ed9 | 527 | |
11bf3cce LP |
528 | while ((address = link->pool_addresses)) { |
529 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
530 | address_free(address); | |
531 | } | |
532 | ||
bfcdba8d | 533 | sd_dhcp_server_unref(link->dhcp_server); |
e5b04c8d | 534 | sd_dhcp_client_unref(link->dhcp_client); |
a6cc569e | 535 | sd_dhcp_lease_unref(link->dhcp_lease); |
f5be5601 | 536 | |
7272b25e | 537 | link_lldp_emit_stop(link); |
8e1ad1ea | 538 | |
68a8723c TG |
539 | free(link->lease_file); |
540 | ||
4afd3348 | 541 | sd_lldp_unref(link->lldp); |
49699bac SS |
542 | free(link->lldp_file); |
543 | ||
c69305ff LP |
544 | ndisc_flush(link); |
545 | ||
56cd007a | 546 | sd_ipv4ll_unref(link->ipv4ll); |
4138fb2c | 547 | sd_dhcp6_client_unref(link->dhcp6_client); |
1e7a0e21 | 548 | sd_ndisc_unref(link->ndisc); |
7465dd22 | 549 | sd_radv_unref(link->radv); |
1e7a0e21 | 550 | |
27dfc982 | 551 | if (link->manager) { |
ae06ab10 | 552 | hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)); |
27dfc982 | 553 | set_remove(link->manager->links_requesting_uuid, link); |
1046bf9b | 554 | link_clean(link); |
27dfc982 | 555 | } |
f579559b | 556 | |
c166a070 | 557 | free(link->ifname); |
68a8723c | 558 | |
ceac4078 | 559 | free(link->kind); |
6cad256d | 560 | |
db2f8a2e | 561 | (void) unlink(link->state_file); |
fe8db0c5 | 562 | free(link->state_file); |
c166a070 | 563 | |
51517f9e | 564 | sd_device_unref(link->sd_device); |
b5db00e5 | 565 | |
0d4ad91d AR |
566 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) |
567 | hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex)); | |
568 | hashmap_free(link->bound_to_links); | |
569 | ||
570 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) | |
571 | hashmap_remove(link->bound_by_links, INT_TO_PTR(carrier->ifindex)); | |
572 | hashmap_free(link->bound_by_links); | |
573 | ||
8301aa0b | 574 | return mfree(link); |
14b746f7 TG |
575 | } |
576 | ||
8301aa0b | 577 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free); |
14b746f7 | 578 | |
1046bf9b YW |
579 | void link_netlink_destroy_callback(void *userdata) { |
580 | Link *link = userdata; | |
581 | ||
582 | assert(userdata); | |
583 | ||
584 | link_unref(link); | |
585 | } | |
586 | ||
11a7f229 TG |
587 | int link_get(Manager *m, int ifindex, Link **ret) { |
588 | Link *link; | |
11a7f229 TG |
589 | |
590 | assert(m); | |
11a7f229 TG |
591 | assert(ifindex); |
592 | assert(ret); | |
593 | ||
ae06ab10 | 594 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
595 | if (!link) |
596 | return -ENODEV; | |
597 | ||
598 | *ret = link; | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
e331e246 TG |
603 | static void link_set_state(Link *link, LinkState state) { |
604 | assert(link); | |
605 | ||
606 | if (link->state == state) | |
607 | return; | |
608 | ||
609 | link->state = state; | |
610 | ||
611 | link_send_changed(link, "AdministrativeState", NULL); | |
e331e246 TG |
612 | } |
613 | ||
57bd6899 TG |
614 | static void link_enter_unmanaged(Link *link) { |
615 | assert(link); | |
616 | ||
6a7a4e4d | 617 | log_link_debug(link, "Unmanaged"); |
57bd6899 | 618 | |
e331e246 | 619 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 | 620 | |
84de38c5 | 621 | link_dirty(link); |
57bd6899 TG |
622 | } |
623 | ||
111bb8f9 TG |
624 | static int link_stop_clients(Link *link) { |
625 | int r = 0, k; | |
626 | ||
627 | assert(link); | |
628 | assert(link->manager); | |
629 | assert(link->manager->event); | |
630 | ||
ba179154 | 631 | if (link->dhcp_client) { |
111bb8f9 | 632 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d | 633 | if (k < 0) |
36c7d709 | 634 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m"); |
111bb8f9 TG |
635 | } |
636 | ||
ba179154 | 637 | if (link->ipv4ll) { |
111bb8f9 | 638 | k = sd_ipv4ll_stop(link->ipv4ll); |
6a7a4e4d | 639 | if (k < 0) |
36c7d709 | 640 | r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m"); |
dd43110f TG |
641 | } |
642 | ||
f5a8c43f TG |
643 | if (link->dhcp6_client) { |
644 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
645 | if (k < 0) | |
36c7d709 | 646 | r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m"); |
f5a8c43f | 647 | } |
4138fb2c | 648 | |
1e7a0e21 LP |
649 | if (link->ndisc) { |
650 | k = sd_ndisc_stop(link->ndisc); | |
6a7a4e4d | 651 | if (k < 0) |
36c7d709 | 652 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m"); |
4138fb2c PF |
653 | } |
654 | ||
7465dd22 PF |
655 | if (link->radv) { |
656 | k = sd_radv_stop(link->radv); | |
657 | if (k < 0) | |
658 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m"); | |
659 | } | |
660 | ||
7272b25e | 661 | link_lldp_emit_stop(link); |
111bb8f9 TG |
662 | return r; |
663 | } | |
664 | ||
b22d8a00 | 665 | void link_enter_failed(Link *link) { |
ef1ba606 | 666 | assert(link); |
f882c247 | 667 | |
370e9930 | 668 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
669 | return; |
670 | ||
6a7a4e4d | 671 | log_link_warning(link, "Failed"); |
449f7554 | 672 | |
e331e246 | 673 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 674 | |
111bb8f9 TG |
675 | link_stop_clients(link); |
676 | ||
84de38c5 | 677 | link_dirty(link); |
f882c247 TG |
678 | } |
679 | ||
4f434938 LP |
680 | static Address* link_find_dhcp_server_address(Link *link) { |
681 | Address *address; | |
682 | ||
683 | assert(link); | |
684 | assert(link->network); | |
685 | ||
d4cdbea5 | 686 | /* The first statically configured address if there is any */ |
4f434938 LP |
687 | LIST_FOREACH(addresses, address, link->network->static_addresses) { |
688 | ||
689 | if (address->family != AF_INET) | |
690 | continue; | |
691 | ||
af93291c | 692 | if (in_addr_is_null(address->family, &address->in_addr)) |
4f434938 LP |
693 | continue; |
694 | ||
695 | return address; | |
696 | } | |
697 | ||
698 | /* If that didn't work, find a suitable address we got from the pool */ | |
699 | LIST_FOREACH(addresses, address, link->pool_addresses) { | |
700 | if (address->family != AF_INET) | |
701 | continue; | |
702 | ||
703 | return address; | |
704 | } | |
705 | ||
706 | return NULL; | |
707 | } | |
708 | ||
e3a7b048 | 709 | static void link_enter_configured(Link *link) { |
dd43110f TG |
710 | assert(link); |
711 | assert(link->network); | |
e3a7b048 SS |
712 | |
713 | if (link->state != LINK_STATE_SETTING_ROUTES) | |
714 | return; | |
dd43110f | 715 | |
6a7a4e4d | 716 | log_link_info(link, "Configured"); |
dd43110f | 717 | |
e331e246 | 718 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f | 719 | |
84de38c5 | 720 | link_dirty(link); |
dd43110f TG |
721 | } |
722 | ||
8012cd39 TG |
723 | void link_check_ready(Link *link) { |
724 | Address *a; | |
725 | Iterator i; | |
726 | ||
3c9b8860 | 727 | assert(link); |
adda1ed9 | 728 | |
7209086d SS |
729 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
730 | return; | |
731 | ||
adda1ed9 TG |
732 | if (!link->network) |
733 | return; | |
3c9b8860 | 734 | |
7715629e ST |
735 | if (!link->static_routes_configured) |
736 | return; | |
737 | ||
738 | if (!link->routing_policy_rules_configured) | |
3c9b8860 TG |
739 | return; |
740 | ||
78c958f8 | 741 | if (link_ipv4ll_enabled(link)) |
3c9b8860 TG |
742 | if (!link->ipv4ll_address || |
743 | !link->ipv4ll_route) | |
744 | return; | |
745 | ||
5971cb9d SS |
746 | if (!link->network->bridge) { |
747 | ||
748 | if (link_ipv6ll_enabled(link)) | |
749 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) > 0) | |
750 | return; | |
751 | ||
752 | if ((link_dhcp4_enabled(link) && !link_dhcp6_enabled(link) && | |
753 | !link->dhcp4_configured) || | |
754 | (link_dhcp6_enabled(link) && !link_dhcp4_enabled(link) && | |
755 | !link->dhcp6_configured) || | |
756 | (link_dhcp4_enabled(link) && link_dhcp6_enabled(link) && | |
757 | !link->dhcp4_configured && !link->dhcp6_configured)) | |
e7ab854c TG |
758 | return; |
759 | ||
5971cb9d SS |
760 | if (link_ipv6_accept_ra_enabled(link) && !link->ndisc_configured) |
761 | return; | |
762 | } | |
fe307276 | 763 | |
8012cd39 TG |
764 | SET_FOREACH(a, link->addresses, i) |
765 | if (!address_is_ready(a)) | |
766 | return; | |
767 | ||
9fdaa992 TG |
768 | if (link->state != LINK_STATE_CONFIGURED) |
769 | link_enter_configured(link); | |
3c9b8860 TG |
770 | |
771 | return; | |
772 | } | |
773 | ||
8a9b3a23 SS |
774 | static int link_set_routing_policy_rule(Link *link) { |
775 | RoutingPolicyRule *rule, *rrule = NULL; | |
776 | int r; | |
777 | ||
778 | assert(link); | |
779 | assert(link->network); | |
780 | ||
781 | LIST_FOREACH(rules, rule, link->network->rules) { | |
782 | r = routing_policy_rule_get(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to, | |
762e2659 | 783 | rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, &rrule); |
8a9b3a23 SS |
784 | if (r == 1) { |
785 | (void) routing_policy_rule_make_local(link->manager, rrule); | |
786 | continue; | |
787 | } | |
788 | ||
789 | r = routing_policy_rule_configure(rule, link, link_routing_policy_rule_handler, false); | |
790 | if (r < 0) { | |
791 | log_link_warning_errno(link, r, "Could not set routing policy rules: %m"); | |
792 | link_enter_failed(link); | |
793 | return r; | |
794 | } | |
795 | ||
7715629e | 796 | link->routing_policy_rule_messages++; |
8a9b3a23 SS |
797 | } |
798 | ||
799 | routing_policy_rule_purge(link->manager, link); | |
7715629e ST |
800 | if (link->routing_policy_rule_messages == 0) { |
801 | link->routing_policy_rules_configured = true; | |
802 | link_check_ready(link); | |
803 | } else | |
804 | log_link_debug(link, "Setting routing policy rules"); | |
8a9b3a23 SS |
805 | |
806 | return 0; | |
807 | } | |
808 | ||
1c4baffc | 809 | static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 810 | Link *link = userdata; |
f882c247 TG |
811 | int r; |
812 | ||
1046bf9b | 813 | assert(link); |
7715629e | 814 | assert(link->route_messages > 0); |
370e9930 TG |
815 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
816 | LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED, | |
817 | LINK_STATE_LINGER)); | |
f882c247 | 818 | |
7715629e | 819 | link->route_messages--; |
f882c247 | 820 | |
77a008c0 | 821 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
822 | return 1; |
823 | ||
1c4baffc | 824 | r = sd_netlink_message_get_errno(m); |
c166a070 | 825 | if (r < 0 && r != -EEXIST) |
a2fae7bb | 826 | log_link_warning_errno(link, r, "Could not set route: %m"); |
f882c247 | 827 | |
7715629e | 828 | if (link->route_messages == 0) { |
6a7a4e4d | 829 | log_link_debug(link, "Routes set"); |
7715629e | 830 | link->static_routes_configured = true; |
8012cd39 | 831 | link_check_ready(link); |
dd3efc09 | 832 | } |
f882c247 TG |
833 | |
834 | return 1; | |
835 | } | |
836 | ||
837 | static int link_enter_set_routes(Link *link) { | |
a6cc569e | 838 | Route *rt; |
f882c247 TG |
839 | int r; |
840 | ||
841 | assert(link); | |
842 | assert(link->network); | |
ef1ba606 | 843 | assert(link->state == LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 844 | |
27c34f73 SS |
845 | (void) link_set_routing_policy_rule(link); |
846 | ||
e331e246 | 847 | link_set_state(link, LINK_STATE_SETTING_ROUTES); |
f882c247 | 848 | |
3d3d4255 | 849 | LIST_FOREACH(routes, rt, link->network->static_routes) { |
483d099e | 850 | r = route_configure(rt, link, route_handler); |
dd3efc09 | 851 | if (r < 0) { |
6a7a4e4d | 852 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 | 853 | link_enter_failed(link); |
a6cc569e TG |
854 | return r; |
855 | } | |
856 | ||
7715629e | 857 | link->route_messages++; |
8ddbeaa2 | 858 | } |
f5be5601 | 859 | |
7715629e ST |
860 | if (link->route_messages == 0) { |
861 | link->static_routes_configured = true; | |
8012cd39 | 862 | link_check_ready(link); |
431ca2ce | 863 | } else |
6a7a4e4d | 864 | log_link_debug(link, "Setting routes"); |
f882c247 TG |
865 | |
866 | return 0; | |
867 | } | |
868 | ||
91b5f997 | 869 | int link_route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 870 | Link *link = userdata; |
5c1d3fc9 UTL |
871 | int r; |
872 | ||
873 | assert(m); | |
874 | assert(link); | |
875 | assert(link->ifname); | |
876 | ||
5da8149f | 877 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
5c1d3fc9 UTL |
878 | return 1; |
879 | ||
1c4baffc | 880 | r = sd_netlink_message_get_errno(m); |
b90b025a | 881 | if (r < 0 && r != -ESRCH) |
a2fae7bb | 882 | log_link_warning_errno(link, r, "Could not drop route: %m"); |
5c1d3fc9 | 883 | |
5bdd314c | 884 | return 1; |
5c1d3fc9 UTL |
885 | } |
886 | ||
1c4baffc | 887 | static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 888 | Link *link = userdata; |
f882c247 TG |
889 | int r; |
890 | ||
4958aee4 | 891 | assert(rtnl); |
f5be5601 TG |
892 | assert(m); |
893 | assert(link); | |
894 | assert(link->ifname); | |
7715629e | 895 | assert(link->address_messages > 0); |
370e9930 TG |
896 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
897 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 898 | |
7715629e | 899 | link->address_messages--; |
f882c247 | 900 | |
5da8149f | 901 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
902 | return 1; |
903 | ||
1c4baffc | 904 | r = sd_netlink_message_get_errno(m); |
c166a070 | 905 | if (r < 0 && r != -EEXIST) |
a2fae7bb | 906 | log_link_warning_errno(link, r, "could not set address: %m"); |
45af44d4 | 907 | else if (r >= 0) |
200a0868 | 908 | manager_rtnl_process_address(rtnl, m, link->manager); |
f882c247 | 909 | |
7715629e | 910 | if (link->address_messages == 0) { |
6a7a4e4d | 911 | log_link_debug(link, "Addresses set"); |
ef1ba606 | 912 | link_enter_set_routes(link); |
dd3efc09 | 913 | } |
f882c247 TG |
914 | |
915 | return 1; | |
916 | } | |
917 | ||
95b74ef6 | 918 | static int address_label_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 919 | Link *link = userdata; |
95b74ef6 SS |
920 | int r; |
921 | ||
922 | assert(rtnl); | |
923 | assert(m); | |
924 | assert(link); | |
925 | assert(link->ifname); | |
7715629e | 926 | assert(link->address_label_messages > 0); |
95b74ef6 | 927 | |
7715629e | 928 | link->address_label_messages--; |
95b74ef6 SS |
929 | |
930 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
931 | return 1; | |
932 | ||
933 | r = sd_netlink_message_get_errno(m); | |
934 | if (r < 0 && r != -EEXIST) | |
935 | log_link_warning_errno(link, r, "could not set address label: %m"); | |
936 | else if (r >= 0) | |
937 | manager_rtnl_process_address(rtnl, m, link->manager); | |
938 | ||
7715629e | 939 | if (link->address_label_messages == 0) |
95b74ef6 | 940 | log_link_debug(link, "Addresses label set"); |
95b74ef6 SS |
941 | |
942 | return 1; | |
943 | } | |
944 | ||
a380b2d4 | 945 | static int link_push_uplink_dns_to_dhcp_server(Link *link, sd_dhcp_server *s) { |
4f5f911e LP |
946 | _cleanup_free_ struct in_addr *addresses = NULL; |
947 | size_t n_addresses = 0, n_allocated = 0; | |
5512a963 | 948 | unsigned i; |
4f5f911e LP |
949 | |
950 | log_debug("Copying DNS server information from %s", link->ifname); | |
951 | ||
952 | if (!link->network) | |
953 | return 0; | |
954 | ||
5512a963 | 955 | for (i = 0; i < link->network->n_dns; i++) { |
49ad6829 | 956 | struct in_addr ia; |
4f5f911e LP |
957 | |
958 | /* Only look for IPv4 addresses */ | |
5512a963 | 959 | if (link->network->dns[i].family != AF_INET) |
4f5f911e LP |
960 | continue; |
961 | ||
49ad6829 LP |
962 | ia = link->network->dns[i].address.in; |
963 | ||
964 | /* Never propagate obviously borked data */ | |
965 | if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia)) | |
966 | continue; | |
967 | ||
4f5f911e LP |
968 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1)) |
969 | return log_oom(); | |
970 | ||
49ad6829 | 971 | addresses[n_addresses++] = ia; |
4f5f911e LP |
972 | } |
973 | ||
5512a963 | 974 | if (link->network->dhcp_use_dns && link->dhcp_lease) { |
4f5f911e LP |
975 | const struct in_addr *da = NULL; |
976 | int n; | |
977 | ||
978 | n = sd_dhcp_lease_get_dns(link->dhcp_lease, &da); | |
979 | if (n > 0) { | |
980 | ||
981 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n)) | |
982 | return log_oom(); | |
983 | ||
984 | memcpy(addresses + n_addresses, da, n * sizeof(struct in_addr)); | |
985 | n_addresses += n; | |
986 | } | |
987 | } | |
988 | ||
989 | if (n_addresses <= 0) | |
990 | return 0; | |
991 | ||
992 | return sd_dhcp_server_set_dns(s, addresses, n_addresses); | |
993 | } | |
994 | ||
a380b2d4 | 995 | static int link_push_uplink_ntp_to_dhcp_server(Link *link, sd_dhcp_server *s) { |
4f5f911e LP |
996 | _cleanup_free_ struct in_addr *addresses = NULL; |
997 | size_t n_addresses = 0, n_allocated = 0; | |
998 | char **a; | |
999 | ||
1000 | if (!link->network) | |
1001 | return 0; | |
1002 | ||
1003 | log_debug("Copying NTP server information from %s", link->ifname); | |
1004 | ||
1005 | STRV_FOREACH(a, link->network->ntp) { | |
1006 | struct in_addr ia; | |
1007 | ||
1008 | /* Only look for IPv4 addresses */ | |
1009 | if (inet_pton(AF_INET, *a, &ia) <= 0) | |
1010 | continue; | |
49ad6829 LP |
1011 | |
1012 | /* Never propagate obviously borked data */ | |
1013 | if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia)) | |
1014 | continue; | |
4f5f911e LP |
1015 | |
1016 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1)) | |
1017 | return log_oom(); | |
1018 | ||
1019 | addresses[n_addresses++] = ia; | |
1020 | } | |
1021 | ||
5512a963 | 1022 | if (link->network->dhcp_use_ntp && link->dhcp_lease) { |
4f5f911e LP |
1023 | const struct in_addr *da = NULL; |
1024 | int n; | |
1025 | ||
1026 | n = sd_dhcp_lease_get_ntp(link->dhcp_lease, &da); | |
1027 | if (n > 0) { | |
1028 | ||
1029 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n)) | |
1030 | return log_oom(); | |
1031 | ||
1032 | memcpy(addresses + n_addresses, da, n * sizeof(struct in_addr)); | |
1033 | n_addresses += n; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | if (n_addresses <= 0) | |
1038 | return 0; | |
1039 | ||
1040 | return sd_dhcp_server_set_ntp(s, addresses, n_addresses); | |
1041 | } | |
1042 | ||
f6bb7ac5 TJ |
1043 | static int link_set_bridge_fdb(Link *link) { |
1044 | FdbEntry *fdb_entry; | |
197e2809 | 1045 | int r; |
f6bb7ac5 TJ |
1046 | |
1047 | LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { | |
1048 | r = fdb_entry_configure(link, fdb_entry); | |
197e2809 | 1049 | if (r < 0) |
f6bb7ac5 | 1050 | return log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m"); |
f6bb7ac5 TJ |
1051 | } |
1052 | ||
197e2809 | 1053 | return 0; |
f6bb7ac5 TJ |
1054 | } |
1055 | ||
f882c247 | 1056 | static int link_enter_set_addresses(Link *link) { |
95b74ef6 | 1057 | AddressLabel *label; |
a6cc569e | 1058 | Address *ad; |
f882c247 TG |
1059 | int r; |
1060 | ||
1061 | assert(link); | |
1062 | assert(link->network); | |
f5be5601 | 1063 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 1064 | |
f6bb7ac5 TJ |
1065 | r = link_set_bridge_fdb(link); |
1066 | if (r < 0) | |
1067 | return r; | |
1068 | ||
e331e246 | 1069 | link_set_state(link, LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 1070 | |
3d3d4255 | 1071 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
483d099e | 1072 | r = address_configure(ad, link, address_handler, false); |
dd3efc09 | 1073 | if (r < 0) { |
5a8bcb67 | 1074 | log_link_warning_errno(link, r, "Could not set addresses: %m"); |
f5be5601 | 1075 | link_enter_failed(link); |
95b74ef6 SS |
1076 | return r; |
1077 | } | |
1078 | ||
7715629e | 1079 | link->address_messages++; |
95b74ef6 SS |
1080 | } |
1081 | ||
1082 | LIST_FOREACH(labels, label, link->network->address_labels) { | |
1083 | r = address_label_configure(label, link, address_label_handler, false); | |
1084 | if (r < 0) { | |
1085 | log_link_warning_errno(link, r, "Could not set address label: %m"); | |
1086 | link_enter_failed(link); | |
f5be5601 TG |
1087 | return r; |
1088 | } | |
1089 | ||
7715629e | 1090 | link->address_label_messages++; |
f882c247 TG |
1091 | } |
1092 | ||
d4cdbea5 TG |
1093 | /* now that we can figure out a default address for the dhcp server, |
1094 | start it */ | |
708c425d | 1095 | if (link_dhcp4_server_enabled(link) && (link->flags & IFF_UP)) { |
d4cdbea5 | 1096 | Address *address; |
4f5f911e LP |
1097 | Link *uplink = NULL; |
1098 | bool acquired_uplink = false; | |
d4cdbea5 TG |
1099 | |
1100 | address = link_find_dhcp_server_address(link); | |
1101 | if (!address) { | |
6a7a4e4d | 1102 | log_link_warning(link, "Failed to find suitable address for DHCPv4 server instance."); |
d4cdbea5 TG |
1103 | link_enter_failed(link); |
1104 | return 0; | |
1105 | } | |
1106 | ||
61986155 | 1107 | /* use the server address' subnet as the pool */ |
9b3a67c5 TG |
1108 | r = sd_dhcp_server_configure_pool(link->dhcp_server, &address->in_addr.in, address->prefixlen, |
1109 | link->network->dhcp_server_pool_offset, link->network->dhcp_server_pool_size); | |
d4cdbea5 TG |
1110 | if (r < 0) |
1111 | return r; | |
1112 | ||
1113 | /* TODO: | |
1114 | r = sd_dhcp_server_set_router(link->dhcp_server, | |
1115 | &main_address->in_addr.in); | |
1116 | if (r < 0) | |
1117 | return r; | |
d4cdbea5 TG |
1118 | */ |
1119 | ||
586ac6f7 LP |
1120 | if (link->network->dhcp_server_max_lease_time_usec > 0) { |
1121 | r = sd_dhcp_server_set_max_lease_time( | |
1122 | link->dhcp_server, | |
1123 | DIV_ROUND_UP(link->network->dhcp_server_max_lease_time_usec, USEC_PER_SEC)); | |
1124 | if (r < 0) | |
1125 | return r; | |
1126 | } | |
1127 | ||
1128 | if (link->network->dhcp_server_default_lease_time_usec > 0) { | |
1129 | r = sd_dhcp_server_set_default_lease_time( | |
1130 | link->dhcp_server, | |
1131 | DIV_ROUND_UP(link->network->dhcp_server_default_lease_time_usec, USEC_PER_SEC)); | |
1132 | if (r < 0) | |
1133 | return r; | |
1134 | } | |
1135 | ||
1a04db0f LP |
1136 | if (link->network->dhcp_server_emit_dns) { |
1137 | ||
4f5f911e | 1138 | if (link->network->n_dhcp_server_dns > 0) |
1a04db0f | 1139 | r = sd_dhcp_server_set_dns(link->dhcp_server, link->network->dhcp_server_dns, link->network->n_dhcp_server_dns); |
4f5f911e LP |
1140 | else { |
1141 | uplink = manager_find_uplink(link->manager, link); | |
1142 | acquired_uplink = true; | |
1143 | ||
1144 | if (!uplink) { | |
1145 | log_link_debug(link, "Not emitting DNS server information on link, couldn't find suitable uplink."); | |
1146 | r = 0; | |
1147 | } else | |
a380b2d4 | 1148 | r = link_push_uplink_dns_to_dhcp_server(uplink, link->dhcp_server); |
4f5f911e LP |
1149 | } |
1150 | if (r < 0) | |
1151 | log_link_warning_errno(link, r, "Failed to set DNS server for DHCP server, ignoring: %m"); | |
1a04db0f LP |
1152 | } |
1153 | ||
1a04db0f LP |
1154 | if (link->network->dhcp_server_emit_ntp) { |
1155 | ||
4f5f911e | 1156 | if (link->network->n_dhcp_server_ntp > 0) |
1a04db0f | 1157 | r = sd_dhcp_server_set_ntp(link->dhcp_server, link->network->dhcp_server_ntp, link->network->n_dhcp_server_ntp); |
4f5f911e LP |
1158 | else { |
1159 | if (!acquired_uplink) | |
1160 | uplink = manager_find_uplink(link->manager, link); | |
1161 | ||
1162 | if (!uplink) { | |
1163 | log_link_debug(link, "Not emitting NTP server information on link, couldn't find suitable uplink."); | |
1164 | r = 0; | |
1165 | } else | |
a380b2d4 | 1166 | r = link_push_uplink_ntp_to_dhcp_server(uplink, link->dhcp_server); |
4f5f911e LP |
1167 | |
1168 | } | |
1169 | if (r < 0) | |
1170 | log_link_warning_errno(link, r, "Failed to set NTP server for DHCP server, ignoring: %m"); | |
1a04db0f LP |
1171 | } |
1172 | ||
77ff6022 | 1173 | r = sd_dhcp_server_set_emit_router(link->dhcp_server, link->network->dhcp_server_emit_router); |
fc95c359 YW |
1174 | if (r < 0) |
1175 | return log_link_warning_errno(link, r, "Failed to set router emission for DHCP server: %m"); | |
77ff6022 | 1176 | |
8eb9058d LP |
1177 | if (link->network->dhcp_server_emit_timezone) { |
1178 | _cleanup_free_ char *buffer = NULL; | |
0ab8a1b6 | 1179 | const char *tz = NULL; |
8eb9058d LP |
1180 | |
1181 | if (link->network->dhcp_server_timezone) | |
1182 | tz = link->network->dhcp_server_timezone; | |
1183 | else { | |
1184 | r = get_timezone(&buffer); | |
1185 | if (r < 0) | |
1186 | log_warning_errno(r, "Failed to determine timezone: %m"); | |
1187 | else | |
1188 | tz = buffer; | |
1189 | } | |
1190 | ||
1191 | if (tz) { | |
1192 | r = sd_dhcp_server_set_timezone(link->dhcp_server, tz); | |
1193 | if (r < 0) | |
1194 | return r; | |
1195 | } | |
1196 | } | |
1197 | ||
d4cdbea5 TG |
1198 | r = sd_dhcp_server_start(link->dhcp_server); |
1199 | if (r < 0) { | |
6a7a4e4d | 1200 | log_link_warning_errno(link, r, "Could not start DHCPv4 server instance: %m"); |
d4cdbea5 TG |
1201 | |
1202 | link_enter_failed(link); | |
1203 | ||
1204 | return 0; | |
1205 | } | |
1206 | ||
6a7a4e4d | 1207 | log_link_debug(link, "Offering DHCPv4 leases"); |
d4cdbea5 TG |
1208 | } |
1209 | ||
7715629e | 1210 | if (link->address_messages == 0) |
431ca2ce | 1211 | link_enter_set_routes(link); |
6a7a4e4d LP |
1212 | else |
1213 | log_link_debug(link, "Setting addresses"); | |
431ca2ce | 1214 | |
f882c247 TG |
1215 | return 0; |
1216 | } | |
1217 | ||
91b5f997 | 1218 | int link_address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1219 | Link *link = userdata; |
ff254138 TG |
1220 | int r; |
1221 | ||
1222 | assert(m); | |
1223 | assert(link); | |
1224 | assert(link->ifname); | |
1225 | ||
5da8149f | 1226 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ff254138 TG |
1227 | return 1; |
1228 | ||
1c4baffc | 1229 | r = sd_netlink_message_get_errno(m); |
b90b025a | 1230 | if (r < 0 && r != -EADDRNOTAVAIL) |
a2fae7bb | 1231 | log_link_warning_errno(link, r, "Could not drop address: %m"); |
ff254138 | 1232 | |
5bdd314c | 1233 | return 1; |
ff254138 TG |
1234 | } |
1235 | ||
13b498f9 TJ |
1236 | static int link_set_bridge_vlan(Link *link) { |
1237 | int r = 0; | |
1238 | ||
1239 | r = br_vlan_configure(link, link->network->pvid, link->network->br_vid_bitmap, link->network->br_untagged_bitmap); | |
1240 | if (r < 0) | |
1241 | log_link_error_errno(link, r, "Failed to assign VLANs to bridge port: %m"); | |
1242 | ||
1243 | return r; | |
1244 | } | |
1245 | ||
a60a720c | 1246 | static int link_set_proxy_arp(Link *link) { |
23d8b221 SS |
1247 | const char *p = NULL; |
1248 | int r; | |
1249 | ||
1250 | if (!link_proxy_arp_enabled(link)) | |
1251 | return 0; | |
1252 | ||
1253 | p = strjoina("/proc/sys/net/ipv4/conf/", link->ifname, "/proxy_arp"); | |
1254 | ||
1255 | r = write_string_file(p, one_zero(link->network->proxy_arp), WRITE_STRING_FILE_VERIFY_ON_FAILURE); | |
1256 | if (r < 0) | |
1257 | log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m"); | |
1258 | ||
1259 | return 0; | |
1260 | } | |
1261 | ||
1c4baffc | 1262 | static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1263 | Link *link = userdata; |
e1853b00 SS |
1264 | int r; |
1265 | ||
1046bf9b YW |
1266 | assert(link); |
1267 | ||
6a7a4e4d | 1268 | log_link_debug(link, "Set link"); |
e1853b00 | 1269 | |
1c4baffc | 1270 | r = sd_netlink_message_get_errno(m); |
e1853b00 | 1271 | if (r < 0 && r != -EEXIST) { |
f2341e0a | 1272 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
e1853b00 SS |
1273 | link_enter_failed(link); |
1274 | return 1; | |
1275 | } | |
1276 | ||
1277 | return 0; | |
1278 | } | |
1279 | ||
55dc8c4a YW |
1280 | static int link_configure_after_setting_mtu(Link *link); |
1281 | ||
1c4baffc | 1282 | static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1283 | Link *link = userdata; |
4f882b2a TG |
1284 | int r; |
1285 | ||
1286 | assert(m); | |
1287 | assert(link); | |
1288 | assert(link->ifname); | |
1289 | ||
55dc8c4a YW |
1290 | link->setting_mtu = false; |
1291 | ||
5da8149f | 1292 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
1293 | return 1; |
1294 | ||
1c4baffc | 1295 | r = sd_netlink_message_get_errno(m); |
55dc8c4a | 1296 | if (r < 0) { |
a2fae7bb | 1297 | log_link_warning_errno(link, r, "Could not set MTU: %m"); |
55dc8c4a YW |
1298 | return 1; |
1299 | } | |
1300 | ||
1301 | log_link_debug(link, "Setting MTU done."); | |
1302 | ||
1303 | if (link->state == LINK_STATE_PENDING) | |
1304 | (void) link_configure_after_setting_mtu(link); | |
4f882b2a TG |
1305 | |
1306 | return 1; | |
1307 | } | |
1308 | ||
3c9b8860 | 1309 | int link_set_mtu(Link *link, uint32_t mtu) { |
4afd3348 | 1310 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f882b2a TG |
1311 | int r; |
1312 | ||
1313 | assert(link); | |
1314 | assert(link->manager); | |
1315 | assert(link->manager->rtnl); | |
1316 | ||
55dc8c4a | 1317 | if (link->mtu == mtu || link->setting_mtu) |
ee493106 YW |
1318 | return 0; |
1319 | ||
6a7a4e4d | 1320 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 1321 | |
6a7a4e4d LP |
1322 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1323 | if (r < 0) | |
1324 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a | 1325 | |
44b598a1 | 1326 | /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled) |
72c2500b | 1327 | * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */ |
44b598a1 SS |
1328 | (void) link_enable_ipv6(link); |
1329 | ||
1330 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes | |
72c2500b YW |
1331 | * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ |
1332 | if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) { | |
44b598a1 SS |
1333 | |
1334 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
1335 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m"); | |
1336 | ||
72c2500b | 1337 | mtu = IPV6_MIN_MTU; |
44b598a1 SS |
1338 | } |
1339 | ||
1c4baffc | 1340 | r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu); |
6a7a4e4d LP |
1341 | if (r < 0) |
1342 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 1343 | |
1046bf9b YW |
1344 | r = sd_netlink_call_async(link->manager->rtnl, req, set_mtu_handler, |
1345 | link_netlink_destroy_callback, link, 0, NULL); | |
6a7a4e4d LP |
1346 | if (r < 0) |
1347 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 1348 | |
ae941762 | 1349 | link_ref(link); |
1046bf9b | 1350 | link->setting_mtu = true; |
b226d99b | 1351 | |
4f882b2a TG |
1352 | return 0; |
1353 | } | |
1354 | ||
99d2baa2 | 1355 | static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1356 | Link *link = userdata; |
99d2baa2 SS |
1357 | int r; |
1358 | ||
1359 | assert(m); | |
1360 | assert(link); | |
1361 | assert(link->ifname); | |
1362 | ||
1363 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1364 | return 1; | |
1365 | ||
1366 | r = sd_netlink_message_get_errno(m); | |
1367 | if (r < 0) | |
1368 | log_link_warning_errno(link, r, "Could not set link flags: %m"); | |
1369 | ||
1370 | return 1; | |
1371 | } | |
1372 | ||
1373 | static int link_set_flags(Link *link) { | |
1374 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1375 | unsigned ifi_change = 0; | |
1376 | unsigned ifi_flags = 0; | |
1377 | int r; | |
1378 | ||
1379 | assert(link); | |
1380 | assert(link->manager); | |
1381 | assert(link->manager->rtnl); | |
1382 | ||
1383 | if (link->flags & IFF_LOOPBACK) | |
1384 | return 0; | |
1385 | ||
1386 | if (!link->network) | |
1387 | return 0; | |
1388 | ||
866e6b7a | 1389 | if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0) |
99d2baa2 SS |
1390 | return 0; |
1391 | ||
1392 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1393 | if (r < 0) | |
1394 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1395 | ||
1396 | if (link->network->arp >= 0) { | |
1397 | ifi_change |= IFF_NOARP; | |
e6ebebbe SS |
1398 | SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0); |
1399 | } | |
1400 | ||
1401 | if (link->network->multicast >= 0) { | |
1402 | ifi_change |= IFF_MULTICAST; | |
1403 | SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast); | |
99d2baa2 SS |
1404 | } |
1405 | ||
866e6b7a SS |
1406 | if (link->network->allmulticast >= 0) { |
1407 | ifi_change |= IFF_ALLMULTI; | |
1408 | SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast); | |
1409 | } | |
1410 | ||
99d2baa2 SS |
1411 | r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change); |
1412 | if (r < 0) | |
1413 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1414 | ||
1046bf9b YW |
1415 | r = sd_netlink_call_async(link->manager->rtnl, req, set_flags_handler, |
1416 | link_netlink_destroy_callback, link, 0, NULL); | |
99d2baa2 SS |
1417 | if (r < 0) |
1418 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1419 | ||
1420 | link_ref(link); | |
1421 | ||
1422 | return 0; | |
1423 | } | |
1424 | ||
e1853b00 | 1425 | static int link_set_bridge(Link *link) { |
4afd3348 | 1426 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
e1853b00 SS |
1427 | int r; |
1428 | ||
1429 | assert(link); | |
1430 | assert(link->network); | |
1431 | ||
6a7a4e4d LP |
1432 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1433 | if (r < 0) | |
1434 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
1435 | |
1436 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
1437 | if (r < 0) |
1438 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 | 1439 | |
1c4baffc | 1440 | r = sd_netlink_message_open_container(req, IFLA_PROTINFO); |
6a7a4e4d LP |
1441 | if (r < 0) |
1442 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 | 1443 | |
7f9915f0 SS |
1444 | if (link->network->use_bpdu >= 0) { |
1445 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_GUARD, link->network->use_bpdu); | |
1446 | if (r < 0) | |
1447 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_GUARD attribute: %m"); | |
1448 | } | |
eb7ff4dd | 1449 | |
7f9915f0 SS |
1450 | if (link->network->hairpin >= 0) { |
1451 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_MODE, link->network->hairpin); | |
1452 | if (r < 0) | |
1453 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_MODE attribute: %m"); | |
1454 | } | |
eb7ff4dd | 1455 | |
7f9915f0 SS |
1456 | if (link->network->fast_leave >= 0) { |
1457 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_FAST_LEAVE, link->network->fast_leave); | |
1458 | if (r < 0) | |
1459 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m"); | |
1460 | } | |
eb7ff4dd | 1461 | |
7f9915f0 SS |
1462 | if (link->network->allow_port_to_be_root >= 0) { |
1463 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_PROTECT, link->network->allow_port_to_be_root); | |
1464 | if (r < 0) | |
1465 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PROTECT attribute: %m"); | |
eb7ff4dd | 1466 | |
7f9915f0 SS |
1467 | } |
1468 | ||
1469 | if (link->network->unicast_flood >= 0) { | |
1470 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_UNICAST_FLOOD, link->network->unicast_flood); | |
1471 | if (r < 0) | |
1472 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m"); | |
1473 | ||
1474 | } | |
eb7ff4dd | 1475 | |
34437b4f | 1476 | if (link->network->cost != 0) { |
1c4baffc | 1477 | r = sd_netlink_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); |
6a7a4e4d LP |
1478 | if (r < 0) |
1479 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 | 1480 | } |
b56be296 DJL |
1481 | if (link->network->priority != LINK_BRIDGE_PORT_PRIORITY_INVALID) { |
1482 | r = sd_netlink_message_append_u16(req, IFLA_BRPORT_PRIORITY, link->network->priority); | |
1483 | if (r < 0) | |
1484 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PRIORITY attribute: %m"); | |
1485 | } | |
e1853b00 | 1486 | |
1c4baffc | 1487 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1488 | if (r < 0) |
1489 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 | 1490 | |
1046bf9b YW |
1491 | r = sd_netlink_call_async(link->manager->rtnl, req, link_set_handler, |
1492 | link_netlink_destroy_callback, link, 0, NULL); | |
6a7a4e4d LP |
1493 | if (r < 0) |
1494 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
1495 | |
1496 | link_ref(link); | |
1497 | ||
1498 | return r; | |
1499 | } | |
1500 | ||
eb64b435 SS |
1501 | static int link_bond_set(Link *link) { |
1502 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1503 | int r; | |
1504 | ||
1505 | assert(link); | |
1506 | assert(link->network); | |
1507 | ||
1508 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_NEWLINK, link->network->bond->ifindex); | |
1509 | if (r < 0) | |
1510 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1511 | ||
1512 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
1513 | if (r < 0) | |
1514 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1515 | ||
1516 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
1517 | if (r < 0) | |
1518 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
1519 | ||
1520 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, "bond"); | |
1521 | if (r < 0) | |
1522 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1523 | ||
1524 | if (link->network->active_slave) { | |
1525 | r = sd_netlink_message_append_u32(req, IFLA_BOND_ACTIVE_SLAVE, link->ifindex); | |
1526 | if (r < 0) | |
1527 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m"); | |
1528 | } | |
1529 | ||
1530 | if (link->network->primary_slave) { | |
1531 | r = sd_netlink_message_append_u32(req, IFLA_BOND_PRIMARY, link->ifindex); | |
1532 | if (r < 0) | |
1533 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_PRIMARY attribute: %m"); | |
1534 | } | |
1535 | ||
1536 | r = sd_netlink_message_close_container(req); | |
1537 | if (r < 0) | |
1538 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
1539 | ||
1540 | r = sd_netlink_message_close_container(req); | |
1541 | if (r < 0) | |
1542 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1543 | ||
1046bf9b YW |
1544 | r = sd_netlink_call_async(link->manager->rtnl, req, set_flags_handler, |
1545 | link_netlink_destroy_callback, link, 0, NULL); | |
eb64b435 SS |
1546 | if (r < 0) |
1547 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1548 | ||
1549 | link_ref(link); | |
1550 | ||
1551 | return r; | |
1552 | } | |
1553 | ||
34437b4f LP |
1554 | static int link_lldp_save(Link *link) { |
1555 | _cleanup_free_ char *temp_path = NULL; | |
1556 | _cleanup_fclose_ FILE *f = NULL; | |
1557 | sd_lldp_neighbor **l = NULL; | |
1558 | int n = 0, r, i; | |
49699bac SS |
1559 | |
1560 | assert(link); | |
34437b4f LP |
1561 | assert(link->lldp_file); |
1562 | ||
1563 | if (!link->lldp) { | |
1564 | (void) unlink(link->lldp_file); | |
1565 | return 0; | |
1566 | } | |
1567 | ||
1568 | r = sd_lldp_get_neighbors(link->lldp, &l); | |
1569 | if (r < 0) | |
1570 | goto finish; | |
1571 | if (r == 0) { | |
1572 | (void) unlink(link->lldp_file); | |
1573 | goto finish; | |
1574 | } | |
1575 | ||
1576 | n = r; | |
1577 | ||
1578 | r = fopen_temporary(link->lldp_file, &f, &temp_path); | |
1579 | if (r < 0) | |
1580 | goto finish; | |
1581 | ||
1582 | fchmod(fileno(f), 0644); | |
49699bac | 1583 | |
34437b4f LP |
1584 | for (i = 0; i < n; i++) { |
1585 | const void *p; | |
1586 | le64_t u; | |
1587 | size_t sz; | |
1588 | ||
1589 | r = sd_lldp_neighbor_get_raw(l[i], &p, &sz); | |
9ef61f2e | 1590 | if (r < 0) |
34437b4f LP |
1591 | goto finish; |
1592 | ||
1593 | u = htole64(sz); | |
1594 | (void) fwrite(&u, 1, sizeof(u), f); | |
1595 | (void) fwrite(p, 1, sz, f); | |
1596 | } | |
49699bac | 1597 | |
34437b4f LP |
1598 | r = fflush_and_check(f); |
1599 | if (r < 0) | |
1600 | goto finish; | |
1601 | ||
1602 | if (rename(temp_path, link->lldp_file) < 0) { | |
1603 | r = -errno; | |
1604 | goto finish; | |
9ef61f2e | 1605 | } |
34437b4f LP |
1606 | |
1607 | finish: | |
1608 | if (r < 0) { | |
1609 | (void) unlink(link->lldp_file); | |
1610 | if (temp_path) | |
1611 | (void) unlink(temp_path); | |
1612 | ||
1613 | log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file); | |
1614 | } | |
1615 | ||
1616 | if (l) { | |
1617 | for (i = 0; i < n; i++) | |
1618 | sd_lldp_neighbor_unref(l[i]); | |
1619 | free(l); | |
1620 | } | |
1621 | ||
1622 | return r; | |
1623 | } | |
1624 | ||
90dffb22 | 1625 | static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) { |
34437b4f | 1626 | Link *link = userdata; |
90dffb22 | 1627 | int r; |
34437b4f LP |
1628 | |
1629 | assert(link); | |
1630 | ||
1631 | (void) link_lldp_save(link); | |
90dffb22 | 1632 | |
7272b25e | 1633 | if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) { |
90dffb22 LP |
1634 | /* If we received information about a new neighbor, restart the LLDP "fast" logic */ |
1635 | ||
1636 | log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission."); | |
1637 | ||
7272b25e | 1638 | r = link_lldp_emit_start(link); |
90dffb22 LP |
1639 | if (r < 0) |
1640 | log_link_warning_errno(link, r, "Failed to restart LLDP transmission: %m"); | |
1641 | } | |
49699bac SS |
1642 | } |
1643 | ||
e7ab854c TG |
1644 | static int link_acquire_ipv6_conf(Link *link) { |
1645 | int r; | |
1646 | ||
1647 | assert(link); | |
1648 | ||
e7ab854c | 1649 | if (link_ipv6_accept_ra_enabled(link)) { |
1e7a0e21 | 1650 | assert(link->ndisc); |
e7ab854c TG |
1651 | |
1652 | log_link_debug(link, "Discovering IPv6 routers"); | |
1653 | ||
1e7a0e21 | 1654 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1655 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1656 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1657 | } | |
1658 | ||
7465dd22 PF |
1659 | if (link_radv_enabled(link)) { |
1660 | assert(link->radv); | |
1661 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1662 | ||
1663 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
1664 | ||
1665 | r = sd_radv_start(link->radv); | |
1666 | if (r < 0 && r != -EBUSY) | |
1667 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); | |
1668 | } | |
1669 | ||
10752343 PF |
1670 | (void) dhcp6_request_prefix_delegation(link); |
1671 | ||
e7ab854c TG |
1672 | return 0; |
1673 | } | |
1674 | ||
6fc25497 | 1675 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1676 | int r; |
1677 | ||
1678 | assert(link); | |
1679 | assert(link->network); | |
ff254138 TG |
1680 | assert(link->manager); |
1681 | assert(link->manager->event); | |
1682 | ||
78c958f8 | 1683 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 1684 | assert(link->ipv4ll); |
ff254138 | 1685 | |
6a7a4e4d | 1686 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1687 | |
1688 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1689 | if (r < 0) |
1690 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1691 | } |
1692 | ||
78c958f8 | 1693 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1694 | assert(link->dhcp_client); |
ff254138 | 1695 | |
6a7a4e4d | 1696 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1697 | |
5c1d3fc9 | 1698 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1699 | if (r < 0) |
1700 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1701 | } |
ff254138 | 1702 | |
6fc25497 SS |
1703 | return 0; |
1704 | } | |
1705 | ||
1706 | static int link_acquire_conf(Link *link) { | |
1707 | int r; | |
1708 | ||
1709 | assert(link); | |
1710 | ||
1711 | r = link_acquire_ipv4_conf(link); | |
1712 | if (r < 0) | |
1713 | return r; | |
1714 | ||
1715 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) == 0) { | |
1716 | r = link_acquire_ipv6_conf(link); | |
1717 | if (r < 0) | |
1718 | return r; | |
1719 | } | |
1720 | ||
7272b25e LP |
1721 | if (link_lldp_emit_enabled(link)) { |
1722 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1723 | if (r < 0) |
1724 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1725 | } | |
1726 | ||
ff254138 TG |
1727 | return 0; |
1728 | } | |
1729 | ||
a61bb41c | 1730 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1731 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1732 | ||
a61bb41c | 1733 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1734 | return true; |
1735 | ||
a61bb41c | 1736 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1737 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 1738 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
1739 | return true; |
1740 | ||
1741 | return false; | |
1742 | } | |
1743 | ||
1c4baffc | 1744 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1745 | Link *link = userdata; |
dd3efc09 TG |
1746 | int r; |
1747 | ||
1746cf2a TG |
1748 | assert(link); |
1749 | ||
5da8149f | 1750 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1751 | return 1; |
1752 | ||
1c4baffc | 1753 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d LP |
1754 | if (r < 0) |
1755 | /* we warn but don't fail the link, as it may be | |
1756 | brought up later */ | |
a2fae7bb | 1757 | log_link_warning_errno(link, r, "Could not bring up interface: %m"); |
45ad2c13 | 1758 | |
f882c247 TG |
1759 | return 1; |
1760 | } | |
1761 | ||
14b6bb77 | 1762 | int link_up(Link *link) { |
4afd3348 | 1763 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c3072ea | 1764 | uint8_t ipv6ll_mode; |
f579559b TG |
1765 | int r; |
1766 | ||
f882c247 | 1767 | assert(link); |
c106cc36 | 1768 | assert(link->network); |
f882c247 TG |
1769 | assert(link->manager); |
1770 | assert(link->manager->rtnl); | |
1771 | ||
6a7a4e4d | 1772 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1773 | |
6a7a4e4d LP |
1774 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1775 | if (r < 0) | |
1776 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1777 | |
2b2d8603 | 1778 | /* set it free if not enslaved with networkd */ |
6cb955c6 | 1779 | if (!link->network->bridge && !link->network->bond && !link->network->vrf) { |
2b2d8603 TY |
1780 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); |
1781 | if (r < 0) | |
1782 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
1783 | } | |
1784 | ||
5d4795f3 | 1785 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1786 | if (r < 0) |
1787 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1788 | |
c106cc36 | 1789 | if (link->network->mac) { |
1c4baffc | 1790 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1791 | if (r < 0) |
1792 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1793 | } |
1794 | ||
1c4baffc | 1795 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1796 | if (r < 0) |
1797 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1798 | |
439689c6 | 1799 | if (link_ipv6_enabled(link)) { |
01d28f81 | 1800 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ |
1c4baffc | 1801 | r = sd_netlink_message_open_container(req, AF_INET6); |
01d28f81 TG |
1802 | if (r < 0) |
1803 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1804 | |
fbc38f23 TY |
1805 | if (!link_ipv6ll_enabled(link)) |
1806 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
1807 | else { | |
1808 | const char *p = NULL; | |
1809 | _cleanup_free_ char *stable_secret = NULL; | |
1810 | ||
1811 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/stable_secret"); | |
1812 | r = read_one_line_file(p, &stable_secret); | |
1813 | ||
1814 | if (r < 0) | |
1815 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1816 | else | |
1817 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1818 | } | |
1c4baffc | 1819 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); |
01d28f81 TG |
1820 | if (r < 0) |
1821 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1822 | |
01d28f81 | 1823 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1c4baffc | 1824 | r = sd_netlink_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); |
01d28f81 TG |
1825 | if (r < 0) |
1826 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1827 | } | |
1828 | ||
1c4baffc | 1829 | r = sd_netlink_message_close_container(req); |
6a7a4e4d | 1830 | if (r < 0) |
01d28f81 | 1831 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1832 | } |
d0d6a4cd | 1833 | |
1c4baffc | 1834 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1835 | if (r < 0) |
1836 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1837 | |
1046bf9b YW |
1838 | r = sd_netlink_call_async(link->manager->rtnl, req, link_up_handler, |
1839 | link_netlink_destroy_callback, link, 0, NULL); | |
6a7a4e4d LP |
1840 | if (r < 0) |
1841 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1842 | |
b226d99b TG |
1843 | link_ref(link); |
1844 | ||
f882c247 TG |
1845 | return 0; |
1846 | } | |
1847 | ||
bd5038f8 MKB |
1848 | static int link_up_can(Link *link) { |
1849 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1850 | int r; | |
1851 | ||
1852 | assert(link); | |
1853 | ||
1854 | log_link_debug(link, "Bringing CAN link up"); | |
1855 | ||
1856 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1857 | if (r < 0) | |
1858 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1859 | ||
1860 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
1861 | if (r < 0) | |
1862 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1863 | ||
1046bf9b YW |
1864 | r = sd_netlink_call_async(link->manager->rtnl, req, link_up_handler, |
1865 | link_netlink_destroy_callback, link, 0, NULL); | |
bd5038f8 MKB |
1866 | if (r < 0) |
1867 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1868 | ||
1869 | link_ref(link); | |
1870 | ||
1871 | return 0; | |
1872 | } | |
1873 | ||
06828bb6 HP |
1874 | static int link_set_can(Link *link) { |
1875 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
1876 | int r; | |
1877 | ||
1878 | assert(link); | |
1879 | assert(link->network); | |
1880 | assert(link->manager); | |
1881 | assert(link->manager->rtnl); | |
1882 | ||
1883 | log_link_debug(link, "link_set_can"); | |
1884 | ||
1885 | r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, link->ifindex); | |
1886 | if (r < 0) | |
1887 | return log_link_error_errno(link, r, "Failed to allocate netlink message: %m"); | |
1888 | ||
1889 | r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK); | |
1890 | if (r < 0) | |
1891 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1892 | ||
1893 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
1894 | if (r < 0) | |
1895 | return log_link_error_errno(link, r, "Failed to open netlink container: %m"); | |
1896 | ||
1897 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind); | |
1898 | if (r < 0) | |
1899 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1900 | ||
1901 | if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) { | |
1902 | struct can_bittiming bt = { | |
1903 | .bitrate = link->network->can_bitrate, | |
1904 | .sample_point = link->network->can_sample_point, | |
1905 | }; | |
1906 | ||
1907 | if (link->network->can_bitrate > UINT32_MAX) { | |
1908 | log_link_error(link, "bitrate (%zu) too big.", link->network->can_bitrate); | |
1909 | return -ERANGE; | |
1910 | } | |
1911 | ||
1912 | log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate); | |
1913 | if (link->network->can_sample_point > 0) | |
1914 | log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10); | |
1915 | else | |
1916 | log_link_debug(link, "Using default sample point"); | |
1917 | ||
1918 | r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); | |
1919 | if (r < 0) | |
1920 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m"); | |
1921 | } | |
1922 | ||
1923 | if (link->network->can_restart_us > 0) { | |
1924 | char time_string[FORMAT_TIMESPAN_MAX]; | |
1925 | uint64_t restart_ms; | |
1926 | ||
1927 | if (link->network->can_restart_us == USEC_INFINITY) | |
1928 | restart_ms = 0; | |
1929 | else | |
1930 | restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC); | |
1931 | ||
1932 | format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC); | |
1933 | ||
1934 | if (restart_ms > UINT32_MAX) { | |
1935 | log_link_error(link, "restart timeout (%s) too big.", time_string); | |
1936 | return -ERANGE; | |
1937 | } | |
1938 | ||
1939 | log_link_debug(link, "Setting restart = %s", time_string); | |
1940 | ||
1941 | r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms); | |
1942 | if (r < 0) | |
1943 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m"); | |
1944 | } | |
1945 | ||
1946 | r = sd_netlink_message_close_container(m); | |
1947 | if (r < 0) | |
1948 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
1949 | ||
1950 | r = sd_netlink_message_close_container(m); | |
1951 | if (r < 0) | |
1952 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
1953 | ||
1046bf9b YW |
1954 | r = sd_netlink_call_async(link->manager->rtnl, m, link_set_handler, |
1955 | link_netlink_destroy_callback, link, 0, NULL); | |
06828bb6 HP |
1956 | if (r < 0) |
1957 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1958 | ||
1959 | link_ref(link); | |
1960 | ||
1961 | if (!(link->flags & IFF_UP)) { | |
1962 | r = link_up_can(link); | |
1963 | if (r < 0) { | |
1964 | link_enter_failed(link); | |
1965 | return r; | |
1966 | } | |
1967 | } | |
1968 | ||
1969 | log_link_debug(link, "link_set_can done"); | |
1970 | ||
1971 | return r; | |
1972 | } | |
1973 | ||
1c4baffc | 1974 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1975 | Link *link = userdata; |
0d4ad91d AR |
1976 | int r; |
1977 | ||
1978 | assert(link); | |
1979 | ||
1980 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1981 | return 1; | |
1982 | ||
1c4baffc | 1983 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1984 | if (r < 0) |
a2fae7bb | 1985 | log_link_warning_errno(link, r, "Could not bring down interface: %m"); |
0d4ad91d | 1986 | |
1046bf9b | 1987 | if (streq_ptr(link->kind, "can")) |
06828bb6 | 1988 | link_set_can(link); |
06828bb6 | 1989 | |
0d4ad91d AR |
1990 | return 1; |
1991 | } | |
1992 | ||
14b6bb77 | 1993 | int link_down(Link *link) { |
4afd3348 | 1994 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1995 | int r; |
1996 | ||
1997 | assert(link); | |
1998 | assert(link->manager); | |
1999 | assert(link->manager->rtnl); | |
2000 | ||
6a7a4e4d | 2001 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
2002 | |
2003 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
2004 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
2005 | if (r < 0) |
2006 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
2007 | |
2008 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
2009 | if (r < 0) |
2010 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 2011 | |
1046bf9b YW |
2012 | r = sd_netlink_call_async(link->manager->rtnl, req, link_down_handler, |
2013 | link_netlink_destroy_callback, link, 0, NULL); | |
6a7a4e4d LP |
2014 | if (r < 0) |
2015 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
2016 | |
2017 | link_ref(link); | |
2018 | ||
2019 | return 0; | |
2020 | } | |
2021 | ||
2022 | static int link_handle_bound_to_list(Link *link) { | |
2023 | Link *l; | |
2024 | Iterator i; | |
2025 | int r; | |
2026 | bool required_up = false; | |
2027 | bool link_is_up = false; | |
2028 | ||
2029 | assert(link); | |
2030 | ||
2031 | if (hashmap_isempty(link->bound_to_links)) | |
2032 | return 0; | |
2033 | ||
2034 | if (link->flags & IFF_UP) | |
2035 | link_is_up = true; | |
2036 | ||
2037 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
2038 | if (link_has_carrier(l)) { | |
2039 | required_up = true; | |
2040 | break; | |
2041 | } | |
2042 | ||
2043 | if (!required_up && link_is_up) { | |
2044 | r = link_down(link); | |
2045 | if (r < 0) | |
2046 | return r; | |
2047 | } else if (required_up && !link_is_up) { | |
2048 | r = link_up(link); | |
2049 | if (r < 0) | |
2050 | return r; | |
2051 | } | |
2052 | ||
2053 | return 0; | |
2054 | } | |
2055 | ||
2056 | static int link_handle_bound_by_list(Link *link) { | |
2057 | Iterator i; | |
2058 | Link *l; | |
2059 | int r; | |
2060 | ||
2061 | assert(link); | |
2062 | ||
2063 | if (hashmap_isempty(link->bound_by_links)) | |
2064 | return 0; | |
2065 | ||
2066 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
2067 | r = link_handle_bound_to_list(l); | |
2068 | if (r < 0) | |
2069 | return r; | |
2070 | } | |
2071 | ||
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
2076 | int r; | |
2077 | ||
2078 | assert(link); | |
2079 | assert(carrier); | |
2080 | ||
2081 | if (link == carrier) | |
2082 | return 0; | |
2083 | ||
2084 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
2085 | return 0; | |
2086 | ||
2087 | r = hashmap_ensure_allocated(h, NULL); | |
2088 | if (r < 0) | |
2089 | return r; | |
2090 | ||
2091 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
2092 | if (r < 0) | |
2093 | return r; | |
2094 | ||
2095 | return 0; | |
2096 | } | |
2097 | ||
2098 | static int link_new_bound_by_list(Link *link) { | |
2099 | Manager *m; | |
2100 | Link *carrier; | |
2101 | Iterator i; | |
2102 | int r; | |
2103 | bool list_updated = false; | |
2104 | ||
2105 | assert(link); | |
2106 | assert(link->manager); | |
2107 | ||
2108 | m = link->manager; | |
2109 | ||
b295beea | 2110 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
2111 | if (!carrier->network) |
2112 | continue; | |
2113 | ||
2114 | if (strv_isempty(carrier->network->bind_carrier)) | |
2115 | continue; | |
2116 | ||
2117 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
2118 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
2119 | if (r < 0) | |
2120 | return r; | |
2121 | ||
2122 | list_updated = true; | |
2123 | } | |
2124 | } | |
2125 | ||
2126 | if (list_updated) | |
84de38c5 | 2127 | link_dirty(link); |
0d4ad91d | 2128 | |
b295beea | 2129 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
2130 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
2131 | if (r < 0) | |
2132 | return r; | |
2133 | ||
84de38c5 | 2134 | link_dirty(carrier); |
0d4ad91d AR |
2135 | } |
2136 | ||
2137 | return 0; | |
2138 | } | |
2139 | ||
2140 | static int link_new_bound_to_list(Link *link) { | |
2141 | Manager *m; | |
2142 | Link *carrier; | |
2143 | Iterator i; | |
2144 | int r; | |
2145 | bool list_updated = false; | |
2146 | ||
2147 | assert(link); | |
2148 | assert(link->manager); | |
2149 | ||
2150 | if (!link->network) | |
2151 | return 0; | |
2152 | ||
2153 | if (strv_isempty(link->network->bind_carrier)) | |
2154 | return 0; | |
2155 | ||
2156 | m = link->manager; | |
2157 | ||
2158 | HASHMAP_FOREACH (carrier, m->links, i) { | |
2159 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
2160 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
2161 | if (r < 0) | |
2162 | return r; | |
2163 | ||
2164 | list_updated = true; | |
2165 | } | |
2166 | } | |
2167 | ||
2168 | if (list_updated) | |
84de38c5 | 2169 | link_dirty(link); |
0d4ad91d AR |
2170 | |
2171 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
2172 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
2173 | if (r < 0) | |
2174 | return r; | |
2175 | ||
84de38c5 | 2176 | link_dirty(carrier); |
0d4ad91d AR |
2177 | } |
2178 | ||
2179 | return 0; | |
2180 | } | |
2181 | ||
2182 | static int link_new_carrier_maps(Link *link) { | |
2183 | int r; | |
2184 | ||
2185 | r = link_new_bound_by_list(link); | |
2186 | if (r < 0) | |
2187 | return r; | |
2188 | ||
2189 | r = link_handle_bound_by_list(link); | |
2190 | if (r < 0) | |
2191 | return r; | |
2192 | ||
2193 | r = link_new_bound_to_list(link); | |
2194 | if (r < 0) | |
2195 | return r; | |
2196 | ||
2197 | r = link_handle_bound_to_list(link); | |
2198 | if (r < 0) | |
2199 | return r; | |
2200 | ||
2201 | return 0; | |
2202 | } | |
2203 | ||
2204 | static void link_free_bound_to_list(Link *link) { | |
2205 | Link *bound_to; | |
2206 | Iterator i; | |
2207 | ||
2208 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
2209 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
2210 | ||
2211 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 2212 | link_dirty(bound_to); |
0d4ad91d AR |
2213 | } |
2214 | ||
2215 | return; | |
2216 | } | |
2217 | ||
2218 | static void link_free_bound_by_list(Link *link) { | |
2219 | Link *bound_by; | |
2220 | Iterator i; | |
2221 | ||
2222 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
2223 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
2224 | ||
2225 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 2226 | link_dirty(bound_by); |
0d4ad91d AR |
2227 | link_handle_bound_to_list(bound_by); |
2228 | } | |
2229 | } | |
2230 | ||
2231 | return; | |
2232 | } | |
2233 | ||
2234 | static void link_free_carrier_maps(Link *link) { | |
2235 | bool list_updated = false; | |
2236 | ||
2237 | assert(link); | |
2238 | ||
2239 | if (!hashmap_isempty(link->bound_to_links)) { | |
2240 | link_free_bound_to_list(link); | |
2241 | list_updated = true; | |
2242 | } | |
2243 | ||
2244 | if (!hashmap_isempty(link->bound_by_links)) { | |
2245 | link_free_bound_by_list(link); | |
2246 | list_updated = true; | |
2247 | } | |
2248 | ||
2249 | if (list_updated) | |
84de38c5 | 2250 | link_dirty(link); |
0d4ad91d AR |
2251 | |
2252 | return; | |
2253 | } | |
2254 | ||
2255 | void link_drop(Link *link) { | |
2256 | if (!link || link->state == LINK_STATE_LINGER) | |
2257 | return; | |
2258 | ||
2259 | link_set_state(link, LINK_STATE_LINGER); | |
2260 | ||
2261 | link_free_carrier_maps(link); | |
2262 | ||
6a7a4e4d | 2263 | log_link_debug(link, "Link removed"); |
0d4ad91d | 2264 | |
db2f8a2e | 2265 | (void) unlink(link->state_file); |
0d4ad91d AR |
2266 | link_unref(link); |
2267 | ||
2268 | return; | |
2269 | } | |
2270 | ||
3f265037 | 2271 | static int link_joined(Link *link) { |
f882c247 TG |
2272 | int r; |
2273 | ||
ef1ba606 | 2274 | assert(link); |
f5be5601 | 2275 | assert(link->network); |
dd3efc09 | 2276 | |
0d4ad91d AR |
2277 | if (!hashmap_isempty(link->bound_to_links)) { |
2278 | r = link_handle_bound_to_list(link); | |
2279 | if (r < 0) | |
2280 | return r; | |
2281 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
2282 | r = link_up(link); |
2283 | if (r < 0) { | |
2284 | link_enter_failed(link); | |
2285 | return r; | |
2286 | } | |
ef1ba606 | 2287 | } |
f882c247 | 2288 | |
9ed794a3 | 2289 | if (link->network->bridge) { |
e1853b00 | 2290 | r = link_set_bridge(link); |
6a7a4e4d LP |
2291 | if (r < 0) |
2292 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
2293 | } |
2294 | ||
eb64b435 SS |
2295 | if (link->network->bond) { |
2296 | r = link_bond_set(link); | |
2297 | if (r < 0) | |
2298 | log_link_error_errno(link, r, "Could not set bond message: %m"); | |
2299 | } | |
2300 | ||
ffff9abe TJ |
2301 | if (link->network->use_br_vlan && |
2302 | (link->network->bridge || streq_ptr("bridge", link->kind))) { | |
13b498f9 TJ |
2303 | r = link_set_bridge_vlan(link); |
2304 | if (r < 0) | |
2305 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2306 | } | |
2307 | ||
c1835a42 RM |
2308 | /* Skip setting up addresses until it gets carrier, |
2309 | or it would try to set addresses twice, | |
2310 | which is bad for non-idempotent steps. */ | |
dad2d78e | 2311 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
c1835a42 RM |
2312 | return 0; |
2313 | ||
fb6730c4 | 2314 | return link_enter_set_addresses(link); |
02b59d57 TG |
2315 | } |
2316 | ||
62e2d5bb | 2317 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 2318 | Link *link = userdata; |
02b59d57 TG |
2319 | int r; |
2320 | ||
1746cf2a | 2321 | assert(link); |
ef1ba606 | 2322 | assert(link->network); |
02b59d57 | 2323 | |
313cefa1 | 2324 | link->enslaving--; |
52433f6b | 2325 | |
5da8149f | 2326 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2327 | return 1; |
2328 | ||
1c4baffc | 2329 | r = sd_netlink_message_get_errno(m); |
856f962c | 2330 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 2331 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
2332 | link_enter_failed(link); |
2333 | return 1; | |
ba179154 | 2334 | } else |
6a7a4e4d | 2335 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 2336 | |
856f962c | 2337 | if (link->enslaving <= 0) |
3f265037 | 2338 | link_joined(link); |
02b59d57 TG |
2339 | |
2340 | return 1; | |
2341 | } | |
2342 | ||
3f265037 | 2343 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2344 | NetDev *netdev; |
672682a6 | 2345 | Iterator i; |
02b59d57 TG |
2346 | int r; |
2347 | ||
2348 | assert(link); | |
2349 | assert(link->network); | |
8434fd5c | 2350 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 2351 | |
e331e246 | 2352 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 2353 | |
84de38c5 | 2354 | link_dirty(link); |
fe8db0c5 | 2355 | |
7951dea2 SS |
2356 | if (!link->network->bridge && |
2357 | !link->network->bond && | |
6cb955c6 | 2358 | !link->network->vrf && |
6a0a2f86 | 2359 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 2360 | return link_joined(link); |
02b59d57 | 2361 | |
d9c67ea1 | 2362 | if (link->network->bond) { |
f2341e0a LP |
2363 | log_struct(LOG_DEBUG, |
2364 | LOG_LINK_INTERFACE(link), | |
2365 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2366 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname)); |
f2341e0a LP |
2367 | |
2368 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2369 | if (r < 0) { |
f2341e0a LP |
2370 | log_struct_errno(LOG_WARNING, r, |
2371 | LOG_LINK_INTERFACE(link), | |
2372 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2373 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname)); |
52433f6b TG |
2374 | link_enter_failed(link); |
2375 | return r; | |
2376 | } | |
2377 | ||
313cefa1 | 2378 | link->enslaving++; |
0ad6148e MO |
2379 | } |
2380 | ||
d9c67ea1 | 2381 | if (link->network->bridge) { |
f2341e0a LP |
2382 | log_struct(LOG_DEBUG, |
2383 | LOG_LINK_INTERFACE(link), | |
2384 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2385 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname)); |
f2341e0a LP |
2386 | |
2387 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2388 | if (r < 0) { |
f2341e0a LP |
2389 | log_struct_errno(LOG_WARNING, r, |
2390 | LOG_LINK_INTERFACE(link), | |
2391 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2392 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname)); |
0ad6148e MO |
2393 | link_enter_failed(link); |
2394 | return r; | |
2395 | } | |
2396 | ||
313cefa1 | 2397 | link->enslaving++; |
52433f6b TG |
2398 | } |
2399 | ||
6cb955c6 AR |
2400 | if (link->network->vrf) { |
2401 | log_struct(LOG_DEBUG, | |
2402 | LOG_LINK_INTERFACE(link), | |
2403 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 ZJS |
2404 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname)); |
2405 | ||
6cb955c6 AR |
2406 | r = netdev_join(link->network->vrf, link, netdev_join_handler); |
2407 | if (r < 0) { | |
2408 | log_struct_errno(LOG_WARNING, r, | |
2409 | LOG_LINK_INTERFACE(link), | |
2410 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 | 2411 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname)); |
6cb955c6 AR |
2412 | link_enter_failed(link); |
2413 | return r; | |
2414 | } | |
2415 | ||
2416 | link->enslaving++; | |
2417 | } | |
2418 | ||
6a0a2f86 | 2419 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2420 | |
a63e5daa SS |
2421 | if (netdev->ifindex > 0) { |
2422 | link_joined(link); | |
2423 | continue; | |
2424 | } | |
2425 | ||
f2341e0a LP |
2426 | log_struct(LOG_DEBUG, |
2427 | LOG_LINK_INTERFACE(link), | |
2428 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2429 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); |
f2341e0a LP |
2430 | |
2431 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2432 | if (r < 0) { |
f2341e0a LP |
2433 | log_struct_errno(LOG_WARNING, r, |
2434 | LOG_LINK_INTERFACE(link), | |
2435 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2436 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); |
326cb406 SS |
2437 | link_enter_failed(link); |
2438 | return r; | |
2439 | } | |
2440 | ||
313cefa1 | 2441 | link->enslaving++; |
326cb406 SS |
2442 | } |
2443 | ||
ef1ba606 TG |
2444 | return 0; |
2445 | } | |
2446 | ||
769d324c | 2447 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2448 | int r; |
2449 | ||
765afd5c | 2450 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2451 | return 0; |
2452 | ||
765afd5c LP |
2453 | /* We propagate the forwarding flag from one interface to the |
2454 | * global setting one way. This means: as long as at least one | |
2455 | * interface was configured at any time that had IP forwarding | |
2456 | * enabled the setting will stay on for good. We do this | |
2457 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2458 | * somewhat in sync (see below). */ | |
15dee3f0 | 2459 | |
765afd5c | 2460 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2461 | if (r < 0) |
765afd5c | 2462 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2463 | |
769d324c LP |
2464 | return 0; |
2465 | } | |
2466 | ||
2467 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2468 | int r; |
2469 | ||
765afd5c | 2470 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2471 | return 0; |
2472 | ||
61233823 | 2473 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2474 | * packet forwarding setting: either packet forwarding is on |
2475 | * for all, or off for all. We hence don't bother with a | |
2476 | * per-interface setting, but simply propagate the interface | |
2477 | * flag, if it is set, to the global flag, one-way. Note that | |
2478 | * while IPv4 would allow a per-interface flag, we expose the | |
2479 | * same behaviour there and also propagate the setting from | |
2480 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2481 | |
765afd5c | 2482 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2483 | if (r < 0) |
765afd5c | 2484 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2485 | |
2486 | return 0; | |
2487 | } | |
2488 | ||
49092e22 | 2489 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2490 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2491 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2492 | const char *p = NULL; |
2493 | int r; | |
2494 | ||
1f0d9695 | 2495 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2496 | if (s < 0) |
49092e22 SS |
2497 | return 0; |
2498 | ||
2499 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2500 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2501 | |
eb3da901 LP |
2502 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2503 | if (r < 0) | |
49092e22 SS |
2504 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2505 | ||
2506 | return 0; | |
2507 | } | |
2508 | ||
4f2e437a | 2509 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2510 | const char *p = NULL; |
4f2e437a SS |
2511 | int r; |
2512 | ||
2513 | /* Make this a NOP if IPv6 is not available */ | |
2514 | if (!socket_ipv6_is_supported()) | |
2515 | return 0; | |
2516 | ||
2517 | if (link->flags & IFF_LOOPBACK) | |
2518 | return 0; | |
2519 | ||
d68e2e59 LP |
2520 | if (!link->network) |
2521 | return 0; | |
2522 | ||
4f2e437a | 2523 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2524 | |
0053598f | 2525 | /* We handle router advertisements ourselves, tell the kernel to GTFO */ |
fe307276 | 2526 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2527 | if (r < 0) |
fe307276 | 2528 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2529 | |
2530 | return 0; | |
2531 | } | |
2532 | ||
8749cbcd | 2533 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2534 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2535 | const char *p = NULL; |
2536 | int r; | |
2537 | ||
2538 | /* Make this a NOP if IPv6 is not available */ | |
2539 | if (!socket_ipv6_is_supported()) | |
2540 | return 0; | |
2541 | ||
2542 | if (link->flags & IFF_LOOPBACK) | |
2543 | return 0; | |
2544 | ||
d68e2e59 LP |
2545 | if (!link->network) |
2546 | return 0; | |
2547 | ||
8749cbcd SS |
2548 | if (link->network->ipv6_dad_transmits < 0) |
2549 | return 0; | |
2550 | ||
2551 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2552 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2553 | |
eb3da901 LP |
2554 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2555 | if (r < 0) | |
8749cbcd | 2556 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2557 | |
2558 | return 0; | |
2559 | } | |
2560 | ||
b69c3180 | 2561 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2562 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2563 | const char *p = NULL; |
2564 | int r; | |
2565 | ||
2566 | /* Make this a NOP if IPv6 is not available */ | |
2567 | if (!socket_ipv6_is_supported()) | |
2568 | return 0; | |
2569 | ||
2570 | if (link->flags & IFF_LOOPBACK) | |
2571 | return 0; | |
2572 | ||
d68e2e59 LP |
2573 | if (!link->network) |
2574 | return 0; | |
2575 | ||
b69c3180 SS |
2576 | if (link->network->ipv6_hop_limit < 0) |
2577 | return 0; | |
2578 | ||
2579 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2580 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2581 | |
eb3da901 LP |
2582 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2583 | if (r < 0) | |
b69c3180 | 2584 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2585 | |
2586 | return 0; | |
2587 | } | |
2588 | ||
11102cba SS |
2589 | static int link_set_ipv6_mtu(Link *link) { |
2590 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; | |
2591 | const char *p = NULL; | |
2592 | int r; | |
2593 | ||
2594 | /* Make this a NOP if IPv6 is not available */ | |
2595 | if (!socket_ipv6_is_supported()) | |
2596 | return 0; | |
2597 | ||
2598 | if (link->flags & IFF_LOOPBACK) | |
2599 | return 0; | |
2600 | ||
2601 | if (link->network->ipv6_mtu == 0) | |
2602 | return 0; | |
2603 | ||
2604 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/mtu"); | |
2605 | ||
4e964aa0 | 2606 | xsprintf(buf, "%" PRIu32, link->network->ipv6_mtu); |
11102cba SS |
2607 | |
2608 | r = write_string_file(p, buf, 0); | |
2609 | if (r < 0) | |
2610 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m"); | |
2611 | ||
2612 | return 0; | |
2613 | } | |
2614 | ||
5e5b137a TG |
2615 | static int link_drop_foreign_config(Link *link) { |
2616 | Address *address; | |
2617 | Route *route; | |
2618 | Iterator i; | |
2619 | int r; | |
2620 | ||
2621 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2622 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2623 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2624 | continue; | |
2625 | ||
2626 | r = address_remove(address, link, link_address_remove_handler); | |
2627 | if (r < 0) | |
2628 | return r; | |
2629 | } | |
2630 | ||
2631 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2632 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2633 | if (route->protocol == RTPROT_KERNEL) |
2634 | continue; | |
2635 | ||
3fb1ac5d | 2636 | r = route_remove(route, link, link_route_remove_handler); |
5e5b137a TG |
2637 | if (r < 0) |
2638 | return r; | |
2639 | } | |
2640 | ||
2641 | return 0; | |
2642 | } | |
2643 | ||
3104883d | 2644 | static int link_drop_config(Link *link) { |
410a7f15 | 2645 | Address *address, *pool_address; |
3104883d SS |
2646 | Route *route; |
2647 | Iterator i; | |
2648 | int r; | |
2649 | ||
2650 | SET_FOREACH(address, link->addresses, i) { | |
2651 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
2652 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) | |
2653 | continue; | |
2654 | ||
2655 | r = address_remove(address, link, link_address_remove_handler); | |
2656 | if (r < 0) | |
2657 | return r; | |
410a7f15 RM |
2658 | |
2659 | /* If this address came from an address pool, clean up the pool */ | |
2660 | LIST_FOREACH(addresses, pool_address, link->pool_addresses) { | |
2661 | if (address_equal(address, pool_address)) { | |
2662 | LIST_REMOVE(addresses, link->pool_addresses, pool_address); | |
2663 | address_free(pool_address); | |
2664 | break; | |
2665 | } | |
2666 | } | |
3104883d SS |
2667 | } |
2668 | ||
2669 | SET_FOREACH(route, link->routes, i) { | |
2670 | /* do not touch routes managed by the kernel */ | |
2671 | if (route->protocol == RTPROT_KERNEL) | |
2672 | continue; | |
2673 | ||
2674 | r = route_remove(route, link, link_route_remove_handler); | |
2675 | if (r < 0) | |
2676 | return r; | |
2677 | } | |
2678 | ||
c69305ff LP |
2679 | ndisc_flush(link); |
2680 | ||
3104883d SS |
2681 | return 0; |
2682 | } | |
2683 | ||
273eec24 LP |
2684 | static int link_update_lldp(Link *link) { |
2685 | int r; | |
2686 | ||
2687 | assert(link); | |
2688 | ||
2689 | if (!link->lldp) | |
2690 | return 0; | |
2691 | ||
2692 | if (link->flags & IFF_UP) { | |
2693 | r = sd_lldp_start(link->lldp); | |
2694 | if (r > 0) | |
2695 | log_link_debug(link, "Started LLDP."); | |
2696 | } else { | |
2697 | r = sd_lldp_stop(link->lldp); | |
2698 | if (r > 0) | |
2699 | log_link_debug(link, "Stopped LLDP."); | |
2700 | } | |
2701 | ||
2702 | return r; | |
2703 | } | |
2704 | ||
93ea7750 MKB |
2705 | static int link_configure_can(Link *link) { |
2706 | int r; | |
2707 | ||
06828bb6 HP |
2708 | if (streq_ptr(link->kind, "can")) { |
2709 | /* The CAN interface must be down to configure bitrate, etc... */ | |
2710 | if ((link->flags & IFF_UP)) { | |
2711 | r = link_down(link); | |
2712 | if (r < 0) { | |
2713 | link_enter_failed(link); | |
2714 | return r; | |
2715 | } | |
2716 | ||
2717 | return 0; | |
2718 | } | |
2719 | ||
2720 | return link_set_can(link); | |
2721 | } | |
2722 | ||
93ea7750 MKB |
2723 | if (!(link->flags & IFF_UP)) { |
2724 | r = link_up_can(link); | |
2725 | if (r < 0) { | |
2726 | link_enter_failed(link); | |
2727 | return r; | |
2728 | } | |
2729 | } | |
2730 | ||
2731 | return 0; | |
2732 | } | |
2733 | ||
a748b692 | 2734 | static int link_configure(Link *link) { |
02b59d57 TG |
2735 | int r; |
2736 | ||
ef1ba606 | 2737 | assert(link); |
b22d8a00 | 2738 | assert(link->network); |
8434fd5c | 2739 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2740 | |
06828bb6 | 2741 | if (STRPTR_IN_SET(link->kind, "can", "vcan")) |
93ea7750 | 2742 | return link_configure_can(link); |
92c918b0 | 2743 | |
02e28621 ДГ |
2744 | /* Drop foreign config, but ignore loopback or critical devices. |
2745 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2746 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2747 | r = link_drop_foreign_config(link); |
2748 | if (r < 0) | |
2749 | return r; | |
2750 | } | |
5e5b137a | 2751 | |
23d8b221 SS |
2752 | r = link_set_proxy_arp(link); |
2753 | if (r < 0) | |
2754 | return r; | |
2755 | ||
a0e5c15d FK |
2756 | r = ipv6_proxy_ndp_addresses_configure(link); |
2757 | if (r < 0) | |
2758 | return r; | |
2759 | ||
769d324c LP |
2760 | r = link_set_ipv4_forward(link); |
2761 | if (r < 0) | |
2762 | return r; | |
2763 | ||
2764 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2765 | if (r < 0) |
2766 | return r; | |
2767 | ||
49092e22 SS |
2768 | r = link_set_ipv6_privacy_extensions(link); |
2769 | if (r < 0) | |
2770 | return r; | |
2771 | ||
4f2e437a SS |
2772 | r = link_set_ipv6_accept_ra(link); |
2773 | if (r < 0) | |
2774 | return r; | |
2775 | ||
8749cbcd SS |
2776 | r = link_set_ipv6_dad_transmits(link); |
2777 | if (r < 0) | |
2778 | return r; | |
b69c3180 SS |
2779 | |
2780 | r = link_set_ipv6_hop_limit(link); | |
2781 | if (r < 0) | |
2782 | return r; | |
8749cbcd | 2783 | |
99d2baa2 SS |
2784 | r = link_set_flags(link); |
2785 | if (r < 0) | |
2786 | return r; | |
2787 | ||
11102cba SS |
2788 | r = link_set_ipv6_mtu(link); |
2789 | if (r < 0) | |
2790 | return r; | |
2791 | ||
78c958f8 | 2792 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2793 | r = ipv4ll_configure(link); |
eb34d4af TG |
2794 | if (r < 0) |
2795 | return r; | |
2796 | } | |
2797 | ||
78c958f8 | 2798 | if (link_dhcp4_enabled(link)) { |
64b21ece MV |
2799 | r = dhcp4_set_promote_secondaries(link); |
2800 | if (r < 0) | |
2801 | return r; | |
2802 | ||
3c9b8860 | 2803 | r = dhcp4_configure(link); |
eb34d4af TG |
2804 | if (r < 0) |
2805 | return r; | |
eb34d4af TG |
2806 | } |
2807 | ||
78c958f8 | 2808 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2809 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2810 | if (r < 0) | |
2811 | return r; | |
2812 | ||
2813 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2814 | if (r < 0) | |
2815 | return r; | |
dd43110f TG |
2816 | } |
2817 | ||
62379e88 TG |
2818 | if (link_dhcp6_enabled(link) || |
2819 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2820 | r = dhcp6_configure(link); |
2821 | if (r < 0) | |
2822 | return r; | |
2823 | } | |
2824 | ||
2825 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2826 | r = ndisc_configure(link); |
4138fb2c PF |
2827 | if (r < 0) |
2828 | return r; | |
2829 | } | |
2830 | ||
7465dd22 PF |
2831 | if (link_radv_enabled(link)) { |
2832 | r = radv_configure(link); | |
2833 | if (r < 0) | |
2834 | return r; | |
2835 | } | |
2836 | ||
8e1ad1ea | 2837 | if (link_lldp_rx_enabled(link)) { |
fc6a313b LP |
2838 | r = sd_lldp_new(&link->lldp); |
2839 | if (r < 0) | |
2840 | return r; | |
2841 | ||
2842 | r = sd_lldp_set_ifindex(link->lldp, link->ifindex); | |
34437b4f LP |
2843 | if (r < 0) |
2844 | return r; | |
2845 | ||
2846 | r = sd_lldp_match_capabilities(link->lldp, | |
2847 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2848 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2849 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2850 | if (r < 0) |
2851 | return r; | |
2852 | ||
b553a6b1 LP |
2853 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2854 | if (r < 0) | |
2855 | return r; | |
2856 | ||
ce43e484 SS |
2857 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2858 | if (r < 0) | |
2859 | return r; | |
49699bac | 2860 | |
032b27f5 | 2861 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2862 | if (r < 0) |
2863 | return r; | |
273eec24 LP |
2864 | |
2865 | r = link_update_lldp(link); | |
2866 | if (r < 0) | |
2867 | return r; | |
ce43e484 SS |
2868 | } |
2869 | ||
44b598a1 SS |
2870 | if (link->network->mtu > 0) { |
2871 | r = link_set_mtu(link, link->network->mtu); | |
2872 | if (r < 0) | |
2873 | return r; | |
2874 | } | |
2875 | ||
55dc8c4a YW |
2876 | return link_configure_after_setting_mtu(link); |
2877 | } | |
2878 | ||
2879 | static int link_configure_after_setting_mtu(Link *link) { | |
2880 | int r; | |
2881 | ||
2882 | assert(link); | |
2883 | assert(link->network); | |
2884 | assert(link->state == LINK_STATE_PENDING); | |
2885 | ||
2886 | if (link->setting_mtu) | |
2887 | return 0; | |
2888 | ||
dad2d78e | 2889 | if (link_has_carrier(link) || link->network->configure_without_carrier) { |
1e9be60b TG |
2890 | r = link_acquire_conf(link); |
2891 | if (r < 0) | |
2892 | return r; | |
cc544d5f | 2893 | } |
1e9be60b | 2894 | |
3f265037 | 2895 | return link_enter_join_netdev(link); |
505f8da7 TG |
2896 | } |
2897 | ||
27dfc982 YW |
2898 | static int duid_set_uuid(DUID *duid, sd_id128_t uuid) { |
2899 | assert(duid); | |
2900 | ||
2901 | if (duid->raw_data_len > 0) | |
2902 | return 0; | |
2903 | ||
2904 | if (duid->type != DUID_TYPE_UUID) | |
2905 | return -EINVAL; | |
2906 | ||
2907 | memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t)); | |
2908 | duid->raw_data_len = sizeof(sd_id128_t); | |
2909 | ||
2910 | return 1; | |
2911 | } | |
2912 | ||
2913 | int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
2914 | Manager *manager = userdata; | |
2915 | const sd_bus_error *e; | |
2916 | const void *a; | |
2917 | size_t sz; | |
2918 | DUID *duid; | |
2919 | Link *link; | |
2920 | int r; | |
2921 | ||
2922 | assert(m); | |
2923 | assert(manager); | |
2924 | ||
2925 | e = sd_bus_message_get_error(m); | |
2926 | if (e) { | |
2927 | log_error_errno(sd_bus_error_get_errno(e), | |
ab4dd984 | 2928 | "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s", |
27dfc982 YW |
2929 | e->message); |
2930 | goto configure; | |
2931 | } | |
2932 | ||
2933 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
2934 | if (r < 0) | |
2935 | goto configure; | |
2936 | ||
2937 | if (sz != sizeof(sd_id128_t)) { | |
ab4dd984 | 2938 | log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID."); |
27dfc982 YW |
2939 | goto configure; |
2940 | } | |
2941 | ||
2942 | memcpy(&manager->product_uuid, a, sz); | |
2943 | while ((duid = set_steal_first(manager->duids_requesting_uuid))) | |
2944 | (void) duid_set_uuid(duid, manager->product_uuid); | |
2945 | ||
2946 | manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid); | |
2947 | ||
2948 | configure: | |
2949 | while ((link = set_steal_first(manager->links_requesting_uuid))) { | |
2950 | r = link_configure(link); | |
2951 | if (r < 0) | |
2952 | log_link_error_errno(link, r, "Failed to configure link: %m"); | |
2953 | } | |
2954 | ||
2955 | manager->links_requesting_uuid = set_free(manager->links_requesting_uuid); | |
2956 | ||
2957 | /* To avoid calling GetProductUUID() bus method so frequently, set the flag below | |
2958 | * even if the method fails. */ | |
2959 | manager->has_product_uuid = true; | |
2960 | ||
2961 | return 1; | |
2962 | } | |
2963 | ||
2964 | static bool link_requires_uuid(Link *link) { | |
2965 | const DUID *duid; | |
2966 | ||
2967 | assert(link); | |
2968 | assert(link->manager); | |
2969 | assert(link->network); | |
2970 | ||
2971 | duid = link_get_duid(link); | |
2972 | if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0) | |
2973 | return false; | |
2974 | ||
2975 | if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY)) | |
2976 | return true; | |
2977 | ||
2978 | if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link)) | |
2979 | return true; | |
2980 | ||
2981 | return false; | |
2982 | } | |
2983 | ||
2984 | static int link_configure_duid(Link *link) { | |
2985 | Manager *m; | |
2986 | DUID *duid; | |
2987 | int r; | |
2988 | ||
2989 | assert(link); | |
2990 | assert(link->manager); | |
2991 | assert(link->network); | |
2992 | ||
2993 | m = link->manager; | |
2994 | duid = link_get_duid(link); | |
2995 | ||
2996 | if (!link_requires_uuid(link)) | |
2997 | return 1; | |
2998 | ||
2999 | if (m->has_product_uuid) { | |
3000 | (void) duid_set_uuid(duid, m->product_uuid); | |
3001 | return 1; | |
3002 | } | |
3003 | ||
3004 | if (!m->links_requesting_uuid) { | |
3005 | r = manager_request_product_uuid(m, link); | |
3006 | if (r < 0) { | |
3007 | if (r == -ENOMEM) | |
3008 | return r; | |
3009 | ||
ab4dd984 ZJS |
3010 | log_link_warning_errno(link, r, |
3011 | "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); | |
27dfc982 YW |
3012 | return 1; |
3013 | } | |
3014 | } else { | |
3015 | r = set_put(m->links_requesting_uuid, link); | |
3016 | if (r < 0) | |
3017 | return log_oom(); | |
3018 | ||
3019 | r = set_put(m->duids_requesting_uuid, duid); | |
3020 | if (r < 0) | |
3021 | return log_oom(); | |
3022 | } | |
3023 | ||
3024 | return 0; | |
3025 | } | |
3026 | ||
e6bf7774 | 3027 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 3028 | Network *network; |
505f8da7 TG |
3029 | int r; |
3030 | ||
3031 | assert(link); | |
3032 | assert(link->ifname); | |
3033 | assert(link->manager); | |
3034 | ||
8434fd5c | 3035 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 3036 | return 1; |
505f8da7 | 3037 | |
6a7a4e4d | 3038 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 3039 | |
0d4ad91d AR |
3040 | r = link_new_bound_by_list(link); |
3041 | if (r < 0) | |
3042 | return r; | |
3043 | ||
3044 | r = link_handle_bound_by_list(link); | |
3045 | if (r < 0) | |
3046 | return r; | |
3047 | ||
c4a03a56 | 3048 | if (!link->network) { |
51517f9e | 3049 | r = network_get(link->manager, link->sd_device, link->ifname, |
c4a03a56 TG |
3050 | &link->mac, &network); |
3051 | if (r == -ENOENT) { | |
3052 | link_enter_unmanaged(link); | |
3053 | return 1; | |
a09dc546 DM |
3054 | } else if (r == 0 && network->unmanaged) { |
3055 | link_enter_unmanaged(link); | |
3056 | return 0; | |
c4a03a56 TG |
3057 | } else if (r < 0) |
3058 | return r; | |
505f8da7 | 3059 | |
c4a03a56 TG |
3060 | if (link->flags & IFF_LOOPBACK) { |
3061 | if (network->link_local != ADDRESS_FAMILY_NO) | |
3062 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 3063 | |
c4a03a56 TG |
3064 | if (network->dhcp != ADDRESS_FAMILY_NO) |
3065 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 3066 | |
c4a03a56 TG |
3067 | if (network->dhcp_server) |
3068 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
3069 | } | |
bd2efe92 | 3070 | |
7d342c03 | 3071 | r = network_apply(network, link); |
c4a03a56 TG |
3072 | if (r < 0) |
3073 | return r; | |
3074 | } | |
505f8da7 | 3075 | |
0d4ad91d AR |
3076 | r = link_new_bound_to_list(link); |
3077 | if (r < 0) | |
3078 | return r; | |
3079 | ||
27dfc982 YW |
3080 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, |
3081 | * link_configure() is called later asynchronously. */ | |
3082 | r = link_configure_duid(link); | |
3083 | if (r <= 0) | |
3084 | return r; | |
3085 | ||
a748b692 TG |
3086 | r = link_configure(link); |
3087 | if (r < 0) | |
3088 | return r; | |
3089 | ||
5da8149f | 3090 | return 1; |
505f8da7 TG |
3091 | } |
3092 | ||
e6bf7774 YW |
3093 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
3094 | (void) link_initialized_and_synced(userdata); | |
3095 | return 1; | |
3096 | } | |
3097 | ||
51517f9e | 3098 | int link_initialized(Link *link, sd_device *device) { |
4afd3348 | 3099 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
3100 | int r; |
3101 | ||
3102 | assert(link); | |
3103 | assert(link->manager); | |
3104 | assert(link->manager->rtnl); | |
3105 | assert(device); | |
3106 | ||
8434fd5c | 3107 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
3108 | return 0; |
3109 | ||
51517f9e | 3110 | if (link->sd_device) |
679b3605 TG |
3111 | return 0; |
3112 | ||
79008bdd | 3113 | log_link_debug(link, "udev initialized link"); |
4f561e8e | 3114 | |
51517f9e | 3115 | link->sd_device = sd_device_ref(device); |
4f561e8e | 3116 | |
3c9b8860 TG |
3117 | /* udev has initialized the link, but we don't know if we have yet |
3118 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
3119 | * when it returns we know that the pending NEWLINKs have already been | |
3120 | * processed and that we are up-to-date */ | |
4f561e8e | 3121 | |
3c9b8860 TG |
3122 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
3123 | link->ifindex); | |
4f561e8e TG |
3124 | if (r < 0) |
3125 | return r; | |
3126 | ||
e6bf7774 | 3127 | r = sd_netlink_call_async(link->manager->rtnl, req, link_initialized_handler, |
1046bf9b | 3128 | link_netlink_destroy_callback, link, 0, NULL); |
4f561e8e TG |
3129 | if (r < 0) |
3130 | return r; | |
3131 | ||
5da8149f TG |
3132 | link_ref(link); |
3133 | ||
4f561e8e TG |
3134 | return 0; |
3135 | } | |
3136 | ||
c4a03a56 | 3137 | static int link_load(Link *link) { |
0bc70f1d TG |
3138 | _cleanup_free_ char *network_file = NULL, |
3139 | *addresses = NULL, | |
f703cc2c | 3140 | *routes = NULL, |
0bc70f1d TG |
3141 | *dhcp4_address = NULL, |
3142 | *ipv4ll_address = NULL; | |
3143 | union in_addr_union address; | |
f703cc2c | 3144 | union in_addr_union route_dst; |
c598ac76 | 3145 | const char *p; |
c4a03a56 TG |
3146 | int r; |
3147 | ||
3148 | assert(link); | |
3149 | ||
1a5a177e | 3150 | r = parse_env_file(NULL, link->state_file, NEWLINE, |
c4a03a56 TG |
3151 | "NETWORK_FILE", &network_file, |
3152 | "ADDRESSES", &addresses, | |
f703cc2c | 3153 | "ROUTES", &routes, |
0bc70f1d TG |
3154 | "DHCP4_ADDRESS", &dhcp4_address, |
3155 | "IPV4LL_ADDRESS", &ipv4ll_address, | |
c4a03a56 TG |
3156 | NULL); |
3157 | if (r < 0 && r != -ENOENT) | |
3158 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
3159 | ||
3160 | if (network_file) { | |
3161 | Network *network; | |
3162 | char *suffix; | |
3163 | ||
3164 | /* drop suffix */ | |
3165 | suffix = strrchr(network_file, '.'); | |
3166 | if (!suffix) { | |
3167 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
3168 | goto network_file_fail; | |
3169 | } | |
3170 | *suffix = '\0'; | |
3171 | ||
3172 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
3173 | if (r < 0) { | |
3174 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
3175 | goto network_file_fail; | |
3176 | } | |
3177 | ||
7d342c03 | 3178 | r = network_apply(network, link); |
c4a03a56 TG |
3179 | if (r < 0) |
3180 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
3181 | } | |
3182 | ||
3183 | network_file_fail: | |
3184 | ||
3185 | if (addresses) { | |
c598ac76 | 3186 | p = addresses; |
c4a03a56 | 3187 | |
c598ac76 TG |
3188 | for (;;) { |
3189 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
3190 | char *prefixlen_str; |
3191 | int family; | |
3192 | unsigned char prefixlen; | |
c4a03a56 | 3193 | |
c598ac76 TG |
3194 | r = extract_first_word(&p, &address_str, NULL, 0); |
3195 | if (r < 0) { | |
3196 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
3197 | continue; | |
928bc597 TA |
3198 | } |
3199 | if (r == 0) | |
c598ac76 TG |
3200 | break; |
3201 | ||
3202 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 3203 | if (!prefixlen_str) { |
c598ac76 | 3204 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
3205 | continue; |
3206 | } | |
3207 | ||
313cefa1 | 3208 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
3209 | |
3210 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
3211 | if (r != 1) { | |
3212 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
3213 | continue; | |
3214 | } | |
3215 | ||
c598ac76 | 3216 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 3217 | if (r < 0) { |
c598ac76 | 3218 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
3219 | continue; |
3220 | } | |
3221 | ||
3222 | r = address_add(link, family, &address, prefixlen, NULL); | |
3223 | if (r < 0) | |
3224 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
3225 | } | |
3226 | } | |
3227 | ||
f703cc2c | 3228 | if (routes) { |
74544b4e TA |
3229 | p = routes; |
3230 | ||
c598ac76 | 3231 | for (;;) { |
f833694d | 3232 | Route *route; |
c598ac76 | 3233 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 3234 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 3235 | usec_t lifetime; |
f703cc2c TG |
3236 | char *prefixlen_str; |
3237 | int family; | |
3238 | unsigned char prefixlen, tos, table; | |
3239 | uint32_t priority; | |
3240 | ||
c598ac76 TG |
3241 | r = extract_first_word(&p, &route_str, NULL, 0); |
3242 | if (r < 0) { | |
3243 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
3244 | continue; | |
928bc597 TA |
3245 | } |
3246 | if (r == 0) | |
c598ac76 TG |
3247 | break; |
3248 | ||
3249 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 3250 | if (!prefixlen_str) { |
c598ac76 | 3251 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
3252 | continue; |
3253 | } | |
3254 | ||
313cefa1 | 3255 | *prefixlen_str++ = '\0'; |
f703cc2c | 3256 | |
f833694d TG |
3257 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
3258 | if (r != 5) { | |
3259 | log_link_debug(link, | |
3260 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
3261 | prefixlen_str); | |
f703cc2c TG |
3262 | continue; |
3263 | } | |
3264 | ||
c598ac76 | 3265 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 3266 | if (r < 0) { |
c598ac76 | 3267 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
3268 | continue; |
3269 | } | |
3270 | ||
f833694d | 3271 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
3272 | if (r < 0) |
3273 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d | 3274 | |
f02ba163 | 3275 | if (lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
f833694d TG |
3276 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, |
3277 | 0, route_expire_handler, route); | |
3278 | if (r < 0) | |
3279 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
3280 | } | |
3281 | ||
3282 | route->lifetime = lifetime; | |
3283 | sd_event_source_unref(route->expire); | |
1cc6c93a | 3284 | route->expire = TAKE_PTR(expire); |
f703cc2c TG |
3285 | } |
3286 | } | |
3287 | ||
0bc70f1d TG |
3288 | if (dhcp4_address) { |
3289 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
3290 | if (r < 0) { | |
b68d26b8 | 3291 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3292 | goto dhcp4_address_fail; |
3293 | } | |
3294 | ||
b1c626f6 | 3295 | r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0); |
0bc70f1d | 3296 | if (r < 0) |
b68d26b8 | 3297 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d TG |
3298 | |
3299 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
3300 | if (r < 0) | |
b68d26b8 | 3301 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3302 | } |
3303 | ||
3304 | dhcp4_address_fail: | |
3305 | ||
3306 | if (ipv4ll_address) { | |
3307 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
3308 | if (r < 0) { | |
b68d26b8 | 3309 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3310 | goto ipv4ll_address_fail; |
3311 | } | |
3312 | ||
3313 | r = sd_ipv4ll_new(&link->ipv4ll); | |
3314 | if (r < 0) | |
b68d26b8 | 3315 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d TG |
3316 | |
3317 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
3318 | if (r < 0) | |
b68d26b8 | 3319 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3320 | } |
3321 | ||
3322 | ipv4ll_address_fail: | |
3323 | ||
c4a03a56 TG |
3324 | return 0; |
3325 | } | |
3326 | ||
1c4baffc | 3327 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
51517f9e | 3328 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
505f8da7 | 3329 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
51517f9e YW |
3330 | int initialized, r; |
3331 | Link *link; | |
505f8da7 TG |
3332 | |
3333 | assert(m); | |
fbbeb65a | 3334 | assert(m->rtnl); |
505f8da7 TG |
3335 | assert(message); |
3336 | assert(ret); | |
3337 | ||
3338 | r = link_new(m, message, ret); | |
3339 | if (r < 0) | |
3340 | return r; | |
3341 | ||
3342 | link = *ret; | |
3343 | ||
6a7a4e4d | 3344 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 3345 | |
c4a03a56 TG |
3346 | r = link_load(link); |
3347 | if (r < 0) | |
3348 | return r; | |
3349 | ||
75f86906 | 3350 | if (detect_container() <= 0) { |
505f8da7 | 3351 | /* not in a container, udev will be around */ |
ae06ab10 | 3352 | sprintf(ifindex_str, "n%d", link->ifindex); |
51517f9e YW |
3353 | r = sd_device_new_from_device_id(&device, ifindex_str); |
3354 | if (r < 0) { | |
3355 | log_link_warning_errno(link, r, "Could not find device: %m"); | |
5c416fc4 TG |
3356 | goto failed; |
3357 | } | |
505f8da7 | 3358 | |
51517f9e YW |
3359 | r = sd_device_get_is_initialized(device, &initialized); |
3360 | if (r < 0) { | |
3361 | log_link_warning_errno(link, r, "Could not determine whether the device is initialized or not: %m"); | |
3362 | goto failed; | |
3363 | } | |
3364 | if (!initialized) { | |
505f8da7 | 3365 | /* not yet ready */ |
79008bdd | 3366 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 3367 | return 0; |
3c4cb064 | 3368 | } |
505f8da7 | 3369 | |
4f561e8e TG |
3370 | r = link_initialized(link, device); |
3371 | if (r < 0) | |
5c416fc4 | 3372 | goto failed; |
4f561e8e | 3373 | } else { |
e6bf7774 | 3374 | r = link_initialized_and_synced(link); |
4f561e8e | 3375 | if (r < 0) |
5c416fc4 | 3376 | goto failed; |
4f561e8e | 3377 | } |
505f8da7 | 3378 | |
a748b692 | 3379 | return 0; |
5c416fc4 TG |
3380 | failed: |
3381 | link_enter_failed(link); | |
3382 | return r; | |
a748b692 TG |
3383 | } |
3384 | ||
c601ebf7 | 3385 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
3386 | int r; |
3387 | ||
3388 | assert(link); | |
3389 | ||
3390 | log_link_info(link, "Gained IPv6LL"); | |
3391 | ||
c601ebf7 | 3392 | link->ipv6ll_address = *address; |
e7ab854c TG |
3393 | link_check_ready(link); |
3394 | ||
201f0c91 | 3395 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
3396 | r = link_acquire_ipv6_conf(link); |
3397 | if (r < 0) { | |
3398 | link_enter_failed(link); | |
3399 | return r; | |
3400 | } | |
3401 | } | |
3402 | ||
3403 | return 0; | |
3404 | } | |
3405 | ||
9c0a72f9 TG |
3406 | static int link_carrier_gained(Link *link) { |
3407 | int r; | |
3408 | ||
3409 | assert(link); | |
3410 | ||
201f0c91 | 3411 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
3412 | r = link_acquire_conf(link); |
3413 | if (r < 0) { | |
3414 | link_enter_failed(link); | |
3415 | return r; | |
3416 | } | |
6fc25497 SS |
3417 | |
3418 | r = link_enter_set_addresses(link); | |
3419 | if (r < 0) | |
3420 | return r; | |
9c0a72f9 TG |
3421 | } |
3422 | ||
0d4ad91d AR |
3423 | r = link_handle_bound_by_list(link); |
3424 | if (r < 0) | |
3425 | return r; | |
3426 | ||
9c0a72f9 TG |
3427 | return 0; |
3428 | } | |
3429 | ||
3430 | static int link_carrier_lost(Link *link) { | |
3431 | int r; | |
3432 | ||
3433 | assert(link); | |
3434 | ||
fbdb6605 | 3435 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
55dc8c4a YW |
3436 | * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the |
3437 | * configuration and stop the clients as well. */ | |
97e7fb39 SS |
3438 | if (link->setting_mtu) |
3439 | return 0; | |
3440 | ||
9c0a72f9 TG |
3441 | r = link_stop_clients(link); |
3442 | if (r < 0) { | |
3443 | link_enter_failed(link); | |
3444 | return r; | |
3445 | } | |
3446 | ||
28464ae0 SS |
3447 | if (link_dhcp4_server_enabled(link)) |
3448 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
45a9eac9 | 3449 | |
3104883d SS |
3450 | r = link_drop_config(link); |
3451 | if (r < 0) | |
3452 | return r; | |
3453 | ||
f258e948 MP |
3454 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING)) { |
3455 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); | |
c436d553 MM |
3456 | r = link_drop_foreign_config(link); |
3457 | if (r < 0) | |
3458 | return r; | |
3459 | } | |
3104883d | 3460 | |
0d4ad91d AR |
3461 | r = link_handle_bound_by_list(link); |
3462 | if (r < 0) | |
3463 | return r; | |
3464 | ||
9c0a72f9 TG |
3465 | return 0; |
3466 | } | |
3467 | ||
3468 | int link_carrier_reset(Link *link) { | |
3469 | int r; | |
3470 | ||
3471 | assert(link); | |
3472 | ||
3473 | if (link_has_carrier(link)) { | |
3474 | r = link_carrier_lost(link); | |
3475 | if (r < 0) | |
3476 | return r; | |
3477 | ||
3478 | r = link_carrier_gained(link); | |
3479 | if (r < 0) | |
3480 | return r; | |
3481 | ||
6a7a4e4d | 3482 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
3483 | } |
3484 | ||
3485 | return 0; | |
3486 | } | |
3487 | ||
1c4baffc | 3488 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 3489 | struct ether_addr mac; |
ca4e095a | 3490 | const char *ifname; |
afe7fd56 | 3491 | uint32_t mtu; |
a61bb41c | 3492 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
3493 | int r; |
3494 | ||
dd3efc09 | 3495 | assert(link); |
b8941f74 | 3496 | assert(link->ifname); |
22936833 TG |
3497 | assert(m); |
3498 | ||
7619683b | 3499 | if (link->state == LINK_STATE_LINGER) { |
6a7a4e4d | 3500 | log_link_info(link, "Link readded"); |
e331e246 | 3501 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
3502 | |
3503 | r = link_new_carrier_maps(link); | |
3504 | if (r < 0) | |
3505 | return r; | |
7619683b TG |
3506 | } |
3507 | ||
1c4baffc | 3508 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3509 | if (r >= 0 && !streq(ifname, link->ifname)) { |
aa2b10da | 3510 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
b8941f74 | 3511 | |
587c4586 YW |
3512 | if (link->state == LINK_STATE_PENDING) { |
3513 | r = free_and_strdup(&link->ifname, ifname); | |
3514 | if (r < 0) | |
3515 | return r; | |
3516 | } else { | |
3517 | Manager *manager = link->manager; | |
0d4ad91d | 3518 | |
587c4586 YW |
3519 | link_drop(link); |
3520 | r = link_add(manager, m, &link); | |
3521 | if (r < 0) | |
3522 | return r; | |
3523 | } | |
b8941f74 TG |
3524 | } |
3525 | ||
1c4baffc | 3526 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3527 | if (r >= 0 && mtu > 0) { |
3528 | link->mtu = mtu; | |
4e964aa0 | 3529 | if (link->original_mtu == 0) { |
afe7fd56 | 3530 | link->original_mtu = mtu; |
6a7a4e4d | 3531 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3532 | } |
3533 | ||
3534 | if (link->dhcp_client) { | |
3c9b8860 TG |
3535 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3536 | link->mtu); | |
fc95c359 YW |
3537 | if (r < 0) |
3538 | return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); | |
afe7fd56 | 3539 | } |
7465dd22 PF |
3540 | |
3541 | if (link->radv) { | |
3542 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
3543 | if (r < 0) | |
3544 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
3545 | } | |
9842de0d | 3546 | } |
69629de9 | 3547 | |
e9189a1f TG |
3548 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3549 | set, simply ignore them. */ | |
1c4baffc | 3550 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3551 | if (r >= 0) { |
3c9b8860 TG |
3552 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3553 | ETH_ALEN)) { | |
c49b33ac | 3554 | |
3c9b8860 TG |
3555 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3556 | ETH_ALEN); | |
c49b33ac | 3557 | |
79008bdd | 3558 | log_link_debug(link, "MAC address: " |
20861203 TG |
3559 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3560 | mac.ether_addr_octet[0], | |
3561 | mac.ether_addr_octet[1], | |
3562 | mac.ether_addr_octet[2], | |
3563 | mac.ether_addr_octet[3], | |
3564 | mac.ether_addr_octet[4], | |
3565 | mac.ether_addr_octet[5]); | |
c49b33ac | 3566 | |
20861203 TG |
3567 | if (link->ipv4ll) { |
3568 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3569 | if (r < 0) |
3570 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3571 | } |
c49b33ac | 3572 | |
20861203 | 3573 | if (link->dhcp_client) { |
3c9b8860 | 3574 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3575 | (const uint8_t *) &link->mac, |
3576 | sizeof (link->mac), | |
3577 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3578 | if (r < 0) |
3579 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3580 | |
fff1f40c YW |
3581 | r = dhcp4_set_client_identifier(link); |
3582 | if (r < 0) | |
3583 | return r; | |
c49b33ac | 3584 | } |
4138fb2c PF |
3585 | |
3586 | if (link->dhcp6_client) { | |
f24648a6 | 3587 | const DUID* duid = link_get_duid(link); |
8341a5c3 | 3588 | |
4138fb2c | 3589 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3590 | (const uint8_t *) &link->mac, |
3591 | sizeof (link->mac), | |
3592 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3593 | if (r < 0) |
3594 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
3595 | |
3596 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3597 | link->network->iaid); | |
3598 | if (r < 0) | |
3599 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3600 | ||
8341a5c3 ZJS |
3601 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3602 | duid->type, | |
3603 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3604 | duid->raw_data_len); | |
413708d1 VK |
3605 | if (r < 0) |
3606 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3607 | } |
7465dd22 PF |
3608 | |
3609 | if (link->radv) { | |
3610 | r = sd_radv_set_mac(link->radv, &link->mac); | |
3611 | if (r < 0) | |
3612 | return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m"); | |
3613 | } | |
420d2058 SS |
3614 | |
3615 | if (link->ndisc) { | |
3616 | r = sd_ndisc_set_mac(link->ndisc, &link->mac); | |
3617 | if (r < 0) | |
3618 | return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m"); | |
3619 | } | |
c49b33ac | 3620 | } |
4f882b2a TG |
3621 | } |
3622 | ||
a61bb41c TG |
3623 | had_carrier = link_has_carrier(link); |
3624 | ||
3625 | r = link_update_flags(link, m); | |
3626 | if (r < 0) | |
3627 | return r; | |
3628 | ||
273eec24 LP |
3629 | r = link_update_lldp(link); |
3630 | if (r < 0) | |
3631 | return r; | |
3632 | ||
a61bb41c TG |
3633 | carrier_gained = !had_carrier && link_has_carrier(link); |
3634 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3635 | ||
3636 | if (carrier_gained) { | |
6a7a4e4d | 3637 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3638 | |
9c0a72f9 TG |
3639 | r = link_carrier_gained(link); |
3640 | if (r < 0) | |
3641 | return r; | |
a61bb41c | 3642 | } else if (carrier_lost) { |
6a7a4e4d | 3643 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3644 | |
9c0a72f9 TG |
3645 | r = link_carrier_lost(link); |
3646 | if (r < 0) | |
a61bb41c | 3647 | return r; |
a61bb41c TG |
3648 | } |
3649 | ||
3650 | return 0; | |
dd3efc09 | 3651 | } |
fe8db0c5 | 3652 | |
b295beea LP |
3653 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3654 | bool space = false; | |
3655 | Iterator i; | |
3656 | Link *link; | |
3657 | ||
3658 | assert(f); | |
3659 | assert(prefix); | |
3660 | ||
3661 | if (hashmap_isempty(h)) | |
3662 | return; | |
3663 | ||
0d536673 | 3664 | fputs(prefix, f); |
b295beea LP |
3665 | HASHMAP_FOREACH(link, h, i) { |
3666 | if (space) | |
0d536673 | 3667 | fputc(' ', f); |
b295beea LP |
3668 | |
3669 | fprintf(f, "%i", link->ifindex); | |
3670 | space = true; | |
3671 | } | |
3672 | ||
0d536673 | 3673 | fputc('\n', f); |
b295beea LP |
3674 | } |
3675 | ||
fe8db0c5 | 3676 | int link_save(Link *link) { |
68a8723c | 3677 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3678 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3679 | const char *admin_state, *oper_state; |
e7780c8d | 3680 | Address *a; |
c1eb9872 | 3681 | Route *route; |
e7780c8d | 3682 | Iterator i; |
fe8db0c5 TG |
3683 | int r; |
3684 | ||
3685 | assert(link); | |
3686 | assert(link->state_file); | |
68a8723c | 3687 | assert(link->lease_file); |
bbf7c048 TG |
3688 | assert(link->manager); |
3689 | ||
370e9930 TG |
3690 | if (link->state == LINK_STATE_LINGER) { |
3691 | unlink(link->state_file); | |
3692 | return 0; | |
3693 | } | |
3694 | ||
34437b4f LP |
3695 | link_lldp_save(link); |
3696 | ||
deb2e523 TG |
3697 | admin_state = link_state_to_string(link->state); |
3698 | assert(admin_state); | |
3699 | ||
e375dcde TG |
3700 | oper_state = link_operstate_to_string(link->operstate); |
3701 | assert(oper_state); | |
deb2e523 | 3702 | |
fe8db0c5 TG |
3703 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3704 | if (r < 0) | |
6a7a4e4d | 3705 | goto fail; |
fe8db0c5 | 3706 | |
0d536673 | 3707 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 3708 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
3709 | |
3710 | fprintf(f, | |
3711 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3712 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3713 | "OPER_STATE=%s\n", |
3714 | admin_state, oper_state); | |
fe8db0c5 | 3715 | |
bcb7a07e | 3716 | if (link->network) { |
ea352b40 | 3717 | bool space; |
07bdc70d | 3718 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3719 | const char *dhcp_domainname = NULL; |
3720 | char **dhcp6_domains = NULL; | |
b85bc551 | 3721 | char **dhcp_domains = NULL; |
5512a963 | 3722 | unsigned j; |
07bdc70d | 3723 | |
c1a38904 MTL |
3724 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
3725 | yes_no(link->network->required_for_online)); | |
3726 | ||
07bdc70d | 3727 | if (link->dhcp6_client) { |
4058d339 TG |
3728 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3729 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3730 | log_link_debug(link, "No DHCPv6 lease"); |
3731 | } | |
b0e39c82 | 3732 | |
adc5b2e2 TG |
3733 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3734 | ||
0d536673 | 3735 | fputs("DNS=", f); |
ea352b40 | 3736 | space = false; |
5512a963 LP |
3737 | |
3738 | for (j = 0; j < link->network->n_dns; j++) { | |
3739 | _cleanup_free_ char *b = NULL; | |
3740 | ||
3741 | r = in_addr_to_string(link->network->dns[j].family, | |
3742 | &link->network->dns[j].address, &b); | |
3743 | if (r < 0) { | |
3744 | log_debug_errno(r, "Failed to format address, ignoring: %m"); | |
3745 | continue; | |
3746 | } | |
3747 | ||
3748 | if (space) | |
0d536673 LP |
3749 | fputc(' ', f); |
3750 | fputs(b, f); | |
5512a963 LP |
3751 | space = true; |
3752 | } | |
d5314fff | 3753 | |
27cb34f5 | 3754 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3755 | link->dhcp_lease) { |
3756 | const struct in_addr *addresses; | |
3757 | ||
3758 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3759 | if (r > 0) { | |
ea352b40 | 3760 | if (space) |
0d536673 | 3761 | fputc(' ', f); |
b0e39c82 | 3762 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3763 | space = true; |
3764 | } | |
3765 | } | |
3766 | ||
27cb34f5 | 3767 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3768 | struct in6_addr *in6_addrs; |
3769 | ||
3770 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3771 | if (r > 0) { | |
3772 | if (space) | |
0d536673 | 3773 | fputc(' ', f); |
07bdc70d | 3774 | serialize_in6_addrs(f, in6_addrs, r); |
1e7a0e21 LP |
3775 | space = true; |
3776 | } | |
3777 | } | |
3778 | ||
3779 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3780 | ndisc_vacuum(link); | |
3781 | ||
b296797f | 3782 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
3783 | NDiscRDNSS *dd; |
3784 | ||
3785 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3786 | if (space) | |
0d536673 | 3787 | fputc(' ', f); |
1e7a0e21 LP |
3788 | |
3789 | serialize_in6_addrs(f, &dd->address, 1); | |
3790 | space = true; | |
b0e39c82 TG |
3791 | } |
3792 | } | |
3793 | ||
0d536673 | 3794 | fputc('\n', f); |
b0e39c82 | 3795 | |
0d536673 | 3796 | fputs("NTP=", f); |
ea352b40 | 3797 | space = false; |
3df9bec5 | 3798 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3799 | |
27cb34f5 | 3800 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3801 | link->dhcp_lease) { |
3802 | const struct in_addr *addresses; | |
3803 | ||
3804 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3805 | if (r > 0) { | |
ea352b40 | 3806 | if (space) |
0d536673 | 3807 | fputc(' ', f); |
b0e39c82 | 3808 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3809 | space = true; |
3810 | } | |
3811 | } | |
3812 | ||
27cb34f5 | 3813 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3814 | struct in6_addr *in6_addrs; |
3815 | char **hosts; | |
07bdc70d PF |
3816 | |
3817 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3818 | &in6_addrs); | |
3819 | if (r > 0) { | |
3820 | if (space) | |
0d536673 | 3821 | fputc(' ', f); |
07bdc70d PF |
3822 | serialize_in6_addrs(f, in6_addrs, r); |
3823 | space = true; | |
3824 | } | |
3825 | ||
3826 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3827 | if (r > 0) |
3828 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3829 | } |
3830 | ||
0d536673 | 3831 | fputc('\n', f); |
bd8f6538 | 3832 | |
b2a81c0b | 3833 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 3834 | if (link->dhcp_lease) { |
b2a81c0b | 3835 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
3836 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
3837 | } | |
b2a81c0b LP |
3838 | if (dhcp6_lease) |
3839 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3840 | } |
3841 | ||
0d536673 | 3842 | fputs("DOMAINS=", f); |
b85bc551 | 3843 | space = false; |
b2a81c0b | 3844 | fputstrv(f, link->network->search_domains, NULL, &space); |
07bdc70d | 3845 | |
1e7a0e21 LP |
3846 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3847 | NDiscDNSSL *dd; | |
9b4d1882 | 3848 | |
1e7a0e21 LP |
3849 | if (dhcp_domainname) |
3850 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3851 | if (dhcp_domains) |
3852 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3853 | if (dhcp6_domains) |
3854 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3855 | ||
3856 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3857 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3858 | } | |
b2a81c0b | 3859 | |
0d536673 | 3860 | fputc('\n', f); |
6192b846 | 3861 | |
0d536673 | 3862 | fputs("ROUTE_DOMAINS=", f); |
b85bc551 DW |
3863 | space = false; |
3864 | fputstrv(f, link->network->route_domains, NULL, &space); | |
b2a81c0b | 3865 | |
1e7a0e21 LP |
3866 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3867 | NDiscDNSSL *dd; | |
b2a81c0b | 3868 | |
1e7a0e21 LP |
3869 | if (dhcp_domainname) |
3870 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3871 | if (dhcp_domains) |
3872 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3873 | if (dhcp6_domains) |
3874 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3875 | ||
3876 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3877 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3878 | } | |
b2a81c0b | 3879 | |
0d536673 | 3880 | fputc('\n', f); |
67272d15 | 3881 | |
3c9b8860 | 3882 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3883 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3884 | fprintf(f, "MDNS=%s\n", |
3885 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3886 | |
c9299be2 IT |
3887 | if (link->network->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
3888 | fprintf(f, "DNS_OVER_TLS=%s\n", | |
3889 | dns_over_tls_mode_to_string(link->network->dns_over_tls_mode)); | |
d050561a | 3890 | |
ad6c0475 LP |
3891 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3892 | fprintf(f, "DNSSEC=%s\n", | |
3893 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3894 | ||
8a516214 LP |
3895 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3896 | const char *n; | |
3897 | ||
0d536673 | 3898 | fputs("DNSSEC_NTA=", f); |
8a516214 | 3899 | space = false; |
d390f8ef LP |
3900 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3901 | fputs_with_space(f, n, NULL, &space); | |
0d536673 | 3902 | fputc('\n', f); |
8a516214 LP |
3903 | } |
3904 | ||
0d536673 | 3905 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3906 | space = false; |
3907 | SET_FOREACH(a, link->addresses, i) { | |
3908 | _cleanup_free_ char *address_str = NULL; | |
3909 | ||
3910 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3911 | if (r < 0) | |
3912 | goto fail; | |
3913 | ||
e7780c8d TG |
3914 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3915 | space = true; | |
3916 | } | |
0d536673 | 3917 | fputc('\n', f); |
c1eb9872 | 3918 | |
0d536673 | 3919 | fputs("ROUTES=", f); |
c1eb9872 TG |
3920 | space = false; |
3921 | SET_FOREACH(route, link->routes, i) { | |
3922 | _cleanup_free_ char *route_str = NULL; | |
3923 | ||
3924 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3925 | if (r < 0) | |
3926 | goto fail; | |
3927 | ||
ecc3f340 ZJS |
3928 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, |
3929 | space ? " " : "", route_str, | |
f833694d | 3930 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); |
c1eb9872 TG |
3931 | space = true; |
3932 | } | |
3933 | ||
0d536673 | 3934 | fputc('\n', f); |
bcb7a07e | 3935 | } |
7374f9d8 | 3936 | |
b295beea LP |
3937 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3938 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3939 | |
8eb9058d | 3940 | if (link->dhcp_lease) { |
0bc70f1d | 3941 | struct in_addr address; |
8eb9058d LP |
3942 | const char *tz = NULL; |
3943 | ||
0bc70f1d TG |
3944 | assert(link->network); |
3945 | ||
8eb9058d LP |
3946 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3947 | if (r >= 0) | |
3948 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3949 | |
0bc70f1d TG |
3950 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3951 | if (r >= 0) { | |
0d536673 | 3952 | fputs("DHCP4_ADDRESS=", f); |
0bc70f1d | 3953 | serialize_in_addrs(f, &address, 1); |
0d536673 | 3954 | fputc('\n', f); |
0bc70f1d | 3955 | } |
d9876a52 | 3956 | |
bd91b83e | 3957 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3958 | if (r < 0) |
c2d6bd61 | 3959 | goto fail; |
fe8db0c5 | 3960 | |
7374f9d8 | 3961 | fprintf(f, |
b0e39c82 TG |
3962 | "DHCP_LEASE=%s\n", |
3963 | link->lease_file); | |
deb2e523 | 3964 | } else |
68a8723c | 3965 | unlink(link->lease_file); |
fe8db0c5 | 3966 | |
0bc70f1d TG |
3967 | if (link->ipv4ll) { |
3968 | struct in_addr address; | |
3969 | ||
3970 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3971 | if (r >= 0) { | |
0d536673 | 3972 | fputs("IPV4LL_ADDRESS=", f); |
0bc70f1d | 3973 | serialize_in_addrs(f, &address, 1); |
0d536673 | 3974 | fputc('\n', f); |
0bc70f1d TG |
3975 | } |
3976 | } | |
3977 | ||
c2d6bd61 LP |
3978 | r = fflush_and_check(f); |
3979 | if (r < 0) | |
3980 | goto fail; | |
fe8db0c5 | 3981 | |
c2d6bd61 | 3982 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3983 | r = -errno; |
c2d6bd61 | 3984 | goto fail; |
fe8db0c5 TG |
3985 | } |
3986 | ||
c2d6bd61 | 3987 | return 0; |
dacd6cee | 3988 | |
c2d6bd61 | 3989 | fail: |
6a7a4e4d | 3990 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3991 | if (temp_path) |
3992 | (void) unlink(temp_path); | |
3993 | ||
dacd6cee | 3994 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3995 | } |
3996 | ||
84de38c5 TG |
3997 | /* The serialized state in /run is no longer up-to-date. */ |
3998 | void link_dirty(Link *link) { | |
3999 | int r; | |
4000 | ||
4001 | assert(link); | |
4002 | ||
0c241a37 SS |
4003 | /* mark manager dirty as link is dirty */ |
4004 | manager_dirty(link->manager); | |
4005 | ||
84de38c5 TG |
4006 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
4007 | if (r < 0) | |
4008 | /* allocation errors are ignored */ | |
4009 | return; | |
4010 | ||
4011 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
4012 | if (r <= 0) |
4013 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
4014 | return; |
4015 | ||
4016 | link_ref(link); | |
4017 | } | |
4018 | ||
4019 | /* The serialized state in /run is up-to-date */ | |
4020 | void link_clean(Link *link) { | |
4021 | assert(link); | |
4022 | assert(link->manager); | |
4023 | ||
1046bf9b | 4024 | link_unref(set_remove(link->manager->dirty_links, link)); |
84de38c5 TG |
4025 | } |
4026 | ||
fe8db0c5 | 4027 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 4028 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
4029 | [LINK_STATE_ENSLAVING] = "configuring", |
4030 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
4031 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
4032 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 4033 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 4034 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 4035 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
4036 | }; |
4037 | ||
4038 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
4039 | |
4040 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
4041 | [LINK_OPERSTATE_OFF] = "off", |
4042 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
4043 | [LINK_OPERSTATE_DORMANT] = "dormant", |
4044 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
4045 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
4046 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
4047 | }; | |
4048 | ||
4049 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |