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 "qemu/osdep.h"
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
23 #include <sys/prctl.h>
27 #include <linux/sockios.h>
30 #include <linux/if_bridge.h>
33 #include "qemu/queue.h"
35 #include "net/tap-linux.h"
41 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
50 typedef struct ACLRule
{
53 QSIMPLEQ_ENTRY(ACLRule
) entry
;
56 typedef QSIMPLEQ_HEAD(ACLList
, ACLRule
) ACLList
;
58 static void usage(void)
61 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
64 static int parse_acl_file(const char *filename
, ACLList
*acl_list
)
70 f
= fopen(filename
, "r");
75 while (fgets(line
, sizeof(line
), f
) != NULL
) {
77 char *cmd
, *arg
, *argend
;
79 while (isspace(*ptr
)) {
83 /* skip comments and empty lines */
84 if (*ptr
== '#' || *ptr
== 0) {
89 arg
= strchr(cmd
, ' ');
91 arg
= strchr(cmd
, '\t');
95 fprintf(stderr
, "Invalid config line:\n %s\n", line
);
103 while (isspace(*arg
)) {
107 argend
= arg
+ strlen(arg
);
108 while (arg
!= argend
&& isspace(*(argend
- 1))) {
113 if (strcmp(cmd
, "deny") == 0) {
114 acl_rule
= g_malloc(sizeof(*acl_rule
));
115 if (strcmp(arg
, "all") == 0) {
116 acl_rule
->type
= ACL_DENY_ALL
;
118 acl_rule
->type
= ACL_DENY
;
119 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
121 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
122 } else if (strcmp(cmd
, "allow") == 0) {
123 acl_rule
= g_malloc(sizeof(*acl_rule
));
124 if (strcmp(arg
, "all") == 0) {
125 acl_rule
->type
= ACL_ALLOW_ALL
;
127 acl_rule
->type
= ACL_ALLOW
;
128 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
130 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
131 } else if (strcmp(cmd
, "include") == 0) {
133 parse_acl_file(arg
, acl_list
);
135 fprintf(stderr
, "Unknown command `%s'\n", cmd
);
147 static bool has_vnet_hdr(int fd
)
149 unsigned int features
= 0;
151 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
155 if (!(features
& IFF_VNET_HDR
)) {
162 static void prep_ifreq(struct ifreq
*ifr
, const char *ifname
)
164 memset(ifr
, 0, sizeof(*ifr
));
165 snprintf(ifr
->ifr_name
, IFNAMSIZ
, "%s", ifname
);
168 static int send_fd(int c
, int fd
)
170 char msgbuf
[CMSG_SPACE(sizeof(fd
))];
171 struct msghdr msg
= {
172 .msg_control
= msgbuf
,
173 .msg_controllen
= sizeof(msgbuf
),
175 struct cmsghdr
*cmsg
;
177 char req
[1] = { 0x00 };
179 cmsg
= CMSG_FIRSTHDR(&msg
);
180 cmsg
->cmsg_level
= SOL_SOCKET
;
181 cmsg
->cmsg_type
= SCM_RIGHTS
;
182 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
183 msg
.msg_controllen
= cmsg
->cmsg_len
;
186 iov
.iov_len
= sizeof(req
);
190 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
192 return sendmsg(c
, &msg
, 0);
196 static int drop_privileges(void)
198 /* clear all capabilities */
199 capng_clear(CAPNG_SELECT_BOTH
);
201 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
202 CAP_NET_ADMIN
) < 0) {
206 /* change to calling user's real uid and gid, retaining supplemental
207 * groups and CAP_NET_ADMIN */
208 if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING
)) {
216 int main(int argc
, char **argv
)
220 unsigned long ifargs
[4];
223 int fd
= -1, ctlfd
= -1, unixfd
= -1;
226 const char *bridge
= NULL
;
227 char iface
[IFNAMSIZ
];
231 int access_allowed
, access_denied
;
232 int ret
= EXIT_SUCCESS
;
235 /* if we're run from an suid binary, immediately drop privileges preserving
237 if (geteuid() == 0 && getuid() != geteuid()) {
238 if (drop_privileges() == -1) {
239 fprintf(stderr
, "failed to drop privileges\n");
245 /* parse arguments */
246 for (index
= 1; index
< argc
; index
++) {
247 if (strcmp(argv
[index
], "--use-vnet") == 0) {
249 } else if (strncmp(argv
[index
], "--br=", 5) == 0) {
250 bridge
= &argv
[index
][5];
251 } else if (strncmp(argv
[index
], "--fd=", 5) == 0) {
252 unixfd
= atoi(&argv
[index
][5]);
259 if (bridge
== NULL
|| unixfd
== -1) {
264 /* parse default acl file */
265 QSIMPLEQ_INIT(&acl_list
);
266 if (parse_acl_file(DEFAULT_ACL_FILE
, &acl_list
) == -1) {
267 fprintf(stderr
, "failed to parse default acl file `%s'\n",
273 /* validate bridge against acl -- default policy is to deny
274 * according acl policy if we have a deny and allow both
275 * then deny should always win over allow
279 QSIMPLEQ_FOREACH(acl_rule
, &acl_list
, entry
) {
280 switch (acl_rule
->type
) {
285 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
293 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
300 if ((access_allowed
== 0) || (access_denied
== 1)) {
301 fprintf(stderr
, "access denied by acl file\n");
306 /* open a socket to use to control the network interfaces */
307 ctlfd
= socket(AF_INET
, SOCK_STREAM
, 0);
309 fprintf(stderr
, "failed to open control socket: %s\n", strerror(errno
));
314 /* open the tap device */
315 fd
= open("/dev/net/tun", O_RDWR
);
317 fprintf(stderr
, "failed to open /dev/net/tun: %s\n", strerror(errno
));
322 /* request a tap device, disable PI, and add vnet header support if
323 * requested and it's available. */
324 prep_ifreq(&ifr
, "tap%d");
325 ifr
.ifr_flags
= IFF_TAP
|IFF_NO_PI
;
326 if (use_vnet
&& has_vnet_hdr(fd
)) {
327 ifr
.ifr_flags
|= IFF_VNET_HDR
;
330 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
331 fprintf(stderr
, "failed to create tun device: %s\n", strerror(errno
));
336 /* save tap device name */
337 snprintf(iface
, sizeof(iface
), "%s", ifr
.ifr_name
);
339 /* get the mtu of the bridge */
340 prep_ifreq(&ifr
, bridge
);
341 if (ioctl(ctlfd
, SIOCGIFMTU
, &ifr
) == -1) {
342 fprintf(stderr
, "failed to get mtu of bridge `%s': %s\n",
343 bridge
, strerror(errno
));
351 /* set the mtu of the interface based on the bridge */
352 prep_ifreq(&ifr
, iface
);
354 if (ioctl(ctlfd
, SIOCSIFMTU
, &ifr
) == -1) {
355 fprintf(stderr
, "failed to set mtu of device `%s' to %d: %s\n",
356 iface
, mtu
, strerror(errno
));
361 /* Linux uses the lowest enslaved MAC address as the MAC address of
362 * the bridge. Set MAC address to a high value so that it doesn't
363 * affect the MAC address of the bridge.
365 if (ioctl(ctlfd
, SIOCGIFHWADDR
, &ifr
) < 0) {
366 fprintf(stderr
, "failed to get MAC address of device `%s': %s\n",
367 iface
, strerror(errno
));
371 ifr
.ifr_hwaddr
.sa_data
[0] = 0xFE;
372 if (ioctl(ctlfd
, SIOCSIFHWADDR
, &ifr
) < 0) {
373 fprintf(stderr
, "failed to set MAC address of device `%s': %s\n",
374 iface
, strerror(errno
));
379 /* add the interface to the bridge */
380 prep_ifreq(&ifr
, bridge
);
381 ifindex
= if_nametoindex(iface
);
383 ifargs
[0] = BRCTL_ADD_IF
;
387 ifr
.ifr_data
= (void *)ifargs
;
388 ret
= ioctl(ctlfd
, SIOCDEVPRIVATE
, &ifr
);
390 ifr
.ifr_ifindex
= ifindex
;
391 ret
= ioctl(ctlfd
, SIOCBRADDIF
, &ifr
);
394 fprintf(stderr
, "failed to add interface `%s' to bridge `%s': %s\n",
395 iface
, bridge
, strerror(errno
));
400 /* bring the interface up */
401 prep_ifreq(&ifr
, iface
);
402 if (ioctl(ctlfd
, SIOCGIFFLAGS
, &ifr
) == -1) {
403 fprintf(stderr
, "failed to get interface flags for `%s': %s\n",
404 iface
, strerror(errno
));
409 ifr
.ifr_flags
|= IFF_UP
;
410 if (ioctl(ctlfd
, SIOCSIFFLAGS
, &ifr
) == -1) {
411 fprintf(stderr
, "failed to bring up interface `%s': %s\n",
412 iface
, strerror(errno
));
417 /* write fd to the domain socket */
418 if (send_fd(unixfd
, fd
) == -1) {
419 fprintf(stderr
, "failed to write fd to unix socket: %s\n",
436 while ((acl_rule
= QSIMPLEQ_FIRST(&acl_list
)) != NULL
) {
437 QSIMPLEQ_REMOVE_HEAD(&acl_list
, entry
);