]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
network: support UID based routing policy
[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 Manager *m = userdata;
966 unsigned flags;
967 uint16_t type;
968 int r;
969
970 assert(rtnl);
971 assert(message);
972 assert(m);
973
974 if (sd_netlink_message_is_error(message)) {
975 r = sd_netlink_message_get_errno(message);
976 if (r < 0)
977 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
978
979 return 0;
980 }
981
982 r = sd_netlink_message_get_type(message, &type);
983 if (r < 0) {
984 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
985 return 0;
986 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
987 log_warning("rtnl: received unexpected message type %u when processing rule, ignoring.", type);
988 return 0;
989 }
990
991 r = routing_policy_rule_new(&tmp);
992 if (r < 0) {
993 log_oom();
994 return 0;
995 }
996
997 r = sd_rtnl_message_get_family(message, &tmp->family);
998 if (r < 0) {
999 log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
1000 return 0;
1001 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1002 log_debug("rtnl: received rule message with invalid family %d, ignoring.", tmp->family);
1003 return 0;
1004 }
1005
1006 switch (tmp->family) {
1007 case AF_INET:
1008 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &tmp->from.in);
1009 if (r < 0 && r != -ENODATA) {
1010 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1011 return 0;
1012 } else if (r >= 0) {
1013 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1014 if (r < 0) {
1015 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1016 return 0;
1017 }
1018 }
1019
1020 r = sd_netlink_message_read_in_addr(message, FRA_DST, &tmp->to.in);
1021 if (r < 0 && r != -ENODATA) {
1022 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1023 return 0;
1024 } else if (r >= 0) {
1025 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1026 if (r < 0) {
1027 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1028 return 0;
1029 }
1030 }
1031
1032 break;
1033
1034 case AF_INET6:
1035 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &tmp->from.in6);
1036 if (r < 0 && r != -ENODATA) {
1037 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
1038 return 0;
1039 } else if (r >= 0) {
1040 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
1041 if (r < 0) {
1042 log_warning_errno(r, "rtnl: received rule message without valid source prefix length, ignoring: %m");
1043 return 0;
1044 }
1045 }
1046
1047 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &tmp->to.in6);
1048 if (r < 0 && r != -ENODATA) {
1049 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
1050 return 0;
1051 } else if (r >= 0) {
1052 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
1053 if (r < 0) {
1054 log_warning_errno(r, "rtnl: received rule message without valid destination prefix length, ignoring: %m");
1055 return 0;
1056 }
1057 }
1058
1059 break;
1060
1061 default:
1062 assert_not_reached("Received rule message with unsupported address family");
1063 }
1064
1065 if (tmp->from_prefixlen == 0 && tmp->to_prefixlen == 0)
1066 return 0;
1067
1068 r = sd_rtnl_message_routing_policy_rule_get_flags(message, &flags);
1069 if (r < 0) {
1070 log_warning_errno(r, "rtnl: received rule message without valid flag, ignoring: %m");
1071 return 0;
1072 }
1073 tmp->invert_rule = flags & FIB_RULE_INVERT;
1074
1075 r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark);
1076 if (r < 0 && r != -ENODATA) {
1077 log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
1078 return 0;
1079 }
1080
1081 r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask);
1082 if (r < 0 && r != -ENODATA) {
1083 log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
1084 return 0;
1085 }
1086
1087 r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority);
1088 if (r < 0 && r != -ENODATA) {
1089 log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
1090 return 0;
1091 }
1092
1093 r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table);
1094 if (r < 0 && r != -ENODATA) {
1095 log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
1096 return 0;
1097 }
1098
1099 r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos);
1100 if (r < 0 && r != -ENODATA) {
1101 log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m");
1102 return 0;
1103 }
1104
1105 r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
1106 if (r < 0 && r != -ENODATA) {
1107 log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
1108 return 0;
1109 }
1110 r = free_and_strdup(&tmp->iif, iif);
1111 if (r < 0)
1112 return log_oom();
1113
1114 r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
1115 if (r < 0 && r != -ENODATA) {
1116 log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
1117 return 0;
1118 }
1119 r = free_and_strdup(&tmp->oif, oif);
1120 if (r < 0)
1121 return log_oom();
1122
1123 r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->protocol);
1124 if (r < 0 && r != -ENODATA) {
1125 log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
1126 return 0;
1127 }
1128
1129 r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport);
1130 if (r < 0 && r != -ENODATA) {
1131 log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
1132 return 0;
1133 }
1134
1135 r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport);
1136 if (r < 0 && r != -ENODATA) {
1137 log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
1138 return 0;
1139 }
1140
1141 r = sd_netlink_message_read(message, FRA_UID_RANGE, sizeof(tmp->uid_range), &tmp->uid_range);
1142 if (r < 0 && r != -ENODATA) {
1143 log_warning_errno(r, "rtnl: could not get FRA_UID_RANGE attribute, ignoring: %m");
1144 return 0;
1145 }
1146
1147 (void) routing_policy_rule_get(m, tmp, &rule);
1148
1149 if (DEBUG_LOGGING) {
1150 (void) in_addr_to_string(tmp->family, &tmp->from, &from);
1151 (void) in_addr_to_string(tmp->family, &tmp->to, &to);
1152 }
1153
1154 switch (type) {
1155 case RTM_NEWRULE:
1156 if (!rule) {
1157 log_debug("Remembering foreign routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1158 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1159 r = routing_policy_rule_add_foreign(m, tmp, &rule);
1160 if (r < 0) {
1161 log_warning_errno(r, "Could not remember foreign rule, ignoring: %m");
1162 return 0;
1163 }
1164 }
1165 break;
1166 case RTM_DELRULE:
1167 log_debug("Forgetting routing policy rule: %s/%u -> %s/%u, iif: %s, oif: %s, table: %u",
1168 from, tmp->from_prefixlen, to, tmp->to_prefixlen, strna(tmp->iif), strna(tmp->oif), tmp->table);
1169 routing_policy_rule_free(rule);
1170
1171 break;
1172
1173 default:
1174 assert_not_reached("Received invalid RTNL message type");
1175 }
1176
1177 return 1;
1178 }
1179
1180 int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
1181 _cleanup_(nexthop_freep) NextHop *tmp = NULL;
1182 _cleanup_free_ char *gateway = NULL;
1183 NextHop *nexthop = NULL;
1184 Manager *m = userdata;
1185 Link *link = NULL;
1186 uint16_t type;
1187 int r;
1188
1189 assert(rtnl);
1190 assert(message);
1191 assert(m);
1192
1193 if (sd_netlink_message_is_error(message)) {
1194 r = sd_netlink_message_get_errno(message);
1195 if (r < 0)
1196 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
1197
1198 return 0;
1199 }
1200
1201 r = sd_netlink_message_get_type(message, &type);
1202 if (r < 0) {
1203 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1204 return 0;
1205 } else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) {
1206 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type);
1207 return 0;
1208 }
1209
1210 r = nexthop_new(&tmp);
1211 if (r < 0)
1212 return log_oom();
1213
1214 r = sd_rtnl_message_get_family(message, &tmp->family);
1215 if (r < 0) {
1216 log_warning_errno(r, "rtnl: could not get nexthop family, ignoring: %m");
1217 return 0;
1218 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1219 log_debug("rtnl: received nexthop message with invalid family %d, ignoring.", tmp->family);
1220 return 0;
1221 }
1222
1223 switch (tmp->family) {
1224 case AF_INET:
1225 r = sd_netlink_message_read_in_addr(message, NHA_GATEWAY, &tmp->gw.in);
1226 if (r < 0 && r != -ENODATA) {
1227 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1228 return 0;
1229 }
1230 break;
1231
1232 case AF_INET6:
1233 r = sd_netlink_message_read_in6_addr(message, NHA_GATEWAY, &tmp->gw.in6);
1234 if (r < 0 && r != -ENODATA) {
1235 log_warning_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
1236 return 0;
1237 }
1238 break;
1239
1240 default:
1241 assert_not_reached("Received rule message with unsupported address family");
1242 }
1243
1244 r = sd_netlink_message_read_u32(message, NHA_ID, &tmp->id);
1245 if (r < 0 && r != -ENODATA) {
1246 log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m");
1247 return 0;
1248 }
1249
1250 r = sd_netlink_message_read_u32(message, NHA_OIF, &tmp->oif);
1251 if (r < 0 && r != -ENODATA) {
1252 log_warning_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
1253 return 0;
1254 }
1255
1256 r = link_get(m, tmp->oif, &link);
1257 if (r < 0 || !link) {
1258 if (!m->enumerating)
1259 log_warning("rtnl: received nexthop message for link (%d) we do not know about, ignoring", tmp->oif);
1260 return 0;
1261 }
1262
1263 (void) nexthop_get(link, tmp, &nexthop);
1264
1265 if (DEBUG_LOGGING)
1266 (void) in_addr_to_string(tmp->family, &tmp->gw, &gateway);
1267
1268 switch (type) {
1269 case RTM_NEWNEXTHOP:
1270 if (!nexthop) {
1271 log_debug("Remembering foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1272 r = nexthop_add_foreign(link, tmp, &nexthop);
1273 if (r < 0) {
1274 log_warning_errno(r, "Could not remember foreign nexthop, ignoring: %m");
1275 return 0;
1276 }
1277 }
1278 break;
1279 case RTM_DELNEXTHOP:
1280 log_debug("Forgetting foreign nexthop: %s, oif: %d, id: %d", gateway, tmp->oif, tmp->id);
1281 nexthop_free(nexthop);
1282
1283 break;
1284
1285 default:
1286 assert_not_reached("Received invalid RTNL message type");
1287 }
1288
1289 return 1;
1290 }
1291
1292 static int systemd_netlink_fd(void) {
1293 int n, fd, rtnl_fd = -EINVAL;
1294
1295 n = sd_listen_fds(true);
1296 if (n <= 0)
1297 return -EINVAL;
1298
1299 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
1300 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
1301 if (rtnl_fd >= 0)
1302 return -EINVAL;
1303
1304 rtnl_fd = fd;
1305 }
1306 }
1307
1308 return rtnl_fd;
1309 }
1310
1311 static int manager_connect_genl(Manager *m) {
1312 int r;
1313
1314 assert(m);
1315
1316 r = sd_genl_socket_open(&m->genl);
1317 if (r < 0)
1318 return r;
1319
1320 r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE);
1321 if (r < 0)
1322 return r;
1323
1324 r = sd_netlink_attach_event(m->genl, m->event, 0);
1325 if (r < 0)
1326 return r;
1327
1328 return 0;
1329 }
1330
1331 static int manager_connect_rtnl(Manager *m) {
1332 int fd, r;
1333
1334 assert(m);
1335
1336 fd = systemd_netlink_fd();
1337 if (fd < 0)
1338 r = sd_netlink_open(&m->rtnl);
1339 else
1340 r = sd_netlink_open_fd(&m->rtnl, fd);
1341 if (r < 0)
1342 return r;
1343
1344 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
1345 if (r < 0)
1346 return r;
1347
1348 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
1349 if (r < 0)
1350 return r;
1351
1352 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1353 if (r < 0)
1354 return r;
1355
1356 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
1357 if (r < 0)
1358 return r;
1359
1360 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1361 if (r < 0)
1362 return r;
1363
1364 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1365 if (r < 0)
1366 return r;
1367
1368 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1369 if (r < 0)
1370 return r;
1371
1372 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEIGH, &manager_rtnl_process_neighbor, NULL, m, "network-rtnl_process_neighbor");
1373 if (r < 0)
1374 return r;
1375
1376 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1377 if (r < 0)
1378 return r;
1379
1380 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1381 if (r < 0)
1382 return r;
1383
1384 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1385 if (r < 0)
1386 return r;
1387
1388 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1389 if (r < 0)
1390 return r;
1391
1392 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1393 if (r < 0)
1394 return r;
1395
1396 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELNEXTHOP, &manager_rtnl_process_nexthop, NULL, m, "network-rtnl_process_nexthop");
1397 if (r < 0)
1398 return r;
1399
1400 return 0;
1401 }
1402
1403 static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
1404 char *p;
1405 int r;
1406
1407 assert(s);
1408 assert(address);
1409
1410 r = in_addr_to_string(address->family, &address->address, &p);
1411 if (r < 0)
1412 return r;
1413
1414 r = ordered_set_consume(s, p);
1415 if (r == -EEXIST)
1416 return 0;
1417
1418 return r;
1419 }
1420
1421 static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
1422 int r, c = 0;
1423 unsigned i;
1424
1425 assert(s);
1426 assert(addresses || n == 0);
1427
1428 for (i = 0; i < n; i++) {
1429 r = ordered_set_put_in_addr_data(s, addresses+i);
1430 if (r < 0)
1431 return r;
1432
1433 c += r;
1434 }
1435
1436 return c;
1437 }
1438
1439 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
1440 char *p;
1441 int r;
1442
1443 assert(s);
1444 assert(address);
1445
1446 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
1447 if (r < 0)
1448 return r;
1449
1450 r = ordered_set_consume(s, p);
1451 if (r == -EEXIST)
1452 return 0;
1453
1454 return r;
1455 }
1456
1457 static int ordered_set_put_in4_addrv(OrderedSet *s,
1458 const struct in_addr *addresses,
1459 size_t n,
1460 bool (*predicate)(const struct in_addr *addr)) {
1461 int r, c = 0;
1462 size_t i;
1463
1464 assert(s);
1465 assert(n == 0 || addresses);
1466
1467 for (i = 0; i < n; i++) {
1468 if (predicate && !predicate(&addresses[i]))
1469 continue;
1470 r = ordered_set_put_in4_addr(s, addresses+i);
1471 if (r < 0)
1472 return r;
1473
1474 c += r;
1475 }
1476
1477 return c;
1478 }
1479
1480 static int manager_save(Manager *m) {
1481 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *sip = NULL, *search_domains = NULL, *route_domains = NULL;
1482 const char *operstate_str, *carrier_state_str, *address_state_str;
1483 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1484 LinkCarrierState carrier_state = LINK_CARRIER_STATE_OFF;
1485 LinkAddressState address_state = LINK_ADDRESS_STATE_OFF;
1486 _cleanup_free_ char *temp_path = NULL;
1487 _cleanup_strv_free_ char **p = NULL;
1488 _cleanup_fclose_ FILE *f = NULL;
1489 Link *link;
1490 Iterator i;
1491 int r;
1492
1493 assert(m);
1494 assert(m->state_file);
1495
1496 /* We add all NTP and DNS server to a set, to filter out duplicates */
1497 dns = ordered_set_new(&string_hash_ops);
1498 if (!dns)
1499 return -ENOMEM;
1500
1501 ntp = ordered_set_new(&string_hash_ops);
1502 if (!ntp)
1503 return -ENOMEM;
1504
1505 sip = ordered_set_new(&string_hash_ops);
1506 if (!sip)
1507 return -ENOMEM;
1508
1509 search_domains = ordered_set_new(&dns_name_hash_ops);
1510 if (!search_domains)
1511 return -ENOMEM;
1512
1513 route_domains = ordered_set_new(&dns_name_hash_ops);
1514 if (!route_domains)
1515 return -ENOMEM;
1516
1517 HASHMAP_FOREACH(link, m->links, i) {
1518 if (link->flags & IFF_LOOPBACK)
1519 continue;
1520
1521 if (link->operstate > operstate)
1522 operstate = link->operstate;
1523
1524 if (link->carrier_state > carrier_state)
1525 carrier_state = link->carrier_state;
1526
1527 if (link->address_state > address_state)
1528 address_state = link->address_state;
1529
1530 if (!link->network)
1531 continue;
1532
1533 /* First add the static configured entries */
1534 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
1535 if (r < 0)
1536 return r;
1537
1538 r = ordered_set_put_strdupv(ntp, link->ntp ?: link->network->ntp);
1539 if (r < 0)
1540 return r;
1541
1542 r = ordered_set_put_string_set(search_domains, link->search_domains ?: link->network->search_domains);
1543 if (r < 0)
1544 return r;
1545
1546 r = ordered_set_put_string_set(route_domains, link->route_domains ?: link->network->route_domains);
1547 if (r < 0)
1548 return r;
1549
1550 if (!link->dhcp_lease)
1551 continue;
1552
1553 /* Secondly, add the entries acquired via DHCP */
1554 if (link->network->dhcp_use_dns) {
1555 const struct in_addr *addresses;
1556
1557 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1558 if (r > 0) {
1559 r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local);
1560 if (r < 0)
1561 return r;
1562 } else if (r < 0 && r != -ENODATA)
1563 return r;
1564 }
1565
1566 if (link->network->dhcp_use_ntp) {
1567 const struct in_addr *addresses;
1568
1569 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1570 if (r > 0) {
1571 r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local);
1572 if (r < 0)
1573 return r;
1574 } else if (r < 0 && r != -ENODATA)
1575 return r;
1576 }
1577
1578 if (link->network->dhcp_use_sip) {
1579 const struct in_addr *addresses;
1580
1581 r = sd_dhcp_lease_get_sip(link->dhcp_lease, &addresses);
1582 if (r > 0) {
1583 r = ordered_set_put_in4_addrv(sip, addresses, r, in4_addr_is_non_local);
1584 if (r < 0)
1585 return r;
1586 } else if (r < 0 && r != -ENODATA)
1587 return r;
1588 }
1589
1590 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1591 const char *domainname;
1592 char **domains = NULL;
1593
1594 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
1595 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1596 if (r >= 0) {
1597 r = ordered_set_put_strdup(target_domains, domainname);
1598 if (r < 0)
1599 return r;
1600 } else if (r != -ENODATA)
1601 return r;
1602
1603 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1604 if (r >= 0) {
1605 r = ordered_set_put_strdupv(target_domains, domains);
1606 if (r < 0)
1607 return r;
1608 } else if (r != -ENODATA)
1609 return r;
1610 }
1611 }
1612
1613 if (carrier_state >= LINK_CARRIER_STATE_ENSLAVED)
1614 carrier_state = LINK_CARRIER_STATE_CARRIER;
1615
1616 operstate_str = link_operstate_to_string(operstate);
1617 assert(operstate_str);
1618
1619 carrier_state_str = link_carrier_state_to_string(carrier_state);
1620 assert(carrier_state_str);
1621
1622 address_state_str = link_address_state_to_string(address_state);
1623 assert(address_state_str);
1624
1625 r = fopen_temporary(m->state_file, &f, &temp_path);
1626 if (r < 0)
1627 return r;
1628
1629 (void) fchmod(fileno(f), 0644);
1630
1631 fprintf(f,
1632 "# This is private data. Do not parse.\n"
1633 "OPER_STATE=%s\n"
1634 "CARRIER_STATE=%s\n"
1635 "ADDRESS_STATE=%s\n",
1636 operstate_str, carrier_state_str, address_state_str);
1637
1638 ordered_set_print(f, "DNS=", dns);
1639 ordered_set_print(f, "NTP=", ntp);
1640 ordered_set_print(f, "SIP=", sip);
1641 ordered_set_print(f, "DOMAINS=", search_domains);
1642 ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
1643
1644 r = routing_policy_serialize_rules(m->rules, f);
1645 if (r < 0)
1646 goto fail;
1647
1648 r = fflush_and_check(f);
1649 if (r < 0)
1650 goto fail;
1651
1652 if (rename(temp_path, m->state_file) < 0) {
1653 r = -errno;
1654 goto fail;
1655 }
1656
1657 if (m->operational_state != operstate) {
1658 m->operational_state = operstate;
1659 if (strv_extend(&p, "OperationalState") < 0)
1660 log_oom();
1661 }
1662
1663 if (m->carrier_state != carrier_state) {
1664 m->carrier_state = carrier_state;
1665 if (strv_extend(&p, "CarrierState") < 0)
1666 log_oom();
1667 }
1668
1669 if (m->address_state != address_state) {
1670 m->address_state = address_state;
1671 if (strv_extend(&p, "AddressState") < 0)
1672 log_oom();
1673 }
1674
1675 if (p) {
1676 r = manager_send_changed_strv(m, p);
1677 if (r < 0)
1678 log_error_errno(r, "Could not emit changed properties: %m");
1679 }
1680
1681 m->dirty = false;
1682
1683 return 0;
1684
1685 fail:
1686 (void) unlink(m->state_file);
1687 (void) unlink(temp_path);
1688
1689 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1690 }
1691
1692 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1693 Manager *m = userdata;
1694 Link *link;
1695 Iterator i;
1696
1697 assert(m);
1698
1699 if (m->dirty)
1700 manager_save(m);
1701
1702 SET_FOREACH(link, m->dirty_links, i)
1703 if (link_save(link) >= 0)
1704 link_clean(link);
1705
1706 return 1;
1707 }
1708
1709 static int signal_terminate_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1710 Manager *m = userdata;
1711
1712 assert(m);
1713 m->restarting = false;
1714
1715 log_debug("Terminate operation initiated.");
1716
1717 return sd_event_exit(sd_event_source_get_event(s), 0);
1718 }
1719
1720 static int signal_restart_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1721 Manager *m = userdata;
1722
1723 assert(m);
1724 m->restarting = true;
1725
1726 log_debug("Restart operation initiated.");
1727
1728 return sd_event_exit(sd_event_source_get_event(s), 0);
1729 }
1730
1731 int manager_new(Manager **ret) {
1732 _cleanup_(manager_freep) Manager *m = NULL;
1733 int r;
1734
1735 m = new(Manager, 1);
1736 if (!m)
1737 return -ENOMEM;
1738
1739 *m = (Manager) {
1740 .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL,
1741 };
1742
1743 m->state_file = strdup("/run/systemd/netif/state");
1744 if (!m->state_file)
1745 return -ENOMEM;
1746
1747 r = sd_event_default(&m->event);
1748 if (r < 0)
1749 return r;
1750
1751 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR2, -1) >= 0);
1752
1753 (void) sd_event_set_watchdog(m->event, true);
1754 (void) sd_event_add_signal(m->event, NULL, SIGTERM, signal_terminate_callback, m);
1755 (void) sd_event_add_signal(m->event, NULL, SIGINT, signal_terminate_callback, m);
1756 (void) sd_event_add_signal(m->event, NULL, SIGUSR2, signal_restart_callback, m);
1757
1758 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1759 if (r < 0)
1760 return r;
1761
1762 r = manager_connect_rtnl(m);
1763 if (r < 0)
1764 return r;
1765
1766 r = manager_connect_genl(m);
1767 if (r < 0)
1768 return r;
1769
1770 r = manager_connect_udev(m);
1771 if (r < 0)
1772 return r;
1773
1774 r = sd_resolve_default(&m->resolve);
1775 if (r < 0)
1776 return r;
1777
1778 r = sd_resolve_attach_event(m->resolve, m->event, 0);
1779 if (r < 0)
1780 return r;
1781
1782 r = setup_default_address_pool(m);
1783 if (r < 0)
1784 return r;
1785
1786 m->duid.type = DUID_TYPE_EN;
1787
1788 (void) routing_policy_load_rules(m->state_file, &m->rules_saved);
1789
1790 *ret = TAKE_PTR(m);
1791
1792 return 0;
1793 }
1794
1795 void manager_free(Manager *m) {
1796 struct in6_addr *a;
1797 AddressPool *pool;
1798 Link *link;
1799
1800 if (!m)
1801 return;
1802
1803 free(m->state_file);
1804
1805 while ((a = hashmap_first_key(m->dhcp6_prefixes)))
1806 (void) dhcp6_prefix_remove(m, a);
1807 m->dhcp6_prefixes = hashmap_free(m->dhcp6_prefixes);
1808
1809 while ((link = hashmap_steal_first(m->links))) {
1810 if (link->dhcp6_client)
1811 (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link);
1812
1813 (void) link_stop_clients(link, true);
1814
1815 link_unref(link);
1816 }
1817
1818 m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
1819 m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref);
1820 m->links = hashmap_free_with_destructor(m->links, link_unref);
1821
1822 m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
1823 m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
1824
1825 m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
1826
1827 while ((pool = m->address_pools))
1828 address_pool_free(pool);
1829
1830 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1831 * So, it is necessary to set NULL after the sets are freed. */
1832 m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free);
1833 m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free);
1834 set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
1835
1836 sd_netlink_unref(m->rtnl);
1837 sd_netlink_unref(m->genl);
1838 sd_resolve_unref(m->resolve);
1839
1840 sd_event_source_unref(m->speed_meter_event_source);
1841 sd_event_unref(m->event);
1842
1843 sd_device_monitor_unref(m->device_monitor);
1844
1845 bus_verify_polkit_async_registry_free(m->polkit_registry);
1846 sd_bus_flush_close_unref(m->bus);
1847
1848 free(m->dynamic_timezone);
1849 free(m->dynamic_hostname);
1850
1851 free(m);
1852 }
1853
1854 int manager_start(Manager *m) {
1855 Link *link;
1856 Iterator i;
1857 int r;
1858
1859 assert(m);
1860
1861 r = manager_start_speed_meter(m);
1862 if (r < 0)
1863 return log_error_errno(r, "Failed to initialize speed meter: %m");
1864
1865 /* The dirty handler will deal with future serialization, but the first one
1866 must be done explicitly. */
1867
1868 manager_save(m);
1869
1870 HASHMAP_FOREACH(link, m->links, i)
1871 link_save(link);
1872
1873 return 0;
1874 }
1875
1876 int manager_load_config(Manager *m) {
1877 int r;
1878
1879 /* update timestamp */
1880 paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true);
1881
1882 r = netdev_load(m, false);
1883 if (r < 0)
1884 return r;
1885
1886 r = network_load(m, &m->networks);
1887 if (r < 0)
1888 return r;
1889
1890 return 0;
1891 }
1892
1893 bool manager_should_reload(Manager *m) {
1894 return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false);
1895 }
1896
1897 int manager_rtnl_enumerate_links(Manager *m) {
1898 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1899 sd_netlink_message *link;
1900 int r;
1901
1902 assert(m);
1903 assert(m->rtnl);
1904
1905 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1906 if (r < 0)
1907 return r;
1908
1909 r = sd_netlink_message_request_dump(req, true);
1910 if (r < 0)
1911 return r;
1912
1913 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1914 if (r < 0)
1915 return r;
1916
1917 for (link = reply; link; link = sd_netlink_message_next(link)) {
1918 int k;
1919
1920 m->enumerating = true;
1921
1922 k = manager_rtnl_process_link(m->rtnl, link, m);
1923 if (k < 0)
1924 r = k;
1925
1926 m->enumerating = false;
1927 }
1928
1929 return r;
1930 }
1931
1932 int manager_rtnl_enumerate_addresses(Manager *m) {
1933 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1934 sd_netlink_message *addr;
1935 int r;
1936
1937 assert(m);
1938 assert(m->rtnl);
1939
1940 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1941 if (r < 0)
1942 return r;
1943
1944 r = sd_netlink_message_request_dump(req, true);
1945 if (r < 0)
1946 return r;
1947
1948 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1949 if (r < 0)
1950 return r;
1951
1952 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1953 int k;
1954
1955 m->enumerating = true;
1956
1957 k = manager_rtnl_process_address(m->rtnl, addr, m);
1958 if (k < 0)
1959 r = k;
1960
1961 m->enumerating = false;
1962 }
1963
1964 return r;
1965 }
1966
1967 int manager_rtnl_enumerate_neighbors(Manager *m) {
1968 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1969 sd_netlink_message *neigh;
1970 int r;
1971
1972 assert(m);
1973 assert(m->rtnl);
1974
1975 r = sd_rtnl_message_new_neigh(m->rtnl, &req, RTM_GETNEIGH, 0, AF_UNSPEC);
1976 if (r < 0)
1977 return r;
1978
1979 r = sd_netlink_message_request_dump(req, true);
1980 if (r < 0)
1981 return r;
1982
1983 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1984 if (r < 0)
1985 return r;
1986
1987 for (neigh = reply; neigh; neigh = sd_netlink_message_next(neigh)) {
1988 int k;
1989
1990 m->enumerating = true;
1991
1992 k = manager_rtnl_process_neighbor(m->rtnl, neigh, m);
1993 if (k < 0)
1994 r = k;
1995
1996 m->enumerating = false;
1997 }
1998
1999 return r;
2000 }
2001
2002 int manager_rtnl_enumerate_routes(Manager *m) {
2003 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2004 sd_netlink_message *route;
2005 int r;
2006
2007 assert(m);
2008 assert(m->rtnl);
2009
2010 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
2011 if (r < 0)
2012 return r;
2013
2014 r = sd_netlink_message_request_dump(req, true);
2015 if (r < 0)
2016 return r;
2017
2018 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2019 if (r < 0)
2020 return r;
2021
2022 for (route = reply; route; route = sd_netlink_message_next(route)) {
2023 int k;
2024
2025 m->enumerating = true;
2026
2027 k = manager_rtnl_process_route(m->rtnl, route, m);
2028 if (k < 0)
2029 r = k;
2030
2031 m->enumerating = false;
2032 }
2033
2034 return r;
2035 }
2036
2037 int manager_rtnl_enumerate_rules(Manager *m) {
2038 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2039 sd_netlink_message *rule;
2040 int r;
2041
2042 assert(m);
2043 assert(m->rtnl);
2044
2045 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 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 if (r == -EOPNOTSUPP) {
2056 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
2057 return 0;
2058 }
2059
2060 return r;
2061 }
2062
2063 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
2064 int k;
2065
2066 m->enumerating = true;
2067
2068 k = manager_rtnl_process_rule(m->rtnl, rule, m);
2069 if (k < 0)
2070 r = k;
2071
2072 m->enumerating = false;
2073 }
2074
2075 return r;
2076 }
2077
2078 int manager_rtnl_enumerate_nexthop(Manager *m) {
2079 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
2080 sd_netlink_message *nexthop;
2081 int r;
2082
2083 assert(m);
2084 assert(m->rtnl);
2085
2086 r = sd_rtnl_message_new_nexthop(m->rtnl, &req, RTM_GETNEXTHOP, 0, 0);
2087 if (r < 0)
2088 return r;
2089
2090 r = sd_netlink_message_request_dump(req, true);
2091 if (r < 0)
2092 return r;
2093
2094 r = sd_netlink_call(m->rtnl, req, 0, &reply);
2095 if (r < 0) {
2096 if (r == -EOPNOTSUPP) {
2097 log_debug("Nexthop are not supported by the kernel. Ignoring.");
2098 return 0;
2099 }
2100
2101 return r;
2102 }
2103
2104 for (nexthop = reply; nexthop; nexthop = sd_netlink_message_next(nexthop)) {
2105 int k;
2106
2107 m->enumerating = true;
2108
2109 k = manager_rtnl_process_nexthop(m->rtnl, nexthop, m);
2110 if (k < 0)
2111 r = k;
2112
2113 m->enumerating = false;
2114 }
2115
2116 return r;
2117 }
2118
2119 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
2120 AddressPool *p;
2121 int r;
2122
2123 assert(m);
2124 assert(prefixlen > 0);
2125 assert(found);
2126
2127 LIST_FOREACH(address_pools, p, m->address_pools) {
2128 if (p->family != family)
2129 continue;
2130
2131 r = address_pool_acquire(p, prefixlen, found);
2132 if (r != 0)
2133 return r;
2134 }
2135
2136 return 0;
2137 }
2138
2139 Link* manager_find_uplink(Manager *m, Link *exclude) {
2140 _cleanup_free_ struct local_address *gateways = NULL;
2141 int n, i;
2142
2143 assert(m);
2144
2145 /* Looks for a suitable "uplink", via black magic: an
2146 * interface that is up and where the default route with the
2147 * highest priority points to. */
2148
2149 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
2150 if (n < 0) {
2151 log_warning_errno(n, "Failed to determine list of default gateways: %m");
2152 return NULL;
2153 }
2154
2155 for (i = 0; i < n; i++) {
2156 Link *link;
2157
2158 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
2159 if (!link) {
2160 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
2161 continue;
2162 }
2163
2164 if (link == exclude)
2165 continue;
2166
2167 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
2168 continue;
2169
2170 return link;
2171 }
2172
2173 return NULL;
2174 }
2175
2176 void manager_dirty(Manager *manager) {
2177 assert(manager);
2178
2179 /* the serialized state in /run is no longer up-to-date */
2180 manager->dirty = true;
2181 }
2182
2183 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2184 Manager *manager = userdata;
2185 const sd_bus_error *e;
2186
2187 assert(m);
2188 assert(manager);
2189
2190 e = sd_bus_message_get_error(m);
2191 if (e)
2192 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
2193
2194 return 1;
2195 }
2196
2197 int manager_set_hostname(Manager *m, const char *hostname) {
2198 int r;
2199
2200 log_debug("Setting transient hostname: '%s'", strna(hostname));
2201
2202 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
2203 return log_oom();
2204
2205 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2206 log_debug("Not connected to system bus, setting hostname later.");
2207 return 0;
2208 }
2209
2210 r = sd_bus_call_method_async(
2211 m->bus,
2212 NULL,
2213 "org.freedesktop.hostname1",
2214 "/org/freedesktop/hostname1",
2215 "org.freedesktop.hostname1",
2216 "SetHostname",
2217 set_hostname_handler,
2218 m,
2219 "sb",
2220 hostname,
2221 false);
2222
2223 if (r < 0)
2224 return log_error_errno(r, "Could not set transient hostname: %m");
2225
2226 return 0;
2227 }
2228
2229 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
2230 Manager *manager = userdata;
2231 const sd_bus_error *e;
2232
2233 assert(m);
2234 assert(manager);
2235
2236 e = sd_bus_message_get_error(m);
2237 if (e)
2238 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
2239
2240 return 1;
2241 }
2242
2243 int manager_set_timezone(Manager *m, const char *tz) {
2244 int r;
2245
2246 assert(m);
2247 assert(tz);
2248
2249 log_debug("Setting system timezone: '%s'", tz);
2250 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
2251 return log_oom();
2252
2253 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2254 log_debug("Not connected to system bus, setting timezone later.");
2255 return 0;
2256 }
2257
2258 r = sd_bus_call_method_async(
2259 m->bus,
2260 NULL,
2261 "org.freedesktop.timedate1",
2262 "/org/freedesktop/timedate1",
2263 "org.freedesktop.timedate1",
2264 "SetTimezone",
2265 set_timezone_handler,
2266 m,
2267 "sb",
2268 tz,
2269 false);
2270 if (r < 0)
2271 return log_error_errno(r, "Could not set timezone: %m");
2272
2273 return 0;
2274 }
2275
2276 int manager_request_product_uuid(Manager *m, Link *link) {
2277 int r;
2278
2279 assert(m);
2280
2281 if (m->has_product_uuid)
2282 return 0;
2283
2284 log_debug("Requesting product UUID");
2285
2286 if (link) {
2287 DUID *duid;
2288
2289 assert_se(duid = link_get_duid(link));
2290
2291 r = set_ensure_allocated(&m->links_requesting_uuid, NULL);
2292 if (r < 0)
2293 return log_oom();
2294
2295 r = set_ensure_allocated(&m->duids_requesting_uuid, NULL);
2296 if (r < 0)
2297 return log_oom();
2298
2299 r = set_put(m->links_requesting_uuid, link);
2300 if (r < 0)
2301 return log_oom();
2302
2303 r = set_put(m->duids_requesting_uuid, duid);
2304 if (r < 0)
2305 return log_oom();
2306
2307 link_ref(link);
2308 }
2309
2310 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
2311 log_debug("Not connected to system bus, requesting product UUID later.");
2312 return 0;
2313 }
2314
2315 r = sd_bus_call_method_async(
2316 m->bus,
2317 NULL,
2318 "org.freedesktop.hostname1",
2319 "/org/freedesktop/hostname1",
2320 "org.freedesktop.hostname1",
2321 "GetProductUUID",
2322 get_product_uuid_handler,
2323 m,
2324 "b",
2325 false);
2326 if (r < 0)
2327 return log_warning_errno(r, "Failed to get product UUID: %m");
2328
2329 return 0;
2330 }