LinuxEthernetTap.cpp 16 KB

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