OSXEthernetTapFactory.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  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 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/stat.h>
  32. #include "OSXEthernetTapFactory.hpp"
  33. #include "OSXEthernetTap.hpp"
  34. #include "../node/Utils.hpp"
  35. namespace ZeroTier {
  36. OSXEthernetTapFactory::OSXEthernetTapFactory(const char *pathToTapKext,const char *tapKextName) :
  37. _pathToTapKext((pathToTapKext) ? pathToTapKext : ""),
  38. _tapKextName((tapKextName) ? tapKextName : "")
  39. {
  40. struct stat stattmp;
  41. if ((_pathToTapKext.length())&&(_tapKextName.length())) {
  42. if (stat("/dev/zt0",&stattmp)) {
  43. long kextpid = (long)vfork();
  44. if (kextpid == 0) {
  45. ::chdir(_pathToTapKext.c_str());
  46. Utils::redirectUnixOutputs("/dev/null",(const char *)0);
  47. ::execl("/sbin/kextload","/sbin/kextload","-q","-repository",_pathToTapKext.c_str(),_tapKextName.c_str(),(const char *)0);
  48. ::_exit(-1);
  49. } else if (kextpid > 0) {
  50. int exitcode = -1;
  51. ::waitpid(kextpid,&exitcode,0);
  52. } else throw std::runtime_error("unable to create subprocess with fork()");
  53. }
  54. }
  55. if (stat("/dev/zt0",&stattmp)) {
  56. ::usleep(500); // give tap device driver time to start up and try again
  57. if (stat("/dev/zt0",&stattmp))
  58. throw std::runtime_error("/dev/zt# tap devices do not exist and unable to load kernel extension");
  59. }
  60. }
  61. OSXEthernetTapFactory::~OSXEthernetTapFactory()
  62. {
  63. Mutex::Lock _l(_devices_m);
  64. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d)
  65. delete *d;
  66. if ((_pathToTapKext.length())&&(_tapKextName.length())) {
  67. // Attempt to unload kext. If anything else is using a /dev/zt# node, this
  68. // fails and the kext stays in the kernel.
  69. char tmp[16384];
  70. sprintf(tmp,"%s/%s",_pathToTapKext.c_str(),_tapKextName.c_str());
  71. long kextpid = (long)vfork();
  72. if (kextpid == 0) {
  73. Utils::redirectUnixOutputs("/dev/null",(const char *)0);
  74. ::execl("/sbin/kextunload","/sbin/kextunload",tmp,(const char *)0);
  75. ::_exit(-1);
  76. } else if (kextpid > 0) {
  77. int exitcode = -1;
  78. ::waitpid(kextpid,&exitcode,0);
  79. }
  80. }
  81. }
  82. EthernetTap *OSXEthernetTapFactory::open(
  83. const MAC &mac,
  84. unsigned int mtu,
  85. unsigned int metric,
  86. uint64_t nwid,
  87. const char *desiredDevice,
  88. const char *friendlyName,
  89. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  90. void *arg)
  91. {
  92. Mutex::Lock _l(_devices_m);
  93. EthernetTap *t = new OSXEthernetTap(mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  94. _devices.push_back(t);
  95. return t;
  96. }
  97. void OSXEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  98. {
  99. {
  100. Mutex::Lock _l(_devices_m);
  101. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d) {
  102. if (*d == tap) {
  103. _devices.erase(d);
  104. break;
  105. }
  106. }
  107. }
  108. delete tap;
  109. }
  110. } // namespace ZeroTier