LinuxEthernetTap.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <signal.h>
  33. #include <fcntl.h>
  34. #include <errno.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/wait.h>
  39. #include <sys/select.h>
  40. #include <netinet/in.h>
  41. #include <net/if_arp.h>
  42. #include <arpa/inet.h>
  43. #include <linux/if.h>
  44. #include <linux/if_tun.h>
  45. #include <linux/if_addr.h>
  46. #include <linux/if_ether.h>
  47. #include <ifaddrs.h>
  48. #include <string>
  49. #include <map>
  50. #include <set>
  51. #include <algorithm>
  52. #include "../node/Constants.hpp"
  53. #include "../node/Utils.hpp"
  54. #include "../node/Mutex.hpp"
  55. #include "LinuxEthernetTap.hpp"
  56. // ff:ff:ff:ff:ff:ff with no ADI
  57. static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff),0);
  58. namespace ZeroTier {
  59. static Mutex __tapCreateLock;
  60. LinuxEthernetTap::LinuxEthernetTap(
  61. const MAC &mac,
  62. unsigned int mtu,
  63. unsigned int metric,
  64. uint64_t nwid,
  65. const char *desiredDevice,
  66. const char *friendlyName,
  67. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  68. void *arg) :
  69. EthernetTap("LinuxEthernetTap",mac,mtu,metric),
  70. _handler(handler),
  71. _arg(arg),
  72. _fd(0),
  73. _enabled(true)
  74. {
  75. char procpath[128];
  76. struct stat sbuf;
  77. Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
  78. if (mtu > 2800)
  79. throw std::runtime_error("max tap MTU is 2800");
  80. _fd = ::open("/dev/net/tun",O_RDWR);
  81. if (_fd <= 0)
  82. throw std::runtime_error(std::string("could not open TUN/TAP device: ") + strerror(errno));
  83. struct ifreq ifr;
  84. memset(&ifr,0,sizeof(ifr));
  85. // Try to recall our last device name, or pick an unused one if that fails.
  86. bool recalledDevice = false;
  87. if ((desiredDevice)&&(desiredDevice[0])) {
  88. Utils::scopy(ifr.ifr_name,sizeof(ifr.ifr_name),desiredDevice);
  89. Utils::snprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name);
  90. recalledDevice = (stat(procpath,&sbuf) != 0);
  91. }
  92. if (!recalledDevice) {
  93. int devno = 0;
  94. do {
  95. Utils::snprintf(ifr.ifr_name,sizeof(ifr.ifr_name),"zt%d",devno++);
  96. Utils::snprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name);
  97. } while (stat(procpath,&sbuf) == 0); // try zt#++ until we find one that does not exist
  98. }
  99. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  100. if (ioctl(_fd,TUNSETIFF,(void *)&ifr) < 0) {
  101. ::close(_fd);
  102. throw std::runtime_error("unable to configure TUN/TAP device for TAP operation");
  103. }
  104. _dev = ifr.ifr_name;
  105. ::ioctl(_fd,TUNSETPERSIST,0); // valgrind may generate a false alarm here
  106. // Open an arbitrary socket to talk to netlink
  107. int sock = socket(AF_INET,SOCK_DGRAM,0);
  108. if (sock <= 0) {
  109. ::close(_fd);
  110. throw std::runtime_error("unable to open netlink socket");
  111. }
  112. // Set MAC address
  113. ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  114. mac.copyTo(ifr.ifr_ifru.ifru_hwaddr.sa_data,6);
  115. if (ioctl(sock,SIOCSIFHWADDR,(void *)&ifr) < 0) {
  116. ::close(_fd);
  117. ::close(sock);
  118. throw std::runtime_error("unable to configure TAP hardware (MAC) address");
  119. return;
  120. }
  121. // Set MTU
  122. ifr.ifr_ifru.ifru_mtu = (int)mtu;
  123. if (ioctl(sock,SIOCSIFMTU,(void *)&ifr) < 0) {
  124. ::close(_fd);
  125. ::close(sock);
  126. throw std::runtime_error("unable to configure TAP MTU");
  127. }
  128. if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
  129. ::close(_fd);
  130. throw std::runtime_error("unable to set flags on file descriptor for TAP device");
  131. }
  132. /* Bring interface up */
  133. if (ioctl(sock,SIOCGIFFLAGS,(void *)&ifr) < 0) {
  134. ::close(_fd);
  135. ::close(sock);
  136. throw std::runtime_error("unable to get TAP interface flags");
  137. }
  138. ifr.ifr_flags |= IFF_UP;
  139. if (ioctl(sock,SIOCSIFFLAGS,(void *)&ifr) < 0) {
  140. ::close(_fd);
  141. ::close(sock);
  142. throw std::runtime_error("unable to set TAP interface flags");
  143. }
  144. ::close(sock);
  145. // Set close-on-exec so that devices cannot persist if we fork/exec for update
  146. ::fcntl(_fd,F_SETFD,fcntl(_fd,F_GETFD) | FD_CLOEXEC);
  147. ::pipe(_shutdownSignalPipe);
  148. _thread = Thread::start(this);
  149. }
  150. LinuxEthernetTap::~LinuxEthernetTap()
  151. {
  152. ::write(_shutdownSignalPipe[1],"\0",1); // causes thread to exit
  153. Thread::join(_thread);
  154. ::close(_fd);
  155. ::close(_shutdownSignalPipe[0]);
  156. ::close(_shutdownSignalPipe[1]);
  157. }
  158. void LinuxEthernetTap::setEnabled(bool en)
  159. {
  160. _enabled = en;
  161. // TODO: interface status change
  162. }
  163. bool LinuxEthernetTap::enabled() const
  164. {
  165. return _enabled;
  166. }
  167. static bool ___removeIp(const std::string &_dev,const InetAddress &ip)
  168. {
  169. long cpid = (long)vfork();
  170. if (cpid == 0) {
  171. Utils::redirectUnixOutputs("/dev/null",(const char *)0);
  172. ::execl("/sbin/ip","/sbin/ip","addr","del",ip.toString().c_str(),"dev",_dev.c_str(),(const char *)0);
  173. ::execl("/usr/sbin/ip","/usr/sbin/ip","addr","del",ip.toString().c_str(),"dev",_dev.c_str(),(const char *)0);
  174. ::_exit(-1);
  175. } else {
  176. int exitcode = -1;
  177. ::waitpid(cpid,&exitcode,0);
  178. return (exitcode == 0);
  179. }
  180. }
  181. bool LinuxEthernetTap::addIP(const InetAddress &ip)
  182. {
  183. if (!ip)
  184. return false;
  185. std::set<InetAddress> allIps(ips());
  186. if (allIps.count(ip) > 0)
  187. return true;
  188. // Remove and reconfigure if address is the same but netmask is different
  189. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i) {
  190. if (i->ipsEqual(ip))
  191. ___removeIp(_dev,*i);
  192. }
  193. long cpid = (long)vfork();
  194. if (cpid == 0) {
  195. Utils::redirectUnixOutputs("/dev/null",(const char *)0);
  196. ::execl("/sbin/ip","/sbin/ip","addr","add",ip.toString().c_str(),"dev",_dev.c_str(),(const char *)0);
  197. ::execl("/usr/sbin/ip","/usr/sbin/ip","addr","add",ip.toString().c_str(),"dev",_dev.c_str(),(const char *)0);
  198. ::_exit(-1);
  199. } else if (cpid > 0) {
  200. int exitcode = -1;
  201. ::waitpid(cpid,&exitcode,0);
  202. return (exitcode == 0);
  203. }
  204. return false;
  205. }
  206. bool LinuxEthernetTap::removeIP(const InetAddress &ip)
  207. {
  208. if (ips().count(ip) > 0) {
  209. if (___removeIp(_dev,ip))
  210. return true;
  211. }
  212. return false;
  213. }
  214. std::set<InetAddress> LinuxEthernetTap::ips() const
  215. {
  216. struct ifaddrs *ifa = (struct ifaddrs *)0;
  217. if (getifaddrs(&ifa))
  218. return std::set<InetAddress>();
  219. std::set<InetAddress> r;
  220. struct ifaddrs *p = ifa;
  221. while (p) {
  222. if ((!strcmp(p->ifa_name,_dev.c_str()))&&(p->ifa_addr)&&(p->ifa_netmask)&&(p->ifa_addr->sa_family == p->ifa_netmask->sa_family)) {
  223. switch(p->ifa_addr->sa_family) {
  224. case AF_INET: {
  225. struct sockaddr_in *sin = (struct sockaddr_in *)p->ifa_addr;
  226. struct sockaddr_in *nm = (struct sockaddr_in *)p->ifa_netmask;
  227. r.insert(InetAddress(&(sin->sin_addr.s_addr),4,Utils::countBits((uint32_t)nm->sin_addr.s_addr)));
  228. } break;
  229. case AF_INET6: {
  230. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)p->ifa_addr;
  231. struct sockaddr_in6 *nm = (struct sockaddr_in6 *)p->ifa_netmask;
  232. uint32_t b[4];
  233. memcpy(b,nm->sin6_addr.s6_addr,sizeof(b));
  234. r.insert(InetAddress(sin->sin6_addr.s6_addr,16,Utils::countBits(b[0]) + Utils::countBits(b[1]) + Utils::countBits(b[2]) + Utils::countBits(b[3])));
  235. } break;
  236. }
  237. }
  238. p = p->ifa_next;
  239. }
  240. if (ifa)
  241. freeifaddrs(ifa);
  242. return r;
  243. }
  244. void LinuxEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  245. {
  246. char putBuf[8194];
  247. if ((_fd > 0)&&(len <= _mtu)&&(_enabled)) {
  248. to.copyTo(putBuf,6);
  249. from.copyTo(putBuf + 6,6);
  250. *((uint16_t *)(putBuf + 12)) = htons((uint16_t)etherType);
  251. memcpy(putBuf + 14,data,len);
  252. len += 14;
  253. ::write(_fd,putBuf,len);
  254. }
  255. }
  256. std::string LinuxEthernetTap::deviceName() const
  257. {
  258. return _dev;
  259. }
  260. void LinuxEthernetTap::setFriendlyName(const char *friendlyName)
  261. {
  262. }
  263. bool LinuxEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  264. {
  265. char *ptr,*ptr2;
  266. unsigned char mac[6];
  267. std::set<MulticastGroup> newGroups;
  268. int fd = ::open("/proc/net/dev_mcast",O_RDONLY);
  269. if (fd > 0) {
  270. char buf[131072];
  271. int n = (int)::read(fd,buf,sizeof(buf));
  272. if ((n > 0)&&(n < (int)sizeof(buf))) {
  273. buf[n] = (char)0;
  274. for(char *l=strtok_r(buf,"\r\n",&ptr);(l);l=strtok_r((char *)0,"\r\n",&ptr)) {
  275. int fno = 0;
  276. char *devname = (char *)0;
  277. char *mcastmac = (char *)0;
  278. for(char *f=strtok_r(l," \t",&ptr2);(f);f=strtok_r((char *)0," \t",&ptr2)) {
  279. if (fno == 1)
  280. devname = f;
  281. else if (fno == 4)
  282. mcastmac = f;
  283. ++fno;
  284. }
  285. if ((devname)&&(!strcmp(devname,_dev.c_str()))&&(mcastmac)&&(Utils::unhex(mcastmac,mac,6) == 6))
  286. newGroups.insert(MulticastGroup(MAC(mac,6),0));
  287. }
  288. }
  289. ::close(fd);
  290. }
  291. {
  292. std::set<InetAddress> allIps(ips());
  293. for(std::set<InetAddress>::const_iterator i(allIps.begin());i!=allIps.end();++i)
  294. newGroups.insert(MulticastGroup::deriveMulticastGroupForAddressResolution(*i));
  295. }
  296. bool changed = false;
  297. for(std::set<MulticastGroup>::iterator mg(newGroups.begin());mg!=newGroups.end();++mg) {
  298. if (!groups.count(*mg)) {
  299. groups.insert(*mg);
  300. changed = true;
  301. }
  302. }
  303. for(std::set<MulticastGroup>::iterator mg(groups.begin());mg!=groups.end();) {
  304. if ((!newGroups.count(*mg))&&(*mg != _blindWildcardMulticastGroup)) {
  305. groups.erase(mg++);
  306. changed = true;
  307. } else ++mg;
  308. }
  309. return changed;
  310. }
  311. void LinuxEthernetTap::threadMain()
  312. throw()
  313. {
  314. fd_set readfds,nullfds;
  315. MAC to,from;
  316. int n,nfds,r;
  317. char getBuf[8194];
  318. Buffer<4096> data;
  319. // Wait for a moment after startup -- wait for Network to finish
  320. // constructing itself.
  321. Thread::sleep(500);
  322. FD_ZERO(&readfds);
  323. FD_ZERO(&nullfds);
  324. nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1;
  325. r = 0;
  326. for(;;) {
  327. FD_SET(_shutdownSignalPipe[0],&readfds);
  328. FD_SET(_fd,&readfds);
  329. select(nfds,&readfds,&nullfds,&nullfds,(struct timeval *)0);
  330. if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) // writes to shutdown pipe terminate thread
  331. break;
  332. if (FD_ISSET(_fd,&readfds)) {
  333. n = (int)::read(_fd,getBuf + r,sizeof(getBuf) - r);
  334. if (n < 0) {
  335. if ((errno != EINTR)&&(errno != ETIMEDOUT))
  336. break;
  337. } else {
  338. // Some tap drivers like to send the ethernet frame and the
  339. // payload in two chunks, so handle that by accumulating
  340. // data until we have at least a frame.
  341. r += n;
  342. if (r > 14) {
  343. if (r > ((int)_mtu + 14)) // sanity check for weird TAP behavior on some platforms
  344. r = _mtu + 14;
  345. if (_enabled) {
  346. to.setTo(getBuf,6);
  347. from.setTo(getBuf + 6,6);
  348. unsigned int etherType = ntohs(((const uint16_t *)getBuf)[6]);
  349. data.copyFrom(getBuf + 14,(unsigned int)r - 14);
  350. _handler(_arg,from,to,etherType,data);
  351. }
  352. r = 0;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. } // namespace ZeroTier