LinuxEthernetTap.cpp 16 KB

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