1
0

TUNInterface_freebsd.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "interface/tuntap/TUNInterface.h"
  17. #include "interface/tuntap/BSDMessageTypeWrapper.h"
  18. #include "util/AddrTools.h"
  19. #include "util/Bits.h"
  20. #include "util/events/Pipe.h"
  21. #include <errno.h>
  22. #include <ctype.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <sys/ioctl.h>
  26. #include <unistd.h>
  27. #include <sys/socket.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <stdlib.h>
  31. #include <stddef.h>
  32. #include <net/if.h>
  33. #include <string.h>
  34. #include <netdb.h>
  35. #include <net/if_var.h>
  36. #include <net/if_tun.h>
  37. #include <netinet/in.h>
  38. #include <netinet6/in6_var.h>
  39. #include <netinet6/nd6.h>
  40. struct Interface* TUNInterface_new(const char* interfaceName,
  41. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  42. struct EventBase* base,
  43. struct Log* logger,
  44. struct Except* eh,
  45. struct Allocator* alloc)
  46. {
  47. // Open the descriptor
  48. int tunFd = open("/dev/tun", O_RDWR);
  49. //Get the resulting device name
  50. const char* assignedDevname;
  51. assignedDevname = fdevname(tunFd);
  52. // Extract the number eg: 0 from tun0
  53. int ppa = 0;
  54. for (uint32_t i = 0; i < strlen(assignedDevname); i++) {
  55. if (isdigit(assignedDevname[i])) {
  56. ppa = atoi(assignedDevname);
  57. }
  58. }
  59. if (tunFd < 0 || ppa < 0 ) {
  60. int err = errno;
  61. close(tunFd);
  62. char* error = NULL;
  63. if (tunFd < 0) {
  64. error = "open(\"/dev/tun\")";
  65. } else if (ppa < 0) {
  66. error = "fdevname/getting number from fdevname";
  67. }
  68. Except_raise(eh, TUNInterface_new_INTERNAL, error, strerror(err));
  69. }
  70. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  71. // so we'll skip the pretty names and call everything tunX
  72. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "tun%d", ppa);
  73. char* error = NULL;
  74. // We want to send IPv6 through our tun device, so we need to be able to specify "ethertype"
  75. int tunhead = 1;
  76. if (ioctl(tunFd,TUNSIFHEAD,&tunhead) == -1) {
  77. error = "TUNSIFHEAD";
  78. }
  79. if (error) {
  80. int err = errno;
  81. close(tunFd);
  82. Except_raise(eh, TUNInterface_new_INTERNAL, "%s [%s]", error, strerror(err));
  83. }
  84. struct Pipe* p = Pipe_forFiles(tunFd, tunFd, base, eh, alloc);
  85. return BSDMessageTypeWrapper_new(&p->iface, logger);
  86. }