You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qemu-bridge-helper.c 12KB

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