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 11KB

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