]>
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 | 1273 | link_enter_failed(link); |
e1853b00 SS |
1274 | } |
1275 | ||
0ae286e6 | 1276 | return 1; |
e1853b00 SS |
1277 | } |
1278 | ||
55dc8c4a YW |
1279 | static int link_configure_after_setting_mtu(Link *link); |
1280 | ||
1c4baffc | 1281 | static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1282 | Link *link = userdata; |
4f882b2a TG |
1283 | int r; |
1284 | ||
1285 | assert(m); | |
1286 | assert(link); | |
1287 | assert(link->ifname); | |
1288 | ||
55dc8c4a YW |
1289 | link->setting_mtu = false; |
1290 | ||
5da8149f | 1291 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
1292 | return 1; |
1293 | ||
1c4baffc | 1294 | r = sd_netlink_message_get_errno(m); |
55dc8c4a | 1295 | if (r < 0) { |
a2fae7bb | 1296 | log_link_warning_errno(link, r, "Could not set MTU: %m"); |
55dc8c4a YW |
1297 | return 1; |
1298 | } | |
1299 | ||
1300 | log_link_debug(link, "Setting MTU done."); | |
1301 | ||
1302 | if (link->state == LINK_STATE_PENDING) | |
1303 | (void) link_configure_after_setting_mtu(link); | |
4f882b2a TG |
1304 | |
1305 | return 1; | |
1306 | } | |
1307 | ||
3c9b8860 | 1308 | int link_set_mtu(Link *link, uint32_t mtu) { |
4afd3348 | 1309 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f882b2a TG |
1310 | int r; |
1311 | ||
1312 | assert(link); | |
1313 | assert(link->manager); | |
1314 | assert(link->manager->rtnl); | |
1315 | ||
55dc8c4a | 1316 | if (link->mtu == mtu || link->setting_mtu) |
ee493106 YW |
1317 | return 0; |
1318 | ||
6a7a4e4d | 1319 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 1320 | |
6a7a4e4d LP |
1321 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1322 | if (r < 0) | |
1323 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a | 1324 | |
44b598a1 | 1325 | /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled) |
72c2500b | 1326 | * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */ |
44b598a1 SS |
1327 | (void) link_enable_ipv6(link); |
1328 | ||
1329 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes | |
72c2500b YW |
1330 | * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ |
1331 | if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) { | |
44b598a1 SS |
1332 | |
1333 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
1334 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m"); | |
1335 | ||
72c2500b | 1336 | mtu = IPV6_MIN_MTU; |
44b598a1 SS |
1337 | } |
1338 | ||
1c4baffc | 1339 | r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu); |
6a7a4e4d LP |
1340 | if (r < 0) |
1341 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 1342 | |
ee38400b YW |
1343 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler, |
1344 | link_netlink_destroy_callback, link, 0); | |
6a7a4e4d LP |
1345 | if (r < 0) |
1346 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 1347 | |
ae941762 | 1348 | link_ref(link); |
1046bf9b | 1349 | link->setting_mtu = true; |
b226d99b | 1350 | |
4f882b2a TG |
1351 | return 0; |
1352 | } | |
1353 | ||
99d2baa2 | 1354 | static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1355 | Link *link = userdata; |
99d2baa2 SS |
1356 | int r; |
1357 | ||
1358 | assert(m); | |
1359 | assert(link); | |
1360 | assert(link->ifname); | |
1361 | ||
1362 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1363 | return 1; | |
1364 | ||
1365 | r = sd_netlink_message_get_errno(m); | |
1366 | if (r < 0) | |
1367 | log_link_warning_errno(link, r, "Could not set link flags: %m"); | |
1368 | ||
1369 | return 1; | |
1370 | } | |
1371 | ||
1372 | static int link_set_flags(Link *link) { | |
1373 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1374 | unsigned ifi_change = 0; | |
1375 | unsigned ifi_flags = 0; | |
1376 | int r; | |
1377 | ||
1378 | assert(link); | |
1379 | assert(link->manager); | |
1380 | assert(link->manager->rtnl); | |
1381 | ||
1382 | if (link->flags & IFF_LOOPBACK) | |
1383 | return 0; | |
1384 | ||
1385 | if (!link->network) | |
1386 | return 0; | |
1387 | ||
866e6b7a | 1388 | if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0) |
99d2baa2 SS |
1389 | return 0; |
1390 | ||
1391 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1392 | if (r < 0) | |
1393 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1394 | ||
1395 | if (link->network->arp >= 0) { | |
1396 | ifi_change |= IFF_NOARP; | |
e6ebebbe SS |
1397 | SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0); |
1398 | } | |
1399 | ||
1400 | if (link->network->multicast >= 0) { | |
1401 | ifi_change |= IFF_MULTICAST; | |
1402 | SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast); | |
99d2baa2 SS |
1403 | } |
1404 | ||
866e6b7a SS |
1405 | if (link->network->allmulticast >= 0) { |
1406 | ifi_change |= IFF_ALLMULTI; | |
1407 | SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast); | |
1408 | } | |
1409 | ||
99d2baa2 SS |
1410 | r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change); |
1411 | if (r < 0) | |
1412 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1413 | ||
ee38400b YW |
1414 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler, |
1415 | link_netlink_destroy_callback, link, 0); | |
99d2baa2 SS |
1416 | if (r < 0) |
1417 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1418 | ||
1419 | link_ref(link); | |
1420 | ||
1421 | return 0; | |
1422 | } | |
1423 | ||
e1853b00 | 1424 | static int link_set_bridge(Link *link) { |
4afd3348 | 1425 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
e1853b00 SS |
1426 | int r; |
1427 | ||
1428 | assert(link); | |
1429 | assert(link->network); | |
1430 | ||
6a7a4e4d LP |
1431 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1432 | if (r < 0) | |
1433 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
1434 | |
1435 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
1436 | if (r < 0) |
1437 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 | 1438 | |
1c4baffc | 1439 | r = sd_netlink_message_open_container(req, IFLA_PROTINFO); |
6a7a4e4d LP |
1440 | if (r < 0) |
1441 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 | 1442 | |
7f9915f0 SS |
1443 | if (link->network->use_bpdu >= 0) { |
1444 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_GUARD, link->network->use_bpdu); | |
1445 | if (r < 0) | |
1446 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_GUARD attribute: %m"); | |
1447 | } | |
eb7ff4dd | 1448 | |
7f9915f0 SS |
1449 | if (link->network->hairpin >= 0) { |
1450 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_MODE, link->network->hairpin); | |
1451 | if (r < 0) | |
1452 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_MODE attribute: %m"); | |
1453 | } | |
eb7ff4dd | 1454 | |
7f9915f0 SS |
1455 | if (link->network->fast_leave >= 0) { |
1456 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_FAST_LEAVE, link->network->fast_leave); | |
1457 | if (r < 0) | |
1458 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m"); | |
1459 | } | |
eb7ff4dd | 1460 | |
7f9915f0 SS |
1461 | if (link->network->allow_port_to_be_root >= 0) { |
1462 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_PROTECT, link->network->allow_port_to_be_root); | |
1463 | if (r < 0) | |
1464 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PROTECT attribute: %m"); | |
eb7ff4dd | 1465 | |
7f9915f0 SS |
1466 | } |
1467 | ||
1468 | if (link->network->unicast_flood >= 0) { | |
1469 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_UNICAST_FLOOD, link->network->unicast_flood); | |
1470 | if (r < 0) | |
1471 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m"); | |
1472 | ||
1473 | } | |
eb7ff4dd | 1474 | |
34437b4f | 1475 | if (link->network->cost != 0) { |
1c4baffc | 1476 | r = sd_netlink_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); |
6a7a4e4d LP |
1477 | if (r < 0) |
1478 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 | 1479 | } |
b56be296 DJL |
1480 | if (link->network->priority != LINK_BRIDGE_PORT_PRIORITY_INVALID) { |
1481 | r = sd_netlink_message_append_u16(req, IFLA_BRPORT_PRIORITY, link->network->priority); | |
1482 | if (r < 0) | |
1483 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PRIORITY attribute: %m"); | |
1484 | } | |
e1853b00 | 1485 | |
1c4baffc | 1486 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1487 | if (r < 0) |
1488 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 | 1489 | |
ee38400b YW |
1490 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_set_handler, |
1491 | link_netlink_destroy_callback, link, 0); | |
6a7a4e4d LP |
1492 | if (r < 0) |
1493 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
1494 | |
1495 | link_ref(link); | |
1496 | ||
1497 | return r; | |
1498 | } | |
1499 | ||
eb64b435 SS |
1500 | static int link_bond_set(Link *link) { |
1501 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1502 | int r; | |
1503 | ||
1504 | assert(link); | |
1505 | assert(link->network); | |
1506 | ||
1507 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_NEWLINK, link->network->bond->ifindex); | |
1508 | if (r < 0) | |
1509 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1510 | ||
1511 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
1512 | if (r < 0) | |
1513 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1514 | ||
1515 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
1516 | if (r < 0) | |
1517 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
1518 | ||
1519 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, "bond"); | |
1520 | if (r < 0) | |
1521 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1522 | ||
1523 | if (link->network->active_slave) { | |
1524 | r = sd_netlink_message_append_u32(req, IFLA_BOND_ACTIVE_SLAVE, link->ifindex); | |
1525 | if (r < 0) | |
1526 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m"); | |
1527 | } | |
1528 | ||
1529 | if (link->network->primary_slave) { | |
1530 | r = sd_netlink_message_append_u32(req, IFLA_BOND_PRIMARY, link->ifindex); | |
1531 | if (r < 0) | |
1532 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_PRIMARY attribute: %m"); | |
1533 | } | |
1534 | ||
1535 | r = sd_netlink_message_close_container(req); | |
1536 | if (r < 0) | |
1537 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
1538 | ||
1539 | r = sd_netlink_message_close_container(req); | |
1540 | if (r < 0) | |
1541 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1542 | ||
ee38400b YW |
1543 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler, |
1544 | link_netlink_destroy_callback, link, 0); | |
eb64b435 SS |
1545 | if (r < 0) |
1546 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1547 | ||
1548 | link_ref(link); | |
1549 | ||
1550 | return r; | |
1551 | } | |
1552 | ||
34437b4f LP |
1553 | static int link_lldp_save(Link *link) { |
1554 | _cleanup_free_ char *temp_path = NULL; | |
1555 | _cleanup_fclose_ FILE *f = NULL; | |
1556 | sd_lldp_neighbor **l = NULL; | |
1557 | int n = 0, r, i; | |
49699bac SS |
1558 | |
1559 | assert(link); | |
34437b4f LP |
1560 | assert(link->lldp_file); |
1561 | ||
1562 | if (!link->lldp) { | |
1563 | (void) unlink(link->lldp_file); | |
1564 | return 0; | |
1565 | } | |
1566 | ||
1567 | r = sd_lldp_get_neighbors(link->lldp, &l); | |
1568 | if (r < 0) | |
1569 | goto finish; | |
1570 | if (r == 0) { | |
1571 | (void) unlink(link->lldp_file); | |
1572 | goto finish; | |
1573 | } | |
1574 | ||
1575 | n = r; | |
1576 | ||
1577 | r = fopen_temporary(link->lldp_file, &f, &temp_path); | |
1578 | if (r < 0) | |
1579 | goto finish; | |
1580 | ||
1581 | fchmod(fileno(f), 0644); | |
49699bac | 1582 | |
34437b4f LP |
1583 | for (i = 0; i < n; i++) { |
1584 | const void *p; | |
1585 | le64_t u; | |
1586 | size_t sz; | |
1587 | ||
1588 | r = sd_lldp_neighbor_get_raw(l[i], &p, &sz); | |
9ef61f2e | 1589 | if (r < 0) |
34437b4f LP |
1590 | goto finish; |
1591 | ||
1592 | u = htole64(sz); | |
1593 | (void) fwrite(&u, 1, sizeof(u), f); | |
1594 | (void) fwrite(p, 1, sz, f); | |
1595 | } | |
49699bac | 1596 | |
34437b4f LP |
1597 | r = fflush_and_check(f); |
1598 | if (r < 0) | |
1599 | goto finish; | |
1600 | ||
1601 | if (rename(temp_path, link->lldp_file) < 0) { | |
1602 | r = -errno; | |
1603 | goto finish; | |
9ef61f2e | 1604 | } |
34437b4f LP |
1605 | |
1606 | finish: | |
1607 | if (r < 0) { | |
1608 | (void) unlink(link->lldp_file); | |
1609 | if (temp_path) | |
1610 | (void) unlink(temp_path); | |
1611 | ||
1612 | log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file); | |
1613 | } | |
1614 | ||
1615 | if (l) { | |
1616 | for (i = 0; i < n; i++) | |
1617 | sd_lldp_neighbor_unref(l[i]); | |
1618 | free(l); | |
1619 | } | |
1620 | ||
1621 | return r; | |
1622 | } | |
1623 | ||
90dffb22 | 1624 | static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) { |
34437b4f | 1625 | Link *link = userdata; |
90dffb22 | 1626 | int r; |
34437b4f LP |
1627 | |
1628 | assert(link); | |
1629 | ||
1630 | (void) link_lldp_save(link); | |
90dffb22 | 1631 | |
7272b25e | 1632 | if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) { |
90dffb22 LP |
1633 | /* If we received information about a new neighbor, restart the LLDP "fast" logic */ |
1634 | ||
1635 | log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission."); | |
1636 | ||
7272b25e | 1637 | r = link_lldp_emit_start(link); |
90dffb22 LP |
1638 | if (r < 0) |
1639 | log_link_warning_errno(link, r, "Failed to restart LLDP transmission: %m"); | |
1640 | } | |
49699bac SS |
1641 | } |
1642 | ||
e7ab854c TG |
1643 | static int link_acquire_ipv6_conf(Link *link) { |
1644 | int r; | |
1645 | ||
1646 | assert(link); | |
1647 | ||
e7ab854c | 1648 | if (link_ipv6_accept_ra_enabled(link)) { |
1e7a0e21 | 1649 | assert(link->ndisc); |
e7ab854c TG |
1650 | |
1651 | log_link_debug(link, "Discovering IPv6 routers"); | |
1652 | ||
1e7a0e21 | 1653 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1654 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1655 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1656 | } | |
1657 | ||
7465dd22 PF |
1658 | if (link_radv_enabled(link)) { |
1659 | assert(link->radv); | |
1660 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1661 | ||
1662 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
1663 | ||
1664 | r = sd_radv_start(link->radv); | |
1665 | if (r < 0 && r != -EBUSY) | |
1666 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); | |
1667 | } | |
1668 | ||
10752343 PF |
1669 | (void) dhcp6_request_prefix_delegation(link); |
1670 | ||
e7ab854c TG |
1671 | return 0; |
1672 | } | |
1673 | ||
6fc25497 | 1674 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1675 | int r; |
1676 | ||
1677 | assert(link); | |
1678 | assert(link->network); | |
ff254138 TG |
1679 | assert(link->manager); |
1680 | assert(link->manager->event); | |
1681 | ||
78c958f8 | 1682 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 1683 | assert(link->ipv4ll); |
ff254138 | 1684 | |
6a7a4e4d | 1685 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1686 | |
1687 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1688 | if (r < 0) |
1689 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1690 | } |
1691 | ||
78c958f8 | 1692 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1693 | assert(link->dhcp_client); |
ff254138 | 1694 | |
6a7a4e4d | 1695 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1696 | |
5c1d3fc9 | 1697 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1698 | if (r < 0) |
1699 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1700 | } |
ff254138 | 1701 | |
6fc25497 SS |
1702 | return 0; |
1703 | } | |
1704 | ||
1705 | static int link_acquire_conf(Link *link) { | |
1706 | int r; | |
1707 | ||
1708 | assert(link); | |
1709 | ||
1710 | r = link_acquire_ipv4_conf(link); | |
1711 | if (r < 0) | |
1712 | return r; | |
1713 | ||
1714 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) == 0) { | |
1715 | r = link_acquire_ipv6_conf(link); | |
1716 | if (r < 0) | |
1717 | return r; | |
1718 | } | |
1719 | ||
7272b25e LP |
1720 | if (link_lldp_emit_enabled(link)) { |
1721 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1722 | if (r < 0) |
1723 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1724 | } | |
1725 | ||
ff254138 TG |
1726 | return 0; |
1727 | } | |
1728 | ||
a61bb41c | 1729 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1730 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1731 | ||
a61bb41c | 1732 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1733 | return true; |
1734 | ||
a61bb41c | 1735 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1736 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 1737 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
1738 | return true; |
1739 | ||
1740 | return false; | |
1741 | } | |
1742 | ||
1c4baffc | 1743 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1744 | Link *link = userdata; |
dd3efc09 TG |
1745 | int r; |
1746 | ||
1746cf2a TG |
1747 | assert(link); |
1748 | ||
5da8149f | 1749 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1750 | return 1; |
1751 | ||
1c4baffc | 1752 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d | 1753 | if (r < 0) |
26d6b214 | 1754 | /* we warn but don't fail the link, as it may be brought up later */ |
a2fae7bb | 1755 | log_link_warning_errno(link, r, "Could not bring up interface: %m"); |
45ad2c13 | 1756 | |
f882c247 TG |
1757 | return 1; |
1758 | } | |
1759 | ||
14b6bb77 | 1760 | int link_up(Link *link) { |
4afd3348 | 1761 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c3072ea | 1762 | uint8_t ipv6ll_mode; |
f579559b TG |
1763 | int r; |
1764 | ||
f882c247 | 1765 | assert(link); |
c106cc36 | 1766 | assert(link->network); |
f882c247 TG |
1767 | assert(link->manager); |
1768 | assert(link->manager->rtnl); | |
1769 | ||
6a7a4e4d | 1770 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1771 | |
6a7a4e4d LP |
1772 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1773 | if (r < 0) | |
1774 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1775 | |
2b2d8603 | 1776 | /* set it free if not enslaved with networkd */ |
6cb955c6 | 1777 | if (!link->network->bridge && !link->network->bond && !link->network->vrf) { |
2b2d8603 TY |
1778 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); |
1779 | if (r < 0) | |
1780 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
1781 | } | |
1782 | ||
5d4795f3 | 1783 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1784 | if (r < 0) |
1785 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1786 | |
c106cc36 | 1787 | if (link->network->mac) { |
1c4baffc | 1788 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1789 | if (r < 0) |
1790 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1791 | } |
1792 | ||
1c4baffc | 1793 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1794 | if (r < 0) |
1795 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1796 | |
439689c6 | 1797 | if (link_ipv6_enabled(link)) { |
01d28f81 | 1798 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ |
1c4baffc | 1799 | r = sd_netlink_message_open_container(req, AF_INET6); |
01d28f81 TG |
1800 | if (r < 0) |
1801 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1802 | |
fbc38f23 TY |
1803 | if (!link_ipv6ll_enabled(link)) |
1804 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
1805 | else { | |
1806 | const char *p = NULL; | |
1807 | _cleanup_free_ char *stable_secret = NULL; | |
1808 | ||
1809 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/stable_secret"); | |
1810 | r = read_one_line_file(p, &stable_secret); | |
1811 | ||
1812 | if (r < 0) | |
1813 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1814 | else | |
1815 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1816 | } | |
1c4baffc | 1817 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); |
01d28f81 TG |
1818 | if (r < 0) |
1819 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1820 | |
01d28f81 | 1821 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1c4baffc | 1822 | r = sd_netlink_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); |
01d28f81 TG |
1823 | if (r < 0) |
1824 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1825 | } | |
1826 | ||
1c4baffc | 1827 | r = sd_netlink_message_close_container(req); |
6a7a4e4d | 1828 | if (r < 0) |
01d28f81 | 1829 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1830 | } |
d0d6a4cd | 1831 | |
1c4baffc | 1832 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1833 | if (r < 0) |
1834 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1835 | |
ee38400b YW |
1836 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, |
1837 | link_netlink_destroy_callback, link, 0); | |
6a7a4e4d LP |
1838 | if (r < 0) |
1839 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1840 | |
b226d99b TG |
1841 | link_ref(link); |
1842 | ||
f882c247 TG |
1843 | return 0; |
1844 | } | |
1845 | ||
bd5038f8 MKB |
1846 | static int link_up_can(Link *link) { |
1847 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1848 | int r; | |
1849 | ||
1850 | assert(link); | |
1851 | ||
1852 | log_link_debug(link, "Bringing CAN link up"); | |
1853 | ||
1854 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1855 | if (r < 0) | |
1856 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1857 | ||
1858 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
1859 | if (r < 0) | |
1860 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1861 | ||
ee38400b YW |
1862 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, |
1863 | link_netlink_destroy_callback, link, 0); | |
bd5038f8 MKB |
1864 | if (r < 0) |
1865 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1866 | ||
1867 | link_ref(link); | |
1868 | ||
1869 | return 0; | |
1870 | } | |
1871 | ||
06828bb6 HP |
1872 | static int link_set_can(Link *link) { |
1873 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
1874 | int r; | |
1875 | ||
1876 | assert(link); | |
1877 | assert(link->network); | |
1878 | assert(link->manager); | |
1879 | assert(link->manager->rtnl); | |
1880 | ||
1881 | log_link_debug(link, "link_set_can"); | |
1882 | ||
1883 | r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, link->ifindex); | |
1884 | if (r < 0) | |
1885 | return log_link_error_errno(link, r, "Failed to allocate netlink message: %m"); | |
1886 | ||
1887 | r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK); | |
1888 | if (r < 0) | |
1889 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1890 | ||
1891 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
1892 | if (r < 0) | |
1893 | return log_link_error_errno(link, r, "Failed to open netlink container: %m"); | |
1894 | ||
1895 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind); | |
1896 | if (r < 0) | |
1897 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1898 | ||
1899 | if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) { | |
1900 | struct can_bittiming bt = { | |
1901 | .bitrate = link->network->can_bitrate, | |
1902 | .sample_point = link->network->can_sample_point, | |
1903 | }; | |
1904 | ||
1905 | if (link->network->can_bitrate > UINT32_MAX) { | |
1906 | log_link_error(link, "bitrate (%zu) too big.", link->network->can_bitrate); | |
1907 | return -ERANGE; | |
1908 | } | |
1909 | ||
1910 | log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate); | |
1911 | if (link->network->can_sample_point > 0) | |
1912 | log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10); | |
1913 | else | |
1914 | log_link_debug(link, "Using default sample point"); | |
1915 | ||
1916 | r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); | |
1917 | if (r < 0) | |
1918 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m"); | |
1919 | } | |
1920 | ||
1921 | if (link->network->can_restart_us > 0) { | |
1922 | char time_string[FORMAT_TIMESPAN_MAX]; | |
1923 | uint64_t restart_ms; | |
1924 | ||
1925 | if (link->network->can_restart_us == USEC_INFINITY) | |
1926 | restart_ms = 0; | |
1927 | else | |
1928 | restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC); | |
1929 | ||
1930 | format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC); | |
1931 | ||
1932 | if (restart_ms > UINT32_MAX) { | |
1933 | log_link_error(link, "restart timeout (%s) too big.", time_string); | |
1934 | return -ERANGE; | |
1935 | } | |
1936 | ||
1937 | log_link_debug(link, "Setting restart = %s", time_string); | |
1938 | ||
1939 | r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms); | |
1940 | if (r < 0) | |
1941 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m"); | |
1942 | } | |
1943 | ||
1944 | r = sd_netlink_message_close_container(m); | |
1945 | if (r < 0) | |
1946 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
1947 | ||
1948 | r = sd_netlink_message_close_container(m); | |
1949 | if (r < 0) | |
1950 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
1951 | ||
ee38400b YW |
1952 | r = sd_netlink_call_async(link->manager->rtnl, NULL, m, link_set_handler, |
1953 | link_netlink_destroy_callback, link, 0); | |
06828bb6 HP |
1954 | if (r < 0) |
1955 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1956 | ||
1957 | link_ref(link); | |
1958 | ||
1959 | if (!(link->flags & IFF_UP)) { | |
1960 | r = link_up_can(link); | |
1961 | if (r < 0) { | |
1962 | link_enter_failed(link); | |
1963 | return r; | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | log_link_debug(link, "link_set_can done"); | |
1968 | ||
1969 | return r; | |
1970 | } | |
1971 | ||
1c4baffc | 1972 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1973 | Link *link = userdata; |
0d4ad91d AR |
1974 | int r; |
1975 | ||
1976 | assert(link); | |
1977 | ||
1978 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1979 | return 1; | |
1980 | ||
1c4baffc | 1981 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1982 | if (r < 0) |
a2fae7bb | 1983 | log_link_warning_errno(link, r, "Could not bring down interface: %m"); |
0d4ad91d | 1984 | |
1046bf9b | 1985 | if (streq_ptr(link->kind, "can")) |
06828bb6 | 1986 | link_set_can(link); |
06828bb6 | 1987 | |
0d4ad91d AR |
1988 | return 1; |
1989 | } | |
1990 | ||
14b6bb77 | 1991 | int link_down(Link *link) { |
4afd3348 | 1992 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1993 | int r; |
1994 | ||
1995 | assert(link); | |
1996 | assert(link->manager); | |
1997 | assert(link->manager->rtnl); | |
1998 | ||
6a7a4e4d | 1999 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
2000 | |
2001 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
2002 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
2003 | if (r < 0) |
2004 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
2005 | |
2006 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
2007 | if (r < 0) |
2008 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 2009 | |
ee38400b YW |
2010 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_down_handler, |
2011 | link_netlink_destroy_callback, link, 0); | |
6a7a4e4d LP |
2012 | if (r < 0) |
2013 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
2014 | |
2015 | link_ref(link); | |
2016 | ||
2017 | return 0; | |
2018 | } | |
2019 | ||
2020 | static int link_handle_bound_to_list(Link *link) { | |
2021 | Link *l; | |
2022 | Iterator i; | |
2023 | int r; | |
2024 | bool required_up = false; | |
2025 | bool link_is_up = false; | |
2026 | ||
2027 | assert(link); | |
2028 | ||
2029 | if (hashmap_isempty(link->bound_to_links)) | |
2030 | return 0; | |
2031 | ||
2032 | if (link->flags & IFF_UP) | |
2033 | link_is_up = true; | |
2034 | ||
2035 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
2036 | if (link_has_carrier(l)) { | |
2037 | required_up = true; | |
2038 | break; | |
2039 | } | |
2040 | ||
2041 | if (!required_up && link_is_up) { | |
2042 | r = link_down(link); | |
2043 | if (r < 0) | |
2044 | return r; | |
2045 | } else if (required_up && !link_is_up) { | |
2046 | r = link_up(link); | |
2047 | if (r < 0) | |
2048 | return r; | |
2049 | } | |
2050 | ||
2051 | return 0; | |
2052 | } | |
2053 | ||
2054 | static int link_handle_bound_by_list(Link *link) { | |
2055 | Iterator i; | |
2056 | Link *l; | |
2057 | int r; | |
2058 | ||
2059 | assert(link); | |
2060 | ||
2061 | if (hashmap_isempty(link->bound_by_links)) | |
2062 | return 0; | |
2063 | ||
2064 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
2065 | r = link_handle_bound_to_list(l); | |
2066 | if (r < 0) | |
2067 | return r; | |
2068 | } | |
2069 | ||
2070 | return 0; | |
2071 | } | |
2072 | ||
2073 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
2074 | int r; | |
2075 | ||
2076 | assert(link); | |
2077 | assert(carrier); | |
2078 | ||
2079 | if (link == carrier) | |
2080 | return 0; | |
2081 | ||
2082 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
2083 | return 0; | |
2084 | ||
2085 | r = hashmap_ensure_allocated(h, NULL); | |
2086 | if (r < 0) | |
2087 | return r; | |
2088 | ||
2089 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
2090 | if (r < 0) | |
2091 | return r; | |
2092 | ||
2093 | return 0; | |
2094 | } | |
2095 | ||
2096 | static int link_new_bound_by_list(Link *link) { | |
2097 | Manager *m; | |
2098 | Link *carrier; | |
2099 | Iterator i; | |
2100 | int r; | |
2101 | bool list_updated = false; | |
2102 | ||
2103 | assert(link); | |
2104 | assert(link->manager); | |
2105 | ||
2106 | m = link->manager; | |
2107 | ||
b295beea | 2108 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
2109 | if (!carrier->network) |
2110 | continue; | |
2111 | ||
2112 | if (strv_isempty(carrier->network->bind_carrier)) | |
2113 | continue; | |
2114 | ||
2115 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
2116 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
2117 | if (r < 0) | |
2118 | return r; | |
2119 | ||
2120 | list_updated = true; | |
2121 | } | |
2122 | } | |
2123 | ||
2124 | if (list_updated) | |
84de38c5 | 2125 | link_dirty(link); |
0d4ad91d | 2126 | |
b295beea | 2127 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
2128 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
2129 | if (r < 0) | |
2130 | return r; | |
2131 | ||
84de38c5 | 2132 | link_dirty(carrier); |
0d4ad91d AR |
2133 | } |
2134 | ||
2135 | return 0; | |
2136 | } | |
2137 | ||
2138 | static int link_new_bound_to_list(Link *link) { | |
2139 | Manager *m; | |
2140 | Link *carrier; | |
2141 | Iterator i; | |
2142 | int r; | |
2143 | bool list_updated = false; | |
2144 | ||
2145 | assert(link); | |
2146 | assert(link->manager); | |
2147 | ||
2148 | if (!link->network) | |
2149 | return 0; | |
2150 | ||
2151 | if (strv_isempty(link->network->bind_carrier)) | |
2152 | return 0; | |
2153 | ||
2154 | m = link->manager; | |
2155 | ||
2156 | HASHMAP_FOREACH (carrier, m->links, i) { | |
2157 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
2158 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
2159 | if (r < 0) | |
2160 | return r; | |
2161 | ||
2162 | list_updated = true; | |
2163 | } | |
2164 | } | |
2165 | ||
2166 | if (list_updated) | |
84de38c5 | 2167 | link_dirty(link); |
0d4ad91d AR |
2168 | |
2169 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
2170 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
2171 | if (r < 0) | |
2172 | return r; | |
2173 | ||
84de38c5 | 2174 | link_dirty(carrier); |
0d4ad91d AR |
2175 | } |
2176 | ||
2177 | return 0; | |
2178 | } | |
2179 | ||
2180 | static int link_new_carrier_maps(Link *link) { | |
2181 | int r; | |
2182 | ||
2183 | r = link_new_bound_by_list(link); | |
2184 | if (r < 0) | |
2185 | return r; | |
2186 | ||
2187 | r = link_handle_bound_by_list(link); | |
2188 | if (r < 0) | |
2189 | return r; | |
2190 | ||
2191 | r = link_new_bound_to_list(link); | |
2192 | if (r < 0) | |
2193 | return r; | |
2194 | ||
2195 | r = link_handle_bound_to_list(link); | |
2196 | if (r < 0) | |
2197 | return r; | |
2198 | ||
2199 | return 0; | |
2200 | } | |
2201 | ||
2202 | static void link_free_bound_to_list(Link *link) { | |
2203 | Link *bound_to; | |
2204 | Iterator i; | |
2205 | ||
2206 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
2207 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
2208 | ||
2209 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 2210 | link_dirty(bound_to); |
0d4ad91d AR |
2211 | } |
2212 | ||
2213 | return; | |
2214 | } | |
2215 | ||
2216 | static void link_free_bound_by_list(Link *link) { | |
2217 | Link *bound_by; | |
2218 | Iterator i; | |
2219 | ||
2220 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
2221 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
2222 | ||
2223 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 2224 | link_dirty(bound_by); |
0d4ad91d AR |
2225 | link_handle_bound_to_list(bound_by); |
2226 | } | |
2227 | } | |
2228 | ||
2229 | return; | |
2230 | } | |
2231 | ||
2232 | static void link_free_carrier_maps(Link *link) { | |
2233 | bool list_updated = false; | |
2234 | ||
2235 | assert(link); | |
2236 | ||
2237 | if (!hashmap_isempty(link->bound_to_links)) { | |
2238 | link_free_bound_to_list(link); | |
2239 | list_updated = true; | |
2240 | } | |
2241 | ||
2242 | if (!hashmap_isempty(link->bound_by_links)) { | |
2243 | link_free_bound_by_list(link); | |
2244 | list_updated = true; | |
2245 | } | |
2246 | ||
2247 | if (list_updated) | |
84de38c5 | 2248 | link_dirty(link); |
0d4ad91d AR |
2249 | |
2250 | return; | |
2251 | } | |
2252 | ||
2253 | void link_drop(Link *link) { | |
2254 | if (!link || link->state == LINK_STATE_LINGER) | |
2255 | return; | |
2256 | ||
2257 | link_set_state(link, LINK_STATE_LINGER); | |
2258 | ||
2259 | link_free_carrier_maps(link); | |
2260 | ||
6a7a4e4d | 2261 | log_link_debug(link, "Link removed"); |
0d4ad91d | 2262 | |
db2f8a2e | 2263 | (void) unlink(link->state_file); |
0d4ad91d AR |
2264 | link_unref(link); |
2265 | ||
2266 | return; | |
2267 | } | |
2268 | ||
3f265037 | 2269 | static int link_joined(Link *link) { |
f882c247 TG |
2270 | int r; |
2271 | ||
ef1ba606 | 2272 | assert(link); |
f5be5601 | 2273 | assert(link->network); |
dd3efc09 | 2274 | |
0d4ad91d AR |
2275 | if (!hashmap_isempty(link->bound_to_links)) { |
2276 | r = link_handle_bound_to_list(link); | |
2277 | if (r < 0) | |
2278 | return r; | |
2279 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
2280 | r = link_up(link); |
2281 | if (r < 0) { | |
2282 | link_enter_failed(link); | |
2283 | return r; | |
2284 | } | |
ef1ba606 | 2285 | } |
f882c247 | 2286 | |
9ed794a3 | 2287 | if (link->network->bridge) { |
e1853b00 | 2288 | r = link_set_bridge(link); |
6a7a4e4d LP |
2289 | if (r < 0) |
2290 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
2291 | } |
2292 | ||
eb64b435 SS |
2293 | if (link->network->bond) { |
2294 | r = link_bond_set(link); | |
2295 | if (r < 0) | |
2296 | log_link_error_errno(link, r, "Could not set bond message: %m"); | |
2297 | } | |
2298 | ||
ffff9abe TJ |
2299 | if (link->network->use_br_vlan && |
2300 | (link->network->bridge || streq_ptr("bridge", link->kind))) { | |
13b498f9 TJ |
2301 | r = link_set_bridge_vlan(link); |
2302 | if (r < 0) | |
2303 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2304 | } | |
2305 | ||
c1835a42 RM |
2306 | /* Skip setting up addresses until it gets carrier, |
2307 | or it would try to set addresses twice, | |
2308 | which is bad for non-idempotent steps. */ | |
dad2d78e | 2309 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
c1835a42 RM |
2310 | return 0; |
2311 | ||
fb6730c4 | 2312 | return link_enter_set_addresses(link); |
02b59d57 TG |
2313 | } |
2314 | ||
62e2d5bb | 2315 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 2316 | Link *link = userdata; |
02b59d57 TG |
2317 | int r; |
2318 | ||
1746cf2a | 2319 | assert(link); |
ef1ba606 | 2320 | assert(link->network); |
02b59d57 | 2321 | |
313cefa1 | 2322 | link->enslaving--; |
52433f6b | 2323 | |
5da8149f | 2324 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2325 | return 1; |
2326 | ||
1c4baffc | 2327 | r = sd_netlink_message_get_errno(m); |
856f962c | 2328 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 2329 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
2330 | link_enter_failed(link); |
2331 | return 1; | |
ba179154 | 2332 | } else |
6a7a4e4d | 2333 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 2334 | |
856f962c | 2335 | if (link->enslaving <= 0) |
3f265037 | 2336 | link_joined(link); |
02b59d57 TG |
2337 | |
2338 | return 1; | |
2339 | } | |
2340 | ||
3f265037 | 2341 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2342 | NetDev *netdev; |
672682a6 | 2343 | Iterator i; |
02b59d57 TG |
2344 | int r; |
2345 | ||
2346 | assert(link); | |
2347 | assert(link->network); | |
8434fd5c | 2348 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 2349 | |
e331e246 | 2350 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 2351 | |
84de38c5 | 2352 | link_dirty(link); |
fe8db0c5 | 2353 | |
7951dea2 SS |
2354 | if (!link->network->bridge && |
2355 | !link->network->bond && | |
6cb955c6 | 2356 | !link->network->vrf && |
6a0a2f86 | 2357 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 2358 | return link_joined(link); |
02b59d57 | 2359 | |
d9c67ea1 | 2360 | if (link->network->bond) { |
f2341e0a LP |
2361 | log_struct(LOG_DEBUG, |
2362 | LOG_LINK_INTERFACE(link), | |
2363 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2364 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname)); |
f2341e0a LP |
2365 | |
2366 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2367 | if (r < 0) { |
f2341e0a LP |
2368 | log_struct_errno(LOG_WARNING, r, |
2369 | LOG_LINK_INTERFACE(link), | |
2370 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2371 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname)); |
52433f6b TG |
2372 | link_enter_failed(link); |
2373 | return r; | |
2374 | } | |
2375 | ||
313cefa1 | 2376 | link->enslaving++; |
0ad6148e MO |
2377 | } |
2378 | ||
d9c67ea1 | 2379 | if (link->network->bridge) { |
f2341e0a LP |
2380 | log_struct(LOG_DEBUG, |
2381 | LOG_LINK_INTERFACE(link), | |
2382 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2383 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname)); |
f2341e0a LP |
2384 | |
2385 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2386 | if (r < 0) { |
f2341e0a LP |
2387 | log_struct_errno(LOG_WARNING, r, |
2388 | LOG_LINK_INTERFACE(link), | |
2389 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2390 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname)); |
0ad6148e MO |
2391 | link_enter_failed(link); |
2392 | return r; | |
2393 | } | |
2394 | ||
313cefa1 | 2395 | link->enslaving++; |
52433f6b TG |
2396 | } |
2397 | ||
6cb955c6 AR |
2398 | if (link->network->vrf) { |
2399 | log_struct(LOG_DEBUG, | |
2400 | LOG_LINK_INTERFACE(link), | |
2401 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 ZJS |
2402 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname)); |
2403 | ||
6cb955c6 AR |
2404 | r = netdev_join(link->network->vrf, link, netdev_join_handler); |
2405 | if (r < 0) { | |
2406 | log_struct_errno(LOG_WARNING, r, | |
2407 | LOG_LINK_INTERFACE(link), | |
2408 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 | 2409 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname)); |
6cb955c6 AR |
2410 | link_enter_failed(link); |
2411 | return r; | |
2412 | } | |
2413 | ||
2414 | link->enslaving++; | |
2415 | } | |
2416 | ||
6a0a2f86 | 2417 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2418 | |
a63e5daa SS |
2419 | if (netdev->ifindex > 0) { |
2420 | link_joined(link); | |
2421 | continue; | |
2422 | } | |
2423 | ||
f2341e0a LP |
2424 | log_struct(LOG_DEBUG, |
2425 | LOG_LINK_INTERFACE(link), | |
2426 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2427 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); |
f2341e0a LP |
2428 | |
2429 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2430 | if (r < 0) { |
f2341e0a LP |
2431 | log_struct_errno(LOG_WARNING, r, |
2432 | LOG_LINK_INTERFACE(link), | |
2433 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2434 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); |
326cb406 SS |
2435 | link_enter_failed(link); |
2436 | return r; | |
2437 | } | |
2438 | ||
313cefa1 | 2439 | link->enslaving++; |
326cb406 SS |
2440 | } |
2441 | ||
ef1ba606 TG |
2442 | return 0; |
2443 | } | |
2444 | ||
769d324c | 2445 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2446 | int r; |
2447 | ||
765afd5c | 2448 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2449 | return 0; |
2450 | ||
765afd5c LP |
2451 | /* We propagate the forwarding flag from one interface to the |
2452 | * global setting one way. This means: as long as at least one | |
2453 | * interface was configured at any time that had IP forwarding | |
2454 | * enabled the setting will stay on for good. We do this | |
2455 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2456 | * somewhat in sync (see below). */ | |
15dee3f0 | 2457 | |
765afd5c | 2458 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2459 | if (r < 0) |
765afd5c | 2460 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2461 | |
769d324c LP |
2462 | return 0; |
2463 | } | |
2464 | ||
2465 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2466 | int r; |
2467 | ||
765afd5c | 2468 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2469 | return 0; |
2470 | ||
61233823 | 2471 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2472 | * packet forwarding setting: either packet forwarding is on |
2473 | * for all, or off for all. We hence don't bother with a | |
2474 | * per-interface setting, but simply propagate the interface | |
2475 | * flag, if it is set, to the global flag, one-way. Note that | |
2476 | * while IPv4 would allow a per-interface flag, we expose the | |
2477 | * same behaviour there and also propagate the setting from | |
2478 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2479 | |
765afd5c | 2480 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2481 | if (r < 0) |
765afd5c | 2482 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2483 | |
2484 | return 0; | |
2485 | } | |
2486 | ||
49092e22 | 2487 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2488 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2489 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2490 | const char *p = NULL; |
2491 | int r; | |
2492 | ||
1f0d9695 | 2493 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2494 | if (s < 0) |
49092e22 SS |
2495 | return 0; |
2496 | ||
2497 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2498 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2499 | |
eb3da901 LP |
2500 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2501 | if (r < 0) | |
49092e22 SS |
2502 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2503 | ||
2504 | return 0; | |
2505 | } | |
2506 | ||
4f2e437a | 2507 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2508 | const char *p = NULL; |
4f2e437a SS |
2509 | int r; |
2510 | ||
2511 | /* Make this a NOP if IPv6 is not available */ | |
2512 | if (!socket_ipv6_is_supported()) | |
2513 | return 0; | |
2514 | ||
2515 | if (link->flags & IFF_LOOPBACK) | |
2516 | return 0; | |
2517 | ||
d68e2e59 LP |
2518 | if (!link->network) |
2519 | return 0; | |
2520 | ||
4f2e437a | 2521 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2522 | |
0053598f | 2523 | /* We handle router advertisements ourselves, tell the kernel to GTFO */ |
fe307276 | 2524 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2525 | if (r < 0) |
fe307276 | 2526 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2527 | |
2528 | return 0; | |
2529 | } | |
2530 | ||
8749cbcd | 2531 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2532 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2533 | const char *p = NULL; |
2534 | int r; | |
2535 | ||
2536 | /* Make this a NOP if IPv6 is not available */ | |
2537 | if (!socket_ipv6_is_supported()) | |
2538 | return 0; | |
2539 | ||
2540 | if (link->flags & IFF_LOOPBACK) | |
2541 | return 0; | |
2542 | ||
d68e2e59 LP |
2543 | if (!link->network) |
2544 | return 0; | |
2545 | ||
8749cbcd SS |
2546 | if (link->network->ipv6_dad_transmits < 0) |
2547 | return 0; | |
2548 | ||
2549 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2550 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2551 | |
eb3da901 LP |
2552 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2553 | if (r < 0) | |
8749cbcd | 2554 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2555 | |
2556 | return 0; | |
2557 | } | |
2558 | ||
b69c3180 | 2559 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2560 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2561 | const char *p = NULL; |
2562 | int r; | |
2563 | ||
2564 | /* Make this a NOP if IPv6 is not available */ | |
2565 | if (!socket_ipv6_is_supported()) | |
2566 | return 0; | |
2567 | ||
2568 | if (link->flags & IFF_LOOPBACK) | |
2569 | return 0; | |
2570 | ||
d68e2e59 LP |
2571 | if (!link->network) |
2572 | return 0; | |
2573 | ||
b69c3180 SS |
2574 | if (link->network->ipv6_hop_limit < 0) |
2575 | return 0; | |
2576 | ||
2577 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2578 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2579 | |
eb3da901 LP |
2580 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2581 | if (r < 0) | |
b69c3180 | 2582 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2583 | |
2584 | return 0; | |
2585 | } | |
2586 | ||
11102cba SS |
2587 | static int link_set_ipv6_mtu(Link *link) { |
2588 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; | |
2589 | const char *p = NULL; | |
2590 | int r; | |
2591 | ||
2592 | /* Make this a NOP if IPv6 is not available */ | |
2593 | if (!socket_ipv6_is_supported()) | |
2594 | return 0; | |
2595 | ||
2596 | if (link->flags & IFF_LOOPBACK) | |
2597 | return 0; | |
2598 | ||
2599 | if (link->network->ipv6_mtu == 0) | |
2600 | return 0; | |
2601 | ||
2602 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/mtu"); | |
2603 | ||
4e964aa0 | 2604 | xsprintf(buf, "%" PRIu32, link->network->ipv6_mtu); |
11102cba SS |
2605 | |
2606 | r = write_string_file(p, buf, 0); | |
2607 | if (r < 0) | |
2608 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m"); | |
2609 | ||
2610 | return 0; | |
2611 | } | |
2612 | ||
5e5b137a TG |
2613 | static int link_drop_foreign_config(Link *link) { |
2614 | Address *address; | |
2615 | Route *route; | |
2616 | Iterator i; | |
2617 | int r; | |
2618 | ||
2619 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2620 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2621 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2622 | continue; | |
2623 | ||
2624 | r = address_remove(address, link, link_address_remove_handler); | |
2625 | if (r < 0) | |
2626 | return r; | |
2627 | } | |
2628 | ||
2629 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2630 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2631 | if (route->protocol == RTPROT_KERNEL) |
2632 | continue; | |
2633 | ||
3fb1ac5d | 2634 | r = route_remove(route, link, link_route_remove_handler); |
5e5b137a TG |
2635 | if (r < 0) |
2636 | return r; | |
2637 | } | |
2638 | ||
2639 | return 0; | |
2640 | } | |
2641 | ||
3104883d | 2642 | static int link_drop_config(Link *link) { |
410a7f15 | 2643 | Address *address, *pool_address; |
3104883d SS |
2644 | Route *route; |
2645 | Iterator i; | |
2646 | int r; | |
2647 | ||
2648 | SET_FOREACH(address, link->addresses, i) { | |
2649 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
2650 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) | |
2651 | continue; | |
2652 | ||
2653 | r = address_remove(address, link, link_address_remove_handler); | |
2654 | if (r < 0) | |
2655 | return r; | |
410a7f15 RM |
2656 | |
2657 | /* If this address came from an address pool, clean up the pool */ | |
2658 | LIST_FOREACH(addresses, pool_address, link->pool_addresses) { | |
2659 | if (address_equal(address, pool_address)) { | |
2660 | LIST_REMOVE(addresses, link->pool_addresses, pool_address); | |
2661 | address_free(pool_address); | |
2662 | break; | |
2663 | } | |
2664 | } | |
3104883d SS |
2665 | } |
2666 | ||
2667 | SET_FOREACH(route, link->routes, i) { | |
2668 | /* do not touch routes managed by the kernel */ | |
2669 | if (route->protocol == RTPROT_KERNEL) | |
2670 | continue; | |
2671 | ||
2672 | r = route_remove(route, link, link_route_remove_handler); | |
2673 | if (r < 0) | |
2674 | return r; | |
2675 | } | |
2676 | ||
c69305ff LP |
2677 | ndisc_flush(link); |
2678 | ||
3104883d SS |
2679 | return 0; |
2680 | } | |
2681 | ||
273eec24 LP |
2682 | static int link_update_lldp(Link *link) { |
2683 | int r; | |
2684 | ||
2685 | assert(link); | |
2686 | ||
2687 | if (!link->lldp) | |
2688 | return 0; | |
2689 | ||
2690 | if (link->flags & IFF_UP) { | |
2691 | r = sd_lldp_start(link->lldp); | |
2692 | if (r > 0) | |
2693 | log_link_debug(link, "Started LLDP."); | |
2694 | } else { | |
2695 | r = sd_lldp_stop(link->lldp); | |
2696 | if (r > 0) | |
2697 | log_link_debug(link, "Stopped LLDP."); | |
2698 | } | |
2699 | ||
2700 | return r; | |
2701 | } | |
2702 | ||
93ea7750 MKB |
2703 | static int link_configure_can(Link *link) { |
2704 | int r; | |
2705 | ||
06828bb6 HP |
2706 | if (streq_ptr(link->kind, "can")) { |
2707 | /* The CAN interface must be down to configure bitrate, etc... */ | |
2708 | if ((link->flags & IFF_UP)) { | |
2709 | r = link_down(link); | |
2710 | if (r < 0) { | |
2711 | link_enter_failed(link); | |
2712 | return r; | |
2713 | } | |
2714 | ||
2715 | return 0; | |
2716 | } | |
2717 | ||
2718 | return link_set_can(link); | |
2719 | } | |
2720 | ||
93ea7750 MKB |
2721 | if (!(link->flags & IFF_UP)) { |
2722 | r = link_up_can(link); | |
2723 | if (r < 0) { | |
2724 | link_enter_failed(link); | |
2725 | return r; | |
2726 | } | |
2727 | } | |
2728 | ||
2729 | return 0; | |
2730 | } | |
2731 | ||
a748b692 | 2732 | static int link_configure(Link *link) { |
02b59d57 TG |
2733 | int r; |
2734 | ||
ef1ba606 | 2735 | assert(link); |
b22d8a00 | 2736 | assert(link->network); |
8434fd5c | 2737 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2738 | |
06828bb6 | 2739 | if (STRPTR_IN_SET(link->kind, "can", "vcan")) |
93ea7750 | 2740 | return link_configure_can(link); |
92c918b0 | 2741 | |
02e28621 ДГ |
2742 | /* Drop foreign config, but ignore loopback or critical devices. |
2743 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2744 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2745 | r = link_drop_foreign_config(link); |
2746 | if (r < 0) | |
2747 | return r; | |
2748 | } | |
5e5b137a | 2749 | |
23d8b221 SS |
2750 | r = link_set_proxy_arp(link); |
2751 | if (r < 0) | |
2752 | return r; | |
2753 | ||
a0e5c15d FK |
2754 | r = ipv6_proxy_ndp_addresses_configure(link); |
2755 | if (r < 0) | |
2756 | return r; | |
2757 | ||
769d324c LP |
2758 | r = link_set_ipv4_forward(link); |
2759 | if (r < 0) | |
2760 | return r; | |
2761 | ||
2762 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2763 | if (r < 0) |
2764 | return r; | |
2765 | ||
49092e22 SS |
2766 | r = link_set_ipv6_privacy_extensions(link); |
2767 | if (r < 0) | |
2768 | return r; | |
2769 | ||
4f2e437a SS |
2770 | r = link_set_ipv6_accept_ra(link); |
2771 | if (r < 0) | |
2772 | return r; | |
2773 | ||
8749cbcd SS |
2774 | r = link_set_ipv6_dad_transmits(link); |
2775 | if (r < 0) | |
2776 | return r; | |
b69c3180 SS |
2777 | |
2778 | r = link_set_ipv6_hop_limit(link); | |
2779 | if (r < 0) | |
2780 | return r; | |
8749cbcd | 2781 | |
99d2baa2 SS |
2782 | r = link_set_flags(link); |
2783 | if (r < 0) | |
2784 | return r; | |
2785 | ||
11102cba SS |
2786 | r = link_set_ipv6_mtu(link); |
2787 | if (r < 0) | |
2788 | return r; | |
2789 | ||
78c958f8 | 2790 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2791 | r = ipv4ll_configure(link); |
eb34d4af TG |
2792 | if (r < 0) |
2793 | return r; | |
2794 | } | |
2795 | ||
78c958f8 | 2796 | if (link_dhcp4_enabled(link)) { |
64b21ece MV |
2797 | r = dhcp4_set_promote_secondaries(link); |
2798 | if (r < 0) | |
2799 | return r; | |
2800 | ||
3c9b8860 | 2801 | r = dhcp4_configure(link); |
eb34d4af TG |
2802 | if (r < 0) |
2803 | return r; | |
eb34d4af TG |
2804 | } |
2805 | ||
78c958f8 | 2806 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2807 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2808 | if (r < 0) | |
2809 | return r; | |
2810 | ||
2811 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2812 | if (r < 0) | |
2813 | return r; | |
dd43110f TG |
2814 | } |
2815 | ||
62379e88 TG |
2816 | if (link_dhcp6_enabled(link) || |
2817 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2818 | r = dhcp6_configure(link); |
2819 | if (r < 0) | |
2820 | return r; | |
2821 | } | |
2822 | ||
2823 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2824 | r = ndisc_configure(link); |
4138fb2c PF |
2825 | if (r < 0) |
2826 | return r; | |
2827 | } | |
2828 | ||
7465dd22 PF |
2829 | if (link_radv_enabled(link)) { |
2830 | r = radv_configure(link); | |
2831 | if (r < 0) | |
2832 | return r; | |
2833 | } | |
2834 | ||
8e1ad1ea | 2835 | if (link_lldp_rx_enabled(link)) { |
fc6a313b LP |
2836 | r = sd_lldp_new(&link->lldp); |
2837 | if (r < 0) | |
2838 | return r; | |
2839 | ||
2840 | r = sd_lldp_set_ifindex(link->lldp, link->ifindex); | |
34437b4f LP |
2841 | if (r < 0) |
2842 | return r; | |
2843 | ||
2844 | r = sd_lldp_match_capabilities(link->lldp, | |
2845 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2846 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2847 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2848 | if (r < 0) |
2849 | return r; | |
2850 | ||
b553a6b1 LP |
2851 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2852 | if (r < 0) | |
2853 | return r; | |
2854 | ||
ce43e484 SS |
2855 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2856 | if (r < 0) | |
2857 | return r; | |
49699bac | 2858 | |
032b27f5 | 2859 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2860 | if (r < 0) |
2861 | return r; | |
273eec24 LP |
2862 | |
2863 | r = link_update_lldp(link); | |
2864 | if (r < 0) | |
2865 | return r; | |
ce43e484 SS |
2866 | } |
2867 | ||
44b598a1 SS |
2868 | if (link->network->mtu > 0) { |
2869 | r = link_set_mtu(link, link->network->mtu); | |
2870 | if (r < 0) | |
2871 | return r; | |
2872 | } | |
2873 | ||
55dc8c4a YW |
2874 | return link_configure_after_setting_mtu(link); |
2875 | } | |
2876 | ||
2877 | static int link_configure_after_setting_mtu(Link *link) { | |
2878 | int r; | |
2879 | ||
2880 | assert(link); | |
2881 | assert(link->network); | |
2882 | assert(link->state == LINK_STATE_PENDING); | |
2883 | ||
2884 | if (link->setting_mtu) | |
2885 | return 0; | |
2886 | ||
dad2d78e | 2887 | if (link_has_carrier(link) || link->network->configure_without_carrier) { |
1e9be60b TG |
2888 | r = link_acquire_conf(link); |
2889 | if (r < 0) | |
2890 | return r; | |
cc544d5f | 2891 | } |
1e9be60b | 2892 | |
3f265037 | 2893 | return link_enter_join_netdev(link); |
505f8da7 TG |
2894 | } |
2895 | ||
27dfc982 YW |
2896 | static int duid_set_uuid(DUID *duid, sd_id128_t uuid) { |
2897 | assert(duid); | |
2898 | ||
2899 | if (duid->raw_data_len > 0) | |
2900 | return 0; | |
2901 | ||
2902 | if (duid->type != DUID_TYPE_UUID) | |
2903 | return -EINVAL; | |
2904 | ||
2905 | memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t)); | |
2906 | duid->raw_data_len = sizeof(sd_id128_t); | |
2907 | ||
2908 | return 1; | |
2909 | } | |
2910 | ||
2911 | int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
2912 | Manager *manager = userdata; | |
2913 | const sd_bus_error *e; | |
2914 | const void *a; | |
2915 | size_t sz; | |
2916 | DUID *duid; | |
2917 | Link *link; | |
2918 | int r; | |
2919 | ||
2920 | assert(m); | |
2921 | assert(manager); | |
2922 | ||
2923 | e = sd_bus_message_get_error(m); | |
2924 | if (e) { | |
2925 | log_error_errno(sd_bus_error_get_errno(e), | |
ab4dd984 | 2926 | "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s", |
27dfc982 YW |
2927 | e->message); |
2928 | goto configure; | |
2929 | } | |
2930 | ||
2931 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
2932 | if (r < 0) | |
2933 | goto configure; | |
2934 | ||
2935 | if (sz != sizeof(sd_id128_t)) { | |
ab4dd984 | 2936 | log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID."); |
27dfc982 YW |
2937 | goto configure; |
2938 | } | |
2939 | ||
2940 | memcpy(&manager->product_uuid, a, sz); | |
2941 | while ((duid = set_steal_first(manager->duids_requesting_uuid))) | |
2942 | (void) duid_set_uuid(duid, manager->product_uuid); | |
2943 | ||
2944 | manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid); | |
2945 | ||
2946 | configure: | |
2947 | while ((link = set_steal_first(manager->links_requesting_uuid))) { | |
2948 | r = link_configure(link); | |
2949 | if (r < 0) | |
2950 | log_link_error_errno(link, r, "Failed to configure link: %m"); | |
2951 | } | |
2952 | ||
2953 | manager->links_requesting_uuid = set_free(manager->links_requesting_uuid); | |
2954 | ||
2955 | /* To avoid calling GetProductUUID() bus method so frequently, set the flag below | |
2956 | * even if the method fails. */ | |
2957 | manager->has_product_uuid = true; | |
2958 | ||
2959 | return 1; | |
2960 | } | |
2961 | ||
2962 | static bool link_requires_uuid(Link *link) { | |
2963 | const DUID *duid; | |
2964 | ||
2965 | assert(link); | |
2966 | assert(link->manager); | |
2967 | assert(link->network); | |
2968 | ||
2969 | duid = link_get_duid(link); | |
2970 | if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0) | |
2971 | return false; | |
2972 | ||
2973 | if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY)) | |
2974 | return true; | |
2975 | ||
2976 | if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link)) | |
2977 | return true; | |
2978 | ||
2979 | return false; | |
2980 | } | |
2981 | ||
2982 | static int link_configure_duid(Link *link) { | |
2983 | Manager *m; | |
2984 | DUID *duid; | |
2985 | int r; | |
2986 | ||
2987 | assert(link); | |
2988 | assert(link->manager); | |
2989 | assert(link->network); | |
2990 | ||
2991 | m = link->manager; | |
2992 | duid = link_get_duid(link); | |
2993 | ||
2994 | if (!link_requires_uuid(link)) | |
2995 | return 1; | |
2996 | ||
2997 | if (m->has_product_uuid) { | |
2998 | (void) duid_set_uuid(duid, m->product_uuid); | |
2999 | return 1; | |
3000 | } | |
3001 | ||
3002 | if (!m->links_requesting_uuid) { | |
3003 | r = manager_request_product_uuid(m, link); | |
3004 | if (r < 0) { | |
3005 | if (r == -ENOMEM) | |
3006 | return r; | |
3007 | ||
ab4dd984 ZJS |
3008 | log_link_warning_errno(link, r, |
3009 | "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); | |
27dfc982 YW |
3010 | return 1; |
3011 | } | |
3012 | } else { | |
3013 | r = set_put(m->links_requesting_uuid, link); | |
3014 | if (r < 0) | |
3015 | return log_oom(); | |
3016 | ||
3017 | r = set_put(m->duids_requesting_uuid, duid); | |
3018 | if (r < 0) | |
3019 | return log_oom(); | |
3020 | } | |
3021 | ||
3022 | return 0; | |
3023 | } | |
3024 | ||
e6bf7774 | 3025 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 3026 | Network *network; |
505f8da7 TG |
3027 | int r; |
3028 | ||
3029 | assert(link); | |
3030 | assert(link->ifname); | |
3031 | assert(link->manager); | |
3032 | ||
8434fd5c | 3033 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 3034 | return 1; |
505f8da7 | 3035 | |
6a7a4e4d | 3036 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 3037 | |
0d4ad91d AR |
3038 | r = link_new_bound_by_list(link); |
3039 | if (r < 0) | |
3040 | return r; | |
3041 | ||
3042 | r = link_handle_bound_by_list(link); | |
3043 | if (r < 0) | |
3044 | return r; | |
3045 | ||
c4a03a56 | 3046 | if (!link->network) { |
51517f9e | 3047 | r = network_get(link->manager, link->sd_device, link->ifname, |
c4a03a56 TG |
3048 | &link->mac, &network); |
3049 | if (r == -ENOENT) { | |
3050 | link_enter_unmanaged(link); | |
3051 | return 1; | |
a09dc546 DM |
3052 | } else if (r == 0 && network->unmanaged) { |
3053 | link_enter_unmanaged(link); | |
3054 | return 0; | |
c4a03a56 TG |
3055 | } else if (r < 0) |
3056 | return r; | |
505f8da7 | 3057 | |
c4a03a56 TG |
3058 | if (link->flags & IFF_LOOPBACK) { |
3059 | if (network->link_local != ADDRESS_FAMILY_NO) | |
3060 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 3061 | |
c4a03a56 TG |
3062 | if (network->dhcp != ADDRESS_FAMILY_NO) |
3063 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 3064 | |
c4a03a56 TG |
3065 | if (network->dhcp_server) |
3066 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
3067 | } | |
bd2efe92 | 3068 | |
7d342c03 | 3069 | r = network_apply(network, link); |
c4a03a56 TG |
3070 | if (r < 0) |
3071 | return r; | |
3072 | } | |
505f8da7 | 3073 | |
0d4ad91d AR |
3074 | r = link_new_bound_to_list(link); |
3075 | if (r < 0) | |
3076 | return r; | |
3077 | ||
27dfc982 YW |
3078 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, |
3079 | * link_configure() is called later asynchronously. */ | |
3080 | r = link_configure_duid(link); | |
3081 | if (r <= 0) | |
3082 | return r; | |
3083 | ||
a748b692 TG |
3084 | r = link_configure(link); |
3085 | if (r < 0) | |
3086 | return r; | |
3087 | ||
5da8149f | 3088 | return 1; |
505f8da7 TG |
3089 | } |
3090 | ||
e6bf7774 YW |
3091 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
3092 | (void) link_initialized_and_synced(userdata); | |
3093 | return 1; | |
3094 | } | |
3095 | ||
51517f9e | 3096 | int link_initialized(Link *link, sd_device *device) { |
4afd3348 | 3097 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
3098 | int r; |
3099 | ||
3100 | assert(link); | |
3101 | assert(link->manager); | |
3102 | assert(link->manager->rtnl); | |
3103 | assert(device); | |
3104 | ||
8434fd5c | 3105 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
3106 | return 0; |
3107 | ||
51517f9e | 3108 | if (link->sd_device) |
679b3605 TG |
3109 | return 0; |
3110 | ||
79008bdd | 3111 | log_link_debug(link, "udev initialized link"); |
4f561e8e | 3112 | |
51517f9e | 3113 | link->sd_device = sd_device_ref(device); |
4f561e8e | 3114 | |
3c9b8860 TG |
3115 | /* udev has initialized the link, but we don't know if we have yet |
3116 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
3117 | * when it returns we know that the pending NEWLINKs have already been | |
3118 | * processed and that we are up-to-date */ | |
4f561e8e | 3119 | |
3c9b8860 TG |
3120 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
3121 | link->ifindex); | |
4f561e8e TG |
3122 | if (r < 0) |
3123 | return r; | |
3124 | ||
ee38400b YW |
3125 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler, |
3126 | link_netlink_destroy_callback, link, 0); | |
4f561e8e TG |
3127 | if (r < 0) |
3128 | return r; | |
3129 | ||
5da8149f TG |
3130 | link_ref(link); |
3131 | ||
4f561e8e TG |
3132 | return 0; |
3133 | } | |
3134 | ||
c4a03a56 | 3135 | static int link_load(Link *link) { |
0bc70f1d TG |
3136 | _cleanup_free_ char *network_file = NULL, |
3137 | *addresses = NULL, | |
f703cc2c | 3138 | *routes = NULL, |
0bc70f1d TG |
3139 | *dhcp4_address = NULL, |
3140 | *ipv4ll_address = NULL; | |
3141 | union in_addr_union address; | |
f703cc2c | 3142 | union in_addr_union route_dst; |
c598ac76 | 3143 | const char *p; |
c4a03a56 TG |
3144 | int r; |
3145 | ||
3146 | assert(link); | |
3147 | ||
1a5a177e | 3148 | r = parse_env_file(NULL, link->state_file, NEWLINE, |
c4a03a56 TG |
3149 | "NETWORK_FILE", &network_file, |
3150 | "ADDRESSES", &addresses, | |
f703cc2c | 3151 | "ROUTES", &routes, |
0bc70f1d TG |
3152 | "DHCP4_ADDRESS", &dhcp4_address, |
3153 | "IPV4LL_ADDRESS", &ipv4ll_address, | |
c4a03a56 TG |
3154 | NULL); |
3155 | if (r < 0 && r != -ENOENT) | |
3156 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
3157 | ||
3158 | if (network_file) { | |
3159 | Network *network; | |
3160 | char *suffix; | |
3161 | ||
3162 | /* drop suffix */ | |
3163 | suffix = strrchr(network_file, '.'); | |
3164 | if (!suffix) { | |
3165 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
3166 | goto network_file_fail; | |
3167 | } | |
3168 | *suffix = '\0'; | |
3169 | ||
3170 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
3171 | if (r < 0) { | |
3172 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
3173 | goto network_file_fail; | |
3174 | } | |
3175 | ||
7d342c03 | 3176 | r = network_apply(network, link); |
c4a03a56 TG |
3177 | if (r < 0) |
3178 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
3179 | } | |
3180 | ||
3181 | network_file_fail: | |
3182 | ||
3183 | if (addresses) { | |
c598ac76 | 3184 | p = addresses; |
c4a03a56 | 3185 | |
c598ac76 TG |
3186 | for (;;) { |
3187 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
3188 | char *prefixlen_str; |
3189 | int family; | |
3190 | unsigned char prefixlen; | |
c4a03a56 | 3191 | |
c598ac76 TG |
3192 | r = extract_first_word(&p, &address_str, NULL, 0); |
3193 | if (r < 0) { | |
3194 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
3195 | continue; | |
928bc597 TA |
3196 | } |
3197 | if (r == 0) | |
c598ac76 TG |
3198 | break; |
3199 | ||
3200 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 3201 | if (!prefixlen_str) { |
c598ac76 | 3202 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
3203 | continue; |
3204 | } | |
3205 | ||
313cefa1 | 3206 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
3207 | |
3208 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
3209 | if (r != 1) { | |
3210 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
3211 | continue; | |
3212 | } | |
3213 | ||
c598ac76 | 3214 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 3215 | if (r < 0) { |
c598ac76 | 3216 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
3217 | continue; |
3218 | } | |
3219 | ||
3220 | r = address_add(link, family, &address, prefixlen, NULL); | |
3221 | if (r < 0) | |
3222 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
3223 | } | |
3224 | } | |
3225 | ||
f703cc2c | 3226 | if (routes) { |
74544b4e TA |
3227 | p = routes; |
3228 | ||
c598ac76 | 3229 | for (;;) { |
f833694d | 3230 | Route *route; |
c598ac76 | 3231 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 3232 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 3233 | usec_t lifetime; |
f703cc2c TG |
3234 | char *prefixlen_str; |
3235 | int family; | |
3236 | unsigned char prefixlen, tos, table; | |
3237 | uint32_t priority; | |
3238 | ||
c598ac76 TG |
3239 | r = extract_first_word(&p, &route_str, NULL, 0); |
3240 | if (r < 0) { | |
3241 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
3242 | continue; | |
928bc597 TA |
3243 | } |
3244 | if (r == 0) | |
c598ac76 TG |
3245 | break; |
3246 | ||
3247 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 3248 | if (!prefixlen_str) { |
c598ac76 | 3249 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
3250 | continue; |
3251 | } | |
3252 | ||
313cefa1 | 3253 | *prefixlen_str++ = '\0'; |
f703cc2c | 3254 | |
f833694d TG |
3255 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
3256 | if (r != 5) { | |
3257 | log_link_debug(link, | |
3258 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
3259 | prefixlen_str); | |
f703cc2c TG |
3260 | continue; |
3261 | } | |
3262 | ||
c598ac76 | 3263 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 3264 | if (r < 0) { |
c598ac76 | 3265 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
3266 | continue; |
3267 | } | |
3268 | ||
f833694d | 3269 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
3270 | if (r < 0) |
3271 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d | 3272 | |
f02ba163 | 3273 | if (lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
f833694d TG |
3274 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, |
3275 | 0, route_expire_handler, route); | |
3276 | if (r < 0) | |
3277 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
3278 | } | |
3279 | ||
3280 | route->lifetime = lifetime; | |
3281 | sd_event_source_unref(route->expire); | |
1cc6c93a | 3282 | route->expire = TAKE_PTR(expire); |
f703cc2c TG |
3283 | } |
3284 | } | |
3285 | ||
0bc70f1d TG |
3286 | if (dhcp4_address) { |
3287 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
3288 | if (r < 0) { | |
b68d26b8 | 3289 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3290 | goto dhcp4_address_fail; |
3291 | } | |
3292 | ||
b1c626f6 | 3293 | r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0); |
0bc70f1d | 3294 | if (r < 0) |
b68d26b8 | 3295 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d TG |
3296 | |
3297 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
3298 | if (r < 0) | |
b68d26b8 | 3299 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3300 | } |
3301 | ||
3302 | dhcp4_address_fail: | |
3303 | ||
3304 | if (ipv4ll_address) { | |
3305 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
3306 | if (r < 0) { | |
b68d26b8 | 3307 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3308 | goto ipv4ll_address_fail; |
3309 | } | |
3310 | ||
3311 | r = sd_ipv4ll_new(&link->ipv4ll); | |
3312 | if (r < 0) | |
b68d26b8 | 3313 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d TG |
3314 | |
3315 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
3316 | if (r < 0) | |
b68d26b8 | 3317 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3318 | } |
3319 | ||
3320 | ipv4ll_address_fail: | |
3321 | ||
c4a03a56 TG |
3322 | return 0; |
3323 | } | |
3324 | ||
1c4baffc | 3325 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
51517f9e | 3326 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
505f8da7 | 3327 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
51517f9e YW |
3328 | int initialized, r; |
3329 | Link *link; | |
505f8da7 TG |
3330 | |
3331 | assert(m); | |
fbbeb65a | 3332 | assert(m->rtnl); |
505f8da7 TG |
3333 | assert(message); |
3334 | assert(ret); | |
3335 | ||
3336 | r = link_new(m, message, ret); | |
3337 | if (r < 0) | |
3338 | return r; | |
3339 | ||
3340 | link = *ret; | |
3341 | ||
6a7a4e4d | 3342 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 3343 | |
c4a03a56 TG |
3344 | r = link_load(link); |
3345 | if (r < 0) | |
3346 | return r; | |
3347 | ||
75f86906 | 3348 | if (detect_container() <= 0) { |
505f8da7 | 3349 | /* not in a container, udev will be around */ |
ae06ab10 | 3350 | sprintf(ifindex_str, "n%d", link->ifindex); |
51517f9e YW |
3351 | r = sd_device_new_from_device_id(&device, ifindex_str); |
3352 | if (r < 0) { | |
3353 | log_link_warning_errno(link, r, "Could not find device: %m"); | |
5c416fc4 TG |
3354 | goto failed; |
3355 | } | |
505f8da7 | 3356 | |
51517f9e YW |
3357 | r = sd_device_get_is_initialized(device, &initialized); |
3358 | if (r < 0) { | |
3359 | log_link_warning_errno(link, r, "Could not determine whether the device is initialized or not: %m"); | |
3360 | goto failed; | |
3361 | } | |
3362 | if (!initialized) { | |
505f8da7 | 3363 | /* not yet ready */ |
79008bdd | 3364 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 3365 | return 0; |
3c4cb064 | 3366 | } |
505f8da7 | 3367 | |
4f561e8e TG |
3368 | r = link_initialized(link, device); |
3369 | if (r < 0) | |
5c416fc4 | 3370 | goto failed; |
4f561e8e | 3371 | } else { |
e6bf7774 | 3372 | r = link_initialized_and_synced(link); |
4f561e8e | 3373 | if (r < 0) |
5c416fc4 | 3374 | goto failed; |
4f561e8e | 3375 | } |
505f8da7 | 3376 | |
a748b692 | 3377 | return 0; |
5c416fc4 TG |
3378 | failed: |
3379 | link_enter_failed(link); | |
3380 | return r; | |
a748b692 TG |
3381 | } |
3382 | ||
c601ebf7 | 3383 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
3384 | int r; |
3385 | ||
3386 | assert(link); | |
3387 | ||
3388 | log_link_info(link, "Gained IPv6LL"); | |
3389 | ||
c601ebf7 | 3390 | link->ipv6ll_address = *address; |
e7ab854c TG |
3391 | link_check_ready(link); |
3392 | ||
201f0c91 | 3393 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
3394 | r = link_acquire_ipv6_conf(link); |
3395 | if (r < 0) { | |
3396 | link_enter_failed(link); | |
3397 | return r; | |
3398 | } | |
3399 | } | |
3400 | ||
3401 | return 0; | |
3402 | } | |
3403 | ||
9c0a72f9 TG |
3404 | static int link_carrier_gained(Link *link) { |
3405 | int r; | |
3406 | ||
3407 | assert(link); | |
3408 | ||
201f0c91 | 3409 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
3410 | r = link_acquire_conf(link); |
3411 | if (r < 0) { | |
3412 | link_enter_failed(link); | |
3413 | return r; | |
3414 | } | |
6fc25497 SS |
3415 | |
3416 | r = link_enter_set_addresses(link); | |
3417 | if (r < 0) | |
3418 | return r; | |
9c0a72f9 TG |
3419 | } |
3420 | ||
0d4ad91d AR |
3421 | r = link_handle_bound_by_list(link); |
3422 | if (r < 0) | |
3423 | return r; | |
3424 | ||
9c0a72f9 TG |
3425 | return 0; |
3426 | } | |
3427 | ||
3428 | static int link_carrier_lost(Link *link) { | |
3429 | int r; | |
3430 | ||
3431 | assert(link); | |
3432 | ||
fbdb6605 | 3433 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
55dc8c4a YW |
3434 | * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the |
3435 | * configuration and stop the clients as well. */ | |
97e7fb39 SS |
3436 | if (link->setting_mtu) |
3437 | return 0; | |
3438 | ||
9c0a72f9 TG |
3439 | r = link_stop_clients(link); |
3440 | if (r < 0) { | |
3441 | link_enter_failed(link); | |
3442 | return r; | |
3443 | } | |
3444 | ||
28464ae0 SS |
3445 | if (link_dhcp4_server_enabled(link)) |
3446 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
45a9eac9 | 3447 | |
3104883d SS |
3448 | r = link_drop_config(link); |
3449 | if (r < 0) | |
3450 | return r; | |
3451 | ||
f258e948 MP |
3452 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING)) { |
3453 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); | |
c436d553 MM |
3454 | r = link_drop_foreign_config(link); |
3455 | if (r < 0) | |
3456 | return r; | |
3457 | } | |
3104883d | 3458 | |
0d4ad91d AR |
3459 | r = link_handle_bound_by_list(link); |
3460 | if (r < 0) | |
3461 | return r; | |
3462 | ||
9c0a72f9 TG |
3463 | return 0; |
3464 | } | |
3465 | ||
3466 | int link_carrier_reset(Link *link) { | |
3467 | int r; | |
3468 | ||
3469 | assert(link); | |
3470 | ||
3471 | if (link_has_carrier(link)) { | |
3472 | r = link_carrier_lost(link); | |
3473 | if (r < 0) | |
3474 | return r; | |
3475 | ||
3476 | r = link_carrier_gained(link); | |
3477 | if (r < 0) | |
3478 | return r; | |
3479 | ||
6a7a4e4d | 3480 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
3481 | } |
3482 | ||
3483 | return 0; | |
3484 | } | |
3485 | ||
1c4baffc | 3486 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 3487 | struct ether_addr mac; |
ca4e095a | 3488 | const char *ifname; |
afe7fd56 | 3489 | uint32_t mtu; |
a61bb41c | 3490 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
3491 | int r; |
3492 | ||
dd3efc09 | 3493 | assert(link); |
b8941f74 | 3494 | assert(link->ifname); |
22936833 TG |
3495 | assert(m); |
3496 | ||
7619683b | 3497 | if (link->state == LINK_STATE_LINGER) { |
6a7a4e4d | 3498 | log_link_info(link, "Link readded"); |
e331e246 | 3499 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
3500 | |
3501 | r = link_new_carrier_maps(link); | |
3502 | if (r < 0) | |
3503 | return r; | |
7619683b TG |
3504 | } |
3505 | ||
1c4baffc | 3506 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3507 | if (r >= 0 && !streq(ifname, link->ifname)) { |
aa2b10da | 3508 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
b8941f74 | 3509 | |
587c4586 YW |
3510 | if (link->state == LINK_STATE_PENDING) { |
3511 | r = free_and_strdup(&link->ifname, ifname); | |
3512 | if (r < 0) | |
3513 | return r; | |
3514 | } else { | |
3515 | Manager *manager = link->manager; | |
0d4ad91d | 3516 | |
587c4586 YW |
3517 | link_drop(link); |
3518 | r = link_add(manager, m, &link); | |
3519 | if (r < 0) | |
3520 | return r; | |
3521 | } | |
b8941f74 TG |
3522 | } |
3523 | ||
1c4baffc | 3524 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3525 | if (r >= 0 && mtu > 0) { |
3526 | link->mtu = mtu; | |
4e964aa0 | 3527 | if (link->original_mtu == 0) { |
afe7fd56 | 3528 | link->original_mtu = mtu; |
6a7a4e4d | 3529 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3530 | } |
3531 | ||
3532 | if (link->dhcp_client) { | |
3c9b8860 TG |
3533 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3534 | link->mtu); | |
fc95c359 YW |
3535 | if (r < 0) |
3536 | return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); | |
afe7fd56 | 3537 | } |
7465dd22 PF |
3538 | |
3539 | if (link->radv) { | |
3540 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
3541 | if (r < 0) | |
3542 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
3543 | } | |
9842de0d | 3544 | } |
69629de9 | 3545 | |
e9189a1f TG |
3546 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3547 | set, simply ignore them. */ | |
1c4baffc | 3548 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3549 | if (r >= 0) { |
3c9b8860 TG |
3550 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3551 | ETH_ALEN)) { | |
c49b33ac | 3552 | |
3c9b8860 TG |
3553 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3554 | ETH_ALEN); | |
c49b33ac | 3555 | |
79008bdd | 3556 | log_link_debug(link, "MAC address: " |
20861203 TG |
3557 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3558 | mac.ether_addr_octet[0], | |
3559 | mac.ether_addr_octet[1], | |
3560 | mac.ether_addr_octet[2], | |
3561 | mac.ether_addr_octet[3], | |
3562 | mac.ether_addr_octet[4], | |
3563 | mac.ether_addr_octet[5]); | |
c49b33ac | 3564 | |
20861203 TG |
3565 | if (link->ipv4ll) { |
3566 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3567 | if (r < 0) |
3568 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3569 | } |
c49b33ac | 3570 | |
20861203 | 3571 | if (link->dhcp_client) { |
3c9b8860 | 3572 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3573 | (const uint8_t *) &link->mac, |
3574 | sizeof (link->mac), | |
3575 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3576 | if (r < 0) |
3577 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3578 | |
fff1f40c YW |
3579 | r = dhcp4_set_client_identifier(link); |
3580 | if (r < 0) | |
3581 | return r; | |
c49b33ac | 3582 | } |
4138fb2c PF |
3583 | |
3584 | if (link->dhcp6_client) { | |
f24648a6 | 3585 | const DUID* duid = link_get_duid(link); |
8341a5c3 | 3586 | |
4138fb2c | 3587 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3588 | (const uint8_t *) &link->mac, |
3589 | sizeof (link->mac), | |
3590 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3591 | if (r < 0) |
3592 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
3593 | |
3594 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3595 | link->network->iaid); | |
3596 | if (r < 0) | |
3597 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3598 | ||
8341a5c3 ZJS |
3599 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3600 | duid->type, | |
3601 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3602 | duid->raw_data_len); | |
413708d1 VK |
3603 | if (r < 0) |
3604 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3605 | } |
7465dd22 PF |
3606 | |
3607 | if (link->radv) { | |
3608 | r = sd_radv_set_mac(link->radv, &link->mac); | |
3609 | if (r < 0) | |
3610 | return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m"); | |
3611 | } | |
420d2058 SS |
3612 | |
3613 | if (link->ndisc) { | |
3614 | r = sd_ndisc_set_mac(link->ndisc, &link->mac); | |
3615 | if (r < 0) | |
3616 | return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m"); | |
3617 | } | |
c49b33ac | 3618 | } |
4f882b2a TG |
3619 | } |
3620 | ||
a61bb41c TG |
3621 | had_carrier = link_has_carrier(link); |
3622 | ||
3623 | r = link_update_flags(link, m); | |
3624 | if (r < 0) | |
3625 | return r; | |
3626 | ||
273eec24 LP |
3627 | r = link_update_lldp(link); |
3628 | if (r < 0) | |
3629 | return r; | |
3630 | ||
a61bb41c TG |
3631 | carrier_gained = !had_carrier && link_has_carrier(link); |
3632 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3633 | ||
3634 | if (carrier_gained) { | |
6a7a4e4d | 3635 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3636 | |
9c0a72f9 TG |
3637 | r = link_carrier_gained(link); |
3638 | if (r < 0) | |
3639 | return r; | |
a61bb41c | 3640 | } else if (carrier_lost) { |
6a7a4e4d | 3641 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3642 | |
9c0a72f9 TG |
3643 | r = link_carrier_lost(link); |
3644 | if (r < 0) | |
a61bb41c | 3645 | return r; |
a61bb41c TG |
3646 | } |
3647 | ||
3648 | return 0; | |
dd3efc09 | 3649 | } |
fe8db0c5 | 3650 | |
b295beea LP |
3651 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3652 | bool space = false; | |
3653 | Iterator i; | |
3654 | Link *link; | |
3655 | ||
3656 | assert(f); | |
3657 | assert(prefix); | |
3658 | ||
3659 | if (hashmap_isempty(h)) | |
3660 | return; | |
3661 | ||
0d536673 | 3662 | fputs(prefix, f); |
b295beea LP |
3663 | HASHMAP_FOREACH(link, h, i) { |
3664 | if (space) | |
0d536673 | 3665 | fputc(' ', f); |
b295beea LP |
3666 | |
3667 | fprintf(f, "%i", link->ifindex); | |
3668 | space = true; | |
3669 | } | |
3670 | ||
0d536673 | 3671 | fputc('\n', f); |
b295beea LP |
3672 | } |
3673 | ||
fe8db0c5 | 3674 | int link_save(Link *link) { |
68a8723c | 3675 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3676 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3677 | const char *admin_state, *oper_state; |
e7780c8d | 3678 | Address *a; |
c1eb9872 | 3679 | Route *route; |
e7780c8d | 3680 | Iterator i; |
fe8db0c5 TG |
3681 | int r; |
3682 | ||
3683 | assert(link); | |
3684 | assert(link->state_file); | |
68a8723c | 3685 | assert(link->lease_file); |
bbf7c048 TG |
3686 | assert(link->manager); |
3687 | ||
370e9930 TG |
3688 | if (link->state == LINK_STATE_LINGER) { |
3689 | unlink(link->state_file); | |
3690 | return 0; | |
3691 | } | |
3692 | ||
34437b4f LP |
3693 | link_lldp_save(link); |
3694 | ||
deb2e523 TG |
3695 | admin_state = link_state_to_string(link->state); |
3696 | assert(admin_state); | |
3697 | ||
e375dcde TG |
3698 | oper_state = link_operstate_to_string(link->operstate); |
3699 | assert(oper_state); | |
deb2e523 | 3700 | |
fe8db0c5 TG |
3701 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3702 | if (r < 0) | |
6a7a4e4d | 3703 | goto fail; |
fe8db0c5 | 3704 | |
0d536673 | 3705 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 3706 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
3707 | |
3708 | fprintf(f, | |
3709 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3710 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3711 | "OPER_STATE=%s\n", |
3712 | admin_state, oper_state); | |
fe8db0c5 | 3713 | |
bcb7a07e | 3714 | if (link->network) { |
ea352b40 | 3715 | bool space; |
07bdc70d | 3716 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3717 | const char *dhcp_domainname = NULL; |
3718 | char **dhcp6_domains = NULL; | |
b85bc551 | 3719 | char **dhcp_domains = NULL; |
5512a963 | 3720 | unsigned j; |
07bdc70d | 3721 | |
c1a38904 MTL |
3722 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
3723 | yes_no(link->network->required_for_online)); | |
3724 | ||
07bdc70d | 3725 | if (link->dhcp6_client) { |
4058d339 TG |
3726 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3727 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3728 | log_link_debug(link, "No DHCPv6 lease"); |
3729 | } | |
b0e39c82 | 3730 | |
adc5b2e2 TG |
3731 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3732 | ||
0d536673 | 3733 | fputs("DNS=", f); |
ea352b40 | 3734 | space = false; |
5512a963 LP |
3735 | |
3736 | for (j = 0; j < link->network->n_dns; j++) { | |
3737 | _cleanup_free_ char *b = NULL; | |
3738 | ||
3739 | r = in_addr_to_string(link->network->dns[j].family, | |
3740 | &link->network->dns[j].address, &b); | |
3741 | if (r < 0) { | |
3742 | log_debug_errno(r, "Failed to format address, ignoring: %m"); | |
3743 | continue; | |
3744 | } | |
3745 | ||
3746 | if (space) | |
0d536673 LP |
3747 | fputc(' ', f); |
3748 | fputs(b, f); | |
5512a963 LP |
3749 | space = true; |
3750 | } | |
d5314fff | 3751 | |
27cb34f5 | 3752 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3753 | link->dhcp_lease) { |
3754 | const struct in_addr *addresses; | |
3755 | ||
3756 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3757 | if (r > 0) { | |
ea352b40 | 3758 | if (space) |
0d536673 | 3759 | fputc(' ', f); |
b0e39c82 | 3760 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3761 | space = true; |
3762 | } | |
3763 | } | |
3764 | ||
27cb34f5 | 3765 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3766 | struct in6_addr *in6_addrs; |
3767 | ||
3768 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3769 | if (r > 0) { | |
3770 | if (space) | |
0d536673 | 3771 | fputc(' ', f); |
07bdc70d | 3772 | serialize_in6_addrs(f, in6_addrs, r); |
1e7a0e21 LP |
3773 | space = true; |
3774 | } | |
3775 | } | |
3776 | ||
3777 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3778 | ndisc_vacuum(link); | |
3779 | ||
b296797f | 3780 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
3781 | NDiscRDNSS *dd; |
3782 | ||
3783 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3784 | if (space) | |
0d536673 | 3785 | fputc(' ', f); |
1e7a0e21 LP |
3786 | |
3787 | serialize_in6_addrs(f, &dd->address, 1); | |
3788 | space = true; | |
b0e39c82 TG |
3789 | } |
3790 | } | |
3791 | ||
0d536673 | 3792 | fputc('\n', f); |
b0e39c82 | 3793 | |
0d536673 | 3794 | fputs("NTP=", f); |
ea352b40 | 3795 | space = false; |
3df9bec5 | 3796 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3797 | |
27cb34f5 | 3798 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3799 | link->dhcp_lease) { |
3800 | const struct in_addr *addresses; | |
3801 | ||
3802 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3803 | if (r > 0) { | |
ea352b40 | 3804 | if (space) |
0d536673 | 3805 | fputc(' ', f); |
b0e39c82 | 3806 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3807 | space = true; |
3808 | } | |
3809 | } | |
3810 | ||
27cb34f5 | 3811 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3812 | struct in6_addr *in6_addrs; |
3813 | char **hosts; | |
07bdc70d PF |
3814 | |
3815 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3816 | &in6_addrs); | |
3817 | if (r > 0) { | |
3818 | if (space) | |
0d536673 | 3819 | fputc(' ', f); |
07bdc70d PF |
3820 | serialize_in6_addrs(f, in6_addrs, r); |
3821 | space = true; | |
3822 | } | |
3823 | ||
3824 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3825 | if (r > 0) |
3826 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3827 | } |
3828 | ||
0d536673 | 3829 | fputc('\n', f); |
bd8f6538 | 3830 | |
b2a81c0b | 3831 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 3832 | if (link->dhcp_lease) { |
b2a81c0b | 3833 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
3834 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
3835 | } | |
b2a81c0b LP |
3836 | if (dhcp6_lease) |
3837 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3838 | } |
3839 | ||
0d536673 | 3840 | fputs("DOMAINS=", f); |
b85bc551 | 3841 | space = false; |
b2a81c0b | 3842 | fputstrv(f, link->network->search_domains, NULL, &space); |
07bdc70d | 3843 | |
1e7a0e21 LP |
3844 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3845 | NDiscDNSSL *dd; | |
9b4d1882 | 3846 | |
1e7a0e21 LP |
3847 | if (dhcp_domainname) |
3848 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3849 | if (dhcp_domains) |
3850 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3851 | if (dhcp6_domains) |
3852 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3853 | ||
3854 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3855 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3856 | } | |
b2a81c0b | 3857 | |
0d536673 | 3858 | fputc('\n', f); |
6192b846 | 3859 | |
0d536673 | 3860 | fputs("ROUTE_DOMAINS=", f); |
b85bc551 DW |
3861 | space = false; |
3862 | fputstrv(f, link->network->route_domains, NULL, &space); | |
b2a81c0b | 3863 | |
1e7a0e21 LP |
3864 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3865 | NDiscDNSSL *dd; | |
b2a81c0b | 3866 | |
1e7a0e21 LP |
3867 | if (dhcp_domainname) |
3868 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3869 | if (dhcp_domains) |
3870 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3871 | if (dhcp6_domains) |
3872 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3873 | ||
3874 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3875 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3876 | } | |
b2a81c0b | 3877 | |
0d536673 | 3878 | fputc('\n', f); |
67272d15 | 3879 | |
3c9b8860 | 3880 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3881 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3882 | fprintf(f, "MDNS=%s\n", |
3883 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3884 | |
c9299be2 IT |
3885 | if (link->network->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
3886 | fprintf(f, "DNS_OVER_TLS=%s\n", | |
3887 | dns_over_tls_mode_to_string(link->network->dns_over_tls_mode)); | |
d050561a | 3888 | |
ad6c0475 LP |
3889 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3890 | fprintf(f, "DNSSEC=%s\n", | |
3891 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3892 | ||
8a516214 LP |
3893 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3894 | const char *n; | |
3895 | ||
0d536673 | 3896 | fputs("DNSSEC_NTA=", f); |
8a516214 | 3897 | space = false; |
d390f8ef LP |
3898 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3899 | fputs_with_space(f, n, NULL, &space); | |
0d536673 | 3900 | fputc('\n', f); |
8a516214 LP |
3901 | } |
3902 | ||
0d536673 | 3903 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3904 | space = false; |
3905 | SET_FOREACH(a, link->addresses, i) { | |
3906 | _cleanup_free_ char *address_str = NULL; | |
3907 | ||
3908 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3909 | if (r < 0) | |
3910 | goto fail; | |
3911 | ||
e7780c8d TG |
3912 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3913 | space = true; | |
3914 | } | |
0d536673 | 3915 | fputc('\n', f); |
c1eb9872 | 3916 | |
0d536673 | 3917 | fputs("ROUTES=", f); |
c1eb9872 TG |
3918 | space = false; |
3919 | SET_FOREACH(route, link->routes, i) { | |
3920 | _cleanup_free_ char *route_str = NULL; | |
3921 | ||
3922 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3923 | if (r < 0) | |
3924 | goto fail; | |
3925 | ||
ecc3f340 ZJS |
3926 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, |
3927 | space ? " " : "", route_str, | |
f833694d | 3928 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); |
c1eb9872 TG |
3929 | space = true; |
3930 | } | |
3931 | ||
0d536673 | 3932 | fputc('\n', f); |
bcb7a07e | 3933 | } |
7374f9d8 | 3934 | |
b295beea LP |
3935 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3936 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3937 | |
8eb9058d | 3938 | if (link->dhcp_lease) { |
0bc70f1d | 3939 | struct in_addr address; |
8eb9058d LP |
3940 | const char *tz = NULL; |
3941 | ||
0bc70f1d TG |
3942 | assert(link->network); |
3943 | ||
8eb9058d LP |
3944 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3945 | if (r >= 0) | |
3946 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3947 | |
0bc70f1d TG |
3948 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3949 | if (r >= 0) { | |
0d536673 | 3950 | fputs("DHCP4_ADDRESS=", f); |
0bc70f1d | 3951 | serialize_in_addrs(f, &address, 1); |
0d536673 | 3952 | fputc('\n', f); |
0bc70f1d | 3953 | } |
d9876a52 | 3954 | |
bd91b83e | 3955 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3956 | if (r < 0) |
c2d6bd61 | 3957 | goto fail; |
fe8db0c5 | 3958 | |
7374f9d8 | 3959 | fprintf(f, |
b0e39c82 TG |
3960 | "DHCP_LEASE=%s\n", |
3961 | link->lease_file); | |
deb2e523 | 3962 | } else |
68a8723c | 3963 | unlink(link->lease_file); |
fe8db0c5 | 3964 | |
0bc70f1d TG |
3965 | if (link->ipv4ll) { |
3966 | struct in_addr address; | |
3967 | ||
3968 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3969 | if (r >= 0) { | |
0d536673 | 3970 | fputs("IPV4LL_ADDRESS=", f); |
0bc70f1d | 3971 | serialize_in_addrs(f, &address, 1); |
0d536673 | 3972 | fputc('\n', f); |
0bc70f1d TG |
3973 | } |
3974 | } | |
3975 | ||
c2d6bd61 LP |
3976 | r = fflush_and_check(f); |
3977 | if (r < 0) | |
3978 | goto fail; | |
fe8db0c5 | 3979 | |
c2d6bd61 | 3980 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3981 | r = -errno; |
c2d6bd61 | 3982 | goto fail; |
fe8db0c5 TG |
3983 | } |
3984 | ||
c2d6bd61 | 3985 | return 0; |
dacd6cee | 3986 | |
c2d6bd61 | 3987 | fail: |
6a7a4e4d | 3988 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3989 | if (temp_path) |
3990 | (void) unlink(temp_path); | |
3991 | ||
dacd6cee | 3992 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3993 | } |
3994 | ||
84de38c5 TG |
3995 | /* The serialized state in /run is no longer up-to-date. */ |
3996 | void link_dirty(Link *link) { | |
3997 | int r; | |
3998 | ||
3999 | assert(link); | |
4000 | ||
0c241a37 SS |
4001 | /* mark manager dirty as link is dirty */ |
4002 | manager_dirty(link->manager); | |
4003 | ||
84de38c5 TG |
4004 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
4005 | if (r < 0) | |
4006 | /* allocation errors are ignored */ | |
4007 | return; | |
4008 | ||
4009 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
4010 | if (r <= 0) |
4011 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
4012 | return; |
4013 | ||
4014 | link_ref(link); | |
4015 | } | |
4016 | ||
4017 | /* The serialized state in /run is up-to-date */ | |
4018 | void link_clean(Link *link) { | |
4019 | assert(link); | |
4020 | assert(link->manager); | |
4021 | ||
1046bf9b | 4022 | link_unref(set_remove(link->manager->dirty_links, link)); |
84de38c5 TG |
4023 | } |
4024 | ||
fe8db0c5 | 4025 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 4026 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
4027 | [LINK_STATE_ENSLAVING] = "configuring", |
4028 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
4029 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
4030 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 4031 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 4032 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 4033 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
4034 | }; |
4035 | ||
4036 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
4037 | |
4038 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
4039 | [LINK_OPERSTATE_OFF] = "off", |
4040 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
4041 | [LINK_OPERSTATE_DORMANT] = "dormant", |
4042 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
4043 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
4044 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
4045 | }; | |
4046 | ||
4047 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |