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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Zebra NS collector and notifier for Network NameSpaces
* Copyright (C) 2017 6WIND
*/
#include <zebra.h>
#include <fcntl.h>
#ifdef HAVE_NETLINK
#ifdef HAVE_NETNS
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <sched.h>
#endif
#include <dirent.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include "frrevent.h"
#include "ns.h"
#include "command.h"
#include "memory.h"
#include "lib_errors.h"
#include "zebra_router.h"
#endif /* defined(HAVE_NETLINK) */
#include "zebra_netns_notify.h"
#include "zebra_netns_id.h"
#include "zebra_errors.h"
#include "interface.h"
#ifdef HAVE_NETLINK
/* upon creation of folder under /var/run/netns,
* wait that netns context is bound to
* that folder 10 seconds
*/
#define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
#define ZEBRA_NS_POLLING_MAX_RETRIES 200
DEFINE_MTYPE_STATIC(ZEBRA, NETNS_MISC, "ZebraNetNSInfo");
static struct event *zebra_netns_notify_current;
struct zebra_netns_info {
const char *netnspath;
unsigned int retries;
};
static void zebra_ns_ready_read(struct event *t);
static void zebra_ns_notify_create_context_from_entry_name(const char *name);
static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
int stop_retry);
static void zebra_ns_notify_read(struct event *t);
static struct vrf *vrf_handler_create(struct vty *vty, const char *vrfname)
{
if (strlen(vrfname) > VRF_NAMSIZ) {
flog_warn(EC_LIB_VRF_LENGTH,
"%% VRF name %s invalid: length exceeds %d bytes",
vrfname, VRF_NAMSIZ);
return NULL;
}
return vrf_get(VRF_UNKNOWN, vrfname);
}
static void zebra_ns_notify_create_context_from_entry_name(const char *name)
{
char *netnspath = ns_netns_pathname(NULL, name);
struct vrf *vrf;
int ret;
ns_id_t ns_id, ns_id_external, ns_id_relative = NS_UNKNOWN;
struct ns *default_ns;
if (netnspath == NULL)
return;
frr_with_privs(&zserv_privs) {
ns_id = zebra_ns_id_get(netnspath, -1);
}
if (ns_id == NS_UNKNOWN)
return;
ns_id_external = ns_map_nsid_with_external(ns_id, true);
/* if VRF with NS ID already present */
vrf = vrf_lookup_by_id((vrf_id_t)ns_id_external);
if (vrf) {
zlog_debug(
"NS notify : same NSID used by VRF %s. Ignore NS %s creation",
vrf->name, netnspath);
return;
}
vrf = vrf_handler_create(NULL, name);
if (!vrf) {
flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
"NS notify : failed to create VRF %s", name);
ns_map_nsid_with_external(ns_id, false);
return;
}
default_ns = ns_get_default();
/* force kernel ns_id creation in that new vrf */
frr_with_privs(&zserv_privs) {
ns_switch_to_netns(netnspath);
ns_id_relative = zebra_ns_id_get(NULL, default_ns->fd);
ns_switchback_to_initial();
}
frr_with_privs(&zserv_privs) {
ret = zebra_vrf_netns_handler_create(NULL, vrf, netnspath,
ns_id_external, ns_id,
ns_id_relative);
}
if (ret != CMD_SUCCESS) {
flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
"NS notify : failed to create NS %s", netnspath);
ns_map_nsid_with_external(ns_id, false);
vrf_delete(vrf);
return;
}
zlog_info("NS notify : created VRF %s NS %s", name, netnspath);
}
static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
int stop_retry)
{
void *ns_path_ptr = (void *)zns_info->netnspath;
if (stop_retry) {
XFREE(MTYPE_NETNS_MISC, ns_path_ptr);
XFREE(MTYPE_NETNS_MISC, zns_info);
return 0;
}
event_add_timer_msec(zrouter.master, zebra_ns_ready_read,
(void *)zns_info, ZEBRA_NS_POLLING_INTERVAL_MSEC,
NULL);
return 0;
}
static int zebra_ns_delete(char *name)
{
struct vrf *vrf = vrf_lookup_by_name(name);
struct interface *ifp, *tmp;
struct ns *ns;
if (!vrf) {
flog_warn(EC_ZEBRA_NS_DELETION_FAILED_NO_VRF,
"NS notify : no VRF found using NS %s", name);
return 0;
}
/*
* We don't receive interface down/delete notifications from kernel
* when a netns is deleted. Therefore we have to manually replicate
* the necessary actions here.
*/
RB_FOREACH_SAFE (ifp, if_name_head, &vrf->ifaces_by_name, tmp) {
if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
continue;
if (if_is_no_ptm_operative(ifp)) {
UNSET_FLAG(ifp->flags, IFF_RUNNING);
if_down(ifp);
}
if (IS_ZEBRA_IF_BOND(ifp))
zebra_l2if_update_bond(ifp, false);
if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
zebra_l2if_update_bond_slave(ifp, IFINDEX_INTERNAL,
false);
/* Special handling for bridge or VxLAN interfaces. */
if (IS_ZEBRA_IF_BRIDGE(ifp))
zebra_l2_bridge_del(ifp);
else if (IS_ZEBRA_IF_VXLAN(ifp))
zebra_l2_vxlanif_del(ifp);
UNSET_FLAG(ifp->flags, IFF_UP);
if_delete_update(&ifp);
}
ns = (struct ns *)vrf->ns_ctxt;
/* the deletion order is the same
* as the one used when siging signal is received
*/
vrf->ns_ctxt = NULL;
vrf_delete(vrf);
if (ns)
ns_delete(ns);
zlog_info("NS notify : deleted VRF %s", name);
return 0;
}
static int zebra_ns_notify_self_identify(struct stat *netst)
{
char net_path[PATH_MAX];
int netns;
snprintf(net_path, sizeof(net_path), "/proc/self/ns/net");
netns = open(net_path, O_RDONLY);
if (netns < 0)
return -1;
if (fstat(netns, netst) < 0) {
close(netns);
return -1;
}
close(netns);
return 0;
}
static bool zebra_ns_notify_is_default_netns(const char *name)
{
struct stat default_netns_stat;
struct stat st;
char netnspath[PATH_MAX];
if (zebra_ns_notify_self_identify(&default_netns_stat))
return false;
memset(&st, 0, sizeof(st));
snprintf(netnspath, sizeof(netnspath), "%s/%s", NS_RUN_DIR, name);
/* compare with local stat */
if (stat(netnspath, &st) == 0 &&
(st.st_dev == default_netns_stat.st_dev) &&
(st.st_ino == default_netns_stat.st_ino))
return true;
return false;
}
static void zebra_ns_ready_read(struct event *t)
{
struct zebra_netns_info *zns_info = EVENT_ARG(t);
const char *netnspath;
int err, stop_retry = 0;
if (!zns_info)
return;
if (!zns_info->netnspath) {
XFREE(MTYPE_NETNS_MISC, zns_info);
return;
}
netnspath = zns_info->netnspath;
if (--zns_info->retries == 0)
stop_retry = 1;
frr_with_privs(&zserv_privs) {
err = ns_switch_to_netns(netnspath);
}
if (err < 0) {
zebra_ns_continue_read(zns_info, stop_retry);
return;
}
/* go back to default ns */
frr_with_privs(&zserv_privs) {
err = ns_switchback_to_initial();
}
if (err < 0) {
zebra_ns_continue_read(zns_info, stop_retry);
return;
}
/* check default name is not already set */
if (strmatch(VRF_DEFAULT_NAME, basename(netnspath))) {
zlog_warn("NS notify : NS %s is already default VRF.Cancel VRF Creation", basename(netnspath));
zebra_ns_continue_read(zns_info, 1);
return;
}
if (zebra_ns_notify_is_default_netns(basename(netnspath))) {
zlog_warn(
"NS notify : NS %s is default VRF. Ignore VRF creation",
basename(netnspath));
zebra_ns_continue_read(zns_info, 1);
return;
}
/* success : close fd and create zns context */
zebra_ns_notify_create_context_from_entry_name(basename(netnspath));
zebra_ns_continue_read(zns_info, 1);
}
static void zebra_ns_notify_read(struct event *t)
{
int fd_monitor = EVENT_FD(t);
struct inotify_event *event;
char buf[BUFSIZ];
ssize_t len;
char event_name[NAME_MAX + 1];
event_add_read(zrouter.master, zebra_ns_notify_read, NULL, fd_monitor,
&zebra_netns_notify_current);
len = read(fd_monitor, buf, sizeof(buf));
if (len < 0) {
flog_err_sys(EC_ZEBRA_NS_NOTIFY_READ,
"NS notify read: failed to read (%s)",
safe_strerror(errno));
return;
}
for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
event = (struct inotify_event *)((char *)event + sizeof(*event)
+ event->len)) {
char *netnspath;
struct zebra_netns_info *netnsinfo;
if (!(event->mask & (IN_CREATE | IN_DELETE)))
continue;
if (offsetof(struct inotify_event, name) + event->len
>= sizeof(buf)) {
flog_err(EC_ZEBRA_NS_NOTIFY_READ,
"NS notify read: buffer underflow");
break;
}
if (strnlen(event->name, event->len) == event->len) {
flog_err(EC_ZEBRA_NS_NOTIFY_READ,
"NS notify error: bad event name");
break;
}
/*
* Coverity Scan extra steps to satisfy `STRING_NULL` warning:
* - Make sure event name is present by checking `len != 0`
* - Event name length must be at most `NAME_MAX + 1`
* (null byte inclusive)
* - Copy event name to a stack buffer to make sure it
* includes the null byte. `event->name` includes at least
* one null byte and `event->len` accounts the null bytes,
* so the operation after `memcpy` will look like a
* truncation to satisfy Coverity Scan null byte ending.
*
* Example:
* if `event->name` is `abc\0` and `event->len` is 4,
* `memcpy` will copy the 4 bytes and then we set the
* null byte again at the position 4.
*
* For more information please read inotify(7) man page.
*/
if (event->len == 0)
continue;
if (event->len > sizeof(event_name)) {
flog_err(EC_ZEBRA_NS_NOTIFY_READ,
"NS notify error: unexpected big event name");
break;
}
memcpy(event_name, event->name, event->len);
event_name[event->len - 1] = 0;
if (event->mask & IN_DELETE) {
zebra_ns_delete(event_name);
continue;
}
netnspath = ns_netns_pathname(NULL, event_name);
if (!netnspath)
continue;
netnspath = XSTRDUP(MTYPE_NETNS_MISC, netnspath);
netnsinfo = XCALLOC(MTYPE_NETNS_MISC,
sizeof(struct zebra_netns_info));
netnsinfo->retries = ZEBRA_NS_POLLING_MAX_RETRIES;
netnsinfo->netnspath = netnspath;
event_add_timer_msec(zrouter.master, zebra_ns_ready_read,
(void *)netnsinfo, 0, NULL);
}
}
void zebra_ns_notify_parse(void)
{
struct dirent *dent;
DIR *srcdir = opendir(NS_RUN_DIR);
if (srcdir == NULL) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
"NS parsing init: failed to parse %s", NS_RUN_DIR);
return;
}
while ((dent = readdir(srcdir)) != NULL) {
struct stat st;
if (strcmp(dent->d_name, ".") == 0
|| strcmp(dent->d_name, "..") == 0)
continue;
if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
flog_err_sys(
EC_LIB_SYSTEM_CALL,
"NS parsing init: failed to parse entry %s",
dent->d_name);
continue;
}
if (S_ISDIR(st.st_mode)) {
zlog_debug("NS parsing init: %s is not a NS",
dent->d_name);
continue;
}
/* check default name is not already set */
if (strmatch(VRF_DEFAULT_NAME, basename(dent->d_name))) {
zlog_warn("NS notify : NS %s is already default VRF.Cancel VRF Creation", dent->d_name);
continue;
}
if (zebra_ns_notify_is_default_netns(dent->d_name)) {
zlog_warn(
"NS notify : NS %s is default VRF. Ignore VRF creation",
dent->d_name);
continue;
}
zebra_ns_notify_create_context_from_entry_name(dent->d_name);
}
closedir(srcdir);
}
void zebra_ns_notify_init(void)
{
int fd_monitor;
fd_monitor = inotify_init();
if (fd_monitor < 0) {
flog_err_sys(
EC_LIB_SYSTEM_CALL,
"NS notify init: failed to initialize inotify (%s)",
safe_strerror(errno));
}
if (inotify_add_watch(fd_monitor, NS_RUN_DIR,
IN_CREATE | IN_DELETE) < 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
"NS notify watch: failed to add watch (%s)",
safe_strerror(errno));
}
event_add_read(zrouter.master, zebra_ns_notify_read, NULL, fd_monitor,
&zebra_netns_notify_current);
}
void zebra_ns_notify_close(void)
{
if (zebra_netns_notify_current == NULL)
return;
int fd = 0;
if (zebra_netns_notify_current->u.fd > 0)
fd = zebra_netns_notify_current->u.fd;
if (zebra_netns_notify_current->master != NULL)
EVENT_OFF(zebra_netns_notify_current);
/* auto-removal of notify items */
if (fd > 0)
close(fd);
}
#else
void zebra_ns_notify_parse(void)
{
}
void zebra_ns_notify_init(void)
{
}
void zebra_ns_notify_close(void)
{
}
#endif /* !HAVE_NETLINK */
|