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