]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_netns_notify.c
*: conform with COMMUNITY.md formatting rules, via 'make indent'
[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
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
54 DEFINE_MTYPE_STATIC(ZEBRA, NETNS_MISC, "ZebraNetNSInfo")
55 static struct thread *zebra_netns_notify_current;
56
57 struct zebra_netns_info {
58 const char *netnspath;
59 unsigned int retries;
60 };
61
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,
65 int stop_retry);
66 static int zebra_ns_notify_read(struct thread *t);
67
68 static 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;
73 ns_id_t ns_id;
74
75 if (netnspath == NULL)
76 return;
77
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);
85 if (vrf) {
86 zlog_warn(
87 "NS notify : same NSID used by VRF %s. Ignore NS %s creation",
88 vrf->name, netnspath);
89 return;
90 }
91 if (vrf_handler_create(NULL, name, &vrf) != CMD_SUCCESS) {
92 zlog_warn("NS notify : failed to create VRF %s", name);
93 return;
94 }
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);
98 return;
99 }
100 zlog_info("NS notify : created VRF %s NS %s", name, netnspath);
101 }
102
103 static int zebra_ns_continue_read(struct zebra_netns_info *zns_info,
104 int stop_retry)
105 {
106 void *ns_path_ptr = (void *)zns_info->netnspath;
107
108 if (stop_retry) {
109 XFREE(MTYPE_NETNS_MISC, ns_path_ptr);
110 XFREE(MTYPE_NETNS_MISC, zns_info);
111 return 0;
112 }
113 thread_add_timer_msec(zebrad.master, zebra_ns_ready_read,
114 (void *)zns_info, ZEBRA_NS_POLLING_INTERVAL_MSEC,
115 NULL);
116 return 0;
117 }
118
119 static int zebra_ns_ready_read(struct thread *t)
120 {
121 struct zebra_netns_info *zns_info = THREAD_ARG(t);
122 const char *netnspath;
123 int err, stop_retry = 0;
124
125 if (!zns_info)
126 return 0;
127 if (!zns_info->netnspath) {
128 XFREE(MTYPE_NETNS_MISC, zns_info);
129 return 0;
130 }
131 netnspath = zns_info->netnspath;
132 if (--zns_info->retries == 0)
133 stop_retry = 1;
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");
139 if (err < 0)
140 return zebra_ns_continue_read(zns_info, stop_retry);
141
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");
148 if (err < 0)
149 return zebra_ns_continue_read(zns_info, stop_retry);
150
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);
154 }
155
156 static int zebra_ns_notify_read(struct thread *t)
157 {
158 int fd_monitor = THREAD_FD(t);
159 struct inotify_event *event;
160 char buf[BUFSIZ];
161 ssize_t len;
162
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));
166 if (len < 0) {
167 zlog_warn("NS notify read: failed to read (%s)",
168 safe_strerror(errno));
169 return 0;
170 }
171 for (event = (struct inotify_event *)buf; (char *)event < &buf[len];
172 event = (struct inotify_event *)((char *)event + sizeof(*event)
173 + event->len)) {
174 char *netnspath;
175 struct zebra_netns_info *netnsinfo;
176
177 if (!(event->mask & IN_CREATE))
178 continue;
179 netnspath = ns_netns_pathname(NULL, event->name);
180 if (!netnspath)
181 continue;
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);
189 }
190 return 0;
191 }
192
193 void zebra_ns_notify_parse(void)
194 {
195 struct dirent *dent;
196 DIR *srcdir = opendir(NS_RUN_DIR);
197
198 if (srcdir == NULL) {
199 zlog_warn("NS parsing init: failed to parse %s", NS_RUN_DIR);
200 return;
201 }
202 while ((dent = readdir(srcdir)) != NULL) {
203 struct stat st;
204
205 if (strcmp(dent->d_name, ".") == 0
206 || strcmp(dent->d_name, "..") == 0)
207 continue;
208 if (fstatat(dirfd(srcdir), dent->d_name, &st, 0) < 0) {
209 zlog_warn("NS parsing init: failed to parse entry %s",
210 dent->d_name);
211 continue;
212 }
213 if (S_ISDIR(st.st_mode)) {
214 zlog_warn("NS parsing init: %s is not a NS",
215 dent->d_name);
216 continue;
217 }
218 zebra_ns_notify_create_context_from_entry_name(dent->d_name);
219 }
220 closedir(srcdir);
221 }
222
223 void zebra_ns_notify_init(void)
224 {
225 int fd_monitor;
226
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));
232 }
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));
236 }
237 zebra_netns_notify_current = thread_add_read(
238 zebrad.master, zebra_ns_notify_read, NULL, fd_monitor, NULL);
239 }
240
241 void zebra_ns_notify_close(void)
242 {
243 if (zebra_netns_notify_current == NULL)
244 return;
245
246 int fd = 0;
247
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 */
252 if (fd > 0)
253 close(fd);
254 }
255
256 #else
257 void zebra_ns_notify_parse(void)
258 {
259 }
260
261 void zebra_ns_notify_init(void)
262 {
263 }
264
265 void zebra_ns_notify_close(void)
266 {
267 }
268 #endif /* !HAVE_NETLINK */