forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netns.c
435 lines (387 loc) · 9.38 KB
/
netns.c
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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2014 Red Hat, Inc. -- Jiri Benc <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*/
#include "netns.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "handler.h"
#include "if.h"
#include "list.h"
#include "master.h"
#include "match.h"
#include "netlink.h"
#include "sysfs.h"
#include "compat.h"
#define NETNS_RUN_DIR "/var/run/netns"
static long netns_get_kernel_id(const char *path)
{
char dest[PATH_MAX], *s, *endptr;
ssize_t len;
long result;
len = readlink(path, dest, sizeof(dest));
if (len < 0)
return -errno;
dest[len] = 0;
if (strncmp("net:[", dest, 5))
return -EINVAL;
s = strrchr(dest, ']');
if (!s)
return -EINVAL;
*s = '\0';
s = dest + 5;
result = strtol(s, &endptr, 10);
if (!*s || *endptr)
return -EINVAL;
return result;
}
static struct netns_entry *netns_check_duplicate(struct list *netns_list,
long int kernel_id)
{
struct netns_entry *ns;
list_for_each(ns, *netns_list)
if (ns->kernel_id == kernel_id)
return ns;
return NULL;
}
static struct netns_entry *netns_create()
{
struct netns_entry *ns;
ns = calloc(1, sizeof(struct netns_entry));
if (!ns)
return NULL;
list_init(&ns->ifaces);
list_init(&ns->warnings);
return ns;
}
static int netns_get_var_entry(struct netns_entry **result,
struct list *netns_list,
const char *name)
{
struct netns_entry *entry;
char path[PATH_MAX];
long kernel_id;
int err;
*result = entry = netns_create();
if (!entry)
return ENOMEM;
snprintf(path, sizeof(path), "%s/%s", NETNS_RUN_DIR, name);
entry->fd = open(path, O_RDONLY);
if (entry->fd < 0)
return errno;
/* to get the kernel_id, we need to switch to that ns and examine
* /proc/self */
err = netns_switch(entry);
if (err)
return err;
kernel_id = netns_get_kernel_id("/proc/self/ns/net");
if (kernel_id < 0)
return -kernel_id;
if (netns_check_duplicate(netns_list, kernel_id)) {
close(entry->fd);
free(entry);
return -1;
}
entry->kernel_id = kernel_id;
entry->name = strdup(name);
if (!entry->name)
return ENOMEM;
return netns_switch_root();
}
static void netns_proc_entry_set_name(struct netns_entry *entry,
const char *spid)
{
char path[PATH_MAX], buf[PATH_MAX];
ssize_t len;
int commfd;
snprintf(path, sizeof(path), "/proc/%s/comm", spid);
commfd = open(path, O_RDONLY);
len = -1;
if (commfd >= 0) {
len = read(commfd, path, sizeof(path));
if (len >= 0) {
path[sizeof(path) - 1] = '\0';
if (path[len - 1] == '\n')
path[len - 1] = '\0';
}
close(commfd);
}
if (len >= 0)
snprintf(buf, sizeof(buf), "PID %s (%s)", spid, path);
else
snprintf(buf, sizeof(buf), "PID %s", spid);
entry->name = strdup(buf);
if (!entry->name)
entry->name = "?";
}
static int netns_get_proc_entry(struct netns_entry **result,
struct list *netns_list,
const char *spid)
{
struct netns_entry *entry, *dup;
char path[PATH_MAX];
pid_t pid;
long kernel_id;
snprintf(path, sizeof(path), "/proc/%s/ns/net", spid);
kernel_id = netns_get_kernel_id(path);
if (kernel_id < 0) {
/* ignore entries that cannot be read */
return -1;
}
pid = atol(spid);
dup = netns_check_duplicate(netns_list, kernel_id);
if (dup) {
if (dup->pid && (dup->pid > pid)) {
dup->pid = pid;
netns_proc_entry_set_name(dup, spid);
}
return -1;
}
*result = entry = netns_create();
if (!entry)
return ENOMEM;
entry->kernel_id = kernel_id;
entry->pid = pid;
entry->fd = open(path, O_RDONLY);
if (entry->fd < 0) {
/* ignore entries that cannot be read */
free(entry);
return -1;
}
netns_proc_entry_set_name(entry, spid);
return 0;
}
static int netns_new_list(struct list *result, int supported)
{
struct netns_entry *root;
root = netns_create();
if (!root)
return ENOMEM;
if (supported) {
root->kernel_id = netns_get_kernel_id("/proc/1/ns/net");
if (root->kernel_id < 0)
return -root->kernel_id;
root->fd = open("/proc/1/ns/net", O_RDONLY);
if (root->fd < 0)
return errno;
}
list_init(result);
list_append(result, node(root));
return 0;
}
static int netns_add_var_list(struct list *netns_list)
{
struct netns_entry *entry;
struct dirent *de;
DIR *dir;
int err;
dir = opendir(NETNS_RUN_DIR);
if (!dir)
return 0;
while ((de = readdir(dir)) != NULL) {
if (!strcmp(de->d_name, ".") ||
!strcmp(de->d_name, ".."))
continue;
err = netns_get_var_entry(&entry, netns_list, de->d_name);
if (err < 0) {
/* duplicate entry */
continue;
}
if (err)
return err;
list_append(netns_list, node(entry));
}
closedir(dir);
return 0;
}
static int netns_add_proc_list(struct list *netns_list)
{
struct netns_entry *entry;
struct dirent *de;
DIR *dir;
int err;
dir = opendir("/proc");
if (!dir)
return 0;
while ((de = readdir(dir)) != NULL) {
if (!strcmp(de->d_name, ".") ||
!strcmp(de->d_name, ".."))
continue;
if (de->d_name[0] < '0' || de->d_name[0] > '9')
continue;
err = netns_get_proc_entry(&entry, netns_list, de->d_name);
if (err < 0) {
/* duplicate entry */
continue;
}
if (err)
return err;
list_append(netns_list, node(entry));
}
closedir(dir);
return 0;
}
/* Returns -1 if netnsids are not supported. */
static int netns_get_id(struct nl_handle *hnd, struct netns_entry *entry)
{
struct nlmsg *req, *resp;
int res = -1;
req = rtnlmsg_new(RTM_GETNSID, AF_UNSPEC, 0, sizeof(struct rtgenmsg));
if (!req)
return -1;
if (nla_put_u32(req, NETNSA_FD, entry->fd))
goto out_req;
if (nl_exchange(hnd, req, &resp))
goto out_req;
if (!nlmsg_get(resp, sizeof(struct rtgenmsg)))
goto out_resp;
for_each_nla(a, resp) {
if (a->nla_type == NETNSA_NSID) {
res = nla_read_s32(a);
break;
}
}
out_resp:
nlmsg_free(resp);
out_req:
nlmsg_free(req);
return res;
}
/* This is best effort only, if anything fails (e.g. netnsids are not
* supported by kernel), we fail back to heuristics. */
static void netns_get_all_ids(struct netns_entry *current, struct list *netns_list)
{
struct nl_handle hnd;
struct netns_entry *entry;
struct netns_id *nsid;
int id;
if (netns_switch(current))
return;
if (rtnl_open(&hnd) < 0)
return;
list_init(¤t->ids);
list_for_each(entry, *netns_list) {
id = netns_get_id(&hnd, entry);
if (id < 0)
continue;
nsid = malloc(sizeof(*nsid));
if (!nsid)
break;
nsid->ns = entry;
nsid->id = id;
list_append(¤t->ids, node(nsid));
}
nl_close(&hnd);
}
int netns_fill_list(struct list *result, int supported)
{
struct netns_entry *entry;
int err;
err = netns_new_list(result, supported);
if (err)
return err;
if (supported) {
err = netns_add_var_list(result);
if (err)
return err;
err = netns_add_proc_list(result);
if (err)
return err;
}
if ((err = sysfs_init()))
return err;
list_for_each(entry, *result) {
if (entry->name) {
/* Do not try to switch to the root netns, as we're
* already there when processing the first entry,
* and netns_switch fails hard if there's no netns
* support available. */
if ((err = netns_switch(entry)))
return err;
}
if ((err = sysfs_mount(entry->name)))
return err;
if ((err = if_list(&entry->ifaces, entry)))
return err;
if ((err = netns_handler_scan(entry)))
return err;
sysfs_umount();
}
/* Walk all net name spaces again and gather all kernel assigned
* netnsids. We don't assign netnsids ourselves to prevent assigning
* them needlessly - the kernel assigns only those that are really
* needed while doing netlinks dumps. Note also that netnsids are
* per name space and this is O(n^2). */
list_for_each(entry, *result)
netns_get_all_ids(entry, result);
/* And finally, resolve netnsid+ifindex to the if_entry pointers. */
match_all_netnsid(result);
if ((err = master_resolve(result)))
return err;
if ((err = global_handler_post(result)))
return err;
if ((err = if_handler_post(result)))
return err;
return 0;
}
static int do_netns_switch(int fd)
{
if (syscall(__NR_setns, fd, CLONE_NEWNET) < 0)
return errno;
return 0;
}
int netns_switch(struct netns_entry *dest)
{
return do_netns_switch(dest->fd);
}
/* Used also to detect whether netns support is available.
* Returns 0 if everything went okay, -1 if there's no netns support,
* positive error code in case of an error. */
int netns_switch_root(void)
{
int fd, res;
fd = open("/proc/1/ns/net", O_RDONLY);
if (fd < 0) {
res = errno;
if (res == ENOENT)
res = -1;
return res;
}
res = do_netns_switch(fd);
close(fd);
if (res == ENOENT)
return -1;
return res;
}
static void netns_list_destruct(struct netns_entry *entry)
{
netns_handler_cleanup(entry);
list_free(&entry->ids, NULL);
if_list_free(&entry->ifaces);
free(entry->name);
}
void netns_list_free(struct list *netns_list)
{
list_free(netns_list, (destruct_f)netns_list_destruct);
}