]>
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.
16 #include "qemu/osdep.h"
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
22 #include <sys/prctl.h>
26 #include <linux/sockios.h>
29 #include <linux/if_bridge.h>
32 #include "qemu/queue.h"
34 #include "net/tap-linux.h"
40 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
49 typedef struct ACLRule
{
52 QSIMPLEQ_ENTRY(ACLRule
) entry
;
55 typedef QSIMPLEQ_HEAD(ACLList
, ACLRule
) ACLList
;
57 static void usage(void)
60 "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
63 static int parse_acl_file(const char *filename
, ACLList
*acl_list
)
69 f
= fopen(filename
, "r");
74 while (fgets(line
, sizeof(line
), f
) != NULL
) {
76 char *cmd
, *arg
, *argend
;
78 while (isspace(*ptr
)) {
82 /* skip comments and empty lines */
83 if (*ptr
== '#' || *ptr
== 0) {
88 arg
= strchr(cmd
, ' ');
90 arg
= strchr(cmd
, '\t');
94 fprintf(stderr
, "Invalid config line:\n %s\n", line
);
102 while (isspace(*arg
)) {
106 argend
= arg
+ strlen(arg
);
107 while (arg
!= argend
&& isspace(*(argend
- 1))) {
112 if (strcmp(cmd
, "deny") == 0) {
113 acl_rule
= g_malloc(sizeof(*acl_rule
));
114 if (strcmp(arg
, "all") == 0) {
115 acl_rule
->type
= ACL_DENY_ALL
;
117 acl_rule
->type
= ACL_DENY
;
118 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
120 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
121 } else if (strcmp(cmd
, "allow") == 0) {
122 acl_rule
= g_malloc(sizeof(*acl_rule
));
123 if (strcmp(arg
, "all") == 0) {
124 acl_rule
->type
= ACL_ALLOW_ALL
;
126 acl_rule
->type
= ACL_ALLOW
;
127 snprintf(acl_rule
->iface
, IFNAMSIZ
, "%s", arg
);
129 QSIMPLEQ_INSERT_TAIL(acl_list
, acl_rule
, entry
);
130 } else if (strcmp(cmd
, "include") == 0) {
132 parse_acl_file(arg
, acl_list
);
134 fprintf(stderr
, "Unknown command `%s'\n", cmd
);
146 static bool has_vnet_hdr(int fd
)
148 unsigned int features
= 0;
150 if (ioctl(fd
, TUNGETFEATURES
, &features
) == -1) {
154 if (!(features
& IFF_VNET_HDR
)) {
161 static void prep_ifreq(struct ifreq
*ifr
, const char *ifname
)
163 memset(ifr
, 0, sizeof(*ifr
));
164 snprintf(ifr
->ifr_name
, IFNAMSIZ
, "%s", ifname
);
167 static int send_fd(int c
, int fd
)
169 char msgbuf
[CMSG_SPACE(sizeof(fd
))];
170 struct msghdr msg
= {
171 .msg_control
= msgbuf
,
172 .msg_controllen
= sizeof(msgbuf
),
174 struct cmsghdr
*cmsg
;
176 char req
[1] = { 0x00 };
178 cmsg
= CMSG_FIRSTHDR(&msg
);
179 cmsg
->cmsg_level
= SOL_SOCKET
;
180 cmsg
->cmsg_type
= SCM_RIGHTS
;
181 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
182 msg
.msg_controllen
= cmsg
->cmsg_len
;
185 iov
.iov_len
= sizeof(req
);
189 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(fd
));
191 return sendmsg(c
, &msg
, 0);
195 static int drop_privileges(void)
197 /* clear all capabilities */
198 capng_clear(CAPNG_SELECT_BOTH
);
200 if (capng_update(CAPNG_ADD
, CAPNG_EFFECTIVE
| CAPNG_PERMITTED
,
201 CAP_NET_ADMIN
) < 0) {
205 /* change to calling user's real uid and gid, retaining supplemental
206 * groups and CAP_NET_ADMIN */
207 if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING
)) {
215 int main(int argc
, char **argv
)
219 unsigned long ifargs
[4];
222 int fd
= -1, ctlfd
= -1, unixfd
= -1;
225 const char *bridge
= NULL
;
226 char iface
[IFNAMSIZ
];
230 int access_allowed
, access_denied
;
231 int ret
= EXIT_SUCCESS
;
234 /* if we're run from an suid binary, immediately drop privileges preserving
236 if (geteuid() == 0 && getuid() != geteuid()) {
237 if (drop_privileges() == -1) {
238 fprintf(stderr
, "failed to drop privileges\n");
244 /* parse arguments */
245 for (index
= 1; index
< argc
; index
++) {
246 if (strcmp(argv
[index
], "--use-vnet") == 0) {
248 } else if (strncmp(argv
[index
], "--br=", 5) == 0) {
249 bridge
= &argv
[index
][5];
250 } else if (strncmp(argv
[index
], "--fd=", 5) == 0) {
251 unixfd
= atoi(&argv
[index
][5]);
258 if (bridge
== NULL
|| unixfd
== -1) {
263 /* parse default acl file */
264 QSIMPLEQ_INIT(&acl_list
);
265 if (parse_acl_file(DEFAULT_ACL_FILE
, &acl_list
) == -1) {
266 fprintf(stderr
, "failed to parse default acl file `%s'\n",
272 /* validate bridge against acl -- default policy is to deny
273 * according acl policy if we have a deny and allow both
274 * then deny should always win over allow
278 QSIMPLEQ_FOREACH(acl_rule
, &acl_list
, entry
) {
279 switch (acl_rule
->type
) {
284 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
292 if (strcmp(bridge
, acl_rule
->iface
) == 0) {
299 if ((access_allowed
== 0) || (access_denied
== 1)) {
300 fprintf(stderr
, "access denied by acl file\n");
305 /* open a socket to use to control the network interfaces */
306 ctlfd
= socket(AF_INET
, SOCK_STREAM
, 0);
308 fprintf(stderr
, "failed to open control socket: %s\n", strerror(errno
));
313 /* open the tap device */
314 fd
= open("/dev/net/tun", O_RDWR
);
316 fprintf(stderr
, "failed to open /dev/net/tun: %s\n", strerror(errno
));
321 /* request a tap device, disable PI, and add vnet header support if
322 * requested and it's available. */
323 prep_ifreq(&ifr
, "tap%d");
324 ifr
.ifr_flags
= IFF_TAP
|IFF_NO_PI
;
325 if (use_vnet
&& has_vnet_hdr(fd
)) {
326 ifr
.ifr_flags
|= IFF_VNET_HDR
;
329 if (ioctl(fd
, TUNSETIFF
, &ifr
) == -1) {
330 fprintf(stderr
, "failed to create tun device: %s\n", strerror(errno
));
335 /* save tap device name */
336 snprintf(iface
, sizeof(iface
), "%s", ifr
.ifr_name
);
338 /* get the mtu of the bridge */
339 prep_ifreq(&ifr
, bridge
);
340 if (ioctl(ctlfd
, SIOCGIFMTU
, &ifr
) == -1) {
341 fprintf(stderr
, "failed to get mtu of bridge `%s': %s\n",
342 bridge
, strerror(errno
));
350 /* set the mtu of the interface based on the bridge */
351 prep_ifreq(&ifr
, iface
);
353 if (ioctl(ctlfd
, SIOCSIFMTU
, &ifr
) == -1) {
354 fprintf(stderr
, "failed to set mtu of device `%s' to %d: %s\n",
355 iface
, mtu
, strerror(errno
));
360 /* Linux uses the lowest enslaved MAC address as the MAC address of
361 * the bridge. Set MAC address to a high value so that it doesn't
362 * affect the MAC address of the bridge.
364 if (ioctl(ctlfd
, SIOCGIFHWADDR
, &ifr
) < 0) {
365 fprintf(stderr
, "failed to get MAC address of device `%s': %s\n",
366 iface
, strerror(errno
));
370 ifr
.ifr_hwaddr
.sa_data
[0] = 0xFE;
371 if (ioctl(ctlfd
, SIOCSIFHWADDR
, &ifr
) < 0) {
372 fprintf(stderr
, "failed to set MAC address of device `%s': %s\n",
373 iface
, strerror(errno
));
378 /* add the interface to the bridge */
379 prep_ifreq(&ifr
, bridge
);
380 ifindex
= if_nametoindex(iface
);
382 ifargs
[0] = BRCTL_ADD_IF
;
386 ifr
.ifr_data
= (void *)ifargs
;
387 ret
= ioctl(ctlfd
, SIOCDEVPRIVATE
, &ifr
);
389 ifr
.ifr_ifindex
= ifindex
;
390 ret
= ioctl(ctlfd
, SIOCBRADDIF
, &ifr
);
393 fprintf(stderr
, "failed to add interface `%s' to bridge `%s': %s\n",
394 iface
, bridge
, strerror(errno
));
399 /* bring the interface up */
400 prep_ifreq(&ifr
, iface
);
401 if (ioctl(ctlfd
, SIOCGIFFLAGS
, &ifr
) == -1) {
402 fprintf(stderr
, "failed to get interface flags for `%s': %s\n",
403 iface
, strerror(errno
));
408 ifr
.ifr_flags
|= IFF_UP
;
409 if (ioctl(ctlfd
, SIOCSIFFLAGS
, &ifr
) == -1) {
410 fprintf(stderr
, "failed to bring up interface `%s': %s\n",
411 iface
, strerror(errno
));
416 /* write fd to the domain socket */
417 if (send_fd(unixfd
, fd
) == -1) {
418 fprintf(stderr
, "failed to write fd to unix socket: %s\n",
435 while ((acl_rule
= QSIMPLEQ_FIRST(&acl_list
)) != NULL
) {
436 QSIMPLEQ_REMOVE_HEAD(&acl_list
, entry
);