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.
16 #include "config-host.h"
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
32 #include <sys/prctl.h>
36 #include <linux/sockios.h>
38 #include "qemu-queue.h"
40 #include "net/tap-linux.h"
46 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
55 typedef struct ACLRule
{
58 QSIMPLEQ_ENTRY(ACLRule
) entry
;
61 typedef QSIMPLEQ_HEAD(ACLList
, ACLRule
) ACLList
;
63 static void usage(void)
66 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
69 static int parse_acl_file(const char *filename
, ACLList
*acl_list
)
75 f
= fopen(filename
, "r");
80 while (fgets(line
, sizeof(line
), f
) != NULL
) {
82 char *cmd
, *arg
, *argend
;
84 while (isspace(*ptr
)) {
88 /* skip comments and empty lines */
89 if (*ptr
== '#' || *ptr
== 0) {
94 arg
= strchr(cmd
, ' ');
96 arg
= strchr(cmd
, '\t');
100 fprintf(stderr
, "Invalid config line:\n %s\n", line
);
108 while (isspace(*arg
)) {
112 argend
= arg
+ strlen(arg
);
113 while (arg
!= argend
&& isspace(*(argend
- 1))) {
118 if (strcmp(cmd
, "deny") == 0) {
119 acl_rule
= g_malloc(sizeof(*acl_rule
));
120 if (strcmp(arg
, "all") == 0) {
121 acl_rule
->type
= ACL_DENY_ALL
;
123 acl_rule
->type
= ACL_DENY
;
124 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
126 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
127 } else if (strcmp(cmd
, "allow") == 0) {
128 acl_rule
= g_malloc(sizeof(*acl_rule
));
129 if (strcmp(arg
, "all") == 0) {
130 acl_rule
->type
= ACL_ALLOW_ALL
;
132 acl_rule
->type
= ACL_ALLOW
;
133 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
135 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
136 } else if (strcmp(cmd
, "include") == 0) {
138 parse_acl_file(arg
, acl_list
);
140 fprintf(stderr
, "Unknown command `%s'\n", cmd
);
152 static bool has_vnet_hdr(int fd
)
154 unsigned int features
= 0;
156 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
160 if (!(features
& IFF_VNET_HDR
)) {
167 static void prep_ifreq(struct ifreq
*ifr
, const char *ifname
)
169 memset(ifr
, 0, sizeof(*ifr
));
170 snprintf(ifr
->ifr_name
, IFNAMSIZ
, "%s", ifname
);
173 static int send_fd(int c
, int fd
)
175 char msgbuf
[CMSG_SPACE(sizeof(fd
))];
176 struct msghdr msg
= {
177 .msg_control
= msgbuf
,
178 .msg_controllen
= sizeof(msgbuf
),
180 struct cmsghdr
*cmsg
;
182 char req
[1] = { 0x00 };
184 cmsg
= CMSG_FIRSTHDR(&msg
);
185 cmsg
->cmsg_level
= SOL_SOCKET
;
186 cmsg
->cmsg_type
= SCM_RIGHTS
;
187 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
188 msg
.msg_controllen
= cmsg
->cmsg_len
;
191 iov
.iov_len
= sizeof(req
);
195 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
197 return sendmsg(c
, &msg
, 0);
201 static int drop_privileges(void)
203 /* clear all capabilities */
204 capng_clear(CAPNG_SELECT_BOTH
);
206 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
207 CAP_NET_ADMIN
) < 0) {
211 /* change to calling user's real uid and gid, retaining supplemental
212 * groups and CAP_NET_ADMIN */
213 if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING
)) {
221 int main(int argc
, char **argv
)
224 int fd
, ctlfd
, unixfd
= -1;
227 const char *bridge
= NULL
;
228 char iface
[IFNAMSIZ
];
232 int access_allowed
, access_denied
;
233 int ret
= EXIT_SUCCESS
;
236 /* if we're run from an suid binary, immediately drop privileges preserving
238 if (geteuid() == 0 && getuid() != geteuid()) {
239 if (drop_privileges() == -1) {
240 fprintf(stderr
, "failed to drop privileges\n");
246 /* parse arguments */
247 for (index
= 1; index
< argc
; index
++) {
248 if (strcmp(argv
[index
], "--use-vnet") == 0) {
250 } else if (strncmp(argv
[index
], "--br=", 5) == 0) {
251 bridge
= &argv
[index
][5];
252 } else if (strncmp(argv
[index
], "--fd=", 5) == 0) {
253 unixfd
= atoi(&argv
[index
][5]);
260 if (bridge
== NULL
|| unixfd
== -1) {
265 /* parse default acl file */
266 QSIMPLEQ_INIT(&acl_list
);
267 if (parse_acl_file(DEFAULT_ACL_FILE
, &acl_list
) == -1) {
268 fprintf(stderr
, "failed to parse default acl file `%s'\n",
274 /* validate bridge against acl -- default policy is to deny
275 * according acl policy if we have a deny and allow both
276 * then deny should always win over allow
280 QSIMPLEQ_FOREACH(acl_rule
, &acl_list
, entry
) {
281 switch (acl_rule
->type
) {
286 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
294 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
301 if ((access_allowed
== 0) || (access_denied
== 1)) {
302 fprintf(stderr
, "access denied by acl file\n");
307 /* open a socket to use to control the network interfaces */
308 ctlfd
= socket(AF_INET
, SOCK_STREAM
, 0);
310 fprintf(stderr
, "failed to open control socket: %s\n", strerror(errno
));
315 /* open the tap device */
316 fd
= open("/dev/net/tun", O_RDWR
);
318 fprintf(stderr
, "failed to open /dev/net/tun: %s\n", strerror(errno
));
323 /* request a tap device, disable PI, and add vnet header support if
324 * requested and it's available. */
325 prep_ifreq(&ifr
, "tap%d");
326 ifr
.ifr_flags
= IFF_TAP
|IFF_NO_PI
;
327 if (use_vnet
&& has_vnet_hdr(fd
)) {
328 ifr
.ifr_flags
|= IFF_VNET_HDR
;
331 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
332 fprintf(stderr
, "failed to create tun device: %s\n", strerror(errno
));
337 /* save tap device name */
338 snprintf(iface
, sizeof(iface
), "%s", ifr
.ifr_name
);
340 /* get the mtu of the bridge */
341 prep_ifreq(&ifr
, bridge
);
342 if (ioctl(ctlfd
, SIOCGIFMTU
, &ifr
) == -1) {
343 fprintf(stderr
, "failed to get mtu of bridge `%s': %s\n",
344 bridge
, strerror(errno
));
352 /* set the mtu of the interface based on the bridge */
353 prep_ifreq(&ifr
, iface
);
355 if (ioctl(ctlfd
, SIOCSIFMTU
, &ifr
) == -1) {
356 fprintf(stderr
, "failed to set mtu of device `%s' to %d: %s\n",
357 iface
, mtu
, strerror(errno
));
362 /* add the interface to the bridge */
363 prep_ifreq(&ifr
, bridge
);
364 ifr
.ifr_ifindex
= if_nametoindex(iface
);
366 if (ioctl(ctlfd
, SIOCBRADDIF
, &ifr
) == -1) {
367 fprintf(stderr
, "failed to add interface `%s' to bridge `%s': %s\n",
368 iface
, bridge
, strerror(errno
));
373 /* bring the interface up */
374 prep_ifreq(&ifr
, iface
);
375 if (ioctl(ctlfd
, SIOCGIFFLAGS
, &ifr
) == -1) {
376 fprintf(stderr
, "failed to get interface flags for `%s': %s\n",
377 iface
, strerror(errno
));
382 ifr
.ifr_flags
|= IFF_UP
;
383 if (ioctl(ctlfd
, SIOCSIFFLAGS
, &ifr
) == -1) {
384 fprintf(stderr
, "failed to bring up interface `%s': %s\n",
385 iface
, strerror(errno
));
390 /* write fd to the domain socket */
391 if (send_fd(unixfd
, fd
) == -1) {
392 fprintf(stderr
, "failed to write fd to unix socket: %s\n",
404 while ((acl_rule
= QSIMPLEQ_FIRST(&acl_list
)) != NULL
) {
405 QSIMPLEQ_REMOVE_HEAD(&acl_list
, entry
);