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