]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
network: set key destructor in several hash_ops
[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.");
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) ? "Ignoring received foreign" :
509 type == RTM_DELROUTE ? "Forgetting" :
510 route ? "Received remembered" : "Remembering",
511 strna(buf_dst), strempty(buf_dst_prefixlen),
512 strna(buf_src), strna(buf_gw), strna(buf_prefsrc),
513 format_route_scope(tmp->scope, buf_scope, sizeof buf_scope),
514 format_route_table(tmp->table, buf_table, sizeof buf_table),
515 format_route_protocol(tmp->protocol, buf_protocol, sizeof buf_protocol),
516 strna(route_type_to_string(tmp->type)));
517 }
518
519 switch (type) {
520 case RTM_NEWROUTE:
521 if (!route && link->manager->manage_foreign_routes) {
522 /* A route appeared that we did not request */
523 r = route_add_foreign(link, tmp, &route);
524 if (r < 0) {
525 log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m");
526 return 0;
527 }
528 }
529
530 break;
531
532 case RTM_DELROUTE:
533 route_free(route);
534 break;
535
536 default:
537 assert_not_reached("Received route message with invalid RTNL message type");
538 }
539
540 return 1;
541 }
542
543 static int manager_rtnl_process_neighbor_lladdr(sd_netlink_message *message, union lladdr_union *lladdr, size_t *size, char **str) {
544 int r;
545
546 assert(message);
547 assert(lladdr);
548 assert(size);
549 assert(str);
550
551 *str = NULL;
552
553 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in6), &lladdr->ip.in6);
554 if (r >= 0) {
555 *size = sizeof(lladdr->ip.in6);
556 if (in_addr_to_string(AF_INET6, &lladdr->ip, str) < 0)
557 log_warning_errno(r, "Could not print lower address: %m");
558 return r;
559 }
560
561 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->mac), &lladdr->mac);
562 if (r >= 0) {
563 *size = sizeof(lladdr->mac);
564 *str = new(char, ETHER_ADDR_TO_STRING_MAX);
565 if (!*str) {
566 log_oom();
567 return r;
568 }
569 ether_addr_to_string(&lladdr->mac, *str);
570 return r;
571 }
572
573 r = sd_netlink_message_read(message, NDA_LLADDR, sizeof(lladdr->ip.in), &lladdr->ip.in);
574 if (r >= 0) {
575 *size = sizeof(lladdr->ip.in);
576 if (in_addr_to_string(AF_INET, &lladdr->ip, str) < 0)
577 log_warning_errno(r, "Could not print lower address: %m");
578 return r;
579 }
580
581 return r;
582 }
583
584 int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
585 Manager *m = userdata;
586 Link *link = NULL;
587 Neighbor *neighbor = NULL;
588 int ifindex, family, r;
589 uint16_t type, state;
590 union in_addr_union in_addr = IN_ADDR_NULL;
591 _cleanup_free_ char *addr_str = NULL;
592 union lladdr_union lladdr;
593 size_t lladdr_size = 0;
594 _cleanup_free_ char *lladdr_str = NULL;
595
596 assert(rtnl);
597 assert(message);
598 assert(m);
599
600 if (sd_netlink_message_is_error(message)) {
601 r = sd_netlink_message_get_errno(message);
602 if (r < 0)
603 log_message_warning_errno(message, r, "rtnl: failed to receive neighbor message, ignoring");
604
605 return 0;
606 }
607
608 r = sd_netlink_message_get_type(message, &type);
609 if (r < 0) {
610 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
611 return 0;
612 } else if (!IN_SET(type, RTM_NEWNEIGH, RTM_DELNEIGH)) {
613 log_warning("rtnl: received unexpected message type %u when processing neighbor, ignoring.", type);
614 return 0;
615 }
616
617 r = sd_rtnl_message_neigh_get_state(message, &state);
618 if (r < 0) {
619 log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid state, ignoring: %m");
620 return 0;
621 } else if (!FLAGS_SET(state, NUD_PERMANENT)) {
622 log_debug("rtnl: received non-static neighbor, ignoring.");
623 return 0;
624 }
625
626 r = sd_rtnl_message_neigh_get_ifindex(message, &ifindex);
627 if (r < 0) {
628 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
629 return 0;
630 } else if (ifindex <= 0) {
631 log_warning("rtnl: received neighbor message with invalid ifindex %d, ignoring.", ifindex);
632 return 0;
633 }
634
635 r = link_get(m, ifindex, &link);
636 if (r < 0 || !link) {
637 /* when enumerating we might be out of sync, but we will get the neighbor again, so just
638 * ignore it */
639 if (!m->enumerating)
640 log_warning("rtnl: received neighbor for link '%d' we don't know about, ignoring.", ifindex);
641 return 0;
642 }
643
644 r = sd_rtnl_message_neigh_get_family(message, &family);
645 if (r < 0) {
646 log_link_warning(link, "rtnl: received neighbor message without family, ignoring.");
647 return 0;
648 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
649 log_link_debug(link, "rtnl: received neighbor message with invalid family '%i', ignoring.", family);
650 return 0;
651 }
652
653 switch (family) {
654 case AF_INET:
655 r = sd_netlink_message_read_in_addr(message, NDA_DST, &in_addr.in);
656 if (r < 0) {
657 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
658 return 0;
659 }
660
661 break;
662
663 case AF_INET6:
664 r = sd_netlink_message_read_in6_addr(message, NDA_DST, &in_addr.in6);
665 if (r < 0) {
666 log_link_warning_errno(link, r, "rtnl: received neighbor message without valid address, ignoring: %m");
667 return 0;
668 }
669
670 break;
671
672 default:
673 assert_not_reached("Received unsupported address family");
674 }
675
676 if (in_addr_to_string(family, &in_addr, &addr_str) < 0)
677 log_link_warning_errno(link, r, "Could not print address: %m");
678
679 r = manager_rtnl_process_neighbor_lladdr(message, &lladdr, &lladdr_size, &lladdr_str);
680 if (r < 0) {
681 log_link_warning_errno(link, r, "rtnl: received neighbor message with invalid lladdr, ignoring: %m");
682 return 0;
683 }
684
685 (void) neighbor_get(link, family, &in_addr, &lladdr, lladdr_size, &neighbor);
686
687 switch (type) {
688 case RTM_NEWNEIGH:
689 if (neighbor)
690 log_link_debug(link, "Remembering neighbor: %s->%s",
691 strnull(addr_str), strnull(lladdr_str));
692 else {
693 /* A neighbor appeared that we did not request */
694 r = neighbor_add_foreign(link, family, &in_addr, &lladdr, lladdr_size, &neighbor);
695 if (r < 0) {
696 log_link_warning_errno(link, r, "Failed to remember foreign neighbor %s->%s, ignoring: %m",
697 strnull(addr_str), strnull(lladdr_str));
698 return 0;
699 } else
700 log_link_debug(link, "Remembering foreign neighbor: %s->%s",
701 strnull(addr_str), strnull(lladdr_str));
702 }
703
704 break;
705
706 case RTM_DELNEIGH:
707 if (neighbor) {
708 log_link_debug(link, "Forgetting neighbor: %s->%s",
709 strnull(addr_str), strnull(lladdr_str));
710 (void) neighbor_free(neighbor);
711 } else
712 log_link_debug(link, "Kernel removed a neighbor we don't remember: %s->%s, ignoring.",
713 strnull(addr_str), strnull(lladdr_str));
714
715 break;
716
717 default:
718 assert_not_reached("Received invalid RTNL message type");
719 }
720
721 return 1;
722 }
723
724 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
725 _cleanup_free_ char *buf = NULL;
726 Manager *m = userdata;
727 Link *link = NULL;
728 uint16_t type;
729 unsigned char flags, prefixlen, scope;
730 union in_addr_union in_addr = IN_ADDR_NULL;
731 struct ifa_cacheinfo cinfo;
732 Address *address = NULL;
733 char valid_buf[FORMAT_TIMESPAN_MAX];
734 const char *valid_str = NULL;
735 int ifindex, family, r;
736
737 assert(rtnl);
738 assert(message);
739 assert(m);
740
741 if (sd_netlink_message_is_error(message)) {
742 r = sd_netlink_message_get_errno(message);
743 if (r < 0)
744 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
745
746 return 0;
747 }
748
749 r = sd_netlink_message_get_type(message, &type);
750 if (r < 0) {
751 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
752 return 0;
753 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
754 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
755 return 0;
756 }
757
758 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
759 if (r < 0) {
760 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
761 return 0;
762 } else if (ifindex <= 0) {
763 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
764 return 0;
765 }
766
767 r = link_get(m, ifindex, &link);
768 if (r < 0 || !link) {
769 /* when enumerating we might be out of sync, but we will get the address again, so just
770 * ignore it */
771 if (!m->enumerating)
772 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
773 return 0;
774 }
775
776 r = sd_rtnl_message_addr_get_family(message, &family);
777 if (r < 0) {
778 log_link_warning(link, "rtnl: received address message without family, ignoring.");
779 return 0;
780 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
781 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", family);
782 return 0;
783 }
784
785 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
786 if (r < 0) {
787 log_link_warning_errno(link, r, "rtnl: received address message with invalid prefixlen, ignoring: %m");
788 return 0;
789 }
790
791 r = sd_rtnl_message_addr_get_scope(message, &scope);
792 if (r < 0) {
793 log_link_warning_errno(link, r, "rtnl: received address message with invalid scope, ignoring: %m");
794 return 0;
795 }
796
797 r = sd_rtnl_message_addr_get_flags(message, &flags);
798 if (r < 0) {
799 log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m");
800 return 0;
801 }
802
803 switch (family) {
804 case AF_INET:
805 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
806 if (r < 0) {
807 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
808 return 0;
809 }
810
811 break;
812
813 case AF_INET6:
814 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
815 if (r < 0) {
816 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
817 return 0;
818 }
819
820 break;
821
822 default:
823 assert_not_reached("Received unsupported address family");
824 }
825
826 r = in_addr_to_string(family, &in_addr, &buf);
827 if (r < 0)
828 log_link_warning_errno(link, r, "Could not print address: %m");
829
830 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
831 if (r < 0 && r != -ENODATA) {
832 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
833 return 0;
834 } else if (r >= 0 && cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
835 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
836 cinfo.ifa_valid * USEC_PER_SEC,
837 USEC_PER_SEC);
838
839 (void) address_get(link, family, &in_addr, prefixlen, &address);
840
841 switch (type) {
842 case RTM_NEWADDR:
843 if (address)
844 log_link_debug(link, "Remembering updated address: %s/%u (valid %s%s)",
845 strnull(buf), prefixlen,
846 valid_str ? "for " : "forever", strempty(valid_str));
847 else {
848 /* An address appeared that we did not request */
849 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
850 if (r < 0) {
851 log_link_warning_errno(link, r, "Failed to remember foreign address %s/%u, ignoring: %m",
852 strnull(buf), prefixlen);
853 return 0;
854 } else
855 log_link_debug(link, "Remembering foreign address: %s/%u (valid %s%s)",
856 strnull(buf), prefixlen,
857 valid_str ? "for " : "forever", strempty(valid_str));
858 }
859
860 /* address_update() logs internally, so we don't need to here. */
861 r = address_update(address, flags, scope, &cinfo);
862 if (r < 0)
863 link_enter_failed(link);
864
865 break;
866
867 case RTM_DELADDR:
868 if (address) {
869 log_link_debug(link, "Forgetting address: %s/%u (valid %s%s)",
870 strnull(buf), prefixlen,
871 valid_str ? "for " : "forever", strempty(valid_str));
872 (void) address_drop(address);
873 } else
874 log_link_debug(link, "Kernel removed an address we don't remember: %s/%u (valid %s%s), ignoring.",
875 strnull(buf), prefixlen,
876 valid_str ? "for " : "forever", strempty(valid_str));
877
878 break;
879
880 default:
881 assert_not_reached("Received invalid RTNL message type");
882 }
883
884 return 1;
885 }
886
887 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
888 Manager *m = userdata;
889 Link *link = NULL;
890 NetDev *netdev = NULL;
891 uint16_t type;
892 const char *name;
893 int r, ifindex;
894
895 assert(rtnl);
896 assert(message);
897 assert(m);
898
899 if (sd_netlink_message_is_error(message)) {
900 r = sd_netlink_message_get_errno(message);
901 if (r < 0)
902 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
903
904 return 0;
905 }
906
907 r = sd_netlink_message_get_type(message, &type);
908 if (r < 0) {
909 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
910 return 0;
911 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
912 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
913 return 0;
914 }
915
916 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
917 if (r < 0) {
918 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
919 return 0;
920 } else if (ifindex <= 0) {
921 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
922 return 0;
923 }
924
925 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
926 if (r < 0) {
927 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
928 return 0;
929 }
930
931 (void) link_get(m, ifindex, &link);
932 (void) netdev_get(m, name, &netdev);
933
934 switch (type) {
935 case RTM_NEWLINK:
936 if (!link) {
937 /* link is new, so add it */
938 r = link_add(m, message, &link);
939 if (r < 0) {
940 log_warning_errno(r, "Could not process new link message, ignoring: %m");
941 return 0;
942 }
943 }
944
945 if (netdev) {
946 /* netdev exists, so make sure the ifindex matches */
947 r = netdev_set_ifindex(netdev, message);
948 if (r < 0) {
949 log_warning_errno(r, "Could not process new link message for netdev, ignoring: %m");
950 return 0;
951 }
952 }
953
954 r = link_update(link, message);
955 if (r < 0) {
956 log_warning_errno(r, "Could not process link message, ignoring: %m");
957 return 0;
958 }
959
960 break;
961
962 case RTM_DELLINK:
963 link_drop(link);
964 netdev_drop(netdev);
965
966 break;
967
968 default:
969 assert_not_reached("Received link message with invalid RTNL message type.");
970 }
971
972 return 1;
973 }
974
975 int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
976 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *tmp = NULL;
977 _cleanup_free_ char *from = NULL, *to = NULL;
978 RoutingPolicyRule *rule = NULL;
979 const char *iif = NULL, *oif = NULL;
980 uint32_t suppress_prefixlen;
981 Manager *m = userdata;
982 unsigned flags;
983 uint16_t type;
984 int r;
985
986 assert(rtnl);
987 assert(message);
988 assert(m);
989
990 if (sd_netlink_message_is_error(message)) {
991 r = sd_netlink_message_get_errno(message);
992 if (r < 0)
993 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
994
995 return 0;
996 }
997
998 r = sd_netlink_message_get_type(message, &type);
999 if (r < 0) {
1000 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1001 return 0;
1002 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
1003 log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type);
1004 return 0;
1005 }
1006
1007 r = routing_policy_rule_new(&tmp);
1008 if (r < 0) {
1009 log_oom();
1010 return 0;
1011 }
1012
1013 r = sd_rtnl_message_get_family(message, &tmp->family);
1014 if (r < 0) {
1015 log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
1016 return 0;
1017 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1018 log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family);
1019 return 0;
1020 }
1021
1022 switch (tmp->family) {
1023 case AF_INET:
1024 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &tmp->from.in);
1025 if (r < 0 && r != -ENODATA) {
1026 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1027 return 0;
1028 } else if (r >= 0) {
1029 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1030 if (r < 0) {
1031 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1032 return 0;
1033 }
1034 }
1035
1036 r = sd_netlink_message_read_in_addr(message, FRA_DST, &tmp->to.in);
1037 if (r < 0 && r != -ENODATA) {
1038 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1039 return 0;
1040 } else if (r >= 0) {
1041 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1042 if (r < 0) {
1043 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1044 return 0;
1045 }
1046 }
1047
1048 break;
1049
1050 case AF_INET6:
1051 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &tmp->from.in6);
1052 if (r < 0 && r != -ENODATA) {
1053 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1054 return 0;
1055 } else if (r >= 0) {
1056 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1057 if (r < 0) {
1058 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1059 return 0;
1060 }
1061 }
1062
1063 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &tmp->to.in6);
1064 if (r < 0 && r != -ENODATA) {
1065 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1066 return 0;
1067 } else if (r >= 0) {
1068 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1069 if (r < 0) {
1070 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1071 return 0;
1072 }
1073 }
1074
1075 break;
1076
1077 default:
1078 assert_not_reached("Received rule message with unsupported address family");
1079 }
1080
1081 if (tmp->from_prefixlen == 0 && tmp->to_prefixlen == 0)
1082 return 0;
1083
1084 r = sd_rtnl_message_routing_policy_rule_get_flags(message, &flags);
1085 if (r < 0) {
1086 log_warning_errno(r, "rtnl: received rule message without valid flag, ignoring: %m");
1087 return 0;
1088 }
1089 tmp->invert_rule = flags & FIB_RULE_INVERT;
1090
1091 r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark);
1092 if (r < 0 && r != -ENODATA) {
1093 log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
1094 return 0;
1095 }
1096
1097 r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask);
1098 if (r < 0 && r != -ENODATA) {
1099 log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
1100 return 0;
1101 }
1102
1103 r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority);
1104 if (r < 0 && r != -ENODATA) {
1105 log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
1106 return 0;
1107 }
1108
1109 r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table);
1110 if (r < 0 && r != -ENODATA) {
1111 log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
1112 return 0;
1113 }
1114
1115 r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos);
1116 if (r < 0 && r != -ENODATA) {
1117 log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m");
1118 return 0;
1119 }
1120
1121 r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
1122 if (r < 0 && r != -ENODATA) {
1123 log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
1124 return 0;
1125 }
1126 r = free_and_strdup(&tmp->iif, iif);
1127 if (r < 0)
1128 return log_oom();
1129
1130 r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
1131 if (r < 0 && r != -ENODATA) {
1132 log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
1133 return 0;
1134 }
1135 r = free_and_strdup(&tmp->oif, oif);
1136 if (r < 0)
1137 return log_oom();
1138
1139 r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->protocol);
1140 if (r < 0 && r != -ENODATA) {
1141 log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
1142 return 0;
1143 }
1144
1145 r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport);
1146 if (r < 0 && r != -ENODATA) {
1147 log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
1148 return 0;
1149 }
1150
1151 r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport);
1152 if (r < 0 && r != -ENODATA) {
1153 log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
1154 return 0;
1155 }
1156
1157 r = sd_netlink_message_read(message, FRA_UID_RANGE, sizeof(tmp->uid_range), &tmp->uid_range);
1158 if (r < 0 && r != -ENODATA) {
1159 log_warning_errno(r, "rtnl: could not get FRA_UID_RANGE attribute, ignoring: %m");
1160 return 0;
1161 }
1162
1163 r = sd_netlink_message_read_u32(message, FRA_SUPPRESS_PREFIXLEN, &suppress_prefixlen);
1164 if (r < 0 && r != -ENODATA) {
1165 log_warning_errno(r, "rtnl: could not get FRA_SUPPRESS_PREFIXLEN attribute, ignoring: %m");
1166 return 0;
1167 }
1168 if (r >= 0)
1169 tmp->suppress_prefixlen = (int) suppress_prefixlen;
1170
1171 (void) routing_policy_rule_get(m, tmp, &rule);
1172
1173 if (DEBUG_LOGGING) {
1174 (void) in_addr_to_string(tmp->family, &tmp->from, &from);
1175 (void) in_addr_to_string(tmp->family, &tmp->to, &to);
1176 }
1177
1178 switch (type) {
1179 case RTM_NEWRULE:
1180 if (!rule) {
1181 log_debug("Remembering foreign routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1182 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1183 r = routing_policy_rule_add_foreign(m, tmp, &rule);
1184 if (r < 0) {
1185 log_warning_errno(r, "Could not remember foreign rule, ignoring: %m");
1186 return 0;
1187 }
1188 }
1189 break;
1190 case RTM_DELRULE:
1191 log_debug("Forgetting routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1192 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1193 routing_policy_rule_free(rule);
1194
1195 break;
1196
1197 default:
1198 assert_not_reached("Received invalid RTNL message type");
1199 }
1200
1201 return 1;
1202 }
1203
1204 int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
1205 _cleanup_(nexthop_freep) NextHop *tmp = NULL;
1206 _cleanup_free_ char *gateway = NULL;
1207 NextHop *nexthop = NULL;
1208 Manager *m = userdata;
1209 Link *link = NULL;
1210 uint16_t type;
1211 int r;
1212
1213 assert(rtnl);
1214 assert(message);
1215 assert(m);
1216
1217 if (sd_netlink_message_is_error(message)) {
1218 r = sd_netlink_message_get_errno(message);
1219 if (r < 0)
1220 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
1221
1222 return 0;
1223 }
1224
1225 r = sd_netlink_message_get_type(message, &type);
1226 if (r < 0) {
1227 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1228 return 0;
1229 } else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) {
1230 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type);
1231 return 0;
1232 }
1233
1234 r = nexthop_new(&tmp);
1235 if (r < 0)
1236 return log_oom();
1237
1238 r = sd_rtnl_message_get_family(message, &tmp->family);
1239 if (r < 0) {
1240 log_warning_errno(r, "rtnl: could not get nexthop family, ignoring: %m");
1241 return 0;
1242 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1243 log_debug("rtnl: received nexthop message with invalid family %d, ignoring.", tmp->family);
1244 return 0;
1245 }
1246
1247 switch (tmp->family) {
1248 case AF_INET:
1249 r = sd_netlink_message_read_in_addr(message, NHA_GATEWAY, &tmp->gw.in);
1250 if (r < 0 && r != -ENODATA) {
1251 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1252 return 0;
1253 }
1254 break;
1255
1256 case AF_INET6:
1257 r = sd_netlink_message_read_in6_addr(message, NHA_GATEWAY, &tmp->gw.in6);
1258 if (r < 0 && r != -ENODATA) {
1259 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1260 return 0;
1261 }
1262 break;
1263
1264 default:
1265 assert_not_reached("Received rule message with unsupported address family");
1266 }
1267
1268 r = sd_netlink_message_read_u32(message, NHA_ID, &tmp->id);
1269 if (r < 0 && r != -ENODATA) {
1270 log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1271 return 0;
1272 }
1273
1274 r = sd_netlink_message_read_u32(message, NHA_OIF, &tmp->oif);
1275 if (r < 0 && r != -ENODATA) {
1276 log_warning_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1277 return 0;
1278 } else if (tmp->oif <= 0) {
1279 log_warning("rtnl: received nexthop message with invalid ifindex %d, ignoring.", tmp->oif);
1280 return 0;
1281 }
1282
1283 r = link_get(m, tmp->oif, &link);
1284 if (r < 0 || !link) {
1285 if (!m->enumerating)
1286 log_warning("rtnl: received nexthop message for link (%d) we do not know about, ignoring", tmp->oif);
1287 return 0;
1288 }
1289
1290 (void) nexthop_get(link, tmp, &nexthop);
1291
1292 if (DEBUG_LOGGING)
1293 (void) in_addr_to_string(tmp->family, &tmp->gw, &gateway);
1294
1295 switch (type) {
1296 case RTM_NEWNEXTHOP:
1297 if (!nexthop) {
1298 log_debug("Remembering foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1299 r = nexthop_add_foreign(link, tmp, &nexthop);
1300 if (r < 0) {
1301 log_warning_errno(r, "Could not remember foreign nexthop, ignoring: %m");
1302 return 0;
1303 }
1304 }
1305 break;
1306 case RTM_DELNEXTHOP:
1307 log_debug("Forgetting foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1308 nexthop_free(nexthop);
1309
1310 break;
1311
1312 default:
1313 assert_not_reached("Received invalid RTNL message type");
1314 }
1315
1316 return 1;
1317 }
1318
1319 static int systemd_netlink_fd(void) {
1320 int n, fd, rtnl_fd = -EINVAL;
1321
1322 n = sd_listen_fds(true);
1323 if (n <= 0)
1324 return -EINVAL;
1325
1326 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
1327 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
1328 if (rtnl_fd >= 0)
1329 return -EINVAL;
1330
1331 rtnl_fd = fd;
1332 }
1333 }
1334
1335 return rtnl_fd;
1336 }
1337
1338 static int manager_connect_genl(Manager *m) {
1339 int r;
1340
1341 assert(m);
1342
1343 r = sd_genl_socket_open(&m->genl);
1344 if (r < 0)
1345 return r;
1346
1347 r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE);
1348 if (r < 0)
1349 return r;
1350
1351 r = sd_netlink_attach_event(m->genl, m->event, 0);
1352 if (r < 0)
1353 return r;
1354
1355 return 0;
1356 }
1357
1358 static int manager_connect_rtnl(Manager *m) {
1359 int fd, r;
1360
1361 assert(m);
1362
1363 fd = systemd_netlink_fd();
1364 if (fd < 0)
1365 r = sd_netlink_open(&m->rtnl);
1366 else
1367 r = sd_netlink_open_fd(&m->rtnl, fd);
1368 if (r < 0)
1369 return r;
1370
1371 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
1372 if (r < 0)
1373 return r;
1374
1375 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
1376 if (r < 0)
1377 return r;
1378
1379 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1380 if (r < 0)
1381 return r;
1382
1383 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1384 if (r < 0)
1385 return r;
1386
1387 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1388 if (r < 0)
1389 return r;
1390
1391 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1392 if (r < 0)
1393 return r;
1394
1395 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1396 if (r < 0)
1397 return r;
1398
1399 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1400 if (r < 0)
1401 return r;
1402
1403 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1404 if (r < 0)
1405 return r;
1406
1407 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1408 if (r < 0)
1409 return r;
1410
1411 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1412 if (r < 0)
1413 return r;
1414
1415 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1416 if (r < 0)
1417 return r;
1418
1419 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1420 if (r < 0)
1421 return r;
1422
1423 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1424 if (r < 0)
1425 return r;
1426
1427 return 0;
1428 }
1429
1430 static int ordered_set_put_dns_server(OrderedSet *s, int ifindex, struct in_addr_full *dns) {
1431 const char *p;
1432 int r;
1433
1434 assert(s);
1435 assert(dns);
1436
1437 if (dns->ifindex != 0 && dns->ifindex != ifindex)
1438 return 0;
1439
1440 p = in_addr_full_to_string(dns);
1441 if (!p)
1442 return 0;
1443
1444 r = ordered_set_put_strdup(s, p);
1445 if (r == -EEXIST)
1446 return 0;
1447
1448 return r;
1449 }
1450
1451 static int ordered_set_put_dns_servers(OrderedSet *s, int ifindex, struct in_addr_full **dns, unsigned n) {
1452 int r, c = 0;
1453 unsigned i;
1454
1455 assert(s);
1456 assert(dns || n == 0);
1457
1458 for (i = 0; i < n; i++) {
1459 r = ordered_set_put_dns_server(s, ifindex, dns[i]);
1460 if (r < 0)
1461 return r;
1462
1463 c += r;
1464 }
1465
1466 return c;
1467 }
1468
1469 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
1470 char *p;
1471 int r;
1472
1473 assert(s);
1474 assert(address);
1475
1476 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
1477 if (r < 0)
1478 return r;
1479
1480 r = ordered_set_consume(s, p);
1481 if (r == -EEXIST)
1482 return 0;
1483
1484 return r;
1485 }
1486
1487 static int ordered_set_put_in4_addrv(OrderedSet *s,
1488 const struct in_addr *addresses,
1489 size_t n,
1490 bool (*predicate)(const struct in_addr *addr)) {
1491 int r, c = 0;
1492 size_t i;
1493
1494 assert(s);
1495 assert(n == 0 || addresses);
1496
1497 for (i = 0; i < n; i++) {
1498 if (predicate && !predicate(&addresses[i]))
1499 continue;
1500 r = ordered_set_put_in4_addr(s, addresses+i);
1501 if (r < 0)
1502 return r;
1503
1504 c += r;
1505 }
1506
1507 return c;
1508 }
1509
1510 static int manager_save(Manager *m) {
1511 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *sip = NULL, *search_domains = NULL, *route_domains = NULL;
1512 const char *operstate_str, *carrier_state_str, *address_state_str;
1513 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1514 LinkCarrierState carrier_state = LINK_CARRIER_STATE_OFF;
1515 LinkAddressState address_state = LINK_ADDRESS_STATE_OFF;
1516 _cleanup_free_ char *temp_path = NULL;
1517 _cleanup_strv_free_ char **p = NULL;
1518 _cleanup_fclose_ FILE *f = NULL;
1519 Link *link;
1520 Iterator i;
1521 int r;
1522
1523 assert(m);
1524 assert(m->state_file);
1525
1526 /* We add all NTP and DNS server to a set, to filter out duplicates */
1527 dns = ordered_set_new(&string_hash_ops);
1528 if (!dns)
1529 return -ENOMEM;
1530
1531 ntp = ordered_set_new(&string_hash_ops);
1532 if (!ntp)
1533 return -ENOMEM;
1534
1535 sip = ordered_set_new(&string_hash_ops);
1536 if (!sip)
1537 return -ENOMEM;
1538
1539 search_domains = ordered_set_new(&dns_name_hash_ops);
1540 if (!search_domains)
1541 return -ENOMEM;
1542
1543 route_domains = ordered_set_new(&dns_name_hash_ops);
1544 if (!route_domains)
1545 return -ENOMEM;
1546
1547 HASHMAP_FOREACH(link, m->links, i) {
1548 const struct in_addr *addresses;
1549
1550 if (link->flags & IFF_LOOPBACK)
1551 continue;
1552
1553 if (link->operstate > operstate)
1554 operstate = link->operstate;
1555
1556 if (link->carrier_state > carrier_state)
1557 carrier_state = link->carrier_state;
1558
1559 if (link->address_state > address_state)
1560 address_state = link->address_state;
1561
1562 if (!link->network)
1563 continue;
1564
1565 /* First add the static configured entries */
1566 if (link->n_dns != (unsigned) -1)
1567 r = ordered_set_put_dns_servers(dns, link->ifindex, link->dns, link->n_dns);
1568 else
1569 r = ordered_set_put_dns_servers(dns, link->ifindex, link->network->dns, link->network->n_dns);
1570 if (r < 0)
1571 return r;
1572
1573 r = ordered_set_put_strdupv(ntp, link->ntp ?: link->network->ntp);
1574 if (r < 0)
1575 return r;
1576
1577 r = ordered_set_put_string_set(search_domains, link->search_domains ?: link->network->search_domains);
1578 if (r < 0)
1579 return r;
1580
1581 r = ordered_set_put_string_set(route_domains, link->route_domains ?: link->network->route_domains);
1582 if (r < 0)
1583 return r;
1584
1585 if (!link->dhcp_lease)
1586 continue;
1587
1588 /* Secondly, add the entries acquired via DHCP */
1589 if (link->network->dhcp_use_dns) {
1590 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1591 if (r > 0) {
1592 r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local);
1593 if (r < 0)
1594 return r;
1595 } else if (r < 0 && r != -ENODATA)
1596 return r;
1597 }
1598
1599 if (link->network->dhcp_use_ntp) {
1600 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1601 if (r > 0) {
1602 r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local);
1603 if (r < 0)
1604 return r;
1605 } else if (r < 0 && r != -ENODATA)
1606 return r;
1607 }
1608
1609 if (link->network->dhcp_use_sip) {
1610 r = sd_dhcp_lease_get_sip(link->dhcp_lease, &addresses);
1611 if (r > 0) {
1612 r = ordered_set_put_in4_addrv(sip, addresses, r, in4_addr_is_non_local);
1613 if (r < 0)
1614 return r;
1615 } else if (r < 0 && r != -ENODATA)
1616 return r;
1617 }
1618
1619 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1620 const char *domainname;
1621 char **domains = NULL;
1622
1623 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
1624 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1625 if (r >= 0) {
1626 r = ordered_set_put_strdup(target_domains, domainname);
1627 if (r < 0)
1628 return r;
1629 } else if (r != -ENODATA)
1630 return r;
1631
1632 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1633 if (r >= 0) {
1634 r = ordered_set_put_strdupv(target_domains, domains);
1635 if (r < 0)
1636 return r;
1637 } else if (r != -ENODATA)
1638 return r;
1639 }
1640 }
1641
1642 if (carrier_state >= LINK_CARRIER_STATE_ENSLAVED)
1643 carrier_state = LINK_CARRIER_STATE_CARRIER;
1644
1645 operstate_str = link_operstate_to_string(operstate);
1646 assert(operstate_str);
1647
1648 carrier_state_str = link_carrier_state_to_string(carrier_state);
1649 assert(carrier_state_str);
1650
1651 address_state_str = link_address_state_to_string(address_state);
1652 assert(address_state_str);
1653
1654 r = fopen_temporary(m->state_file, &f, &temp_path);
1655 if (r < 0)
1656 return r;
1657
1658 (void) fchmod(fileno(f), 0644);
1659
1660 fprintf(f,
1661 "# This is private data. Do not parse.\n"
1662 "OPER_STATE=%s\n"
1663 "CARRIER_STATE=%s\n"
1664 "ADDRESS_STATE=%s\n",
1665 operstate_str, carrier_state_str, address_state_str);
1666
1667 ordered_set_print(f, "DNS=", dns);
1668 ordered_set_print(f, "NTP=", ntp);
1669 ordered_set_print(f, "SIP=", sip);
1670 ordered_set_print(f, "DOMAINS=", search_domains);
1671 ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
1672
1673 r = routing_policy_serialize_rules(m->rules, f);
1674 if (r < 0)
1675 goto fail;
1676
1677 r = fflush_and_check(f);
1678 if (r < 0)
1679 goto fail;
1680
1681 if (rename(temp_path, m->state_file) < 0) {
1682 r = -errno;
1683 goto fail;
1684 }
1685
1686 if (m->operational_state != operstate) {
1687 m->operational_state = operstate;
1688 if (strv_extend(&p, "OperationalState") < 0)
1689 log_oom();
1690 }
1691
1692 if (m->carrier_state != carrier_state) {
1693 m->carrier_state = carrier_state;
1694 if (strv_extend(&p, "CarrierState") < 0)
1695 log_oom();
1696 }
1697
1698 if (m->address_state != address_state) {
1699 m->address_state = address_state;
1700 if (strv_extend(&p, "AddressState") < 0)
1701 log_oom();
1702 }
1703
1704 if (p) {
1705 r = manager_send_changed_strv(m, p);
1706 if (r < 0)
1707 log_error_errno(r, "Could not emit changed properties: %m");
1708 }
1709
1710 m->dirty = false;
1711
1712 return 0;
1713
1714 fail:
1715 (void) unlink(m->state_file);
1716 (void) unlink(temp_path);
1717
1718 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1719 }
1720
1721 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1722 Manager *m = userdata;
1723 Link *link;
1724 Iterator i;
1725
1726 assert(m);
1727
1728 if (m->dirty)
1729 manager_save(m);
1730
1731 SET_FOREACH(link, m->dirty_links, i)
1732 (void) link_save_and_clean(link);
1733
1734 return 1;
1735 }
1736
1737 static int signal_terminate_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1738 Manager *m = userdata;
1739
1740 assert(m);
1741 m->restarting = false;
1742
1743 log_debug("Terminate operation initiated.");
1744
1745 return sd_event_exit(sd_event_source_get_event(s), 0);
1746 }
1747
1748 static int signal_restart_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1749 Manager *m = userdata;
1750
1751 assert(m);
1752 m->restarting = true;
1753
1754 log_debug("Restart operation initiated.");
1755
1756 return sd_event_exit(sd_event_source_get_event(s), 0);
1757 }
1758
1759 int manager_new(Manager **ret) {
1760 _cleanup_(manager_freep) Manager *m = NULL;
1761 int r;
1762
1763 m = new(Manager, 1);
1764 if (!m)
1765 return -ENOMEM;
1766
1767 *m = (Manager) {
1768 .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL,
1769 .manage_foreign_routes = true,
1770 .ethtool_fd = -1,
1771 };
1772
1773 m->state_file = strdup("/run/systemd/netif/state");
1774 if (!m->state_file)
1775 return -ENOMEM;
1776
1777 r = sd_event_default(&m->event);
1778 if (r < 0)
1779 return r;
1780
1781 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR2, -1) >= 0);
1782
1783 (void) sd_event_set_watchdog(m->event, true);
1784 (void) sd_event_add_signal(m->event, NULL, SIGTERM, signal_terminate_callback, m);
1785 (void) sd_event_add_signal(m->event, NULL, SIGINT, signal_terminate_callback, m);
1786 (void) sd_event_add_signal(m->event, NULL, SIGUSR2, signal_restart_callback, m);
1787
1788 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1789 if (r < 0)
1790 return r;
1791
1792 r = manager_connect_rtnl(m);
1793 if (r < 0)
1794 return r;
1795
1796 r = manager_connect_genl(m);
1797 if (r < 0)
1798 return r;
1799
1800 r = manager_connect_udev(m);
1801 if (r < 0)
1802 return r;
1803
1804 r = sd_resolve_default(&m->resolve);
1805 if (r < 0)
1806 return r;
1807
1808 r = sd_resolve_attach_event(m->resolve, m->event, 0);
1809 if (r < 0)
1810 return r;
1811
1812 r = setup_default_address_pool(m);
1813 if (r < 0)
1814 return r;
1815
1816 m->duid.type = DUID_TYPE_EN;
1817
1818 (void) routing_policy_load_rules(m->state_file, &m->rules_saved);
1819
1820 *ret = TAKE_PTR(m);
1821
1822 return 0;
1823 }
1824
1825 void manager_free(Manager *m) {
1826 struct in6_addr *a;
1827 AddressPool *pool;
1828 Link *link;
1829
1830 if (!m)
1831 return;
1832
1833 free(m->state_file);
1834
1835 while ((a = hashmap_first_key(m->dhcp6_prefixes)))
1836 (void) dhcp6_prefix_remove(m, a);
1837 m->dhcp6_prefixes = hashmap_free(m->dhcp6_prefixes);
1838
1839 while ((link = hashmap_steal_first(m->links))) {
1840 if (link->dhcp6_client)
1841 (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link);
1842
1843 (void) link_stop_clients(link, true);
1844
1845 link_unref(link);
1846 }
1847
1848 m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
1849 m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref);
1850 m->links = hashmap_free_with_destructor(m->links, link_unref);
1851
1852 m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
1853 m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
1854
1855 m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
1856
1857 while ((pool = m->address_pools))
1858 address_pool_free(pool);
1859
1860 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1861 * So, it is necessary to set NULL after the sets are freed. */
1862 m->rules = set_free(m->rules);
1863 m->rules_foreign = set_free(m->rules_foreign);
1864 set_free(m->rules_saved);
1865
1866 sd_netlink_unref(m->rtnl);
1867 sd_netlink_unref(m->genl);
1868 sd_resolve_unref(m->resolve);
1869
1870 sd_event_source_unref(m->speed_meter_event_source);
1871 sd_event_unref(m->event);
1872
1873 sd_device_monitor_unref(m->device_monitor);
1874
1875 bus_verify_polkit_async_registry_free(m->polkit_registry);
1876 sd_bus_flush_close_unref(m->bus);
1877
1878 free(m->dynamic_timezone);
1879 free(m->dynamic_hostname);
1880
1881 safe_close(m->ethtool_fd);
1882
1883 free(m);
1884 }
1885
1886 int manager_start(Manager *m) {
1887 Link *link;
1888 Iterator i;
1889 int r;
1890
1891 assert(m);
1892
1893 r = manager_start_speed_meter(m);
1894 if (r < 0)
1895 return log_error_errno(r, "Failed to initialize speed meter: %m");
1896
1897 /* The dirty handler will deal with future serialization, but the first one
1898 must be done explicitly. */
1899
1900 manager_save(m);
1901
1902 HASHMAP_FOREACH(link, m->links, i)
1903 (void) link_save(link);
1904
1905 return 0;
1906 }
1907
1908 int manager_load_config(Manager *m) {
1909 int r;
1910
1911 /* update timestamp */
1912 paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true);
1913
1914 r = netdev_load(m, false);
1915 if (r < 0)
1916 return r;
1917
1918 r = network_load(m, &m->networks);
1919 if (r < 0)
1920 return r;
1921
1922 return 0;
1923 }
1924
1925 bool manager_should_reload(Manager *m) {
1926 return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false);
1927 }
1928
1929 int manager_rtnl_enumerate_links(Manager *m) {
1930 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1931 sd_netlink_message *link;
1932 int r;
1933
1934 assert(m);
1935 assert(m->rtnl);
1936
1937 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1938 if (r < 0)
1939 return r;
1940
1941 r = sd_netlink_message_request_dump(req, true);
1942 if (r < 0)
1943 return r;
1944
1945 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1946 if (r < 0)
1947 return r;
1948
1949 for (link = reply; link; link = sd_netlink_message_next(link)) {
1950 int k;
1951
1952 m->enumerating = true;
1953
1954 k = manager_rtnl_process_link(m->rtnl, link, m);
1955 if (k < 0)
1956 r = k;
1957
1958 m->enumerating = false;
1959 }
1960
1961 return r;
1962 }
1963
1964 int manager_rtnl_enumerate_addresses(Manager *m) {
1965 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1966 sd_netlink_message *addr;
1967 int r;
1968
1969 assert(m);
1970 assert(m->rtnl);
1971
1972 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1973 if (r < 0)
1974 return r;
1975
1976 r = sd_netlink_message_request_dump(req, true);
1977 if (r < 0)
1978 return r;
1979
1980 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1981 if (r < 0)
1982 return r;
1983
1984 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1985 int k;
1986
1987 m->enumerating = true;
1988
1989 k = manager_rtnl_process_address(m->rtnl, addr, m);
1990 if (k < 0)
1991 r = k;
1992
1993 m->enumerating = false;
1994 }
1995
1996 return r;
1997 }
1998
1999 int manager_rtnl_enumerate_neighbors(Manager *m) {
2000 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2001 sd_netlink_message *neigh;
2002 int r;
2003
2004 assert(m);
2005 assert(m->rtnl);
2006
2007 r = sd_rtnl_message_new_neigh(m->rtnl, &req, RTM_GETNEIGH, 0, AF_UNSPEC);
2008 if (r < 0)
2009 return r;
2010
2011 r = sd_netlink_message_request_dump(req, true);
2012 if (r < 0)
2013 return r;
2014
2015 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2016 if (r < 0)
2017 return r;
2018
2019 for (neigh = reply; neigh; neigh = sd_netlink_message_next(neigh)) {
2020 int k;
2021
2022 m->enumerating = true;
2023
2024 k = manager_rtnl_process_neighbor(m->rtnl, neigh, m);
2025 if (k < 0)
2026 r = k;
2027
2028 m->enumerating = false;
2029 }
2030
2031 return r;
2032 }
2033
2034 int manager_rtnl_enumerate_routes(Manager *m) {
2035 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2036 sd_netlink_message *route;
2037 int r;
2038
2039 assert(m);
2040 assert(m->rtnl);
2041
2042 if (!m->manage_foreign_routes)
2043 return 0;
2044
2045 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
2046 if (r < 0)
2047 return r;
2048
2049 r = sd_netlink_message_request_dump(req, true);
2050 if (r < 0)
2051 return r;
2052
2053 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2054 if (r < 0)
2055 return r;
2056
2057 for (route = reply; route; route = sd_netlink_message_next(route)) {
2058 int k;
2059
2060 m->enumerating = true;
2061
2062 k = manager_rtnl_process_route(m->rtnl, route, m);
2063 if (k < 0)
2064 r = k;
2065
2066 m->enumerating = false;
2067 }
2068
2069 return r;
2070 }
2071
2072 int manager_rtnl_enumerate_rules(Manager *m) {
2073 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2074 sd_netlink_message *rule;
2075 int r;
2076
2077 assert(m);
2078 assert(m->rtnl);
2079
2080 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0);
2081 if (r < 0)
2082 return r;
2083
2084 r = sd_netlink_message_request_dump(req, true);
2085 if (r < 0)
2086 return r;
2087
2088 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2089 if (r < 0) {
2090 if (r == -EOPNOTSUPP) {
2091 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
2092 return 0;
2093 }
2094
2095 return r;
2096 }
2097
2098 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
2099 int k;
2100
2101 m->enumerating = true;
2102
2103 k = manager_rtnl_process_rule(m->rtnl, rule, m);
2104 if (k < 0)
2105 r = k;
2106
2107 m->enumerating = false;
2108 }
2109
2110 return r;
2111 }
2112
2113 int manager_rtnl_enumerate_nexthop(Manager *m) {
2114 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2115 sd_netlink_message *nexthop;
2116 int r;
2117
2118 assert(m);
2119 assert(m->rtnl);
2120
2121 r = sd_rtnl_message_new_nexthop(m->rtnl, &req, RTM_GETNEXTHOP, 0, 0);
2122 if (r < 0)
2123 return r;
2124
2125 r = sd_netlink_message_request_dump(req, true);
2126 if (r < 0)
2127 return r;
2128
2129 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2130 if (r < 0) {
2131 if (r == -EOPNOTSUPP) {
2132 log_debug("Nexthop are not supported by the kernel. Ignoring.");
2133 return 0;
2134 }
2135
2136 return r;
2137 }
2138
2139 for (nexthop = reply; nexthop; nexthop = sd_netlink_message_next(nexthop)) {
2140 int k;
2141
2142 m->enumerating = true;
2143
2144 k = manager_rtnl_process_nexthop(m->rtnl, nexthop, m);
2145 if (k < 0)
2146 r = k;
2147
2148 m->enumerating = false;
2149 }
2150
2151 return r;
2152 }
2153
2154 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
2155 AddressPool *p;
2156 int r;
2157
2158 assert(m);
2159 assert(prefixlen > 0);
2160 assert(found);
2161
2162 LIST_FOREACH(address_pools, p, m->address_pools) {
2163 if (p->family != family)
2164 continue;
2165
2166 r = address_pool_acquire(p, prefixlen, found);
2167 if (r != 0)
2168 return r;
2169 }
2170
2171 return 0;
2172 }
2173
2174 Link* manager_find_uplink(Manager *m, Link *exclude) {
2175 _cleanup_free_ struct local_address *gateways = NULL;
2176 int n, i;
2177
2178 assert(m);
2179
2180 /* Looks for a suitable "uplink", via black magic: an
2181 * interface that is up and where the default route with the
2182 * highest priority points to. */
2183
2184 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
2185 if (n < 0) {
2186 log_warning_errno(n, "Failed to determine list of default gateways: %m");
2187 return NULL;
2188 }
2189
2190 for (i = 0; i < n; i++) {
2191 Link *link;
2192
2193 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
2194 if (!link) {
2195 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
2196 continue;
2197 }
2198
2199 if (link == exclude)
2200 continue;
2201
2202 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
2203 continue;
2204
2205 return link;
2206 }
2207
2208 return NULL;
2209 }
2210
2211 void manager_dirty(Manager *manager) {
2212 assert(manager);
2213
2214 /* the serialized state in /run is no longer up-to-date */
2215 manager->dirty = true;
2216 }
2217
2218 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2219 _unused_ Manager *manager = userdata;
2220 const sd_bus_error *e;
2221
2222 assert(m);
2223 assert(manager);
2224
2225 e = sd_bus_message_get_error(m);
2226 if (e)
2227 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
2228
2229 return 1;
2230 }
2231
2232 int manager_set_hostname(Manager *m, const char *hostname) {
2233 int r;
2234
2235 log_debug("Setting transient hostname: '%s'", strna(hostname));
2236
2237 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
2238 return log_oom();
2239
2240 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2241 log_debug("Not connected to system bus, setting hostname later.");
2242 return 0;
2243 }
2244
2245 r = sd_bus_call_method_async(
2246 m->bus,
2247 NULL,
2248 "org.freedesktop.hostname1",
2249 "/org/freedesktop/hostname1",
2250 "org.freedesktop.hostname1",
2251 "SetHostname",
2252 set_hostname_handler,
2253 m,
2254 "sb",
2255 hostname,
2256 false);
2257
2258 if (r < 0)
2259 return log_error_errno(r, "Could not set transient hostname: %m");
2260
2261 return 0;
2262 }
2263
2264 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2265 _unused_ Manager *manager = userdata;
2266 const sd_bus_error *e;
2267
2268 assert(m);
2269 assert(manager);
2270
2271 e = sd_bus_message_get_error(m);
2272 if (e)
2273 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
2274
2275 return 1;
2276 }
2277
2278 int manager_set_timezone(Manager *m, const char *tz) {
2279 int r;
2280
2281 assert(m);
2282 assert(tz);
2283
2284 log_debug("Setting system timezone: '%s'", tz);
2285 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
2286 return log_oom();
2287
2288 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2289 log_debug("Not connected to system bus, setting timezone later.");
2290 return 0;
2291 }
2292
2293 r = sd_bus_call_method_async(
2294 m->bus,
2295 NULL,
2296 "org.freedesktop.timedate1",
2297 "/org/freedesktop/timedate1",
2298 "org.freedesktop.timedate1",
2299 "SetTimezone",
2300 set_timezone_handler,
2301 m,
2302 "sb",
2303 tz,
2304 false);
2305 if (r < 0)
2306 return log_error_errno(r, "Could not set timezone: %m");
2307
2308 return 0;
2309 }
2310
2311 int manager_request_product_uuid(Manager *m, Link *link) {
2312 int r;
2313
2314 assert(m);
2315
2316 if (m->has_product_uuid)
2317 return 0;
2318
2319 log_debug("Requesting product UUID");
2320
2321 if (link) {
2322 DUID *duid;
2323
2324 assert_se(duid = link_get_duid(link));
2325
2326 r = set_ensure_put(&m->links_requesting_uuid, NULL, link);
2327 if (r < 0)
2328 return log_oom();
2329 if (r > 0)
2330 link_ref(link);
2331
2332 r = set_ensure_put(&m->duids_requesting_uuid, NULL, duid);
2333 if (r < 0)
2334 return log_oom();
2335 }
2336
2337 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2338 log_debug("Not connected to system bus, requesting product UUID later.");
2339 return 0;
2340 }
2341
2342 r = sd_bus_call_method_async(
2343 m->bus,
2344 NULL,
2345 "org.freedesktop.hostname1",
2346 "/org/freedesktop/hostname1",
2347 "org.freedesktop.hostname1",
2348 "GetProductUUID",
2349 get_product_uuid_handler,
2350 m,
2351 "b",
2352 false);
2353 if (r < 0)
2354 return log_warning_errno(r, "Failed to get product UUID: %m");
2355
2356 return 0;
2357 }