2
0

qemu-bridge-helper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #include "config-host.h"
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include <ctype.h>
  24. #include <glib.h>
  25. #include <sys/types.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #include <sys/prctl.h>
  30. #include <net/if.h>
  31. #include <linux/sockios.h>
  32. #include "qemu-queue.h"
  33. #include "net/tap-linux.h"
  34. #ifdef CONFIG_LIBCAP
  35. #include <cap-ng.h>
  36. #endif
  37. #define DEFAULT_ACL_FILE CONFIG_QEMU_CONFDIR "/bridge.conf"
  38. enum {
  39. ACL_ALLOW = 0,
  40. ACL_ALLOW_ALL,
  41. ACL_DENY,
  42. ACL_DENY_ALL,
  43. };
  44. typedef struct ACLRule {
  45. int type;
  46. char iface[IFNAMSIZ];
  47. QSIMPLEQ_ENTRY(ACLRule) entry;
  48. } ACLRule;
  49. typedef QSIMPLEQ_HEAD(ACLList, ACLRule) ACLList;
  50. static void usage(void)
  51. {
  52. fprintf(stderr,
  53. "Usage: qemu-bridge-helper [--use-vnet] --br=bridge --fd=unixfd\n");
  54. }
  55. static int parse_acl_file(const char *filename, ACLList *acl_list)
  56. {
  57. FILE *f;
  58. char line[4096];
  59. ACLRule *acl_rule;
  60. f = fopen(filename, "r");
  61. if (f == NULL) {
  62. return -1;
  63. }
  64. while (fgets(line, sizeof(line), f) != NULL) {
  65. char *ptr = line;
  66. char *cmd, *arg, *argend;
  67. while (isspace(*ptr)) {
  68. ptr++;
  69. }
  70. /* skip comments and empty lines */
  71. if (*ptr == '#' || *ptr == 0) {
  72. continue;
  73. }
  74. cmd = ptr;
  75. arg = strchr(cmd, ' ');
  76. if (arg == NULL) {
  77. arg = strchr(cmd, '\t');
  78. }
  79. if (arg == NULL) {
  80. fprintf(stderr, "Invalid config line:\n %s\n", line);
  81. fclose(f);
  82. errno = EINVAL;
  83. return -1;
  84. }
  85. *arg = 0;
  86. arg++;
  87. while (isspace(*arg)) {
  88. arg++;
  89. }
  90. argend = arg + strlen(arg);
  91. while (arg != argend && isspace(*(argend - 1))) {
  92. argend--;
  93. }
  94. *argend = 0;
  95. if (strcmp(cmd, "deny") == 0) {
  96. acl_rule = g_malloc(sizeof(*acl_rule));
  97. if (strcmp(arg, "all") == 0) {
  98. acl_rule->type = ACL_DENY_ALL;
  99. } else {
  100. acl_rule->type = ACL_DENY;
  101. snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
  102. }
  103. QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
  104. } else if (strcmp(cmd, "allow") == 0) {
  105. acl_rule = g_malloc(sizeof(*acl_rule));
  106. if (strcmp(arg, "all") == 0) {
  107. acl_rule->type = ACL_ALLOW_ALL;
  108. } else {
  109. acl_rule->type = ACL_ALLOW;
  110. snprintf(acl_rule->iface, IFNAMSIZ, "%s", arg);
  111. }
  112. QSIMPLEQ_INSERT_TAIL(acl_list, acl_rule, entry);
  113. } else if (strcmp(cmd, "include") == 0) {
  114. /* ignore errors */
  115. parse_acl_file(arg, acl_list);
  116. } else {
  117. fprintf(stderr, "Unknown command `%s'\n", cmd);
  118. fclose(f);
  119. errno = EINVAL;
  120. return -1;
  121. }
  122. }
  123. fclose(f);
  124. return 0;
  125. }
  126. static bool has_vnet_hdr(int fd)
  127. {
  128. unsigned int features = 0;
  129. if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
  130. return false;
  131. }
  132. if (!(features & IFF_VNET_HDR)) {
  133. return false;
  134. }
  135. return true;
  136. }
  137. static void prep_ifreq(struct ifreq *ifr, const char *ifname)
  138. {
  139. memset(ifr, 0, sizeof(*ifr));
  140. snprintf(ifr->ifr_name, IFNAMSIZ, "%s", ifname);
  141. }
  142. static int send_fd(int c, int fd)
  143. {
  144. char msgbuf[CMSG_SPACE(sizeof(fd))];
  145. struct msghdr msg = {
  146. .msg_control = msgbuf,
  147. .msg_controllen = sizeof(msgbuf),
  148. };
  149. struct cmsghdr *cmsg;
  150. struct iovec iov;
  151. char req[1] = { 0x00 };
  152. cmsg = CMSG_FIRSTHDR(&msg);
  153. cmsg->cmsg_level = SOL_SOCKET;
  154. cmsg->cmsg_type = SCM_RIGHTS;
  155. cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
  156. msg.msg_controllen = cmsg->cmsg_len;
  157. iov.iov_base = req;
  158. iov.iov_len = sizeof(req);
  159. msg.msg_iov = &iov;
  160. msg.msg_iovlen = 1;
  161. memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
  162. return sendmsg(c, &msg, 0);
  163. }
  164. #ifdef CONFIG_LIBCAP
  165. static int drop_privileges(void)
  166. {
  167. /* clear all capabilities */
  168. capng_clear(CAPNG_SELECT_BOTH);
  169. if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
  170. CAP_NET_ADMIN) < 0) {
  171. return -1;
  172. }
  173. /* change to calling user's real uid and gid, retaining supplemental
  174. * groups and CAP_NET_ADMIN */
  175. if (capng_change_id(getuid(), getgid(), CAPNG_CLEAR_BOUNDING)) {
  176. return -1;
  177. }
  178. return 0;
  179. }
  180. #endif
  181. int main(int argc, char **argv)
  182. {
  183. struct ifreq ifr;
  184. int fd, ctlfd, unixfd = -1;
  185. int use_vnet = 0;
  186. int mtu;
  187. const char *bridge = NULL;
  188. char iface[IFNAMSIZ];
  189. int index;
  190. ACLRule *acl_rule;
  191. ACLList acl_list;
  192. int access_allowed, access_denied;
  193. int ret = EXIT_SUCCESS;
  194. #ifdef CONFIG_LIBCAP
  195. /* if we're run from an suid binary, immediately drop privileges preserving
  196. * cap_net_admin */
  197. if (geteuid() == 0 && getuid() != geteuid()) {
  198. if (drop_privileges() == -1) {
  199. fprintf(stderr, "failed to drop privileges\n");
  200. return 1;
  201. }
  202. }
  203. #endif
  204. /* parse arguments */
  205. for (index = 1; index < argc; index++) {
  206. if (strcmp(argv[index], "--use-vnet") == 0) {
  207. use_vnet = 1;
  208. } else if (strncmp(argv[index], "--br=", 5) == 0) {
  209. bridge = &argv[index][5];
  210. } else if (strncmp(argv[index], "--fd=", 5) == 0) {
  211. unixfd = atoi(&argv[index][5]);
  212. } else {
  213. usage();
  214. return EXIT_FAILURE;
  215. }
  216. }
  217. if (bridge == NULL || unixfd == -1) {
  218. usage();
  219. return EXIT_FAILURE;
  220. }
  221. /* parse default acl file */
  222. QSIMPLEQ_INIT(&acl_list);
  223. if (parse_acl_file(DEFAULT_ACL_FILE, &acl_list) == -1) {
  224. fprintf(stderr, "failed to parse default acl file `%s'\n",
  225. DEFAULT_ACL_FILE);
  226. ret = EXIT_FAILURE;
  227. goto cleanup;
  228. }
  229. /* validate bridge against acl -- default policy is to deny
  230. * according acl policy if we have a deny and allow both
  231. * then deny should always win over allow
  232. */
  233. access_allowed = 0;
  234. access_denied = 0;
  235. QSIMPLEQ_FOREACH(acl_rule, &acl_list, entry) {
  236. switch (acl_rule->type) {
  237. case ACL_ALLOW_ALL:
  238. access_allowed = 1;
  239. break;
  240. case ACL_ALLOW:
  241. if (strcmp(bridge, acl_rule->iface) == 0) {
  242. access_allowed = 1;
  243. }
  244. break;
  245. case ACL_DENY_ALL:
  246. access_denied = 1;
  247. break;
  248. case ACL_DENY:
  249. if (strcmp(bridge, acl_rule->iface) == 0) {
  250. access_denied = 1;
  251. }
  252. break;
  253. }
  254. }
  255. if ((access_allowed == 0) || (access_denied == 1)) {
  256. fprintf(stderr, "access denied by acl file\n");
  257. ret = EXIT_FAILURE;
  258. goto cleanup;
  259. }
  260. /* open a socket to use to control the network interfaces */
  261. ctlfd = socket(AF_INET, SOCK_STREAM, 0);
  262. if (ctlfd == -1) {
  263. fprintf(stderr, "failed to open control socket: %s\n", strerror(errno));
  264. ret = EXIT_FAILURE;
  265. goto cleanup;
  266. }
  267. /* open the tap device */
  268. fd = open("/dev/net/tun", O_RDWR);
  269. if (fd == -1) {
  270. fprintf(stderr, "failed to open /dev/net/tun: %s\n", strerror(errno));
  271. ret = EXIT_FAILURE;
  272. goto cleanup;
  273. }
  274. /* request a tap device, disable PI, and add vnet header support if
  275. * requested and it's available. */
  276. prep_ifreq(&ifr, "tap%d");
  277. ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
  278. if (use_vnet && has_vnet_hdr(fd)) {
  279. ifr.ifr_flags |= IFF_VNET_HDR;
  280. }
  281. if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
  282. fprintf(stderr, "failed to create tun device: %s\n", strerror(errno));
  283. ret = EXIT_FAILURE;
  284. goto cleanup;
  285. }
  286. /* save tap device name */
  287. snprintf(iface, sizeof(iface), "%s", ifr.ifr_name);
  288. /* get the mtu of the bridge */
  289. prep_ifreq(&ifr, bridge);
  290. if (ioctl(ctlfd, SIOCGIFMTU, &ifr) == -1) {
  291. fprintf(stderr, "failed to get mtu of bridge `%s': %s\n",
  292. bridge, strerror(errno));
  293. ret = EXIT_FAILURE;
  294. goto cleanup;
  295. }
  296. /* save mtu */
  297. mtu = ifr.ifr_mtu;
  298. /* set the mtu of the interface based on the bridge */
  299. prep_ifreq(&ifr, iface);
  300. ifr.ifr_mtu = mtu;
  301. if (ioctl(ctlfd, SIOCSIFMTU, &ifr) == -1) {
  302. fprintf(stderr, "failed to set mtu of device `%s' to %d: %s\n",
  303. iface, mtu, strerror(errno));
  304. ret = EXIT_FAILURE;
  305. goto cleanup;
  306. }
  307. /* add the interface to the bridge */
  308. prep_ifreq(&ifr, bridge);
  309. ifr.ifr_ifindex = if_nametoindex(iface);
  310. if (ioctl(ctlfd, SIOCBRADDIF, &ifr) == -1) {
  311. fprintf(stderr, "failed to add interface `%s' to bridge `%s': %s\n",
  312. iface, bridge, strerror(errno));
  313. ret = EXIT_FAILURE;
  314. goto cleanup;
  315. }
  316. /* bring the interface up */
  317. prep_ifreq(&ifr, iface);
  318. if (ioctl(ctlfd, SIOCGIFFLAGS, &ifr) == -1) {
  319. fprintf(stderr, "failed to get interface flags for `%s': %s\n",
  320. iface, strerror(errno));
  321. ret = EXIT_FAILURE;
  322. goto cleanup;
  323. }
  324. ifr.ifr_flags |= IFF_UP;
  325. if (ioctl(ctlfd, SIOCSIFFLAGS, &ifr) == -1) {
  326. fprintf(stderr, "failed to bring up interface `%s': %s\n",
  327. iface, strerror(errno));
  328. ret = EXIT_FAILURE;
  329. goto cleanup;
  330. }
  331. /* write fd to the domain socket */
  332. if (send_fd(unixfd, fd) == -1) {
  333. fprintf(stderr, "failed to write fd to unix socket: %s\n",
  334. strerror(errno));
  335. ret = EXIT_FAILURE;
  336. goto cleanup;
  337. }
  338. /* ... */
  339. /* profit! */
  340. cleanup:
  341. while ((acl_rule = QSIMPLEQ_FIRST(&acl_list)) != NULL) {
  342. QSIMPLEQ_REMOVE_HEAD(&acl_list, entry);
  343. g_free(acl_rule);
  344. }
  345. return ret;
  346. }