]>
git.proxmox.com Git - mirror_qemu.git/blob - qemu-bridge-helper.c
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Richa Marwaha <rmarwah@linux.vnet.ibm.com>
9 * Corey Bryant <coreyb@linux.vnet.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
17 * - There is no manual page
18 * - The syntax of the ACL file is not documented anywhere
19 * - parse_acl_file() doesn't report fopen() failure properly, fails
20 * to check ferror() after fgets() failure, arbitrarily truncates
21 * long lines, handles whitespace inconsistently, error messages
22 * don't point to the offending file and line, errors in included
23 * files are reported, but otherwise ignored, ...
26 #include "qemu/osdep.h"
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
32 #include <sys/prctl.h>
36 #include <linux/sockios.h>
39 #include <linux/if_bridge.h>
42 #include "qemu/queue.h"
43 #include "qemu/cutils.h"
45 #include "net/tap-linux.h"
47 #ifdef CONFIG_LIBCAP_NG
51 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
60 typedef struct ACLRule
{
63 QSIMPLEQ_ENTRY(ACLRule
) entry
;
66 typedef QSIMPLEQ_HEAD(ACLList
, ACLRule
) ACLList
;
68 static void usage(void)
71 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
74 static int parse_acl_file(const char *filename
, ACLList
*acl_list
)
80 f
= fopen(filename
, "r");
85 while (fgets(line
, sizeof(line
), f
) != NULL
) {
87 char *cmd
, *arg
, *argend
;
89 while (g_ascii_isspace(*ptr
)) {
93 /* skip comments and empty lines */
94 if (*ptr
== '#' || *ptr
== 0) {
99 arg
= strchr(cmd
, ' ');
101 arg
= strchr(cmd
, '\t');
105 fprintf(stderr
, "Invalid config line:\n %s\n", line
);
111 while (g_ascii_isspace(*arg
)) {
115 argend
= arg
+ strlen(arg
);
116 while (arg
!= argend
&& g_ascii_isspace(*(argend
- 1))) {
121 if (!g_str_equal(cmd
, "include") && strlen(arg
) >= IFNAMSIZ
) {
122 fprintf(stderr
, "name `%s' too long: %zu\n", arg
, strlen(arg
));
126 if (strcmp(cmd
, "deny") == 0) {
127 acl_rule
= g_malloc(sizeof(*acl_rule
));
128 if (strcmp(arg
, "all") == 0) {
129 acl_rule
->type
= ACL_DENY_ALL
;
131 acl_rule
->type
= ACL_DENY
;
132 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
134 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
135 } else if (strcmp(cmd
, "allow") == 0) {
136 acl_rule
= g_malloc(sizeof(*acl_rule
));
137 if (strcmp(arg
, "all") == 0) {
138 acl_rule
->type
= ACL_ALLOW_ALL
;
140 acl_rule
->type
= ACL_ALLOW
;
141 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
143 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
144 } else if (strcmp(cmd
, "include") == 0) {
146 parse_acl_file(arg
, acl_list
);
148 fprintf(stderr
, "Unknown command `%s'\n", cmd
);
163 static bool has_vnet_hdr(int fd
)
165 unsigned int features
= 0;
167 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
171 if (!(features
& IFF_VNET_HDR
)) {
178 static void prep_ifreq(struct ifreq
*ifr
, const char *ifname
)
180 memset(ifr
, 0, sizeof(*ifr
));
181 snprintf(ifr
->ifr_name
, IFNAMSIZ
, "%s", ifname
);
184 static int send_fd(int c
, int fd
)
186 char msgbuf
[CMSG_SPACE(sizeof(fd
))];
187 struct msghdr msg
= {
188 .msg_control
= msgbuf
,
189 .msg_controllen
= sizeof(msgbuf
),
191 struct cmsghdr
*cmsg
;
193 char req
[1] = { 0x00 };
195 cmsg
= CMSG_FIRSTHDR(&msg
);
196 cmsg
->cmsg_level
= SOL_SOCKET
;
197 cmsg
->cmsg_type
= SCM_RIGHTS
;
198 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
199 msg
.msg_controllen
= cmsg
->cmsg_len
;
202 iov
.iov_len
= sizeof(req
);
206 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
208 return sendmsg(c
, &msg
, 0);
211 #ifdef CONFIG_LIBCAP_NG
212 static int drop_privileges(void)
214 /* clear all capabilities */
215 capng_clear(CAPNG_SELECT_BOTH
);
217 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
218 CAP_NET_ADMIN
) < 0) {
222 /* change to calling user's real uid and gid, retaining supplemental
223 * groups and CAP_NET_ADMIN */
224 if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING
)) {
232 int main(int argc
, char **argv
)
236 unsigned long ifargs
[4];
239 int fd
= -1, ctlfd
= -1, unixfd
= -1;
242 const char *bridge
= NULL
;
243 char iface
[IFNAMSIZ
];
247 int access_allowed
, access_denied
;
248 int ret
= EXIT_SUCCESS
;
249 g_autofree
char *acl_file
= NULL
;
251 #ifdef CONFIG_LIBCAP_NG
252 /* if we're run from an suid binary, immediately drop privileges preserving
254 if (geteuid() == 0 && getuid() != geteuid()) {
255 if (drop_privileges() == -1) {
256 fprintf(stderr
, "failed to drop privileges\n");
262 qemu_init_exec_dir(argv
[0]);
264 /* parse arguments */
265 for (index
= 1; index
< argc
; index
++) {
266 if (strcmp(argv
[index
], "--use-vnet") == 0) {
268 } else if (strncmp(argv
[index
], "--br=", 5) == 0) {
269 bridge
= &argv
[index
][5];
270 } else if (strncmp(argv
[index
], "--fd=", 5) == 0) {
271 unixfd
= atoi(&argv
[index
][5]);
278 if (bridge
== NULL
|| unixfd
== -1) {
282 if (strlen(bridge
) >= IFNAMSIZ
) {
283 fprintf(stderr
, "name `%s' too long: %zu\n", bridge
, strlen(bridge
));
287 /* parse default acl file */
288 QSIMPLEQ_INIT(&acl_list
);
289 acl_file
= get_relocated_path(DEFAULT_ACL_FILE
);
290 if (parse_acl_file(acl_file
, &acl_list
) == -1) {
291 fprintf(stderr
, "failed to parse default acl file `%s'\n",
297 /* validate bridge against acl -- default policy is to deny
298 * according acl policy if we have a deny and allow both
299 * then deny should always win over allow
303 QSIMPLEQ_FOREACH(acl_rule
, &acl_list
, entry
) {
304 switch (acl_rule
->type
) {
309 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
317 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
324 if ((access_allowed
== 0) || (access_denied
== 1)) {
325 fprintf(stderr
, "access denied by acl file\n");
330 /* open a socket to use to control the network interfaces */
331 ctlfd
= socket(AF_INET
, SOCK_STREAM
, 0);
333 fprintf(stderr
, "failed to open control socket: %s\n", strerror(errno
));
338 /* open the tap device */
339 fd
= open("/dev/net/tun", O_RDWR
);
341 fprintf(stderr
, "failed to open /dev/net/tun: %s\n", strerror(errno
));
346 /* request a tap device, disable PI, and add vnet header support if
347 * requested and it's available. */
348 prep_ifreq(&ifr
, "tap%d");
349 ifr
.ifr_flags
= IFF_TAP
|IFF_NO_PI
;
350 if (use_vnet
&& has_vnet_hdr(fd
)) {
351 ifr
.ifr_flags
|= IFF_VNET_HDR
;
354 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
355 fprintf(stderr
, "failed to create tun device: %s\n", strerror(errno
));
360 /* save tap device name */
361 snprintf(iface
, sizeof(iface
), "%s", ifr
.ifr_name
);
363 /* get the mtu of the bridge */
364 prep_ifreq(&ifr
, bridge
);
365 if (ioctl(ctlfd
, SIOCGIFMTU
, &ifr
) == -1) {
366 fprintf(stderr
, "failed to get mtu of bridge `%s': %s\n",
367 bridge
, strerror(errno
));
375 /* set the mtu of the interface based on the bridge */
376 prep_ifreq(&ifr
, iface
);
378 if (ioctl(ctlfd
, SIOCSIFMTU
, &ifr
) == -1) {
379 fprintf(stderr
, "failed to set mtu of device `%s' to %d: %s\n",
380 iface
, mtu
, strerror(errno
));
385 /* Linux uses the lowest enslaved MAC address as the MAC address of
386 * the bridge. Set MAC address to a high value so that it doesn't
387 * affect the MAC address of the bridge.
389 if (ioctl(ctlfd
, SIOCGIFHWADDR
, &ifr
) < 0) {
390 fprintf(stderr
, "failed to get MAC address of device `%s': %s\n",
391 iface
, strerror(errno
));
395 ifr
.ifr_hwaddr
.sa_data
[0] = 0xFE;
396 if (ioctl(ctlfd
, SIOCSIFHWADDR
, &ifr
) < 0) {
397 fprintf(stderr
, "failed to set MAC address of device `%s': %s\n",
398 iface
, strerror(errno
));
403 /* add the interface to the bridge */
404 prep_ifreq(&ifr
, bridge
);
405 ifindex
= if_nametoindex(iface
);
407 ifargs
[0] = BRCTL_ADD_IF
;
411 ifr
.ifr_data
= (void *)ifargs
;
412 ret
= ioctl(ctlfd
, SIOCDEVPRIVATE
, &ifr
);
414 ifr
.ifr_ifindex
= ifindex
;
415 ret
= ioctl(ctlfd
, SIOCBRADDIF
, &ifr
);
418 fprintf(stderr
, "failed to add interface `%s' to bridge `%s': %s\n",
419 iface
, bridge
, strerror(errno
));
424 /* bring the interface up */
425 prep_ifreq(&ifr
, iface
);
426 if (ioctl(ctlfd
, SIOCGIFFLAGS
, &ifr
) == -1) {
427 fprintf(stderr
, "failed to get interface flags for `%s': %s\n",
428 iface
, strerror(errno
));
433 ifr
.ifr_flags
|= IFF_UP
;
434 if (ioctl(ctlfd
, SIOCSIFFLAGS
, &ifr
) == -1) {
435 fprintf(stderr
, "failed to bring up interface `%s': %s\n",
436 iface
, strerror(errno
));
441 /* write fd to the domain socket */
442 if (send_fd(unixfd
, fd
) == -1) {
443 fprintf(stderr
, "failed to write fd to unix socket: %s\n",
460 while ((acl_rule
= QSIMPLEQ_FIRST(&acl_list
)) != NULL
) {
461 QSIMPLEQ_REMOVE_HEAD(&acl_list
, entry
);