]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_netns_notify.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / zebra / zebra_netns_notify.c
CommitLineData
e27dec3c
PG
1/*
2 * Zebra NS collector and notifier for Network NameSpaces
3 * Copyright (C) 2017 6WIND
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <zebra.h>
21
22#ifdef HAVE_NETLINK
23#ifdef HAVE_NETNS
24#undef _GNU_SOURCE
25#define _GNU_SOURCE
26
27#include <sched.h>
28#endif
29#include <dirent.h>
30#include <sys/inotify.h>
31#include <sys/stat.h>
32
33#include "thread.h"
34#include "ns.h"
35#include "command.h"
36#include "memory.h"
174482ef 37#include "lib_errors.h"
e27dec3c 38
3801e764 39#include "zebra_router.h"
e27dec3c
PG
40#include "zebra_memory.h"
41#endif /* defined(HAVE_NETLINK) */
42
43#include "zebra_netns_notify.h"
44#include "zebra_netns_id.h"
9df414fe 45#include "zebra_errors.h"
e27dec3c
PG
46
47#ifdef HAVE_NETLINK
48
49/* upon creation of folder under /var/run/netns,
50 * wait that netns context is bound to
51 * that folder 10 seconds
52 */
53#define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
54#define ZEBRA_NS_POLLING_MAX_RETRIES 200
55
56DEFINE_MTYPE_STATIC(ZEBRA, NETNS_MISC, "ZebraNetNSInfo")
57static struct thread *zebra_netns_notify_current;
58
59struct zebra_netns_info {
60 const char *netnspath;
61 unsigned int retries;
62};
63
64static int zebra_ns_ready_read(struct thread *t);
65static void zebra_ns_notify_create_context_from_entry_name(const char *name);
66static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
67 int stop_retry);
68static int zebra_ns_notify_read(struct thread *t);
69
70static void zebra_ns_notify_create_context_from_entry_name(const char *name)
71{
72 char *netnspath = ns_netns_pathname(NULL, name);
73 struct vrf *vrf;
74 int ret;
03aff2d8 75 ns_id_t ns_id, ns_id_external;
e27dec3c
PG
76
77 if (netnspath == NULL)
78 return;
79
0cf6db21 80 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
81 ns_id = zebra_ns_id_get(netnspath);
82 }
85a0edca
PG
83 if (ns_id == NS_UNKNOWN)
84 return;
03aff2d8 85 ns_id_external = ns_map_nsid_with_external(ns_id, true);
b7b816df 86 /* if VRF with NS ID already present */
03aff2d8 87 vrf = vrf_lookup_by_id((vrf_id_t)ns_id_external);
b7b816df 88 if (vrf) {
9df414fe 89 zlog_debug(
996c9314
LB
90 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
91 vrf->name, netnspath);
b7b816df
PG
92 return;
93 }
94 if (vrf_handler_create(NULL, name, &vrf) != CMD_SUCCESS) {
e914ccbe 95 flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
9df414fe 96 "NS notify : failed to create VRF %s", name);
03aff2d8 97 ns_map_nsid_with_external(ns_id, false);
b7b816df
PG
98 return;
99 }
0cf6db21 100 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
101 ret = vrf_netns_handler_create(NULL, vrf, netnspath,
102 ns_id_external, ns_id);
103 }
e27dec3c 104 if (ret != CMD_SUCCESS) {
e914ccbe 105 flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
9df414fe 106 "NS notify : failed to create NS %s", netnspath);
03aff2d8 107 ns_map_nsid_with_external(ns_id, false);
73899a2f 108 vrf_delete(vrf);
e27dec3c
PG
109 return;
110 }
996c9314 111 zlog_info("NS notify : created VRF %s NS %s", name, netnspath);
e27dec3c
PG
112}
113
114static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
115 int stop_retry)
116{
117 void *ns_path_ptr = (void *)zns_info->netnspath;
118
119 if (stop_retry) {
120 XFREE(MTYPE_NETNS_MISC, ns_path_ptr);
121 XFREE(MTYPE_NETNS_MISC, zns_info);
122 return 0;
123 }
3801e764 124 thread_add_timer_msec(zrouter.master, zebra_ns_ready_read,
996c9314
LB
125 (void *)zns_info, ZEBRA_NS_POLLING_INTERVAL_MSEC,
126 NULL);
e27dec3c
PG
127 return 0;
128}
129
0c902ba5
PG
130static int zebra_ns_delete(char *name)
131{
132 struct vrf *vrf = vrf_lookup_by_name(name);
133 struct ns *ns;
134
135 if (!vrf) {
e914ccbe 136 flog_warn(EC_ZEBRA_NS_DELETION_FAILED_NO_VRF,
9df414fe 137 "NS notify : no VRF found using NS %s", name);
0c902ba5
PG
138 return 0;
139 }
140 /* Clear configured flag and invoke delete. */
141 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
142 ns = (struct ns *)vrf->ns_ctxt;
143 /* the deletion order is the same
144 * as the one used when siging signal is received
145 */
146 vrf_delete(vrf);
147 if (ns)
148 ns_delete(ns);
149
150 zlog_info("NS notify : deleted VRF %s", name);
151 return 0;
152}
153
3ed78e8c
PG
154static int zebra_ns_notify_self_identify(struct stat *netst)
155{
156 char net_path[64];
157 int netns;
158
159 sprintf(net_path, "/proc/self/ns/net");
160 netns = open(net_path, O_RDONLY);
161 if (netns < 0)
162 return -1;
163 if (fstat(netns, netst) < 0) {
164 close(netns);
165 return -1;
166 }
167 close(netns);
168 return 0;
169}
170
171static bool zebra_ns_notify_is_default_netns(const char *name)
172{
173 struct stat default_netns_stat;
174 struct stat st;
175 char netnspath[64];
176
177 if (zebra_ns_notify_self_identify(&default_netns_stat))
178 return false;
179
180 memset(&st, 0, sizeof(struct stat));
181 snprintf(netnspath, 64, "%s/%s", NS_RUN_DIR, name);
182 /* compare with local stat */
183 if (stat(netnspath, &st) == 0 &&
184 (st.st_dev == default_netns_stat.st_dev) &&
185 (st.st_ino == default_netns_stat.st_ino))
186 return true;
187 return false;
188}
0c902ba5 189
e27dec3c
PG
190static int zebra_ns_ready_read(struct thread *t)
191{
192 struct zebra_netns_info *zns_info = THREAD_ARG(t);
193 const char *netnspath;
194 int err, stop_retry = 0;
195
196 if (!zns_info)
197 return 0;
198 if (!zns_info->netnspath) {
199 XFREE(MTYPE_NETNS_MISC, zns_info);
200 return 0;
201 }
202 netnspath = zns_info->netnspath;
203 if (--zns_info->retries == 0)
204 stop_retry = 1;
0cf6db21 205 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
206 err = ns_switch_to_netns(netnspath);
207 }
e27dec3c
PG
208 if (err < 0)
209 return zebra_ns_continue_read(zns_info, stop_retry);
210
211 /* go back to default ns */
0cf6db21 212 frr_with_privs(&zserv_privs) {
01b9e3fd
DL
213 err = ns_switchback_to_initial();
214 }
e27dec3c
PG
215 if (err < 0)
216 return zebra_ns_continue_read(zns_info, stop_retry);
217
167b0889
PG
218 /* check default name is not already set */
219 if (strmatch(VRF_DEFAULT_NAME, basename(netnspath))) {
220 zlog_warn("NS notify : NS %s is already default VRF."
221 "Cancel VRF Creation", basename(netnspath));
222 return zebra_ns_continue_read(zns_info, 1);
223 }
3ed78e8c
PG
224 if (zebra_ns_notify_is_default_netns(basename(netnspath))) {
225 zlog_warn(
226 "NS notify : NS %s is default VRF."
227 " Updating VRF Name", basename(netnspath));
4fe52e76 228 vrf_set_default_name(basename(netnspath), false);
3ed78e8c
PG
229 return zebra_ns_continue_read(zns_info, 1);
230 }
231
e27dec3c
PG
232 /* success : close fd and create zns context */
233 zebra_ns_notify_create_context_from_entry_name(basename(netnspath));
234 return zebra_ns_continue_read(zns_info, 1);
235}
236
237static int zebra_ns_notify_read(struct thread *t)
238{
239 int fd_monitor = THREAD_FD(t);
240 struct inotify_event *event;
241 char buf[BUFSIZ];
242 ssize_t len;
243
996c9314 244 zebra_netns_notify_current = thread_add_read(
3801e764 245 zrouter.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
e27dec3c
PG
246 len = read(fd_monitor, buf, sizeof(buf));
247 if (len < 0) {
e914ccbe 248 flog_err_sys(EC_ZEBRA_NS_NOTIFY_READ,
9df414fe
QY
249 "NS notify read: failed to read (%s)",
250 safe_strerror(errno));
e27dec3c
PG
251 return 0;
252 }
996c9314
LB
253 for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
254 event = (struct inotify_event *)((char *)event + sizeof(*event)
255 + event->len)) {
e27dec3c
PG
256 char *netnspath;
257 struct zebra_netns_info *netnsinfo;
258
0c902ba5 259 if (!(event->mask & (IN_CREATE | IN_DELETE)))
e27dec3c 260 continue;
45981fda 261
262 if (offsetof(struct inotify_event, name) + event->len
263 >= sizeof(buf)) {
e914ccbe 264 flog_err(EC_ZEBRA_NS_NOTIFY_READ,
9df414fe 265 "NS notify read: buffer underflow");
32ac96b2 266 break;
267 }
b6312ad1 268
269 if (strnlen(event->name, event->len) == event->len) {
e914ccbe 270 flog_err(EC_ZEBRA_NS_NOTIFY_READ,
9df414fe 271 "NS notify error: bad event name");
b6312ad1 272 break;
273 }
274
f245bcae
PG
275 if (event->mask & IN_DELETE) {
276 zebra_ns_delete(event->name);
277 continue;
278 }
e27dec3c
PG
279 netnspath = ns_netns_pathname(NULL, event->name);
280 if (!netnspath)
281 continue;
282 netnspath = XSTRDUP(MTYPE_NETNS_MISC, netnspath);
283 netnsinfo = XCALLOC(MTYPE_NETNS_MISC,
284 sizeof(struct zebra_netns_info));
285 netnsinfo->retries = ZEBRA_NS_POLLING_MAX_RETRIES;
286 netnsinfo->netnspath = netnspath;
3801e764 287 thread_add_timer_msec(zrouter.master, zebra_ns_ready_read,
996c9314 288 (void *)netnsinfo, 0, NULL);
e27dec3c
PG
289 }
290 return 0;
291}
292
293void zebra_ns_notify_parse(void)
294{
295 struct dirent *dent;
296 DIR *srcdir = opendir(NS_RUN_DIR);
297
298 if (srcdir == NULL) {
450971aa 299 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe 300 "NS parsing init: failed to parse %s", NS_RUN_DIR);
e27dec3c
PG
301 return;
302 }
303 while ((dent = readdir(srcdir)) != NULL) {
304 struct stat st;
305
306 if (strcmp(dent->d_name, ".") == 0
996c9314 307 || strcmp(dent->d_name, "..") == 0)
e27dec3c
PG
308 continue;
309 if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
9df414fe 310 flog_err_sys(
450971aa 311 EC_LIB_SYSTEM_CALL,
9df414fe
QY
312 "NS parsing init: failed to parse entry %s",
313 dent->d_name);
e27dec3c
PG
314 continue;
315 }
316 if (S_ISDIR(st.st_mode)) {
9df414fe
QY
317 zlog_debug("NS parsing init: %s is not a NS",
318 dent->d_name);
e27dec3c
PG
319 continue;
320 }
167b0889
PG
321 /* check default name is not already set */
322 if (strmatch(VRF_DEFAULT_NAME, basename(dent->d_name))) {
323 zlog_warn("NS notify : NS %s is already default VRF."
324 "Cancel VRF Creation", dent->d_name);
325 continue;
326 }
3ed78e8c
PG
327 if (zebra_ns_notify_is_default_netns(dent->d_name)) {
328 zlog_warn(
329 "NS notify : NS %s is default VRF."
330 " Updating VRF Name", dent->d_name);
4fe52e76 331 vrf_set_default_name(dent->d_name, false);
3ed78e8c
PG
332 continue;
333 }
e27dec3c
PG
334 zebra_ns_notify_create_context_from_entry_name(dent->d_name);
335 }
336 closedir(srcdir);
337}
338
339void zebra_ns_notify_init(void)
340{
341 int fd_monitor;
342
343 zebra_netns_notify_current = NULL;
344 fd_monitor = inotify_init();
345 if (fd_monitor < 0) {
9df414fe 346 flog_err_sys(
450971aa 347 EC_LIB_SYSTEM_CALL,
9df414fe
QY
348 "NS notify init: failed to initialize inotify (%s)",
349 safe_strerror(errno));
e27dec3c 350 }
0c902ba5
PG
351 if (inotify_add_watch(fd_monitor, NS_RUN_DIR,
352 IN_CREATE | IN_DELETE) < 0) {
450971aa 353 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe
QY
354 "NS notify watch: failed to add watch (%s)",
355 safe_strerror(errno));
e27dec3c 356 }
996c9314 357 zebra_netns_notify_current = thread_add_read(
3801e764 358 zrouter.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
e27dec3c
PG
359}
360
361void zebra_ns_notify_close(void)
362{
363 if (zebra_netns_notify_current == NULL)
364 return;
365
366 int fd = 0;
367
368 if (zebra_netns_notify_current->u.fd > 0)
369 fd = zebra_netns_notify_current->u.fd;
71077c48
MS
370
371 if (zebra_netns_notify_current->master != NULL)
372 thread_cancel(zebra_netns_notify_current);
373
374 /* auto-removal of notify items */
e27dec3c
PG
375 if (fd > 0)
376 close(fd);
377}
378
379#else
380void zebra_ns_notify_parse(void)
381{
382}
383
384void zebra_ns_notify_init(void)
385{
386}
387
388void zebra_ns_notify_close(void)
389{
390}
391#endif /* !HAVE_NETLINK */