]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7de12ae7 TG |
2 | |
3 | #include <netinet/ether.h> | |
4 | #include <linux/if.h> | |
5d8689d7 | 5 | #include <fnmatch.h> |
7de12ae7 | 6 | |
b5efdb8a | 7 | #include "alloc-util.h" |
c5fcf6e4 TG |
8 | #include "link.h" |
9 | #include "manager.h" | |
1c4baffc | 10 | #include "netlink-util.h" |
7de12ae7 | 11 | #include "network-internal.h" |
51517f9e | 12 | #include "strv.h" |
e56cdb7a | 13 | #include "time-util.h" |
b5efdb8a | 14 | #include "util.h" |
7de12ae7 | 15 | |
79b1f37d TG |
16 | bool manager_ignore_link(Manager *m, Link *link) { |
17 | assert(m); | |
18 | assert(link); | |
19 | ||
be0c1370 | 20 | /* always ignore the loopback interface */ |
79b1f37d TG |
21 | if (link->flags & IFF_LOOPBACK) |
22 | return true; | |
23 | ||
be0c1370 TG |
24 | /* if interfaces are given on the command line, ignore all others */ |
25 | if (m->interfaces && !strv_contains(m->interfaces, link->ifname)) | |
26 | return true; | |
27 | ||
c1a38904 MTL |
28 | if (!link->required_for_online) |
29 | return true; | |
30 | ||
be0c1370 | 31 | /* ignore interfaces we explicitly are asked to ignore */ |
03fb3334 | 32 | return strv_fnmatch(m->ignore, link->ifname, 0); |
79b1f37d TG |
33 | } |
34 | ||
7de12ae7 TG |
35 | bool manager_all_configured(Manager *m) { |
36 | Iterator i; | |
37 | Link *l; | |
38 | char **ifname; | |
39 | bool one_ready = false; | |
40 | ||
3f85ef0f | 41 | /* wait for all the links given on the command line to appear */ |
7de12ae7 TG |
42 | STRV_FOREACH(ifname, m->interfaces) { |
43 | l = hashmap_get(m->links_by_name, *ifname); | |
44 | if (!l) { | |
45 | log_debug("still waiting for %s", *ifname); | |
46 | return false; | |
47 | } | |
48 | } | |
49 | ||
50 | /* wait for all links networkd manages to be in admin state 'configured' | |
51 | and at least one link to gain a carrier */ | |
52 | HASHMAP_FOREACH(l, m->links, i) { | |
79b1f37d TG |
53 | if (manager_ignore_link(m, l)) { |
54 | log_info("ignoring: %s", l->ifname); | |
7de12ae7 TG |
55 | continue; |
56 | } | |
57 | ||
58 | if (!l->state) { | |
59 | log_debug("link %s has not yet been processed by udev", | |
60 | l->ifname); | |
61 | return false; | |
62 | } | |
63 | ||
79ac8ba9 | 64 | if (STR_IN_SET(l->state, "configuring", "pending")) { |
7de12ae7 TG |
65 | log_debug("link %s is being processed by networkd", |
66 | l->ifname); | |
67 | return false; | |
68 | } | |
69 | ||
70 | if (l->operational_state && | |
71 | STR_IN_SET(l->operational_state, "degraded", "routable")) | |
72 | /* we wait for at least one link to be ready, | |
73 | regardless of who manages it */ | |
74 | one_ready = true; | |
75 | } | |
76 | ||
77 | return one_ready; | |
78 | } | |
79 | ||
1c4baffc | 80 | static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
7de12ae7 TG |
81 | Manager *m = userdata; |
82 | uint16_t type; | |
83 | Link *l; | |
d2437732 | 84 | const char *ifname; |
7de12ae7 TG |
85 | int ifindex, r; |
86 | ||
87 | assert(rtnl); | |
88 | assert(m); | |
89 | assert(mm); | |
90 | ||
1c4baffc | 91 | r = sd_netlink_message_get_type(mm, &type); |
61a38e02 YW |
92 | if (r < 0) { |
93 | log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m"); | |
94 | return 0; | |
95 | } | |
7de12ae7 TG |
96 | |
97 | r = sd_rtnl_message_link_get_ifindex(mm, &ifindex); | |
61a38e02 YW |
98 | if (r < 0) { |
99 | log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m"); | |
100 | return 0; | |
101 | } else if (ifindex <= 0) { | |
102 | log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex); | |
103 | return 0; | |
104 | } | |
7de12ae7 | 105 | |
1c4baffc | 106 | r = sd_netlink_message_read_string(mm, IFLA_IFNAME, &ifname); |
61a38e02 YW |
107 | if (r < 0) { |
108 | log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m"); | |
109 | return 0; | |
110 | } | |
7de12ae7 TG |
111 | |
112 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
113 | ||
114 | switch (type) { | |
115 | ||
116 | case RTM_NEWLINK: | |
117 | if (!l) { | |
118 | log_debug("Found link %i", ifindex); | |
119 | ||
120 | r = link_new(m, &l, ifindex, ifname); | |
121 | if (r < 0) | |
61a38e02 | 122 | return log_error_errno(r, "Failed to create link object: %m"); |
7de12ae7 TG |
123 | |
124 | r = link_update_monitor(l); | |
125 | if (r < 0) | |
61a38e02 | 126 | return log_error_errno(r, "Failed to initialize link object: %m"); |
7de12ae7 TG |
127 | } |
128 | ||
129 | r = link_update_rtnl(l, mm); | |
130 | if (r < 0) | |
61a38e02 | 131 | return log_warning_errno(r, "Failed to process RTNL link message: %m");; |
7de12ae7 TG |
132 | |
133 | break; | |
134 | ||
135 | case RTM_DELLINK: | |
136 | if (l) { | |
137 | log_debug("Removing link %i", l->ifindex); | |
138 | link_free(l); | |
139 | } | |
140 | ||
141 | break; | |
142 | } | |
143 | ||
144 | return 0; | |
7de12ae7 TG |
145 | } |
146 | ||
1c4baffc | 147 | static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
7de12ae7 TG |
148 | Manager *m = userdata; |
149 | int r; | |
150 | ||
151 | r = manager_process_link(rtnl, mm, m); | |
152 | if (r < 0) | |
153 | return r; | |
154 | ||
155 | if (manager_all_configured(m)) | |
156 | sd_event_exit(m->event, 0); | |
157 | ||
158 | return 1; | |
159 | } | |
160 | ||
161 | static int manager_rtnl_listen(Manager *m) { | |
4afd3348 | 162 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1c4baffc | 163 | sd_netlink_message *i; |
7de12ae7 TG |
164 | int r; |
165 | ||
166 | assert(m); | |
167 | ||
6fb2f9ab | 168 | /* First, subscribe to interfaces coming and going */ |
1c4baffc | 169 | r = sd_netlink_open(&m->rtnl); |
7de12ae7 TG |
170 | if (r < 0) |
171 | return r; | |
172 | ||
1c4baffc | 173 | r = sd_netlink_attach_event(m->rtnl, m->event, 0); |
7de12ae7 TG |
174 | if (r < 0) |
175 | return r; | |
176 | ||
8190a388 | 177 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, on_rtnl_event, NULL, m, "wait-online-on-NEWLINK"); |
7de12ae7 TG |
178 | if (r < 0) |
179 | return r; | |
180 | ||
8190a388 | 181 | r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, on_rtnl_event, NULL, m, "wait-online-on-DELLINK"); |
7de12ae7 TG |
182 | if (r < 0) |
183 | return r; | |
184 | ||
185 | /* Then, enumerate all links */ | |
186 | r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); | |
187 | if (r < 0) | |
188 | return r; | |
189 | ||
1c4baffc | 190 | r = sd_netlink_message_request_dump(req, true); |
7de12ae7 TG |
191 | if (r < 0) |
192 | return r; | |
193 | ||
1c4baffc | 194 | r = sd_netlink_call(m->rtnl, req, 0, &reply); |
7de12ae7 TG |
195 | if (r < 0) |
196 | return r; | |
197 | ||
1c4baffc | 198 | for (i = reply; i; i = sd_netlink_message_next(i)) { |
7de12ae7 TG |
199 | r = manager_process_link(m->rtnl, i, m); |
200 | if (r < 0) | |
201 | return r; | |
202 | } | |
203 | ||
204 | return r; | |
205 | } | |
206 | ||
207 | static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
208 | Manager *m = userdata; | |
209 | Iterator i; | |
210 | Link *l; | |
211 | int r; | |
212 | ||
213 | assert(m); | |
214 | ||
215 | sd_network_monitor_flush(m->network_monitor); | |
216 | ||
217 | HASHMAP_FOREACH(l, m->links, i) { | |
218 | r = link_update_monitor(l); | |
219 | if (r < 0) | |
da927ba9 | 220 | log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex); |
7de12ae7 TG |
221 | } |
222 | ||
223 | if (manager_all_configured(m)) | |
224 | sd_event_exit(m->event, 0); | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
229 | static int manager_network_monitor_listen(Manager *m) { | |
230 | int r, fd, events; | |
231 | ||
232 | assert(m); | |
233 | ||
234 | r = sd_network_monitor_new(&m->network_monitor, NULL); | |
235 | if (r < 0) | |
236 | return r; | |
237 | ||
238 | fd = sd_network_monitor_get_fd(m->network_monitor); | |
239 | if (fd < 0) | |
240 | return fd; | |
241 | ||
242 | events = sd_network_monitor_get_events(m->network_monitor); | |
243 | if (events < 0) | |
244 | return events; | |
245 | ||
246 | r = sd_event_add_io(m->event, &m->network_monitor_event_source, | |
247 | fd, events, &on_network_event, m); | |
248 | if (r < 0) | |
249 | return r; | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
e56cdb7a | 254 | int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout) { |
7de12ae7 TG |
255 | _cleanup_(manager_freep) Manager *m = NULL; |
256 | int r; | |
257 | ||
258 | assert(ret); | |
259 | ||
260 | m = new0(Manager, 1); | |
261 | if (!m) | |
262 | return -ENOMEM; | |
263 | ||
264 | m->interfaces = interfaces; | |
79b1f37d | 265 | m->ignore = ignore; |
7de12ae7 TG |
266 | |
267 | r = sd_event_default(&m->event); | |
268 | if (r < 0) | |
269 | return r; | |
270 | ||
0b2c35b8 LP |
271 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); |
272 | (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
7de12ae7 | 273 | |
e56cdb7a TG |
274 | if (timeout > 0) { |
275 | usec_t usec; | |
276 | ||
277 | usec = now(clock_boottime_or_monotonic()) + timeout; | |
278 | ||
279 | r = sd_event_add_time(m->event, NULL, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT)); | |
280 | if (r < 0) | |
281 | return r; | |
282 | } | |
283 | ||
7de12ae7 TG |
284 | sd_event_set_watchdog(m->event, true); |
285 | ||
286 | r = manager_network_monitor_listen(m); | |
287 | if (r < 0) | |
288 | return r; | |
289 | ||
290 | r = manager_rtnl_listen(m); | |
291 | if (r < 0) | |
292 | return r; | |
293 | ||
1cc6c93a | 294 | *ret = TAKE_PTR(m); |
7de12ae7 TG |
295 | |
296 | return 0; | |
297 | } | |
298 | ||
299 | void manager_free(Manager *m) { | |
7de12ae7 TG |
300 | if (!m) |
301 | return; | |
302 | ||
3b00878e | 303 | hashmap_free_with_destructor(m->links, link_free); |
7de12ae7 TG |
304 | hashmap_free(m->links_by_name); |
305 | ||
306 | sd_event_source_unref(m->network_monitor_event_source); | |
307 | sd_network_monitor_unref(m->network_monitor); | |
308 | ||
309 | sd_event_source_unref(m->rtnl_event_source); | |
1c4baffc | 310 | sd_netlink_unref(m->rtnl); |
7de12ae7 TG |
311 | |
312 | sd_event_unref(m->event); | |
313 | free(m); | |
314 | ||
315 | return; | |
316 | } |