qemu-bridge-helper.c 11 KB

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