]>
Commit | Line | Data |
---|---|---|
f579559b TG |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <netinet/ether.h> | |
23 | #include <linux/if.h> | |
4cc7a82c | 24 | #include <unistd.h> |
f579559b | 25 | |
f579559b | 26 | #include "util.h" |
505f8da7 | 27 | #include "virt.h" |
5a8bcb67 | 28 | #include "fileio.h" |
1346b1f0 | 29 | #include "bus-util.h" |
5a8bcb67 | 30 | #include "udev-util.h" |
c6f7c917 | 31 | #include "network-internal.h" |
5a8bcb67 LP |
32 | #include "networkd-link.h" |
33 | #include "networkd-netdev.h" | |
fe8db0c5 | 34 | |
a97dcc12 | 35 | bool link_dhcp6_enabled(Link *link) { |
78c958f8 TG |
36 | if (link->flags & IFF_LOOPBACK) |
37 | return false; | |
38 | ||
39 | if (!link->network) | |
40 | return false; | |
41 | ||
cb9fc36a | 42 | return IN_SET(link->network->dhcp, ADDRESS_FAMILY_IPV6, ADDRESS_FAMILY_YES); |
78c958f8 TG |
43 | } |
44 | ||
a97dcc12 | 45 | bool link_dhcp4_enabled(Link *link) { |
78c958f8 TG |
46 | if (link->flags & IFF_LOOPBACK) |
47 | return false; | |
48 | ||
49 | if (!link->network) | |
50 | return false; | |
51 | ||
cb9fc36a | 52 | return IN_SET(link->network->dhcp, ADDRESS_FAMILY_IPV4, ADDRESS_FAMILY_YES); |
78c958f8 TG |
53 | } |
54 | ||
a97dcc12 | 55 | bool link_dhcp4_server_enabled(Link *link) { |
78c958f8 TG |
56 | if (link->flags & IFF_LOOPBACK) |
57 | return false; | |
58 | ||
59 | if (!link->network) | |
60 | return false; | |
61 | ||
62 | return link->network->dhcp_server; | |
63 | } | |
64 | ||
a97dcc12 | 65 | bool link_ipv4ll_enabled(Link *link) { |
78c958f8 TG |
66 | if (link->flags & IFF_LOOPBACK) |
67 | return false; | |
68 | ||
69 | if (!link->network) | |
70 | return false; | |
71 | ||
d0d6a4cd TG |
72 | return IN_SET(link->network->link_local, ADDRESS_FAMILY_IPV4, ADDRESS_FAMILY_YES); |
73 | } | |
74 | ||
75 | bool link_ipv6ll_enabled(Link *link) { | |
76 | if (link->flags & IFF_LOOPBACK) | |
77 | return false; | |
78 | ||
79 | if (!link->network) | |
80 | return false; | |
81 | ||
82 | return IN_SET(link->network->link_local, ADDRESS_FAMILY_IPV6, ADDRESS_FAMILY_YES); | |
78c958f8 TG |
83 | } |
84 | ||
a97dcc12 | 85 | bool link_lldp_enabled(Link *link) { |
ce43e484 SS |
86 | if (link->flags & IFF_LOOPBACK) |
87 | return false; | |
88 | ||
89 | if (!link->network) | |
90 | return false; | |
91 | ||
5a8bcb67 | 92 | if (link->network->bridge) |
ce43e484 SS |
93 | return false; |
94 | ||
95 | return link->network->lldp; | |
96 | } | |
97 | ||
769d324c | 98 | static bool link_ipv4_forward_enabled(Link *link) { |
5a8bcb67 LP |
99 | if (link->flags & IFF_LOOPBACK) |
100 | return false; | |
101 | ||
102 | if (!link->network) | |
103 | return false; | |
104 | ||
769d324c LP |
105 | return IN_SET(link->network->ip_forward, ADDRESS_FAMILY_IPV4, ADDRESS_FAMILY_YES); |
106 | } | |
107 | ||
108 | static bool link_ipv6_forward_enabled(Link *link) { | |
109 | if (link->flags & IFF_LOOPBACK) | |
110 | return false; | |
111 | ||
112 | if (!link->network) | |
113 | return false; | |
114 | ||
115 | return IN_SET(link->network->ip_forward, ADDRESS_FAMILY_IPV6, ADDRESS_FAMILY_YES); | |
5a8bcb67 LP |
116 | } |
117 | ||
51d18171 TG |
118 | #define FLAG_STRING(string, flag, old, new) \ |
119 | (((old ^ new) & flag) \ | |
120 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
121 | : "") | |
122 | ||
123 | static int link_update_flags(Link *link, sd_rtnl_message *m) { | |
124 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; | |
125 | uint8_t operstate; | |
126 | int r; | |
127 | ||
128 | assert(link); | |
129 | ||
130 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
131 | if (r < 0) |
132 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 TG |
133 | |
134 | r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate); | |
135 | if (r < 0) | |
136 | /* if we got a message without operstate, take it to mean | |
137 | the state was unchanged */ | |
138 | operstate = link->kernel_operstate; | |
139 | ||
140 | if ((link->flags == flags) && (link->kernel_operstate == operstate)) | |
141 | return 0; | |
142 | ||
143 | if (link->flags != flags) { | |
6a7a4e4d | 144 | 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 |
145 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
146 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
147 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
148 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
149 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
150 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
151 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
152 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
153 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
154 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
155 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
156 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
157 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
158 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
159 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
160 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
161 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
162 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
163 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
164 | ||
165 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
166 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
167 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
168 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
169 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
170 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
171 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
172 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
173 | ||
174 | /* link flags are currently at most 18 bits, let's align to | |
175 | * printing 20 */ | |
176 | if (unknown_flags_added) | |
79008bdd | 177 | log_link_debug(link, |
6a7a4e4d | 178 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
179 | unknown_flags_added); |
180 | ||
181 | if (unknown_flags_removed) | |
79008bdd | 182 | log_link_debug(link, |
6a7a4e4d | 183 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
184 | unknown_flags_removed); |
185 | } | |
186 | ||
187 | link->flags = flags; | |
188 | link->kernel_operstate = operstate; | |
189 | ||
190 | link_save(link); | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
505f8da7 | 195 | static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) { |
14b746f7 | 196 | _cleanup_link_unref_ Link *link = NULL; |
505f8da7 | 197 | uint16_t type; |
ca4e095a | 198 | const char *ifname; |
505f8da7 | 199 | int r, ifindex; |
f579559b | 200 | |
0c2f9b84 | 201 | assert(manager); |
505f8da7 | 202 | assert(message); |
f579559b TG |
203 | assert(ret); |
204 | ||
505f8da7 TG |
205 | r = sd_rtnl_message_get_type(message, &type); |
206 | if (r < 0) | |
207 | return r; | |
208 | else if (type != RTM_NEWLINK) | |
209 | return -EINVAL; | |
210 | ||
211 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
212 | if (r < 0) | |
213 | return r; | |
214 | else if (ifindex <= 0) | |
215 | return -EINVAL; | |
216 | ||
217 | r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &ifname); | |
218 | if (r < 0) | |
219 | return r; | |
220 | ||
f579559b TG |
221 | link = new0(Link, 1); |
222 | if (!link) | |
223 | return -ENOMEM; | |
224 | ||
14b746f7 | 225 | link->n_ref = 1; |
5a3eb5a7 | 226 | link->manager = manager; |
8434fd5c | 227 | link->state = LINK_STATE_PENDING; |
505f8da7 TG |
228 | link->ifindex = ifindex; |
229 | link->ifname = strdup(ifname); | |
230 | if (!link->ifname) | |
231 | return -ENOMEM; | |
f579559b | 232 | |
512922f8 TG |
233 | r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
234 | if (r < 0) | |
79008bdd | 235 | log_link_debug(link, "MAC address not found for new device, continuing without"); |
512922f8 | 236 | |
3c9b8860 TG |
237 | r = asprintf(&link->state_file, "/run/systemd/netif/links/%d", |
238 | link->ifindex); | |
fe8db0c5 | 239 | if (r < 0) |
315db1a8 | 240 | return -ENOMEM; |
fe8db0c5 | 241 | |
3c9b8860 TG |
242 | r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", |
243 | link->ifindex); | |
68a8723c TG |
244 | if (r < 0) |
245 | return -ENOMEM; | |
246 | ||
49699bac SS |
247 | r = asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", |
248 | link->ifindex); | |
249 | if (r < 0) | |
250 | return -ENOMEM; | |
251 | ||
252 | ||
d5099efc | 253 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
254 | if (r < 0) |
255 | return r; | |
256 | ||
257 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
258 | if (r < 0) |
259 | return r; | |
260 | ||
51d18171 TG |
261 | r = link_update_flags(link, message); |
262 | if (r < 0) | |
263 | return r; | |
264 | ||
f579559b TG |
265 | *ret = link; |
266 | link = NULL; | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
14b746f7 | 271 | static void link_free(Link *link) { |
428fd0a7 | 272 | Address *address; |
0d4ad91d AR |
273 | Iterator i; |
274 | Link *carrier; | |
428fd0a7 | 275 | |
f579559b TG |
276 | if (!link) |
277 | return; | |
278 | ||
428fd0a7 TG |
279 | while ((address = link->addresses)) { |
280 | LIST_REMOVE(addresses, link->addresses, address); | |
281 | address_free(address); | |
282 | } | |
283 | ||
11bf3cce LP |
284 | while ((address = link->pool_addresses)) { |
285 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
286 | address_free(address); | |
287 | } | |
288 | ||
bfcdba8d | 289 | sd_dhcp_server_unref(link->dhcp_server); |
e5b04c8d | 290 | sd_dhcp_client_unref(link->dhcp_client); |
a6cc569e | 291 | sd_dhcp_lease_unref(link->dhcp_lease); |
f5be5601 | 292 | |
68a8723c TG |
293 | free(link->lease_file); |
294 | ||
07e10d1a TG |
295 | sd_lldp_free(link->lldp); |
296 | ||
49699bac SS |
297 | free(link->lldp_file); |
298 | ||
56cd007a | 299 | sd_ipv4ll_unref(link->ipv4ll); |
4138fb2c | 300 | sd_dhcp6_client_unref(link->dhcp6_client); |
6b66097b | 301 | sd_icmp6_nd_unref(link->icmp6_router_discovery); |
4138fb2c | 302 | |
28aeb07f | 303 | if (link->manager) |
ae06ab10 | 304 | hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)); |
f579559b | 305 | |
c166a070 | 306 | free(link->ifname); |
68a8723c | 307 | |
fe8db0c5 | 308 | free(link->state_file); |
c166a070 | 309 | |
b5db00e5 UTL |
310 | udev_device_unref(link->udev_device); |
311 | ||
0d4ad91d AR |
312 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) |
313 | hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex)); | |
314 | hashmap_free(link->bound_to_links); | |
315 | ||
316 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) | |
317 | hashmap_remove(link->bound_by_links, INT_TO_PTR(carrier->ifindex)); | |
318 | hashmap_free(link->bound_by_links); | |
319 | ||
f579559b TG |
320 | free(link); |
321 | } | |
322 | ||
14b746f7 TG |
323 | Link *link_unref(Link *link) { |
324 | if (link && (-- link->n_ref <= 0)) | |
325 | link_free(link); | |
326 | ||
327 | return NULL; | |
328 | } | |
329 | ||
330 | Link *link_ref(Link *link) { | |
331 | if (link) | |
332 | assert_se(++ link->n_ref >= 2); | |
333 | ||
334 | return link; | |
335 | } | |
336 | ||
11a7f229 TG |
337 | int link_get(Manager *m, int ifindex, Link **ret) { |
338 | Link *link; | |
11a7f229 TG |
339 | |
340 | assert(m); | |
11a7f229 TG |
341 | assert(ifindex); |
342 | assert(ret); | |
343 | ||
ae06ab10 | 344 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
345 | if (!link) |
346 | return -ENODEV; | |
347 | ||
348 | *ret = link; | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
e331e246 TG |
353 | static void link_set_state(Link *link, LinkState state) { |
354 | assert(link); | |
355 | ||
356 | if (link->state == state) | |
357 | return; | |
358 | ||
359 | link->state = state; | |
360 | ||
361 | link_send_changed(link, "AdministrativeState", NULL); | |
362 | ||
363 | return; | |
364 | } | |
365 | ||
57bd6899 TG |
366 | static void link_enter_unmanaged(Link *link) { |
367 | assert(link); | |
368 | ||
6a7a4e4d | 369 | log_link_debug(link, "Unmanaged"); |
57bd6899 | 370 | |
e331e246 | 371 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 TG |
372 | |
373 | link_save(link); | |
374 | } | |
375 | ||
111bb8f9 TG |
376 | static int link_stop_clients(Link *link) { |
377 | int r = 0, k; | |
378 | ||
379 | assert(link); | |
380 | assert(link->manager); | |
381 | assert(link->manager->event); | |
382 | ||
383 | if (!link->network) | |
384 | return 0; | |
385 | ||
ba179154 | 386 | if (link->dhcp_client) { |
111bb8f9 | 387 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d LP |
388 | if (k < 0) |
389 | r = log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"); | |
111bb8f9 TG |
390 | } |
391 | ||
ba179154 | 392 | if (link->ipv4ll) { |
111bb8f9 | 393 | k = sd_ipv4ll_stop(link->ipv4ll); |
6a7a4e4d LP |
394 | if (k < 0) |
395 | r = log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"); | |
dd43110f TG |
396 | } |
397 | ||
ba179154 | 398 | if(link->icmp6_router_discovery) { |
1873a3d3 PF |
399 | if (link->dhcp6_client) { |
400 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
6a7a4e4d LP |
401 | if (k < 0) |
402 | r = log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"); | |
1873a3d3 | 403 | } |
4138fb2c | 404 | |
1873a3d3 | 405 | k = sd_icmp6_nd_stop(link->icmp6_router_discovery); |
6a7a4e4d LP |
406 | if (k < 0) |
407 | r = log_link_warning_errno(link, r, "Could not stop ICMPv6 router discovery: %m"); | |
4138fb2c PF |
408 | } |
409 | ||
ce43e484 | 410 | if (link->lldp) { |
ce43e484 | 411 | k = sd_lldp_stop(link->lldp); |
6a7a4e4d LP |
412 | if (k < 0) |
413 | r = log_link_warning_errno(link, r, "Could not stop LLDP: %m"); | |
ce43e484 SS |
414 | } |
415 | ||
111bb8f9 TG |
416 | return r; |
417 | } | |
418 | ||
b22d8a00 | 419 | void link_enter_failed(Link *link) { |
ef1ba606 | 420 | assert(link); |
f882c247 | 421 | |
370e9930 | 422 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
423 | return; |
424 | ||
6a7a4e4d | 425 | log_link_warning(link, "Failed"); |
449f7554 | 426 | |
e331e246 | 427 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 428 | |
111bb8f9 TG |
429 | link_stop_clients(link); |
430 | ||
fe8db0c5 | 431 | link_save(link); |
f882c247 TG |
432 | } |
433 | ||
4f434938 LP |
434 | static Address* link_find_dhcp_server_address(Link *link) { |
435 | Address *address; | |
436 | ||
437 | assert(link); | |
438 | assert(link->network); | |
439 | ||
d4cdbea5 | 440 | /* The first statically configured address if there is any */ |
4f434938 LP |
441 | LIST_FOREACH(addresses, address, link->network->static_addresses) { |
442 | ||
443 | if (address->family != AF_INET) | |
444 | continue; | |
445 | ||
af93291c | 446 | if (in_addr_is_null(address->family, &address->in_addr)) |
4f434938 LP |
447 | continue; |
448 | ||
449 | return address; | |
450 | } | |
451 | ||
452 | /* If that didn't work, find a suitable address we got from the pool */ | |
453 | LIST_FOREACH(addresses, address, link->pool_addresses) { | |
454 | if (address->family != AF_INET) | |
455 | continue; | |
456 | ||
457 | return address; | |
458 | } | |
459 | ||
460 | return NULL; | |
461 | } | |
462 | ||
dd43110f | 463 | static int link_enter_configured(Link *link) { |
dd43110f TG |
464 | assert(link); |
465 | assert(link->network); | |
466 | assert(link->state == LINK_STATE_SETTING_ROUTES); | |
467 | ||
6a7a4e4d | 468 | log_link_info(link, "Configured"); |
dd43110f | 469 | |
e331e246 | 470 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f TG |
471 | |
472 | link_save(link); | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
3c9b8860 TG |
477 | void link_client_handler(Link *link) { |
478 | assert(link); | |
479 | assert(link->network); | |
480 | ||
481 | if (!link->static_configured) | |
482 | return; | |
483 | ||
78c958f8 | 484 | if (link_ipv4ll_enabled(link)) |
3c9b8860 TG |
485 | if (!link->ipv4ll_address || |
486 | !link->ipv4ll_route) | |
487 | return; | |
488 | ||
78c958f8 | 489 | if (link_dhcp4_enabled(link) && !link->dhcp4_configured) |
3c9b8860 TG |
490 | return; |
491 | ||
9fdaa992 TG |
492 | if (link->state != LINK_STATE_CONFIGURED) |
493 | link_enter_configured(link); | |
3c9b8860 TG |
494 | |
495 | return; | |
496 | } | |
497 | ||
f882c247 | 498 | static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 499 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
500 | int r; |
501 | ||
3c9b8860 | 502 | assert(link->link_messages > 0); |
370e9930 TG |
503 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
504 | LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED, | |
505 | LINK_STATE_LINGER)); | |
f882c247 | 506 | |
3c9b8860 | 507 | link->link_messages --; |
f882c247 | 508 | |
77a008c0 | 509 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
510 | return 1; |
511 | ||
512 | r = sd_rtnl_message_get_errno(m); | |
c166a070 | 513 | if (r < 0 && r != -EEXIST) |
6a7a4e4d | 514 | log_link_warning_errno(link, r, "%-*s: could not set route: %m", IFNAMSIZ, link->ifname); |
f882c247 | 515 | |
3c9b8860 | 516 | if (link->link_messages == 0) { |
6a7a4e4d | 517 | log_link_debug(link, "Routes set"); |
3c9b8860 TG |
518 | link->static_configured = true; |
519 | link_client_handler(link); | |
dd3efc09 | 520 | } |
f882c247 TG |
521 | |
522 | return 1; | |
523 | } | |
524 | ||
525 | static int link_enter_set_routes(Link *link) { | |
a6cc569e | 526 | Route *rt; |
f882c247 TG |
527 | int r; |
528 | ||
529 | assert(link); | |
530 | assert(link->network); | |
ef1ba606 | 531 | assert(link->state == LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 532 | |
e331e246 | 533 | link_set_state(link, LINK_STATE_SETTING_ROUTES); |
f882c247 | 534 | |
3d3d4255 | 535 | LIST_FOREACH(routes, rt, link->network->static_routes) { |
a6cc569e | 536 | r = route_configure(rt, link, &route_handler); |
dd3efc09 | 537 | if (r < 0) { |
6a7a4e4d | 538 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 | 539 | link_enter_failed(link); |
a6cc569e TG |
540 | return r; |
541 | } | |
542 | ||
3c9b8860 | 543 | link->link_messages ++; |
8ddbeaa2 | 544 | } |
f5be5601 | 545 | |
3c9b8860 TG |
546 | if (link->link_messages == 0) { |
547 | link->static_configured = true; | |
548 | link_client_handler(link); | |
431ca2ce | 549 | } else |
6a7a4e4d | 550 | log_link_debug(link, "Setting routes"); |
f882c247 TG |
551 | |
552 | return 0; | |
553 | } | |
554 | ||
b22d8a00 | 555 | int link_route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 556 | _cleanup_link_unref_ Link *link = userdata; |
5c1d3fc9 UTL |
557 | int r; |
558 | ||
559 | assert(m); | |
560 | assert(link); | |
561 | assert(link->ifname); | |
562 | ||
5da8149f | 563 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
5c1d3fc9 UTL |
564 | return 1; |
565 | ||
566 | r = sd_rtnl_message_get_errno(m); | |
b90b025a | 567 | if (r < 0 && r != -ESRCH) |
6a7a4e4d | 568 | log_link_warning_errno(link, r, "%-*s: could not drop route: %m", IFNAMSIZ, link->ifname); |
5c1d3fc9 | 569 | |
5bdd314c | 570 | return 1; |
5c1d3fc9 UTL |
571 | } |
572 | ||
f882c247 | 573 | static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 574 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
575 | int r; |
576 | ||
4958aee4 | 577 | assert(rtnl); |
f5be5601 TG |
578 | assert(m); |
579 | assert(link); | |
580 | assert(link->ifname); | |
3c9b8860 | 581 | assert(link->link_messages > 0); |
370e9930 TG |
582 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
583 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 584 | |
3c9b8860 | 585 | link->link_messages --; |
f882c247 | 586 | |
5da8149f | 587 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
588 | return 1; |
589 | ||
590 | r = sd_rtnl_message_get_errno(m); | |
c166a070 | 591 | if (r < 0 && r != -EEXIST) |
6a7a4e4d | 592 | log_link_warning_errno(link, r, "%-*s: could not set address: %m", IFNAMSIZ, link->ifname); |
45af44d4 TG |
593 | else if (r >= 0) |
594 | link_rtnl_process_address(rtnl, m, link->manager); | |
f882c247 | 595 | |
3c9b8860 | 596 | if (link->link_messages == 0) { |
6a7a4e4d | 597 | log_link_debug(link, "Addresses set"); |
ef1ba606 | 598 | link_enter_set_routes(link); |
dd3efc09 | 599 | } |
f882c247 TG |
600 | |
601 | return 1; | |
602 | } | |
603 | ||
604 | static int link_enter_set_addresses(Link *link) { | |
a6cc569e | 605 | Address *ad; |
f882c247 TG |
606 | int r; |
607 | ||
608 | assert(link); | |
609 | assert(link->network); | |
f5be5601 | 610 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 611 | |
e331e246 | 612 | link_set_state(link, LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 613 | |
3d3d4255 | 614 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
a6cc569e | 615 | r = address_configure(ad, link, &address_handler); |
dd3efc09 | 616 | if (r < 0) { |
5a8bcb67 | 617 | log_link_warning_errno(link, r, "Could not set addresses: %m"); |
f5be5601 TG |
618 | link_enter_failed(link); |
619 | return r; | |
620 | } | |
621 | ||
3c9b8860 | 622 | link->link_messages ++; |
f882c247 TG |
623 | } |
624 | ||
d4cdbea5 TG |
625 | /* now that we can figure out a default address for the dhcp server, |
626 | start it */ | |
627 | if (link_dhcp4_server_enabled(link)) { | |
628 | struct in_addr pool_start; | |
629 | Address *address; | |
630 | ||
631 | address = link_find_dhcp_server_address(link); | |
632 | if (!address) { | |
6a7a4e4d | 633 | log_link_warning(link, "Failed to find suitable address for DHCPv4 server instance."); |
d4cdbea5 TG |
634 | link_enter_failed(link); |
635 | return 0; | |
636 | } | |
637 | ||
638 | r = sd_dhcp_server_set_address(link->dhcp_server, | |
639 | &address->in_addr.in, | |
640 | address->prefixlen); | |
641 | if (r < 0) | |
642 | return r; | |
643 | ||
644 | /* offer 32 addresses starting from the address following the server address */ | |
645 | pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1); | |
646 | r = sd_dhcp_server_set_lease_pool(link->dhcp_server, | |
647 | &pool_start, 32); | |
648 | if (r < 0) | |
649 | return r; | |
650 | ||
651 | /* TODO: | |
652 | r = sd_dhcp_server_set_router(link->dhcp_server, | |
653 | &main_address->in_addr.in); | |
654 | if (r < 0) | |
655 | return r; | |
656 | ||
657 | r = sd_dhcp_server_set_prefixlen(link->dhcp_server, | |
658 | main_address->prefixlen); | |
659 | if (r < 0) | |
660 | return r; | |
661 | */ | |
662 | ||
663 | r = sd_dhcp_server_start(link->dhcp_server); | |
664 | if (r < 0) { | |
6a7a4e4d | 665 | log_link_warning_errno(link, r, "Could not start DHCPv4 server instance: %m"); |
d4cdbea5 TG |
666 | |
667 | link_enter_failed(link); | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
6a7a4e4d | 672 | log_link_debug(link, "Offering DHCPv4 leases"); |
d4cdbea5 TG |
673 | } |
674 | ||
6a7a4e4d | 675 | if (link->link_messages == 0) |
431ca2ce | 676 | link_enter_set_routes(link); |
6a7a4e4d LP |
677 | else |
678 | log_link_debug(link, "Setting addresses"); | |
431ca2ce | 679 | |
f882c247 TG |
680 | return 0; |
681 | } | |
682 | ||
b22d8a00 | 683 | int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 684 | _cleanup_link_unref_ Link *link = userdata; |
ff254138 TG |
685 | int r; |
686 | ||
687 | assert(m); | |
688 | assert(link); | |
689 | assert(link->ifname); | |
690 | ||
5da8149f | 691 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ff254138 TG |
692 | return 1; |
693 | ||
694 | r = sd_rtnl_message_get_errno(m); | |
b90b025a | 695 | if (r < 0 && r != -EADDRNOTAVAIL) |
6a7a4e4d | 696 | log_link_warning_errno(link, r, "%-*s: could not drop address: %m", IFNAMSIZ, link->ifname); |
ff254138 | 697 | |
5bdd314c | 698 | return 1; |
ff254138 TG |
699 | } |
700 | ||
a245ced0 | 701 | static int link_set_bridge_fdb(Link *const link) { |
b98b483b AR |
702 | FdbEntry *fdb_entry; |
703 | int r = 0; | |
704 | ||
705 | LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { | |
ea6ec096 | 706 | r = fdb_entry_configure(link, fdb_entry); |
b98b483b | 707 | if(r < 0) { |
6a7a4e4d | 708 | log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m"); |
b98b483b AR |
709 | break; |
710 | } | |
711 | } | |
712 | ||
713 | return r; | |
714 | } | |
715 | ||
e1853b00 SS |
716 | static int link_set_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
717 | _cleanup_link_unref_ Link *link = userdata; | |
718 | int r; | |
719 | ||
6a7a4e4d | 720 | log_link_debug(link, "Set link"); |
e1853b00 SS |
721 | |
722 | r = sd_rtnl_message_get_errno(m); | |
723 | if (r < 0 && r != -EEXIST) { | |
00e8d83a | 724 | log_link_struct(link, LOG_ERR, |
e1853b00 | 725 | "MESSAGE=%-*s: could not join netdev: %s", |
6a7a4e4d LP |
726 | IFNAMSIZ, link->ifname, |
727 | strerror(-r), | |
e1853b00 SS |
728 | "ERRNO=%d", -r, |
729 | NULL); | |
730 | link_enter_failed(link); | |
731 | return 1; | |
732 | } | |
733 | ||
734 | return 0; | |
735 | } | |
736 | ||
19070062 | 737 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
5da8149f | 738 | _cleanup_link_unref_ Link *link = userdata; |
1346b1f0 TG |
739 | int r; |
740 | ||
19070062 | 741 | assert(m); |
b226d99b TG |
742 | assert(link); |
743 | ||
5da8149f | 744 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
370e9930 | 745 | return 1; |
370e9930 | 746 | |
1346b1f0 | 747 | r = sd_bus_message_get_errno(m); |
70b34f5d | 748 | if (r > 0) |
6a7a4e4d | 749 | log_link_warning_errno(link, r, "Could not set hostname: %m"); |
1346b1f0 TG |
750 | |
751 | return 1; | |
752 | } | |
753 | ||
3c9b8860 | 754 | int link_set_hostname(Link *link, const char *hostname) { |
1346b1f0 TG |
755 | _cleanup_bus_message_unref_ sd_bus_message *m = NULL; |
756 | int r = 0; | |
757 | ||
b226d99b TG |
758 | assert(link); |
759 | assert(link->manager); | |
1346b1f0 TG |
760 | assert(hostname); |
761 | ||
79008bdd | 762 | log_link_debug(link, "Setting transient hostname: '%s'", hostname); |
1346b1f0 | 763 | |
3c9b8860 TG |
764 | if (!link->manager->bus) { |
765 | /* TODO: replace by assert when we can rely on kdbus */ | |
6a7a4e4d | 766 | log_link_info(link, "Not connected to system bus, ignoring transient hostname."); |
bcbca829 TG |
767 | return 0; |
768 | } | |
769 | ||
1346b1f0 | 770 | r = sd_bus_message_new_method_call( |
b226d99b | 771 | link->manager->bus, |
151b9b96 | 772 | &m, |
1346b1f0 TG |
773 | "org.freedesktop.hostname1", |
774 | "/org/freedesktop/hostname1", | |
775 | "org.freedesktop.hostname1", | |
151b9b96 | 776 | "SetHostname"); |
1346b1f0 TG |
777 | if (r < 0) |
778 | return r; | |
779 | ||
780 | r = sd_bus_message_append(m, "sb", hostname, false); | |
781 | if (r < 0) | |
782 | return r; | |
783 | ||
3c9b8860 TG |
784 | r = sd_bus_call_async(link->manager->bus, NULL, m, set_hostname_handler, |
785 | link, 0); | |
6a7a4e4d LP |
786 | if (r < 0) |
787 | return log_link_error_errno(link, r, "Could not set transient hostname: %m"); | |
b226d99b TG |
788 | |
789 | link_ref(link); | |
1346b1f0 | 790 | |
5da8149f | 791 | return 0; |
1346b1f0 TG |
792 | } |
793 | ||
4f882b2a | 794 | static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 795 | _cleanup_link_unref_ Link *link = userdata; |
4f882b2a TG |
796 | int r; |
797 | ||
798 | assert(m); | |
799 | assert(link); | |
800 | assert(link->ifname); | |
801 | ||
5da8149f | 802 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
803 | return 1; |
804 | ||
805 | r = sd_rtnl_message_get_errno(m); | |
c9ccc19f | 806 | if (r < 0) |
6a7a4e4d | 807 | log_link_warning_errno(link, r, "%-*s: could not set MTU: %m", IFNAMSIZ, link->ifname); |
4f882b2a TG |
808 | |
809 | return 1; | |
810 | } | |
811 | ||
3c9b8860 | 812 | int link_set_mtu(Link *link, uint32_t mtu) { |
cf6a8911 | 813 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
4f882b2a TG |
814 | int r; |
815 | ||
816 | assert(link); | |
817 | assert(link->manager); | |
818 | assert(link->manager->rtnl); | |
819 | ||
6a7a4e4d | 820 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 821 | |
6a7a4e4d LP |
822 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
823 | if (r < 0) | |
824 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a TG |
825 | |
826 | r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu); | |
6a7a4e4d LP |
827 | if (r < 0) |
828 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 829 | |
6a7a4e4d LP |
830 | r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL); |
831 | if (r < 0) | |
832 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 833 | |
ae941762 | 834 | link_ref(link); |
b226d99b | 835 | |
4f882b2a TG |
836 | return 0; |
837 | } | |
838 | ||
e1853b00 SS |
839 | static int link_set_bridge(Link *link) { |
840 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
841 | int r; | |
842 | ||
843 | assert(link); | |
844 | assert(link->network); | |
845 | ||
846 | if(link->network->cost == 0) | |
847 | return 0; | |
848 | ||
6a7a4e4d LP |
849 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
850 | if (r < 0) | |
851 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
852 | |
853 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
854 | if (r < 0) |
855 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 SS |
856 | |
857 | r = sd_rtnl_message_open_container(req, IFLA_PROTINFO); | |
6a7a4e4d LP |
858 | if (r < 0) |
859 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 SS |
860 | |
861 | if(link->network->cost != 0) { | |
862 | r = sd_rtnl_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); | |
6a7a4e4d LP |
863 | if (r < 0) |
864 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 SS |
865 | } |
866 | ||
867 | r = sd_rtnl_message_close_container(req); | |
6a7a4e4d LP |
868 | if (r < 0) |
869 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 SS |
870 | |
871 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL); | |
6a7a4e4d LP |
872 | if (r < 0) |
873 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
874 | |
875 | link_ref(link); | |
876 | ||
877 | return r; | |
878 | } | |
879 | ||
49699bac SS |
880 | static void lldp_handler(sd_lldp *lldp, int event, void *userdata) { |
881 | Link *link = userdata; | |
882 | int r; | |
883 | ||
884 | assert(link); | |
885 | assert(link->network); | |
886 | assert(link->manager); | |
887 | ||
888 | if (event != UPDATE_INFO) | |
889 | return; | |
890 | ||
891 | r = sd_lldp_save(link->lldp, link->lldp_file); | |
892 | if (r < 0) | |
6a7a4e4d | 893 | log_link_warning_errno(link, r, "Could not save LLDP: %m"); |
49699bac SS |
894 | |
895 | } | |
896 | ||
ff254138 TG |
897 | static int link_acquire_conf(Link *link) { |
898 | int r; | |
899 | ||
900 | assert(link); | |
901 | assert(link->network); | |
ff254138 TG |
902 | assert(link->manager); |
903 | assert(link->manager->event); | |
904 | ||
78c958f8 | 905 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 906 | assert(link->ipv4ll); |
ff254138 | 907 | |
6a7a4e4d | 908 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
909 | |
910 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
911 | if (r < 0) |
912 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
913 | } |
914 | ||
78c958f8 | 915 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 916 | assert(link->dhcp_client); |
ff254138 | 917 | |
6a7a4e4d | 918 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 919 | |
5c1d3fc9 | 920 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
921 | if (r < 0) |
922 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 923 | } |
ff254138 | 924 | |
78c958f8 | 925 | if (link_dhcp6_enabled(link)) { |
4138fb2c PF |
926 | assert(link->icmp6_router_discovery); |
927 | ||
6a7a4e4d | 928 | log_link_debug(link, "Discovering IPv6 routers"); |
4138fb2c PF |
929 | |
930 | r = sd_icmp6_router_solicitation_start(link->icmp6_router_discovery); | |
6a7a4e4d LP |
931 | if (r < 0) |
932 | return log_link_warning_errno(link, r, "Could not start IPv6 router discovery: %m"); | |
4138fb2c PF |
933 | } |
934 | ||
ce43e484 SS |
935 | if (link_lldp_enabled(link)) { |
936 | assert(link->lldp); | |
937 | ||
938 | log_link_debug(link, "Starting LLDP"); | |
939 | ||
940 | r = sd_lldp_start(link->lldp); | |
6a7a4e4d LP |
941 | if (r < 0) |
942 | return log_link_warning_errno(link, r, "Could not start LLDP: %m"); | |
ce43e484 SS |
943 | } |
944 | ||
ff254138 TG |
945 | return 0; |
946 | } | |
947 | ||
a61bb41c | 948 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
949 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
950 | ||
a61bb41c | 951 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
952 | return true; |
953 | ||
a61bb41c | 954 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 955 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 956 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
957 | return true; |
958 | ||
959 | return false; | |
960 | } | |
961 | ||
dd3efc09 | 962 | static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 963 | _cleanup_link_unref_ Link *link = userdata; |
dd3efc09 TG |
964 | int r; |
965 | ||
1746cf2a TG |
966 | assert(link); |
967 | ||
5da8149f | 968 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
969 | return 1; |
970 | ||
dd3efc09 | 971 | r = sd_rtnl_message_get_errno(m); |
6a7a4e4d LP |
972 | if (r < 0) |
973 | /* we warn but don't fail the link, as it may be | |
974 | brought up later */ | |
975 | log_link_warning_errno(link, r, "%-*s: could not bring up interface: %m", IFNAMSIZ, link->ifname); | |
45ad2c13 | 976 | |
f882c247 TG |
977 | return 1; |
978 | } | |
979 | ||
980 | static int link_up(Link *link) { | |
cf6a8911 | 981 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
5c3072ea | 982 | uint8_t ipv6ll_mode; |
f579559b TG |
983 | int r; |
984 | ||
f882c247 | 985 | assert(link); |
c106cc36 | 986 | assert(link->network); |
f882c247 TG |
987 | assert(link->manager); |
988 | assert(link->manager->rtnl); | |
989 | ||
6a7a4e4d | 990 | log_link_debug(link, "Bringing link up"); |
449f7554 | 991 | |
6a7a4e4d LP |
992 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
993 | if (r < 0) | |
994 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 995 | |
5d4795f3 | 996 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
997 | if (r < 0) |
998 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 999 | |
c106cc36 TG |
1000 | if (link->network->mac) { |
1001 | r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); | |
6a7a4e4d LP |
1002 | if (r < 0) |
1003 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1004 | } |
1005 | ||
1006 | if (link->network->mtu) { | |
1007 | r = sd_rtnl_message_append_u32(req, IFLA_MTU, link->network->mtu); | |
6a7a4e4d LP |
1008 | if (r < 0) |
1009 | return log_link_error_errno(link, r, "Could not set MTU: %m"); | |
c106cc36 TG |
1010 | } |
1011 | ||
7f77697a | 1012 | r = sd_rtnl_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1013 | if (r < 0) |
1014 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1015 | |
7f77697a | 1016 | r = sd_rtnl_message_open_container(req, AF_INET6); |
6a7a4e4d LP |
1017 | if (r < 0) |
1018 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1019 | |
5c3072ea TG |
1020 | ipv6ll_mode = link_ipv6ll_enabled(link) ? IN6_ADDR_GEN_MODE_EUI64 : IN6_ADDR_GEN_MODE_NONE; |
1021 | r = sd_rtnl_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); | |
6a7a4e4d LP |
1022 | if (r < 0) |
1023 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1024 | |
7f77697a TG |
1025 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1026 | r = sd_rtnl_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); | |
6a7a4e4d LP |
1027 | if (r < 0) |
1028 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
7f77697a | 1029 | } |
d0d6a4cd | 1030 | |
7f77697a | 1031 | r = sd_rtnl_message_close_container(req); |
6a7a4e4d LP |
1032 | if (r < 0) |
1033 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); | |
7f77697a TG |
1034 | |
1035 | r = sd_rtnl_message_close_container(req); | |
6a7a4e4d LP |
1036 | if (r < 0) |
1037 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1038 | |
6a7a4e4d LP |
1039 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); |
1040 | if (r < 0) | |
1041 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1042 | |
b226d99b TG |
1043 | link_ref(link); |
1044 | ||
f882c247 TG |
1045 | return 0; |
1046 | } | |
1047 | ||
0d4ad91d AR |
1048 | static int link_down_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
1049 | _cleanup_link_unref_ Link *link = userdata; | |
1050 | int r; | |
1051 | ||
1052 | assert(link); | |
1053 | ||
1054 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1055 | return 1; | |
1056 | ||
1057 | r = sd_rtnl_message_get_errno(m); | |
1058 | if (r < 0) | |
6a7a4e4d | 1059 | log_link_warning_errno(link, r, "%-*s: could not bring down interface: %m", IFNAMSIZ, link->ifname); |
0d4ad91d AR |
1060 | |
1061 | return 1; | |
1062 | } | |
1063 | ||
1064 | static int link_down(Link *link) { | |
1065 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
1066 | int r; | |
1067 | ||
1068 | assert(link); | |
1069 | assert(link->manager); | |
1070 | assert(link->manager->rtnl); | |
1071 | ||
6a7a4e4d | 1072 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1073 | |
1074 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1075 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1076 | if (r < 0) |
1077 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1078 | |
1079 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1080 | if (r < 0) |
1081 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1082 | |
6a7a4e4d LP |
1083 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_down_handler, link, 0, NULL); |
1084 | if (r < 0) | |
1085 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1086 | |
1087 | link_ref(link); | |
1088 | ||
1089 | return 0; | |
1090 | } | |
1091 | ||
1092 | static int link_handle_bound_to_list(Link *link) { | |
1093 | Link *l; | |
1094 | Iterator i; | |
1095 | int r; | |
1096 | bool required_up = false; | |
1097 | bool link_is_up = false; | |
1098 | ||
1099 | assert(link); | |
1100 | ||
1101 | if (hashmap_isempty(link->bound_to_links)) | |
1102 | return 0; | |
1103 | ||
1104 | if (link->flags & IFF_UP) | |
1105 | link_is_up = true; | |
1106 | ||
1107 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1108 | if (link_has_carrier(l)) { | |
1109 | required_up = true; | |
1110 | break; | |
1111 | } | |
1112 | ||
1113 | if (!required_up && link_is_up) { | |
1114 | r = link_down(link); | |
1115 | if (r < 0) | |
1116 | return r; | |
1117 | } else if (required_up && !link_is_up) { | |
1118 | r = link_up(link); | |
1119 | if (r < 0) | |
1120 | return r; | |
1121 | } | |
1122 | ||
1123 | return 0; | |
1124 | } | |
1125 | ||
1126 | static int link_handle_bound_by_list(Link *link) { | |
1127 | Iterator i; | |
1128 | Link *l; | |
1129 | int r; | |
1130 | ||
1131 | assert(link); | |
1132 | ||
1133 | if (hashmap_isempty(link->bound_by_links)) | |
1134 | return 0; | |
1135 | ||
1136 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1137 | r = link_handle_bound_to_list(l); | |
1138 | if (r < 0) | |
1139 | return r; | |
1140 | } | |
1141 | ||
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1146 | int r; | |
1147 | ||
1148 | assert(link); | |
1149 | assert(carrier); | |
1150 | ||
1151 | if (link == carrier) | |
1152 | return 0; | |
1153 | ||
1154 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1155 | return 0; | |
1156 | ||
1157 | r = hashmap_ensure_allocated(h, NULL); | |
1158 | if (r < 0) | |
1159 | return r; | |
1160 | ||
1161 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1162 | if (r < 0) | |
1163 | return r; | |
1164 | ||
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static int link_new_bound_by_list(Link *link) { | |
1169 | Manager *m; | |
1170 | Link *carrier; | |
1171 | Iterator i; | |
1172 | int r; | |
1173 | bool list_updated = false; | |
1174 | ||
1175 | assert(link); | |
1176 | assert(link->manager); | |
1177 | ||
1178 | m = link->manager; | |
1179 | ||
1180 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1181 | if (!carrier->network) | |
1182 | continue; | |
1183 | ||
1184 | if (strv_isempty(carrier->network->bind_carrier)) | |
1185 | continue; | |
1186 | ||
1187 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
1188 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
1189 | if (r < 0) | |
1190 | return r; | |
1191 | ||
1192 | list_updated = true; | |
1193 | } | |
1194 | } | |
1195 | ||
1196 | if (list_updated) | |
1197 | link_save(link); | |
1198 | ||
1199 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) { | |
1200 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); | |
1201 | if (r < 0) | |
1202 | return r; | |
1203 | ||
1204 | link_save(carrier); | |
1205 | } | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
1210 | static int link_new_bound_to_list(Link *link) { | |
1211 | Manager *m; | |
1212 | Link *carrier; | |
1213 | Iterator i; | |
1214 | int r; | |
1215 | bool list_updated = false; | |
1216 | ||
1217 | assert(link); | |
1218 | assert(link->manager); | |
1219 | ||
1220 | if (!link->network) | |
1221 | return 0; | |
1222 | ||
1223 | if (strv_isempty(link->network->bind_carrier)) | |
1224 | return 0; | |
1225 | ||
1226 | m = link->manager; | |
1227 | ||
1228 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1229 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
1230 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
1231 | if (r < 0) | |
1232 | return r; | |
1233 | ||
1234 | list_updated = true; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | if (list_updated) | |
1239 | link_save(link); | |
1240 | ||
1241 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
1242 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
1243 | if (r < 0) | |
1244 | return r; | |
1245 | ||
1246 | link_save(carrier); | |
1247 | } | |
1248 | ||
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | static int link_new_carrier_maps(Link *link) { | |
1253 | int r; | |
1254 | ||
1255 | r = link_new_bound_by_list(link); | |
1256 | if (r < 0) | |
1257 | return r; | |
1258 | ||
1259 | r = link_handle_bound_by_list(link); | |
1260 | if (r < 0) | |
1261 | return r; | |
1262 | ||
1263 | r = link_new_bound_to_list(link); | |
1264 | if (r < 0) | |
1265 | return r; | |
1266 | ||
1267 | r = link_handle_bound_to_list(link); | |
1268 | if (r < 0) | |
1269 | return r; | |
1270 | ||
1271 | return 0; | |
1272 | } | |
1273 | ||
1274 | static void link_free_bound_to_list(Link *link) { | |
1275 | Link *bound_to; | |
1276 | Iterator i; | |
1277 | ||
1278 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
1279 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
1280 | ||
1281 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
1282 | link_save(bound_to); | |
1283 | } | |
1284 | ||
1285 | return; | |
1286 | } | |
1287 | ||
1288 | static void link_free_bound_by_list(Link *link) { | |
1289 | Link *bound_by; | |
1290 | Iterator i; | |
1291 | ||
1292 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
1293 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
1294 | ||
1295 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
1296 | link_save(bound_by); | |
1297 | link_handle_bound_to_list(bound_by); | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | return; | |
1302 | } | |
1303 | ||
1304 | static void link_free_carrier_maps(Link *link) { | |
1305 | bool list_updated = false; | |
1306 | ||
1307 | assert(link); | |
1308 | ||
1309 | if (!hashmap_isempty(link->bound_to_links)) { | |
1310 | link_free_bound_to_list(link); | |
1311 | list_updated = true; | |
1312 | } | |
1313 | ||
1314 | if (!hashmap_isempty(link->bound_by_links)) { | |
1315 | link_free_bound_by_list(link); | |
1316 | list_updated = true; | |
1317 | } | |
1318 | ||
1319 | if (list_updated) | |
1320 | link_save(link); | |
1321 | ||
1322 | return; | |
1323 | } | |
1324 | ||
1325 | void link_drop(Link *link) { | |
1326 | if (!link || link->state == LINK_STATE_LINGER) | |
1327 | return; | |
1328 | ||
1329 | link_set_state(link, LINK_STATE_LINGER); | |
1330 | ||
1331 | link_free_carrier_maps(link); | |
1332 | ||
6a7a4e4d | 1333 | log_link_debug(link, "Link removed"); |
0d4ad91d AR |
1334 | |
1335 | link_unref(link); | |
1336 | ||
1337 | return; | |
1338 | } | |
1339 | ||
3f265037 | 1340 | static int link_joined(Link *link) { |
f882c247 TG |
1341 | int r; |
1342 | ||
ef1ba606 | 1343 | assert(link); |
f5be5601 | 1344 | assert(link->network); |
dd3efc09 | 1345 | |
0d4ad91d AR |
1346 | if (!hashmap_isempty(link->bound_to_links)) { |
1347 | r = link_handle_bound_to_list(link); | |
1348 | if (r < 0) | |
1349 | return r; | |
1350 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
1351 | r = link_up(link); |
1352 | if (r < 0) { | |
1353 | link_enter_failed(link); | |
1354 | return r; | |
1355 | } | |
ef1ba606 | 1356 | } |
f882c247 | 1357 | |
e1853b00 SS |
1358 | if(link->network->bridge) { |
1359 | r = link_set_bridge(link); | |
6a7a4e4d LP |
1360 | if (r < 0) |
1361 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
1362 | } |
1363 | ||
fb6730c4 | 1364 | return link_enter_set_addresses(link); |
02b59d57 TG |
1365 | } |
1366 | ||
3c9b8860 TG |
1367 | static int netdev_join_handler(sd_rtnl *rtnl, sd_rtnl_message *m, |
1368 | void *userdata) { | |
5da8149f | 1369 | _cleanup_link_unref_ Link *link = userdata; |
02b59d57 TG |
1370 | int r; |
1371 | ||
1746cf2a | 1372 | assert(link); |
ef1ba606 | 1373 | assert(link->network); |
02b59d57 | 1374 | |
52433f6b TG |
1375 | link->enslaving --; |
1376 | ||
5da8149f | 1377 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
1378 | return 1; |
1379 | ||
1380 | r = sd_rtnl_message_get_errno(m); | |
856f962c | 1381 | if (r < 0 && r != -EEXIST) { |
6a7a4e4d | 1382 | log_link_error_errno(link, r, "%-*s: could not join netdev: %m", IFNAMSIZ, link->ifname); |
ef1ba606 TG |
1383 | link_enter_failed(link); |
1384 | return 1; | |
ba179154 | 1385 | } else |
6a7a4e4d | 1386 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 1387 | |
856f962c | 1388 | if (link->enslaving <= 0) |
3f265037 | 1389 | link_joined(link); |
02b59d57 TG |
1390 | |
1391 | return 1; | |
1392 | } | |
1393 | ||
3f265037 | 1394 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 1395 | NetDev *netdev; |
672682a6 | 1396 | Iterator i; |
02b59d57 TG |
1397 | int r; |
1398 | ||
1399 | assert(link); | |
1400 | assert(link->network); | |
8434fd5c | 1401 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 1402 | |
e331e246 | 1403 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 1404 | |
fe8db0c5 TG |
1405 | link_save(link); |
1406 | ||
7951dea2 SS |
1407 | if (!link->network->bridge && |
1408 | !link->network->bond && | |
6a0a2f86 | 1409 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 1410 | return link_joined(link); |
02b59d57 | 1411 | |
d9c67ea1 | 1412 | if (link->network->bond) { |
6c861f0a | 1413 | log_link_struct(link, LOG_DEBUG, |
97578344 | 1414 | "MESSAGE=%-*s: enslaving by '%s'", |
6a7a4e4d LP |
1415 | IFNAMSIZ, link->ifname, |
1416 | link->network->bond->ifname, | |
aa9f1140 | 1417 | NETDEVIF(link->network->bond), |
52433f6b | 1418 | NULL); |
449f7554 | 1419 | |
3f265037 | 1420 | r = netdev_join(link->network->bond, link, &netdev_join_handler); |
52433f6b | 1421 | if (r < 0) { |
6c861f0a | 1422 | log_link_struct(link, LOG_WARNING, |
3f265037 | 1423 | "MESSAGE=%-*s: could not join netdev '%s': %s", |
6a7a4e4d LP |
1424 | IFNAMSIZ, link->ifname, |
1425 | link->network->bond->ifname, | |
3c9b8860 | 1426 | strerror(-r), |
aa9f1140 | 1427 | NETDEVIF(link->network->bond), |
52433f6b TG |
1428 | NULL); |
1429 | link_enter_failed(link); | |
1430 | return r; | |
1431 | } | |
1432 | ||
0ad6148e MO |
1433 | link->enslaving ++; |
1434 | } | |
1435 | ||
d9c67ea1 | 1436 | if (link->network->bridge) { |
6c861f0a | 1437 | log_link_struct(link, LOG_DEBUG, |
97578344 | 1438 | "MESSAGE=%-*s: enslaving by '%s'", |
6a7a4e4d LP |
1439 | IFNAMSIZ, link->ifname, |
1440 | link->network->bridge->ifname, | |
aa9f1140 | 1441 | NETDEVIF(link->network->bridge), |
0ad6148e MO |
1442 | NULL); |
1443 | ||
3c9b8860 TG |
1444 | r = netdev_join(link->network->bridge, link, |
1445 | &netdev_join_handler); | |
0ad6148e | 1446 | if (r < 0) { |
6c861f0a | 1447 | log_link_struct(link, LOG_WARNING, |
3f265037 | 1448 | "MESSAGE=%-*s: could not join netdev '%s': %s", |
6a7a4e4d LP |
1449 | IFNAMSIZ, link->ifname, |
1450 | link->network->bridge->ifname, | |
3c9b8860 | 1451 | strerror(-r), |
aa9f1140 | 1452 | NETDEVIF(link->network->bridge), |
0ad6148e MO |
1453 | NULL); |
1454 | link_enter_failed(link); | |
1455 | return r; | |
1456 | } | |
1457 | ||
52433f6b TG |
1458 | link->enslaving ++; |
1459 | } | |
1460 | ||
6a0a2f86 | 1461 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
6c861f0a | 1462 | log_link_struct(link, LOG_DEBUG, |
97578344 | 1463 | "MESSAGE=%-*s: enslaving by '%s'", |
6a7a4e4d LP |
1464 | IFNAMSIZ, link->ifname, |
1465 | netdev->ifname, NETDEVIF(netdev), | |
3c9b8860 | 1466 | NULL); |
7951dea2 | 1467 | |
6a0a2f86 | 1468 | r = netdev_join(netdev, link, &netdev_join_handler); |
7951dea2 | 1469 | if (r < 0) { |
6c861f0a | 1470 | log_link_struct(link, LOG_WARNING, |
3f265037 | 1471 | "MESSAGE=%-*s: could not join netdev '%s': %s", |
6a7a4e4d LP |
1472 | IFNAMSIZ, link->ifname, |
1473 | netdev->ifname, | |
3c9b8860 | 1474 | strerror(-r), |
6a0a2f86 | 1475 | NETDEVIF(netdev), NULL); |
326cb406 SS |
1476 | link_enter_failed(link); |
1477 | return r; | |
1478 | } | |
1479 | ||
326cb406 SS |
1480 | link->enslaving ++; |
1481 | } | |
1482 | ||
ef1ba606 TG |
1483 | return 0; |
1484 | } | |
1485 | ||
769d324c | 1486 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 | 1487 | const char *p = NULL; |
43c6d5ab | 1488 | bool b; |
5a8bcb67 LP |
1489 | int r; |
1490 | ||
43c6d5ab LP |
1491 | b = link_ipv4_forward_enabled(link); |
1492 | ||
63c372cb | 1493 | p = strjoina("/proc/sys/net/ipv4/conf/", link->ifname, "/forwarding"); |
43c6d5ab | 1494 | r = write_string_file_no_create(p, one_zero(b)); |
5a8bcb67 | 1495 | if (r < 0) |
43c6d5ab LP |
1496 | log_link_warning_errno(link, r, "Cannot configure IPv4 forwarding for interface %s: %m", link->ifname); |
1497 | ||
1498 | if (b) { | |
1499 | _cleanup_free_ char *buf = NULL; | |
1500 | ||
1501 | /* If IP forwarding is turned on for this interface, | |
1502 | * then propagate this to the global setting. Given | |
1503 | * that turning this on has side-effects on other | |
1504 | * fields, we'll try to avoid doing this unless | |
1505 | * necessary, hence check the previous value | |
1506 | * first. Note that we never turn this option off | |
1507 | * again, since all interfaces we manage do not do | |
1508 | * forwarding anyway by default, and ownership rules | |
1509 | * of this control are so unclear. */ | |
1510 | ||
1511 | r = read_one_line_file("/proc/sys/net/ipv4/ip_forward", &buf); | |
1512 | if (r < 0) | |
1513 | log_link_warning_errno(link, r, "Cannot read /proc/sys/net/ipv4/ip_forward: %m"); | |
1514 | else if (!streq(buf, "1")) { | |
1515 | r = write_string_file_no_create("/proc/sys/net/ipv4/ip_forward", "1"); | |
1516 | if (r < 0) | |
1517 | log_link_warning_errno(link, r, "Cannot write /proc/sys/net/ipv4/ip_forward: %m"); | |
1518 | } | |
1519 | } | |
769d324c LP |
1520 | |
1521 | return 0; | |
1522 | } | |
1523 | ||
1524 | static int link_set_ipv6_forward(Link *link) { | |
1525 | const char *p = NULL; | |
1526 | int r; | |
1527 | ||
63c372cb | 1528 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/forwarding"); |
769d324c LP |
1529 | r = write_string_file_no_create(p, one_zero(link_ipv6_forward_enabled(link))); |
1530 | if (r < 0) | |
1531 | log_link_warning_errno(link, r, "Cannot configure IPv6 forwarding for interface: %m"); | |
5a8bcb67 LP |
1532 | |
1533 | return 0; | |
1534 | } | |
1535 | ||
a748b692 | 1536 | static int link_configure(Link *link) { |
02b59d57 TG |
1537 | int r; |
1538 | ||
ef1ba606 | 1539 | assert(link); |
b22d8a00 | 1540 | assert(link->network); |
8434fd5c | 1541 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 1542 | |
b98b483b AR |
1543 | r = link_set_bridge_fdb(link); |
1544 | if (r < 0) | |
1545 | return r; | |
1546 | ||
769d324c LP |
1547 | r = link_set_ipv4_forward(link); |
1548 | if (r < 0) | |
1549 | return r; | |
1550 | ||
1551 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
1552 | if (r < 0) |
1553 | return r; | |
1554 | ||
78c958f8 | 1555 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 1556 | r = ipv4ll_configure(link); |
eb34d4af TG |
1557 | if (r < 0) |
1558 | return r; | |
1559 | } | |
1560 | ||
78c958f8 | 1561 | if (link_dhcp4_enabled(link)) { |
3c9b8860 | 1562 | r = dhcp4_configure(link); |
eb34d4af TG |
1563 | if (r < 0) |
1564 | return r; | |
eb34d4af TG |
1565 | } |
1566 | ||
78c958f8 | 1567 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
1568 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
1569 | if (r < 0) | |
1570 | return r; | |
1571 | ||
1572 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
1573 | if (r < 0) | |
1574 | return r; | |
dd43110f TG |
1575 | } |
1576 | ||
78c958f8 | 1577 | if (link_dhcp6_enabled(link)) { |
5c79bd79 | 1578 | r = icmp6_configure(link); |
4138fb2c PF |
1579 | if (r < 0) |
1580 | return r; | |
1581 | } | |
1582 | ||
ce43e484 SS |
1583 | if (link_lldp_enabled(link)) { |
1584 | r = sd_lldp_new(link->ifindex, link->ifname, &link->mac, &link->lldp); | |
1585 | if (r < 0) | |
1586 | return r; | |
1587 | ||
1588 | r = sd_lldp_attach_event(link->lldp, NULL, 0); | |
1589 | if (r < 0) | |
1590 | return r; | |
49699bac SS |
1591 | |
1592 | r = sd_lldp_set_callback(link->lldp, | |
1593 | lldp_handler, link); | |
1594 | if (r < 0) | |
1595 | return r; | |
ce43e484 SS |
1596 | } |
1597 | ||
a61bb41c | 1598 | if (link_has_carrier(link)) { |
1e9be60b TG |
1599 | r = link_acquire_conf(link); |
1600 | if (r < 0) | |
1601 | return r; | |
cc544d5f | 1602 | } |
1e9be60b | 1603 | |
3f265037 | 1604 | return link_enter_join_netdev(link); |
505f8da7 TG |
1605 | } |
1606 | ||
3c9b8860 TG |
1607 | static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m, |
1608 | void *userdata) { | |
5da8149f | 1609 | _cleanup_link_unref_ Link *link = userdata; |
505f8da7 | 1610 | Network *network; |
505f8da7 TG |
1611 | int r; |
1612 | ||
1613 | assert(link); | |
1614 | assert(link->ifname); | |
1615 | assert(link->manager); | |
1616 | ||
8434fd5c | 1617 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 1618 | return 1; |
505f8da7 | 1619 | |
6a7a4e4d | 1620 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 1621 | |
0d4ad91d AR |
1622 | r = link_new_bound_by_list(link); |
1623 | if (r < 0) | |
1624 | return r; | |
1625 | ||
1626 | r = link_handle_bound_by_list(link); | |
1627 | if (r < 0) | |
1628 | return r; | |
1629 | ||
3c9b8860 TG |
1630 | r = network_get(link->manager, link->udev_device, link->ifname, |
1631 | &link->mac, &network); | |
57bd6899 TG |
1632 | if (r == -ENOENT) { |
1633 | link_enter_unmanaged(link); | |
5da8149f | 1634 | return 1; |
57bd6899 TG |
1635 | } else if (r < 0) |
1636 | return r; | |
505f8da7 | 1637 | |
bd2efe92 | 1638 | if (link->flags & IFF_LOOPBACK) { |
d0d6a4cd | 1639 | if (network->link_local != ADDRESS_FAMILY_NO) |
6a7a4e4d | 1640 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); |
78c958f8 | 1641 | |
cb9fc36a | 1642 | if (network->dhcp != ADDRESS_FAMILY_NO) |
6a7a4e4d | 1643 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); |
78c958f8 TG |
1644 | |
1645 | if (network->dhcp_server) | |
6a7a4e4d | 1646 | log_link_debug(link, "Ignoring DHCP server for loopback link"); |
bd2efe92 TG |
1647 | } |
1648 | ||
505f8da7 TG |
1649 | r = network_apply(link->manager, network, link); |
1650 | if (r < 0) | |
1651 | return r; | |
1652 | ||
0d4ad91d AR |
1653 | r = link_new_bound_to_list(link); |
1654 | if (r < 0) | |
1655 | return r; | |
1656 | ||
a748b692 TG |
1657 | r = link_configure(link); |
1658 | if (r < 0) | |
1659 | return r; | |
1660 | ||
5da8149f | 1661 | return 1; |
505f8da7 TG |
1662 | } |
1663 | ||
4f561e8e TG |
1664 | int link_initialized(Link *link, struct udev_device *device) { |
1665 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
1666 | int r; | |
1667 | ||
1668 | assert(link); | |
1669 | assert(link->manager); | |
1670 | assert(link->manager->rtnl); | |
1671 | assert(device); | |
1672 | ||
8434fd5c | 1673 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
1674 | return 0; |
1675 | ||
679b3605 TG |
1676 | if (link->udev_device) |
1677 | return 0; | |
1678 | ||
79008bdd | 1679 | log_link_debug(link, "udev initialized link"); |
4f561e8e TG |
1680 | |
1681 | link->udev_device = udev_device_ref(device); | |
1682 | ||
3c9b8860 TG |
1683 | /* udev has initialized the link, but we don't know if we have yet |
1684 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
1685 | * when it returns we know that the pending NEWLINKs have already been | |
1686 | * processed and that we are up-to-date */ | |
4f561e8e | 1687 | |
3c9b8860 TG |
1688 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
1689 | link->ifindex); | |
4f561e8e TG |
1690 | if (r < 0) |
1691 | return r; | |
1692 | ||
3c9b8860 TG |
1693 | r = sd_rtnl_call_async(link->manager->rtnl, req, |
1694 | link_initialized_and_synced, link, 0, NULL); | |
4f561e8e TG |
1695 | if (r < 0) |
1696 | return r; | |
1697 | ||
5da8149f TG |
1698 | link_ref(link); |
1699 | ||
4f561e8e TG |
1700 | return 0; |
1701 | } | |
1702 | ||
5a8bcb67 LP |
1703 | static Address* link_get_equal_address(Link *link, Address *needle) { |
1704 | Address *i; | |
1705 | ||
1706 | assert(link); | |
1707 | assert(needle); | |
1708 | ||
1709 | LIST_FOREACH(addresses, i, link->addresses) | |
1710 | if (address_equal(i, needle)) | |
1711 | return i; | |
1712 | ||
1713 | return NULL; | |
1714 | } | |
1715 | ||
45af44d4 | 1716 | int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) { |
fbbeb65a TG |
1717 | Manager *m = userdata; |
1718 | Link *link = NULL; | |
1719 | uint16_t type; | |
1720 | _cleanup_address_free_ Address *address = NULL; | |
851c9f82 | 1721 | unsigned char flags; |
5a8bcb67 LP |
1722 | Address *existing; |
1723 | char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX]; | |
c6d3b303 | 1724 | const char *valid_str = NULL; |
fbbeb65a TG |
1725 | int r, ifindex; |
1726 | ||
1727 | assert(rtnl); | |
1728 | assert(message); | |
1729 | assert(m); | |
1730 | ||
45af44d4 TG |
1731 | if (sd_rtnl_message_is_error(message)) { |
1732 | r = sd_rtnl_message_get_errno(message); | |
1733 | if (r < 0) | |
1734 | log_warning_errno(r, "rtnl: failed to receive address: %m"); | |
1735 | ||
1736 | return 0; | |
1737 | } | |
1738 | ||
fbbeb65a TG |
1739 | r = sd_rtnl_message_get_type(message, &type); |
1740 | if (r < 0) { | |
6a7a4e4d | 1741 | log_warning_errno(r, "rtnl: could not get message type: %m"); |
fbbeb65a | 1742 | return 0; |
cdfee943 TG |
1743 | } else if (type != RTM_NEWADDR && type != RTM_DELADDR) { |
1744 | log_warning("rtnl: received unexpected message type when processing address"); | |
1745 | return 0; | |
fbbeb65a TG |
1746 | } |
1747 | ||
1748 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
45af44d4 | 1749 | if (r < 0) { |
cdfee943 | 1750 | log_warning_errno(r, "rtnl: could not get ifindex from address: %m"); |
45af44d4 TG |
1751 | return 0; |
1752 | } else if (ifindex <= 0) { | |
1753 | log_warning("rtnl: received address message with invalid ifindex: %d", ifindex); | |
fbbeb65a TG |
1754 | return 0; |
1755 | } else { | |
1756 | r = link_get(m, ifindex, &link); | |
1757 | if (r < 0 || !link) { | |
6a24f148 TG |
1758 | /* when enumerating we might be out of sync, but we will |
1759 | * get the address again, so just ignore it */ | |
1760 | if (!m->enumerating) | |
1761 | log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex); | |
fbbeb65a TG |
1762 | return 0; |
1763 | } | |
1764 | } | |
1765 | ||
1766 | r = address_new_dynamic(&address); | |
1767 | if (r < 0) | |
393c0c5e | 1768 | return r; |
fbbeb65a TG |
1769 | |
1770 | r = sd_rtnl_message_addr_get_family(message, &address->family); | |
1771 | if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) { | |
6a7a4e4d | 1772 | log_link_warning(link, "rtnl: received address with invalid family, ignoring."); |
fbbeb65a TG |
1773 | return 0; |
1774 | } | |
1775 | ||
1776 | r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen); | |
1777 | if (r < 0) { | |
6a7a4e4d | 1778 | log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m"); |
e375dcde TG |
1779 | return 0; |
1780 | } | |
1781 | ||
1782 | r = sd_rtnl_message_addr_get_scope(message, &address->scope); | |
1783 | if (r < 0) { | |
6a7a4e4d | 1784 | log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m"); |
fbbeb65a TG |
1785 | return 0; |
1786 | } | |
1787 | ||
851c9f82 | 1788 | r = sd_rtnl_message_addr_get_flags(message, &flags); |
81163121 | 1789 | if (r < 0) { |
6a7a4e4d | 1790 | log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m"); |
81163121 TG |
1791 | return 0; |
1792 | } | |
851c9f82 | 1793 | address->flags = flags; |
81163121 | 1794 | |
fbbeb65a TG |
1795 | switch (address->family) { |
1796 | case AF_INET: | |
5a8bcb67 | 1797 | r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in); |
fbbeb65a | 1798 | if (r < 0) { |
6a7a4e4d | 1799 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); |
fbbeb65a TG |
1800 | return 0; |
1801 | } | |
1802 | ||
1803 | break; | |
1804 | ||
1805 | case AF_INET6: | |
5a8bcb67 | 1806 | r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6); |
fbbeb65a | 1807 | if (r < 0) { |
6a7a4e4d | 1808 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); |
fbbeb65a TG |
1809 | return 0; |
1810 | } | |
1811 | ||
1812 | break; | |
1813 | ||
1814 | default: | |
1815 | assert_not_reached("invalid address family"); | |
1816 | } | |
1817 | ||
5a8bcb67 | 1818 | if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) { |
6a7a4e4d | 1819 | log_link_warning(link, "Could not print address"); |
fbbeb65a TG |
1820 | return 0; |
1821 | } | |
1822 | ||
5a8bcb67 | 1823 | r = sd_rtnl_message_read_cache_info(message, IFA_CACHEINFO, &address->cinfo); |
c6d3b303 TG |
1824 | if (r >= 0) { |
1825 | if (address->cinfo.ifa_valid == CACHE_INFO_INFINITY_LIFE_TIME) | |
1826 | valid_str = "ever"; | |
1827 | else | |
1828 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, | |
1829 | address->cinfo.ifa_valid * USEC_PER_SEC, | |
1830 | USEC_PER_SEC); | |
1831 | } | |
1832 | ||
5a8bcb67 LP |
1833 | existing = link_get_equal_address(link, address); |
1834 | ||
1835 | switch (type) { | |
1836 | case RTM_NEWADDR: | |
1837 | if (existing) { | |
1838 | log_link_debug(link, "Updating address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); | |
428fd0a7 | 1839 | |
428fd0a7 | 1840 | |
5a8bcb67 LP |
1841 | existing->scope = address->scope; |
1842 | existing->flags = address->flags; | |
1843 | existing->cinfo = address->cinfo; | |
428fd0a7 | 1844 | |
5a8bcb67 LP |
1845 | } else { |
1846 | log_link_debug(link, "Adding address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); | |
428fd0a7 | 1847 | |
5a8bcb67 LP |
1848 | LIST_PREPEND(addresses, link->addresses, address); |
1849 | address_establish(address, link); | |
fbbeb65a | 1850 | |
5a8bcb67 | 1851 | address = NULL; |
428fd0a7 | 1852 | |
5a8bcb67 LP |
1853 | link_save(link); |
1854 | } | |
f5602be9 | 1855 | |
428fd0a7 | 1856 | break; |
5a8bcb67 | 1857 | |
fbbeb65a | 1858 | case RTM_DELADDR: |
428fd0a7 | 1859 | |
5a8bcb67 LP |
1860 | if (existing) { |
1861 | log_link_debug(link, "Removing address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); | |
1862 | address_release(existing, link); | |
1863 | LIST_REMOVE(addresses, link->addresses, existing); | |
1864 | address_free(existing); | |
393c0c5e | 1865 | } else |
5a8bcb67 | 1866 | log_link_warning(link, "Removing non-existent address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); |
f5602be9 | 1867 | |
fbbeb65a TG |
1868 | break; |
1869 | default: | |
1870 | assert_not_reached("Received invalid RTNL message type"); | |
1871 | } | |
1872 | ||
1873 | return 1; | |
1874 | } | |
1875 | ||
505f8da7 TG |
1876 | int link_add(Manager *m, sd_rtnl_message *message, Link **ret) { |
1877 | Link *link; | |
1878 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; | |
1879 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; | |
1880 | int r; | |
1881 | ||
1882 | assert(m); | |
fbbeb65a | 1883 | assert(m->rtnl); |
505f8da7 TG |
1884 | assert(message); |
1885 | assert(ret); | |
1886 | ||
1887 | r = link_new(m, message, ret); | |
1888 | if (r < 0) | |
1889 | return r; | |
1890 | ||
1891 | link = *ret; | |
1892 | ||
6a7a4e4d | 1893 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 TG |
1894 | |
1895 | if (detect_container(NULL) <= 0) { | |
1896 | /* not in a container, udev will be around */ | |
ae06ab10 | 1897 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 | 1898 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
6a7a4e4d LP |
1899 | if (!device) |
1900 | return log_link_warning_errno(link, errno, "Could not find udev device: %m"); | |
505f8da7 | 1901 | |
3c4cb064 | 1902 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 1903 | /* not yet ready */ |
79008bdd | 1904 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 1905 | return 0; |
3c4cb064 | 1906 | } |
505f8da7 | 1907 | |
4f561e8e TG |
1908 | r = link_initialized(link, device); |
1909 | if (r < 0) | |
1910 | return r; | |
1911 | } else { | |
5da8149f TG |
1912 | /* we are calling a callback directly, so must take a ref */ |
1913 | link_ref(link); | |
1914 | ||
4f561e8e TG |
1915 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
1916 | if (r < 0) | |
1917 | return r; | |
1918 | } | |
505f8da7 | 1919 | |
a748b692 TG |
1920 | return 0; |
1921 | } | |
1922 | ||
9c0a72f9 TG |
1923 | static int link_carrier_gained(Link *link) { |
1924 | int r; | |
1925 | ||
1926 | assert(link); | |
1927 | ||
1928 | if (link->network) { | |
1929 | r = link_acquire_conf(link); | |
1930 | if (r < 0) { | |
1931 | link_enter_failed(link); | |
1932 | return r; | |
1933 | } | |
1934 | } | |
1935 | ||
0d4ad91d AR |
1936 | r = link_handle_bound_by_list(link); |
1937 | if (r < 0) | |
1938 | return r; | |
1939 | ||
9c0a72f9 TG |
1940 | return 0; |
1941 | } | |
1942 | ||
1943 | static int link_carrier_lost(Link *link) { | |
1944 | int r; | |
1945 | ||
1946 | assert(link); | |
1947 | ||
1948 | r = link_stop_clients(link); | |
1949 | if (r < 0) { | |
1950 | link_enter_failed(link); | |
1951 | return r; | |
1952 | } | |
1953 | ||
0d4ad91d AR |
1954 | r = link_handle_bound_by_list(link); |
1955 | if (r < 0) | |
1956 | return r; | |
1957 | ||
9c0a72f9 TG |
1958 | return 0; |
1959 | } | |
1960 | ||
1961 | int link_carrier_reset(Link *link) { | |
1962 | int r; | |
1963 | ||
1964 | assert(link); | |
1965 | ||
1966 | if (link_has_carrier(link)) { | |
1967 | r = link_carrier_lost(link); | |
1968 | if (r < 0) | |
1969 | return r; | |
1970 | ||
1971 | r = link_carrier_gained(link); | |
1972 | if (r < 0) | |
1973 | return r; | |
1974 | ||
6a7a4e4d | 1975 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
1976 | } |
1977 | ||
1978 | return 0; | |
1979 | } | |
1980 | ||
1981 | ||
22936833 | 1982 | int link_update(Link *link, sd_rtnl_message *m) { |
c49b33ac | 1983 | struct ether_addr mac; |
ca4e095a | 1984 | const char *ifname; |
afe7fd56 | 1985 | uint32_t mtu; |
a61bb41c | 1986 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
1987 | int r; |
1988 | ||
dd3efc09 | 1989 | assert(link); |
b8941f74 | 1990 | assert(link->ifname); |
22936833 TG |
1991 | assert(m); |
1992 | ||
7619683b TG |
1993 | if (link->state == LINK_STATE_LINGER) { |
1994 | link_ref(link); | |
6a7a4e4d | 1995 | log_link_info(link, "Link readded"); |
e331e246 | 1996 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
1997 | |
1998 | r = link_new_carrier_maps(link); | |
1999 | if (r < 0) | |
2000 | return r; | |
7619683b TG |
2001 | } |
2002 | ||
b8941f74 TG |
2003 | r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname); |
2004 | if (r >= 0 && !streq(ifname, link->ifname)) { | |
6a7a4e4d | 2005 | log_link_info(link, "Renamed to %s", ifname); |
b8941f74 | 2006 | |
0d4ad91d AR |
2007 | link_free_carrier_maps(link); |
2008 | ||
b8941f74 TG |
2009 | free(link->ifname); |
2010 | link->ifname = strdup(ifname); | |
2011 | if (!link->ifname) | |
2012 | return -ENOMEM; | |
0d4ad91d AR |
2013 | |
2014 | r = link_new_carrier_maps(link); | |
2015 | if (r < 0) | |
2016 | return r; | |
b8941f74 TG |
2017 | } |
2018 | ||
afe7fd56 TG |
2019 | r = sd_rtnl_message_read_u32(m, IFLA_MTU, &mtu); |
2020 | if (r >= 0 && mtu > 0) { | |
2021 | link->mtu = mtu; | |
2022 | if (!link->original_mtu) { | |
2023 | link->original_mtu = mtu; | |
6a7a4e4d | 2024 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
2025 | } |
2026 | ||
2027 | if (link->dhcp_client) { | |
3c9b8860 TG |
2028 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
2029 | link->mtu); | |
afe7fd56 | 2030 | if (r < 0) { |
6a7a4e4d | 2031 | log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); |
afe7fd56 TG |
2032 | return r; |
2033 | } | |
2034 | } | |
9842de0d | 2035 | } |
69629de9 | 2036 | |
e9189a1f TG |
2037 | /* The kernel may broadcast NEWLINK messages without the MAC address |
2038 | set, simply ignore them. */ | |
c49b33ac | 2039 | r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 2040 | if (r >= 0) { |
3c9b8860 TG |
2041 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2042 | ETH_ALEN)) { | |
c49b33ac | 2043 | |
3c9b8860 TG |
2044 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2045 | ETH_ALEN); | |
c49b33ac | 2046 | |
79008bdd | 2047 | log_link_debug(link, "MAC address: " |
20861203 TG |
2048 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
2049 | mac.ether_addr_octet[0], | |
2050 | mac.ether_addr_octet[1], | |
2051 | mac.ether_addr_octet[2], | |
2052 | mac.ether_addr_octet[3], | |
2053 | mac.ether_addr_octet[4], | |
2054 | mac.ether_addr_octet[5]); | |
c49b33ac | 2055 | |
20861203 TG |
2056 | if (link->ipv4ll) { |
2057 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
2058 | if (r < 0) |
2059 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 2060 | } |
c49b33ac | 2061 | |
20861203 | 2062 | if (link->dhcp_client) { |
3c9b8860 | 2063 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
2064 | (const uint8_t *) &link->mac, |
2065 | sizeof (link->mac), | |
2066 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2067 | if (r < 0) |
2068 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
c49b33ac | 2069 | } |
4138fb2c PF |
2070 | |
2071 | if (link->dhcp6_client) { | |
2072 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, | |
76253e73 DW |
2073 | (const uint8_t *) &link->mac, |
2074 | sizeof (link->mac), | |
2075 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2076 | if (r < 0) |
2077 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
4138fb2c | 2078 | } |
c49b33ac | 2079 | } |
4f882b2a TG |
2080 | } |
2081 | ||
a61bb41c TG |
2082 | had_carrier = link_has_carrier(link); |
2083 | ||
2084 | r = link_update_flags(link, m); | |
2085 | if (r < 0) | |
2086 | return r; | |
2087 | ||
2088 | carrier_gained = !had_carrier && link_has_carrier(link); | |
2089 | carrier_lost = had_carrier && !link_has_carrier(link); | |
2090 | ||
2091 | if (carrier_gained) { | |
6a7a4e4d | 2092 | log_link_info(link, "Gained carrier"); |
a61bb41c | 2093 | |
9c0a72f9 TG |
2094 | r = link_carrier_gained(link); |
2095 | if (r < 0) | |
2096 | return r; | |
a61bb41c | 2097 | } else if (carrier_lost) { |
6a7a4e4d | 2098 | log_link_info(link, "Lost carrier"); |
a61bb41c | 2099 | |
9c0a72f9 TG |
2100 | r = link_carrier_lost(link); |
2101 | if (r < 0) | |
a61bb41c | 2102 | return r; |
9c0a72f9 | 2103 | |
a61bb41c TG |
2104 | } |
2105 | ||
2106 | return 0; | |
dd3efc09 | 2107 | } |
fe8db0c5 | 2108 | |
e375dcde | 2109 | static void link_update_operstate(Link *link) { |
e331e246 | 2110 | LinkOperationalState operstate; |
e375dcde TG |
2111 | assert(link); |
2112 | ||
2113 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
e331e246 | 2114 | operstate = LINK_OPERSTATE_DORMANT; |
a61bb41c | 2115 | else if (link_has_carrier(link)) { |
e375dcde TG |
2116 | Address *address; |
2117 | uint8_t scope = RT_SCOPE_NOWHERE; | |
2118 | ||
2119 | /* if we have carrier, check what addresses we have */ | |
2120 | LIST_FOREACH(addresses, address, link->addresses) { | |
81163121 TG |
2121 | if (address->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED)) |
2122 | continue; | |
2123 | ||
e375dcde TG |
2124 | if (address->scope < scope) |
2125 | scope = address->scope; | |
2126 | } | |
2127 | ||
2128 | if (scope < RT_SCOPE_SITE) | |
2129 | /* universally accessible addresses found */ | |
e331e246 | 2130 | operstate = LINK_OPERSTATE_ROUTABLE; |
e375dcde TG |
2131 | else if (scope < RT_SCOPE_HOST) |
2132 | /* only link or site local addresses found */ | |
e331e246 | 2133 | operstate = LINK_OPERSTATE_DEGRADED; |
e375dcde TG |
2134 | else |
2135 | /* no useful addresses found */ | |
e331e246 | 2136 | operstate = LINK_OPERSTATE_CARRIER; |
54cba0b1 | 2137 | } else if (link->flags & IFF_UP) |
e331e246 | 2138 | operstate = LINK_OPERSTATE_NO_CARRIER; |
54cba0b1 | 2139 | else |
e331e246 TG |
2140 | operstate = LINK_OPERSTATE_OFF; |
2141 | ||
2142 | if (link->operstate != operstate) { | |
2143 | link->operstate = operstate; | |
2144 | link_send_changed(link, "OperationalState", NULL); | |
2145 | } | |
e375dcde TG |
2146 | } |
2147 | ||
fe8db0c5 | 2148 | int link_save(Link *link) { |
68a8723c | 2149 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 2150 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 2151 | const char *admin_state, *oper_state; |
fe8db0c5 TG |
2152 | int r; |
2153 | ||
2154 | assert(link); | |
2155 | assert(link->state_file); | |
68a8723c | 2156 | assert(link->lease_file); |
bbf7c048 TG |
2157 | assert(link->manager); |
2158 | ||
e375dcde TG |
2159 | link_update_operstate(link); |
2160 | ||
bbf7c048 TG |
2161 | r = manager_save(link->manager); |
2162 | if (r < 0) | |
2163 | return r; | |
fe8db0c5 | 2164 | |
370e9930 TG |
2165 | if (link->state == LINK_STATE_LINGER) { |
2166 | unlink(link->state_file); | |
2167 | return 0; | |
2168 | } | |
2169 | ||
deb2e523 TG |
2170 | admin_state = link_state_to_string(link->state); |
2171 | assert(admin_state); | |
2172 | ||
e375dcde TG |
2173 | oper_state = link_operstate_to_string(link->operstate); |
2174 | assert(oper_state); | |
deb2e523 | 2175 | |
fe8db0c5 TG |
2176 | r = fopen_temporary(link->state_file, &f, &temp_path); |
2177 | if (r < 0) | |
6a7a4e4d | 2178 | goto fail; |
fe8db0c5 TG |
2179 | |
2180 | fchmod(fileno(f), 0644); | |
2181 | ||
2182 | fprintf(f, | |
2183 | "# This is private data. Do not parse.\n" | |
deb2e523 | 2184 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
2185 | "OPER_STATE=%s\n", |
2186 | admin_state, oper_state); | |
fe8db0c5 | 2187 | |
bcb7a07e | 2188 | if (link->network) { |
ea352b40 LP |
2189 | char **address, **domain; |
2190 | bool space; | |
b0e39c82 | 2191 | |
adc5b2e2 TG |
2192 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
2193 | ||
b0e39c82 | 2194 | fputs("DNS=", f); |
ea352b40 LP |
2195 | space = false; |
2196 | STRV_FOREACH(address, link->network->dns) { | |
2197 | if (space) | |
2198 | fputc(' ', f); | |
2199 | fputs(*address, f); | |
2200 | space = true; | |
2201 | } | |
d5314fff | 2202 | |
b0e39c82 TG |
2203 | if (link->network->dhcp_dns && |
2204 | link->dhcp_lease) { | |
2205 | const struct in_addr *addresses; | |
2206 | ||
2207 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
2208 | if (r > 0) { | |
ea352b40 LP |
2209 | if (space) |
2210 | fputc(' ', f); | |
b0e39c82 | 2211 | serialize_in_addrs(f, addresses, r); |
b0e39c82 TG |
2212 | } |
2213 | } | |
2214 | ||
b0e39c82 TG |
2215 | fputs("\n", f); |
2216 | ||
2217 | fprintf(f, "NTP="); | |
ea352b40 LP |
2218 | space = false; |
2219 | STRV_FOREACH(address, link->network->ntp) { | |
2220 | if (space) | |
2221 | fputc(' ', f); | |
2222 | fputs(*address, f); | |
2223 | space = true; | |
2224 | } | |
d5314fff | 2225 | |
b0e39c82 TG |
2226 | if (link->network->dhcp_ntp && |
2227 | link->dhcp_lease) { | |
2228 | const struct in_addr *addresses; | |
2229 | ||
2230 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
2231 | if (r > 0) { | |
ea352b40 LP |
2232 | if (space) |
2233 | fputc(' ', f); | |
b0e39c82 | 2234 | serialize_in_addrs(f, addresses, r); |
b0e39c82 TG |
2235 | } |
2236 | } | |
2237 | ||
b0e39c82 | 2238 | fputs("\n", f); |
bd8f6538 | 2239 | |
6192b846 | 2240 | fprintf(f, "DOMAINS="); |
ea352b40 LP |
2241 | space = false; |
2242 | STRV_FOREACH(domain, link->network->domains) { | |
2243 | if (space) | |
2244 | fputc(' ', f); | |
2245 | fputs(*domain, f); | |
2246 | space = true; | |
2247 | } | |
d5314fff | 2248 | |
ad0734e8 | 2249 | if (link->network->dhcp_domains && |
9b4d1882 TG |
2250 | link->dhcp_lease) { |
2251 | const char *domainname; | |
2252 | ||
2253 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); | |
6192b846 | 2254 | if (r >= 0) { |
ea352b40 LP |
2255 | if (space) |
2256 | fputc(' ', f); | |
6192b846 | 2257 | fputs(domainname, f); |
6192b846 | 2258 | } |
9b4d1882 TG |
2259 | } |
2260 | ||
6192b846 TG |
2261 | fputs("\n", f); |
2262 | ||
67272d15 TG |
2263 | fprintf(f, "WILDCARD_DOMAIN=%s\n", |
2264 | yes_no(link->network->wildcard_domain)); | |
2265 | ||
3c9b8860 TG |
2266 | fprintf(f, "LLMNR=%s\n", |
2267 | llmnr_support_to_string(link->network->llmnr)); | |
bcb7a07e | 2268 | } |
7374f9d8 | 2269 | |
0d4ad91d AR |
2270 | if (!hashmap_isempty(link->bound_to_links)) { |
2271 | Link *carrier; | |
2272 | Iterator i; | |
2273 | bool space = false; | |
2274 | ||
2275 | fputs("CARRIER_BOUND_TO=", f); | |
2276 | HASHMAP_FOREACH(carrier, link->bound_to_links, i) { | |
2277 | if (space) | |
2278 | fputc(' ', f); | |
2279 | fputs(carrier->ifname, f); | |
2280 | space = true; | |
2281 | } | |
2282 | ||
2283 | fputs("\n", f); | |
2284 | } | |
2285 | ||
2286 | if (!hashmap_isempty(link->bound_by_links)) { | |
2287 | Link *carrier; | |
2288 | Iterator i; | |
2289 | bool space = false; | |
2290 | ||
2291 | fputs("CARRIER_BOUND_BY=", f); | |
0d4ad91d AR |
2292 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
2293 | if (space) | |
2294 | fputc(' ', f); | |
2295 | fputs(carrier->ifname, f); | |
2296 | space = true; | |
2297 | } | |
2298 | ||
2299 | fputs("\n", f); | |
2300 | } | |
2301 | ||
fe8db0c5 | 2302 | if (link->dhcp_lease) { |
d9876a52 TG |
2303 | assert(link->network); |
2304 | ||
1dc24d5f | 2305 | r = sd_dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 2306 | if (r < 0) |
c2d6bd61 | 2307 | goto fail; |
fe8db0c5 | 2308 | |
7374f9d8 | 2309 | fprintf(f, |
b0e39c82 TG |
2310 | "DHCP_LEASE=%s\n", |
2311 | link->lease_file); | |
deb2e523 | 2312 | } else |
68a8723c | 2313 | unlink(link->lease_file); |
fe8db0c5 | 2314 | |
49699bac SS |
2315 | if (link->lldp) { |
2316 | assert(link->network); | |
2317 | ||
2318 | r = sd_lldp_save(link->lldp, link->lldp_file); | |
2319 | if (r < 0) | |
2320 | goto fail; | |
2321 | ||
2322 | fprintf(f, | |
2323 | "LLDP_FILE=%s\n", | |
2324 | link->lldp_file); | |
2325 | } else | |
2326 | unlink(link->lldp_file); | |
2327 | ||
c2d6bd61 LP |
2328 | r = fflush_and_check(f); |
2329 | if (r < 0) | |
2330 | goto fail; | |
fe8db0c5 | 2331 | |
c2d6bd61 | 2332 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 2333 | r = -errno; |
c2d6bd61 | 2334 | goto fail; |
fe8db0c5 TG |
2335 | } |
2336 | ||
c2d6bd61 | 2337 | return 0; |
c2d6bd61 | 2338 | fail: |
6a7a4e4d LP |
2339 | log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
2340 | (void) unlink(link->state_file); | |
2341 | ||
2342 | if (temp_path) | |
2343 | (void) unlink(temp_path); | |
2344 | ||
fe8db0c5 TG |
2345 | return r; |
2346 | } | |
2347 | ||
2348 | static const char* const link_state_table[_LINK_STATE_MAX] = { | |
8434fd5c | 2349 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
2350 | [LINK_STATE_ENSLAVING] = "configuring", |
2351 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
2352 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
2353 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 2354 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 2355 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 2356 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
2357 | }; |
2358 | ||
2359 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
2360 | |
2361 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
2362 | [LINK_OPERSTATE_OFF] = "off", |
2363 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
2364 | [LINK_OPERSTATE_DORMANT] = "dormant", |
2365 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
2366 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
2367 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
2368 | }; | |
2369 | ||
2370 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |