qemu-bridge-helper.c 10 KB

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