BSDEthernetTap.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <signal.h>
  25. #include <fcntl.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/wait.h>
  30. #include <sys/select.h>
  31. #include <sys/cdefs.h>
  32. #include <sys/uio.h>
  33. #include <sys/param.h>
  34. #include <sys/sysctl.h>
  35. #include <sys/ioctl.h>
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #include <arpa/inet.h>
  39. #include <net/if.h>
  40. #include <ifaddrs.h>
  41. #include <net/if_arp.h>
  42. #include <net/if_dl.h>
  43. #include <net/if_media.h>
  44. #include <net/route.h>
  45. #include <string>
  46. #include <map>
  47. #include <set>
  48. #include <algorithm>
  49. #include <utility>
  50. #include "../node/Constants.hpp"
  51. #include "../node/Utils.hpp"
  52. #include "../node/Mutex.hpp"
  53. #include "OSUtils.hpp"
  54. #include "BSDEthernetTap.hpp"
  55. #define ZT_BASE32_CHARS "0123456789abcdefghijklmnopqrstuv"
  56. // ff:ff:ff:ff:ff:ff with no ADI
  57. static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff),0);
  58. namespace ZeroTier {
  59. BSDEthernetTap::BSDEthernetTap(
  60. const char *homePath,
  61. const MAC &mac,
  62. unsigned int mtu,
  63. unsigned int metric,
  64. uint64_t nwid,
  65. const char *friendlyName,
  66. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  67. void *arg) :
  68. _handler(handler),
  69. _arg(arg),
  70. _nwid(nwid),
  71. _mtu(mtu),
  72. _metric(metric),
  73. _fd(0),
  74. _enabled(true)
  75. {
  76. static Mutex globalTapCreateLock;
  77. char devpath[64],ethaddr[64],mtustr[32],metstr[32],tmpdevname[32];
  78. struct stat stattmp;
  79. // On FreeBSD at least we can rename, so use nwid to generate a deterministic unique zt#### name using base32
  80. // As a result we don't use desiredDevice
  81. _dev = "zt";
  82. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 60) & 0x1f)]);
  83. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 55) & 0x1f)]);
  84. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 50) & 0x1f)]);
  85. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 45) & 0x1f)]);
  86. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 40) & 0x1f)]);
  87. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 35) & 0x1f)]);
  88. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 30) & 0x1f)]);
  89. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 25) & 0x1f)]);
  90. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 20) & 0x1f)]);
  91. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 15) & 0x1f)]);
  92. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 10) & 0x1f)]);
  93. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)((nwid >> 5) & 0x1f)]);
  94. _dev.push_back(ZT_BASE32_CHARS[(unsigned long)(nwid & 0x1f)]);
  95. Mutex::Lock _gl(globalTapCreateLock);
  96. if (mtu > 2800)
  97. throw std::runtime_error("max tap MTU is 2800");
  98. // On BSD we create taps and they can have high numbers, so use ones starting
  99. // at 9993 to not conflict with other stuff. Then we rename it to zt<base32 of nwid>
  100. std::vector<std::string> devFiles(OSUtils::listDirectory("/dev"));
  101. for(int i=9993;i<(9993+128);++i) {
  102. Utils::snprintf(tmpdevname,sizeof(tmpdevname),"tap%d",i);
  103. Utils::snprintf(devpath,sizeof(devpath),"/dev/%s",tmpdevname);
  104. if (std::find(devFiles.begin(),devFiles.end(),std::string(tmpdevname)) == devFiles.end()) {
  105. long cpid = (long)vfork();
  106. if (cpid == 0) {
  107. ::execl("/sbin/ifconfig","/sbin/ifconfig",tmpdevname,"create",(const char *)0);
  108. ::_exit(-1);
  109. } else if (cpid > 0) {
  110. int exitcode = -1;
  111. ::waitpid(cpid,&exitcode,0);
  112. } else throw std::runtime_error("fork() failed");
  113. if (!stat(devpath,&stattmp)) {
  114. cpid = (long)vfork();
  115. if (cpid == 0) {
  116. ::execl("/sbin/ifconfig","/sbin/ifconfig",tmpdevname,"name",_dev.c_str(),(const char *)0);
  117. ::_exit(-1);
  118. } else if (cpid > 0) {
  119. int exitcode = -1;
  120. ::waitpid(cpid,&exitcode,0);
  121. if (exitcode)
  122. throw std::runtime_error("ifconfig rename operation failed");
  123. } else throw std::runtime_error("fork() failed");
  124. _fd = ::open(devpath,O_RDWR);
  125. if (_fd > 0)
  126. break;
  127. else throw std::runtime_error("unable to open created tap device");
  128. } else {
  129. throw std::runtime_error("cannot find /dev node for newly created tap device");
  130. }
  131. }
  132. }
  133. if (_fd <= 0)
  134. throw std::runtime_error("unable to open TAP device or no more devices available");
  135. if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
  136. ::close(_fd);
  137. throw std::runtime_error("unable to set flags on file descriptor for TAP device");
  138. }
  139. // Configure MAC address and MTU, bring interface up
  140. Utils::snprintf(ethaddr,sizeof(ethaddr),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]);
  141. Utils::snprintf(mtustr,sizeof(mtustr),"%u",_mtu);
  142. Utils::snprintf(metstr,sizeof(metstr),"%u",_metric);
  143. long cpid = (long)vfork();
  144. if (cpid == 0) {
  145. ::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"lladdr",ethaddr,"mtu",mtustr,"metric",metstr,"up",(const char *)0);
  146. ::_exit(-1);
  147. } else if (cpid > 0) {
  148. int exitcode = -1;
  149. ::waitpid(cpid,&exitcode,0);
  150. if (exitcode) {
  151. ::close(_fd);
  152. throw std::runtime_error("ifconfig failure setting link-layer address and activating tap interface");
  153. }
  154. }
  155. // Set close-on-exec so that devices cannot persist if we fork/exec for update
  156. fcntl(_fd,F_SETFD,fcntl(_fd,F_GETFD) | FD_CLOEXEC);
  157. ::pipe(_shutdownSignalPipe);
  158. _thread = Thread::start(this);
  159. }
  160. BSDEthernetTap::~BSDEthernetTap()
  161. {
  162. ::write(_shutdownSignalPipe[1],"\0",1); // causes thread to exit
  163. Thread::join(_thread);
  164. ::close(_fd);
  165. ::close(_shutdownSignalPipe[0]);
  166. ::close(_shutdownSignalPipe[1]);
  167. long cpid = (long)vfork();
  168. if (cpid == 0) {
  169. ::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"destroy",(const char *)0);
  170. ::_exit(-1);
  171. } else if (cpid > 0) {
  172. int exitcode = -1;
  173. ::waitpid(cpid,&exitcode,0);
  174. }
  175. }
  176. void BSDEthernetTap::setEnabled(bool en)
  177. {
  178. _enabled = en;
  179. }
  180. bool BSDEthernetTap::enabled() const
  181. {
  182. return _enabled;
  183. }
  184. static bool ___removeIp(const std::string &_dev,const InetAddress &ip)
  185. {
  186. long cpid = (long)vfork();
  187. if (cpid == 0) {
  188. execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"inet",ip.toIpString().c_str(),"-alias",(const char *)0);
  189. _exit(-1);
  190. } else if (cpid > 0) {
  191. int exitcode = -1;
  192. waitpid(cpid,&exitcode,0);
  193. return (exitcode == 0);
  194. }
  195. return false; // never reached, make compiler shut up about return value
  196. }
  197. bool BSDEthernetTap::addIp(const InetAddress &ip)
  198. {
  199. if (!ip)
  200. return false;
  201. std::vector<InetAddress> allIps(ips());
  202. if (std::find(allIps.begin(),allIps.end(),ip) != allIps.end())
  203. return true; // IP/netmask already assigned
  204. // Remove and reconfigure if address is the same but netmask is different
  205. for(std::vector<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i) {
  206. if ((i->ipsEqual(ip))&&(i->netmaskBits() != ip.netmaskBits())) {
  207. if (___removeIp(_dev,*i))
  208. break;
  209. }
  210. }
  211. long cpid = (long)vfork();
  212. if (cpid == 0) {
  213. ::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),ip.isV4() ? "inet" : "inet6",ip.toString().c_str(),"alias",(const char *)0);
  214. ::_exit(-1);
  215. } else if (cpid > 0) {
  216. int exitcode = -1;
  217. ::waitpid(cpid,&exitcode,0);
  218. return (exitcode == 0);
  219. }
  220. return false;
  221. }
  222. bool BSDEthernetTap::removeIp(const InetAddress &ip)
  223. {
  224. if (!ip)
  225. return false;
  226. std::vector<InetAddress> allIps(ips());
  227. if (std::find(allIps.begin(),allIps.end(),ip) != allIps.end()) {
  228. if (___removeIp(_dev,ip))
  229. return true;
  230. }
  231. return false;
  232. }
  233. std::vector<InetAddress> BSDEthernetTap::ips() const
  234. {
  235. struct ifaddrs *ifa = (struct ifaddrs *)0;
  236. if (getifaddrs(&ifa))
  237. return std::vector<InetAddress>();
  238. std::vector<InetAddress> r;
  239. struct ifaddrs *p = ifa;
  240. while (p) {
  241. if ((!strcmp(p->ifa_name,_dev.c_str()))&&(p->ifa_addr)&&(p->ifa_netmask)&&(p->ifa_addr->sa_family == p->ifa_netmask->sa_family)) {
  242. switch(p->ifa_addr->sa_family) {
  243. case AF_INET: {
  244. struct sockaddr_in *sin = (struct sockaddr_in *)p->ifa_addr;
  245. struct sockaddr_in *nm = (struct sockaddr_in *)p->ifa_netmask;
  246. r.push_back(InetAddress(&(sin->sin_addr.s_addr),4,Utils::countBits((uint32_t)nm->sin_addr.s_addr)));
  247. } break;
  248. case AF_INET6: {
  249. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)p->ifa_addr;
  250. struct sockaddr_in6 *nm = (struct sockaddr_in6 *)p->ifa_netmask;
  251. uint32_t b[4];
  252. memcpy(b,nm->sin6_addr.s6_addr,sizeof(b));
  253. r.push_back(InetAddress(sin->sin6_addr.s6_addr,16,Utils::countBits(b[0]) + Utils::countBits(b[1]) + Utils::countBits(b[2]) + Utils::countBits(b[3])));
  254. } break;
  255. }
  256. }
  257. p = p->ifa_next;
  258. }
  259. if (ifa)
  260. freeifaddrs(ifa);
  261. std::sort(r.begin(),r.end());
  262. std::unique(r.begin(),r.end());
  263. return r;
  264. }
  265. void BSDEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  266. {
  267. char putBuf[4096];
  268. if ((_fd > 0)&&(len <= _mtu)&&(_enabled)) {
  269. to.copyTo(putBuf,6);
  270. from.copyTo(putBuf + 6,6);
  271. *((uint16_t *)(putBuf + 12)) = htons((uint16_t)etherType);
  272. memcpy(putBuf + 14,data,len);
  273. len += 14;
  274. ::write(_fd,putBuf,len);
  275. }
  276. }
  277. std::string BSDEthernetTap::deviceName() const
  278. {
  279. return _dev;
  280. }
  281. void BSDEthernetTap::setFriendlyName(const char *friendlyName)
  282. {
  283. }
  284. void BSDEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
  285. {
  286. std::vector<MulticastGroup> newGroups;
  287. struct ifmaddrs *ifmap = (struct ifmaddrs *)0;
  288. if (!getifmaddrs(&ifmap)) {
  289. struct ifmaddrs *p = ifmap;
  290. while (p) {
  291. if (p->ifma_addr->sa_family == AF_LINK) {
  292. struct sockaddr_dl *in = (struct sockaddr_dl *)p->ifma_name;
  293. struct sockaddr_dl *la = (struct sockaddr_dl *)p->ifma_addr;
  294. if ((la->sdl_alen == 6)&&(in->sdl_nlen <= _dev.length())&&(!memcmp(_dev.data(),in->sdl_data,in->sdl_nlen)))
  295. newGroups.push_back(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen,6),0));
  296. }
  297. p = p->ifma_next;
  298. }
  299. freeifmaddrs(ifmap);
  300. }
  301. std::vector<InetAddress> allIps(ips());
  302. for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip)
  303. newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
  304. std::sort(newGroups.begin(),newGroups.end());
  305. std::unique(newGroups.begin(),newGroups.end());
  306. for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) {
  307. if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
  308. added.push_back(*m);
  309. }
  310. for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) {
  311. if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
  312. removed.push_back(*m);
  313. }
  314. _multicastGroups.swap(newGroups);
  315. }
  316. /*
  317. bool BSDEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  318. {
  319. std::set<MulticastGroup> newGroups;
  320. struct ifmaddrs *ifmap = (struct ifmaddrs *)0;
  321. if (!getifmaddrs(&ifmap)) {
  322. struct ifmaddrs *p = ifmap;
  323. while (p) {
  324. if (p->ifma_addr->sa_family == AF_LINK) {
  325. struct sockaddr_dl *in = (struct sockaddr_dl *)p->ifma_name;
  326. struct sockaddr_dl *la = (struct sockaddr_dl *)p->ifma_addr;
  327. if ((la->sdl_alen == 6)&&(in->sdl_nlen <= _dev.length())&&(!memcmp(_dev.data(),in->sdl_data,in->sdl_nlen)))
  328. newGroups.insert(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen,6),0));
  329. }
  330. p = p->ifma_next;
  331. }
  332. freeifmaddrs(ifmap);
  333. }
  334. {
  335. std::set<InetAddress> allIps(ips());
  336. for(std::set<InetAddress>::const_iterator i(allIps.begin());i!=allIps.end();++i)
  337. newGroups.insert(MulticastGroup::deriveMulticastGroupForAddressResolution(*i));
  338. }
  339. bool changed = false;
  340. for(std::set<MulticastGroup>::iterator mg(newGroups.begin());mg!=newGroups.end();++mg) {
  341. if (!groups.count(*mg)) {
  342. groups.insert(*mg);
  343. changed = true;
  344. }
  345. }
  346. for(std::set<MulticastGroup>::iterator mg(groups.begin());mg!=groups.end();) {
  347. if ((!newGroups.count(*mg))&&(*mg != _blindWildcardMulticastGroup)) {
  348. groups.erase(mg++);
  349. changed = true;
  350. } else ++mg;
  351. }
  352. return changed;
  353. }
  354. */
  355. void BSDEthernetTap::threadMain()
  356. throw()
  357. {
  358. fd_set readfds,nullfds;
  359. MAC to,from;
  360. int n,nfds,r;
  361. char getBuf[8194];
  362. // Wait for a moment after startup -- wait for Network to finish
  363. // constructing itself.
  364. Thread::sleep(500);
  365. FD_ZERO(&readfds);
  366. FD_ZERO(&nullfds);
  367. nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1;
  368. r = 0;
  369. for(;;) {
  370. FD_SET(_shutdownSignalPipe[0],&readfds);
  371. FD_SET(_fd,&readfds);
  372. select(nfds,&readfds,&nullfds,&nullfds,(struct timeval *)0);
  373. if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) // writes to shutdown pipe terminate thread
  374. break;
  375. if (FD_ISSET(_fd,&readfds)) {
  376. n = (int)::read(_fd,getBuf + r,sizeof(getBuf) - r);
  377. if (n < 0) {
  378. if ((errno != EINTR)&&(errno != ETIMEDOUT))
  379. break;
  380. } else {
  381. // Some tap drivers like to send the ethernet frame and the
  382. // payload in two chunks, so handle that by accumulating
  383. // data until we have at least a frame.
  384. r += n;
  385. if (r > 14) {
  386. if (r > ((int)_mtu + 14)) // sanity check for weird TAP behavior on some platforms
  387. r = _mtu + 14;
  388. if (_enabled) {
  389. to.setTo(getBuf,6);
  390. from.setTo(getBuf + 6,6);
  391. unsigned int etherType = ntohs(((const uint16_t *)getBuf)[6]);
  392. // TODO: VLAN support
  393. _handler(_arg,_nwid,from,to,etherType,0,(const void *)(getBuf + 14),r - 14);
  394. }
  395. r = 0;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. } // namespace ZeroTier