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