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"
44 #include "net/tap-linux.h"
50 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
59 typedef struct ACLRule
{
62 QSIMPLEQ_ENTRY(ACLRule
) entry
;
65 typedef QSIMPLEQ_HEAD(ACLList
, ACLRule
) ACLList
;
67 static void usage(void)
70 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
73 static int parse_acl_file(const char *filename
, ACLList
*acl_list
)
79 f
= fopen(filename
, "r");
84 while (fgets(line
, sizeof(line
), f
) != NULL
) {
86 char *cmd
, *arg
, *argend
;
88 while (g_ascii_isspace(*ptr
)) {
92 /* skip comments and empty lines */
93 if (*ptr
== '#' || *ptr
== 0) {
98 arg
= strchr(cmd
, ' ');
100 arg
= strchr(cmd
, '\t');
104 fprintf(stderr
, "Invalid config line:\n %s\n", line
);
112 while (g_ascii_isspace(*arg
)) {
116 argend
= arg
+ strlen(arg
);
117 while (arg
!= argend
&& g_ascii_isspace(*(argend
- 1))) {
122 if (strcmp(cmd
, "deny") == 0) {
123 acl_rule
= g_malloc(sizeof(*acl_rule
));
124 if (strcmp(arg
, "all") == 0) {
125 acl_rule
->type
= ACL_DENY_ALL
;
127 acl_rule
->type
= ACL_DENY
;
128 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
130 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
131 } else if (strcmp(cmd
, "allow") == 0) {
132 acl_rule
= g_malloc(sizeof(*acl_rule
));
133 if (strcmp(arg
, "all") == 0) {
134 acl_rule
->type
= ACL_ALLOW_ALL
;
136 acl_rule
->type
= ACL_ALLOW
;
137 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
139 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
140 } else if (strcmp(cmd
, "include") == 0) {
142 parse_acl_file(arg
, acl_list
);
144 fprintf(stderr
, "Unknown command `%s'\n", cmd
);
156 static bool has_vnet_hdr(int fd
)
158 unsigned int features
= 0;
160 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
164 if (!(features
& IFF_VNET_HDR
)) {
171 static void prep_ifreq(struct ifreq
*ifr
, const char *ifname
)
173 memset(ifr
, 0, sizeof(*ifr
));
174 snprintf(ifr
->ifr_name
, IFNAMSIZ
, "%s", ifname
);
177 static int send_fd(int c
, int fd
)
179 char msgbuf
[CMSG_SPACE(sizeof(fd
))];
180 struct msghdr msg
= {
181 .msg_control
= msgbuf
,
182 .msg_controllen
= sizeof(msgbuf
),
184 struct cmsghdr
*cmsg
;
186 char req
[1] = { 0x00 };
188 cmsg
= CMSG_FIRSTHDR(&msg
);
189 cmsg
->cmsg_level
= SOL_SOCKET
;
190 cmsg
->cmsg_type
= SCM_RIGHTS
;
191 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
192 msg
.msg_controllen
= cmsg
->cmsg_len
;
195 iov
.iov_len
= sizeof(req
);
199 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
201 return sendmsg(c
, &msg
, 0);
205 static int drop_privileges(void)
207 /* clear all capabilities */
208 capng_clear(CAPNG_SELECT_BOTH
);
210 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
211 CAP_NET_ADMIN
) < 0) {
215 /* change to calling user's real uid and gid, retaining supplemental
216 * groups and CAP_NET_ADMIN */
217 if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING
)) {
225 int main(int argc
, char **argv
)
229 unsigned long ifargs
[4];
232 int fd
= -1, ctlfd
= -1, unixfd
= -1;
235 const char *bridge
= NULL
;
236 char iface
[IFNAMSIZ
];
240 int access_allowed
, access_denied
;
241 int ret
= EXIT_SUCCESS
;
244 /* if we're run from an suid binary, immediately drop privileges preserving
246 if (geteuid() == 0 && getuid() != geteuid()) {
247 if (drop_privileges() == -1) {
248 fprintf(stderr
, "failed to drop privileges\n");
254 /* parse arguments */
255 for (index
= 1; index
< argc
; index
++) {
256 if (strcmp(argv
[index
], "--use-vnet") == 0) {
258 } else if (strncmp(argv
[index
], "--br=", 5) == 0) {
259 bridge
= &argv
[index
][5];
260 } else if (strncmp(argv
[index
], "--fd=", 5) == 0) {
261 unixfd
= atoi(&argv
[index
][5]);
268 if (bridge
== NULL
|| unixfd
== -1) {
273 /* parse default acl file */
274 QSIMPLEQ_INIT(&acl_list
);
275 if (parse_acl_file(DEFAULT_ACL_FILE
, &acl_list
) == -1) {
276 fprintf(stderr
, "failed to parse default acl file `%s'\n",
282 /* validate bridge against acl -- default policy is to deny
283 * according acl policy if we have a deny and allow both
284 * then deny should always win over allow
288 QSIMPLEQ_FOREACH(acl_rule
, &acl_list
, entry
) {
289 switch (acl_rule
->type
) {
294 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
302 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
309 if ((access_allowed
== 0) || (access_denied
== 1)) {
310 fprintf(stderr
, "access denied by acl file\n");
315 /* open a socket to use to control the network interfaces */
316 ctlfd
= socket(AF_INET
, SOCK_STREAM
, 0);
318 fprintf(stderr
, "failed to open control socket: %s\n", strerror(errno
));
323 /* open the tap device */
324 fd
= open("/dev/net/tun", O_RDWR
);
326 fprintf(stderr
, "failed to open /dev/net/tun: %s\n", strerror(errno
));
331 /* request a tap device, disable PI, and add vnet header support if
332 * requested and it's available. */
333 prep_ifreq(&ifr
, "tap%d");
334 ifr
.ifr_flags
= IFF_TAP
|IFF_NO_PI
;
335 if (use_vnet
&& has_vnet_hdr(fd
)) {
336 ifr
.ifr_flags
|= IFF_VNET_HDR
;
339 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
340 fprintf(stderr
, "failed to create tun device: %s\n", strerror(errno
));
345 /* save tap device name */
346 snprintf(iface
, sizeof(iface
), "%s", ifr
.ifr_name
);
348 /* get the mtu of the bridge */
349 prep_ifreq(&ifr
, bridge
);
350 if (ioctl(ctlfd
, SIOCGIFMTU
, &ifr
) == -1) {
351 fprintf(stderr
, "failed to get mtu of bridge `%s': %s\n",
352 bridge
, strerror(errno
));
360 /* set the mtu of the interface based on the bridge */
361 prep_ifreq(&ifr
, iface
);
363 if (ioctl(ctlfd
, SIOCSIFMTU
, &ifr
) == -1) {
364 fprintf(stderr
, "failed to set mtu of device `%s' to %d: %s\n",
365 iface
, mtu
, strerror(errno
));
370 /* Linux uses the lowest enslaved MAC address as the MAC address of
371 * the bridge. Set MAC address to a high value so that it doesn't
372 * affect the MAC address of the bridge.
374 if (ioctl(ctlfd
, SIOCGIFHWADDR
, &ifr
) < 0) {
375 fprintf(stderr
, "failed to get MAC address of device `%s': %s\n",
376 iface
, strerror(errno
));
380 ifr
.ifr_hwaddr
.sa_data
[0] = 0xFE;
381 if (ioctl(ctlfd
, SIOCSIFHWADDR
, &ifr
) < 0) {
382 fprintf(stderr
, "failed to set MAC address of device `%s': %s\n",
383 iface
, strerror(errno
));
388 /* add the interface to the bridge */
389 prep_ifreq(&ifr
, bridge
);
390 ifindex
= if_nametoindex(iface
);
392 ifargs
[0] = BRCTL_ADD_IF
;
396 ifr
.ifr_data
= (void *)ifargs
;
397 ret
= ioctl(ctlfd
, SIOCDEVPRIVATE
, &ifr
);
399 ifr
.ifr_ifindex
= ifindex
;
400 ret
= ioctl(ctlfd
, SIOCBRADDIF
, &ifr
);
403 fprintf(stderr
, "failed to add interface `%s' to bridge `%s': %s\n",
404 iface
, bridge
, strerror(errno
));
409 /* bring the interface up */
410 prep_ifreq(&ifr
, iface
);
411 if (ioctl(ctlfd
, SIOCGIFFLAGS
, &ifr
) == -1) {
412 fprintf(stderr
, "failed to get interface flags for `%s': %s\n",
413 iface
, strerror(errno
));
418 ifr
.ifr_flags
|= IFF_UP
;
419 if (ioctl(ctlfd
, SIOCSIFFLAGS
, &ifr
) == -1) {
420 fprintf(stderr
, "failed to bring up interface `%s': %s\n",
421 iface
, strerror(errno
));
426 /* write fd to the domain socket */
427 if (send_fd(unixfd
, fd
) == -1) {
428 fprintf(stderr
, "failed to write fd to unix socket: %s\n",
445 while ((acl_rule
= QSIMPLEQ_FIRST(&acl_list
)) != NULL
) {
446 QSIMPLEQ_REMOVE_HEAD(&acl_list
, entry
);