2 * Zebra NS collector and notifier for Network NameSpaces
3 * Copyright (C) 2017 6WIND
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)
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
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
30 #include <sys/inotify.h>
39 #include "zebra_memory.h"
40 #endif /* defined(HAVE_NETLINK) */
42 #include "zebra_netns_notify.h"
43 #include "zebra_netns_id.h"
47 /* upon creation of folder under /var/run/netns,
48 * wait that netns context is bound to
49 * that folder 10 seconds
51 #define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
52 #define ZEBRA_NS_POLLING_MAX_RETRIES 200
54 DEFINE_MTYPE_STATIC(ZEBRA
, NETNS_MISC
, "ZebraNetNSInfo")
55 static struct thread
*zebra_netns_notify_current
;
57 struct zebra_netns_info
{
58 const char *netnspath
;
62 static int zebra_ns_ready_read(struct thread
*t
);
63 static void zebra_ns_notify_create_context_from_entry_name(const char *name
);
64 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
66 static int zebra_ns_notify_read(struct thread
*t
);
68 static void zebra_ns_notify_create_context_from_entry_name(const char *name
)
70 char *netnspath
= ns_netns_pathname(NULL
, name
);
73 ns_id_t ns_id
, ns_id_external
;
75 if (netnspath
== NULL
)
78 if (zserv_privs
.change(ZPRIVS_RAISE
))
79 zlog_err("Can't raise privileges");
80 ns_id
= zebra_ns_id_get(netnspath
);
81 if (zserv_privs
.change(ZPRIVS_LOWER
))
82 zlog_err("Can't lower privileges");
83 if (ns_id
== NS_UNKNOWN
)
85 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
86 /* if VRF with NS ID already present */
87 vrf
= vrf_lookup_by_id((vrf_id_t
)ns_id_external
);
90 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
91 vrf
->name
, netnspath
);
94 if (vrf_handler_create(NULL
, name
, &vrf
) != CMD_SUCCESS
) {
95 zlog_warn("NS notify : failed to create VRF %s", name
);
96 ns_map_nsid_with_external(ns_id
, false);
99 if (zserv_privs
.change(ZPRIVS_RAISE
))
100 zlog_err("Can't raise privileges");
101 ret
= vrf_netns_handler_create(NULL
, vrf
, netnspath
,
102 ns_id_external
, ns_id
);
103 if (zserv_privs
.change(ZPRIVS_LOWER
))
104 zlog_err("Can't lower privileges");
105 if (ret
!= CMD_SUCCESS
) {
106 zlog_warn("NS notify : failed to create NS %s", netnspath
);
107 ns_map_nsid_with_external(ns_id
, false);
111 zlog_info("NS notify : created VRF %s NS %s", name
, netnspath
);
114 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
117 void *ns_path_ptr
= (void *)zns_info
->netnspath
;
120 XFREE(MTYPE_NETNS_MISC
, ns_path_ptr
);
121 XFREE(MTYPE_NETNS_MISC
, zns_info
);
124 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
125 (void *)zns_info
, ZEBRA_NS_POLLING_INTERVAL_MSEC
,
130 static int zebra_ns_delete(char *name
)
132 struct vrf
*vrf
= vrf_lookup_by_name(name
);
137 "NS notify : no VRF found using NS %s",
141 /* Clear configured flag and invoke delete. */
142 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
143 ns
= (struct ns
*)vrf
->ns_ctxt
;
144 /* the deletion order is the same
145 * as the one used when siging signal is received
151 zlog_info("NS notify : deleted VRF %s", name
);
156 static int zebra_ns_ready_read(struct thread
*t
)
158 struct zebra_netns_info
*zns_info
= THREAD_ARG(t
);
159 const char *netnspath
;
160 int err
, stop_retry
= 0;
164 if (!zns_info
->netnspath
) {
165 XFREE(MTYPE_NETNS_MISC
, zns_info
);
168 netnspath
= zns_info
->netnspath
;
169 if (--zns_info
->retries
== 0)
171 if (zserv_privs
.change(ZPRIVS_RAISE
))
172 zlog_err("Can't raise privileges");
173 err
= ns_switch_to_netns(netnspath
);
174 if (zserv_privs
.change(ZPRIVS_LOWER
))
175 zlog_err("Can't lower privileges");
177 return zebra_ns_continue_read(zns_info
, stop_retry
);
179 /* go back to default ns */
180 if (zserv_privs
.change(ZPRIVS_RAISE
))
181 zlog_err("Can't raise privileges");
182 err
= ns_switchback_to_initial();
183 if (zserv_privs
.change(ZPRIVS_LOWER
))
184 zlog_err("Can't lower privileges");
186 return zebra_ns_continue_read(zns_info
, stop_retry
);
188 /* success : close fd and create zns context */
189 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
190 return zebra_ns_continue_read(zns_info
, 1);
193 static int zebra_ns_notify_read(struct thread
*t
)
195 int fd_monitor
= THREAD_FD(t
);
196 struct inotify_event
*event
;
200 zebra_netns_notify_current
= thread_add_read(
201 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
202 len
= read(fd_monitor
, buf
, sizeof(buf
));
204 zlog_warn("NS notify read: failed to read (%s)",
205 safe_strerror(errno
));
208 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
209 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
212 struct zebra_netns_info
*netnsinfo
;
214 if (!(event
->mask
& (IN_CREATE
| IN_DELETE
)))
216 if (event
->mask
& IN_DELETE
)
217 return zebra_ns_delete(event
->name
);
219 if (offsetof(struct inotify_event
, name
) + event
->len
221 zlog_err("NS notify read: buffer underflow");
225 if (strnlen(event
->name
, event
->len
) == event
->len
) {
226 zlog_err("NS notify error: bad event name");
230 netnspath
= ns_netns_pathname(NULL
, event
->name
);
233 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
234 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
235 sizeof(struct zebra_netns_info
));
236 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
237 netnsinfo
->netnspath
= netnspath
;
238 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
239 (void *)netnsinfo
, 0, NULL
);
244 void zebra_ns_notify_parse(void)
247 DIR *srcdir
= opendir(NS_RUN_DIR
);
249 if (srcdir
== NULL
) {
250 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR
);
253 while ((dent
= readdir(srcdir
)) != NULL
) {
256 if (strcmp(dent
->d_name
, ".") == 0
257 || strcmp(dent
->d_name
, "..") == 0)
259 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
260 zlog_warn("NS parsing init: failed to parse entry %s",
264 if (S_ISDIR(st
.st_mode
)) {
265 zlog_warn("NS parsing init: %s is not a NS",
269 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
274 void zebra_ns_notify_init(void)
278 zebra_netns_notify_current
= NULL
;
279 fd_monitor
= inotify_init();
280 if (fd_monitor
< 0) {
281 zlog_warn("NS notify init: failed to initialize inotify (%s)",
282 safe_strerror(errno
));
284 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
,
285 IN_CREATE
| IN_DELETE
) < 0) {
286 zlog_warn("NS notify watch: failed to add watch (%s)",
287 safe_strerror(errno
));
289 zebra_netns_notify_current
= thread_add_read(
290 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
293 void zebra_ns_notify_close(void)
295 if (zebra_netns_notify_current
== NULL
)
300 if (zebra_netns_notify_current
->u
.fd
> 0)
301 fd
= zebra_netns_notify_current
->u
.fd
;
302 thread_cancel(zebra_netns_notify_current
);
303 /* auto-removal of inotify items */
309 void zebra_ns_notify_parse(void)
313 void zebra_ns_notify_init(void)
317 void zebra_ns_notify_close(void)
320 #endif /* !HAVE_NETLINK */