raw_socket_device.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. device.c -- raw socket
  3. Copyright (C) 2002-2005 Ivo Timmermans,
  4. 2002-2014 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #ifdef HAVE_NETPACKET_PACKET_H
  19. #include <netpacket/packet.h>
  20. #endif
  21. #include "conf.h"
  22. #include "device.h"
  23. #include "net.h"
  24. #include "logger.h"
  25. #include "utils.h"
  26. #include "route.h"
  27. #include "xalloc.h"
  28. #if defined(PF_PACKET) && defined(ETH_P_ALL) && defined(AF_PACKET) && defined(SIOCGIFINDEX)
  29. static char *device_info;
  30. static uint64_t device_total_in = 0;
  31. static uint64_t device_total_out = 0;
  32. static bool setup_device(void) {
  33. struct ifreq ifr;
  34. struct sockaddr_ll sa;
  35. if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
  36. iface = xstrdup("eth0");
  37. if(!get_config_string(lookup_config(config_tree, "Device"), &device))
  38. device = xstrdup(iface);
  39. device_info = "raw socket";
  40. if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
  41. logger(LOG_ERR, "Could not open %s: %s", device_info,
  42. strerror(errno));
  43. return false;
  44. }
  45. #ifdef FD_CLOEXEC
  46. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  47. #endif
  48. memset(&ifr, 0, sizeof(ifr));
  49. strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
  50. ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
  51. if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) {
  52. close(device_fd);
  53. logger(LOG_ERR, "Can't find interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(errno));
  54. return false;
  55. }
  56. memset(&sa, 0, sizeof(sa));
  57. sa.sll_family = AF_PACKET;
  58. sa.sll_protocol = htons(ETH_P_ALL);
  59. sa.sll_ifindex = ifr.ifr_ifindex;
  60. if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) {
  61. logger(LOG_ERR, "Could not bind %s to %s: %s", device, ifr.ifr_ifrn.ifrn_name, strerror(errno));
  62. return false;
  63. }
  64. logger(LOG_INFO, "%s is a %s", device, device_info);
  65. return true;
  66. }
  67. static void close_device(void) {
  68. close(device_fd);
  69. free(device);
  70. free(iface);
  71. }
  72. static bool read_packet(vpn_packet_t *packet) {
  73. int lenin;
  74. if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
  75. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
  76. device, strerror(errno));
  77. return false;
  78. }
  79. packet->len = lenin;
  80. device_total_in += packet->len;
  81. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
  82. device_info);
  83. return true;
  84. }
  85. static bool write_packet(vpn_packet_t *packet) {
  86. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
  87. packet->len, device_info);
  88. if(write(device_fd, packet->data, packet->len) < 0) {
  89. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
  90. strerror(errno));
  91. return false;
  92. }
  93. device_total_out += packet->len;
  94. return true;
  95. }
  96. static void dump_device_stats(void) {
  97. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  98. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  99. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  100. }
  101. const devops_t raw_socket_devops = {
  102. .setup = setup_device,
  103. .close = close_device,
  104. .read = read_packet,
  105. .write = write_packet,
  106. .dump_stats = dump_device_stats,
  107. };
  108. #else
  109. static bool not_supported(void) {
  110. logger(LOG_ERR, "Raw socket device not supported on this platform");
  111. return false;
  112. }
  113. const devops_t raw_socket_devops = {
  114. .setup = not_supported,
  115. .close = NULL,
  116. .read = NULL,
  117. .write = NULL,
  118. .dump_stats = NULL,
  119. };
  120. #endif