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