device.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. device.c -- Interaction with FreeBSD tap device
  3. Copyright (C) 2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
  4. 2001-2002 Guus Sliepen <guus@sliepen.warande.net>
  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
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. $Id: device.c,v 1.1.2.2 2002/02/10 21:57:54 guus Exp $
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/socket.h>
  24. #include <fcntl.h>
  25. #include <net/if.h>
  26. #include <unistd.h>
  27. #include <syslog.h>
  28. #include <string.h>
  29. #include <utils.h>
  30. #include "conf.h"
  31. #include "net.h"
  32. #include "subnet.h"
  33. #include "system.h"
  34. #define DEFAULT_DEVICE "/dev/tap0"
  35. int device_fd = -1;
  36. int device_type;
  37. char *device;
  38. char *interface;
  39. char *device_info;
  40. int device_total_in = 0;
  41. int device_total_out = 0;
  42. extern subnet_t mymac;
  43. /*
  44. open the local ethertap device
  45. */
  46. int setup_device(void)
  47. {
  48. cp
  49. if(!get_config_string(lookup_config(config_tree, "Device"), &device))
  50. device = DEFAULT_DEVICE;
  51. if(!get_config_string(lookup_config(config_tree, "Interface"), &interface))
  52. interface = netname;
  53. cp
  54. if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0)
  55. {
  56. syslog(LOG_ERR, _("Could not open %s: %m"), device);
  57. return -1;
  58. }
  59. cp
  60. /* Set default MAC address for ethertap devices */
  61. mymac.type = SUBNET_MAC;
  62. mymac.net.mac.address.x[0] = 0xfe;
  63. mymac.net.mac.address.x[1] = 0xfd;
  64. mymac.net.mac.address.x[2] = 0x00;
  65. mymac.net.mac.address.x[3] = 0x00;
  66. mymac.net.mac.address.x[4] = 0x00;
  67. mymac.net.mac.address.x[5] = 0x00;
  68. device_info = _("FreeBSD tap device");
  69. syslog(LOG_INFO, _("%s is a %s"), device, device_info);
  70. cp
  71. return 0;
  72. }
  73. void close_device(void)
  74. {
  75. cp
  76. close(device_fd);
  77. }
  78. /*
  79. read, encrypt and send data that is
  80. available through the ethertap device
  81. */
  82. int read_packet(vpn_packet_t *packet)
  83. {
  84. int lenin;
  85. cp
  86. if((lenin = read(device_fd, packet->data, MTU)) <= 0)
  87. {
  88. syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device);
  89. return -1;
  90. }
  91. packet->len = lenin;
  92. device_total_in += packet->len;
  93. if(debug_lvl >= DEBUG_TRAFFIC)
  94. syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"),
  95. packet->len, device_info);
  96. return 0;
  97. cp
  98. }
  99. int write_packet(vpn_packet_t *packet)
  100. {
  101. cp
  102. if(debug_lvl >= DEBUG_TRAFFIC)
  103. syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
  104. packet->len, device_info);
  105. if(write(device_fd, packet->data, packet->len) < 0)
  106. {
  107. syslog(LOG_ERR, _("Error while writing to %s %s: %m"), device_info, device);
  108. return -1;
  109. }
  110. device_total_out += packet->len;
  111. cp
  112. }
  113. void dump_device_stats(void)
  114. {
  115. cp
  116. syslog(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
  117. syslog(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
  118. syslog(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
  119. cp
  120. }