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