1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <resolv.h>
#include <linux/if.h>
#include "path-util.h"
#include "networkd.h"
#include "libudev-private.h"
#include "udev-util.h"
#include "rtnl-util.h"
#include "mkdir.h"
#include "virt.h"
#include "sd-rtnl.h"
const char* const network_dirs[] = {
"/etc/systemd/network",
"/run/systemd/network",
"/usr/lib/systemd/network",
#ifdef HAVE_SPLIT_USR
"/lib/systemd/network",
#endif
NULL};
static int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
Manager *m = userdata;
assert(m);
log_received_signal(LOG_INFO, si);
sd_event_exit(m->event, 0);
return 0;
}
static int setup_signals(Manager *m) {
sigset_t mask;
int r;
assert(m);
assert_se(sigemptyset(&mask) == 0);
sigset_add_many(&mask, SIGINT, SIGTERM, -1);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
r = sd_event_add_signal(m->event, &m->sigterm_event_source, SIGTERM, dispatch_sigterm, m);
if (r < 0)
return r;
r = sd_event_add_signal(m->event, &m->sigint_event_source, SIGINT, dispatch_sigterm, m);
if (r < 0)
return r;
return 0;
}
int manager_new(Manager **ret) {
_cleanup_manager_free_ Manager *m = NULL;
int r;
m = new0(Manager, 1);
if (!m)
return -ENOMEM;
m->state_file = strdup("/run/systemd/network/state");
if (!m->state_file)
return -ENOMEM;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_set_watchdog(m->event, true);
r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
if (r < 0)
return r;
r = sd_bus_default_system(&m->bus);
if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */
return r;
r = setup_signals(m);
if (r < 0)
return r;
/* udev does not initialize devices inside containers,
* so we rely on them being already initialized before
* entering the container */
if (detect_container(NULL) <= 0) {
m->udev = udev_new();
if (!m->udev)
return -ENOMEM;
m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
if (!m->udev_monitor)
return -ENOMEM;
}
m->links = hashmap_new(uint64_hash_func, uint64_compare_func);
if (!m->links)
return -ENOMEM;
m->netdevs = hashmap_new(string_hash_func, string_compare_func);
if (!m->netdevs)
return -ENOMEM;
LIST_HEAD_INIT(m->networks);
*ret = m;
m = NULL;
return 0;
}
void manager_free(Manager *m) {
Network *network;
NetDev *netdev;
Link *link;
if (!m)
return;
free(m->state_file);
udev_monitor_unref(m->udev_monitor);
udev_unref(m->udev);
sd_bus_unref(m->bus);
sd_event_source_unref(m->udev_event_source);
sd_event_source_unref(m->sigterm_event_source);
sd_event_source_unref(m->sigint_event_source);
sd_event_unref(m->event);
while ((link = hashmap_first(m->links)))
link_unref(link);
hashmap_free(m->links);
while ((network = m->networks))
network_free(network);
while ((netdev = hashmap_first(m->netdevs)))
netdev_unref(netdev);
hashmap_free(m->netdevs);
sd_rtnl_unref(m->rtnl);
free(m);
}
int manager_load_config(Manager *m) {
int r;
/* update timestamp */
paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
r = netdev_load(m);
if (r < 0)
return r;
r = network_load(m);
if (r < 0)
return r;
return 0;
}
bool manager_should_reload(Manager *m) {
return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
}
static int manager_udev_process_link(Manager *m, struct udev_device *device) {
Link *link = NULL;
int r, ifindex;
assert(m);
assert(device);
if (!streq_ptr(udev_device_get_action(device), "add"))
return 0;
ifindex = udev_device_get_ifindex(device);
if (ifindex <= 0) {
log_debug("ignoring udev ADD event for device with invalid ifindex");
return 0;
}
r = link_get(m, ifindex, &link);
if (r == -ENODEV)
return 0;
else if (r < 0)
return r;
r = link_initialized(link, device);
if (r < 0)
return r;
return 0;
}
static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
Manager *m = userdata;
Link *link = NULL;
NetDev *netdev = NULL;
uint16_t type;
char *name;
int r, ifindex;
assert(rtnl);
assert(message);
assert(m);
r = sd_rtnl_message_get_type(message, &type);
if (r < 0) {
log_warning("rtnl: could not get message type");
return 0;
}
r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
if (r < 0 || ifindex <= 0) {
log_warning("rtnl: received link message without valid ifindex");
return 0;
} else
link_get(m, ifindex, &link);
r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
if (r < 0 || !name) {
log_warning("rtnl: received link message without valid ifname");
return 0;
} else
netdev_get(m, name, &netdev);
switch (type) {
case RTM_NEWLINK:
if (!link) {
/* link is new, so add it */
r = link_add(m, message, &link);
if (r < 0) {
log_debug("could not add new link");
return 0;
}
}
if (netdev) {
/* netdev exists, so make sure the ifindex matches */
r = netdev_set_ifindex(netdev, message);
if (r < 0) {
log_debug("could not set ifindex on netdev");
return 0;
}
}
r = link_update(link, message);
if (r < 0)
return 0;
break;
case RTM_DELLINK:
link_drop(link);
netdev_drop(netdev);
break;
default:
assert_not_reached("Received invalid RTNL message type.");
}
return 1;
}
int manager_rtnl_enumerate_links(Manager *m) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
sd_rtnl_message *link;
int r, k;
assert(m);
assert(m->rtnl);
r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
if (r < 0)
return r;
r = sd_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
r = sd_rtnl_call(m->rtnl, req, 0, &reply);
if (r < 0)
return r;
for (link = reply; link; link = sd_rtnl_message_next(link)) {
uint16_t type;
k = sd_rtnl_message_get_type(link, &type);
if (k < 0)
return k;
if (type != RTM_NEWLINK)
continue;
k = manager_rtnl_process_link(m->rtnl, link, m);
if (k < 0)
r = k;
}
return r;
}
static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
Manager *m = userdata;
struct udev_monitor *monitor = m->udev_monitor;
_cleanup_udev_device_unref_ struct udev_device *device = NULL;
device = udev_monitor_receive_device(monitor);
if (!device)
return -ENOMEM;
manager_udev_process_link(m, device);
return 0;
}
int manager_udev_listen(Manager *m) {
int r;
if (detect_container(NULL) > 0)
return 0;
assert(m->udev_monitor);
r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
if (r < 0) {
log_error("Could not add udev monitor filter: %s", strerror(-r));
return r;
}
r = udev_monitor_enable_receiving(m->udev_monitor);
if (r < 0) {
log_error("Could not enable udev monitor");
return r;
}
r = sd_event_add_io(m->event,
&m->udev_event_source,
udev_monitor_get_fd(m->udev_monitor),
EPOLLIN, manager_dispatch_link_udev,
m);
if (r < 0)
return r;
return 0;
}
int manager_rtnl_listen(Manager *m) {
int r;
r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
if (r < 0)
return r;
r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
if (r < 0)
return r;
r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
if (r < 0)
return r;
return 0;
}
int manager_bus_listen(Manager *m) {
int r;
assert(m->event);
if (!m->bus) /* TODO: drop when we can rely on kdbus */
return 0;
r = sd_bus_attach_event(m->bus, m->event, 0);
if (r < 0)
return r;
return 0;
}
static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
char buf[INET6_ADDRSTRLEN];
const char *address;
address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
if (!address) {
log_warning("Invalid DNS address. Ignoring.");
return;
}
if (*count == MAXNS)
fputs("# Too many DNS servers configured, the following entries "
"will be ignored\n", f);
fprintf(f, "nameserver %s\n", address);
(*count) ++;
}
int manager_update_resolv_conf(Manager *m) {
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
Link *link;
Iterator i;
unsigned count = 0;
const char *domainname = NULL;
int r;
assert(m);
r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path);
if (r < 0)
return r;
fchmod(fileno(f), 0644);
fputs("# This file is managed by systemd-networkd(8). Do not edit.\n#\n"
"# Third party programs must not access this file directly, but\n"
"# only through the symlink at /etc/resolv.conf. To manage\n"
"# resolv.conf(5) in a different way, replace the symlink by a\n"
"# static file or a different symlink.\n\n", f);
HASHMAP_FOREACH(link, m->links, i) {
if (link->dhcp_lease) {
struct in_addr *nameservers;
size_t nameservers_size;
if (link->network->dhcp_dns) {
r = sd_dhcp_lease_get_dns(link->dhcp_lease, &nameservers, &nameservers_size);
if (r >= 0) {
unsigned j;
for (j = 0; j < nameservers_size; j++)
append_dns(f, &nameservers[j], AF_INET, &count);
}
}
if (link->network->dhcp_domainname && !domainname) {
r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
if (r >= 0)
fprintf(f, "domain %s\n", domainname);
}
}
}
HASHMAP_FOREACH(link, m->links, i) {
if (link->network && link->network->dns) {
Address *address;
Iterator j;
SET_FOREACH(address, link->network->dns, j) {
append_dns(f, &address->in_addr.in,
address->family, &count);
}
}
}
fflush(f);
if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) {
r = -errno;
unlink("/run/systemd/network/resolv.conf");
unlink(temp_path);
return r;
}
return 0;
}
int manager_save(Manager *m) {
Link *link;
Iterator i;
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *oper_state = "unknown";
bool dormant, carrier;
int r;
assert(m);
assert(m->state_file);
HASHMAP_FOREACH(link, m->links, i) {
if (link->flags & IFF_LOOPBACK)
continue;
if (link_has_carrier(link->flags, link->operstate))
carrier = true;
else if (link->operstate == IF_OPER_DORMANT)
dormant = true;
}
if (carrier)
oper_state = "carrier";
else if (dormant)
oper_state = "dormant";
r = fopen_temporary(m->state_file, &f, &temp_path);
if (r < 0)
goto finish;
fchmod(fileno(f), 0644);
fprintf(f,
"# This is private data. Do not parse.\n"
"OPER_STATE=%s\n", oper_state);
fflush(f);
if (ferror(f) || rename(temp_path, m->state_file) < 0) {
r = -errno;
unlink(m->state_file);
unlink(temp_path);
}
finish:
if (r < 0)
log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
return r;
}
|