2
0

LinuxEthernetTap.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifdef __GNUC__
  14. #pragma GCC diagnostic ignored "-Wrestrict"
  15. #endif
  16. #include "../node/Constants.hpp"
  17. #include "../node/Metrics.hpp"
  18. #ifdef __LINUX__
  19. #include "../node/Dictionary.hpp"
  20. #include "../node/Mutex.hpp"
  21. #include "../node/Utils.hpp"
  22. #include "LinuxEthernetTap.hpp"
  23. #include "LinuxNetLink.hpp"
  24. #include "OSUtils.hpp"
  25. #include <algorithm>
  26. #include <arpa/inet.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <ifaddrs.h>
  31. #include <linux/if.h>
  32. #include <linux/if_addr.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/if_tun.h>
  35. #include <net/if_arp.h>
  36. #include <netinet/in.h>
  37. #include <signal.h>
  38. #include <stdint.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <string>
  43. #include <sys/ioctl.h>
  44. #include <sys/select.h>
  45. #include <sys/stat.h>
  46. #include <sys/types.h>
  47. #include <sys/utsname.h>
  48. #include <sys/wait.h>
  49. #include <unistd.h>
  50. #include <utility>
  51. #ifndef IFNAMSIZ
  52. #define IFNAMSIZ 16
  53. #endif
  54. #define ZT_TAP_BUF_SIZE (1024 * 16)
  55. // ff:ff:ff:ff:ff:ff with no ADI
  56. static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff), 0);
  57. namespace ZeroTier {
  58. // determine if we're running a really old linux kernel.
  59. // Kernels in the 2.6.x series don't behave the same when bringing up
  60. // the tap devices.
  61. //
  62. // Returns true if the kernel major version is < 3
  63. bool isOldLinuxKernel()
  64. {
  65. struct utsname buffer;
  66. char* p;
  67. long ver[16];
  68. int i = 0;
  69. if (uname(&buffer) != 0) {
  70. perror("uname");
  71. exit(EXIT_FAILURE);
  72. }
  73. p = buffer.release;
  74. while (*p) {
  75. if (isdigit(*p)) {
  76. ver[i] = strtol(p, &p, 10);
  77. i++;
  78. }
  79. else {
  80. p++;
  81. }
  82. }
  83. return ver[0] < 3;
  84. }
  85. static const char _base32_chars[32] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7' };
  86. static void _base32_5_to_8(const uint8_t* in, char* out)
  87. {
  88. out[0] = _base32_chars[(in[0]) >> 3];
  89. out[1] = _base32_chars[(in[0] & 0x07) << 2 | (in[1] & 0xc0) >> 6];
  90. out[2] = _base32_chars[(in[1] & 0x3e) >> 1];
  91. out[3] = _base32_chars[(in[1] & 0x01) << 4 | (in[2] & 0xf0) >> 4];
  92. out[4] = _base32_chars[(in[2] & 0x0f) << 1 | (in[3] & 0x80) >> 7];
  93. out[5] = _base32_chars[(in[3] & 0x7c) >> 2];
  94. out[6] = _base32_chars[(in[3] & 0x03) << 3 | (in[4] & 0xe0) >> 5];
  95. out[7] = _base32_chars[(in[4] & 0x1f)];
  96. }
  97. LinuxEthernetTap::LinuxEthernetTap(
  98. const char* homePath,
  99. unsigned int concurrency,
  100. bool pinning,
  101. const MAC& mac,
  102. unsigned int mtu,
  103. unsigned int metric,
  104. uint64_t nwid,
  105. const char* friendlyName,
  106. void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
  107. void* arg)
  108. : _handler(handler)
  109. , _arg(arg)
  110. , _nwid(nwid)
  111. , _mac(mac)
  112. , _homePath(homePath)
  113. , _mtu(mtu)
  114. , _fd(0)
  115. , _enabled(true)
  116. , _run(true)
  117. , _lastIfAddrsUpdate(0)
  118. {
  119. static std::mutex s_tapCreateLock;
  120. char procpath[128], nwids[32];
  121. struct stat sbuf;
  122. // Create only one tap at a time globally.
  123. std::lock_guard<std::mutex> tapCreateLock(s_tapCreateLock);
  124. // Make sure Linux netlink is initialized.
  125. (void)LinuxNetLink::getInstance();
  126. OSUtils::ztsnprintf(nwids, sizeof(nwids), "%.16llx", nwid);
  127. _fd = ::open("/dev/net/tun", O_RDWR);
  128. if (_fd <= 0) {
  129. _fd = ::open("/dev/tun", O_RDWR);
  130. if (_fd <= 0)
  131. throw std::runtime_error(std::string("could not open TUN/TAP device: ") + strerror(errno));
  132. }
  133. struct ifreq ifr;
  134. memset(&ifr, 0, sizeof(ifr));
  135. // Restore device names from legacy devicemap, but for new devices we use a base32-based
  136. // canonical device name.
  137. std::map<std::string, std::string> globalDeviceMap;
  138. FILE* devmapf = fopen((_homePath + ZT_PATH_SEPARATOR_S + "devicemap").c_str(), "r");
  139. if (devmapf) {
  140. char buf[256];
  141. while (fgets(buf, sizeof(buf), devmapf)) {
  142. char* x = (char*)0;
  143. char* y = (char*)0;
  144. char* saveptr = (char*)0;
  145. for (char* f = Utils::stok(buf, "\r\n=", &saveptr); (f); f = Utils::stok((char*)0, "\r\n=", &saveptr)) {
  146. if (! x)
  147. x = f;
  148. else if (! y)
  149. y = f;
  150. else
  151. break;
  152. }
  153. if ((x) && (y) && (x[0]) && (y[0]))
  154. globalDeviceMap[x] = y;
  155. }
  156. fclose(devmapf);
  157. }
  158. bool recalledDevice = false;
  159. std::map<std::string, std::string>::const_iterator gdmEntry = globalDeviceMap.find(nwids);
  160. if (gdmEntry != globalDeviceMap.end()) {
  161. Utils::scopy(ifr.ifr_name, sizeof(ifr.ifr_name), gdmEntry->second.c_str());
  162. OSUtils::ztsnprintf(procpath, sizeof(procpath), "/proc/sys/net/ipv4/conf/%s", ifr.ifr_name);
  163. recalledDevice = (stat(procpath, &sbuf) != 0);
  164. }
  165. if (! recalledDevice) {
  166. #ifdef __SYNOLOGY__
  167. int devno = 50;
  168. do {
  169. OSUtils::ztsnprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth%d", devno++);
  170. OSUtils::ztsnprintf(procpath, sizeof(procpath), "/proc/sys/net/ipv4/conf/%s", ifr.ifr_name);
  171. } while (stat(procpath, &sbuf) == 0); // try zt#++ until we find one that does not exist
  172. #else
  173. uint64_t trial = 0; // incremented in the very unlikely event of a name collision with another network
  174. do {
  175. const uint64_t nwid40 = (nwid ^ (nwid >> 24)) + trial++;
  176. uint8_t tmp2[5];
  177. char tmp3[11];
  178. tmp2[0] = (uint8_t)((nwid40 >> 32) & 0xff);
  179. tmp2[1] = (uint8_t)((nwid40 >> 24) & 0xff);
  180. tmp2[2] = (uint8_t)((nwid40 >> 16) & 0xff);
  181. tmp2[3] = (uint8_t)((nwid40 >> 8) & 0xff);
  182. tmp2[4] = (uint8_t)(nwid40 & 0xff);
  183. tmp3[0] = 'z';
  184. tmp3[1] = 't';
  185. _base32_5_to_8(tmp2, tmp3 + 2);
  186. tmp3[10] = (char)0;
  187. memcpy(ifr.ifr_name, tmp3, 11);
  188. OSUtils::ztsnprintf(procpath, sizeof(procpath), "/proc/sys/net/ipv4/conf/%s", ifr.ifr_name);
  189. } while (stat(procpath, &sbuf) == 0);
  190. #endif
  191. }
  192. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  193. if (ioctl(_fd, TUNSETIFF, (void*)&ifr) < 0) {
  194. ::close(_fd);
  195. throw std::runtime_error("unable to configure TUN/TAP device for TAP operation");
  196. }
  197. ::ioctl(_fd, TUNSETPERSIST, 0); // valgrind may generate a false alarm here
  198. _dev = ifr.ifr_name;
  199. ::fcntl(_fd, F_SETFD, fcntl(_fd, F_GETFD) | FD_CLOEXEC);
  200. (void)::pipe(_shutdownSignalPipe);
  201. for (unsigned int i = 0; i < concurrency; ++i) {
  202. _rxThreads.push_back(std::thread([this, i, concurrency, pinning] {
  203. if (pinning) {
  204. int pinCore = i % concurrency;
  205. fprintf(stderr, "Pinning tap thread %d to core %d\n", i, pinCore);
  206. pthread_t self = pthread_self();
  207. cpu_set_t cpuset;
  208. CPU_ZERO(&cpuset);
  209. CPU_SET(pinCore, &cpuset);
  210. int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset);
  211. if (rc != 0) {
  212. fprintf(stderr, "Failed to pin tap thread %d to core %d: %s\n", i, pinCore, strerror(errno));
  213. exit(1);
  214. }
  215. }
  216. uint8_t b[ZT_TAP_BUF_SIZE];
  217. fd_set readfds, nullfds;
  218. int n, nfds, r;
  219. if (i == 0) {
  220. struct ifreq ifr;
  221. memset(&ifr, 0, sizeof(ifr));
  222. strcpy(ifr.ifr_name, _dev.c_str());
  223. const int sock = socket(AF_INET, SOCK_DGRAM, 0);
  224. if (sock <= 0)
  225. return;
  226. if (ioctl(sock, SIOCGIFFLAGS, (void*)&ifr) < 0) {
  227. ::close(sock);
  228. printf("WARNING: ioctl() failed setting up Linux tap device (bring interface up)\n");
  229. return;
  230. }
  231. ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  232. _mac.copyTo(ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
  233. if (ioctl(sock, SIOCSIFHWADDR, (void*)&ifr) < 0) {
  234. ::close(sock);
  235. printf("WARNING: ioctl() failed setting up Linux tap device (set MAC)\n");
  236. return;
  237. }
  238. usleep(100000);
  239. if (isOldLinuxKernel()) {
  240. ifr.ifr_ifru.ifru_mtu = (int)_mtu;
  241. if (ioctl(sock, SIOCSIFMTU, (void*)&ifr) < 0) {
  242. ::close(sock);
  243. printf("WARNING: ioctl() failed setting up Linux tap device (set MTU)\n");
  244. return;
  245. }
  246. usleep(100000);
  247. }
  248. ifr.ifr_flags |= IFF_MULTICAST;
  249. ifr.ifr_flags |= IFF_UP;
  250. if (ioctl(sock, SIOCSIFFLAGS, (void*)&ifr) < 0) {
  251. ::close(sock);
  252. printf("WARNING: ioctl() failed setting up Linux tap device (bring interface up)\n");
  253. return;
  254. }
  255. usleep(100000);
  256. if (! isOldLinuxKernel()) {
  257. ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  258. _mac.copyTo(ifr.ifr_ifru.ifru_hwaddr.sa_data, 6);
  259. if (ioctl(sock, SIOCSIFHWADDR, (void*)&ifr) < 0) {
  260. ::close(sock);
  261. printf("WARNING: ioctl() failed setting up Linux tap device (set MAC)\n");
  262. return;
  263. }
  264. ifr.ifr_ifru.ifru_mtu = (int)_mtu;
  265. if (ioctl(sock, SIOCSIFMTU, (void*)&ifr) < 0) {
  266. ::close(sock);
  267. printf("WARNING: ioctl() failed setting up Linux tap device (set MTU)\n");
  268. return;
  269. }
  270. }
  271. fcntl(_fd, F_SETFL, O_NONBLOCK);
  272. ::close(sock);
  273. }
  274. if (! _run) {
  275. return;
  276. }
  277. FD_ZERO(&readfds);
  278. FD_ZERO(&nullfds);
  279. nfds = (int)std::max(_shutdownSignalPipe[0], _fd) + 1;
  280. r = 0;
  281. for (;;) {
  282. FD_SET(_shutdownSignalPipe[0], &readfds);
  283. FD_SET(_fd, &readfds);
  284. select(nfds, &readfds, &nullfds, &nullfds, (struct timeval*)0);
  285. if (FD_ISSET(_shutdownSignalPipe[0], &readfds)) {
  286. break;
  287. }
  288. if (FD_ISSET(_fd, &readfds)) {
  289. for (;;) {
  290. // read until there are no more packets, then return to outer select() loop
  291. n = (int)::read(_fd, b + r, ZT_TAP_BUF_SIZE - r);
  292. if (n > 0) {
  293. // Some tap drivers like to send the ethernet frame and the
  294. // payload in two chunks, so handle that by accumulating
  295. // data until we have at least a frame.
  296. r += n;
  297. if (r > 14) {
  298. if (r > ((int)_mtu + 14)) // sanity check for weird TAP behavior on some platforms
  299. r = _mtu + 14;
  300. if (_enabled) {
  301. MAC to(b, 6), from(b + 6, 6);
  302. unsigned int etherType = Utils::ntoh(((const uint16_t*)b)[6]);
  303. _handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void*)(b + 14), (unsigned int)(r - 14));
  304. }
  305. r = 0;
  306. }
  307. }
  308. else {
  309. r = 0;
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. }));
  316. }
  317. }
  318. LinuxEthernetTap::~LinuxEthernetTap()
  319. {
  320. _run = false;
  321. (void)::write(_shutdownSignalPipe[1], "\0", 1);
  322. ::close(_fd);
  323. ::close(_shutdownSignalPipe[0]);
  324. ::close(_shutdownSignalPipe[1]);
  325. for (std::thread& t : _rxThreads) {
  326. t.join();
  327. }
  328. }
  329. void LinuxEthernetTap::setEnabled(bool en)
  330. {
  331. _enabled = en;
  332. }
  333. bool LinuxEthernetTap::enabled() const
  334. {
  335. return _enabled;
  336. }
  337. static bool ___removeIp(const std::string& _dev, const InetAddress& ip)
  338. {
  339. LinuxNetLink::getInstance().removeAddress(ip, _dev.c_str());
  340. return true;
  341. }
  342. bool LinuxEthernetTap::addIps(std::vector<InetAddress> ips)
  343. {
  344. #ifdef __SYNOLOGY__
  345. std::string filepath = "/etc/sysconfig/network-scripts/ifcfg-" + _dev;
  346. std::string cfg_contents = "DEVICE=" + _dev + "\nBOOTPROTO=static";
  347. int ip4 = 0, ip6 = 0, ip4_tot = 0, ip6_tot = 0;
  348. for (int i = 0; i < (int)ips.size(); i++) {
  349. if (ips[i].isV4())
  350. ip4_tot++;
  351. else
  352. ip6_tot++;
  353. }
  354. // Assemble and write contents of ifcfg-dev file
  355. for (int i = 0; i < (int)ips.size(); i++) {
  356. if (ips[i].isV4()) {
  357. char iptmp[64], iptmp2[64];
  358. std::string numstr4 = ip4_tot > 1 ? std::to_string(ip4) : "";
  359. cfg_contents += "\nIPADDR" + numstr4 + "=" + ips[i].toIpString(iptmp) + "\nNETMASK" + numstr4 + "=" + ips[i].netmask().toIpString(iptmp2) + "\n";
  360. ip4++;
  361. }
  362. else {
  363. char iptmp[64], iptmp2[64];
  364. std::string numstr6 = ip6_tot > 1 ? std::to_string(ip6) : "";
  365. cfg_contents += "\nIPV6ADDR" + numstr6 + "=" + ips[i].toIpString(iptmp) + "\nNETMASK" + numstr6 + "=" + ips[i].netmask().toIpString(iptmp2) + "\n";
  366. ip6++;
  367. }
  368. }
  369. OSUtils::writeFile(filepath.c_str(), cfg_contents.c_str(), cfg_contents.length());
  370. // Finally, add IPs
  371. for (int i = 0; i < (int)ips.size(); i++) {
  372. LinuxNetLink::getInstance().addAddress(ips[i], _dev.c_str());
  373. }
  374. return true;
  375. #endif // __SYNOLOGY__
  376. return false;
  377. }
  378. bool LinuxEthernetTap::addIp(const InetAddress& ip)
  379. {
  380. if (! ip)
  381. return false;
  382. std::vector<InetAddress> allIps(ips());
  383. if (std::binary_search(allIps.begin(), allIps.end(), ip))
  384. return true;
  385. // Remove and reconfigure if address is the same but netmask is different
  386. for (std::vector<InetAddress>::iterator i(allIps.begin()); i != allIps.end(); ++i) {
  387. if (i->ipsEqual(ip))
  388. ___removeIp(_dev, *i);
  389. }
  390. LinuxNetLink::getInstance().addAddress(ip, _dev.c_str());
  391. return true;
  392. }
  393. bool LinuxEthernetTap::removeIp(const InetAddress& ip)
  394. {
  395. if (! ip)
  396. return true;
  397. std::vector<InetAddress> allIps(ips());
  398. if (std::find(allIps.begin(), allIps.end(), ip) != allIps.end()) {
  399. if (___removeIp(_dev, ip))
  400. return true;
  401. }
  402. return false;
  403. }
  404. std::vector<InetAddress> LinuxEthernetTap::ips() const
  405. {
  406. uint64_t now = OSUtils::now();
  407. if ((now - _lastIfAddrsUpdate) <= GETIFADDRS_CACHE_TIME) {
  408. return _ifaddrs;
  409. }
  410. _lastIfAddrsUpdate = now;
  411. struct ifaddrs* ifa = (struct ifaddrs*)0;
  412. if (getifaddrs(&ifa))
  413. return std::vector<InetAddress>();
  414. std::vector<InetAddress> r;
  415. struct ifaddrs* p = ifa;
  416. while (p) {
  417. if ((! strcmp(p->ifa_name, _dev.c_str())) && (p->ifa_addr) && (p->ifa_netmask) && (p->ifa_addr->sa_family == p->ifa_netmask->sa_family)) {
  418. switch (p->ifa_addr->sa_family) {
  419. case AF_INET: {
  420. struct sockaddr_in* sin = (struct sockaddr_in*)p->ifa_addr;
  421. struct sockaddr_in* nm = (struct sockaddr_in*)p->ifa_netmask;
  422. r.push_back(InetAddress(&(sin->sin_addr.s_addr), 4, Utils::countBits((uint32_t)nm->sin_addr.s_addr)));
  423. } break;
  424. case AF_INET6: {
  425. struct sockaddr_in6* sin = (struct sockaddr_in6*)p->ifa_addr;
  426. struct sockaddr_in6* nm = (struct sockaddr_in6*)p->ifa_netmask;
  427. uint32_t b[4];
  428. memcpy(b, nm->sin6_addr.s6_addr, sizeof(b));
  429. 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])));
  430. } break;
  431. }
  432. }
  433. p = p->ifa_next;
  434. }
  435. if (ifa)
  436. freeifaddrs(ifa);
  437. std::sort(r.begin(), r.end());
  438. r.erase(std::unique(r.begin(), r.end()), r.end());
  439. _ifaddrs = r;
  440. return r;
  441. }
  442. void LinuxEthernetTap::put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len)
  443. {
  444. // VL2 frame size histogram
  445. ZeroTier::Metrics::vl2_frame_size_hist.Observe(len);
  446. if (len > this->_mtu) {
  447. ZeroTier::Metrics::vl2_would_fragment_or_drop_rx++;
  448. }
  449. char putBuf[ZT_MAX_MTU + 64];
  450. if ((_fd > 0) && (len <= _mtu) && (_enabled)) {
  451. to.copyTo(putBuf, 6);
  452. from.copyTo(putBuf + 6, 6);
  453. *((uint16_t*)(putBuf + 12)) = htons((uint16_t)etherType);
  454. memcpy(putBuf + 14, data, len);
  455. len += 14;
  456. (void)::write(_fd, putBuf, len);
  457. }
  458. }
  459. std::string LinuxEthernetTap::deviceName() const
  460. {
  461. return _dev;
  462. }
  463. void LinuxEthernetTap::setFriendlyName(const char* friendlyName)
  464. {
  465. }
  466. void LinuxEthernetTap::scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed)
  467. {
  468. char *ptr, *ptr2;
  469. unsigned char mac[6];
  470. std::vector<MulticastGroup> newGroups;
  471. int fd = ::open("/proc/net/dev_mcast", O_RDONLY);
  472. if (fd > 0) {
  473. char buf[131072];
  474. int n = (int)::read(fd, buf, sizeof(buf));
  475. if ((n > 0) && (n < (int)sizeof(buf))) {
  476. buf[n] = (char)0;
  477. for (char* l = strtok_r(buf, "\r\n", &ptr); (l); l = strtok_r((char*)0, "\r\n", &ptr)) {
  478. int fno = 0;
  479. char* devname = (char*)0;
  480. char* mcastmac = (char*)0;
  481. for (char* f = strtok_r(l, " \t", &ptr2); (f); f = strtok_r((char*)0, " \t", &ptr2)) {
  482. if (fno == 1)
  483. devname = f;
  484. else if (fno == 4)
  485. mcastmac = f;
  486. ++fno;
  487. }
  488. if ((devname) && (! strcmp(devname, _dev.c_str())) && (mcastmac) && (Utils::unhex(mcastmac, mac, 6) == 6))
  489. newGroups.push_back(MulticastGroup(MAC(mac, 6), 0));
  490. }
  491. }
  492. ::close(fd);
  493. }
  494. std::vector<InetAddress> allIps(ips());
  495. for (std::vector<InetAddress>::iterator ip(allIps.begin()); ip != allIps.end(); ++ip)
  496. newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
  497. std::sort(newGroups.begin(), newGroups.end());
  498. newGroups.erase(std::unique(newGroups.begin(), newGroups.end()), newGroups.end());
  499. for (std::vector<MulticastGroup>::iterator m(newGroups.begin()); m != newGroups.end(); ++m) {
  500. if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m))
  501. added.push_back(*m);
  502. }
  503. for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) {
  504. if (! std::binary_search(newGroups.begin(), newGroups.end(), *m))
  505. removed.push_back(*m);
  506. }
  507. _multicastGroups.swap(newGroups);
  508. }
  509. void LinuxEthernetTap::setMtu(unsigned int mtu)
  510. {
  511. if (_mtu != mtu) {
  512. _mtu = mtu;
  513. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  514. if (sock > 0) {
  515. struct ifreq ifr;
  516. memset(&ifr, 0, sizeof(ifr));
  517. strcpy(ifr.ifr_name, _dev.c_str());
  518. ifr.ifr_ifru.ifru_mtu = (int)mtu;
  519. if (ioctl(sock, SIOCSIFMTU, (void*)&ifr) < 0) {
  520. printf("WARNING: ioctl() failed updating existing Linux tap device (set MTU)\n");
  521. }
  522. close(sock);
  523. }
  524. }
  525. }
  526. } // namespace ZeroTier
  527. #endif // __LINUX__