TUNInterface_Linux.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "exception/Except.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/EventBase.h"
  18. #include "interface/Interface.h"
  19. #include "interface/tuntap/TUNInterface.h"
  20. #include "util/events/Pipe.h"
  21. #define string_strncpy
  22. #define string_strlen
  23. #define string_strerror
  24. #include "util/platform/libc/string.h"
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <sys/ioctl.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include <sys/socket.h>
  31. #include <sys/types.h>
  32. #include <stdlib.h>
  33. #include <stddef.h>
  34. #include <net/if.h>
  35. #include <arpa/inet.h>
  36. #include <linux/if.h>
  37. #include <linux/if_tun.h>
  38. #include <linux/if_ether.h>
  39. struct Interface* TUNInterface_new(const char* interfaceName,
  40. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  41. struct EventBase* base,
  42. struct Log* logger,
  43. struct Except* eh,
  44. struct Allocator* alloc)
  45. {
  46. uint32_t maxNameSize = (IFNAMSIZ < TUNInterface_IFNAMSIZ) ? IFNAMSIZ : TUNInterface_IFNAMSIZ;
  47. Log_info(logger, "Initializing tun device [%s]", ((interfaceName) ? interfaceName : "auto"));
  48. struct ifreq ifRequest = { .ifr_flags = IFF_TUN };
  49. if (interfaceName) {
  50. if (strlen(interfaceName) > maxNameSize) {
  51. Except_raise(eh, TUNInterface_new_BAD_TUNNEL,
  52. "tunnel name too big, limit is [%d] characters", maxNameSize);
  53. }
  54. strncpy(ifRequest.ifr_name, interfaceName, maxNameSize);
  55. }
  56. int fileno = open("/dev/net/tun", O_RDWR);
  57. if (fileno < 0) {
  58. int err = errno;
  59. int code = (err == EPERM) ? TUNInterface_new_PERMISSION : TUNInterface_new_INTERNAL;
  60. Except_raise(eh, code, "open(\"/dev/net/tun\") [%s]", strerror(err));
  61. }
  62. if (ioctl(fileno, TUNSETIFF, &ifRequest) < 0) {
  63. int err = errno;
  64. int code = (err == EPERM) ? TUNInterface_new_PERMISSION : TUNInterface_new_INTERNAL;
  65. close(fileno);
  66. Except_raise(eh, code, "ioctl(TUNSETIFF) [%s]", strerror(err));
  67. }
  68. strncpy(assignedInterfaceName, ifRequest.ifr_name, maxNameSize);
  69. struct Pipe* p = Pipe_forFiles(fileno, fileno, base, eh, alloc);
  70. return &p->iface;
  71. }