]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b | 2 | /*** |
96b2fb93 | 3 | Copyright © 2013 Tom Gundersen <teg@jklm.no> |
f579559b TG |
4 | ***/ |
5 | ||
091a364c | 6 | #include <sys/socket.h> |
bbf7c048 | 7 | #include <linux/if.h> |
bce67bbe | 8 | #include <linux/fib_rules.h> |
0d536673 | 9 | #include <stdio_ext.h> |
3bef724f | 10 | |
fc2f9534 | 11 | #include "sd-daemon.h" |
07630cea | 12 | #include "sd-netlink.h" |
fc2f9534 | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
a97dcc12 | 15 | #include "bus-util.h" |
07630cea | 16 | #include "conf-parser.h" |
a97dcc12 | 17 | #include "def.h" |
482d1aeb | 18 | #include "dns-domain.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
0d39fa9c | 20 | #include "fileio.h" |
07630cea | 21 | #include "libudev-private.h" |
4f5f911e | 22 | #include "local-addresses.h" |
07630cea | 23 | #include "netlink-util.h" |
23f53b99 | 24 | #include "networkd-manager.h" |
00616955 | 25 | #include "ordered-set.h" |
07630cea LP |
26 | #include "path-util.h" |
27 | #include "set.h" | |
28 | #include "udev-util.h" | |
29 | #include "virt.h" | |
505f8da7 | 30 | |
be660c37 AR |
31 | /* use 8 MB for receive socket kernel queue. */ |
32 | #define RCVBUF_SIZE (8*1024*1024) | |
33 | ||
2ad8416d ZJS |
34 | const char* const network_dirs[] = { |
35 | "/etc/systemd/network", | |
36 | "/run/systemd/network", | |
37 | "/usr/lib/systemd/network", | |
349cc4a5 | 38 | #if HAVE_SPLIT_USR |
2ad8416d ZJS |
39 | "/lib/systemd/network", |
40 | #endif | |
41 | NULL}; | |
42 | ||
11bf3cce LP |
43 | static int setup_default_address_pool(Manager *m) { |
44 | AddressPool *p; | |
45 | int r; | |
46 | ||
47 | assert(m); | |
48 | ||
49 | /* Add in the well-known private address ranges. */ | |
50 | ||
51 | r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7); | |
52 | if (r < 0) | |
53 | return r; | |
54 | ||
55 | r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16); | |
56 | if (r < 0) | |
57 | return r; | |
58 | ||
59 | r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12); | |
60 | if (r < 0) | |
61 | return r; | |
62 | ||
63 | r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8); | |
64 | if (r < 0) | |
65 | return r; | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
9c0a72f9 TG |
70 | static int manager_reset_all(Manager *m) { |
71 | Link *link; | |
72 | Iterator i; | |
73 | int r; | |
74 | ||
75 | assert(m); | |
76 | ||
77 | HASHMAP_FOREACH(link, m->links, i) { | |
78 | r = link_carrier_reset(link); | |
79 | if (r < 0) | |
2f3cf1f9 | 80 | log_link_warning_errno(link, r, "Could not reset carrier: %m"); |
9c0a72f9 TG |
81 | } |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
19070062 | 86 | static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
9c0a72f9 TG |
87 | Manager *m = userdata; |
88 | int b, r; | |
89 | ||
19070062 | 90 | assert(message); |
d7afd945 | 91 | assert(m); |
9c0a72f9 TG |
92 | |
93 | r = sd_bus_message_read(message, "b", &b); | |
94 | if (r < 0) { | |
95 | log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m"); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | if (b) | |
100 | return 0; | |
101 | ||
102 | log_debug("Coming back from suspend, resetting all connections..."); | |
103 | ||
e1694a75 | 104 | (void) manager_reset_all(m); |
9c0a72f9 TG |
105 | |
106 | return 0; | |
107 | } | |
108 | ||
d7afd945 LP |
109 | static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
110 | Manager *m = userdata; | |
9c0a72f9 | 111 | |
d7afd945 | 112 | assert(message); |
9c0a72f9 TG |
113 | assert(m); |
114 | ||
d7afd945 LP |
115 | /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */ |
116 | if (m->dynamic_hostname) | |
117 | (void) manager_set_hostname(m, m->dynamic_hostname); | |
118 | if (m->dynamic_timezone) | |
119 | (void) manager_set_timezone(m, m->dynamic_timezone); | |
9c0a72f9 | 120 | |
d7afd945 LP |
121 | return 0; |
122 | } | |
9c0a72f9 | 123 | |
d7afd945 LP |
124 | int manager_connect_bus(Manager *m) { |
125 | int r; | |
126 | ||
127 | assert(m); | |
9c0a72f9 | 128 | |
d7afd945 | 129 | if (m->bus) |
9c0a72f9 | 130 | return 0; |
7d6884b6 | 131 | |
621e4509 | 132 | r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network"); |
9c0a72f9 | 133 | if (r < 0) |
d7afd945 | 134 | return log_error_errno(r, "Failed to connect to bus: %m"); |
9c0a72f9 | 135 | |
e331e246 TG |
136 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m); |
137 | if (r < 0) | |
138 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
139 | ||
140 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m); | |
141 | if (r < 0) | |
142 | return log_error_errno(r, "Failed to add link object vtable: %m"); | |
143 | ||
144 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m); | |
145 | if (r < 0) | |
146 | return log_error_errno(r, "Failed to add link enumerator: %m"); | |
3175fcde TG |
147 | |
148 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m); | |
149 | if (r < 0) | |
150 | return log_error_errno(r, "Failed to add network object vtable: %m"); | |
151 | ||
152 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m); | |
153 | if (r < 0) | |
154 | return log_error_errno(r, "Failed to add network enumerator: %m"); | |
e331e246 | 155 | |
d4e9e574 | 156 | r = bus_request_name_async_may_reload_dbus(m->bus, NULL, "org.freedesktop.network1", 0, NULL); |
e331e246 | 157 | if (r < 0) |
0c0b9306 | 158 | return log_error_errno(r, "Failed to request name: %m"); |
e331e246 TG |
159 | |
160 | r = sd_bus_attach_event(m->bus, m->event, 0); | |
161 | if (r < 0) | |
162 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
163 | ||
d7afd945 LP |
164 | r = sd_bus_match_signal_async( |
165 | m->bus, | |
166 | &m->connected_slot, | |
167 | "org.freedesktop.DBus.Local", | |
168 | NULL, | |
169 | "org.freedesktop.DBus.Local", | |
170 | "Connected", | |
171 | on_connected, NULL, m); | |
172 | if (r < 0) | |
173 | return log_error_errno(r, "Failed to request match on Connected signal: %m"); | |
174 | ||
175 | r = sd_bus_match_signal_async( | |
176 | m->bus, | |
177 | &m->prepare_for_sleep_slot, | |
178 | "org.freedesktop.login1", | |
179 | "/org/freedesktop/login1", | |
180 | "org.freedesktop.login1.Manager", | |
181 | "PrepareForSleep", | |
182 | match_prepare_for_sleep, NULL, m); | |
183 | if (r < 0) | |
184 | log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m"); | |
7901cea1 | 185 | |
9c0a72f9 TG |
186 | return 0; |
187 | } | |
188 | ||
5fae368b TG |
189 | static int manager_udev_process_link(Manager *m, struct udev_device *device) { |
190 | Link *link = NULL; | |
191 | int r, ifindex; | |
5544ee85 | 192 | |
5fae368b TG |
193 | assert(m); |
194 | assert(device); | |
5544ee85 | 195 | |
5fae368b TG |
196 | if (!streq_ptr(udev_device_get_action(device), "add")) |
197 | return 0; | |
5544ee85 | 198 | |
5fae368b TG |
199 | ifindex = udev_device_get_ifindex(device); |
200 | if (ifindex <= 0) { | |
2f3cf1f9 | 201 | log_debug("Ignoring udev ADD event for device with invalid ifindex"); |
5fae368b | 202 | return 0; |
5544ee85 TG |
203 | } |
204 | ||
5fae368b TG |
205 | r = link_get(m, ifindex, &link); |
206 | if (r == -ENODEV) | |
207 | return 0; | |
208 | else if (r < 0) | |
f579559b TG |
209 | return r; |
210 | ||
5fae368b | 211 | r = link_initialized(link, device); |
44de0efc LP |
212 | if (r < 0) |
213 | return r; | |
214 | ||
5fae368b TG |
215 | return 0; |
216 | } | |
be660c37 | 217 | |
5fae368b TG |
218 | static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
219 | Manager *m = userdata; | |
220 | struct udev_monitor *monitor = m->udev_monitor; | |
8e766630 | 221 | _cleanup_(udev_device_unrefp) struct udev_device *device = NULL; |
f579559b | 222 | |
5fae368b TG |
223 | device = udev_monitor_receive_device(monitor); |
224 | if (!device) | |
02b59d57 TG |
225 | return -ENOMEM; |
226 | ||
e1694a75 YW |
227 | (void) manager_udev_process_link(m, device); |
228 | ||
f579559b TG |
229 | return 0; |
230 | } | |
231 | ||
5fae368b TG |
232 | static int manager_connect_udev(Manager *m) { |
233 | int r; | |
f579559b | 234 | |
5fae368b TG |
235 | /* udev does not initialize devices inside containers, |
236 | * so we rely on them being already initialized before | |
237 | * entering the container */ | |
75f86906 | 238 | if (detect_container() > 0) |
5fae368b | 239 | return 0; |
f579559b | 240 | |
5fae368b TG |
241 | m->udev = udev_new(); |
242 | if (!m->udev) | |
243 | return -ENOMEM; | |
02b59d57 | 244 | |
5fae368b TG |
245 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
246 | if (!m->udev_monitor) | |
247 | return -ENOMEM; | |
02b59d57 | 248 | |
5fae368b | 249 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL); |
02b59d57 | 250 | if (r < 0) |
5fae368b | 251 | return log_error_errno(r, "Could not add udev monitor filter: %m"); |
02b59d57 | 252 | |
5fae368b TG |
253 | r = udev_monitor_enable_receiving(m->udev_monitor); |
254 | if (r < 0) { | |
255 | log_error("Could not enable udev monitor"); | |
02b59d57 | 256 | return r; |
667fcc6d | 257 | } |
505f8da7 | 258 | |
5fae368b TG |
259 | r = sd_event_add_io(m->event, |
260 | &m->udev_event_source, | |
261 | udev_monitor_get_fd(m->udev_monitor), | |
262 | EPOLLIN, manager_dispatch_link_udev, | |
263 | m); | |
264 | if (r < 0) | |
667fcc6d | 265 | return r; |
505f8da7 | 266 | |
5fae368b | 267 | r = sd_event_source_set_description(m->udev_event_source, "networkd-udev"); |
505f8da7 TG |
268 | if (r < 0) |
269 | return r; | |
11a7f229 | 270 | |
505f8da7 TG |
271 | return 0; |
272 | } | |
f579559b | 273 | |
1c8e710c TG |
274 | int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
275 | Manager *m = userdata; | |
276 | Link *link = NULL; | |
277 | uint16_t type; | |
278 | uint32_t ifindex, priority = 0; | |
983226f3 | 279 | unsigned char protocol, scope, tos, table, rt_type; |
1c8e710c TG |
280 | int family; |
281 | unsigned char dst_prefixlen, src_prefixlen; | |
282 | union in_addr_union dst = {}, gw = {}, src = {}, prefsrc = {}; | |
283 | Route *route = NULL; | |
284 | int r; | |
285 | ||
286 | assert(rtnl); | |
287 | assert(message); | |
288 | assert(m); | |
289 | ||
290 | if (sd_netlink_message_is_error(message)) { | |
291 | r = sd_netlink_message_get_errno(message); | |
292 | if (r < 0) | |
e1694a75 | 293 | log_warning_errno(r, "rtnl: failed to receive route, ignoring: %m"); |
1c8e710c TG |
294 | |
295 | return 0; | |
296 | } | |
297 | ||
298 | r = sd_netlink_message_get_type(message, &type); | |
299 | if (r < 0) { | |
e1694a75 | 300 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
1c8e710c | 301 | return 0; |
25a1bffc | 302 | } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) { |
e1694a75 | 303 | log_warning("rtnl: received unexpected message type when processing route, ignoring"); |
1c8e710c TG |
304 | return 0; |
305 | } | |
306 | ||
307 | r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex); | |
308 | if (r == -ENODATA) { | |
309 | log_debug("rtnl: received route without ifindex, ignoring"); | |
310 | return 0; | |
311 | } else if (r < 0) { | |
312 | log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m"); | |
313 | return 0; | |
314 | } else if (ifindex <= 0) { | |
315 | log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex); | |
316 | return 0; | |
317 | } else { | |
318 | r = link_get(m, ifindex, &link); | |
319 | if (r < 0 || !link) { | |
320 | /* when enumerating we might be out of sync, but we will | |
321 | * get the route again, so just ignore it */ | |
322 | if (!m->enumerating) | |
323 | log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex); | |
324 | return 0; | |
325 | } | |
326 | } | |
327 | ||
328 | r = sd_rtnl_message_route_get_family(message, &family); | |
329 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 330 | log_link_warning(link, "rtnl: received address with invalid family, ignoring"); |
1c8e710c TG |
331 | return 0; |
332 | } | |
333 | ||
334 | r = sd_rtnl_message_route_get_protocol(message, &protocol); | |
335 | if (r < 0) { | |
336 | log_warning_errno(r, "rtnl: could not get route protocol: %m"); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | switch (family) { | |
341 | case AF_INET: | |
342 | r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in); | |
343 | if (r < 0 && r != -ENODATA) { | |
344 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
345 | return 0; | |
346 | } | |
347 | ||
348 | r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &gw.in); | |
349 | if (r < 0 && r != -ENODATA) { | |
350 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | r = sd_netlink_message_read_in_addr(message, RTA_SRC, &src.in); | |
355 | if (r < 0 && r != -ENODATA) { | |
356 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
357 | return 0; | |
358 | } | |
359 | ||
360 | r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &prefsrc.in); | |
361 | if (r < 0 && r != -ENODATA) { | |
362 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
363 | return 0; | |
364 | } | |
365 | ||
366 | break; | |
367 | ||
368 | case AF_INET6: | |
369 | r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6); | |
370 | if (r < 0 && r != -ENODATA) { | |
371 | log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m"); | |
372 | return 0; | |
373 | } | |
374 | ||
375 | r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6); | |
376 | if (r < 0 && r != -ENODATA) { | |
377 | log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m"); | |
378 | return 0; | |
379 | } | |
380 | ||
381 | r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6); | |
382 | if (r < 0 && r != -ENODATA) { | |
383 | log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m"); | |
384 | return 0; | |
385 | } | |
386 | ||
387 | r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6); | |
388 | if (r < 0 && r != -ENODATA) { | |
389 | log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m"); | |
390 | return 0; | |
391 | } | |
392 | ||
393 | break; | |
394 | ||
395 | default: | |
74dead34 | 396 | assert_not_reached("Received unsupported address family"); |
1c8e710c TG |
397 | return 0; |
398 | } | |
399 | ||
400 | r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen); | |
401 | if (r < 0) { | |
402 | log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m"); | |
403 | return 0; | |
404 | } | |
405 | ||
406 | r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen); | |
407 | if (r < 0) { | |
408 | log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m"); | |
409 | return 0; | |
410 | } | |
411 | ||
412 | r = sd_rtnl_message_route_get_scope(message, &scope); | |
413 | if (r < 0) { | |
414 | log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m"); | |
415 | return 0; | |
416 | } | |
417 | ||
418 | r = sd_rtnl_message_route_get_tos(message, &tos); | |
419 | if (r < 0) { | |
420 | log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m"); | |
421 | return 0; | |
422 | } | |
423 | ||
983226f3 SS |
424 | r = sd_rtnl_message_route_get_type(message, &rt_type); |
425 | if (r < 0) { | |
426 | log_link_warning_errno(link, r, "rtnl: received route with invalid type, ignoring: %m"); | |
427 | return 0; | |
428 | } | |
429 | ||
1c8e710c TG |
430 | r = sd_rtnl_message_route_get_table(message, &table); |
431 | if (r < 0) { | |
432 | log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m"); | |
433 | return 0; | |
434 | } | |
435 | ||
436 | r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority); | |
437 | if (r < 0 && r != -ENODATA) { | |
438 | log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m"); | |
439 | return 0; | |
440 | } | |
441 | ||
e1694a75 | 442 | (void) route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route); |
1c8e710c TG |
443 | |
444 | switch (type) { | |
445 | case RTM_NEWROUTE: | |
446 | if (!route) { | |
447 | /* A route appeared that we did not request */ | |
448 | r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route); | |
e1694a75 YW |
449 | if (r < 0) { |
450 | log_link_warning_errno(link, r, "Failed to add route, ignoring: %m"); | |
1c8e710c | 451 | return 0; |
e1694a75 | 452 | } |
1c8e710c TG |
453 | } |
454 | ||
bbd15900 | 455 | route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol, rt_type); |
1c8e710c TG |
456 | |
457 | break; | |
458 | ||
459 | case RTM_DELROUTE: | |
b9642f41 | 460 | route_free(route); |
1c8e710c | 461 | break; |
b9642f41 | 462 | |
1c8e710c TG |
463 | default: |
464 | assert_not_reached("Received invalid RTNL message type"); | |
465 | } | |
466 | ||
467 | return 1; | |
468 | } | |
469 | ||
200a0868 TG |
470 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
471 | Manager *m = userdata; | |
472 | Link *link = NULL; | |
473 | uint16_t type; | |
200a0868 | 474 | unsigned char flags; |
054f0db4 TG |
475 | int family; |
476 | unsigned char prefixlen; | |
477 | unsigned char scope; | |
478 | union in_addr_union in_addr; | |
479 | struct ifa_cacheinfo cinfo; | |
480 | Address *address = NULL; | |
200a0868 TG |
481 | char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX]; |
482 | const char *valid_str = NULL; | |
483 | int r, ifindex; | |
484 | ||
485 | assert(rtnl); | |
486 | assert(message); | |
487 | assert(m); | |
488 | ||
489 | if (sd_netlink_message_is_error(message)) { | |
490 | r = sd_netlink_message_get_errno(message); | |
491 | if (r < 0) | |
e1694a75 | 492 | log_warning_errno(r, "rtnl: failed to receive address, ignoring: %m"); |
200a0868 TG |
493 | |
494 | return 0; | |
495 | } | |
496 | ||
497 | r = sd_netlink_message_get_type(message, &type); | |
498 | if (r < 0) { | |
e1694a75 | 499 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
200a0868 | 500 | return 0; |
25a1bffc | 501 | } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) { |
e1694a75 | 502 | log_warning("rtnl: received unexpected message type when processing address, ignoring"); |
200a0868 TG |
503 | return 0; |
504 | } | |
505 | ||
506 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
507 | if (r < 0) { | |
e1694a75 | 508 | log_warning_errno(r, "rtnl: could not get ifindex from address, ignoring: %m"); |
200a0868 TG |
509 | return 0; |
510 | } else if (ifindex <= 0) { | |
e1694a75 | 511 | log_warning("rtnl: received address message with invalid ifindex, ignoring: %d", ifindex); |
200a0868 TG |
512 | return 0; |
513 | } else { | |
514 | r = link_get(m, ifindex, &link); | |
515 | if (r < 0 || !link) { | |
516 | /* when enumerating we might be out of sync, but we will | |
517 | * get the address again, so just ignore it */ | |
518 | if (!m->enumerating) | |
519 | log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex); | |
520 | return 0; | |
521 | } | |
522 | } | |
523 | ||
054f0db4 TG |
524 | r = sd_rtnl_message_addr_get_family(message, &family); |
525 | if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 526 | log_link_warning(link, "rtnl: received address with invalid family, ignoring"); |
200a0868 TG |
527 | return 0; |
528 | } | |
529 | ||
054f0db4 | 530 | r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen); |
200a0868 TG |
531 | if (r < 0) { |
532 | log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m"); | |
533 | return 0; | |
534 | } | |
535 | ||
054f0db4 | 536 | r = sd_rtnl_message_addr_get_scope(message, &scope); |
200a0868 TG |
537 | if (r < 0) { |
538 | log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m"); | |
539 | return 0; | |
540 | } | |
541 | ||
542 | r = sd_rtnl_message_addr_get_flags(message, &flags); | |
543 | if (r < 0) { | |
544 | log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m"); | |
545 | return 0; | |
546 | } | |
200a0868 | 547 | |
054f0db4 | 548 | switch (family) { |
200a0868 | 549 | case AF_INET: |
054f0db4 | 550 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in); |
200a0868 TG |
551 | if (r < 0) { |
552 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | break; | |
557 | ||
558 | case AF_INET6: | |
054f0db4 | 559 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6); |
200a0868 TG |
560 | if (r < 0) { |
561 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); | |
562 | return 0; | |
563 | } | |
564 | ||
565 | break; | |
566 | ||
567 | default: | |
e1694a75 | 568 | assert_not_reached("Received unsupported address family"); |
200a0868 TG |
569 | } |
570 | ||
054f0db4 | 571 | if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) { |
e1694a75 | 572 | log_link_warning(link, "Could not print address, ignoring"); |
200a0868 TG |
573 | return 0; |
574 | } | |
575 | ||
054f0db4 | 576 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
e1694a75 YW |
577 | if (r < 0 && r != -ENODATA) { |
578 | log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m"); | |
579 | return 0; | |
580 | } else if (r >= 0) { | |
4058d339 | 581 | if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) |
200a0868 | 582 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, |
054f0db4 | 583 | cinfo.ifa_valid * USEC_PER_SEC, |
200a0868 TG |
584 | USEC_PER_SEC); |
585 | } | |
586 | ||
e1694a75 | 587 | (void) address_get(link, family, &in_addr, prefixlen, &address); |
200a0868 TG |
588 | |
589 | switch (type) { | |
590 | case RTM_NEWADDR: | |
36c32f61 | 591 | if (address) |
4058d339 | 592 | log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 593 | valid_str ? "for " : "forever", strempty(valid_str)); |
36c32f61 | 594 | else { |
adda1ed9 TG |
595 | /* An address appeared that we did not request */ |
596 | r = address_add_foreign(link, family, &in_addr, prefixlen, &address); | |
cf1d700d | 597 | if (r < 0) { |
e1694a75 | 598 | log_link_warning_errno(link, r, "Failed to add address %s/%u, ignoring: %m", buf, prefixlen); |
cf1d700d TG |
599 | return 0; |
600 | } else | |
4058d339 | 601 | log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 602 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
603 | } |
604 | ||
e1694a75 YW |
605 | r = address_update(address, flags, scope, &cinfo); |
606 | if (r < 0) { | |
607 | log_link_warning_errno(link, r, "Failed to update address %s/%u, ignoring: %m", buf, prefixlen); | |
608 | return 0; | |
609 | } | |
36c32f61 | 610 | |
200a0868 TG |
611 | break; |
612 | ||
613 | case RTM_DELADDR: | |
614 | ||
054f0db4 | 615 | if (address) { |
4058d339 | 616 | log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen, |
87e4e28d | 617 | valid_str ? "for " : "forever", strempty(valid_str)); |
e1694a75 | 618 | (void) address_drop(address); |
200a0868 | 619 | } else |
e1694a75 | 620 | log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s), ignoring", buf, prefixlen, |
87e4e28d | 621 | valid_str ? "for " : "forever", strempty(valid_str)); |
200a0868 TG |
622 | |
623 | break; | |
624 | default: | |
625 | assert_not_reached("Received invalid RTNL message type"); | |
626 | } | |
627 | ||
628 | return 1; | |
629 | } | |
630 | ||
1c4baffc | 631 | static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
505f8da7 TG |
632 | Manager *m = userdata; |
633 | Link *link = NULL; | |
4d473d5d | 634 | NetDev *netdev = NULL; |
f2236469 | 635 | uint16_t type; |
ca4e095a | 636 | const char *name; |
505f8da7 | 637 | int r, ifindex; |
f579559b | 638 | |
505f8da7 TG |
639 | assert(rtnl); |
640 | assert(message); | |
f579559b TG |
641 | assert(m); |
642 | ||
1c4baffc TG |
643 | if (sd_netlink_message_is_error(message)) { |
644 | r = sd_netlink_message_get_errno(message); | |
45af44d4 | 645 | if (r < 0) |
e1694a75 | 646 | log_warning_errno(r, "rtnl: Could not receive link, ignoring: %m"); |
45af44d4 TG |
647 | |
648 | return 0; | |
649 | } | |
650 | ||
1c4baffc | 651 | r = sd_netlink_message_get_type(message, &type); |
f2236469 | 652 | if (r < 0) { |
e1694a75 | 653 | log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); |
f2236469 | 654 | return 0; |
25a1bffc | 655 | } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) { |
e1694a75 | 656 | log_warning("rtnl: Received unexpected message type when processing link, ignoring"); |
cdfee943 | 657 | return 0; |
f2236469 TG |
658 | } |
659 | ||
505f8da7 | 660 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); |
45af44d4 | 661 | if (r < 0) { |
e1694a75 | 662 | log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m"); |
45af44d4 TG |
663 | return 0; |
664 | } else if (ifindex <= 0) { | |
e1694a75 | 665 | log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex); |
505f8da7 | 666 | return 0; |
a0db8e46 | 667 | } |
f579559b | 668 | |
1c4baffc | 669 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name); |
45af44d4 | 670 | if (r < 0) { |
e1694a75 | 671 | log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); |
4d473d5d | 672 | return 0; |
a0db8e46 ZJS |
673 | } |
674 | ||
675 | (void) link_get(m, ifindex, &link); | |
676 | (void) netdev_get(m, name, &netdev); | |
4d473d5d TG |
677 | |
678 | switch (type) { | |
679 | case RTM_NEWLINK: | |
680 | if (!link) { | |
681 | /* link is new, so add it */ | |
682 | r = link_add(m, message, &link); | |
683 | if (r < 0) { | |
e1694a75 | 684 | log_warning_errno(r, "Could not add new link, ignoring: %m"); |
4d473d5d TG |
685 | return 0; |
686 | } | |
687 | } | |
688 | ||
689 | if (netdev) { | |
690 | /* netdev exists, so make sure the ifindex matches */ | |
505f8da7 TG |
691 | r = netdev_set_ifindex(netdev, message); |
692 | if (r < 0) { | |
e1694a75 | 693 | log_warning_errno(r, "Could not set ifindex on netdev, ignoring: %m"); |
505f8da7 TG |
694 | return 0; |
695 | } | |
696 | } | |
e1202047 | 697 | |
f2236469 | 698 | r = link_update(link, message); |
e1694a75 YW |
699 | if (r < 0) { |
700 | log_warning_errno(r, "Could not update link, ignoring: %m"); | |
f2236469 | 701 | return 0; |
e1694a75 | 702 | } |
4d473d5d TG |
703 | |
704 | break; | |
705 | ||
706 | case RTM_DELLINK: | |
707 | link_drop(link); | |
708 | netdev_drop(netdev); | |
709 | ||
710 | break; | |
711 | ||
712 | default: | |
713 | assert_not_reached("Received invalid RTNL message type."); | |
f2236469 | 714 | } |
505f8da7 TG |
715 | |
716 | return 1; | |
717 | } | |
718 | ||
bce67bbe SS |
719 | int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) { |
720 | uint8_t tos = 0, to_prefixlen = 0, from_prefixlen = 0; | |
36e6e28b | 721 | union in_addr_union to = {}, from = {}; |
bce67bbe | 722 | RoutingPolicyRule *rule = NULL; |
bce67bbe | 723 | uint32_t fwmark = 0, table = 0; |
36e6e28b | 724 | char *iif = NULL, *oif = NULL; |
bce67bbe SS |
725 | Manager *m = userdata; |
726 | uint16_t type; | |
727 | int family; | |
728 | int r; | |
729 | ||
730 | assert(rtnl); | |
731 | assert(message); | |
732 | assert(m); | |
733 | ||
734 | if (sd_netlink_message_is_error(message)) { | |
735 | r = sd_netlink_message_get_errno(message); | |
736 | if (r < 0) | |
e1694a75 | 737 | log_warning_errno(r, "rtnl: failed to receive rule, ignoring: %m"); |
bce67bbe SS |
738 | |
739 | return 0; | |
740 | } | |
741 | ||
742 | r = sd_netlink_message_get_type(message, &type); | |
743 | if (r < 0) { | |
e1694a75 | 744 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); |
bce67bbe SS |
745 | return 0; |
746 | } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) { | |
e1694a75 | 747 | log_warning("rtnl: received unexpected message type '%u' when processing rule, ignoring", type); |
bce67bbe SS |
748 | return 0; |
749 | } | |
750 | ||
751 | r = sd_rtnl_message_get_family(message, &family); | |
615ded62 | 752 | if (r < 0) { |
e1694a75 | 753 | log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m"); |
615ded62 YW |
754 | return 0; |
755 | } else if (!IN_SET(family, AF_INET, AF_INET6)) { | |
e1694a75 | 756 | log_debug("rtnl: received address with invalid family %u, ignoring", family); |
bce67bbe SS |
757 | return 0; |
758 | } | |
759 | ||
760 | switch (family) { | |
761 | case AF_INET: | |
762 | r = sd_netlink_message_read_in_addr(message, FRA_SRC, &from.in); | |
e1694a75 YW |
763 | if (r < 0 && r != -ENODATA) { |
764 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
765 | return 0; | |
766 | } else if (r >= 0) { | |
bce67bbe | 767 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen); |
e1694a75 | 768 | if (r < 0) { |
3fe91079 | 769 | log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m"); |
e1694a75 YW |
770 | return 0; |
771 | } | |
bce67bbe SS |
772 | } |
773 | ||
774 | r = sd_netlink_message_read_in_addr(message, FRA_DST, &to.in); | |
e1694a75 YW |
775 | if (r < 0 && r != -ENODATA) { |
776 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
777 | return 0; | |
778 | } else if (r >= 0) { | |
bce67bbe | 779 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen); |
e1694a75 | 780 | if (r < 0) { |
3fe91079 | 781 | log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m"); |
e1694a75 YW |
782 | return 0; |
783 | } | |
bce67bbe SS |
784 | } |
785 | ||
786 | break; | |
787 | ||
788 | case AF_INET6: | |
789 | r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &from.in6); | |
e1694a75 YW |
790 | if (r < 0 && r != -ENODATA) { |
791 | log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m"); | |
792 | return 0; | |
793 | } else if (r >= 0) { | |
bce67bbe | 794 | r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen); |
e1694a75 | 795 | if (r < 0) { |
3fe91079 | 796 | log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m"); |
e1694a75 YW |
797 | return 0; |
798 | } | |
bce67bbe SS |
799 | } |
800 | ||
801 | r = sd_netlink_message_read_in6_addr(message, FRA_DST, &to.in6); | |
e1694a75 YW |
802 | if (r < 0 && r != -ENODATA) { |
803 | log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m"); | |
804 | return 0; | |
805 | } else if (r >= 0) { | |
bce67bbe | 806 | r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen); |
e1694a75 | 807 | if (r < 0) { |
3fe91079 | 808 | log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m"); |
e1694a75 YW |
809 | return 0; |
810 | } | |
bce67bbe SS |
811 | } |
812 | ||
813 | break; | |
814 | ||
815 | default: | |
74dead34 | 816 | assert_not_reached("Received unsupported address family"); |
bce67bbe SS |
817 | } |
818 | ||
819 | if (from_prefixlen == 0 && to_prefixlen == 0) | |
820 | return 0; | |
821 | ||
e1694a75 YW |
822 | r = sd_netlink_message_read_u32(message, FRA_FWMARK, &fwmark); |
823 | if (r < 0 && r != -ENODATA) { | |
824 | log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m"); | |
825 | return 0; | |
826 | } | |
827 | ||
828 | r = sd_netlink_message_read_u32(message, FRA_TABLE, &table); | |
829 | if (r < 0 && r != -ENODATA) { | |
830 | log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m"); | |
831 | return 0; | |
832 | } | |
833 | ||
834 | r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tos); | |
835 | if (r < 0 && r != -ENODATA) { | |
836 | log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m"); | |
837 | return 0; | |
838 | } | |
839 | ||
840 | r = sd_netlink_message_read_string(message, FRA_IIFNAME, (const char **) &iif); | |
841 | if (r < 0 && r != -ENODATA) { | |
842 | log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m"); | |
843 | return 0; | |
844 | } | |
845 | ||
846 | r = sd_netlink_message_read_string(message, FRA_OIFNAME, (const char **) &oif); | |
847 | if (r < 0 && r != -ENODATA) { | |
848 | log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m"); | |
849 | return 0; | |
850 | } | |
bce67bbe | 851 | |
762e2659 | 852 | (void) routing_policy_rule_get(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, &rule); |
bce67bbe SS |
853 | |
854 | switch (type) { | |
855 | case RTM_NEWRULE: | |
508f63b4 | 856 | if (!rule) { |
762e2659 | 857 | r = routing_policy_rule_add_foreign(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, &rule); |
bce67bbe | 858 | if (r < 0) { |
e1694a75 | 859 | log_warning_errno(r, "Could not add rule, ignoring: %m"); |
bce67bbe SS |
860 | return 0; |
861 | } | |
862 | } | |
863 | break; | |
864 | case RTM_DELRULE: | |
865 | routing_policy_rule_free(rule); | |
866 | ||
867 | break; | |
868 | ||
869 | default: | |
870 | assert_not_reached("Received invalid RTNL message type"); | |
871 | } | |
872 | ||
873 | return 1; | |
874 | } | |
875 | ||
5fae368b TG |
876 | static int systemd_netlink_fd(void) { |
877 | int n, fd, rtnl_fd = -EINVAL; | |
878 | ||
879 | n = sd_listen_fds(true); | |
880 | if (n <= 0) | |
881 | return -EINVAL; | |
882 | ||
883 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
884 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { | |
885 | if (rtnl_fd >= 0) | |
886 | return -EINVAL; | |
887 | ||
888 | rtnl_fd = fd; | |
889 | } | |
890 | } | |
891 | ||
892 | return rtnl_fd; | |
893 | } | |
894 | ||
05d0c2e3 JT |
895 | static int manager_connect_genl(Manager *m) { |
896 | int r; | |
897 | ||
898 | assert(m); | |
899 | ||
900 | r = sd_genl_socket_open(&m->genl); | |
901 | if (r < 0) | |
902 | return r; | |
903 | ||
904 | r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE); | |
905 | if (r < 0) | |
906 | return r; | |
907 | ||
908 | r = sd_netlink_attach_event(m->genl, m->event, 0); | |
909 | if (r < 0) | |
910 | return r; | |
911 | ||
912 | return 0; | |
913 | } | |
914 | ||
5fae368b TG |
915 | static int manager_connect_rtnl(Manager *m) { |
916 | int fd, r; | |
505f8da7 TG |
917 | |
918 | assert(m); | |
505f8da7 | 919 | |
5fae368b TG |
920 | fd = systemd_netlink_fd(); |
921 | if (fd < 0) | |
1c4baffc | 922 | r = sd_netlink_open(&m->rtnl); |
5fae368b | 923 | else |
1c4baffc | 924 | r = sd_netlink_open_fd(&m->rtnl, fd); |
505f8da7 TG |
925 | if (r < 0) |
926 | return r; | |
927 | ||
1c4baffc | 928 | r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); |
f579559b | 929 | if (r < 0) |
bf5332d2 | 930 | return r; |
f579559b | 931 | |
1c4baffc | 932 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
505f8da7 TG |
933 | if (r < 0) |
934 | return r; | |
f579559b | 935 | |
1c4baffc | 936 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
937 | if (r < 0) |
938 | return r; | |
505f8da7 | 939 | |
1c4baffc | 940 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); |
5fae368b TG |
941 | if (r < 0) |
942 | return r; | |
45af44d4 | 943 | |
200a0868 | 944 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
945 | if (r < 0) |
946 | return r; | |
947 | ||
200a0868 | 948 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m); |
5fae368b TG |
949 | if (r < 0) |
950 | return r; | |
951 | ||
1c8e710c TG |
952 | r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m); |
953 | if (r < 0) | |
954 | return r; | |
955 | ||
956 | r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m); | |
957 | if (r < 0) | |
958 | return r; | |
959 | ||
bce67bbe SS |
960 | r = sd_netlink_add_match(m->rtnl, RTM_NEWRULE, &manager_rtnl_process_rule, m); |
961 | if (r < 0) | |
962 | return r; | |
963 | ||
964 | r = sd_netlink_add_match(m->rtnl, RTM_DELRULE, &manager_rtnl_process_rule, m); | |
965 | if (r < 0) | |
966 | return r; | |
967 | ||
5fae368b | 968 | return 0; |
45af44d4 | 969 | } |
505f8da7 | 970 | |
5512a963 | 971 | static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) { |
84de38c5 TG |
972 | char *p; |
973 | int r; | |
974 | ||
975 | assert(s); | |
5512a963 LP |
976 | assert(address); |
977 | ||
978 | r = in_addr_to_string(address->family, &address->address, &p); | |
979 | if (r < 0) | |
980 | return r; | |
981 | ||
982 | r = ordered_set_consume(s, p); | |
983 | if (r == -EEXIST) | |
984 | return 0; | |
985 | ||
986 | return r; | |
987 | } | |
988 | ||
989 | static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) { | |
990 | int r, c = 0; | |
991 | unsigned i; | |
992 | ||
993 | assert(s); | |
994 | assert(addresses || n == 0); | |
995 | ||
996 | for (i = 0; i < n; i++) { | |
997 | r = ordered_set_put_in_addr_data(s, addresses+i); | |
998 | if (r < 0) | |
999 | return r; | |
1000 | ||
1001 | c += r; | |
1002 | } | |
1003 | ||
1004 | return c; | |
1005 | } | |
1006 | ||
1007 | static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) { | |
1008 | char *p; | |
1009 | int r; | |
1010 | ||
1011 | assert(s); | |
1012 | assert(address); | |
84de38c5 TG |
1013 | |
1014 | r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p); | |
1015 | if (r < 0) | |
1016 | return r; | |
1017 | ||
00616955 | 1018 | r = ordered_set_consume(s, p); |
84de38c5 TG |
1019 | if (r == -EEXIST) |
1020 | return 0; | |
1021 | ||
1022 | return r; | |
1023 | } | |
1024 | ||
5512a963 LP |
1025 | static int ordered_set_put_in4_addrv(OrderedSet *s, const struct in_addr *addresses, unsigned n) { |
1026 | int r, c = 0; | |
1027 | unsigned i; | |
84de38c5 TG |
1028 | |
1029 | assert(s); | |
5512a963 | 1030 | assert(n == 0 || addresses); |
84de38c5 TG |
1031 | |
1032 | for (i = 0; i < n; i++) { | |
5512a963 | 1033 | r = ordered_set_put_in4_addr(s, addresses+i); |
84de38c5 TG |
1034 | if (r < 0) |
1035 | return r; | |
1036 | ||
1037 | c += r; | |
1038 | } | |
1039 | ||
1040 | return c; | |
1041 | } | |
1042 | ||
00616955 | 1043 | static void print_string_set(FILE *f, const char *field, OrderedSet *s) { |
84de38c5 TG |
1044 | bool space = false; |
1045 | Iterator i; | |
1046 | char *p; | |
1047 | ||
00616955 | 1048 | if (ordered_set_isempty(s)) |
84de38c5 TG |
1049 | return; |
1050 | ||
0d536673 | 1051 | fputs(field, f); |
84de38c5 | 1052 | |
d390f8ef LP |
1053 | ORDERED_SET_FOREACH(p, s, i) |
1054 | fputs_with_space(f, p, NULL, &space); | |
1055 | ||
0d536673 | 1056 | fputc('\n', f); |
84de38c5 TG |
1057 | } |
1058 | ||
1059 | static int manager_save(Manager *m) { | |
00616955 | 1060 | _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL; |
84de38c5 TG |
1061 | Link *link; |
1062 | Iterator i; | |
1063 | _cleanup_free_ char *temp_path = NULL; | |
1064 | _cleanup_fclose_ FILE *f = NULL; | |
1065 | LinkOperationalState operstate = LINK_OPERSTATE_OFF; | |
1066 | const char *operstate_str; | |
1067 | int r; | |
1068 | ||
1069 | assert(m); | |
1070 | assert(m->state_file); | |
1071 | ||
1072 | /* We add all NTP and DNS server to a set, to filter out duplicates */ | |
00616955 | 1073 | dns = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1074 | if (!dns) |
1075 | return -ENOMEM; | |
1076 | ||
00616955 | 1077 | ntp = ordered_set_new(&string_hash_ops); |
84de38c5 TG |
1078 | if (!ntp) |
1079 | return -ENOMEM; | |
1080 | ||
482d1aeb | 1081 | search_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 LP |
1082 | if (!search_domains) |
1083 | return -ENOMEM; | |
1084 | ||
482d1aeb | 1085 | route_domains = ordered_set_new(&dns_name_hash_ops); |
3df9bec5 | 1086 | if (!route_domains) |
84de38c5 TG |
1087 | return -ENOMEM; |
1088 | ||
1089 | HASHMAP_FOREACH(link, m->links, i) { | |
1090 | if (link->flags & IFF_LOOPBACK) | |
1091 | continue; | |
1092 | ||
1093 | if (link->operstate > operstate) | |
1094 | operstate = link->operstate; | |
1095 | ||
1096 | if (!link->network) | |
1097 | continue; | |
1098 | ||
1099 | /* First add the static configured entries */ | |
5512a963 | 1100 | r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns); |
84de38c5 TG |
1101 | if (r < 0) |
1102 | return r; | |
1103 | ||
00616955 | 1104 | r = ordered_set_put_strdupv(ntp, link->network->ntp); |
84de38c5 TG |
1105 | if (r < 0) |
1106 | return r; | |
1107 | ||
00616955 | 1108 | r = ordered_set_put_strdupv(search_domains, link->network->search_domains); |
3df9bec5 LP |
1109 | if (r < 0) |
1110 | return r; | |
1111 | ||
00616955 | 1112 | r = ordered_set_put_strdupv(route_domains, link->network->route_domains); |
84de38c5 TG |
1113 | if (r < 0) |
1114 | return r; | |
1115 | ||
1116 | if (!link->dhcp_lease) | |
1117 | continue; | |
1118 | ||
1119 | /* Secondly, add the entries acquired via DHCP */ | |
27cb34f5 | 1120 | if (link->network->dhcp_use_dns) { |
84de38c5 TG |
1121 | const struct in_addr *addresses; |
1122 | ||
1123 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
1124 | if (r > 0) { | |
5512a963 | 1125 | r = ordered_set_put_in4_addrv(dns, addresses, r); |
84de38c5 TG |
1126 | if (r < 0) |
1127 | return r; | |
1128 | } else if (r < 0 && r != -ENODATA) | |
1129 | return r; | |
1130 | } | |
1131 | ||
27cb34f5 | 1132 | if (link->network->dhcp_use_ntp) { |
84de38c5 TG |
1133 | const struct in_addr *addresses; |
1134 | ||
1135 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
1136 | if (r > 0) { | |
5512a963 | 1137 | r = ordered_set_put_in4_addrv(ntp, addresses, r); |
84de38c5 TG |
1138 | if (r < 0) |
1139 | return r; | |
1140 | } else if (r < 0 && r != -ENODATA) | |
1141 | return r; | |
1142 | } | |
1143 | ||
b2a81c0b | 1144 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
84de38c5 | 1145 | const char *domainname; |
b85bc551 | 1146 | char **domains = NULL; |
84de38c5 | 1147 | |
b85bc551 | 1148 | OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains; |
84de38c5 TG |
1149 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); |
1150 | if (r >= 0) { | |
b85bc551 DW |
1151 | r = ordered_set_put_strdup(target_domains, domainname); |
1152 | if (r < 0) | |
1153 | return r; | |
1154 | } else if (r != -ENODATA) | |
1155 | return r; | |
b2a81c0b | 1156 | |
b85bc551 DW |
1157 | r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains); |
1158 | if (r >= 0) { | |
1159 | r = ordered_set_put_strdupv(target_domains, domains); | |
84de38c5 TG |
1160 | if (r < 0) |
1161 | return r; | |
1162 | } else if (r != -ENODATA) | |
1163 | return r; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | operstate_str = link_operstate_to_string(operstate); | |
1168 | assert(operstate_str); | |
1169 | ||
1170 | r = fopen_temporary(m->state_file, &f, &temp_path); | |
1171 | if (r < 0) | |
1172 | return r; | |
1173 | ||
0d536673 | 1174 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 1175 | (void) fchmod(fileno(f), 0644); |
84de38c5 TG |
1176 | |
1177 | fprintf(f, | |
1178 | "# This is private data. Do not parse.\n" | |
1179 | "OPER_STATE=%s\n", operstate_str); | |
1180 | ||
1181 | print_string_set(f, "DNS=", dns); | |
1182 | print_string_set(f, "NTP=", ntp); | |
3df9bec5 LP |
1183 | print_string_set(f, "DOMAINS=", search_domains); |
1184 | print_string_set(f, "ROUTE_DOMAINS=", route_domains); | |
84de38c5 | 1185 | |
458d8ae3 ZJS |
1186 | r = routing_policy_serialize_rules(m->rules, f); |
1187 | if (r < 0) | |
1188 | goto fail; | |
bce67bbe | 1189 | |
84de38c5 TG |
1190 | r = fflush_and_check(f); |
1191 | if (r < 0) | |
1192 | goto fail; | |
1193 | ||
1194 | if (rename(temp_path, m->state_file) < 0) { | |
1195 | r = -errno; | |
1196 | goto fail; | |
1197 | } | |
1198 | ||
1199 | if (m->operational_state != operstate) { | |
1200 | m->operational_state = operstate; | |
1201 | r = manager_send_changed(m, "OperationalState", NULL); | |
1202 | if (r < 0) | |
1203 | log_error_errno(r, "Could not emit changed OperationalState: %m"); | |
1204 | } | |
1205 | ||
1206 | m->dirty = false; | |
1207 | ||
1208 | return 0; | |
1209 | ||
1210 | fail: | |
1211 | (void) unlink(m->state_file); | |
1212 | (void) unlink(temp_path); | |
1213 | ||
1214 | return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file); | |
1215 | } | |
1216 | ||
1217 | static int manager_dirty_handler(sd_event_source *s, void *userdata) { | |
1218 | Manager *m = userdata; | |
1219 | Link *link; | |
1220 | Iterator i; | |
1221 | int r; | |
1222 | ||
1223 | assert(m); | |
1224 | ||
1225 | if (m->dirty) | |
1226 | manager_save(m); | |
1227 | ||
1228 | SET_FOREACH(link, m->dirty_links, i) { | |
1229 | r = link_save(link); | |
1230 | if (r >= 0) | |
1231 | link_clean(link); | |
1232 | } | |
1233 | ||
1234 | return 1; | |
1235 | } | |
1236 | ||
e133b289 PF |
1237 | Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) { |
1238 | assert_return(m, NULL); | |
1239 | assert_return(m->dhcp6_prefixes, NULL); | |
1240 | assert_return(addr, NULL); | |
1241 | ||
1242 | return hashmap_get(m->dhcp6_prefixes, addr); | |
1243 | } | |
1244 | ||
9f386c6d PF |
1245 | static int dhcp6_route_add_callback(sd_netlink *nl, sd_netlink_message *m, |
1246 | void *userdata) { | |
1247 | Link *l = userdata; | |
1248 | int r; | |
1249 | union in_addr_union prefix; | |
1250 | _cleanup_free_ char *buf = NULL; | |
1251 | ||
1252 | r = sd_netlink_message_get_errno(m); | |
1253 | if (r != 0) { | |
1254 | log_link_debug_errno(l, r, "Received error adding DHCPv6 Prefix Delegation route: %m"); | |
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | r = sd_netlink_message_read_in6_addr(m, RTA_DST, &prefix.in6); | |
1259 | if (r < 0) { | |
1260 | log_link_debug_errno(l, r, "Could not read IPv6 address from DHCPv6 Prefix Delegation while adding route: %m"); | |
1261 | return 0; | |
1262 | } | |
1263 | ||
1264 | (void) in_addr_to_string(AF_INET6, &prefix, &buf); | |
1265 | log_link_debug(l, "Added DHCPv6 Prefix Deleagtion route %s/64", | |
1266 | strnull(buf)); | |
1267 | ||
1268 | return 0; | |
1269 | } | |
1270 | ||
e133b289 | 1271 | int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) { |
9f386c6d PF |
1272 | int r; |
1273 | Route *route; | |
1274 | ||
e133b289 PF |
1275 | assert_return(m, -EINVAL); |
1276 | assert_return(m->dhcp6_prefixes, -ENODATA); | |
1277 | assert_return(addr, -EINVAL); | |
1278 | ||
9f386c6d PF |
1279 | r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64, |
1280 | 0, 0, 0, &route); | |
1281 | if (r < 0) | |
1282 | return r; | |
1283 | ||
1284 | r = route_configure(route, link, dhcp6_route_add_callback); | |
1285 | if (r < 0) | |
1286 | return r; | |
1287 | ||
e133b289 PF |
1288 | return hashmap_put(m->dhcp6_prefixes, addr, link); |
1289 | } | |
1290 | ||
9f386c6d PF |
1291 | static int dhcp6_route_remove_callback(sd_netlink *nl, sd_netlink_message *m, |
1292 | void *userdata) { | |
1293 | Link *l = userdata; | |
1294 | int r; | |
1295 | union in_addr_union prefix; | |
1296 | _cleanup_free_ char *buf = NULL; | |
1297 | ||
1298 | r = sd_netlink_message_get_errno(m); | |
1299 | if (r != 0) { | |
1300 | log_link_debug_errno(l, r, "Received error on DHCPv6 Prefix Delegation route removal: %m"); | |
1301 | return 0; | |
1302 | } | |
1303 | ||
1304 | r = sd_netlink_message_read_in6_addr(m, RTA_DST, &prefix.in6); | |
1305 | if (r < 0) { | |
1306 | log_link_debug_errno(l, r, "Could not read IPv6 address from DHCPv6 Prefix Delegation while removing route: %m"); | |
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | (void) in_addr_to_string(AF_INET6, &prefix, &buf); | |
1311 | log_link_debug(l, "Removed DHCPv6 Prefix Delegation route %s/64", | |
1312 | strnull(buf)); | |
1313 | ||
1314 | return 0; | |
1315 | } | |
1316 | ||
e133b289 PF |
1317 | int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) { |
1318 | Link *l; | |
9f386c6d PF |
1319 | int r; |
1320 | Route *route; | |
e133b289 PF |
1321 | |
1322 | assert_return(m, -EINVAL); | |
1323 | assert_return(m->dhcp6_prefixes, -ENODATA); | |
1324 | assert_return(addr, -EINVAL); | |
1325 | ||
1326 | l = hashmap_remove(m->dhcp6_prefixes, addr); | |
1327 | if (!l) | |
1328 | return -EINVAL; | |
1329 | ||
1330 | (void) sd_radv_remove_prefix(l->radv, addr, 64); | |
9f386c6d PF |
1331 | r = route_get(l, AF_INET6, (union in_addr_union *) addr, 64, |
1332 | 0, 0, 0, &route); | |
1333 | if (r >= 0) | |
1334 | (void) route_remove(route, l, dhcp6_route_remove_callback); | |
e133b289 PF |
1335 | |
1336 | return 0; | |
1337 | } | |
1338 | ||
1339 | int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) { | |
1340 | Iterator i; | |
1341 | Link *l; | |
1342 | struct in6_addr *addr; | |
1343 | ||
1344 | assert_return(m, -EINVAL); | |
b7d16a91 | 1345 | assert_return(link, -EINVAL); |
e133b289 PF |
1346 | |
1347 | HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i) { | |
1348 | if (l != link) | |
1349 | continue; | |
1350 | ||
9f386c6d | 1351 | (void) manager_dhcp6_prefix_remove(m, addr); |
e133b289 PF |
1352 | } |
1353 | ||
1354 | return 0; | |
1355 | } | |
1356 | ||
1357 | static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) { | |
1358 | const struct in6_addr *addr = p; | |
1359 | ||
1360 | assert(p); | |
1361 | ||
1362 | siphash24_compress(addr, sizeof(*addr), state); | |
1363 | } | |
1364 | ||
1365 | static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) { | |
1366 | const struct in6_addr *a = _a, *b = _b; | |
1367 | ||
aa18944d | 1368 | return memcmp(a, b, sizeof(*a)); |
e133b289 PF |
1369 | } |
1370 | ||
1371 | static const struct hash_ops dhcp6_prefixes_hash_ops = { | |
1372 | .hash = dhcp6_prefixes_hash_func, | |
1373 | .compare = dhcp6_prefixes_compare_func, | |
1374 | }; | |
1375 | ||
b76d99d9 | 1376 | int manager_new(Manager **ret, sd_event *event) { |
8e766630 | 1377 | _cleanup_(manager_freep) Manager *m = NULL; |
45af44d4 | 1378 | int r; |
f579559b | 1379 | |
5fae368b TG |
1380 | m = new0(Manager, 1); |
1381 | if (!m) | |
1382 | return -ENOMEM; | |
45af44d4 | 1383 | |
5fae368b TG |
1384 | m->state_file = strdup("/run/systemd/netif/state"); |
1385 | if (!m->state_file) | |
1386 | return -ENOMEM; | |
1387 | ||
b76d99d9 | 1388 | m->event = sd_event_ref(event); |
5fae368b | 1389 | |
84de38c5 TG |
1390 | r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m); |
1391 | if (r < 0) | |
1392 | return r; | |
1393 | ||
5fae368b | 1394 | r = manager_connect_rtnl(m); |
45af44d4 TG |
1395 | if (r < 0) |
1396 | return r; | |
1397 | ||
05d0c2e3 JT |
1398 | r = manager_connect_genl(m); |
1399 | if (r < 0) | |
1400 | return r; | |
1401 | ||
5fae368b TG |
1402 | r = manager_connect_udev(m); |
1403 | if (r < 0) | |
1404 | return r; | |
45af44d4 | 1405 | |
5fae368b TG |
1406 | m->netdevs = hashmap_new(&string_hash_ops); |
1407 | if (!m->netdevs) | |
1408 | return -ENOMEM; | |
f579559b | 1409 | |
5fae368b | 1410 | LIST_HEAD_INIT(m->networks); |
f579559b | 1411 | |
05d0c2e3 JT |
1412 | r = sd_resolve_default(&m->resolve); |
1413 | if (r < 0) | |
1414 | return r; | |
1415 | ||
1416 | r = sd_resolve_attach_event(m->resolve, m->event, 0); | |
1417 | if (r < 0) | |
1418 | return r; | |
1419 | ||
5fae368b TG |
1420 | r = setup_default_address_pool(m); |
1421 | if (r < 0) | |
1422 | return r; | |
f579559b | 1423 | |
e133b289 PF |
1424 | m->dhcp6_prefixes = hashmap_new(&dhcp6_prefixes_hash_ops); |
1425 | if (!m->dhcp6_prefixes) | |
1426 | return -ENOMEM; | |
1427 | ||
8341a5c3 | 1428 | m->duid.type = DUID_TYPE_EN; |
413708d1 | 1429 | |
458d8ae3 | 1430 | (void) routing_policy_load_rules(m->state_file, &m->rules_saved); |
bce67bbe | 1431 | |
1cc6c93a | 1432 | *ret = TAKE_PTR(m); |
f579559b | 1433 | |
f579559b TG |
1434 | return 0; |
1435 | } | |
1436 | ||
5fae368b TG |
1437 | void manager_free(Manager *m) { |
1438 | Network *network; | |
1439 | NetDev *netdev; | |
1440 | Link *link; | |
1441 | AddressPool *pool; | |
f579559b | 1442 | |
5fae368b TG |
1443 | if (!m) |
1444 | return; | |
505f8da7 | 1445 | |
5fae368b | 1446 | free(m->state_file); |
505f8da7 | 1447 | |
bce67bbe SS |
1448 | while ((network = m->networks)) |
1449 | network_free(network); | |
1450 | ||
e133b289 PF |
1451 | while ((link = hashmap_first(m->dhcp6_prefixes))) |
1452 | link_unref(link); | |
1453 | hashmap_free(m->dhcp6_prefixes); | |
1454 | ||
5fae368b TG |
1455 | while ((link = hashmap_first(m->links))) |
1456 | link_unref(link); | |
1457 | hashmap_free(m->links); | |
f579559b | 1458 | |
dbffab87 TG |
1459 | hashmap_free(m->networks_by_name); |
1460 | ||
5fae368b TG |
1461 | while ((netdev = hashmap_first(m->netdevs))) |
1462 | netdev_unref(netdev); | |
1463 | hashmap_free(m->netdevs); | |
1464 | ||
1465 | while ((pool = m->address_pools)) | |
1466 | address_pool_free(pool); | |
1467 | ||
bce67bbe SS |
1468 | set_free(m->rules); |
1469 | set_free(m->rules_foreign); | |
1470 | ||
b921fcb2 | 1471 | set_free_with_destructor(m->rules_saved, routing_policy_rule_free); |
bce67bbe | 1472 | |
1c4baffc | 1473 | sd_netlink_unref(m->rtnl); |
2f5b4a77 | 1474 | sd_event_unref(m->event); |
5fae368b | 1475 | |
05d0c2e3 JT |
1476 | sd_resolve_unref(m->resolve); |
1477 | ||
7d20d375 TG |
1478 | sd_event_source_unref(m->udev_event_source); |
1479 | udev_monitor_unref(m->udev_monitor); | |
1480 | udev_unref(m->udev); | |
1481 | ||
1482 | sd_bus_unref(m->bus); | |
1483 | sd_bus_slot_unref(m->prepare_for_sleep_slot); | |
d7afd945 | 1484 | sd_bus_slot_unref(m->connected_slot); |
7d20d375 | 1485 | |
7901cea1 MP |
1486 | free(m->dynamic_timezone); |
1487 | free(m->dynamic_hostname); | |
1488 | ||
5fae368b TG |
1489 | free(m); |
1490 | } | |
1491 | ||
b76d99d9 | 1492 | int manager_start(Manager *m) { |
84de38c5 TG |
1493 | Link *link; |
1494 | Iterator i; | |
1495 | ||
a97dcc12 TG |
1496 | assert(m); |
1497 | ||
84de38c5 TG |
1498 | /* The dirty handler will deal with future serialization, but the first one |
1499 | must be done explicitly. */ | |
1500 | ||
1501 | manager_save(m); | |
1502 | ||
1503 | HASHMAP_FOREACH(link, m->links, i) | |
1504 | link_save(link); | |
1505 | ||
b76d99d9 | 1506 | return 0; |
a97dcc12 TG |
1507 | } |
1508 | ||
5fae368b TG |
1509 | int manager_load_config(Manager *m) { |
1510 | int r; | |
1511 | ||
1512 | /* update timestamp */ | |
1513 | paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); | |
1514 | ||
1515 | r = netdev_load(m); | |
f579559b TG |
1516 | if (r < 0) |
1517 | return r; | |
1518 | ||
5fae368b | 1519 | r = network_load(m); |
9021bb9f TG |
1520 | if (r < 0) |
1521 | return r; | |
1522 | ||
f579559b TG |
1523 | return 0; |
1524 | } | |
f882c247 | 1525 | |
5fae368b TG |
1526 | bool manager_should_reload(Manager *m) { |
1527 | return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); | |
1528 | } | |
1529 | ||
1530 | int manager_rtnl_enumerate_links(Manager *m) { | |
4afd3348 | 1531 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1532 | sd_netlink_message *link; |
f882c247 TG |
1533 | int r; |
1534 | ||
5da8149f | 1535 | assert(m); |
5fae368b | 1536 | assert(m->rtnl); |
5da8149f | 1537 | |
5fae368b | 1538 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
f882c247 TG |
1539 | if (r < 0) |
1540 | return r; | |
1541 | ||
1c4baffc | 1542 | r = sd_netlink_message_request_dump(req, true); |
dd3efc09 TG |
1543 | if (r < 0) |
1544 | return r; | |
1545 | ||
1c4baffc | 1546 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
f2236469 TG |
1547 | if (r < 0) |
1548 | return r; | |
1549 | ||
1c4baffc | 1550 | for (link = reply; link; link = sd_netlink_message_next(link)) { |
5fae368b | 1551 | int k; |
2e9f08ea | 1552 | |
6a24f148 TG |
1553 | m->enumerating = true; |
1554 | ||
5fae368b TG |
1555 | k = manager_rtnl_process_link(m->rtnl, link, m); |
1556 | if (k < 0) | |
1557 | r = k; | |
6a24f148 TG |
1558 | |
1559 | m->enumerating = false; | |
5fae368b | 1560 | } |
2e9f08ea | 1561 | |
5fae368b | 1562 | return r; |
f882c247 | 1563 | } |
3bef724f | 1564 | |
5fae368b | 1565 | int manager_rtnl_enumerate_addresses(Manager *m) { |
4afd3348 | 1566 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 1567 | sd_netlink_message *addr; |
1346b1f0 TG |
1568 | int r; |
1569 | ||
5fae368b TG |
1570 | assert(m); |
1571 | assert(m->rtnl); | |
bcbca829 | 1572 | |
5fae368b TG |
1573 | r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); |
1574 | if (r < 0) | |
1575 | return r; | |
bcbca829 | 1576 | |
1c4baffc | 1577 | r = sd_netlink_message_request_dump(req, true); |
1346b1f0 TG |
1578 | if (r < 0) |
1579 | return r; | |
1580 | ||
1c4baffc | 1581 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
5fae368b TG |
1582 | if (r < 0) |
1583 | return r; | |
1584 | ||
1c4baffc | 1585 | for (addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
5fae368b TG |
1586 | int k; |
1587 | ||
6a24f148 TG |
1588 | m->enumerating = true; |
1589 | ||
200a0868 | 1590 | k = manager_rtnl_process_address(m->rtnl, addr, m); |
5fae368b TG |
1591 | if (k < 0) |
1592 | r = k; | |
6a24f148 TG |
1593 | |
1594 | m->enumerating = false; | |
5fae368b TG |
1595 | } |
1596 | ||
1597 | return r; | |
1346b1f0 TG |
1598 | } |
1599 | ||
1c8e710c | 1600 | int manager_rtnl_enumerate_routes(Manager *m) { |
4afd3348 | 1601 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c8e710c TG |
1602 | sd_netlink_message *route; |
1603 | int r; | |
1604 | ||
1605 | assert(m); | |
1606 | assert(m->rtnl); | |
1607 | ||
1608 | r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0); | |
1609 | if (r < 0) | |
1610 | return r; | |
1611 | ||
1612 | r = sd_netlink_message_request_dump(req, true); | |
1613 | if (r < 0) | |
1614 | return r; | |
1615 | ||
1616 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
1617 | if (r < 0) | |
1618 | return r; | |
1619 | ||
1620 | for (route = reply; route; route = sd_netlink_message_next(route)) { | |
1621 | int k; | |
1622 | ||
1623 | m->enumerating = true; | |
1624 | ||
1625 | k = manager_rtnl_process_route(m->rtnl, route, m); | |
1626 | if (k < 0) | |
1627 | r = k; | |
1628 | ||
1629 | m->enumerating = false; | |
1630 | } | |
1631 | ||
1632 | return r; | |
1633 | } | |
1634 | ||
bce67bbe SS |
1635 | int manager_rtnl_enumerate_rules(Manager *m) { |
1636 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
1637 | sd_netlink_message *rule; | |
1638 | int r; | |
1639 | ||
1640 | assert(m); | |
1641 | assert(m->rtnl); | |
1642 | ||
1643 | r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0); | |
1644 | if (r < 0) | |
1645 | return r; | |
1646 | ||
1647 | r = sd_netlink_message_request_dump(req, true); | |
1648 | if (r < 0) | |
1649 | return r; | |
1650 | ||
1651 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
6acbbdd4 SS |
1652 | if (r < 0) { |
1653 | if (r == -EOPNOTSUPP) { | |
1654 | log_debug("FIB Rules are not supported by the kernel. Ignoring."); | |
1655 | return 0; | |
1656 | } | |
1657 | ||
bce67bbe | 1658 | return r; |
6acbbdd4 | 1659 | } |
bce67bbe SS |
1660 | |
1661 | for (rule = reply; rule; rule = sd_netlink_message_next(rule)) { | |
1662 | int k; | |
1663 | ||
1664 | m->enumerating = true; | |
1665 | ||
1666 | k = manager_rtnl_process_rule(m->rtnl, rule, m); | |
1667 | if (k < 0) | |
1668 | r = k; | |
1669 | ||
1670 | m->enumerating = false; | |
1671 | } | |
1672 | ||
1673 | return r; | |
1674 | } | |
1675 | ||
0dd25fb9 | 1676 | int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) { |
11bf3cce LP |
1677 | AddressPool *p; |
1678 | int r; | |
1679 | ||
1680 | assert(m); | |
1681 | assert(prefixlen > 0); | |
1682 | assert(found); | |
1683 | ||
1684 | LIST_FOREACH(address_pools, p, m->address_pools) { | |
1685 | if (p->family != family) | |
1686 | continue; | |
1687 | ||
1688 | r = address_pool_acquire(p, prefixlen, found); | |
1689 | if (r != 0) | |
1690 | return r; | |
1691 | } | |
1692 | ||
1693 | return 0; | |
1694 | } | |
4f5f911e LP |
1695 | |
1696 | Link* manager_find_uplink(Manager *m, Link *exclude) { | |
1697 | _cleanup_free_ struct local_address *gateways = NULL; | |
1698 | int n, i; | |
1699 | ||
1700 | assert(m); | |
1701 | ||
1702 | /* Looks for a suitable "uplink", via black magic: an | |
1703 | * interface that is up and where the default route with the | |
1704 | * highest priority points to. */ | |
1705 | ||
1706 | n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways); | |
1707 | if (n < 0) { | |
1708 | log_warning_errno(n, "Failed to determine list of default gateways: %m"); | |
1709 | return NULL; | |
1710 | } | |
1711 | ||
1712 | for (i = 0; i < n; i++) { | |
1713 | Link *link; | |
1714 | ||
1715 | link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex)); | |
1716 | if (!link) { | |
c2c940bd | 1717 | log_debug("Weird, found a gateway for a link we don't know. Ignoring."); |
4f5f911e LP |
1718 | continue; |
1719 | } | |
1720 | ||
1721 | if (link == exclude) | |
1722 | continue; | |
1723 | ||
1724 | if (link->operstate < LINK_OPERSTATE_ROUTABLE) | |
1725 | continue; | |
1726 | ||
1727 | return link; | |
1728 | } | |
1729 | ||
1730 | return NULL; | |
1731 | } | |
84de38c5 TG |
1732 | |
1733 | void manager_dirty(Manager *manager) { | |
1734 | assert(manager); | |
1735 | ||
1736 | /* the serialized state in /run is no longer up-to-date */ | |
1737 | manager->dirty = true; | |
1738 | } | |
59eb33e0 MP |
1739 | |
1740 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1741 | Manager *manager = userdata; | |
1742 | const sd_bus_error *e; | |
1743 | ||
1744 | assert(m); | |
1745 | assert(manager); | |
1746 | ||
1747 | e = sd_bus_message_get_error(m); | |
1748 | if (e) | |
1749 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); | |
1750 | ||
1751 | return 1; | |
1752 | } | |
1753 | ||
1754 | int manager_set_hostname(Manager *m, const char *hostname) { | |
1755 | int r; | |
1756 | ||
1757 | log_debug("Setting transient hostname: '%s'", strna(hostname)); | |
d7afd945 | 1758 | |
7901cea1 MP |
1759 | if (free_and_strdup(&m->dynamic_hostname, hostname) < 0) |
1760 | return log_oom(); | |
59eb33e0 | 1761 | |
d7afd945 LP |
1762 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
1763 | log_info("Not connected to system bus, not setting hostname."); | |
59eb33e0 MP |
1764 | return 0; |
1765 | } | |
1766 | ||
1767 | r = sd_bus_call_method_async( | |
1768 | m->bus, | |
1769 | NULL, | |
1770 | "org.freedesktop.hostname1", | |
1771 | "/org/freedesktop/hostname1", | |
1772 | "org.freedesktop.hostname1", | |
1773 | "SetHostname", | |
1774 | set_hostname_handler, | |
1775 | m, | |
1776 | "sb", | |
1777 | hostname, | |
1778 | false); | |
1779 | ||
1780 | if (r < 0) | |
1781 | return log_error_errno(r, "Could not set transient hostname: %m"); | |
1782 | ||
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
1787 | Manager *manager = userdata; | |
1788 | const sd_bus_error *e; | |
1789 | ||
1790 | assert(m); | |
1791 | assert(manager); | |
1792 | ||
1793 | e = sd_bus_message_get_error(m); | |
1794 | if (e) | |
1795 | log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); | |
1796 | ||
1797 | return 1; | |
1798 | } | |
1799 | ||
1800 | int manager_set_timezone(Manager *m, const char *tz) { | |
1801 | int r; | |
1802 | ||
1803 | assert(m); | |
1804 | assert(tz); | |
1805 | ||
1806 | log_debug("Setting system timezone: '%s'", tz); | |
7901cea1 MP |
1807 | if (free_and_strdup(&m->dynamic_timezone, tz) < 0) |
1808 | return log_oom(); | |
59eb33e0 | 1809 | |
d7afd945 | 1810 | if (!m->bus || sd_bus_is_ready(m->bus) <= 0) { |
3d282fff | 1811 | log_info("Not connected to system bus, not setting timezone."); |
59eb33e0 MP |
1812 | return 0; |
1813 | } | |
1814 | ||
1815 | r = sd_bus_call_method_async( | |
1816 | m->bus, | |
1817 | NULL, | |
1818 | "org.freedesktop.timedate1", | |
1819 | "/org/freedesktop/timedate1", | |
1820 | "org.freedesktop.timedate1", | |
1821 | "SetTimezone", | |
1822 | set_timezone_handler, | |
1823 | m, | |
1824 | "sb", | |
1825 | tz, | |
1826 | false); | |
1827 | if (r < 0) | |
1828 | return log_error_errno(r, "Could not set timezone: %m"); | |
1829 | ||
1830 | return 0; | |
1831 | } |