]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_netns_notify.c
Merge pull request #2669 from netravnen/hotfix/documentation/activate-bgp-rpki
[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"
37
38#include "zserv.h"
39#include "zebra_memory.h"
40#endif /* defined(HAVE_NETLINK) */
41
42#include "zebra_netns_notify.h"
43#include "zebra_netns_id.h"
44
45#ifdef HAVE_NETLINK
46
47/* upon creation of folder under /var/run/netns,
48 * wait that netns context is bound to
49 * that folder 10 seconds
50 */
51#define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
52#define ZEBRA_NS_POLLING_MAX_RETRIES 200
53
54DEFINE_MTYPE_STATIC(ZEBRA, NETNS_MISC, "ZebraNetNSInfo")
55static struct thread *zebra_netns_notify_current;
56
57struct zebra_netns_info {
58 const char *netnspath;
59 unsigned int retries;
60};
61
62static int zebra_ns_ready_read(struct thread *t);
63static void zebra_ns_notify_create_context_from_entry_name(const char *name);
64static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
65 int stop_retry);
66static int zebra_ns_notify_read(struct thread *t);
67
68static void zebra_ns_notify_create_context_from_entry_name(const char *name)
69{
70 char *netnspath = ns_netns_pathname(NULL, name);
71 struct vrf *vrf;
72 int ret;
03aff2d8 73 ns_id_t ns_id, ns_id_external;
e27dec3c
PG
74
75 if (netnspath == NULL)
76 return;
77
e27dec3c
PG
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");
03aff2d8 83 ns_id_external = ns_map_nsid_with_external(ns_id, true);
b7b816df 84 /* if VRF with NS ID already present */
03aff2d8 85 vrf = vrf_lookup_by_id((vrf_id_t)ns_id_external);
b7b816df 86 if (vrf) {
996c9314
LB
87 zlog_warn(
88 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
89 vrf->name, netnspath);
b7b816df
PG
90 return;
91 }
92 if (vrf_handler_create(NULL, name, &vrf) != CMD_SUCCESS) {
93 zlog_warn("NS notify : failed to create VRF %s", name);
03aff2d8 94 ns_map_nsid_with_external(ns_id, false);
b7b816df
PG
95 return;
96 }
1c9d288e
PG
97 if (zserv_privs.change(ZPRIVS_RAISE))
98 zlog_err("Can't raise privileges");
03aff2d8
PG
99 ret = vrf_netns_handler_create(NULL, vrf, netnspath,
100 ns_id_external, ns_id);
1c9d288e
PG
101 if (zserv_privs.change(ZPRIVS_LOWER))
102 zlog_err("Can't lower privileges");
e27dec3c
PG
103 if (ret != CMD_SUCCESS) {
104 zlog_warn("NS notify : failed to create NS %s", netnspath);
03aff2d8 105 ns_map_nsid_with_external(ns_id, false);
e27dec3c
PG
106 return;
107 }
996c9314 108 zlog_info("NS notify : created VRF %s NS %s", name, netnspath);
e27dec3c
PG
109}
110
111static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
112 int stop_retry)
113{
114 void *ns_path_ptr = (void *)zns_info->netnspath;
115
116 if (stop_retry) {
117 XFREE(MTYPE_NETNS_MISC, ns_path_ptr);
118 XFREE(MTYPE_NETNS_MISC, zns_info);
119 return 0;
120 }
121 thread_add_timer_msec(zebrad.master, zebra_ns_ready_read,
996c9314
LB
122 (void *)zns_info, ZEBRA_NS_POLLING_INTERVAL_MSEC,
123 NULL);
e27dec3c
PG
124 return 0;
125}
126
0c902ba5
PG
127static int zebra_ns_delete(char *name)
128{
129 struct vrf *vrf = vrf_lookup_by_name(name);
130 struct ns *ns;
131
132 if (!vrf) {
133 zlog_warn(
134 "NS notify : no VRF found using NS %s",
135 name);
136 return 0;
137 }
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
143 */
144 vrf_delete(vrf);
145 if (ns)
146 ns_delete(ns);
147
148 zlog_info("NS notify : deleted VRF %s", name);
149 return 0;
150}
151
152
e27dec3c
PG
153static int zebra_ns_ready_read(struct thread *t)
154{
155 struct zebra_netns_info *zns_info = THREAD_ARG(t);
156 const char *netnspath;
157 int err, stop_retry = 0;
158
159 if (!zns_info)
160 return 0;
161 if (!zns_info->netnspath) {
162 XFREE(MTYPE_NETNS_MISC, zns_info);
163 return 0;
164 }
165 netnspath = zns_info->netnspath;
166 if (--zns_info->retries == 0)
167 stop_retry = 1;
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");
173 if (err < 0)
174 return zebra_ns_continue_read(zns_info, stop_retry);
175
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");
182 if (err < 0)
183 return zebra_ns_continue_read(zns_info, stop_retry);
184
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);
188}
189
190static int zebra_ns_notify_read(struct thread *t)
191{
192 int fd_monitor = THREAD_FD(t);
193 struct inotify_event *event;
194 char buf[BUFSIZ];
195 ssize_t len;
196
996c9314
LB
197 zebra_netns_notify_current = thread_add_read(
198 zebrad.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
e27dec3c
PG
199 len = read(fd_monitor, buf, sizeof(buf));
200 if (len < 0) {
201 zlog_warn("NS notify read: failed to read (%s)",
202 safe_strerror(errno));
203 return 0;
204 }
996c9314
LB
205 for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
206 event = (struct inotify_event *)((char *)event + sizeof(*event)
207 + event->len)) {
e27dec3c
PG
208 char *netnspath;
209 struct zebra_netns_info *netnsinfo;
210
0c902ba5 211 if (!(event->mask & (IN_CREATE | IN_DELETE)))
e27dec3c 212 continue;
b00592cb 213 if (event->mask & IN_DELETE)
0c902ba5 214 return zebra_ns_delete(event->name);
45981fda 215
216 if (offsetof(struct inotify_event, name) + event->len
217 >= sizeof(buf)) {
32ac96b2 218 zlog_err("NS notify read: buffer underflow");
219 break;
220 }
b6312ad1 221
222 if (strnlen(event->name, event->len) == event->len) {
223 zlog_err("NS notify error: bad event name");
224 break;
225 }
226
e27dec3c
PG
227 netnspath = ns_netns_pathname(NULL, event->name);
228 if (!netnspath)
229 continue;
230 netnspath = XSTRDUP(MTYPE_NETNS_MISC, netnspath);
231 netnsinfo = XCALLOC(MTYPE_NETNS_MISC,
232 sizeof(struct zebra_netns_info));
233 netnsinfo->retries = ZEBRA_NS_POLLING_MAX_RETRIES;
234 netnsinfo->netnspath = netnspath;
235 thread_add_timer_msec(zebrad.master, zebra_ns_ready_read,
996c9314 236 (void *)netnsinfo, 0, NULL);
e27dec3c
PG
237 }
238 return 0;
239}
240
241void zebra_ns_notify_parse(void)
242{
243 struct dirent *dent;
244 DIR *srcdir = opendir(NS_RUN_DIR);
245
246 if (srcdir == NULL) {
247 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR);
248 return;
249 }
250 while ((dent = readdir(srcdir)) != NULL) {
251 struct stat st;
252
253 if (strcmp(dent->d_name, ".") == 0
996c9314 254 || strcmp(dent->d_name, "..") == 0)
e27dec3c
PG
255 continue;
256 if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
257 zlog_warn("NS parsing init: failed to parse entry %s",
258 dent->d_name);
259 continue;
260 }
261 if (S_ISDIR(st.st_mode)) {
262 zlog_warn("NS parsing init: %s is not a NS",
263 dent->d_name);
264 continue;
265 }
266 zebra_ns_notify_create_context_from_entry_name(dent->d_name);
267 }
268 closedir(srcdir);
269}
270
271void zebra_ns_notify_init(void)
272{
273 int fd_monitor;
274
275 zebra_netns_notify_current = NULL;
276 fd_monitor = inotify_init();
277 if (fd_monitor < 0) {
278 zlog_warn("NS notify init: failed to initialize inotify (%s)",
279 safe_strerror(errno));
280 }
0c902ba5
PG
281 if (inotify_add_watch(fd_monitor, NS_RUN_DIR,
282 IN_CREATE | IN_DELETE) < 0) {
e27dec3c
PG
283 zlog_warn("NS notify watch: failed to add watch (%s)",
284 safe_strerror(errno));
285 }
996c9314
LB
286 zebra_netns_notify_current = thread_add_read(
287 zebrad.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
e27dec3c
PG
288}
289
290void zebra_ns_notify_close(void)
291{
292 if (zebra_netns_notify_current == NULL)
293 return;
294
295 int fd = 0;
296
297 if (zebra_netns_notify_current->u.fd > 0)
298 fd = zebra_netns_notify_current->u.fd;
299 thread_cancel(zebra_netns_notify_current);
300 /* auto-removal of inotify items */
301 if (fd > 0)
302 close(fd);
303}
304
305#else
306void zebra_ns_notify_parse(void)
307{
308}
309
310void zebra_ns_notify_init(void)
311{
312}
313
314void zebra_ns_notify_close(void)
315{
316}
317#endif /* !HAVE_NETLINK */