TUNInterface_netbsd.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "interface/tuntap/TUNInterface.h"
  16. #include "exception/Except.h"
  17. #include "interface/tuntap/BSDMessageTypeWrapper.h"
  18. #include "util/AddrTools.h"
  19. #include "util/events/Pipe.h"
  20. #include <errno.h>
  21. #include <ctype.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <sys/ioctl.h>
  25. #include <unistd.h>
  26. #include <sys/socket.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <net/if.h>
  32. #include <string.h>
  33. #include <netdb.h>
  34. #include <net/if_tun.h>
  35. #include <netinet/in.h>
  36. #include <netinet6/in6_var.h>
  37. #include <netinet6/nd6.h>
  38. /* Tun Configurator for NetBSD. */
  39. Er_DEFUN(struct Iface* TUNInterface_new(const char* interfaceName,
  40. char assignedInterfaceName[TUNInterface_IFNAMSIZ],
  41. int isTapMode,
  42. struct EventBase* base,
  43. struct Log* logger,
  44. struct Allocator* alloc))
  45. {
  46. if (isTapMode) { Er_raise(alloc, "tap mode not supported on this platform"); }
  47. int err;
  48. char file[TUNInterface_IFNAMSIZ];
  49. int i;
  50. int ppa = -1; // to store the tunnel device index
  51. int tunFd = -1;
  52. if (interfaceName && strlen(interfaceName) > 3 && !strncmp(interfaceName, "tun", 3)) {
  53. snprintf(file, TUNInterface_IFNAMSIZ, "/dev/%s", interfaceName);
  54. tunFd = open(file, O_RDWR);
  55. } else {
  56. for (ppa = 0;tunFd == -1 && ppa < 99;ppa++) {
  57. snprintf(file, TUNInterface_IFNAMSIZ, "/dev/tun%d", ppa);
  58. tunFd = open(file, O_RDWR);
  59. }
  60. }
  61. if (tunFd < 0 ) {
  62. err = errno;
  63. close(tunFd);
  64. Er_raise(alloc, "%s [%s]", "open(\"/dev/tunX\")", strerror(err));
  65. }
  66. /* from the NetBSD tun man page:
  67. TUNSIFHEAD The argument should be a pointer to an int; a non-zero value
  68. turns off ``link-layer'' mode, and enables ``multi-af'' mode,
  69. where every packet is preceded with a four byte address
  70. family.
  71. */
  72. i = 2;
  73. if (ioctl(tunFd, TUNSIFHEAD, &i) == -1) {
  74. err = errno;
  75. close(tunFd);
  76. Er_raise(alloc, "%s [%s]", "ioctl(tunFd,TUNSIFHEAD,&2)", strerror(err));
  77. }
  78. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  79. // so we'll skip the pretty names and call everything tunX
  80. if (assignedInterfaceName) {
  81. if (ppa == -1) {
  82. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "%s", interfaceName);
  83. } else {
  84. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "tun%d", ppa);
  85. }
  86. }
  87. struct Pipe* p = Er(Pipe_forFd(tunFd, false, base, logger, alloc));
  88. struct BSDMessageTypeWrapper* bmtw = BSDMessageTypeWrapper_new(alloc, logger);
  89. Iface_plumb(&p->iface, &bmtw->wireSide);
  90. Er_ret(&bmtw->inside);
  91. }