]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_netns_id.c
Merge pull request #7384 from opensourcerouting/nb-dyn-modules
[mirror_frr.git] / zebra / zebra_netns_id.c
CommitLineData
05895ad0
PG
1/* zebra NETNS ID handling routines
2 * those routines are implemented locally to avoid having external dependencies.
3 * Copyright (C) 2018 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#include "ns.h"
ec31f30d 23#include "vrf.h"
05895ad0 24#include "log.h"
43e52561 25#include "lib_errors.h"
05895ad0 26
85a75f1e
MS
27#include "zebra/rib.h"
28#include "zebra/zebra_dplane.h"
05895ad0
PG
29#if defined(HAVE_NETLINK)
30
31#include <linux/net_namespace.h>
32#include <linux/netlink.h>
33#include <linux/rtnetlink.h>
34
05895ad0
PG
35#include "zebra_ns.h"
36#include "kernel_netlink.h"
37#endif /* defined(HAVE_NETLINK) */
38
43e52561
QY
39#include "zebra/zebra_netns_id.h"
40#include "zebra/zebra_errors.h"
05895ad0
PG
41
42/* in case NEWNSID not available, the NSID will be locally obtained
43 */
44#define NS_BASE_NSID 0
45
46#if defined(HAVE_NETLINK)
47
48#define NETLINK_SOCKET_BUFFER_SIZE 512
49#define NETLINK_ALIGNTO 4
996c9314
LB
50#define NETLINK_ALIGN(len) \
51 (((len) + NETLINK_ALIGNTO - 1) & ~(NETLINK_ALIGNTO - 1))
05895ad0
PG
52#define NETLINK_NLATTR_LEN(_a, _b) (unsigned int)((char *)_a - (char *)_b)
53
54#endif /* defined(HAVE_NETLINK) */
55
56static ns_id_t zebra_ns_id_get_fallback(const char *netnspath)
57{
58 static int zebra_ns_id_local;
59
60 return zebra_ns_id_local++;
61}
62
63#if defined(HAVE_NETLINK)
64
65static struct nlmsghdr *initiate_nlh(char *buf, unsigned int *seq, int type)
66{
67 struct nlmsghdr *nlh;
68
69 nlh = (struct nlmsghdr *)buf;
70 nlh->nlmsg_len = NETLINK_ALIGN(sizeof(struct nlmsghdr));
71
72 nlh->nlmsg_type = type;
73 nlh->nlmsg_flags = NLM_F_REQUEST;
74 if (type == RTM_NEWNSID)
75 nlh->nlmsg_flags |= NLM_F_ACK;
76 nlh->nlmsg_seq = *seq = time(NULL);
77 return nlh;
78}
79
996c9314
LB
80static int send_receive(int sock, struct nlmsghdr *nlh, unsigned int seq,
81 char *buf)
05895ad0
PG
82{
83 int ret;
996c9314 84 static const struct sockaddr_nl snl = {.nl_family = AF_NETLINK};
05895ad0
PG
85
86 ret = sendto(sock, (const void *)nlh, (size_t)nlh->nlmsg_len, 0,
996c9314 87 (struct sockaddr *)&snl, (socklen_t)sizeof(snl));
05895ad0 88 if (ret < 0) {
450971aa 89 flog_err_sys(EC_LIB_SOCKET, "netlink( %u) sendmsg() error: %s",
09c866e3 90 sock, safe_strerror(errno));
05895ad0
PG
91 return -1;
92 }
93
94 /* reception */
95 struct sockaddr_nl addr;
96 struct iovec iov = {
996c9314 97 .iov_base = buf, .iov_len = NETLINK_SOCKET_BUFFER_SIZE,
05895ad0
PG
98 };
99 struct msghdr msg = {
996c9314
LB
100 .msg_name = &addr,
101 .msg_namelen = sizeof(struct sockaddr_nl),
102 .msg_iov = &iov,
103 .msg_iovlen = 1,
104 .msg_control = NULL,
05895ad0 105 .msg_controllen = 0,
996c9314 106 .msg_flags = 0,
05895ad0
PG
107 };
108 ret = recvmsg(sock, &msg, 0);
109 if (ret < 0) {
450971aa 110 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
111 "netlink recvmsg: error %d (errno %u)", ret,
112 errno);
05895ad0
PG
113 return -1;
114 }
115 if (msg.msg_flags & MSG_TRUNC) {
e914ccbe 116 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR,
1c50c1c0 117 "netlink recvmsg : error message truncated");
05895ad0
PG
118 return -1;
119 }
120 /* nlh already points to buf */
121 if (nlh->nlmsg_seq != seq) {
af4c2728 122 flog_err(
e914ccbe 123 EC_ZEBRA_NETLINK_BAD_SEQUENCE,
996c9314
LB
124 "netlink recvmsg: bad sequence number %x (expected %x)",
125 seq, nlh->nlmsg_seq);
05895ad0
PG
126 return -1;
127 }
128 return ret;
129}
130
131/* extract on a valid nlmsg the nsid
132 * valid nlmsghdr - not a nlmsgerr
133 */
134static ns_id_t extract_nsid(struct nlmsghdr *nlh, char *buf)
135{
136 ns_id_t ns_id = NS_UNKNOWN;
996c9314
LB
137 int offset = NETLINK_ALIGN(sizeof(struct nlmsghdr))
138 + NETLINK_ALIGN(sizeof(struct rtgenmsg));
05895ad0
PG
139 int curr_length = offset;
140 void *tail = (void *)((char *)nlh + NETLINK_ALIGN(nlh->nlmsg_len));
141 struct nlattr *attr;
142
c4efd0f4 143 for (attr = (struct nlattr *)(buf + offset);
996c9314
LB
144 NETLINK_NLATTR_LEN(tail, attr) >= sizeof(struct nlattr)
145 && attr->nla_len >= sizeof(struct nlattr)
146 && attr->nla_len <= NETLINK_NLATTR_LEN(tail, attr);
05895ad0
PG
147 attr += NETLINK_ALIGN(attr->nla_len)) {
148 curr_length += attr->nla_len;
149 if ((attr->nla_type & NLA_TYPE_MASK) == NETNSA_NSID) {
d7c0a89a 150 uint32_t *ptr = (uint32_t *)(attr);
05895ad0
PG
151
152 ns_id = ptr[1];
153 break;
154 }
155 }
156 return ns_id;
157}
158
289b0f0d
PG
159/* fd_param = -1 is ignored.
160 * netnspath set to null is ignored.
161 * one of the 2 params is mandatory. netnspath is looked in priority
162 */
163ns_id_t zebra_ns_id_get(const char *netnspath, int fd_param)
05895ad0
PG
164{
165 int ns_id = -1;
166 struct sockaddr_nl snl;
289b0f0d 167 int fd = -1, sock, ret;
05895ad0
PG
168 unsigned int seq;
169 ns_id_t return_nsid = NS_UNKNOWN;
170
171 /* netns path check */
289b0f0d 172 if (!netnspath && fd_param == -1)
bd23c840 173 return NS_UNKNOWN;
289b0f0d
PG
174 if (netnspath) {
175 fd = open(netnspath, O_RDONLY);
176 if (fd == -1)
177 return NS_UNKNOWN;
178 } else if (fd_param != -1)
179 fd = fd_param;
05895ad0
PG
180 /* netlink socket */
181 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
182 if (sock < 0) {
450971aa 183 flog_err_sys(EC_LIB_SOCKET, "netlink( %u) socket() error: %s",
09c866e3 184 sock, safe_strerror(errno));
7529bf8f 185 if (netnspath)
289b0f0d 186 close(fd);
05895ad0
PG
187 return NS_UNKNOWN;
188 }
189 memset(&snl, 0, sizeof(snl));
190 snl.nl_family = AF_NETLINK;
191 snl.nl_groups = RTNLGRP_NSID;
192 snl.nl_pid = 0; /* AUTO PID */
193 ret = bind(sock, (struct sockaddr *)&snl, sizeof(snl));
194 if (ret < 0) {
450971aa 195 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
196 "netlink( %u) socket() bind error: %s", sock,
197 safe_strerror(errno));
05895ad0 198 close(sock);
7529bf8f 199 if (netnspath)
289b0f0d 200 close(fd);
05895ad0
PG
201 return NS_UNKNOWN;
202 }
203
204 /* message to send to netlink,and response : NEWNSID */
205 char buf[NETLINK_SOCKET_BUFFER_SIZE];
206 struct nlmsghdr *nlh;
207 struct rtgenmsg *rt;
208 int len;
209
210 memset(buf, 0, NETLINK_SOCKET_BUFFER_SIZE);
211 nlh = initiate_nlh(buf, &seq, RTM_NEWNSID);
212 rt = (struct rtgenmsg *)(buf + nlh->nlmsg_len);
213 nlh->nlmsg_len += NETLINK_ALIGN(sizeof(struct rtgenmsg));
214 rt->rtgen_family = AF_UNSPEC;
215
312a6bee
JU
216 nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_FD, fd);
217 nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_NSID, ns_id);
05895ad0
PG
218
219 ret = send_receive(sock, nlh, seq, buf);
220 if (ret < 0) {
221 close(sock);
7529bf8f 222 if (netnspath)
289b0f0d 223 close(fd);
05895ad0
PG
224 return NS_UNKNOWN;
225 }
226 nlh = (struct nlmsghdr *)buf;
227
228 /* message to analyse : NEWNSID response */
05895ad0 229 ret = 0;
b5891e15
A
230 if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
231 return_nsid = extract_nsid(nlh, buf);
232 } else {
233 if (nlh->nlmsg_type == NLMSG_ERROR) {
234 struct nlmsgerr *err =
235 (struct nlmsgerr
236 *)((char *)nlh
237 + NETLINK_ALIGN(
238 sizeof(struct nlmsghdr)));
239
240 ret = -1;
241 if (err->error < 0)
242 errno = -err->error;
243 else
244 errno = err->error;
245 if (errno == 0) {
246 /* request NEWNSID was successfull
247 * return EEXIST error to get GETNSID
05895ad0 248 */
05895ad0 249 errno = EEXIST;
05895ad0 250 }
b5891e15
A
251 } else {
252 /* other errors ignored
253 * attempt to get nsid
254 */
255 ret = -1;
256 errno = EEXIST;
05895ad0 257 }
b5891e15 258 }
05895ad0
PG
259
260 if (ret <= 0) {
261 if (errno != EEXIST && ret != 0) {
af4c2728 262 flog_err(
450971aa 263 EC_LIB_SOCKET,
996c9314
LB
264 "netlink( %u) recvfrom() error 2 when reading: %s",
265 fd, safe_strerror(errno));
05895ad0 266 close(sock);
7529bf8f 267 if (netnspath)
289b0f0d 268 close(fd);
05895ad0 269 if (errno == ENOTSUP) {
9df414fe 270 zlog_debug("NEWNSID locally generated");
05895ad0
PG
271 return zebra_ns_id_get_fallback(netnspath);
272 }
273 return NS_UNKNOWN;
274 }
275 /* message to send to netlink : GETNSID */
276 memset(buf, 0, NETLINK_SOCKET_BUFFER_SIZE);
277 nlh = initiate_nlh(buf, &seq, RTM_GETNSID);
278 rt = (struct rtgenmsg *)(buf + nlh->nlmsg_len);
279 nlh->nlmsg_len += NETLINK_ALIGN(sizeof(struct rtgenmsg));
280 rt->rtgen_family = AF_UNSPEC;
281
312a6bee
JU
282 nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_FD, fd);
283 nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_NSID,
284 ns_id);
05895ad0
PG
285
286 ret = send_receive(sock, nlh, seq, buf);
287 if (ret < 0) {
288 close(sock);
7529bf8f 289 if (netnspath)
289b0f0d 290 close(fd);
05895ad0
PG
291 return NS_UNKNOWN;
292 }
293 nlh = (struct nlmsghdr *)buf;
294 len = ret;
295 ret = 0;
296 do {
297 if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
298 return_nsid = extract_nsid(nlh, buf);
299 if (return_nsid != NS_UNKNOWN)
300 break;
301 } else if (nlh->nlmsg_type == NLMSG_ERROR) {
996c9314
LB
302 struct nlmsgerr *err =
303 (struct nlmsgerr
304 *)((char *)nlh
305 + NETLINK_ALIGN(sizeof(
306 struct
307 nlmsghdr)));
05895ad0
PG
308 if (err->error < 0)
309 errno = -err->error;
310 else
311 errno = err->error;
312 break;
313 }
314 len = len - NETLINK_ALIGN(nlh->nlmsg_len);
996c9314
LB
315 nlh = (struct nlmsghdr *)((char *)nlh
316 + NETLINK_ALIGN(
317 nlh->nlmsg_len));
0cfbff74 318 } while (len != 0 && ret == 0);
05895ad0
PG
319 }
320
7529bf8f 321 if (netnspath)
289b0f0d 322 close(fd);
05895ad0
PG
323 close(sock);
324 return return_nsid;
325}
326
327#else
289b0f0d 328ns_id_t zebra_ns_id_get(const char *netnspath, int fd __attribute__ ((unused)))
05895ad0
PG
329{
330 return zebra_ns_id_get_fallback(netnspath);
331}
2d4e4d39 332
05895ad0 333#endif /* ! defined(HAVE_NETLINK) */
ec31f30d
PG
334
335#ifdef HAVE_NETNS
336static void zebra_ns_create_netns_directory(void)
337{
338 /* check that /var/run/netns is created */
339 /* S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH */
340 if (mkdir(NS_RUN_DIR, 0755)) {
341 if (errno != EEXIST) {
e914ccbe 342 flog_warn(EC_ZEBRA_NAMESPACE_DIR_INACCESSIBLE,
9df414fe 343 "NS check: failed to access %s", NS_RUN_DIR);
ec31f30d
PG
344 return;
345 }
346 }
347}
348#endif
349
350ns_id_t zebra_ns_id_get_default(void)
351{
352#ifdef HAVE_NETNS
353 int fd;
354#endif /* !HAVE_NETNS */
355
356#ifdef HAVE_NETNS
357 if (vrf_is_backend_netns())
358 zebra_ns_create_netns_directory();
359 fd = open(NS_DEFAULT_NAME, O_RDONLY);
360
361 if (fd == -1)
1eb92f06 362 return NS_DEFAULT;
4307629f
DS
363 if (!vrf_is_backend_netns()) {
364 close(fd);
1eb92f06 365 return NS_DEFAULT;
4307629f 366 }
ec31f30d 367 close(fd);
289b0f0d 368 return zebra_ns_id_get((char *)NS_DEFAULT_NAME, -1);
996c9314 369#else /* HAVE_NETNS */
1eb92f06 370 return NS_DEFAULT;
ec31f30d
PG
371#endif /* !HAVE_NETNS */
372}