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