xref: /openbmc/qemu/qemu-bridge-helper.c (revision ba632924)
1 /*
2  * QEMU Bridge Helper
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  * Anthony Liguori   <aliguori@us.ibm.com>
8  * Richa Marwaha     <rmarwah@linux.vnet.ibm.com>
9  * Corey Bryant      <coreyb@linux.vnet.ibm.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  *
14  */
15 
16 #include "qemu/osdep.h"
17 
18 
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <sys/prctl.h>
23 
24 #include <net/if.h>
25 
26 #include <linux/sockios.h>
27 
28 #ifndef SIOCBRADDIF
29 #include <linux/if_bridge.h>
30 #endif
31 
32 #include "qemu/queue.h"
33 
34 #include "net/tap-linux.h"
35 
36 #ifdef CONFIG_LIBCAP
37 #include <cap-ng.h>
38 #endif
39 
40 #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
41 
42 enum {
43     ACL_ALLOW = 0,
44     ACL_ALLOW_ALL,
45     ACL_DENY,
46     ACL_DENY_ALL,
47 };
48 
49 typedef struct ACLRule {
50     int type;
51     char iface[IFNAMSIZ];
52     QSIMPLEQ_ENTRY(ACLRule) entry;
53 } ACLRule;
54 
55 typedef QSIMPLEQ_HEAD(ACLList, ACLRule) ACLList;
56 
57 static void usage(void)
58 {
59     fprintf(stderr,
60             "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
61 }
62 
63 static int parse_acl_file(const char *filename, ACLList *acl_list)
64 {
65     FILE *f;
66     char line[4096];
67     ACLRule *acl_rule;
68 
69     f = fopen(filename, "r");
70     if (f == NULL) {
71         return -1;
72     }
73 
74     while (fgets(line, sizeof(line), f) != NULL) {
75         char *ptr = line;
76         char *cmd, *arg, *argend;
77 
78         while (isspace(*ptr)) {
79             ptr++;
80         }
81 
82         /* skip comments and empty lines */
83         if (*ptr == '#' || *ptr == 0) {
84             continue;
85         }
86 
87         cmd = ptr;
88         arg = strchr(cmd, ' ');
89         if (arg == NULL) {
90             arg = strchr(cmd, '\t');
91         }
92 
93         if (arg == NULL) {
94             fprintf(stderr, "Invalid config line:\n  %s\n", line);
95             fclose(f);
96             errno = EINVAL;
97             return -1;
98         }
99 
100         *arg = 0;
101         arg++;
102         while (isspace(*arg)) {
103             arg++;
104         }
105 
106         argend = arg + strlen(arg);
107         while (arg != argend && isspace(*(argend - 1))) {
108             argend--;
109         }
110         *argend = 0;
111 
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;
116             } else {
117                 acl_rule->type = ACL_DENY;
118                 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
119             }
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;
125             } else {
126                 acl_rule->type = ACL_ALLOW;
127                 snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
128             }
129             QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
130         } else if (strcmp(cmd, "include") == 0) {
131             /* ignore errors */
132             parse_acl_file(arg, acl_list);
133         } else {
134             fprintf(stderr, "Unknown command `%s'\n", cmd);
135             fclose(f);
136             errno = EINVAL;
137             return -1;
138         }
139     }
140 
141     fclose(f);
142 
143     return 0;
144 }
145 
146 static bool has_vnet_hdr(int fd)
147 {
148     unsigned int features = 0;
149 
150     if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
151         return false;
152     }
153 
154     if (!(features & IFF_VNET_HDR)) {
155         return false;
156     }
157 
158     return true;
159 }
160 
161 static void prep_ifreq(struct ifreq *ifr, const char *ifname)
162 {
163     memset(ifr, 0, sizeof(*ifr));
164     snprintf(ifr->ifr_name, IFNAMSIZ, "%s", ifname);
165 }
166 
167 static int send_fd(int c, int fd)
168 {
169     char msgbuf[CMSG_SPACE(sizeof(fd))];
170     struct msghdr msg = {
171         .msg_control = msgbuf,
172         .msg_controllen = sizeof(msgbuf),
173     };
174     struct cmsghdr *cmsg;
175     struct iovec iov;
176     char req[1] = { 0x00 };
177 
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;
183 
184     iov.iov_base = req;
185     iov.iov_len = sizeof(req);
186 
187     msg.msg_iov = &iov;
188     msg.msg_iovlen = 1;
189     memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
190 
191     return sendmsg(c, &msg, 0);
192 }
193 
194 #ifdef CONFIG_LIBCAP
195 static int drop_privileges(void)
196 {
197     /* clear all capabilities */
198     capng_clear(CAPNG_SELECT_BOTH);
199 
200     if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
201                      CAP_NET_ADMIN) < 0) {
202         return -1;
203     }
204 
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)) {
208         return -1;
209     }
210 
211     return 0;
212 }
213 #endif
214 
215 int main(int argc, char **argv)
216 {
217     struct ifreq ifr;
218 #ifndef SIOCBRADDIF
219     unsigned long ifargs[4];
220 #endif
221     int ifindex;
222     int fd = -1, ctlfd = -1, unixfd = -1;
223     int use_vnet = 0;
224     int mtu;
225     const char *bridge = NULL;
226     char iface[IFNAMSIZ];
227     int index;
228     ACLRule *acl_rule;
229     ACLList acl_list;
230     int access_allowed, access_denied;
231     int ret = EXIT_SUCCESS;
232 
233 #ifdef CONFIG_LIBCAP
234     /* if we're run from an suid binary, immediately drop privileges preserving
235      * cap_net_admin */
236     if (geteuid() == 0 && getuid() != geteuid()) {
237         if (drop_privileges() == -1) {
238             fprintf(stderr, "failed to drop privileges\n");
239             return 1;
240         }
241     }
242 #endif
243 
244     /* parse arguments */
245     for (index = 1; index < argc; index++) {
246         if (strcmp(argv[index], "--use-vnet") == 0) {
247             use_vnet = 1;
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]);
252         } else {
253             usage();
254             return EXIT_FAILURE;
255         }
256     }
257 
258     if (bridge == NULL || unixfd == -1) {
259         usage();
260         return EXIT_FAILURE;
261     }
262 
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",
267                 DEFAULT_ACL_FILE);
268         ret = EXIT_FAILURE;
269         goto cleanup;
270     }
271 
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
275      */
276     access_allowed = 0;
277     access_denied = 0;
278     QSIMPLEQ_FOREACH(acl_rule, &acl_list, entry) {
279         switch (acl_rule->type) {
280         case ACL_ALLOW_ALL:
281             access_allowed = 1;
282             break;
283         case ACL_ALLOW:
284             if (strcmp(bridge, acl_rule->iface) == 0) {
285                 access_allowed = 1;
286             }
287             break;
288         case ACL_DENY_ALL:
289             access_denied = 1;
290             break;
291         case ACL_DENY:
292             if (strcmp(bridge, acl_rule->iface) == 0) {
293                 access_denied = 1;
294             }
295             break;
296         }
297     }
298 
299     if ((access_allowed == 0) || (access_denied == 1)) {
300         fprintf(stderr, "access denied by acl file\n");
301         ret = EXIT_FAILURE;
302         goto cleanup;
303     }
304 
305     /* open a socket to use to control the network interfaces */
306     ctlfd = socket(AF_INET, SOCK_STREAM, 0);
307     if (ctlfd == -1) {
308         fprintf(stderr, "failed to open control socket: %s\n", strerror(errno));
309         ret = EXIT_FAILURE;
310         goto cleanup;
311     }
312 
313     /* open the tap device */
314     fd = open("/dev/net/tun", O_RDWR);
315     if (fd == -1) {
316         fprintf(stderr, "failed to open /dev/net/tun: %s\n", strerror(errno));
317         ret = EXIT_FAILURE;
318         goto cleanup;
319     }
320 
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;
327     }
328 
329     if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
330         fprintf(stderr, "failed to create tun device: %s\n", strerror(errno));
331         ret = EXIT_FAILURE;
332         goto cleanup;
333     }
334 
335     /* save tap device name */
336     snprintf(iface, sizeof(iface), "%s", ifr.ifr_name);
337 
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));
343         ret = EXIT_FAILURE;
344         goto cleanup;
345     }
346 
347     /* save mtu */
348     mtu = ifr.ifr_mtu;
349 
350     /* set the mtu of the interface based on the bridge */
351     prep_ifreq(&ifr, iface);
352     ifr.ifr_mtu = mtu;
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));
356         ret = EXIT_FAILURE;
357         goto cleanup;
358     }
359 
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.
363      */
364     if (ioctl(ctlfd, SIOCGIFHWADDR, &ifr) < 0) {
365         fprintf(stderr, "failed to get MAC address of device `%s': %s\n",
366                 iface, strerror(errno));
367         ret = EXIT_FAILURE;
368         goto cleanup;
369     }
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));
374         ret = EXIT_FAILURE;
375         goto cleanup;
376     }
377 
378     /* add the interface to the bridge */
379     prep_ifreq(&ifr, bridge);
380     ifindex = if_nametoindex(iface);
381 #ifndef SIOCBRADDIF
382     ifargs[0] = BRCTL_ADD_IF;
383     ifargs[1] = ifindex;
384     ifargs[2] = 0;
385     ifargs[3] = 0;
386     ifr.ifr_data = (void *)ifargs;
387     ret = ioctl(ctlfd, SIOCDEVPRIVATE, &ifr);
388 #else
389     ifr.ifr_ifindex = ifindex;
390     ret = ioctl(ctlfd, SIOCBRADDIF, &ifr);
391 #endif
392     if (ret == -1) {
393         fprintf(stderr, "failed to add interface `%s' to bridge `%s': %s\n",
394                 iface, bridge, strerror(errno));
395         ret = EXIT_FAILURE;
396         goto cleanup;
397     }
398 
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));
404         ret = EXIT_FAILURE;
405         goto cleanup;
406     }
407 
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));
412         ret = EXIT_FAILURE;
413         goto cleanup;
414     }
415 
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",
419                 strerror(errno));
420         ret = EXIT_FAILURE;
421         goto cleanup;
422     }
423 
424     /* ... */
425 
426     /* profit! */
427 
428 cleanup:
429     if (fd >= 0) {
430         close(fd);
431     }
432     if (ctlfd >= 0) {
433         close(ctlfd);
434     }
435     while ((acl_rule = QSIMPLEQ_FIRST(&acl_list)) != NULL) {
436         QSIMPLEQ_REMOVE_HEAD(&acl_list, entry);
437         g_free(acl_rule);
438     }
439 
440     return ret;
441 }
442