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);
110 zlog_info("NS notify : created VRF %s NS %s", name
, netnspath
);
113 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
116 void *ns_path_ptr
= (void *)zns_info
->netnspath
;
119 XFREE(MTYPE_NETNS_MISC
, ns_path_ptr
);
120 XFREE(MTYPE_NETNS_MISC
, zns_info
);
123 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
124 (void *)zns_info
, ZEBRA_NS_POLLING_INTERVAL_MSEC
,
129 static int zebra_ns_delete(char *name
)
131 struct vrf
*vrf
= vrf_lookup_by_name(name
);
136 "NS notify : no VRF found using NS %s",
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
150 zlog_info("NS notify : deleted VRF %s", name
);
155 static int zebra_ns_ready_read(struct thread
*t
)
157 struct zebra_netns_info
*zns_info
= THREAD_ARG(t
);
158 const char *netnspath
;
159 int err
, stop_retry
= 0;
163 if (!zns_info
->netnspath
) {
164 XFREE(MTYPE_NETNS_MISC
, zns_info
);
167 netnspath
= zns_info
->netnspath
;
168 if (--zns_info
->retries
== 0)
170 if (zserv_privs
.change(ZPRIVS_RAISE
))
171 zlog_err("Can't raise privileges");
172 err
= ns_switch_to_netns(netnspath
);
173 if (zserv_privs
.change(ZPRIVS_LOWER
))
174 zlog_err("Can't lower privileges");
176 return zebra_ns_continue_read(zns_info
, stop_retry
);
178 /* go back to default ns */
179 if (zserv_privs
.change(ZPRIVS_RAISE
))
180 zlog_err("Can't raise privileges");
181 err
= ns_switchback_to_initial();
182 if (zserv_privs
.change(ZPRIVS_LOWER
))
183 zlog_err("Can't lower privileges");
185 return zebra_ns_continue_read(zns_info
, stop_retry
);
187 /* success : close fd and create zns context */
188 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
189 return zebra_ns_continue_read(zns_info
, 1);
192 static int zebra_ns_notify_read(struct thread
*t
)
194 int fd_monitor
= THREAD_FD(t
);
195 struct inotify_event
*event
;
199 zebra_netns_notify_current
= thread_add_read(
200 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
201 len
= read(fd_monitor
, buf
, sizeof(buf
));
203 zlog_warn("NS notify read: failed to read (%s)",
204 safe_strerror(errno
));
207 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
208 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
211 struct zebra_netns_info
*netnsinfo
;
213 if (!(event
->mask
& (IN_CREATE
| IN_DELETE
)))
215 if (event
->mask
& IN_DELETE
)
216 return zebra_ns_delete(event
->name
);
218 if (offsetof(struct inotify_event
, name
) + event
->len
220 zlog_err("NS notify read: buffer underflow");
224 if (strnlen(event
->name
, event
->len
) == event
->len
) {
225 zlog_err("NS notify error: bad event name");
229 netnspath
= ns_netns_pathname(NULL
, event
->name
);
232 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
233 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
234 sizeof(struct zebra_netns_info
));
235 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
236 netnsinfo
->netnspath
= netnspath
;
237 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
238 (void *)netnsinfo
, 0, NULL
);
243 void zebra_ns_notify_parse(void)
246 DIR *srcdir
= opendir(NS_RUN_DIR
);
248 if (srcdir
== NULL
) {
249 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR
);
252 while ((dent
= readdir(srcdir
)) != NULL
) {
255 if (strcmp(dent
->d_name
, ".") == 0
256 || strcmp(dent
->d_name
, "..") == 0)
258 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
259 zlog_warn("NS parsing init: failed to parse entry %s",
263 if (S_ISDIR(st
.st_mode
)) {
264 zlog_warn("NS parsing init: %s is not a NS",
268 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
273 void zebra_ns_notify_init(void)
277 zebra_netns_notify_current
= NULL
;
278 fd_monitor
= inotify_init();
279 if (fd_monitor
< 0) {
280 zlog_warn("NS notify init: failed to initialize inotify (%s)",
281 safe_strerror(errno
));
283 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
,
284 IN_CREATE
| IN_DELETE
) < 0) {
285 zlog_warn("NS notify watch: failed to add watch (%s)",
286 safe_strerror(errno
));
288 zebra_netns_notify_current
= thread_add_read(
289 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
292 void zebra_ns_notify_close(void)
294 if (zebra_netns_notify_current
== NULL
)
299 if (zebra_netns_notify_current
->u
.fd
> 0)
300 fd
= zebra_netns_notify_current
->u
.fd
;
301 thread_cancel(zebra_netns_notify_current
);
302 /* auto-removal of inotify items */
308 void zebra_ns_notify_parse(void)
312 void zebra_ns_notify_init(void)
316 void zebra_ns_notify_close(void)
319 #endif /* !HAVE_NETLINK */