LinuxEthernetTap.cpp 16 KB

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