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