]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/wait-online/manager.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / network / wait-online / manager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/ether.h>
4 #include <linux/if.h>
5 #include <fnmatch.h>
6
7 #include "alloc-util.h"
8 #include "link.h"
9 #include "manager.h"
10 #include "netlink-util.h"
11 #include "network-internal.h"
12 #include "strv.h"
13 #include "time-util.h"
14 #include "util.h"
15
16 bool manager_ignore_link(Manager *m, Link *link) {
17 assert(m);
18 assert(link);
19
20 /* always ignore the loopback interface */
21 if (link->flags & IFF_LOOPBACK)
22 return true;
23
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
28 if (!link->required_for_online)
29 return true;
30
31 /* ignore interfaces we explicitly are asked to ignore */
32 return strv_fnmatch(m->ignore, link->ifname, 0);
33 }
34
35 bool manager_all_configured(Manager *m) {
36 Iterator i;
37 Link *l;
38 char **ifname;
39 bool one_ready = false;
40
41 /* wait for all the links given on the command line to appear */
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) {
53 if (manager_ignore_link(m, l)) {
54 log_info("ignoring: %s", l->ifname);
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
64 if (STR_IN_SET(l->state, "configuring", "pending")) {
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
80 static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
81 Manager *m = userdata;
82 uint16_t type;
83 Link *l;
84 const char *ifname;
85 int ifindex, r;
86
87 assert(rtnl);
88 assert(m);
89 assert(mm);
90
91 r = sd_netlink_message_get_type(mm, &type);
92 if (r < 0) {
93 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
94 return 0;
95 }
96
97 r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
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 }
105
106 r = sd_netlink_message_read_string(mm, IFLA_IFNAME, &ifname);
107 if (r < 0) {
108 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
109 return 0;
110 }
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)
122 return log_error_errno(r, "Failed to create link object: %m");
123
124 r = link_update_monitor(l);
125 if (r < 0)
126 return log_error_errno(r, "Failed to initialize link object: %m");
127 }
128
129 r = link_update_rtnl(l, mm);
130 if (r < 0)
131 return log_warning_errno(r, "Failed to process RTNL link message: %m");;
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;
145 }
146
147 static int on_rtnl_event(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
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) {
162 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
163 sd_netlink_message *i;
164 int r;
165
166 assert(m);
167
168 /* First, subscribe to interfaces coming and going */
169 r = sd_netlink_open(&m->rtnl);
170 if (r < 0)
171 return r;
172
173 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
174 if (r < 0)
175 return r;
176
177 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, on_rtnl_event, NULL, m, "wait-online-on-NEWLINK");
178 if (r < 0)
179 return r;
180
181 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, on_rtnl_event, NULL, m, "wait-online-on-DELLINK");
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
190 r = sd_netlink_message_request_dump(req, true);
191 if (r < 0)
192 return r;
193
194 r = sd_netlink_call(m->rtnl, req, 0, &reply);
195 if (r < 0)
196 return r;
197
198 for (i = reply; i; i = sd_netlink_message_next(i)) {
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)
220 log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex);
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
254 int manager_new(Manager **ret, char **interfaces, char **ignore, usec_t timeout) {
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;
265 m->ignore = ignore;
266
267 r = sd_event_default(&m->event);
268 if (r < 0)
269 return r;
270
271 (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
272 (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
273
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
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
294 *ret = TAKE_PTR(m);
295
296 return 0;
297 }
298
299 void manager_free(Manager *m) {
300 if (!m)
301 return;
302
303 hashmap_free_with_destructor(m->links, link_free);
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);
310 sd_netlink_unref(m->rtnl);
311
312 sd_event_unref(m->event);
313 free(m);
314
315 return;
316 }