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