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