vde_device.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. device.c -- VDE plug
  3. Copyright (C) 2012 Guus Sliepen <guus@tinc-vpn.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "system.h"
  17. #include <libvdeplug_dyn.h>
  18. #include "conf.h"
  19. #include "device.h"
  20. #include "net.h"
  21. #include "logger.h"
  22. #include "utils.h"
  23. #include "route.h"
  24. #include "xalloc.h"
  25. static struct vdepluglib plug;
  26. static struct vdeconn *conn = NULL;
  27. static int port = 0;
  28. static char *group = NULL;
  29. static const char *device_info = "VDE socket";
  30. extern char *identname;
  31. extern volatile bool running;
  32. static uint64_t device_total_in = 0;
  33. static uint64_t device_total_out = 0;
  34. static bool setup_device(void) {
  35. libvdeplug_dynopen(plug);
  36. if(!plug.dl_handle) {
  37. logger(LOG_ERR, "Could not open libvdeplug library!");
  38. return false;
  39. }
  40. if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
  41. xasprintf(&device, RUNSTATEDIR "/vde.ctl");
  42. }
  43. get_config_string(lookup_config(config_tree, "Interface"), &iface);
  44. get_config_int(lookup_config(config_tree, "VDEPort"), &port);
  45. get_config_string(lookup_config(config_tree, "VDEGroup"), &group);
  46. struct vde_open_args args = {
  47. .port = port,
  48. .group = group,
  49. .mode = 0700,
  50. };
  51. conn = plug.vde_open(device, identname, &args);
  52. if(!conn) {
  53. logger(LOG_ERR, "Could not open VDE socket %s", device);
  54. return false;
  55. }
  56. device_fd = plug.vde_datafd(conn);
  57. #ifdef FD_CLOEXEC
  58. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  59. #endif
  60. logger(LOG_INFO, "%s is a %s", device, device_info);
  61. if(routing_mode == RMODE_ROUTER) {
  62. overwrite_mac = true;
  63. }
  64. return true;
  65. }
  66. static void close_device(void) {
  67. if(conn) {
  68. plug.vde_close(conn);
  69. }
  70. if(plug.dl_handle) {
  71. libvdeplug_dynclose(plug);
  72. }
  73. free(device);
  74. free(iface);
  75. }
  76. static bool read_packet(vpn_packet_t *packet) {
  77. int lenin = (ssize_t)plug.vde_recv(conn, packet->data, MTU, 0);
  78. if(lenin <= 0) {
  79. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
  80. running = false;
  81. return false;
  82. }
  83. packet->len = lenin;
  84. device_total_in += packet->len;
  85. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
  86. return true;
  87. }
  88. static bool write_packet(vpn_packet_t *packet) {
  89. if((ssize_t)plug.vde_send(conn, packet->data, packet->len, 0) < 0) {
  90. if(errno != EINTR && errno != EAGAIN) {
  91. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
  92. running = false;
  93. }
  94. return false;
  95. }
  96. device_total_out += packet->len;
  97. return true;
  98. }
  99. static void dump_device_stats(void) {
  100. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  101. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  102. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  103. }
  104. const devops_t vde_devops = {
  105. .setup = setup_device,
  106. .close = close_device,
  107. .read = read_packet,
  108. .write = write_packet,
  109. .dump_stats = dump_device_stats,
  110. };