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