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