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
);
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 VRF with NS ID already present */
84 vrf
= vrf_lookup_by_id((vrf_id_t
)ns_id
);
87 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
88 vrf
->name
, netnspath
);
91 if (vrf_handler_create(NULL
, name
, &vrf
) != CMD_SUCCESS
) {
92 zlog_warn("NS notify : failed to create VRF %s", name
);
95 ret
= vrf_netns_handler_create(NULL
, vrf
, netnspath
, ns_id
);
96 if (ret
!= CMD_SUCCESS
) {
97 zlog_warn("NS notify : failed to create NS %s", netnspath
);
100 zlog_info("NS notify : created VRF %s NS %s", name
, netnspath
);
103 static int zebra_ns_continue_read(struct zebra_netns_info
*zns_info
,
106 void *ns_path_ptr
= (void *)zns_info
->netnspath
;
109 XFREE(MTYPE_NETNS_MISC
, ns_path_ptr
);
110 XFREE(MTYPE_NETNS_MISC
, zns_info
);
113 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
114 (void *)zns_info
, ZEBRA_NS_POLLING_INTERVAL_MSEC
,
119 static int zebra_ns_ready_read(struct thread
*t
)
121 struct zebra_netns_info
*zns_info
= THREAD_ARG(t
);
122 const char *netnspath
;
123 int err
, stop_retry
= 0;
127 if (!zns_info
->netnspath
) {
128 XFREE(MTYPE_NETNS_MISC
, zns_info
);
131 netnspath
= zns_info
->netnspath
;
132 if (--zns_info
->retries
== 0)
134 if (zserv_privs
.change(ZPRIVS_RAISE
))
135 zlog_err("Can't raise privileges");
136 err
= ns_switch_to_netns(netnspath
);
137 if (zserv_privs
.change(ZPRIVS_LOWER
))
138 zlog_err("Can't lower privileges");
140 return zebra_ns_continue_read(zns_info
, stop_retry
);
142 /* go back to default ns */
143 if (zserv_privs
.change(ZPRIVS_RAISE
))
144 zlog_err("Can't raise privileges");
145 err
= ns_switchback_to_initial();
146 if (zserv_privs
.change(ZPRIVS_LOWER
))
147 zlog_err("Can't lower privileges");
149 return zebra_ns_continue_read(zns_info
, stop_retry
);
151 /* success : close fd and create zns context */
152 zebra_ns_notify_create_context_from_entry_name(basename(netnspath
));
153 return zebra_ns_continue_read(zns_info
, 1);
156 static int zebra_ns_notify_read(struct thread
*t
)
158 int fd_monitor
= THREAD_FD(t
);
159 struct inotify_event
*event
;
163 zebra_netns_notify_current
= thread_add_read(
164 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
165 len
= read(fd_monitor
, buf
, sizeof(buf
));
167 zlog_warn("NS notify read: failed to read (%s)",
168 safe_strerror(errno
));
171 for (event
= (struct inotify_event
*)buf
; (char *)event
< &buf
[len
];
172 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
)
175 struct zebra_netns_info
*netnsinfo
;
177 if (!(event
->mask
& IN_CREATE
))
179 netnspath
= ns_netns_pathname(NULL
, event
->name
);
182 netnspath
= XSTRDUP(MTYPE_NETNS_MISC
, netnspath
);
183 netnsinfo
= XCALLOC(MTYPE_NETNS_MISC
,
184 sizeof(struct zebra_netns_info
));
185 netnsinfo
->retries
= ZEBRA_NS_POLLING_MAX_RETRIES
;
186 netnsinfo
->netnspath
= netnspath
;
187 thread_add_timer_msec(zebrad
.master
, zebra_ns_ready_read
,
188 (void *)netnsinfo
, 0, NULL
);
193 void zebra_ns_notify_parse(void)
196 DIR *srcdir
= opendir(NS_RUN_DIR
);
198 if (srcdir
== NULL
) {
199 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR
);
202 while ((dent
= readdir(srcdir
)) != NULL
) {
205 if (strcmp(dent
->d_name
, ".") == 0
206 || strcmp(dent
->d_name
, "..") == 0)
208 if (fstatat(dirfd(srcdir
), dent
->d_name
, &st
, 0) < 0) {
209 zlog_warn("NS parsing init: failed to parse entry %s",
213 if (S_ISDIR(st
.st_mode
)) {
214 zlog_warn("NS parsing init: %s is not a NS",
218 zebra_ns_notify_create_context_from_entry_name(dent
->d_name
);
223 void zebra_ns_notify_init(void)
227 zebra_netns_notify_current
= NULL
;
228 fd_monitor
= inotify_init();
229 if (fd_monitor
< 0) {
230 zlog_warn("NS notify init: failed to initialize inotify (%s)",
231 safe_strerror(errno
));
233 if (inotify_add_watch(fd_monitor
, NS_RUN_DIR
, IN_CREATE
) < 0) {
234 zlog_warn("NS notify watch: failed to add watch (%s)",
235 safe_strerror(errno
));
237 zebra_netns_notify_current
= thread_add_read(
238 zebrad
.master
, zebra_ns_notify_read
, NULL
, fd_monitor
, NULL
);
241 void zebra_ns_notify_close(void)
243 if (zebra_netns_notify_current
== NULL
)
248 if (zebra_netns_notify_current
->u
.fd
> 0)
249 fd
= zebra_netns_notify_current
->u
.fd
;
250 thread_cancel(zebra_netns_notify_current
);
251 /* auto-removal of inotify items */
257 void zebra_ns_notify_parse(void)
261 void zebra_ns_notify_init(void)
265 void zebra_ns_notify_close(void)
268 #endif /* !HAVE_NETLINK */