]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_netns_notify.c
isisd: implement the 'lsp-too-large' notification
[mirror_frr.git] / zebra / zebra_netns_notify.c
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 #include "lib_errors.h"
38
39 #include "zserv.h"
40 #include "zebra_memory.h"
41 #endif /* defined(HAVE_NETLINK) */
42
43 #include "zebra_netns_notify.h"
44 #include "zebra_netns_id.h"
45 #include "zebra_errors.h"
46
47 #ifdef HAVE_NETLINK
48
49 /* upon creation of folder under /var/run/netns,
50 * wait that netns context is bound to
51 * that folder 10 seconds
52 */
53 #define ZEBRA_NS_POLLING_INTERVAL_MSEC 1000
54 #define ZEBRA_NS_POLLING_MAX_RETRIES 200
55
56 DEFINE_MTYPE_STATIC(ZEBRA, NETNS_MISC, "ZebraNetNSInfo")
57 static struct thread *zebra_netns_notify_current;
58
59 struct zebra_netns_info {
60 const char *netnspath;
61 unsigned int retries;
62 };
63
64 static int zebra_ns_ready_read(struct thread *t);
65 static void zebra_ns_notify_create_context_from_entry_name(const char *name);
66 static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
67 int stop_retry);
68 static int zebra_ns_notify_read(struct thread *t);
69
70 static void zebra_ns_notify_create_context_from_entry_name(const char *name)
71 {
72 char *netnspath = ns_netns_pathname(NULL, name);
73 struct vrf *vrf;
74 int ret;
75 ns_id_t ns_id, ns_id_external;
76
77 if (netnspath == NULL)
78 return;
79
80 frr_elevate_privs(&zserv_privs) {
81 ns_id = zebra_ns_id_get(netnspath);
82 }
83 if (ns_id == NS_UNKNOWN)
84 return;
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);
88 if (vrf) {
89 zlog_debug(
90 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
91 vrf->name, netnspath);
92 return;
93 }
94 if (vrf_handler_create(NULL, name, &vrf) != CMD_SUCCESS) {
95 flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
96 "NS notify : failed to create VRF %s", name);
97 ns_map_nsid_with_external(ns_id, false);
98 return;
99 }
100 frr_elevate_privs(&zserv_privs) {
101 ret = vrf_netns_handler_create(NULL, vrf, netnspath,
102 ns_id_external, ns_id);
103 }
104 if (ret != CMD_SUCCESS) {
105 flog_warn(EC_ZEBRA_NS_VRF_CREATION_FAILED,
106 "NS notify : failed to create NS %s", netnspath);
107 ns_map_nsid_with_external(ns_id, false);
108 vrf_delete(vrf);
109 return;
110 }
111 zlog_info("NS notify : created VRF %s NS %s", name, netnspath);
112 }
113
114 static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
115 int stop_retry)
116 {
117 void *ns_path_ptr = (void *)zns_info->netnspath;
118
119 if (stop_retry) {
120 XFREE(MTYPE_NETNS_MISC, ns_path_ptr);
121 XFREE(MTYPE_NETNS_MISC, zns_info);
122 return 0;
123 }
124 thread_add_timer_msec(zebrad.master, zebra_ns_ready_read,
125 (void *)zns_info, ZEBRA_NS_POLLING_INTERVAL_MSEC,
126 NULL);
127 return 0;
128 }
129
130 static int zebra_ns_delete(char *name)
131 {
132 struct vrf *vrf = vrf_lookup_by_name(name);
133 struct ns *ns;
134
135 if (!vrf) {
136 flog_warn(EC_ZEBRA_NS_DELETION_FAILED_NO_VRF,
137 "NS notify : no VRF found using NS %s", name);
138 return 0;
139 }
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
145 */
146 vrf_delete(vrf);
147 if (ns)
148 ns_delete(ns);
149
150 zlog_info("NS notify : deleted VRF %s", name);
151 return 0;
152 }
153
154 static int zebra_ns_notify_self_identify(struct stat *netst)
155 {
156 char net_path[64];
157 int netns;
158
159 sprintf(net_path, "/proc/self/ns/net");
160 netns = open(net_path, O_RDONLY);
161 if (netns < 0)
162 return -1;
163 if (fstat(netns, netst) < 0) {
164 close(netns);
165 return -1;
166 }
167 close(netns);
168 return 0;
169 }
170
171 static bool zebra_ns_notify_is_default_netns(const char *name)
172 {
173 struct stat default_netns_stat;
174 struct stat st;
175 char netnspath[64];
176
177 if (zebra_ns_notify_self_identify(&default_netns_stat))
178 return false;
179
180 memset(&st, 0, sizeof(struct stat));
181 snprintf(netnspath, 64, "%s/%s", NS_RUN_DIR, name);
182 /* compare with local stat */
183 if (stat(netnspath, &st) == 0 &&
184 (st.st_dev == default_netns_stat.st_dev) &&
185 (st.st_ino == default_netns_stat.st_ino))
186 return true;
187 return false;
188 }
189
190 static int zebra_ns_ready_read(struct thread *t)
191 {
192 struct zebra_netns_info *zns_info = THREAD_ARG(t);
193 const char *netnspath;
194 int err, stop_retry = 0;
195
196 if (!zns_info)
197 return 0;
198 if (!zns_info->netnspath) {
199 XFREE(MTYPE_NETNS_MISC, zns_info);
200 return 0;
201 }
202 netnspath = zns_info->netnspath;
203 if (--zns_info->retries == 0)
204 stop_retry = 1;
205 frr_elevate_privs(&zserv_privs) {
206 err = ns_switch_to_netns(netnspath);
207 }
208 if (err < 0)
209 return zebra_ns_continue_read(zns_info, stop_retry);
210
211 /* go back to default ns */
212 frr_elevate_privs(&zserv_privs) {
213 err = ns_switchback_to_initial();
214 }
215 if (err < 0)
216 return zebra_ns_continue_read(zns_info, stop_retry);
217
218 if (zebra_ns_notify_is_default_netns(basename(netnspath))) {
219 zlog_warn(
220 "NS notify : NS %s is default VRF."
221 " Updating VRF Name", basename(netnspath));
222 vrf_set_default_name(basename(netnspath), false);
223 return zebra_ns_continue_read(zns_info, 1);
224 }
225
226 /* success : close fd and create zns context */
227 zebra_ns_notify_create_context_from_entry_name(basename(netnspath));
228 return zebra_ns_continue_read(zns_info, 1);
229 }
230
231 static int zebra_ns_notify_read(struct thread *t)
232 {
233 int fd_monitor = THREAD_FD(t);
234 struct inotify_event *event;
235 char buf[BUFSIZ];
236 ssize_t len;
237
238 zebra_netns_notify_current = thread_add_read(
239 zebrad.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
240 len = read(fd_monitor, buf, sizeof(buf));
241 if (len < 0) {
242 flog_err_sys(EC_ZEBRA_NS_NOTIFY_READ,
243 "NS notify read: failed to read (%s)",
244 safe_strerror(errno));
245 return 0;
246 }
247 for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
248 event = (struct inotify_event *)((char *)event + sizeof(*event)
249 + event->len)) {
250 char *netnspath;
251 struct zebra_netns_info *netnsinfo;
252
253 if (!(event->mask & (IN_CREATE | IN_DELETE)))
254 continue;
255 if (event->mask & IN_DELETE)
256 return zebra_ns_delete(event->name);
257
258 if (offsetof(struct inotify_event, name) + event->len
259 >= sizeof(buf)) {
260 flog_err(EC_ZEBRA_NS_NOTIFY_READ,
261 "NS notify read: buffer underflow");
262 break;
263 }
264
265 if (strnlen(event->name, event->len) == event->len) {
266 flog_err(EC_ZEBRA_NS_NOTIFY_READ,
267 "NS notify error: bad event name");
268 break;
269 }
270
271 netnspath = ns_netns_pathname(NULL, event->name);
272 if (!netnspath)
273 continue;
274 netnspath = XSTRDUP(MTYPE_NETNS_MISC, netnspath);
275 netnsinfo = XCALLOC(MTYPE_NETNS_MISC,
276 sizeof(struct zebra_netns_info));
277 netnsinfo->retries = ZEBRA_NS_POLLING_MAX_RETRIES;
278 netnsinfo->netnspath = netnspath;
279 thread_add_timer_msec(zebrad.master, zebra_ns_ready_read,
280 (void *)netnsinfo, 0, NULL);
281 }
282 return 0;
283 }
284
285 void zebra_ns_notify_parse(void)
286 {
287 struct dirent *dent;
288 DIR *srcdir = opendir(NS_RUN_DIR);
289
290 if (srcdir == NULL) {
291 flog_err_sys(EC_LIB_SYSTEM_CALL,
292 "NS parsing init: failed to parse %s", NS_RUN_DIR);
293 return;
294 }
295 while ((dent = readdir(srcdir)) != NULL) {
296 struct stat st;
297
298 if (strcmp(dent->d_name, ".") == 0
299 || strcmp(dent->d_name, "..") == 0)
300 continue;
301 if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
302 flog_err_sys(
303 EC_LIB_SYSTEM_CALL,
304 "NS parsing init: failed to parse entry %s",
305 dent->d_name);
306 continue;
307 }
308 if (S_ISDIR(st.st_mode)) {
309 zlog_debug("NS parsing init: %s is not a NS",
310 dent->d_name);
311 continue;
312 }
313 if (zebra_ns_notify_is_default_netns(dent->d_name)) {
314 zlog_warn(
315 "NS notify : NS %s is default VRF."
316 " Updating VRF Name", dent->d_name);
317 vrf_set_default_name(dent->d_name, false);
318 continue;
319 }
320 zebra_ns_notify_create_context_from_entry_name(dent->d_name);
321 }
322 closedir(srcdir);
323 }
324
325 void zebra_ns_notify_init(void)
326 {
327 int fd_monitor;
328
329 zebra_netns_notify_current = NULL;
330 fd_monitor = inotify_init();
331 if (fd_monitor < 0) {
332 flog_err_sys(
333 EC_LIB_SYSTEM_CALL,
334 "NS notify init: failed to initialize inotify (%s)",
335 safe_strerror(errno));
336 }
337 if (inotify_add_watch(fd_monitor, NS_RUN_DIR,
338 IN_CREATE | IN_DELETE) < 0) {
339 flog_err_sys(EC_LIB_SYSTEM_CALL,
340 "NS notify watch: failed to add watch (%s)",
341 safe_strerror(errno));
342 }
343 zebra_netns_notify_current = thread_add_read(
344 zebrad.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
345 }
346
347 void zebra_ns_notify_close(void)
348 {
349 if (zebra_netns_notify_current == NULL)
350 return;
351
352 int fd = 0;
353
354 if (zebra_netns_notify_current->u.fd > 0)
355 fd = zebra_netns_notify_current->u.fd;
356
357 if (zebra_netns_notify_current->master != NULL)
358 thread_cancel(zebra_netns_notify_current);
359
360 /* auto-removal of notify items */
361 if (fd > 0)
362 close(fd);
363 }
364
365 #else
366 void zebra_ns_notify_parse(void)
367 {
368 }
369
370 void zebra_ns_notify_init(void)
371 {
372 }
373
374 void zebra_ns_notify_close(void)
375 {
376 }
377 #endif /* !HAVE_NETLINK */