TUNInterface_openbsd.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 OpenBSD. */
  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 ppa = -1; // to store the tunnel device index
  50. int tunFd = -1;
  51. if (interfaceName && strlen(interfaceName) > 3 && !strncmp(interfaceName, "tun", 3)) {
  52. snprintf(file, TUNInterface_IFNAMSIZ, "/dev/%s", interfaceName);
  53. tunFd = open(file, O_RDWR);
  54. } else {
  55. for (int i = 0; tunFd == -1 && i < 99; i++) {
  56. ppa = i;
  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. // Since devices are numbered rather than named, it's not possible to have tun0 and cjdns0
  67. // so we'll skip the pretty names and call everything tunX
  68. if (assignedInterfaceName) {
  69. if (ppa == -1) {
  70. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "%s", interfaceName);
  71. } else {
  72. snprintf(assignedInterfaceName, TUNInterface_IFNAMSIZ, "tun%d", ppa);
  73. }
  74. }
  75. struct Pipe* p = Er(Pipe_forFd(tunFd, false, base, logger, alloc));
  76. struct BSDMessageTypeWrapper* bmtw = BSDMessageTypeWrapper_new(alloc, logger);
  77. Iface_plumb(&p->iface, &bmtw->wireSide);
  78. Er_ret(&bmtw->inside);
  79. }