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
26 #if defined(HAVE_NETLINK)
28 #include <linux/net_namespace.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
34 #include "kernel_netlink.h"
35 #endif /* defined(HAVE_NETLINK) */
37 #include "zebra_netns_id.h"
39 /* default NS ID value used when VRF backend is not NETNS */
40 #define NS_DEFAULT_INTERNAL 0
42 /* in case NEWNSID not available, the NSID will be locally obtained
44 #define NS_BASE_NSID 0
46 #if defined(HAVE_NETLINK)
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)
54 #endif /* defined(HAVE_NETLINK) */
56 static ns_id_t
zebra_ns_id_get_fallback(const char *netnspath
)
58 static int zebra_ns_id_local
;
60 return zebra_ns_id_local
++;
63 #if defined(HAVE_NETLINK)
65 static struct nlmsghdr
*initiate_nlh(char *buf
, unsigned int *seq
, int type
)
69 nlh
= (struct nlmsghdr
*)buf
;
70 nlh
->nlmsg_len
= NETLINK_ALIGN(sizeof(struct nlmsghdr
));
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
);
80 static int send_receive(int sock
, struct nlmsghdr
*nlh
, unsigned int seq
,
84 static const struct sockaddr_nl snl
= {.nl_family
= AF_NETLINK
};
86 ret
= sendto(sock
, (const void *)nlh
, (size_t)nlh
->nlmsg_len
, 0,
87 (struct sockaddr
*)&snl
, (socklen_t
)sizeof(snl
));
89 zlog_err("netlink( %u) sendmsg() error: %s", sock
,
90 safe_strerror(errno
));
95 struct sockaddr_nl addr
;
97 .iov_base
= buf
, .iov_len
= NETLINK_SOCKET_BUFFER_SIZE
,
101 .msg_namelen
= sizeof(struct sockaddr_nl
),
108 ret
= recvmsg(sock
, &msg
, 0);
110 zlog_err("netlink recvmsg: error %d (errno %u)", ret
, errno
);
113 if (msg
.msg_flags
& MSG_TRUNC
) {
114 zlog_err("netlink recvmsg : error message truncated");
117 /* nlh already points to buf */
118 if (nlh
->nlmsg_seq
!= seq
) {
120 "netlink recvmsg: bad sequence number %x (expected %x)",
121 seq
, nlh
->nlmsg_seq
);
127 /* extract on a valid nlmsg the nsid
128 * valid nlmsghdr - not a nlmsgerr
130 static ns_id_t
extract_nsid(struct nlmsghdr
*nlh
, char *buf
)
132 ns_id_t ns_id
= NS_UNKNOWN
;
133 int offset
= NETLINK_ALIGN(sizeof(struct nlmsghdr
))
134 + NETLINK_ALIGN(sizeof(struct rtgenmsg
));
135 int curr_length
= offset
;
136 void *tail
= (void *)((char *)nlh
+ NETLINK_ALIGN(nlh
->nlmsg_len
));
139 for (attr
= (struct nlattr
*)((char *)buf
+ offset
);
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
);
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
);
155 ns_id_t
zebra_ns_id_get(const char *netnspath
)
158 struct sockaddr_nl snl
;
161 ns_id_t return_nsid
= NS_UNKNOWN
;
163 /* netns path check */
166 fd
= open(netnspath
, O_RDONLY
);
171 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
173 zlog_err("netlink( %u) socket() error: %s", sock
,
174 safe_strerror(errno
));
177 memset(&snl
, 0, sizeof(snl
));
178 snl
.nl_family
= AF_NETLINK
;
179 snl
.nl_groups
= RTNLGRP_NSID
;
180 snl
.nl_pid
= 0; /* AUTO PID */
181 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
183 zlog_err("netlink( %u) socket() bind error: %s", sock
,
184 safe_strerror(errno
));
190 /* message to send to netlink,and response : NEWNSID */
191 char buf
[NETLINK_SOCKET_BUFFER_SIZE
];
192 struct nlmsghdr
*nlh
;
196 memset(buf
, 0, NETLINK_SOCKET_BUFFER_SIZE
);
197 nlh
= initiate_nlh(buf
, &seq
, RTM_NEWNSID
);
198 rt
= (struct rtgenmsg
*)(buf
+ nlh
->nlmsg_len
);
199 nlh
->nlmsg_len
+= NETLINK_ALIGN(sizeof(struct rtgenmsg
));
200 rt
->rtgen_family
= AF_UNSPEC
;
202 addattr32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_FD
, fd
);
203 addattr32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_NSID
, ns_id
);
205 ret
= send_receive(sock
, nlh
, seq
, buf
);
211 nlh
= (struct nlmsghdr
*)buf
;
213 /* message to analyse : NEWNSID response */
217 if (nlh
->nlmsg_type
>= NLMSG_MIN_TYPE
) {
218 return_nsid
= extract_nsid(nlh
, buf
);
219 if (return_nsid
!= NS_UNKNOWN
)
222 if (nlh
->nlmsg_type
== NLMSG_ERROR
) {
223 struct nlmsgerr
*err
=
226 + NETLINK_ALIGN(sizeof(
236 /* request NEWNSID was successfull
237 * return EEXIST error to get GETNSID
242 /* other errors ignored
243 * attempt to get nsid
250 len
= len
- NETLINK_ALIGN(nlh
->nlmsg_len
);
251 nlh
= (struct nlmsghdr
*)((char *)nlh
252 + NETLINK_ALIGN(nlh
->nlmsg_len
));
253 } while (len
!= 0 && return_nsid
!= NS_UNKNOWN
&& ret
== 0);
256 if (errno
!= EEXIST
&& ret
!= 0) {
258 "netlink( %u) recvfrom() error 2 when reading: %s",
259 fd
, safe_strerror(errno
));
262 if (errno
== ENOTSUP
) {
263 zlog_warn("NEWNSID locally generated");
264 return zebra_ns_id_get_fallback(netnspath
);
268 /* message to send to netlink : GETNSID */
269 memset(buf
, 0, NETLINK_SOCKET_BUFFER_SIZE
);
270 nlh
= initiate_nlh(buf
, &seq
, RTM_GETNSID
);
271 rt
= (struct rtgenmsg
*)(buf
+ nlh
->nlmsg_len
);
272 nlh
->nlmsg_len
+= NETLINK_ALIGN(sizeof(struct rtgenmsg
));
273 rt
->rtgen_family
= AF_UNSPEC
;
275 addattr32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_FD
, fd
);
276 addattr32(nlh
, NETLINK_SOCKET_BUFFER_SIZE
, NETNSA_NSID
, ns_id
);
278 ret
= send_receive(sock
, nlh
, seq
, buf
);
284 nlh
= (struct nlmsghdr
*)buf
;
288 if (nlh
->nlmsg_type
>= NLMSG_MIN_TYPE
) {
289 return_nsid
= extract_nsid(nlh
, buf
);
290 if (return_nsid
!= NS_UNKNOWN
)
292 } else if (nlh
->nlmsg_type
== NLMSG_ERROR
) {
293 struct nlmsgerr
*err
=
296 + NETLINK_ALIGN(sizeof(
305 len
= len
- NETLINK_ALIGN(nlh
->nlmsg_len
);
306 nlh
= (struct nlmsghdr
*)((char *)nlh
309 } while (len
!= 0 && return_nsid
!= NS_UNKNOWN
&& ret
== 0);
318 ns_id_t
zebra_ns_id_get(const char *netnspath
)
320 return zebra_ns_id_get_fallback(netnspath
);
322 #endif /* ! defined(HAVE_NETLINK) */
325 static void zebra_ns_create_netns_directory(void)
327 /* check that /var/run/netns is created */
328 /* S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH */
329 if (mkdir(NS_RUN_DIR
, 0755)) {
330 if (errno
!= EEXIST
) {
331 zlog_warn("NS check: failed to access %s", NS_RUN_DIR
);
338 ns_id_t
zebra_ns_id_get_default(void)
342 #endif /* !HAVE_NETNS */
345 if (vrf_is_backend_netns())
346 zebra_ns_create_netns_directory();
347 fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
350 return NS_DEFAULT_INTERNAL
;
351 if (!vrf_is_backend_netns())
352 return NS_DEFAULT_INTERNAL
;
354 return zebra_ns_id_get((char *)NS_DEFAULT_NAME
);
355 #else /* HAVE_NETNS */
356 return NS_DEFAULT_INTERNAL
;
357 #endif /* !HAVE_NETNS */