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 const char *device_info = "raw_socket";
  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. }
  38. if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
  39. device = xstrdup(iface);
  40. }
  41. if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
  42. logger(LOG_ERR, "Could not open %s: %s", device_info,
  43. strerror(errno));
  44. return false;
  45. }
  46. #ifdef FD_CLOEXEC
  47. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  48. #endif
  49. memset(&ifr, 0, sizeof(ifr));
  50. strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
  51. ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
  52. if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) {
  53. close(device_fd);
  54. logger(LOG_ERR, "Can't find interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(errno));
  55. return false;
  56. }
  57. memset(&sa, 0, sizeof(sa));
  58. sa.sll_family = AF_PACKET;
  59. sa.sll_protocol = htons(ETH_P_ALL);
  60. sa.sll_ifindex = ifr.ifr_ifindex;
  61. if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) {
  62. logger(LOG_ERR, "Could not bind %s to %s: %s", device, ifr.ifr_ifrn.ifrn_name, strerror(errno));
  63. return false;
  64. }
  65. logger(LOG_INFO, "%s is a %s", device, device_info);
  66. return true;
  67. }
  68. static void close_device(void) {
  69. close(device_fd);
  70. free(device);
  71. free(iface);
  72. }
  73. static bool read_packet(vpn_packet_t *packet) {
  74. int lenin;
  75. if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
  76. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
  77. device, strerror(errno));
  78. return false;
  79. }
  80. packet->len = lenin;
  81. device_total_in += packet->len;
  82. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
  83. device_info);
  84. return true;
  85. }
  86. static bool write_packet(vpn_packet_t *packet) {
  87. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
  88. packet->len, device_info);
  89. if(write(device_fd, packet->data, packet->len) < 0) {
  90. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
  91. strerror(errno));
  92. return false;
  93. }
  94. device_total_out += packet->len;
  95. return true;
  96. }
  97. static void dump_device_stats(void) {
  98. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  99. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  100. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  101. }
  102. const devops_t raw_socket_devops = {
  103. .setup = setup_device,
  104. .close = close_device,
  105. .read = read_packet,
  106. .write = write_packet,
  107. .dump_stats = dump_device_stats,
  108. };
  109. #else
  110. static bool not_supported(void) {
  111. logger(LOG_ERR, "Raw socket device not supported on this platform");
  112. return false;
  113. }
  114. const devops_t raw_socket_devops = {
  115. .setup = not_supported,
  116. .close = NULL,
  117. .read = NULL,
  118. .write = NULL,
  119. .dump_stats = NULL,
  120. };
  121. #endif