EthernetTap.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 <iostream>
  28. #include <string>
  29. #include "EthernetTap.hpp"
  30. #include "Logger.hpp"
  31. #include "RuntimeEnvironment.hpp"
  32. #include "Utils.hpp"
  33. #include "Mutex.hpp"
  34. // ff:ff:ff:ff:ff:ff with no ADI
  35. static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff),0);
  36. //
  37. // TAP implementation for *nix OSes, with some specialization for different flavors
  38. //
  39. #ifdef __UNIX_LIKE__ /////////////////////////////////////////////////////////
  40. #include <stdint.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <signal.h>
  46. #include <fcntl.h>
  47. #include <errno.h>
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50. #include <sys/ioctl.h>
  51. #include <sys/wait.h>
  52. #include <sys/select.h>
  53. #include <netinet/in.h>
  54. #include <net/if_arp.h>
  55. #include <arpa/inet.h>
  56. #ifdef __LINUX__
  57. #include <linux/if.h>
  58. #include <linux/if_tun.h>
  59. #include <linux/if_addr.h>
  60. #include <linux/if_ether.h>
  61. #define ZT_ETHERTAP_IP_COMMAND "/sbin/ip"
  62. #define ZT_ETHERTAP_SYSCTL_COMMAND "/sbin/sysctl"
  63. #endif // __LINUX__
  64. #ifdef __APPLE__
  65. #include <sys/uio.h>
  66. #include <sys/param.h>
  67. #include <sys/sysctl.h>
  68. #include <net/route.h>
  69. #include <net/if_dl.h>
  70. #include <ifaddrs.h>
  71. #define ZT_ETHERTAP_IFCONFIG "/sbin/ifconfig"
  72. #define ZT_MAC_KEXTLOAD "/sbin/kextload"
  73. #define ZT_MAC_IPCONFIG "/usr/sbin/ipconfig"
  74. #endif // __APPLE__
  75. namespace ZeroTier {
  76. // Only permit one tap to be opened concurrently across the entire process
  77. static Mutex __tapCreateLock;
  78. #ifdef __LINUX__
  79. EthernetTap::EthernetTap(
  80. const RuntimeEnvironment *renv,
  81. const MAC &mac,
  82. unsigned int mtu,
  83. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  84. void *arg)
  85. throw(std::runtime_error) :
  86. _mac(mac),
  87. _mtu(mtu),
  88. _r(renv),
  89. _handler(handler),
  90. _arg(arg),
  91. _fd(0)
  92. {
  93. char procpath[128];
  94. Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
  95. if (mtu > 4096)
  96. throw std::runtime_error("max tap MTU is 4096");
  97. _fd = ::open("/dev/net/tun",O_RDWR);
  98. if (_fd <= 0)
  99. throw std::runtime_error(std::string("could not open TUN/TAP device: ") + strerror(errno));
  100. struct ifreq ifr;
  101. memset(&ifr,0,sizeof(ifr));
  102. { // pick an unused device name
  103. int devno = 0;
  104. struct stat sbuf;
  105. do {
  106. sprintf(ifr.ifr_name,"zt%d",devno++);
  107. sprintf(procpath,"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name);
  108. } while (stat(procpath,&sbuf) == 0);
  109. }
  110. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  111. if (ioctl(_fd,TUNSETIFF,(void *)&ifr) < 0) {
  112. ::close(_fd);
  113. throw std::runtime_error("unable to configure TUN/TAP device for TAP operation");
  114. }
  115. strcpy(_dev,ifr.ifr_name);
  116. ioctl(_fd,TUNSETPERSIST,0); // valgrind may generate a false alarm here
  117. // Open an arbitrary socket to talk to netlink
  118. int sock = socket(AF_INET,SOCK_DGRAM,0);
  119. if (sock <= 0) {
  120. ::close(_fd);
  121. throw std::runtime_error("unable to open netlink socket");
  122. }
  123. // Set MAC address
  124. ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  125. memcpy(ifr.ifr_ifru.ifru_hwaddr.sa_data,mac.data,6);
  126. if (ioctl(sock,SIOCSIFHWADDR,(void *)&ifr) < 0) {
  127. ::close(_fd);
  128. ::close(sock);
  129. throw std::runtime_error("unable to configure TAP hardware (MAC) address");
  130. return;
  131. }
  132. // Set MTU
  133. ifr.ifr_ifru.ifru_mtu = (int)mtu;
  134. if (ioctl(sock,SIOCSIFMTU,(void *)&ifr) < 0) {
  135. ::close(_fd);
  136. ::close(sock);
  137. throw std::runtime_error("unable to configure TAP MTU");
  138. }
  139. if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
  140. ::close(_fd);
  141. throw std::runtime_error("unable to set flags on file descriptor for TAP device");
  142. }
  143. /* Bring interface up */
  144. if (ioctl(sock,SIOCGIFFLAGS,(void *)&ifr) < 0) {
  145. ::close(_fd);
  146. ::close(sock);
  147. throw std::runtime_error("unable to get TAP interface flags");
  148. }
  149. ifr.ifr_flags |= IFF_UP;
  150. if (ioctl(sock,SIOCSIFFLAGS,(void *)&ifr) < 0) {
  151. ::close(_fd);
  152. ::close(sock);
  153. throw std::runtime_error("unable to set TAP interface flags");
  154. }
  155. ::close(sock);
  156. ::pipe(_shutdownSignalPipe);
  157. TRACE("tap %s created",_dev);
  158. _thread = Thread::start(this);
  159. }
  160. #endif // __LINUX__
  161. #ifdef __APPLE__
  162. EthernetTap::EthernetTap(
  163. const RuntimeEnvironment *renv,
  164. const MAC &mac,
  165. unsigned int mtu,
  166. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  167. void *arg)
  168. throw(std::runtime_error) :
  169. _mac(mac),
  170. _mtu(mtu),
  171. _r(renv),
  172. _handler(handler),
  173. _arg(arg),
  174. _fd(0)
  175. {
  176. char devpath[64],ethaddr[64],mtustr[16];
  177. struct stat tmp;
  178. Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
  179. if (mtu > 4096)
  180. throw std::runtime_error("max tap MTU is 4096");
  181. // Check for existence of ZT tap devices, try to load module if not there
  182. if (stat("/dev/zt0",&tmp)) {
  183. int kextpid;
  184. char tmp[4096];
  185. strcpy(tmp,_r->homePath.c_str());
  186. if ((kextpid = (int)vfork()) == 0) {
  187. chdir(tmp);
  188. execl(ZT_MAC_KEXTLOAD,ZT_MAC_KEXTLOAD,"-q","-repository",tmp,"tap.kext",(const char *)0);
  189. exit(-1);
  190. } else {
  191. int exitcode = -1;
  192. waitpid(kextpid,&exitcode,0);
  193. usleep(500);
  194. }
  195. }
  196. if (stat("/dev/zt0",&tmp))
  197. throw std::runtime_error("/dev/zt# tap devices do not exist and unable to load kernel extension");
  198. // Open the first available device (ones in use will fail with resource busy)
  199. for(int i=0;i<256;++i) {
  200. sprintf(devpath,"/dev/zt%d",i);
  201. if (stat(devpath,&tmp))
  202. throw std::runtime_error("no more TAP devices available");
  203. _fd = ::open(devpath,O_RDWR);
  204. if (_fd > 0) {
  205. sprintf(_dev,"zt%d",i);
  206. break;
  207. }
  208. }
  209. if (_fd <= 0)
  210. throw std::runtime_error("unable to open TAP device or no more devices available");
  211. if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
  212. ::close(_fd);
  213. throw std::runtime_error("unable to set flags on file descriptor for TAP device");
  214. }
  215. sprintf(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]);
  216. sprintf(mtustr,"%u",mtu);
  217. // Configure MAC address and MTU, bring interface up
  218. long cpid;
  219. if ((cpid = (long)vfork()) == 0) {
  220. execl(ZT_ETHERTAP_IFCONFIG,ZT_ETHERTAP_IFCONFIG,_dev,"lladdr",ethaddr,"mtu",mtustr,"up",(const char *)0);
  221. exit(-1);
  222. } else {
  223. int exitcode = -1;
  224. waitpid(cpid,&exitcode,0);
  225. if (exitcode) {
  226. ::close(_fd);
  227. throw std::runtime_error("ifconfig failure setting link-layer address and activating tap interface");
  228. }
  229. }
  230. whack(); // turns on IPv6 on OSX
  231. ::pipe(_shutdownSignalPipe);
  232. _thread = Thread::start(this);
  233. }
  234. #endif // __APPLE__
  235. EthernetTap::~EthernetTap()
  236. {
  237. ::write(_shutdownSignalPipe[1],"\0",1); // causes thread to exit
  238. Thread::join(_thread);
  239. ::close(_fd);
  240. }
  241. #ifdef __APPLE__
  242. void EthernetTap::whack()
  243. {
  244. long cpid = (long)vfork();
  245. if (cpid == 0) {
  246. execl(ZT_MAC_IPCONFIG,ZT_MAC_IPCONFIG,"set",_dev,"AUTOMATIC-V6",(const char *)0);
  247. exit(-1);
  248. } else {
  249. int exitcode = -1;
  250. waitpid(cpid,&exitcode,0);
  251. if (exitcode) {
  252. LOG("%s: ipconfig set AUTOMATIC-V6 failed",_dev);
  253. }
  254. }
  255. }
  256. #else
  257. void EthernetTap::whack() {}
  258. #endif // __APPLE__ / !__APPLE__
  259. #ifdef __LINUX__
  260. static bool ___removeIp(const char *_dev,const InetAddress &ip)
  261. {
  262. long cpid = (long)vfork();
  263. if (cpid == 0) {
  264. execl(ZT_ETHERTAP_IP_COMMAND,ZT_ETHERTAP_IP_COMMAND,"addr","del",ip.toString().c_str(),"dev",_dev,(const char *)0);
  265. exit(1); /* not reached unless exec fails */
  266. } else {
  267. int exitcode = 1;
  268. waitpid(cpid,&exitcode,0);
  269. return (exitcode == 0);
  270. }
  271. }
  272. bool EthernetTap::addIP(const InetAddress &ip)
  273. {
  274. Mutex::Lock _l(_ips_m);
  275. if (!ip)
  276. return false;
  277. if (_ips.count(ip) > 0)
  278. return true;
  279. // Remove and reconfigure if address is the same but netmask is different
  280. for(std::set<InetAddress>::iterator i(_ips.begin());i!=_ips.end();++i) {
  281. if (i->ipsEqual(ip)) {
  282. if (___removeIp(_dev,*i)) {
  283. _ips.erase(i);
  284. break;
  285. } else {
  286. LOG("WARNING: failed to remove old IP/netmask %s to replace with %s",i->toString().c_str(),ip.toString().c_str());
  287. }
  288. }
  289. }
  290. long cpid;
  291. if ((cpid = (long)vfork()) == 0) {
  292. execl(ZT_ETHERTAP_IP_COMMAND,ZT_ETHERTAP_IP_COMMAND,"addr","add",ip.toString().c_str(),"dev",_dev,(const char *)0);
  293. exit(-1);
  294. } else {
  295. int exitcode = -1;
  296. waitpid(cpid,&exitcode,0);
  297. if (exitcode == 0) {
  298. _ips.insert(ip);
  299. return true;
  300. } else return false;
  301. }
  302. return false;
  303. }
  304. #endif // __LINUX__
  305. #ifdef __APPLE__
  306. static bool ___removeIp(const char *_dev,const InetAddress &ip)
  307. {
  308. int cpid;
  309. if ((cpid = (int)vfork()) == 0) {
  310. execl(ZT_ETHERTAP_IFCONFIG,ZT_ETHERTAP_IFCONFIG,_dev,"inet",ip.toIpString().c_str(),"-alias",(const char *)0);
  311. exit(-1);
  312. } else {
  313. int exitcode = -1;
  314. waitpid(cpid,&exitcode,0);
  315. return (exitcode == 0);
  316. }
  317. return false; // never reached, make compiler shut up about return value
  318. }
  319. bool EthernetTap::addIP(const InetAddress &ip)
  320. {
  321. Mutex::Lock _l(_ips_m);
  322. if (!ip)
  323. return false;
  324. if (_ips.count(ip) > 0)
  325. return true; // IP/netmask already assigned
  326. // Remove and reconfigure if address is the same but netmask is different
  327. for(std::set<InetAddress>::iterator i(_ips.begin());i!=_ips.end();++i) {
  328. if ((i->ipsEqual(ip))&&(i->netmaskBits() != ip.netmaskBits())) {
  329. if (___removeIp(_dev,*i)) {
  330. _ips.erase(i);
  331. break;
  332. } else {
  333. LOG("WARNING: failed to remove old IP/netmask %s to replace with %s",i->toString().c_str(),ip.toString().c_str());
  334. }
  335. }
  336. }
  337. int cpid;
  338. if ((cpid = (int)vfork()) == 0) {
  339. execl(ZT_ETHERTAP_IFCONFIG,ZT_ETHERTAP_IFCONFIG,_dev,ip.isV4() ? "inet" : "inet6",ip.toString().c_str(),"alias",(const char *)0);
  340. exit(-1);
  341. } else {
  342. int exitcode = -1;
  343. waitpid(cpid,&exitcode,0);
  344. if (exitcode == 0) {
  345. _ips.insert(ip);
  346. return true;
  347. }
  348. }
  349. return false;
  350. }
  351. #endif // __APPLE__
  352. bool EthernetTap::removeIP(const InetAddress &ip)
  353. {
  354. Mutex::Lock _l(_ips_m);
  355. if (_ips.count(ip) > 0) {
  356. if (___removeIp(_dev,ip)) {
  357. _ips.erase(ip);
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. void EthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  364. {
  365. char putBuf[4096 + 14];
  366. if ((_fd > 0)&&(len <= _mtu)) {
  367. for(int i=0;i<6;++i)
  368. putBuf[i] = to.data[i];
  369. for(int i=0;i<6;++i)
  370. putBuf[i+6] = from.data[i];
  371. *((uint16_t *)(putBuf + 12)) = htons((uint16_t)etherType);
  372. memcpy(putBuf + 14,data,len);
  373. len += 14;
  374. int n = ::write(_fd,putBuf,len);
  375. if (n <= 0) {
  376. LOG("error writing packet to Ethernet tap device: %s",strerror(errno));
  377. } else if (n != (int)len) {
  378. // Saw this gremlin once, so log it if we see it again... OSX tap
  379. // or something seems to have goofy issues with certain MTUs.
  380. LOG("ERROR: write underrun: %s tap write() wrote %d of %u bytes of frame",_dev,n,len);
  381. }
  382. }
  383. }
  384. std::string EthernetTap::deviceName() const
  385. {
  386. return std::string(_dev);
  387. }
  388. #ifdef __LINUX__
  389. bool EthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  390. {
  391. char *ptr,*ptr2;
  392. unsigned char mac[6];
  393. std::set<MulticastGroup> newGroups;
  394. int fd = ::open("/proc/net/dev_mcast",O_RDONLY);
  395. if (fd > 0) {
  396. char buf[131072];
  397. int n = (int)::read(fd,buf,sizeof(buf));
  398. if ((n > 0)&&(n < (int)sizeof(buf))) {
  399. buf[n] = (char)0;
  400. for(char *l=strtok_r(buf,"\r\n",&ptr);(l);l=strtok_r((char *)0,"\r\n",&ptr)) {
  401. int fno = 0;
  402. char *devname = (char *)0;
  403. char *mcastmac = (char *)0;
  404. for(char *f=strtok_r(l," \t",&ptr2);(f);f=strtok_r((char *)0," \t",&ptr2)) {
  405. if (fno == 1)
  406. devname = f;
  407. else if (fno == 4)
  408. mcastmac = f;
  409. ++fno;
  410. }
  411. if ((devname)&&(!strcmp(devname,_dev))&&(mcastmac)&&(Utils::unhex(mcastmac,mac,6) == 6))
  412. newGroups.insert(MulticastGroup(MAC(mac),0));
  413. }
  414. }
  415. ::close(fd);
  416. }
  417. {
  418. Mutex::Lock _l(_ips_m);
  419. for(std::set<InetAddress>::const_iterator i(_ips.begin());i!=_ips.end();++i)
  420. newGroups.insert(MulticastGroup::deriveMulticastGroupForAddressResolution(*i));
  421. }
  422. bool changed = false;
  423. newGroups.insert(_blindWildcardMulticastGroup); // always join this
  424. for(std::set<MulticastGroup>::iterator mg(newGroups.begin());mg!=newGroups.end();++mg) {
  425. if (!groups.count(*mg)) {
  426. groups.insert(*mg);
  427. changed = true;
  428. }
  429. }
  430. for(std::set<MulticastGroup>::iterator mg(groups.begin());mg!=groups.end();) {
  431. if (!newGroups.count(*mg)) {
  432. groups.erase(mg++);
  433. changed = true;
  434. } else ++mg;
  435. }
  436. return changed;
  437. }
  438. #endif // __LINUX__
  439. #ifdef __APPLE__
  440. bool EthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  441. {
  442. std::set<MulticastGroup> newGroups;
  443. struct ifmaddrs *ifmap = (struct ifmaddrs *)0;
  444. if (!getifmaddrs(&ifmap)) {
  445. struct ifmaddrs *p = ifmap;
  446. while (p) {
  447. if (p->ifma_addr->sa_family == AF_LINK) {
  448. struct sockaddr_dl *in = (struct sockaddr_dl *)p->ifma_name;
  449. struct sockaddr_dl *la = (struct sockaddr_dl *)p->ifma_addr;
  450. if ((la->sdl_alen == 6)&&(in->sdl_nlen <= sizeof(_dev))&&(!memcmp(_dev,in->sdl_data,in->sdl_nlen)))
  451. newGroups.insert(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen),0));
  452. }
  453. p = p->ifma_next;
  454. }
  455. freeifmaddrs(ifmap);
  456. }
  457. {
  458. Mutex::Lock _l(_ips_m);
  459. for(std::set<InetAddress>::const_iterator i(_ips.begin());i!=_ips.end();++i)
  460. newGroups.insert(MulticastGroup::deriveMulticastGroupForAddressResolution(*i));
  461. }
  462. bool changed = false;
  463. newGroups.insert(_blindWildcardMulticastGroup); // always join this
  464. for(std::set<MulticastGroup>::iterator mg(newGroups.begin());mg!=newGroups.end();++mg) {
  465. if (!groups.count(*mg)) {
  466. groups.insert(*mg);
  467. changed = true;
  468. }
  469. }
  470. for(std::set<MulticastGroup>::iterator mg(groups.begin());mg!=groups.end();) {
  471. if (!newGroups.count(*mg)) {
  472. groups.erase(mg++);
  473. changed = true;
  474. } else ++mg;
  475. }
  476. return changed;
  477. }
  478. #endif // __APPLE__
  479. void EthernetTap::threadMain()
  480. throw()
  481. {
  482. fd_set readfds,nullfds;
  483. MAC to,from;
  484. char getBuf[4096 + 14];
  485. Buffer<4096> data;
  486. FD_ZERO(&readfds);
  487. FD_ZERO(&nullfds);
  488. int nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1;
  489. for(;;) {
  490. FD_SET(_shutdownSignalPipe[0],&readfds);
  491. FD_SET(_fd,&readfds);
  492. select(nfds,&readfds,&nullfds,&nullfds,(struct timeval *)0);
  493. if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) // writes to shutdown pipe terminate thread
  494. break;
  495. if (FD_ISSET(_fd,&readfds)) {
  496. int n = (int)::read(_fd,getBuf,_mtu + 14);
  497. if (n > 14) {
  498. for(int i=0;i<6;++i)
  499. to.data[i] = (unsigned char)getBuf[i];
  500. for(int i=0;i<6;++i)
  501. from.data[i] = (unsigned char)getBuf[i + 6];
  502. data.copyFrom(getBuf + 14,(unsigned int)n - 14);
  503. _handler(_arg,from,to,ntohs(((const uint16_t *)getBuf)[6]),data);
  504. } else if (n < 0) {
  505. if ((errno != EINTR)&&(errno != ETIMEDOUT)) {
  506. TRACE("unexpected error reading from tap: %s",strerror(errno));
  507. break;
  508. }
  509. }
  510. }
  511. }
  512. }
  513. } // namespace ZeroTier
  514. #endif // __UNIX_LIKE__ //////////////////////////////////////////////////////
  515. #ifdef __WINDOWS__
  516. // TODO
  517. #endif // __WINDOWS__