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