]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_netns_id.c
tools, doc: update checkpatch for u_int_*
[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
PG
24#include "log.h"
25
26#if defined(HAVE_NETLINK)
27
28#include <linux/net_namespace.h>
29#include <linux/netlink.h>
30#include <linux/rtnetlink.h>
31
32#include "rib.h"
33#include "zebra_ns.h"
34#include "kernel_netlink.h"
35#endif /* defined(HAVE_NETLINK) */
36
37#include "zebra_netns_id.h"
38
ec31f30d
PG
39/* default NS ID value used when VRF backend is not NETNS */
40#define NS_DEFAULT_INTERNAL 0
41
05895ad0
PG
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) {
996c9314
LB
89 zlog_err("netlink( %u) sendmsg() error: %s", sock,
90 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) {
110 zlog_err("netlink recvmsg: error %d (errno %u)", ret, errno);
111 return -1;
112 }
113 if (msg.msg_flags & MSG_TRUNC) {
114 zlog_err("netlink recvmsg : error message truncated");
115 return -1;
116 }
117 /* nlh already points to buf */
118 if (nlh->nlmsg_seq != seq) {
996c9314
LB
119 zlog_err(
120 "netlink recvmsg: bad sequence number %x (expected %x)",
121 seq, nlh->nlmsg_seq);
05895ad0
PG
122 return -1;
123 }
124 return ret;
125}
126
127/* extract on a valid nlmsg the nsid
128 * valid nlmsghdr - not a nlmsgerr
129 */
130static ns_id_t extract_nsid(struct nlmsghdr *nlh, char *buf)
131{
132 ns_id_t ns_id = NS_UNKNOWN;
996c9314
LB
133 int offset = NETLINK_ALIGN(sizeof(struct nlmsghdr))
134 + NETLINK_ALIGN(sizeof(struct rtgenmsg));
05895ad0
PG
135 int curr_length = offset;
136 void *tail = (void *)((char *)nlh + NETLINK_ALIGN(nlh->nlmsg_len));
137 struct nlattr *attr;
138
139 for (attr = (struct nlattr *)((char *)buf + offset);
996c9314
LB
140 NETLINK_NLATTR_LEN(tail, attr) >= sizeof(struct nlattr)
141 && attr->nla_len >= sizeof(struct nlattr)
142 && attr->nla_len <= NETLINK_NLATTR_LEN(tail, attr);
05895ad0
PG
143 attr += NETLINK_ALIGN(attr->nla_len)) {
144 curr_length += attr->nla_len;
145 if ((attr->nla_type & NLA_TYPE_MASK) == NETNSA_NSID) {
146 u_int32_t *ptr = (u_int32_t *)(attr);
147
148 ns_id = ptr[1];
149 break;
150 }
151 }
152 return ns_id;
153}
154
155ns_id_t zebra_ns_id_get(const char *netnspath)
156{
157 int ns_id = -1;
158 struct sockaddr_nl snl;
159 int fd, sock, ret;
160 unsigned int seq;
161 ns_id_t return_nsid = NS_UNKNOWN;
162
163 /* netns path check */
164 if (!netnspath)
165 return NS_UNKNOWN;
166 fd = open(netnspath, O_RDONLY);
167 if (fd == -1)
168 return NS_UNKNOWN;
169
170 /* netlink socket */
171 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
172 if (sock < 0) {
996c9314
LB
173 zlog_err("netlink( %u) socket() error: %s", sock,
174 safe_strerror(errno));
af734bc7 175 close(fd);
05895ad0
PG
176 return NS_UNKNOWN;
177 }
178 memset(&snl, 0, sizeof(snl));
179 snl.nl_family = AF_NETLINK;
180 snl.nl_groups = RTNLGRP_NSID;
181 snl.nl_pid = 0; /* AUTO PID */
182 ret = bind(sock, (struct sockaddr *)&snl, sizeof(snl));
183 if (ret < 0) {
996c9314
LB
184 zlog_err("netlink( %u) socket() bind error: %s", sock,
185 safe_strerror(errno));
05895ad0
PG
186 close(sock);
187 close(fd);
188 return NS_UNKNOWN;
189 }
190
191 /* message to send to netlink,and response : NEWNSID */
192 char buf[NETLINK_SOCKET_BUFFER_SIZE];
193 struct nlmsghdr *nlh;
194 struct rtgenmsg *rt;
195 int len;
196
197 memset(buf, 0, NETLINK_SOCKET_BUFFER_SIZE);
198 nlh = initiate_nlh(buf, &seq, RTM_NEWNSID);
199 rt = (struct rtgenmsg *)(buf + nlh->nlmsg_len);
200 nlh->nlmsg_len += NETLINK_ALIGN(sizeof(struct rtgenmsg));
201 rt->rtgen_family = AF_UNSPEC;
202
203 addattr32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_FD, fd);
204 addattr32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_NSID, ns_id);
205
206 ret = send_receive(sock, nlh, seq, buf);
207 if (ret < 0) {
208 close(sock);
209 close(fd);
210 return NS_UNKNOWN;
211 }
212 nlh = (struct nlmsghdr *)buf;
213
214 /* message to analyse : NEWNSID response */
215 len = ret;
216 ret = 0;
217 do {
218 if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
219 return_nsid = extract_nsid(nlh, buf);
220 if (return_nsid != NS_UNKNOWN)
221 break;
222 } else {
223 if (nlh->nlmsg_type == NLMSG_ERROR) {
996c9314
LB
224 struct nlmsgerr *err =
225 (struct nlmsgerr
226 *)((char *)nlh
227 + NETLINK_ALIGN(sizeof(
228 struct
229 nlmsghdr)));
05895ad0
PG
230
231 ret = -1;
232 if (err->error < 0)
233 errno = -err->error;
234 else
235 errno = err->error;
236 if (errno == 0) {
237 /* request NEWNSID was successfull
238 * return EEXIST error to get GETNSID
239 */
240 errno = EEXIST;
241 }
242 } else {
243 /* other errors ignored
244 * attempt to get nsid
245 */
246 ret = -1;
247 errno = EEXIST;
248 break;
249 }
250 }
251 len = len - NETLINK_ALIGN(nlh->nlmsg_len);
996c9314
LB
252 nlh = (struct nlmsghdr *)((char *)nlh
253 + NETLINK_ALIGN(nlh->nlmsg_len));
05895ad0
PG
254 } while (len != 0 && return_nsid != NS_UNKNOWN && ret == 0);
255
256 if (ret <= 0) {
257 if (errno != EEXIST && ret != 0) {
996c9314
LB
258 zlog_err(
259 "netlink( %u) recvfrom() error 2 when reading: %s",
260 fd, safe_strerror(errno));
05895ad0
PG
261 close(sock);
262 close(fd);
263 if (errno == ENOTSUP) {
264 zlog_warn("NEWNSID locally generated");
265 return zebra_ns_id_get_fallback(netnspath);
266 }
267 return NS_UNKNOWN;
268 }
269 /* message to send to netlink : GETNSID */
270 memset(buf, 0, NETLINK_SOCKET_BUFFER_SIZE);
271 nlh = initiate_nlh(buf, &seq, RTM_GETNSID);
272 rt = (struct rtgenmsg *)(buf + nlh->nlmsg_len);
273 nlh->nlmsg_len += NETLINK_ALIGN(sizeof(struct rtgenmsg));
274 rt->rtgen_family = AF_UNSPEC;
275
276 addattr32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_FD, fd);
277 addattr32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_NSID, ns_id);
278
279 ret = send_receive(sock, nlh, seq, buf);
280 if (ret < 0) {
281 close(sock);
282 close(fd);
283 return NS_UNKNOWN;
284 }
285 nlh = (struct nlmsghdr *)buf;
286 len = ret;
287 ret = 0;
288 do {
289 if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
290 return_nsid = extract_nsid(nlh, buf);
291 if (return_nsid != NS_UNKNOWN)
292 break;
293 } else if (nlh->nlmsg_type == NLMSG_ERROR) {
996c9314
LB
294 struct nlmsgerr *err =
295 (struct nlmsgerr
296 *)((char *)nlh
297 + NETLINK_ALIGN(sizeof(
298 struct
299 nlmsghdr)));
05895ad0
PG
300 if (err->error < 0)
301 errno = -err->error;
302 else
303 errno = err->error;
304 break;
305 }
306 len = len - NETLINK_ALIGN(nlh->nlmsg_len);
996c9314
LB
307 nlh = (struct nlmsghdr *)((char *)nlh
308 + NETLINK_ALIGN(
309 nlh->nlmsg_len));
05895ad0
PG
310 } while (len != 0 && return_nsid != NS_UNKNOWN && ret == 0);
311 }
312
313 close(fd);
314 close(sock);
315 return return_nsid;
316}
317
318#else
319ns_id_t zebra_ns_id_get(const char *netnspath)
320{
321 return zebra_ns_id_get_fallback(netnspath);
322}
323#endif /* ! defined(HAVE_NETLINK) */
ec31f30d
PG
324
325#ifdef HAVE_NETNS
326static void zebra_ns_create_netns_directory(void)
327{
328 /* check that /var/run/netns is created */
329 /* S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH */
330 if (mkdir(NS_RUN_DIR, 0755)) {
331 if (errno != EEXIST) {
332 zlog_warn("NS check: failed to access %s", NS_RUN_DIR);
333 return;
334 }
335 }
336}
337#endif
338
339ns_id_t zebra_ns_id_get_default(void)
340{
341#ifdef HAVE_NETNS
342 int fd;
343#endif /* !HAVE_NETNS */
344
345#ifdef HAVE_NETNS
346 if (vrf_is_backend_netns())
347 zebra_ns_create_netns_directory();
348 fd = open(NS_DEFAULT_NAME, O_RDONLY);
349
350 if (fd == -1)
351 return NS_DEFAULT_INTERNAL;
4307629f
DS
352 if (!vrf_is_backend_netns()) {
353 close(fd);
ec31f30d 354 return NS_DEFAULT_INTERNAL;
4307629f 355 }
ec31f30d
PG
356 close(fd);
357 return zebra_ns_id_get((char *)NS_DEFAULT_NAME);
996c9314 358#else /* HAVE_NETNS */
ec31f30d
PG
359 return NS_DEFAULT_INTERNAL;
360#endif /* !HAVE_NETNS */
361}