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 ns_id_external
= ns_map_nsid_with_external(ns_id
, true);
84 /* if VRF with NS ID already present */
85 vrf
= vrf_lookup_by_id((vrf_id_t
)ns_id_external
);
88 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
89 vrf
->name
, netnspath
);
92 if (vrf_handler_create(NULL
, name
, &vrf
) != CMD_SUCCESS
) {
93 zlog_warn("NS notify : failed to create VRF %s", name
);
94 ns_map_nsid_with_external(ns_id
, false);
97 if (zserv_privs
.change(ZPRIVS_RAISE
))
98 zlog_err("Can't raise privileges");
99 ret
= vrf_netns_handler_create(NULL
, vrf
, netnspath
,
100 ns_id_external
, ns_id
);
101 if (zserv_privs
.change(ZPRIVS_LOWER
))
102 zlog_err("Can't lower privileges");
103 if (ret
!= CMD_SUCCESS
) {
104 zlog_warn("NS notify : failed to create NS %s", netnspath
);
105 ns_map_nsid_with_external(ns_id
, false);
108 zlog_info("NS notify : created VRF %s NS %s", name
, netnspath
);
111 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
114 void *ns_path_ptr
= (void *)zns_info
->netnspath
;
117 XFREE(MTYPE_NETNS_MISC
, ns_path_ptr
);
118 XFREE(MTYPE_NETNS_MISC
, zns_info
);
121 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
122 (void *)zns_info
, ZEBRA_NS_POLLING_INTERVAL_MSEC
,
127 static int zebra_ns_delete(char *name
)
129 struct vrf
*vrf
= vrf_lookup_by_name(name
);
134 "NS notify : no VRF found using NS %s",
138 /* Clear configured flag and invoke delete. */
139 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
140 ns
= (struct ns
*)vrf
->ns_ctxt
;
141 /* the deletion order is the same
142 * as the one used when siging signal is received
148 zlog_info("NS notify : deleted VRF %s", name
);
153 static int zebra_ns_ready_read(struct thread
*t
)
155 struct zebra_netns_info
*zns_info
= THREAD_ARG(t
);
156 const char *netnspath
;
157 int err
, stop_retry
= 0;
161 if (!zns_info
->netnspath
) {
162 XFREE(MTYPE_NETNS_MISC
, zns_info
);
165 netnspath
= zns_info
->netnspath
;
166 if (--zns_info
->retries
== 0)
168 if (zserv_privs
.change(ZPRIVS_RAISE
))
169 zlog_err("Can't raise privileges");
170 err
= ns_switch_to_netns(netnspath
);
171 if (zserv_privs
.change(ZPRIVS_LOWER
))
172 zlog_err("Can't lower privileges");
174 return zebra_ns_continue_read(zns_info
, stop_retry
);
176 /* go back to default ns */
177 if (zserv_privs
.change(ZPRIVS_RAISE
))
178 zlog_err("Can't raise privileges");
179 err
= ns_switchback_to_initial();
180 if (zserv_privs
.change(ZPRIVS_LOWER
))
181 zlog_err("Can't lower privileges");
183 return zebra_ns_continue_read(zns_info
, stop_retry
);
185 /* success : close fd and create zns context */
186 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
187 return zebra_ns_continue_read(zns_info
, 1);
190 static int zebra_ns_notify_read(struct thread
*t
)
192 int fd_monitor
= THREAD_FD(t
);
193 struct inotify_event
*event
;
197 zebra_netns_notify_current
= thread_add_read(
198 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
199 len
= read(fd_monitor
, buf
, sizeof(buf
));
201 zlog_warn("NS notify read: failed to read (%s)",
202 safe_strerror(errno
));
205 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
206 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
209 struct zebra_netns_info
*netnsinfo
;
211 if (!(event
->mask
& (IN_CREATE
| IN_DELETE
)))
213 if (event
->mask
& IN_DELETE
)
214 return zebra_ns_delete(event
->name
);
215 if (&event
->name
[event
->len
] >= &buf
[sizeof(buf
)]) {
216 zlog_err("NS notify read: buffer underflow");
219 event
->name
[event
->len
] = 0;
220 netnspath
= ns_netns_pathname(NULL
, event
->name
);
223 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
224 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
225 sizeof(struct zebra_netns_info
));
226 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
227 netnsinfo
->netnspath
= netnspath
;
228 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
229 (void *)netnsinfo
, 0, NULL
);
234 void zebra_ns_notify_parse(void)
237 DIR *srcdir
= opendir(NS_RUN_DIR
);
239 if (srcdir
== NULL
) {
240 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR
);
243 while ((dent
= readdir(srcdir
)) != NULL
) {
246 if (strcmp(dent
->d_name
, ".") == 0
247 || strcmp(dent
->d_name
, "..") == 0)
249 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
250 zlog_warn("NS parsing init: failed to parse entry %s",
254 if (S_ISDIR(st
.st_mode
)) {
255 zlog_warn("NS parsing init: %s is not a NS",
259 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
264 void zebra_ns_notify_init(void)
268 zebra_netns_notify_current
= NULL
;
269 fd_monitor
= inotify_init();
270 if (fd_monitor
< 0) {
271 zlog_warn("NS notify init: failed to initialize inotify (%s)",
272 safe_strerror(errno
));
274 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
,
275 IN_CREATE
| IN_DELETE
) < 0) {
276 zlog_warn("NS notify watch: failed to add watch (%s)",
277 safe_strerror(errno
));
279 zebra_netns_notify_current
= thread_add_read(
280 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
283 void zebra_ns_notify_close(void)
285 if (zebra_netns_notify_current
== NULL
)
290 if (zebra_netns_notify_current
->u
.fd
> 0)
291 fd
= zebra_netns_notify_current
->u
.fd
;
292 thread_cancel(zebra_netns_notify_current
);
293 /* auto-removal of inotify items */
299 void zebra_ns_notify_parse(void)
303 void zebra_ns_notify_init(void)
307 void zebra_ns_notify_close(void)
310 #endif /* !HAVE_NETLINK */