1 /* zebra NETNS ID handling routines
2 * those routines are implemented locally to avoid having external dependencies.
3 * Copyright (C) 2018 6WIND
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)
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
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
25 #include "lib_errors.h"
27 #include "zebra/rib.h"
28 #include "zebra/zebra_dplane.h"
29 #if defined(HAVE_NETLINK)
31 #include <linux/net_namespace.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
36 #include "kernel_netlink.h"
37 #endif /* defined(HAVE_NETLINK) */
39 #include "zebra/zebra_netns_id.h"
40 #include "zebra/zebra_errors.h"
42 /* default NS ID value used when VRF backend is not NETNS */
43 #define NS_DEFAULT_INTERNAL 0
45 /* in case NEWNSID not available, the NSID will be locally obtained
47 #define NS_BASE_NSID 0
49 #if defined(HAVE_NETLINK)
51 #define NETLINK_SOCKET_BUFFER_SIZE 512
52 #define NETLINK_ALIGNTO 4
53 #define NETLINK_ALIGN(len) \
54 (((len) + NETLINK_ALIGNTO - 1) & ~(NETLINK_ALIGNTO - 1))
55 #define NETLINK_NLATTR_LEN(_a, _b) (unsigned int)((char *)_a - (char *)_b)
57 #endif /* defined(HAVE_NETLINK) */
59 static ns_id_t
zebra_ns_id_get_fallback(const char *netnspath
)
61 static int zebra_ns_id_local
;
63 return zebra_ns_id_local
++;
66 #if defined(HAVE_NETLINK)
68 static struct nlmsghdr
*initiate_nlh(char *buf
, unsigned int *seq
, int type
)
72 nlh
= (struct nlmsghdr
*)buf
;
73 nlh
->nlmsg_len
= NETLINK_ALIGN(sizeof(struct nlmsghdr
));
75 nlh
->nlmsg_type
= type
;
76 nlh
->nlmsg_flags
= NLM_F_REQUEST
;
77 if (type
== RTM_NEWNSID
)
78 nlh
->nlmsg_flags
|= NLM_F_ACK
;
79 nlh
->nlmsg_seq
= *seq
= time(NULL
);
83 static int send_receive(int sock
, struct nlmsghdr
*nlh
, unsigned int seq
,
87 static const struct sockaddr_nl snl
= {.nl_family
= AF_NETLINK
};
89 ret
= sendto(sock
, (const void *)nlh
, (size_t)nlh
->nlmsg_len
, 0,
90 (struct sockaddr
*)&snl
, (socklen_t
)sizeof(snl
));
92 flog_err_sys(EC_LIB_SOCKET
, "netlink( %u) sendmsg() error: %s",
93 sock
, safe_strerror(errno
));
98 struct sockaddr_nl addr
;
100 .iov_base
= buf
, .iov_len
= NETLINK_SOCKET_BUFFER_SIZE
,
102 struct msghdr msg
= {
104 .msg_namelen
= sizeof(struct sockaddr_nl
),
111 ret
= recvmsg(sock
, &msg
, 0);
113 flog_err_sys(EC_LIB_SOCKET
,
114 "netlink recvmsg: error %d (errno %u)", ret
,
118 if (msg
.msg_flags
& MSG_TRUNC
) {
119 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
120 "netlink recvmsg : error message truncated");
123 /* nlh already points to buf */
124 if (nlh
->nlmsg_seq
!= seq
) {
126 EC_ZEBRA_NETLINK_BAD_SEQUENCE
,
127 "netlink recvmsg: bad sequence number %x (expected %x)",
128 seq
, nlh
->nlmsg_seq
);
134 /* extract on a valid nlmsg the nsid
135 * valid nlmsghdr - not a nlmsgerr
137 static ns_id_t
extract_nsid(struct nlmsghdr
*nlh
, char *buf
)
139 ns_id_t ns_id
= NS_UNKNOWN
;
140 int offset
= NETLINK_ALIGN(sizeof(struct nlmsghdr
))
141 + NETLINK_ALIGN(sizeof(struct rtgenmsg
));
142 int curr_length
= offset
;
143 void *tail
= (void *)((char *)nlh
+ NETLINK_ALIGN(nlh
->nlmsg_len
));
146 for (attr
= (struct nlattr
*)(buf
+ offset
);
147 NETLINK_NLATTR_LEN(tail
, attr
) >= sizeof(struct nlattr
)
148 && attr
->nla_len
>= sizeof(struct nlattr
)
149 && attr
->nla_len
<= NETLINK_NLATTR_LEN(tail
, attr
);
150 attr
+= NETLINK_ALIGN(attr
->nla_len
)) {
151 curr_length
+= attr
->nla_len
;
152 if ((attr
->nla_type
& NLA_TYPE_MASK
) == NETNSA_NSID
) {
153 uint32_t *ptr
= (uint32_t *)(attr
);
162 /* fd_param = -1 is ignored.
163 * netnspath set to null is ignored.
164 * one of the 2 params is mandatory. netnspath is looked in priority
166 ns_id_t
zebra_ns_id_get(const char *netnspath
, int fd_param
)
169 struct sockaddr_nl snl
;
170 int fd
= -1, sock
, ret
;
172 ns_id_t return_nsid
= NS_UNKNOWN
;
174 /* netns path check */
175 if (!netnspath
&& fd_param
== -1)
178 fd
= open(netnspath
, O_RDONLY
);
181 } else if (fd_param
!= -1)
184 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
186 flog_err_sys(EC_LIB_SOCKET
, "netlink( %u) socket() error: %s",
187 sock
, safe_strerror(errno
));
192 memset(&snl
, 0, sizeof(snl
));
193 snl
.nl_family
= AF_NETLINK
;
194 snl
.nl_groups
= RTNLGRP_NSID
;
195 snl
.nl_pid
= 0; /* AUTO PID */
196 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
198 flog_err_sys(EC_LIB_SOCKET
,
199 "netlink( %u) socket() bind error: %s", sock
,
200 safe_strerror(errno
));
207 /* message to send to netlink,and response : NEWNSID */
208 char buf
[NETLINK_SOCKET_BUFFER_SIZE
];
209 struct nlmsghdr
*nlh
;
213 memset(buf
, 0, NETLINK_SOCKET_BUFFER_SIZE
);
214 nlh
= initiate_nlh(buf
, &seq
, RTM_NEWNSID
);
215 rt
= (struct rtgenmsg
*)(buf
+ nlh
->nlmsg_len
);
216 nlh
->nlmsg_len
+= NETLINK_ALIGN(sizeof(struct rtgenmsg
));
217 rt
->rtgen_family
= AF_UNSPEC
;
219 nl_attr_put32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_FD
, fd
);
220 nl_attr_put32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_NSID
, ns_id
);
222 ret
= send_receive(sock
, nlh
, seq
, buf
);
229 nlh
= (struct nlmsghdr
*)buf
;
231 /* message to analyse : NEWNSID response */
233 if (nlh
->nlmsg_type
>= NLMSG_MIN_TYPE
) {
234 return_nsid
= extract_nsid(nlh
, buf
);
236 if (nlh
->nlmsg_type
== NLMSG_ERROR
) {
237 struct nlmsgerr
*err
=
241 sizeof(struct nlmsghdr
)));
249 /* request NEWNSID was successfull
250 * return EEXIST error to get GETNSID
255 /* other errors ignored
256 * attempt to get nsid
264 if (errno
!= EEXIST
&& ret
!= 0) {
267 "netlink( %u) recvfrom() error 2 when reading: %s",
268 fd
, safe_strerror(errno
));
272 if (errno
== ENOTSUP
) {
273 zlog_debug("NEWNSID locally generated");
274 return zebra_ns_id_get_fallback(netnspath
);
278 /* message to send to netlink : GETNSID */
279 memset(buf
, 0, NETLINK_SOCKET_BUFFER_SIZE
);
280 nlh
= initiate_nlh(buf
, &seq
, RTM_GETNSID
);
281 rt
= (struct rtgenmsg
*)(buf
+ nlh
->nlmsg_len
);
282 nlh
->nlmsg_len
+= NETLINK_ALIGN(sizeof(struct rtgenmsg
));
283 rt
->rtgen_family
= AF_UNSPEC
;
285 nl_attr_put32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_FD
, fd
);
286 nl_attr_put32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_NSID
,
289 ret
= send_receive(sock
, nlh
, seq
, buf
);
296 nlh
= (struct nlmsghdr
*)buf
;
300 if (nlh
->nlmsg_type
>= NLMSG_MIN_TYPE
) {
301 return_nsid
= extract_nsid(nlh
, buf
);
302 if (return_nsid
!= NS_UNKNOWN
)
304 } else if (nlh
->nlmsg_type
== NLMSG_ERROR
) {
305 struct nlmsgerr
*err
=
308 + NETLINK_ALIGN(sizeof(
317 len
= len
- NETLINK_ALIGN(nlh
->nlmsg_len
);
318 nlh
= (struct nlmsghdr
*)((char *)nlh
321 } while (len
!= 0 && ret
== 0);
331 ns_id_t
zebra_ns_id_get(const char *netnspath
, int fd
__attribute__ ((unused
)))
333 return zebra_ns_id_get_fallback(netnspath
);
336 #endif /* ! defined(HAVE_NETLINK) */
339 static void zebra_ns_create_netns_directory(void)
341 /* check that /var/run/netns is created */
342 /* S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH */
343 if (mkdir(NS_RUN_DIR
, 0755)) {
344 if (errno
!= EEXIST
) {
345 flog_warn(EC_ZEBRA_NAMESPACE_DIR_INACCESSIBLE
,
346 "NS check: failed to access %s", NS_RUN_DIR
);
353 ns_id_t
zebra_ns_id_get_default(void)
357 #endif /* !HAVE_NETNS */
360 if (vrf_is_backend_netns())
361 zebra_ns_create_netns_directory();
362 fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
365 return NS_DEFAULT_INTERNAL
;
366 if (!vrf_is_backend_netns()) {
368 return NS_DEFAULT_INTERNAL
;
371 return zebra_ns_id_get((char *)NS_DEFAULT_NAME
, -1);
372 #else /* HAVE_NETNS */
373 return NS_DEFAULT_INTERNAL
;
374 #endif /* !HAVE_NETNS */