1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <poll.h>
5 #include <sys/inotify.h>
6 
7 #include "sd-network.h"
8 
9 #include "alloc-util.h"
10 #include "env-file.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "inotify-util.h"
14 #include "macro.h"
15 #include "parse-util.h"
16 #include "stdio-util.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "util.h"
20 
network_get_string(const char * field,char ** ret)21 static int network_get_string(const char *field, char **ret) {
22         _cleanup_free_ char *s = NULL;
23         int r;
24 
25         assert_return(ret, -EINVAL);
26 
27         r = parse_env_file(NULL, "/run/systemd/netif/state", field, &s);
28         if (r == -ENOENT)
29                 return -ENODATA;
30         if (r < 0)
31                 return r;
32         if (isempty(s))
33                 return -ENODATA;
34 
35         *ret = TAKE_PTR(s);
36 
37         return 0;
38 }
39 
sd_network_get_operational_state(char ** state)40 int sd_network_get_operational_state(char **state) {
41         return network_get_string("OPER_STATE", state);
42 }
43 
sd_network_get_carrier_state(char ** state)44 int sd_network_get_carrier_state(char **state) {
45         return network_get_string("CARRIER_STATE", state);
46 }
47 
sd_network_get_address_state(char ** state)48 int sd_network_get_address_state(char **state) {
49         return network_get_string("ADDRESS_STATE", state);
50 }
51 
sd_network_get_ipv4_address_state(char ** state)52 int sd_network_get_ipv4_address_state(char **state) {
53         return network_get_string("IPV4_ADDRESS_STATE", state);
54 }
55 
sd_network_get_ipv6_address_state(char ** state)56 int sd_network_get_ipv6_address_state(char **state) {
57         return network_get_string("IPV6_ADDRESS_STATE", state);
58 }
59 
sd_network_get_online_state(char ** state)60 int sd_network_get_online_state(char **state) {
61         return network_get_string("ONLINE_STATE", state);
62 }
63 
network_get_strv(const char * key,char *** ret)64 static int network_get_strv(const char *key, char ***ret) {
65         _cleanup_strv_free_ char **a = NULL;
66         _cleanup_free_ char *s = NULL;
67         int r;
68 
69         assert_return(ret, -EINVAL);
70 
71         r = parse_env_file(NULL, "/run/systemd/netif/state", key, &s);
72         if (r == -ENOENT)
73                 return -ENODATA;
74         if (r < 0)
75                 return r;
76         if (isempty(s)) {
77                 *ret = NULL;
78                 return 0;
79         }
80 
81         a = strv_split(s, " ");
82         if (!a)
83                 return -ENOMEM;
84 
85         strv_uniq(a);
86         r = (int) strv_length(a);
87 
88         *ret = TAKE_PTR(a);
89 
90         return r;
91 }
92 
sd_network_get_dns(char *** ret)93 int sd_network_get_dns(char ***ret) {
94         return network_get_strv("DNS", ret);
95 }
96 
sd_network_get_ntp(char *** ret)97 int sd_network_get_ntp(char ***ret) {
98         return network_get_strv("NTP", ret);
99 }
100 
sd_network_get_search_domains(char *** ret)101 int sd_network_get_search_domains(char ***ret) {
102         return network_get_strv("DOMAINS", ret);
103 }
104 
sd_network_get_route_domains(char *** ret)105 int sd_network_get_route_domains(char ***ret) {
106         return network_get_strv("ROUTE_DOMAINS", ret);
107 }
108 
network_link_get_string(int ifindex,const char * field,char ** ret)109 static int network_link_get_string(int ifindex, const char *field, char **ret) {
110         char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex)];
111         _cleanup_free_ char *s = NULL;
112         int r;
113 
114         assert_return(ifindex > 0, -EINVAL);
115         assert_return(ret, -EINVAL);
116 
117         xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
118 
119         r = parse_env_file(NULL, path, field, &s);
120         if (r == -ENOENT)
121                 return -ENODATA;
122         if (r < 0)
123                 return r;
124         if (isempty(s))
125                 return -ENODATA;
126 
127         *ret = TAKE_PTR(s);
128 
129         return 0;
130 }
131 
network_link_get_strv(int ifindex,const char * key,char *** ret)132 static int network_link_get_strv(int ifindex, const char *key, char ***ret) {
133         char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex)];
134         _cleanup_strv_free_ char **a = NULL;
135         _cleanup_free_ char *s = NULL;
136         int r;
137 
138         assert_return(ifindex > 0, -EINVAL);
139         assert_return(ret, -EINVAL);
140 
141         xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
142         r = parse_env_file(NULL, path, key, &s);
143         if (r == -ENOENT)
144                 return -ENODATA;
145         if (r < 0)
146                 return r;
147         if (isempty(s)) {
148                 *ret = NULL;
149                 return 0;
150         }
151 
152         a = strv_split(s, " ");
153         if (!a)
154                 return -ENOMEM;
155 
156         strv_uniq(a);
157         r = (int) strv_length(a);
158 
159         *ret = TAKE_PTR(a);
160 
161         return r;
162 }
163 
sd_network_link_get_setup_state(int ifindex,char ** state)164 int sd_network_link_get_setup_state(int ifindex, char **state) {
165         return network_link_get_string(ifindex, "ADMIN_STATE", state);
166 }
167 
sd_network_link_get_network_file(int ifindex,char ** filename)168 int sd_network_link_get_network_file(int ifindex, char **filename) {
169         return network_link_get_string(ifindex, "NETWORK_FILE", filename);
170 }
171 
sd_network_link_get_operational_state(int ifindex,char ** state)172 int sd_network_link_get_operational_state(int ifindex, char **state) {
173         return network_link_get_string(ifindex, "OPER_STATE", state);
174 }
175 
sd_network_link_get_required_family_for_online(int ifindex,char ** state)176 int sd_network_link_get_required_family_for_online(int ifindex, char **state) {
177         _cleanup_free_ char *s = NULL;
178         int r;
179 
180         assert_return(state, -EINVAL);
181 
182         r = network_link_get_string(ifindex, "REQUIRED_FAMILY_FOR_ONLINE", &s);
183         if (r < 0) {
184                 if (r != -ENODATA)
185                         return r;
186 
187                 s = strdup("any");
188                 if (!s)
189                         return -ENOMEM;
190         }
191 
192         *state = TAKE_PTR(s);
193         return 0;
194 }
195 
sd_network_link_get_carrier_state(int ifindex,char ** state)196 int sd_network_link_get_carrier_state(int ifindex, char **state) {
197         return network_link_get_string(ifindex, "CARRIER_STATE", state);
198 }
199 
sd_network_link_get_address_state(int ifindex,char ** state)200 int sd_network_link_get_address_state(int ifindex, char **state) {
201         return network_link_get_string(ifindex, "ADDRESS_STATE", state);
202 }
203 
sd_network_link_get_ipv4_address_state(int ifindex,char ** state)204 int sd_network_link_get_ipv4_address_state(int ifindex, char **state) {
205         return network_link_get_string(ifindex, "IPV4_ADDRESS_STATE", state);
206 }
207 
sd_network_link_get_ipv6_address_state(int ifindex,char ** state)208 int sd_network_link_get_ipv6_address_state(int ifindex, char **state) {
209         return network_link_get_string(ifindex, "IPV6_ADDRESS_STATE", state);
210 }
211 
sd_network_link_get_online_state(int ifindex,char ** state)212 int sd_network_link_get_online_state(int ifindex, char **state) {
213         return network_link_get_string(ifindex, "ONLINE_STATE", state);
214 }
215 
sd_network_link_get_dhcp6_client_iaid_string(int ifindex,char ** iaid)216 int sd_network_link_get_dhcp6_client_iaid_string(int ifindex, char **iaid) {
217         return network_link_get_string(ifindex, "DHCP6_CLIENT_IAID", iaid);
218 }
219 
sd_network_link_get_dhcp6_client_duid_string(int ifindex,char ** duid)220 int sd_network_link_get_dhcp6_client_duid_string(int ifindex, char **duid) {
221         return network_link_get_string(ifindex, "DHCP6_CLIENT_DUID", duid);
222 }
223 
sd_network_link_get_required_for_online(int ifindex)224 int sd_network_link_get_required_for_online(int ifindex) {
225         _cleanup_free_ char *s = NULL;
226         int r;
227 
228         r = network_link_get_string(ifindex, "REQUIRED_FOR_ONLINE", &s);
229         if (r < 0) {
230                 /* Handle -ENODATA as RequiredForOnline=yes, for compatibility */
231                 if (r == -ENODATA)
232                         return true;
233                 return r;
234         }
235 
236         return parse_boolean(s);
237 }
238 
sd_network_link_get_required_operstate_for_online(int ifindex,char ** state)239 int sd_network_link_get_required_operstate_for_online(int ifindex, char **state) {
240         _cleanup_free_ char *s = NULL;
241         int r;
242 
243         assert_return(state, -EINVAL);
244 
245         r = network_link_get_string(ifindex, "REQUIRED_OPER_STATE_FOR_ONLINE", &s);
246         if (r < 0) {
247                 if (r != -ENODATA)
248                         return r;
249 
250                 /* For compatibility, assuming degraded. */
251                 s = strdup("degraded");
252                 if (!s)
253                         return -ENOMEM;
254         }
255 
256         *state = TAKE_PTR(s);
257         return 0;
258 }
259 
sd_network_link_get_activation_policy(int ifindex,char ** policy)260 int sd_network_link_get_activation_policy(int ifindex, char **policy) {
261         _cleanup_free_ char *s = NULL;
262         int r;
263 
264         assert_return(policy, -EINVAL);
265 
266         r = network_link_get_string(ifindex, "ACTIVATION_POLICY", &s);
267         if (r < 0) {
268                 if (r != -ENODATA)
269                         return r;
270 
271                 /* For compatibility, assuming up. */
272                 s = strdup("up");
273                 if (!s)
274                         return -ENOMEM;
275         }
276 
277         *policy = TAKE_PTR(s);
278         return 0;
279 }
280 
sd_network_link_get_llmnr(int ifindex,char ** llmnr)281 int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
282         return network_link_get_string(ifindex, "LLMNR", llmnr);
283 }
284 
sd_network_link_get_mdns(int ifindex,char ** mdns)285 int sd_network_link_get_mdns(int ifindex, char **mdns) {
286         return network_link_get_string(ifindex, "MDNS", mdns);
287 }
288 
sd_network_link_get_dns_over_tls(int ifindex,char ** dns_over_tls)289 int sd_network_link_get_dns_over_tls(int ifindex, char **dns_over_tls) {
290         return network_link_get_string(ifindex, "DNS_OVER_TLS", dns_over_tls);
291 }
292 
sd_network_link_get_dnssec(int ifindex,char ** dnssec)293 int sd_network_link_get_dnssec(int ifindex, char **dnssec) {
294         return network_link_get_string(ifindex, "DNSSEC", dnssec);
295 }
296 
sd_network_link_get_dnssec_negative_trust_anchors(int ifindex,char *** nta)297 int sd_network_link_get_dnssec_negative_trust_anchors(int ifindex, char ***nta) {
298         return network_link_get_strv(ifindex, "DNSSEC_NTA", nta);
299 }
300 
sd_network_link_get_dns(int ifindex,char *** ret)301 int sd_network_link_get_dns(int ifindex, char ***ret) {
302         return network_link_get_strv(ifindex, "DNS", ret);
303 }
304 
sd_network_link_get_ntp(int ifindex,char *** ret)305 int sd_network_link_get_ntp(int ifindex, char ***ret) {
306         return network_link_get_strv(ifindex, "NTP", ret);
307 }
308 
sd_network_link_get_sip(int ifindex,char *** ret)309 int sd_network_link_get_sip(int ifindex, char ***ret) {
310         return network_link_get_strv(ifindex, "SIP", ret);
311 }
312 
sd_network_link_get_search_domains(int ifindex,char *** ret)313 int sd_network_link_get_search_domains(int ifindex, char ***ret) {
314         return network_link_get_strv(ifindex, "DOMAINS", ret);
315 }
316 
sd_network_link_get_route_domains(int ifindex,char *** ret)317 int sd_network_link_get_route_domains(int ifindex, char ***ret) {
318         return network_link_get_strv(ifindex, "ROUTE_DOMAINS", ret);
319 }
320 
sd_network_link_get_dns_default_route(int ifindex)321 int sd_network_link_get_dns_default_route(int ifindex) {
322         char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex)];
323         _cleanup_free_ char *s = NULL;
324         int r;
325 
326         assert_return(ifindex > 0, -EINVAL);
327 
328         xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
329 
330         r = parse_env_file(NULL, path, "DNS_DEFAULT_ROUTE", &s);
331         if (r == -ENOENT)
332                 return -ENODATA;
333         if (r < 0)
334                 return r;
335         if (isempty(s))
336                 return -ENODATA;
337         return parse_boolean(s);
338 }
339 
network_link_get_ifindexes(int ifindex,const char * key,int ** ret)340 static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) {
341         char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex)];
342         _cleanup_free_ int *ifis = NULL;
343         _cleanup_free_ char *s = NULL;
344         size_t c = 0;
345         int r;
346 
347         assert_return(ifindex > 0, -EINVAL);
348         assert_return(ret, -EINVAL);
349 
350         xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
351         r = parse_env_file(NULL, path, key, &s);
352         if (r == -ENOENT)
353                 return -ENODATA;
354         if (r < 0)
355                 return r;
356 
357         for (const char *x = s;;) {
358                 _cleanup_free_ char *word = NULL;
359 
360                 r = extract_first_word(&x, &word, NULL, 0);
361                 if (r < 0)
362                         return r;
363                 if (r == 0)
364                         break;
365 
366                 if (!GREEDY_REALLOC(ifis, c + 2))
367                         return -ENOMEM;
368 
369                 r = ifis[c++] = parse_ifindex(word);
370                 if (r < 0)
371                         return r;
372         }
373 
374         if (ifis)
375                 ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */
376 
377         *ret = TAKE_PTR(ifis);
378 
379         return c;
380 }
381 
sd_network_link_get_carrier_bound_to(int ifindex,int ** ret)382 int sd_network_link_get_carrier_bound_to(int ifindex, int **ret) {
383         return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_TO", ret);
384 }
385 
sd_network_link_get_carrier_bound_by(int ifindex,int ** ret)386 int sd_network_link_get_carrier_bound_by(int ifindex, int **ret) {
387         return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_BY", ret);
388 }
389 
sd_network_link_get_stat(int ifindex,struct stat * ret)390 int sd_network_link_get_stat(int ifindex, struct stat *ret) {
391         char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex)];
392         struct stat st;
393 
394         assert_return(ifindex > 0, -EINVAL);
395 
396         xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
397 
398         if (stat(path, &st) < 0)
399                 return -errno;
400 
401         if (ret)
402                 *ret = st;
403 
404         return 0;
405 }
406 
MONITOR_TO_FD(sd_network_monitor * m)407 static int MONITOR_TO_FD(sd_network_monitor *m) {
408         return (int) (unsigned long) m - 1;
409 }
410 
FD_TO_MONITOR(int fd)411 static sd_network_monitor* FD_TO_MONITOR(int fd) {
412         return (sd_network_monitor*) (unsigned long) (fd + 1);
413 }
414 
monitor_add_inotify_watch(int fd)415 static int monitor_add_inotify_watch(int fd) {
416         int wd;
417 
418         wd = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
419         if (wd >= 0)
420                 return wd;
421         else if (errno != ENOENT)
422                 return -errno;
423 
424         wd = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
425         if (wd >= 0)
426                 return wd;
427         else if (errno != ENOENT)
428                 return -errno;
429 
430         wd = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
431         if (wd < 0)
432                 return -errno;
433 
434         return wd;
435 }
436 
sd_network_monitor_new(sd_network_monitor ** m,const char * category)437 int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
438         _cleanup_close_ int fd = -1;
439         int k;
440         bool good = false;
441 
442         assert_return(m, -EINVAL);
443 
444         fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
445         if (fd < 0)
446                 return -errno;
447 
448         if (!category || streq(category, "links")) {
449                 k = monitor_add_inotify_watch(fd);
450                 if (k < 0)
451                         return k;
452 
453                 good = true;
454         }
455 
456         if (!good)
457                 return -EINVAL;
458 
459         *m = FD_TO_MONITOR(TAKE_FD(fd));
460         return 0;
461 }
462 
sd_network_monitor_unref(sd_network_monitor * m)463 sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
464         if (m)
465                 (void) close_nointr(MONITOR_TO_FD(m));
466 
467         return NULL;
468 }
469 
sd_network_monitor_flush(sd_network_monitor * m)470 int sd_network_monitor_flush(sd_network_monitor *m) {
471         union inotify_event_buffer buffer;
472         ssize_t l;
473         int fd;
474 
475         assert_return(m, -EINVAL);
476 
477         fd = MONITOR_TO_FD(m);
478 
479         l = read(fd, &buffer, sizeof(buffer));
480         if (l < 0) {
481                 if (ERRNO_IS_TRANSIENT(errno))
482                         return 0;
483 
484                 return -errno;
485         }
486 
487         FOREACH_INOTIFY_EVENT(e, buffer, l) {
488                 if (e->mask & IN_ISDIR) {
489                         int wd;
490 
491                         wd = monitor_add_inotify_watch(fd);
492                         if (wd < 0)
493                                 return wd;
494 
495                         if (wd != e->wd) {
496                                 if (inotify_rm_watch(fd, e->wd) < 0)
497                                         return -errno;
498                         }
499                 }
500         }
501 
502         return 0;
503 }
504 
sd_network_monitor_get_fd(sd_network_monitor * m)505 int sd_network_monitor_get_fd(sd_network_monitor *m) {
506 
507         assert_return(m, -EINVAL);
508 
509         return MONITOR_TO_FD(m);
510 }
511 
sd_network_monitor_get_events(sd_network_monitor * m)512 int sd_network_monitor_get_events(sd_network_monitor *m) {
513 
514         assert_return(m, -EINVAL);
515 
516         /* For now we will only return POLLIN here, since we don't
517          * need anything else ever for inotify.  However, let's have
518          * this API to keep our options open should we later on need
519          * it. */
520         return POLLIN;
521 }
522 
sd_network_monitor_get_timeout(sd_network_monitor * m,uint64_t * timeout_usec)523 int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
524 
525         assert_return(m, -EINVAL);
526         assert_return(timeout_usec, -EINVAL);
527 
528         /* For now we will only return UINT64_MAX, since we don't
529          * need any timeout. However, let's have this API to keep our
530          * options open should we later on need it. */
531         *timeout_usec = UINT64_MAX;
532         return 0;
533 }
534