]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
Merge pull request #14338 from keszybz/functional-test-rework
[thirdparty/systemd.git] / src / network / networkd-manager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/in.h>
4 #include <sys/socket.h>
5 #include <unistd.h>
6 #include <linux/if.h>
7 #include <linux/fib_rules.h>
8 #include <linux/nexthop.h>
9
10 #include "sd-daemon.h"
11 #include "sd-netlink.h"
12
13 #include "alloc-util.h"
14 #include "bus-polkit.h"
15 #include "bus-util.h"
16 #include "conf-parser.h"
17 #include "def.h"
18 #include "device-private.h"
19 #include "device-util.h"
20 #include "dns-domain.h"
21 #include "fd-util.h"
22 #include "fileio.h"
23 #include "local-addresses.h"
24 #include "netlink-util.h"
25 #include "network-internal.h"
26 #include "networkd-dhcp6.h"
27 #include "networkd-link-bus.h"
28 #include "networkd-manager-bus.h"
29 #include "networkd-manager.h"
30 #include "networkd-network-bus.h"
31 #include "networkd-speed-meter.h"
32 #include "ordered-set.h"
33 #include "path-lookup.h"
34 #include "path-util.h"
35 #include "set.h"
36 #include "signal-util.h"
37 #include "strv.h"
38 #include "sysctl-util.h"
39 #include "tmpfile-util.h"
40 #include "udev-util.h"
41 #include "virt.h"
42
43 /* use 128 MB for receive socket kernel queue. */
44 #define RCVBUF_SIZE (128*1024*1024)
45
46 static int log_message_warning_errno(sd_netlink_message *m, int err, const char *msg) {
47 const char *err_msg = NULL;
48
49 (void) sd_netlink_message_read_string(m, NLMSGERR_ATTR_MSG, &err_msg);
50 return log_warning_errno(err, "%s: %s%s%m", msg, strempty(err_msg), err_msg ? " " : "");
51 }
52
53 static int setup_default_address_pool(Manager *m) {
54 AddressPool *p;
55 int r;
56
57 assert(m);
58
59 /* Add in the well-known private address ranges. */
60
61 r = address_pool_new_from_string(m, &p, AF_INET6, "fd00::", 8);
62 if (r < 0)
63 return r;
64
65 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
66 if (r < 0)
67 return r;
68
69 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
70 if (r < 0)
71 return r;
72
73 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
74 if (r < 0)
75 return r;
76
77 return 0;
78 }
79
80 static int manager_reset_all(Manager *m) {
81 Link *link;
82 Iterator i;
83 int r;
84
85 assert(m);
86
87 HASHMAP_FOREACH(link, m->links, i) {
88 r = link_carrier_reset(link);
89 if (r < 0)
90 log_link_warning_errno(link, r, "Could not reset carrier: %m");
91 }
92
93 return 0;
94 }
95
96 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
97 Manager *m = userdata;
98 int b, r;
99
100 assert(message);
101 assert(m);
102
103 r = sd_bus_message_read(message, "b", &b);
104 if (r < 0) {
105 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
106 return 0;
107 }
108
109 if (b)
110 return 0;
111
112 log_debug("Coming back from suspend, resetting all connections...");
113
114 (void) manager_reset_all(m);
115
116 return 0;
117 }
118
119 static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
120 Manager *m = userdata;
121
122 assert(message);
123 assert(m);
124
125 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
126 if (m->dynamic_hostname)
127 (void) manager_set_hostname(m, m->dynamic_hostname);
128 if (m->dynamic_timezone)
129 (void) manager_set_timezone(m, m->dynamic_timezone);
130 if (m->links_requesting_uuid)
131 (void) manager_request_product_uuid(m, NULL);
132
133 return 0;
134 }
135
136 int manager_connect_bus(Manager *m) {
137 int r;
138
139 assert(m);
140
141 if (m->bus)
142 return 0;
143
144 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network");
145 if (r < 0)
146 return log_error_errno(r, "Failed to connect to bus: %m");
147
148 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
149 if (r < 0)
150 return log_error_errno(r, "Failed to add manager object vtable: %m");
151
152 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
153 if (r < 0)
154 return log_error_errno(r, "Failed to add link object vtable: %m");
155
156 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
157 if (r < 0)
158 return log_error_errno(r, "Failed to add link enumerator: %m");
159
160 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
161 if (r < 0)
162 return log_error_errno(r, "Failed to add network object vtable: %m");
163
164 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
165 if (r < 0)
166 return log_error_errno(r, "Failed to add network enumerator: %m");
167
168 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.network1", 0, NULL, NULL);
169 if (r < 0)
170 return log_error_errno(r, "Failed to request name: %m");
171
172 r = sd_bus_attach_event(m->bus, m->event, 0);
173 if (r < 0)
174 return log_error_errno(r, "Failed to attach bus to event loop: %m");
175
176 r = sd_bus_match_signal_async(
177 m->bus,
178 NULL,
179 "org.freedesktop.DBus.Local",
180 NULL,
181 "org.freedesktop.DBus.Local",
182 "Connected",
183 on_connected, NULL, m);
184 if (r < 0)
185 return log_error_errno(r, "Failed to request match on Connected signal: %m");
186
187 r = sd_bus_match_signal_async(
188 m->bus,
189 NULL,
190 "org.freedesktop.login1",
191 "/org/freedesktop/login1",
192 "org.freedesktop.login1.Manager",
193 "PrepareForSleep",
194 match_prepare_for_sleep, NULL, m);
195 if (r < 0)
196 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
197
198 return 0;
199 }
200
201 static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
202 Manager *m = userdata;
203 DeviceAction action;
204 Link *link = NULL;
205 int r, ifindex;
206
207 assert(m);
208 assert(device);
209
210 r = device_get_action(device, &action);
211 if (r < 0) {
212 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
213 return 0;
214 }
215
216 if (!IN_SET(action, DEVICE_ACTION_ADD, DEVICE_ACTION_CHANGE, DEVICE_ACTION_MOVE)) {
217 log_device_debug(device, "Ignoring udev %s event for device.", device_action_to_string(action));
218 return 0;
219 }
220
221 r = sd_device_get_ifindex(device, &ifindex);
222 if (r < 0) {
223 log_device_debug_errno(device, r, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m");
224 return 0;
225 }
226
227 r = device_is_renaming(device);
228 if (r < 0) {
229 log_device_error_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
230 device_action_to_string(action));
231 return 0;
232 }
233 if (r > 0) {
234 log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed: %m");
235 return 0;
236 }
237
238 r = link_get(m, ifindex, &link);
239 if (r < 0) {
240 if (r != -ENODEV)
241 log_debug_errno(r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
242 return 0;
243 }
244
245 (void) link_initialized(link, device);
246
247 return 0;
248 }
249
250 static int manager_connect_udev(Manager *m) {
251 int r;
252
253 /* udev does not initialize devices inside containers,
254 * so we rely on them being already initialized before
255 * entering the container */
256 if (detect_container() > 0)
257 return 0;
258
259 r = sd_device_monitor_new(&m->device_monitor);
260 if (r < 0)
261 return log_error_errno(r, "Failed to initialize device monitor: %m");
262
263 r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "net", NULL);
264 if (r < 0)
265 return log_error_errno(r, "Could not add device monitor filter: %m");
266
267 r = sd_device_monitor_attach_event(m->device_monitor, m->event);
268 if (r < 0)
269 return log_error_errno(r, "Failed to attach event to device monitor: %m");
270
271 r = sd_device_monitor_start(m->device_monitor, manager_udev_process_link, m);
272 if (r < 0)
273 return log_error_errno(r, "Failed to start device monitor: %m");
274
275 return 0;
276 }
277
278 int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
279 _cleanup_(route_freep) Route *tmp = NULL;
280 Route *route = NULL;
281 Manager *m = userdata;
282 Link *link = NULL;
283 uint32_t ifindex;
284 uint16_t type;
285 unsigned char table;
286 int r;
287
288 assert(rtnl);
289 assert(message);
290 assert(m);
291
292 if (sd_netlink_message_is_error(message)) {
293 r = sd_netlink_message_get_errno(message);
294 if (r < 0)
295 log_message_warning_errno(message, r, "rtnl: failed to receive route message, ignoring");
296
297 return 0;
298 }
299
300 r = sd_netlink_message_get_type(message, &type);
301 if (r < 0) {
302 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
303 return 0;
304 } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) {
305 log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type);
306 return 0;
307 }
308
309 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
310 if (r == -ENODATA) {
311 log_debug("rtnl: received route message without ifindex, ignoring");
312 return 0;
313 } else if (r < 0) {
314 log_warning_errno(r, "rtnl: could not get ifindex from route message, ignoring: %m");
315 return 0;
316 } else if (ifindex <= 0) {
317 log_warning("rtnl: received route message with invalid ifindex %d, ignoring.", ifindex);
318 return 0;
319 }
320
321 r = link_get(m, ifindex, &link);
322 if (r < 0 || !link) {
323 /* when enumerating we might be out of sync, but we will
324 * get the route again, so just ignore it */
325 if (!m->enumerating)
326 log_warning("rtnl: received route message for link (%d) we do not know about, ignoring", ifindex);
327 return 0;
328 }
329
330 r = route_new(&tmp);
331 if (r < 0)
332 return log_oom();
333
334 r = sd_rtnl_message_route_get_family(message, &tmp->family);
335 if (r < 0) {
336 log_link_warning(link, "rtnl: received route message without family, ignoring");
337 return 0;
338 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
339 log_link_debug(link, "rtnl: received route message with invalid family '%i', ignoring", tmp->family);
340 return 0;
341 }
342
343 r = sd_rtnl_message_route_get_protocol(message, &tmp->protocol);
344 if (r < 0) {
345 log_warning_errno(r, "rtnl: received route message without route protocol: %m");
346 return 0;
347 }
348
349 switch (tmp->family) {
350 case AF_INET:
351 r = sd_netlink_message_read_in_addr(message, RTA_DST, &tmp->dst.in);
352 if (r < 0 && r != -ENODATA) {
353 log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m");
354 return 0;
355 }
356
357 r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &tmp->gw.in);
358 if (r < 0 && r != -ENODATA) {
359 log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m");
360 return 0;
361 }
362
363 r = sd_netlink_message_read_in_addr(message, RTA_SRC, &tmp->src.in);
364 if (r < 0 && r != -ENODATA) {
365 log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m");
366 return 0;
367 }
368
369 r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &tmp->prefsrc.in);
370 if (r < 0 && r != -ENODATA) {
371 log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m");
372 return 0;
373 }
374
375 break;
376
377 case AF_INET6:
378 r = sd_netlink_message_read_in6_addr(message, RTA_DST, &tmp->dst.in6);
379 if (r < 0 && r != -ENODATA) {
380 log_link_warning_errno(link, r, "rtnl: received route message without valid destination, ignoring: %m");
381 return 0;
382 }
383
384 r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &tmp->gw.in6);
385 if (r < 0 && r != -ENODATA) {
386 log_link_warning_errno(link, r, "rtnl: received route message without valid gateway, ignoring: %m");
387 return 0;
388 }
389
390 r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &tmp->src.in6);
391 if (r < 0 && r != -ENODATA) {
392 log_link_warning_errno(link, r, "rtnl: received route message without valid source, ignoring: %m");
393 return 0;
394 }
395
396 r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &tmp->prefsrc.in6);
397 if (r < 0 && r != -ENODATA) {
398 log_link_warning_errno(link, r, "rtnl: received route message without valid preferred source, ignoring: %m");
399 return 0;
400 }
401
402 break;
403
404 default:
405 assert_not_reached("Received route message with unsupported address family");
406 return 0;
407 }
408
409 r = sd_rtnl_message_route_get_dst_prefixlen(message, &tmp->dst_prefixlen);
410 if (r < 0) {
411 log_link_warning_errno(link, r, "rtnl: received route message with invalid destination prefixlen, ignoring: %m");
412 return 0;
413 }
414
415 r = sd_rtnl_message_route_get_src_prefixlen(message, &tmp->src_prefixlen);
416 if (r < 0) {
417 log_link_warning_errno(link, r, "rtnl: received route message with invalid source prefixlen, ignoring: %m");
418 return 0;
419 }
420
421 r = sd_rtnl_message_route_get_scope(message, &tmp->scope);
422 if (r < 0) {
423 log_link_warning_errno(link, r, "rtnl: received route message with invalid scope, ignoring: %m");
424 return 0;
425 }
426
427 r = sd_rtnl_message_route_get_tos(message, &tmp->tos);
428 if (r < 0) {
429 log_link_warning_errno(link, r, "rtnl: received route message with invalid tos, ignoring: %m");
430 return 0;
431 }
432
433 r = sd_rtnl_message_route_get_type(message, &tmp->type);
434 if (r < 0) {
435 log_link_warning_errno(link, r, "rtnl: received route message with invalid type, ignoring: %m");
436 return 0;
437 }
438
439 r = sd_rtnl_message_route_get_table(message, &table);
440 if (r < 0) {
441 log_link_warning_errno(link, r, "rtnl: received route message with invalid table, ignoring: %m");
442 return 0;
443 }
444 tmp->table = table;
445
446 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &tmp->priority);
447 if (r < 0 && r != -ENODATA) {
448 log_link_warning_errno(link, r, "rtnl: received route message with invalid priority, ignoring: %m");
449 return 0;
450 }
451
452 r = sd_netlink_message_enter_container(message, RTA_METRICS);
453 if (r < 0 && r != -ENODATA) {
454 log_link_error_errno(link, r, "rtnl: Could not enter RTA_METRICS container: %m");
455 return 0;
456 }
457 if (r >= 0) {
458 r = sd_netlink_message_read_u32(message, RTAX_INITCWND, &tmp->initcwnd);
459 if (r < 0 && r != -ENODATA) {
460 log_link_warning_errno(link, r, "rtnl: received route message with invalid initcwnd, ignoring: %m");
461 return 0;
462 }
463
464 r = sd_netlink_message_read_u32(message, RTAX_INITRWND, &tmp->initrwnd);
465 if (r < 0 && r != -ENODATA) {
466 log_link_warning_errno(link, r, "rtnl: received route message with invalid initrwnd, ignoring: %m");
467 return 0;
468 }
469
470 r = sd_netlink_message_exit_container(message);
471 if (r < 0) {
472 log_link_error_errno(link, r, "rtnl: Could not exit from RTA_METRICS container: %m");
473 return 0;
474 }
475 }
476
477 (void) route_get(link, tmp, &route);
478
479 if (DEBUG_LOGGING) {
480 _cleanup_free_ char *buf_dst = NULL, *buf_dst_prefixlen = NULL,
481 *buf_src = NULL, *buf_gw = NULL, *buf_prefsrc = NULL;
482 char buf_scope[ROUTE_SCOPE_STR_MAX], buf_table[ROUTE_TABLE_STR_MAX],
483 buf_protocol[ROUTE_PROTOCOL_STR_MAX];
484
485 if (!in_addr_is_null(tmp->family, &tmp->dst)) {
486 (void) in_addr_to_string(tmp->family, &tmp->dst, &buf_dst);
487 (void) asprintf(&buf_dst_prefixlen, "/%u", tmp->dst_prefixlen);
488 }
489 if (!in_addr_is_null(tmp->family, &tmp->src))
490 (void) in_addr_to_string(tmp->family, &tmp->src, &buf_src);
491 if (!in_addr_is_null(tmp->family, &tmp->gw))
492 (void) in_addr_to_string(tmp->family, &tmp->gw, &buf_gw);
493 if (!in_addr_is_null(tmp->family, &tmp->prefsrc))
494 (void) in_addr_to_string(tmp->family, &tmp->prefsrc, &buf_prefsrc);
495
496 log_link_debug(link,
497 "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
498 (!route && !link->manager->manage_foreign_routes) || type == RTM_DELROUTE ? "Forgetting" :
499 route ? "Received remembered" : "Remembering",
500 strna(buf_dst), strempty(buf_dst_prefixlen),
501 strna(buf_src), strna(buf_gw), strna(buf_prefsrc),
502 format_route_scope(tmp->scope, buf_scope, sizeof buf_scope),
503 format_route_table(tmp->table, buf_table, sizeof buf_table),
504 format_route_protocol(tmp->protocol, buf_protocol, sizeof buf_protocol),
505 strna(route_type_to_string(tmp->type)));
506 }
507
508 switch (type) {
509 case RTM_NEWROUTE:
510 if (!route && link->manager->manage_foreign_routes) {
511 /* A route appeared that we did not request */
512 r = route_add_foreign(link, tmp, &route);
513 if (r < 0) {
514 log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m");
515 return 0;
516 }
517 }
518
519 break;
520
521 case RTM_DELROUTE:
522 route_free(route);
523 break;
524
525 default:
526 assert_not_reached("Received route message with invalid RTNL message type");
527 }
528
529 return 1;
530 }
531
532 static int manager_rtnl_process_neighbor_lladdr(sd_netlink_message *message, union lladdr_union *lladdr, size_t *size, char **str) {
533 int r;
534
535 assert(message);
536 assert(lladdr);
537 assert(size);
538 assert(str);
539
540 *str = NULL;
541
542 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in6), &lladdr->ip.in6);
543 if (r >= 0) {
544 *size = sizeof(lladdr->ip.in6);
545 if (in_addr_to_string(AF_INET6, &lladdr->ip, str) < 0)
546 log_warning_errno(r, "Could not print lower address: %m");
547 return r;
548 }
549
550 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->mac), &lladdr->mac);
551 if (r >= 0) {
552 *size = sizeof(lladdr->mac);
553 *str = new(char, ETHER_ADDR_TO_STRING_MAX);
554 if (!*str) {
555 log_oom();
556 return r;
557 }
558 ether_addr_to_string(&lladdr->mac, *str);
559 return r;
560 }
561
562 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in), &lladdr->ip.in);
563 if (r >= 0) {
564 *size = sizeof(lladdr->ip.in);
565 if (in_addr_to_string(AF_INET, &lladdr->ip, str) < 0)
566 log_warning_errno(r, "Could not print lower address: %m");
567 return r;
568 }
569
570 return r;
571 }
572
573 int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
574 Manager *m = userdata;
575 Link *link = NULL;
576 Neighbor *neighbor = NULL;
577 int ifindex, family, r;
578 uint16_t type, state;
579 union in_addr_union in_addr = IN_ADDR_NULL;
580 _cleanup_free_ char *addr_str = NULL;
581 union lladdr_union lladdr;
582 size_t lladdr_size = 0;
583 _cleanup_free_ char *lladdr_str = NULL;
584
585 assert(rtnl);
586 assert(message);
587 assert(m);
588
589 if (sd_netlink_message_is_error(message)) {
590 r = sd_netlink_message_get_errno(message);
591 if (r < 0)
592 log_message_warning_errno(message, r, "rtnl: failed to receive neighbor message, ignoring");
593
594 return 0;
595 }
596
597 r = sd_netlink_message_get_type(message, &type);
598 if (r < 0) {
599 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
600 return 0;
601 } else if (!IN_SET(type, RTM_NEWNEIGH, RTM_DELNEIGH)) {
602 log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type);
603 return 0;
604 }
605
606 r = sd_rtnl_message_neigh_get_state(message, &state);
607 if (r < 0) {
608 log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid state, ignoring: %m");
609 return 0;
610 } else if (!FLAGS_SET(state, NUD_PERMANENT)) {
611 log_debug("rtnl: received non-static neighbor, ignoring.");
612 return 0;
613 }
614
615 r = sd_rtnl_message_neigh_get_ifindex(message, &ifindex);
616 if (r < 0) {
617 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
618 return 0;
619 } else if (ifindex <= 0) {
620 log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex);
621 return 0;
622 }
623
624 r = link_get(m, ifindex, &link);
625 if (r < 0 || !link) {
626 /* when enumerating we might be out of sync, but we will get the neighbor again, so just
627 * ignore it */
628 if (!m->enumerating)
629 log_warning("rtnl: received neighbor for link '%d' we don't know about, ignoring.", ifindex);
630 return 0;
631 }
632
633 r = sd_rtnl_message_neigh_get_family(message, &family);
634 if (r < 0) {
635 log_link_warning(link, "rtnl: received neighbor message without family, ignoring.");
636 return 0;
637 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
638 log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", family);
639 return 0;
640 }
641
642 switch (family) {
643 case AF_INET:
644 r = sd_netlink_message_read_in_addr(message, NDA_DST, &in_addr.in);
645 if (r < 0) {
646 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
647 return 0;
648 }
649
650 break;
651
652 case AF_INET6:
653 r = sd_netlink_message_read_in6_addr(message, NDA_DST, &in_addr.in6);
654 if (r < 0) {
655 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
656 return 0;
657 }
658
659 break;
660
661 default:
662 assert_not_reached("Received unsupported address family");
663 }
664
665 if (in_addr_to_string(family, &in_addr, &addr_str) < 0)
666 log_link_warning_errno(link, r, "Could not print address: %m");
667
668 r = manager_rtnl_process_neighbor_lladdr(message, &lladdr, &lladdr_size, &lladdr_str);
669 if (r < 0) {
670 log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid lladdr, ignoring: %m");
671 return 0;
672 }
673
674 (void) neighbor_get(link, family, &in_addr, &lladdr, lladdr_size, &neighbor);
675
676 switch (type) {
677 case RTM_NEWNEIGH:
678 if (neighbor)
679 log_link_debug(link, "Remembering neighbor: %s->%s",
680 strnull(addr_str), strnull(lladdr_str));
681 else {
682 /* A neighbor appeared that we did not request */
683 r = neighbor_add_foreign(link, family, &in_addr, &lladdr, lladdr_size, &neighbor);
684 if (r < 0) {
685 log_link_warning_errno(link, r, "Failed to remember foreign neighbor %s->%s, ignoring: %m",
686 strnull(addr_str), strnull(lladdr_str));
687 return 0;
688 } else
689 log_link_debug(link, "Remembering foreign neighbor: %s->%s",
690 strnull(addr_str), strnull(lladdr_str));
691 }
692
693 break;
694
695 case RTM_DELNEIGH:
696 if (neighbor) {
697 log_link_debug(link, "Forgetting neighbor: %s->%s",
698 strnull(addr_str), strnull(lladdr_str));
699 (void) neighbor_free(neighbor);
700 } else
701 log_link_debug(link, "Kernel removed a neighbor we don't remember: %s->%s, ignoring.",
702 strnull(addr_str), strnull(lladdr_str));
703
704 break;
705
706 default:
707 assert_not_reached("Received invalid RTNL message type");
708 }
709
710 return 1;
711 }
712
713 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
714 _cleanup_free_ char *buf = NULL;
715 Manager *m = userdata;
716 Link *link = NULL;
717 uint16_t type;
718 unsigned char flags, prefixlen, scope;
719 union in_addr_union in_addr = IN_ADDR_NULL;
720 struct ifa_cacheinfo cinfo;
721 Address *address = NULL;
722 char valid_buf[FORMAT_TIMESPAN_MAX];
723 const char *valid_str = NULL;
724 int ifindex, family, r;
725
726 assert(rtnl);
727 assert(message);
728 assert(m);
729
730 if (sd_netlink_message_is_error(message)) {
731 r = sd_netlink_message_get_errno(message);
732 if (r < 0)
733 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
734
735 return 0;
736 }
737
738 r = sd_netlink_message_get_type(message, &type);
739 if (r < 0) {
740 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
741 return 0;
742 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
743 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
744 return 0;
745 }
746
747 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
748 if (r < 0) {
749 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
750 return 0;
751 } else if (ifindex <= 0) {
752 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
753 return 0;
754 }
755
756 r = link_get(m, ifindex, &link);
757 if (r < 0 || !link) {
758 /* when enumerating we might be out of sync, but we will get the address again, so just
759 * ignore it */
760 if (!m->enumerating)
761 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
762 return 0;
763 }
764
765 r = sd_rtnl_message_addr_get_family(message, &family);
766 if (r < 0) {
767 log_link_warning(link, "rtnl: received address message without family, ignoring.");
768 return 0;
769 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
770 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", family);
771 return 0;
772 }
773
774 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
775 if (r < 0) {
776 log_link_warning_errno(link, r, "rtnl: received address message with invalid prefixlen, ignoring: %m");
777 return 0;
778 }
779
780 r = sd_rtnl_message_addr_get_scope(message, &scope);
781 if (r < 0) {
782 log_link_warning_errno(link, r, "rtnl: received address message with invalid scope, ignoring: %m");
783 return 0;
784 }
785
786 r = sd_rtnl_message_addr_get_flags(message, &flags);
787 if (r < 0) {
788 log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m");
789 return 0;
790 }
791
792 switch (family) {
793 case AF_INET:
794 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
795 if (r < 0) {
796 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
797 return 0;
798 }
799
800 break;
801
802 case AF_INET6:
803 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
804 if (r < 0) {
805 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
806 return 0;
807 }
808
809 break;
810
811 default:
812 assert_not_reached("Received unsupported address family");
813 }
814
815 r = in_addr_to_string(family, &in_addr, &buf);
816 if (r < 0)
817 log_link_warning_errno(link, r, "Could not print address: %m");
818
819 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
820 if (r < 0 && r != -ENODATA) {
821 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
822 return 0;
823 } else if (r >= 0 && cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
824 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
825 cinfo.ifa_valid * USEC_PER_SEC,
826 USEC_PER_SEC);
827
828 (void) address_get(link, family, &in_addr, prefixlen, &address);
829
830 switch (type) {
831 case RTM_NEWADDR:
832 if (address)
833 log_link_debug(link, "Remembering updated address: %s/%u (valid %s%s)",
834 strnull(buf), prefixlen,
835 valid_str ? "for " : "forever", strempty(valid_str));
836 else {
837 /* An address appeared that we did not request */
838 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
839 if (r < 0) {
840 log_link_warning_errno(link, r, "Failed to remember foreign address %s/%u, ignoring: %m",
841 strnull(buf), prefixlen);
842 return 0;
843 } else
844 log_link_debug(link, "Remembering foreign address: %s/%u (valid %s%s)",
845 strnull(buf), prefixlen,
846 valid_str ? "for " : "forever", strempty(valid_str));
847 }
848
849 /* address_update() logs internally, so we don't need to. */
850 (void) address_update(address, flags, scope, &cinfo);
851
852 break;
853
854 case RTM_DELADDR:
855 if (address) {
856 log_link_debug(link, "Forgetting address: %s/%u (valid %s%s)",
857 strnull(buf), prefixlen,
858 valid_str ? "for " : "forever", strempty(valid_str));
859 (void) address_drop(address);
860 } else
861 log_link_debug(link, "Kernel removed an address we don't remember: %s/%u (valid %s%s), ignoring.",
862 strnull(buf), prefixlen,
863 valid_str ? "for " : "forever", strempty(valid_str));
864
865 break;
866
867 default:
868 assert_not_reached("Received invalid RTNL message type");
869 }
870
871 return 1;
872 }
873
874 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
875 Manager *m = userdata;
876 Link *link = NULL;
877 NetDev *netdev = NULL;
878 uint16_t type;
879 const char *name;
880 int r, ifindex;
881
882 assert(rtnl);
883 assert(message);
884 assert(m);
885
886 if (sd_netlink_message_is_error(message)) {
887 r = sd_netlink_message_get_errno(message);
888 if (r < 0)
889 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
890
891 return 0;
892 }
893
894 r = sd_netlink_message_get_type(message, &type);
895 if (r < 0) {
896 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
897 return 0;
898 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
899 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
900 return 0;
901 }
902
903 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
904 if (r < 0) {
905 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
906 return 0;
907 } else if (ifindex <= 0) {
908 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
909 return 0;
910 }
911
912 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
913 if (r < 0) {
914 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
915 return 0;
916 }
917
918 (void) link_get(m, ifindex, &link);
919 (void) netdev_get(m, name, &netdev);
920
921 switch (type) {
922 case RTM_NEWLINK:
923 if (!link) {
924 /* link is new, so add it */
925 r = link_add(m, message, &link);
926 if (r < 0) {
927 log_warning_errno(r, "Could not process new link message, ignoring: %m");
928 return 0;
929 }
930 }
931
932 if (netdev) {
933 /* netdev exists, so make sure the ifindex matches */
934 r = netdev_set_ifindex(netdev, message);
935 if (r < 0) {
936 log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m");
937 return 0;
938 }
939 }
940
941 r = link_update(link, message);
942 if (r < 0) {
943 log_warning_errno(r, "Could not process link message, ignoring: %m");
944 return 0;
945 }
946
947 break;
948
949 case RTM_DELLINK:
950 link_drop(link);
951 netdev_drop(netdev);
952
953 break;
954
955 default:
956 assert_not_reached("Received link message with invalid RTNL message type.");
957 }
958
959 return 1;
960 }
961
962 int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
963 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *tmp = NULL;
964 _cleanup_free_ char *from = NULL, *to = NULL;
965 RoutingPolicyRule *rule = NULL;
966 const char *iif = NULL, *oif = NULL;
967 uint32_t suppress_prefixlen;
968 Manager *m = userdata;
969 unsigned flags;
970 uint16_t type;
971 int r;
972
973 assert(rtnl);
974 assert(message);
975 assert(m);
976
977 if (sd_netlink_message_is_error(message)) {
978 r = sd_netlink_message_get_errno(message);
979 if (r < 0)
980 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
981
982 return 0;
983 }
984
985 r = sd_netlink_message_get_type(message, &type);
986 if (r < 0) {
987 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
988 return 0;
989 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
990 log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type);
991 return 0;
992 }
993
994 r = routing_policy_rule_new(&tmp);
995 if (r < 0) {
996 log_oom();
997 return 0;
998 }
999
1000 r = sd_rtnl_message_get_family(message, &tmp->family);
1001 if (r < 0) {
1002 log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
1003 return 0;
1004 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1005 log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family);
1006 return 0;
1007 }
1008
1009 switch (tmp->family) {
1010 case AF_INET:
1011 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &tmp->from.in);
1012 if (r < 0 && r != -ENODATA) {
1013 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1014 return 0;
1015 } else if (r >= 0) {
1016 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1017 if (r < 0) {
1018 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1019 return 0;
1020 }
1021 }
1022
1023 r = sd_netlink_message_read_in_addr(message, FRA_DST, &tmp->to.in);
1024 if (r < 0 && r != -ENODATA) {
1025 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1026 return 0;
1027 } else if (r >= 0) {
1028 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1029 if (r < 0) {
1030 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1031 return 0;
1032 }
1033 }
1034
1035 break;
1036
1037 case AF_INET6:
1038 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &tmp->from.in6);
1039 if (r < 0 && r != -ENODATA) {
1040 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1041 return 0;
1042 } else if (r >= 0) {
1043 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1044 if (r < 0) {
1045 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1046 return 0;
1047 }
1048 }
1049
1050 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &tmp->to.in6);
1051 if (r < 0 && r != -ENODATA) {
1052 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1053 return 0;
1054 } else if (r >= 0) {
1055 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1056 if (r < 0) {
1057 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1058 return 0;
1059 }
1060 }
1061
1062 break;
1063
1064 default:
1065 assert_not_reached("Received rule message with unsupported address family");
1066 }
1067
1068 if (tmp->from_prefixlen == 0 && tmp->to_prefixlen == 0)
1069 return 0;
1070
1071 r = sd_rtnl_message_routing_policy_rule_get_flags(message, &flags);
1072 if (r < 0) {
1073 log_warning_errno(r, "rtnl: received rule message without valid flag, ignoring: %m");
1074 return 0;
1075 }
1076 tmp->invert_rule = flags & FIB_RULE_INVERT;
1077
1078 r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark);
1079 if (r < 0 && r != -ENODATA) {
1080 log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
1081 return 0;
1082 }
1083
1084 r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask);
1085 if (r < 0 && r != -ENODATA) {
1086 log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
1087 return 0;
1088 }
1089
1090 r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority);
1091 if (r < 0 && r != -ENODATA) {
1092 log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
1093 return 0;
1094 }
1095
1096 r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table);
1097 if (r < 0 && r != -ENODATA) {
1098 log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
1099 return 0;
1100 }
1101
1102 r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos);
1103 if (r < 0 && r != -ENODATA) {
1104 log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m");
1105 return 0;
1106 }
1107
1108 r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
1109 if (r < 0 && r != -ENODATA) {
1110 log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
1111 return 0;
1112 }
1113 r = free_and_strdup(&tmp->iif, iif);
1114 if (r < 0)
1115 return log_oom();
1116
1117 r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
1118 if (r < 0 && r != -ENODATA) {
1119 log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
1120 return 0;
1121 }
1122 r = free_and_strdup(&tmp->oif, oif);
1123 if (r < 0)
1124 return log_oom();
1125
1126 r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->protocol);
1127 if (r < 0 && r != -ENODATA) {
1128 log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
1129 return 0;
1130 }
1131
1132 r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport);
1133 if (r < 0 && r != -ENODATA) {
1134 log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
1135 return 0;
1136 }
1137
1138 r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport);
1139 if (r < 0 && r != -ENODATA) {
1140 log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
1141 return 0;
1142 }
1143
1144 r = sd_netlink_message_read(message, FRA_UID_RANGE, sizeof(tmp->uid_range), &tmp->uid_range);
1145 if (r < 0 && r != -ENODATA) {
1146 log_warning_errno(r, "rtnl: could not get FRA_UID_RANGE attribute, ignoring: %m");
1147 return 0;
1148 }
1149
1150 r = sd_netlink_message_read_u32(message, FRA_SUPPRESS_PREFIXLEN, &suppress_prefixlen);
1151 if (r < 0 && r != -ENODATA) {
1152 log_warning_errno(r, "rtnl: could not get FRA_SUPPRESS_PREFIXLEN attribute, ignoring: %m");
1153 return 0;
1154 }
1155 if (r >= 0)
1156 tmp->suppress_prefixlen = (int) suppress_prefixlen;
1157
1158 (void) routing_policy_rule_get(m, tmp, &rule);
1159
1160 if (DEBUG_LOGGING) {
1161 (void) in_addr_to_string(tmp->family, &tmp->from, &from);
1162 (void) in_addr_to_string(tmp->family, &tmp->to, &to);
1163 }
1164
1165 switch (type) {
1166 case RTM_NEWRULE:
1167 if (!rule) {
1168 log_debug("Remembering foreign routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1169 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1170 r = routing_policy_rule_add_foreign(m, tmp, &rule);
1171 if (r < 0) {
1172 log_warning_errno(r, "Could not remember foreign rule, ignoring: %m");
1173 return 0;
1174 }
1175 }
1176 break;
1177 case RTM_DELRULE:
1178 log_debug("Forgetting routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1179 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1180 routing_policy_rule_free(rule);
1181
1182 break;
1183
1184 default:
1185 assert_not_reached("Received invalid RTNL message type");
1186 }
1187
1188 return 1;
1189 }
1190
1191 int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
1192 _cleanup_(nexthop_freep) NextHop *tmp = NULL;
1193 _cleanup_free_ char *gateway = NULL;
1194 NextHop *nexthop = NULL;
1195 Manager *m = userdata;
1196 Link *link = NULL;
1197 uint16_t type;
1198 int r;
1199
1200 assert(rtnl);
1201 assert(message);
1202 assert(m);
1203
1204 if (sd_netlink_message_is_error(message)) {
1205 r = sd_netlink_message_get_errno(message);
1206 if (r < 0)
1207 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
1208
1209 return 0;
1210 }
1211
1212 r = sd_netlink_message_get_type(message, &type);
1213 if (r < 0) {
1214 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1215 return 0;
1216 } else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) {
1217 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type);
1218 return 0;
1219 }
1220
1221 r = nexthop_new(&tmp);
1222 if (r < 0)
1223 return log_oom();
1224
1225 r = sd_rtnl_message_get_family(message, &tmp->family);
1226 if (r < 0) {
1227 log_warning_errno(r, "rtnl: could not get nexthop family, ignoring: %m");
1228 return 0;
1229 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1230 log_debug("rtnl: received nexthop message with invalid family %d, ignoring.", tmp->family);
1231 return 0;
1232 }
1233
1234 switch (tmp->family) {
1235 case AF_INET:
1236 r = sd_netlink_message_read_in_addr(message, NHA_GATEWAY, &tmp->gw.in);
1237 if (r < 0 && r != -ENODATA) {
1238 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1239 return 0;
1240 }
1241 break;
1242
1243 case AF_INET6:
1244 r = sd_netlink_message_read_in6_addr(message, NHA_GATEWAY, &tmp->gw.in6);
1245 if (r < 0 && r != -ENODATA) {
1246 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1247 return 0;
1248 }
1249 break;
1250
1251 default:
1252 assert_not_reached("Received rule message with unsupported address family");
1253 }
1254
1255 r = sd_netlink_message_read_u32(message, NHA_ID, &tmp->id);
1256 if (r < 0 && r != -ENODATA) {
1257 log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1258 return 0;
1259 }
1260
1261 r = sd_netlink_message_read_u32(message, NHA_OIF, &tmp->oif);
1262 if (r < 0 && r != -ENODATA) {
1263 log_warning_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1264 return 0;
1265 }
1266
1267 r = link_get(m, tmp->oif, &link);
1268 if (r < 0 || !link) {
1269 if (!m->enumerating)
1270 log_warning("rtnl: received nexthop message for link (%d) we do not know about, ignoring", tmp->oif);
1271 return 0;
1272 }
1273
1274 (void) nexthop_get(link, tmp, &nexthop);
1275
1276 if (DEBUG_LOGGING)
1277 (void) in_addr_to_string(tmp->family, &tmp->gw, &gateway);
1278
1279 switch (type) {
1280 case RTM_NEWNEXTHOP:
1281 if (!nexthop) {
1282 log_debug("Remembering foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1283 r = nexthop_add_foreign(link, tmp, &nexthop);
1284 if (r < 0) {
1285 log_warning_errno(r, "Could not remember foreign nexthop, ignoring: %m");
1286 return 0;
1287 }
1288 }
1289 break;
1290 case RTM_DELNEXTHOP:
1291 log_debug("Forgetting foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1292 nexthop_free(nexthop);
1293
1294 break;
1295
1296 default:
1297 assert_not_reached("Received invalid RTNL message type");
1298 }
1299
1300 return 1;
1301 }
1302
1303 static int systemd_netlink_fd(void) {
1304 int n, fd, rtnl_fd = -EINVAL;
1305
1306 n = sd_listen_fds(true);
1307 if (n <= 0)
1308 return -EINVAL;
1309
1310 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
1311 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
1312 if (rtnl_fd >= 0)
1313 return -EINVAL;
1314
1315 rtnl_fd = fd;
1316 }
1317 }
1318
1319 return rtnl_fd;
1320 }
1321
1322 static int manager_connect_genl(Manager *m) {
1323 int r;
1324
1325 assert(m);
1326
1327 r = sd_genl_socket_open(&m->genl);
1328 if (r < 0)
1329 return r;
1330
1331 r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE);
1332 if (r < 0)
1333 return r;
1334
1335 r = sd_netlink_attach_event(m->genl, m->event, 0);
1336 if (r < 0)
1337 return r;
1338
1339 return 0;
1340 }
1341
1342 static int manager_connect_rtnl(Manager *m) {
1343 int fd, r;
1344
1345 assert(m);
1346
1347 fd = systemd_netlink_fd();
1348 if (fd < 0)
1349 r = sd_netlink_open(&m->rtnl);
1350 else
1351 r = sd_netlink_open_fd(&m->rtnl, fd);
1352 if (r < 0)
1353 return r;
1354
1355 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
1356 if (r < 0)
1357 return r;
1358
1359 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
1360 if (r < 0)
1361 return r;
1362
1363 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1364 if (r < 0)
1365 return r;
1366
1367 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1368 if (r < 0)
1369 return r;
1370
1371 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1372 if (r < 0)
1373 return r;
1374
1375 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1376 if (r < 0)
1377 return r;
1378
1379 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1380 if (r < 0)
1381 return r;
1382
1383 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1384 if (r < 0)
1385 return r;
1386
1387 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1388 if (r < 0)
1389 return r;
1390
1391 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1392 if (r < 0)
1393 return r;
1394
1395 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1396 if (r < 0)
1397 return r;
1398
1399 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1400 if (r < 0)
1401 return r;
1402
1403 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1404 if (r < 0)
1405 return r;
1406
1407 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1408 if (r < 0)
1409 return r;
1410
1411 return 0;
1412 }
1413
1414 static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
1415 char *p;
1416 int r;
1417
1418 assert(s);
1419 assert(address);
1420
1421 r = in_addr_to_string(address->family, &address->address, &p);
1422 if (r < 0)
1423 return r;
1424
1425 r = ordered_set_consume(s, p);
1426 if (r == -EEXIST)
1427 return 0;
1428
1429 return r;
1430 }
1431
1432 static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
1433 int r, c = 0;
1434 unsigned i;
1435
1436 assert(s);
1437 assert(addresses || n == 0);
1438
1439 for (i = 0; i < n; i++) {
1440 r = ordered_set_put_in_addr_data(s, addresses+i);
1441 if (r < 0)
1442 return r;
1443
1444 c += r;
1445 }
1446
1447 return c;
1448 }
1449
1450 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
1451 char *p;
1452 int r;
1453
1454 assert(s);
1455 assert(address);
1456
1457 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
1458 if (r < 0)
1459 return r;
1460
1461 r = ordered_set_consume(s, p);
1462 if (r == -EEXIST)
1463 return 0;
1464
1465 return r;
1466 }
1467
1468 static int ordered_set_put_in4_addrv(OrderedSet *s,
1469 const struct in_addr *addresses,
1470 size_t n,
1471 bool (*predicate)(const struct in_addr *addr)) {
1472 int r, c = 0;
1473 size_t i;
1474
1475 assert(s);
1476 assert(n == 0 || addresses);
1477
1478 for (i = 0; i < n; i++) {
1479 if (predicate && !predicate(&addresses[i]))
1480 continue;
1481 r = ordered_set_put_in4_addr(s, addresses+i);
1482 if (r < 0)
1483 return r;
1484
1485 c += r;
1486 }
1487
1488 return c;
1489 }
1490
1491 static int manager_save(Manager *m) {
1492 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *sip = NULL, *pop3 = NULL,
1493 *search_domains = NULL, *route_domains = NULL;
1494 const char *operstate_str, *carrier_state_str, *address_state_str;
1495 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1496 LinkCarrierState carrier_state = LINK_CARRIER_STATE_OFF;
1497 LinkAddressState address_state = LINK_ADDRESS_STATE_OFF;
1498 _cleanup_free_ char *temp_path = NULL;
1499 _cleanup_strv_free_ char **p = NULL;
1500 _cleanup_fclose_ FILE *f = NULL;
1501 const struct in_addr *addresses;
1502 Link *link;
1503 Iterator i;
1504 int r;
1505
1506 assert(m);
1507 assert(m->state_file);
1508
1509 /* We add all NTP and DNS server to a set, to filter out duplicates */
1510 dns = ordered_set_new(&string_hash_ops);
1511 if (!dns)
1512 return -ENOMEM;
1513
1514 ntp = ordered_set_new(&string_hash_ops);
1515 if (!ntp)
1516 return -ENOMEM;
1517
1518 sip = ordered_set_new(&string_hash_ops);
1519 if (!sip)
1520 return -ENOMEM;
1521
1522 pop3 = ordered_set_new(&string_hash_ops);
1523 if (!pop3)
1524 return -ENOMEM;
1525
1526 search_domains = ordered_set_new(&dns_name_hash_ops);
1527 if (!search_domains)
1528 return -ENOMEM;
1529
1530 route_domains = ordered_set_new(&dns_name_hash_ops);
1531 if (!route_domains)
1532 return -ENOMEM;
1533
1534 HASHMAP_FOREACH(link, m->links, i) {
1535 if (link->flags & IFF_LOOPBACK)
1536 continue;
1537
1538 if (link->operstate > operstate)
1539 operstate = link->operstate;
1540
1541 if (link->carrier_state > carrier_state)
1542 carrier_state = link->carrier_state;
1543
1544 if (link->address_state > address_state)
1545 address_state = link->address_state;
1546
1547 if (!link->network)
1548 continue;
1549
1550 /* First add the static configured entries */
1551 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
1552 if (r < 0)
1553 return r;
1554
1555 r = ordered_set_put_strdupv(ntp, link->ntp ?: link->network->ntp);
1556 if (r < 0)
1557 return r;
1558
1559 r = ordered_set_put_string_set(search_domains, link->search_domains ?: link->network->search_domains);
1560 if (r < 0)
1561 return r;
1562
1563 r = ordered_set_put_string_set(route_domains, link->route_domains ?: link->network->route_domains);
1564 if (r < 0)
1565 return r;
1566
1567 if (!link->dhcp_lease)
1568 continue;
1569
1570 /* Secondly, add the entries acquired via DHCP */
1571 if (link->network->dhcp_use_dns) {
1572 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1573 if (r > 0) {
1574 r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local);
1575 if (r < 0)
1576 return r;
1577 } else if (r < 0 && r != -ENODATA)
1578 return r;
1579 }
1580
1581 if (link->network->dhcp_use_ntp) {
1582 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1583 if (r > 0) {
1584 r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local);
1585 if (r < 0)
1586 return r;
1587 } else if (r < 0 && r != -ENODATA)
1588 return r;
1589 }
1590
1591 if (link->network->dhcp_use_sip) {
1592 r = sd_dhcp_lease_get_sip(link->dhcp_lease, &addresses);
1593 if (r > 0) {
1594 r = ordered_set_put_in4_addrv(sip, addresses, r, in4_addr_is_non_local);
1595 if (r < 0)
1596 return r;
1597 } else if (r < 0 && r != -ENODATA)
1598 return r;
1599 }
1600
1601
1602 r = sd_dhcp_lease_get_pop3_server(link->dhcp_lease, &addresses);
1603 if (r > 0) {
1604 r = ordered_set_put_in4_addrv(pop3, addresses, r, in4_addr_is_non_local);
1605 if (r < 0)
1606 return r;
1607 } else if (r < 0 && r != -ENODATA)
1608 return r;
1609
1610 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1611 const char *domainname;
1612 char **domains = NULL;
1613
1614 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
1615 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1616 if (r >= 0) {
1617 r = ordered_set_put_strdup(target_domains, domainname);
1618 if (r < 0)
1619 return r;
1620 } else if (r != -ENODATA)
1621 return r;
1622
1623 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1624 if (r >= 0) {
1625 r = ordered_set_put_strdupv(target_domains, domains);
1626 if (r < 0)
1627 return r;
1628 } else if (r != -ENODATA)
1629 return r;
1630 }
1631 }
1632
1633 if (carrier_state >= LINK_CARRIER_STATE_ENSLAVED)
1634 carrier_state = LINK_CARRIER_STATE_CARRIER;
1635
1636 operstate_str = link_operstate_to_string(operstate);
1637 assert(operstate_str);
1638
1639 carrier_state_str = link_carrier_state_to_string(carrier_state);
1640 assert(carrier_state_str);
1641
1642 address_state_str = link_address_state_to_string(address_state);
1643 assert(address_state_str);
1644
1645 r = fopen_temporary(m->state_file, &f, &temp_path);
1646 if (r < 0)
1647 return r;
1648
1649 (void) fchmod(fileno(f), 0644);
1650
1651 fprintf(f,
1652 "# This is private data. Do not parse.\n"
1653 "OPER_STATE=%s\n"
1654 "CARRIER_STATE=%s\n"
1655 "ADDRESS_STATE=%s\n",
1656 operstate_str, carrier_state_str, address_state_str);
1657
1658 ordered_set_print(f, "DNS=", dns);
1659 ordered_set_print(f, "NTP=", ntp);
1660 ordered_set_print(f, "SIP=", sip);
1661 ordered_set_print(f, "POP3_SERVERS=", pop3);
1662 ordered_set_print(f, "DOMAINS=", search_domains);
1663 ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
1664
1665 r = routing_policy_serialize_rules(m->rules, f);
1666 if (r < 0)
1667 goto fail;
1668
1669 r = fflush_and_check(f);
1670 if (r < 0)
1671 goto fail;
1672
1673 if (rename(temp_path, m->state_file) < 0) {
1674 r = -errno;
1675 goto fail;
1676 }
1677
1678 if (m->operational_state != operstate) {
1679 m->operational_state = operstate;
1680 if (strv_extend(&p, "OperationalState") < 0)
1681 log_oom();
1682 }
1683
1684 if (m->carrier_state != carrier_state) {
1685 m->carrier_state = carrier_state;
1686 if (strv_extend(&p, "CarrierState") < 0)
1687 log_oom();
1688 }
1689
1690 if (m->address_state != address_state) {
1691 m->address_state = address_state;
1692 if (strv_extend(&p, "AddressState") < 0)
1693 log_oom();
1694 }
1695
1696 if (p) {
1697 r = manager_send_changed_strv(m, p);
1698 if (r < 0)
1699 log_error_errno(r, "Could not emit changed properties: %m");
1700 }
1701
1702 m->dirty = false;
1703
1704 return 0;
1705
1706 fail:
1707 (void) unlink(m->state_file);
1708 (void) unlink(temp_path);
1709
1710 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1711 }
1712
1713 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1714 Manager *m = userdata;
1715 Link *link;
1716 Iterator i;
1717
1718 assert(m);
1719
1720 if (m->dirty)
1721 manager_save(m);
1722
1723 SET_FOREACH(link, m->dirty_links, i)
1724 if (link_save(link) >= 0)
1725 link_clean(link);
1726
1727 return 1;
1728 }
1729
1730 static int signal_terminate_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1731 Manager *m = userdata;
1732
1733 assert(m);
1734 m->restarting = false;
1735
1736 log_debug("Terminate operation initiated.");
1737
1738 return sd_event_exit(sd_event_source_get_event(s), 0);
1739 }
1740
1741 static int signal_restart_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1742 Manager *m = userdata;
1743
1744 assert(m);
1745 m->restarting = true;
1746
1747 log_debug("Restart operation initiated.");
1748
1749 return sd_event_exit(sd_event_source_get_event(s), 0);
1750 }
1751
1752 int manager_new(Manager **ret) {
1753 _cleanup_(manager_freep) Manager *m = NULL;
1754 int r;
1755
1756 m = new(Manager, 1);
1757 if (!m)
1758 return -ENOMEM;
1759
1760 *m = (Manager) {
1761 .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL,
1762 .manage_foreign_routes = true,
1763 };
1764
1765 m->state_file = strdup("/run/systemd/netif/state");
1766 if (!m->state_file)
1767 return -ENOMEM;
1768
1769 r = sd_event_default(&m->event);
1770 if (r < 0)
1771 return r;
1772
1773 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR2, -1) >= 0);
1774
1775 (void) sd_event_set_watchdog(m->event, true);
1776 (void) sd_event_add_signal(m->event, NULL, SIGTERM, signal_terminate_callback, m);
1777 (void) sd_event_add_signal(m->event, NULL, SIGINT, signal_terminate_callback, m);
1778 (void) sd_event_add_signal(m->event, NULL, SIGUSR2, signal_restart_callback, m);
1779
1780 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1781 if (r < 0)
1782 return r;
1783
1784 r = manager_connect_rtnl(m);
1785 if (r < 0)
1786 return r;
1787
1788 r = manager_connect_genl(m);
1789 if (r < 0)
1790 return r;
1791
1792 r = manager_connect_udev(m);
1793 if (r < 0)
1794 return r;
1795
1796 r = sd_resolve_default(&m->resolve);
1797 if (r < 0)
1798 return r;
1799
1800 r = sd_resolve_attach_event(m->resolve, m->event, 0);
1801 if (r < 0)
1802 return r;
1803
1804 r = setup_default_address_pool(m);
1805 if (r < 0)
1806 return r;
1807
1808 m->duid.type = DUID_TYPE_EN;
1809
1810 (void) routing_policy_load_rules(m->state_file, &m->rules_saved);
1811
1812 *ret = TAKE_PTR(m);
1813
1814 return 0;
1815 }
1816
1817 void manager_free(Manager *m) {
1818 struct in6_addr *a;
1819 AddressPool *pool;
1820 Link *link;
1821
1822 if (!m)
1823 return;
1824
1825 free(m->state_file);
1826
1827 while ((a = hashmap_first_key(m->dhcp6_prefixes)))
1828 (void) dhcp6_prefix_remove(m, a);
1829 m->dhcp6_prefixes = hashmap_free(m->dhcp6_prefixes);
1830
1831 while ((link = hashmap_steal_first(m->links))) {
1832 if (link->dhcp6_client)
1833 (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link);
1834
1835 (void) link_stop_clients(link, true);
1836
1837 link_unref(link);
1838 }
1839
1840 m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
1841 m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref);
1842 m->links = hashmap_free_with_destructor(m->links, link_unref);
1843
1844 m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
1845 m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
1846
1847 m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
1848
1849 while ((pool = m->address_pools))
1850 address_pool_free(pool);
1851
1852 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1853 * So, it is necessary to set NULL after the sets are freed. */
1854 m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free);
1855 m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free);
1856 set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
1857
1858 sd_netlink_unref(m->rtnl);
1859 sd_netlink_unref(m->genl);
1860 sd_resolve_unref(m->resolve);
1861
1862 sd_event_source_unref(m->speed_meter_event_source);
1863 sd_event_unref(m->event);
1864
1865 sd_device_monitor_unref(m->device_monitor);
1866
1867 bus_verify_polkit_async_registry_free(m->polkit_registry);
1868 sd_bus_flush_close_unref(m->bus);
1869
1870 free(m->dynamic_timezone);
1871 free(m->dynamic_hostname);
1872
1873 free(m);
1874 }
1875
1876 int manager_start(Manager *m) {
1877 Link *link;
1878 Iterator i;
1879 int r;
1880
1881 assert(m);
1882
1883 r = manager_start_speed_meter(m);
1884 if (r < 0)
1885 return log_error_errno(r, "Failed to initialize speed meter: %m");
1886
1887 /* The dirty handler will deal with future serialization, but the first one
1888 must be done explicitly. */
1889
1890 manager_save(m);
1891
1892 HASHMAP_FOREACH(link, m->links, i)
1893 link_save(link);
1894
1895 return 0;
1896 }
1897
1898 int manager_load_config(Manager *m) {
1899 int r;
1900
1901 /* update timestamp */
1902 paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true);
1903
1904 r = netdev_load(m, false);
1905 if (r < 0)
1906 return r;
1907
1908 r = network_load(m, &m->networks);
1909 if (r < 0)
1910 return r;
1911
1912 return 0;
1913 }
1914
1915 bool manager_should_reload(Manager *m) {
1916 return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false);
1917 }
1918
1919 int manager_rtnl_enumerate_links(Manager *m) {
1920 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1921 sd_netlink_message *link;
1922 int r;
1923
1924 assert(m);
1925 assert(m->rtnl);
1926
1927 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1928 if (r < 0)
1929 return r;
1930
1931 r = sd_netlink_message_request_dump(req, true);
1932 if (r < 0)
1933 return r;
1934
1935 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1936 if (r < 0)
1937 return r;
1938
1939 for (link = reply; link; link = sd_netlink_message_next(link)) {
1940 int k;
1941
1942 m->enumerating = true;
1943
1944 k = manager_rtnl_process_link(m->rtnl, link, m);
1945 if (k < 0)
1946 r = k;
1947
1948 m->enumerating = false;
1949 }
1950
1951 return r;
1952 }
1953
1954 int manager_rtnl_enumerate_addresses(Manager *m) {
1955 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1956 sd_netlink_message *addr;
1957 int r;
1958
1959 assert(m);
1960 assert(m->rtnl);
1961
1962 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1963 if (r < 0)
1964 return r;
1965
1966 r = sd_netlink_message_request_dump(req, true);
1967 if (r < 0)
1968 return r;
1969
1970 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1971 if (r < 0)
1972 return r;
1973
1974 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1975 int k;
1976
1977 m->enumerating = true;
1978
1979 k = manager_rtnl_process_address(m->rtnl, addr, m);
1980 if (k < 0)
1981 r = k;
1982
1983 m->enumerating = false;
1984 }
1985
1986 return r;
1987 }
1988
1989 int manager_rtnl_enumerate_neighbors(Manager *m) {
1990 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1991 sd_netlink_message *neigh;
1992 int r;
1993
1994 assert(m);
1995 assert(m->rtnl);
1996
1997 r = sd_rtnl_message_new_neigh(m->rtnl, &req, RTM_GETNEIGH, 0, AF_UNSPEC);
1998 if (r < 0)
1999 return r;
2000
2001 r = sd_netlink_message_request_dump(req, true);
2002 if (r < 0)
2003 return r;
2004
2005 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2006 if (r < 0)
2007 return r;
2008
2009 for (neigh = reply; neigh; neigh = sd_netlink_message_next(neigh)) {
2010 int k;
2011
2012 m->enumerating = true;
2013
2014 k = manager_rtnl_process_neighbor(m->rtnl, neigh, m);
2015 if (k < 0)
2016 r = k;
2017
2018 m->enumerating = false;
2019 }
2020
2021 return r;
2022 }
2023
2024 int manager_rtnl_enumerate_routes(Manager *m) {
2025 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2026 sd_netlink_message *route;
2027 int r;
2028
2029 assert(m);
2030 assert(m->rtnl);
2031
2032 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
2033 if (r < 0)
2034 return r;
2035
2036 r = sd_netlink_message_request_dump(req, true);
2037 if (r < 0)
2038 return r;
2039
2040 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2041 if (r < 0)
2042 return r;
2043
2044 for (route = reply; route; route = sd_netlink_message_next(route)) {
2045 int k;
2046
2047 m->enumerating = true;
2048
2049 k = manager_rtnl_process_route(m->rtnl, route, m);
2050 if (k < 0)
2051 r = k;
2052
2053 m->enumerating = false;
2054 }
2055
2056 return r;
2057 }
2058
2059 int manager_rtnl_enumerate_rules(Manager *m) {
2060 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2061 sd_netlink_message *rule;
2062 int r;
2063
2064 assert(m);
2065 assert(m->rtnl);
2066
2067 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0);
2068 if (r < 0)
2069 return r;
2070
2071 r = sd_netlink_message_request_dump(req, true);
2072 if (r < 0)
2073 return r;
2074
2075 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2076 if (r < 0) {
2077 if (r == -EOPNOTSUPP) {
2078 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
2079 return 0;
2080 }
2081
2082 return r;
2083 }
2084
2085 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
2086 int k;
2087
2088 m->enumerating = true;
2089
2090 k = manager_rtnl_process_rule(m->rtnl, rule, m);
2091 if (k < 0)
2092 r = k;
2093
2094 m->enumerating = false;
2095 }
2096
2097 return r;
2098 }
2099
2100 int manager_rtnl_enumerate_nexthop(Manager *m) {
2101 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2102 sd_netlink_message *nexthop;
2103 int r;
2104
2105 assert(m);
2106 assert(m->rtnl);
2107
2108 r = sd_rtnl_message_new_nexthop(m->rtnl, &req, RTM_GETNEXTHOP, 0, 0);
2109 if (r < 0)
2110 return r;
2111
2112 r = sd_netlink_message_request_dump(req, true);
2113 if (r < 0)
2114 return r;
2115
2116 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2117 if (r < 0) {
2118 if (r == -EOPNOTSUPP) {
2119 log_debug("Nexthop are not supported by the kernel. Ignoring.");
2120 return 0;
2121 }
2122
2123 return r;
2124 }
2125
2126 for (nexthop = reply; nexthop; nexthop = sd_netlink_message_next(nexthop)) {
2127 int k;
2128
2129 m->enumerating = true;
2130
2131 k = manager_rtnl_process_nexthop(m->rtnl, nexthop, m);
2132 if (k < 0)
2133 r = k;
2134
2135 m->enumerating = false;
2136 }
2137
2138 return r;
2139 }
2140
2141 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
2142 AddressPool *p;
2143 int r;
2144
2145 assert(m);
2146 assert(prefixlen > 0);
2147 assert(found);
2148
2149 LIST_FOREACH(address_pools, p, m->address_pools) {
2150 if (p->family != family)
2151 continue;
2152
2153 r = address_pool_acquire(p, prefixlen, found);
2154 if (r != 0)
2155 return r;
2156 }
2157
2158 return 0;
2159 }
2160
2161 Link* manager_find_uplink(Manager *m, Link *exclude) {
2162 _cleanup_free_ struct local_address *gateways = NULL;
2163 int n, i;
2164
2165 assert(m);
2166
2167 /* Looks for a suitable "uplink", via black magic: an
2168 * interface that is up and where the default route with the
2169 * highest priority points to. */
2170
2171 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
2172 if (n < 0) {
2173 log_warning_errno(n, "Failed to determine list of default gateways: %m");
2174 return NULL;
2175 }
2176
2177 for (i = 0; i < n; i++) {
2178 Link *link;
2179
2180 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
2181 if (!link) {
2182 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
2183 continue;
2184 }
2185
2186 if (link == exclude)
2187 continue;
2188
2189 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
2190 continue;
2191
2192 return link;
2193 }
2194
2195 return NULL;
2196 }
2197
2198 void manager_dirty(Manager *manager) {
2199 assert(manager);
2200
2201 /* the serialized state in /run is no longer up-to-date */
2202 manager->dirty = true;
2203 }
2204
2205 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2206 Manager *manager = userdata;
2207 const sd_bus_error *e;
2208
2209 assert(m);
2210 assert(manager);
2211
2212 e = sd_bus_message_get_error(m);
2213 if (e)
2214 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
2215
2216 return 1;
2217 }
2218
2219 int manager_set_hostname(Manager *m, const char *hostname) {
2220 int r;
2221
2222 log_debug("Setting transient hostname: '%s'", strna(hostname));
2223
2224 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
2225 return log_oom();
2226
2227 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2228 log_debug("Not connected to system bus, setting hostname later.");
2229 return 0;
2230 }
2231
2232 r = sd_bus_call_method_async(
2233 m->bus,
2234 NULL,
2235 "org.freedesktop.hostname1",
2236 "/org/freedesktop/hostname1",
2237 "org.freedesktop.hostname1",
2238 "SetHostname",
2239 set_hostname_handler,
2240 m,
2241 "sb",
2242 hostname,
2243 false);
2244
2245 if (r < 0)
2246 return log_error_errno(r, "Could not set transient hostname: %m");
2247
2248 return 0;
2249 }
2250
2251 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2252 Manager *manager = userdata;
2253 const sd_bus_error *e;
2254
2255 assert(m);
2256 assert(manager);
2257
2258 e = sd_bus_message_get_error(m);
2259 if (e)
2260 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
2261
2262 return 1;
2263 }
2264
2265 int manager_set_timezone(Manager *m, const char *tz) {
2266 int r;
2267
2268 assert(m);
2269 assert(tz);
2270
2271 log_debug("Setting system timezone: '%s'", tz);
2272 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
2273 return log_oom();
2274
2275 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2276 log_debug("Not connected to system bus, setting timezone later.");
2277 return 0;
2278 }
2279
2280 r = sd_bus_call_method_async(
2281 m->bus,
2282 NULL,
2283 "org.freedesktop.timedate1",
2284 "/org/freedesktop/timedate1",
2285 "org.freedesktop.timedate1",
2286 "SetTimezone",
2287 set_timezone_handler,
2288 m,
2289 "sb",
2290 tz,
2291 false);
2292 if (r < 0)
2293 return log_error_errno(r, "Could not set timezone: %m");
2294
2295 return 0;
2296 }
2297
2298 int manager_request_product_uuid(Manager *m, Link *link) {
2299 int r;
2300
2301 assert(m);
2302
2303 if (m->has_product_uuid)
2304 return 0;
2305
2306 log_debug("Requesting product UUID");
2307
2308 if (link) {
2309 DUID *duid;
2310
2311 assert_se(duid = link_get_duid(link));
2312
2313 r = set_ensure_allocated(&m->links_requesting_uuid, NULL);
2314 if (r < 0)
2315 return log_oom();
2316
2317 r = set_ensure_allocated(&m->duids_requesting_uuid, NULL);
2318 if (r < 0)
2319 return log_oom();
2320
2321 r = set_put(m->links_requesting_uuid, link);
2322 if (r < 0)
2323 return log_oom();
2324
2325 r = set_put(m->duids_requesting_uuid, duid);
2326 if (r < 0)
2327 return log_oom();
2328
2329 link_ref(link);
2330 }
2331
2332 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2333 log_debug("Not connected to system bus, requesting product UUID later.");
2334 return 0;
2335 }
2336
2337 r = sd_bus_call_method_async(
2338 m->bus,
2339 NULL,
2340 "org.freedesktop.hostname1",
2341 "/org/freedesktop/hostname1",
2342 "org.freedesktop.hostname1",
2343 "GetProductUUID",
2344 get_product_uuid_handler,
2345 m,
2346 "b",
2347 false);
2348 if (r < 0)
2349 return log_warning_errno(r, "Failed to get product UUID: %m");
2350
2351 return 0;
2352 }