LinuxRoutingTable.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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 "../node/Constants.hpp"
  28. #ifdef __LINUX__
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <sys/socket.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <sys/wait.h>
  38. #include <fcntl.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <ifaddrs.h>
  42. #include <algorithm>
  43. #include <utility>
  44. #include "../node/Utils.hpp"
  45. #include "LinuxRoutingTable.hpp"
  46. #define ZT_LINUX_IP_COMMAND "/sbin/ip"
  47. namespace ZeroTier {
  48. LinuxRoutingTable::LinuxRoutingTable()
  49. {
  50. }
  51. LinuxRoutingTable::~LinuxRoutingTable()
  52. {
  53. }
  54. std::vector<RoutingTableEntry> LinuxRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const
  55. {
  56. char buf[131072];
  57. char *stmp,*stmp2;
  58. std::vector<RoutingTableEntry> entries;
  59. {
  60. int fd = ::open("/proc/net/route",O_RDONLY);
  61. if (fd <= 0)
  62. buf[0] = (char)0;
  63. else {
  64. int n = (int)::read(fd,buf,sizeof(buf) - 1);
  65. ::close(fd);
  66. if (n < 0) n = 0;
  67. buf[n] = (char)0;
  68. }
  69. }
  70. int lineno = 0;
  71. for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) {
  72. if (lineno == 0) {
  73. ++lineno;
  74. continue; // skip header
  75. }
  76. char *iface = (char *)0;
  77. uint32_t destination = 0;
  78. uint32_t gateway = 0;
  79. int metric = 0;
  80. uint32_t mask = 0;
  81. int fno = 0;
  82. for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) {
  83. switch(fno) {
  84. case 0: iface = f; break;
  85. case 1: destination = (uint32_t)Utils::hexStrToULong(f); break;
  86. case 2: gateway = (uint32_t)Utils::hexStrToULong(f); break;
  87. case 6: metric = (int)Utils::strToInt(f); break;
  88. case 7: mask = (uint32_t)Utils::hexStrToULong(f); break;
  89. }
  90. ++fno;
  91. }
  92. if ((iface)&&(destination)) {
  93. RoutingTableEntry e;
  94. if (destination)
  95. e.destination.set(&destination,4,Utils::countBits(mask));
  96. e.gateway.set(&gateway,4,0);
  97. e.deviceIndex = 0; // not used on Linux
  98. e.metric = metric;
  99. Utils::scopy(e.device,sizeof(e.device),iface);
  100. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(iface,"lo")))))
  101. entries.push_back(e);
  102. }
  103. ++lineno;
  104. }
  105. {
  106. int fd = ::open("/proc/net/ipv6_route",O_RDONLY);
  107. if (fd <= 0)
  108. buf[0] = (char)0;
  109. else {
  110. int n = (int)::read(fd,buf,sizeof(buf) - 1);
  111. ::close(fd);
  112. if (n < 0) n = 0;
  113. buf[n] = (char)0;
  114. }
  115. }
  116. for(char *line=Utils::stok(buf,"\r\n",&stmp);(line);line=Utils::stok((char *)0,"\r\n",&stmp)) {
  117. char *destination = (char *)0;
  118. unsigned int destPrefixLen = 0;
  119. char *gateway = (char *)0; // next hop in ipv6 terminology
  120. int metric = 0;
  121. char *device = (char *)0;
  122. int fno = 0;
  123. for(char *f=Utils::stok(line,"\t \r\n",&stmp2);(f);f=Utils::stok((char *)0,"\t \r\n",&stmp2)) {
  124. switch(fno) {
  125. case 0: destination = f; break;
  126. case 1: destPrefixLen = (unsigned int)Utils::hexStrToULong(f); break;
  127. case 4: gateway = f; break;
  128. case 5: metric = (int)Utils::hexStrToLong(f); break;
  129. case 9: device = f; break;
  130. }
  131. ++fno;
  132. }
  133. if ((device)&&(destination)) {
  134. unsigned char tmp[16];
  135. RoutingTableEntry e;
  136. Utils::unhex(destination,tmp,16);
  137. if ((!Utils::isZero(tmp,16))&&(tmp[0] != 0xff))
  138. e.destination.set(tmp,16,destPrefixLen);
  139. Utils::unhex(gateway,tmp,16);
  140. e.gateway.set(tmp,16,0);
  141. e.deviceIndex = 0; // not used on Linux
  142. e.metric = metric;
  143. Utils::scopy(e.device,sizeof(e.device),device);
  144. if ((e.destination)&&((includeLinkLocal)||(!e.destination.isLinkLocal()))&&((includeLoopback)||((!e.destination.isLoopback())&&(!e.gateway.isLoopback())&&(strcmp(device,"lo")))))
  145. entries.push_back(e);
  146. }
  147. }
  148. std::sort(entries.begin(),entries.end());
  149. return entries;
  150. }
  151. RoutingTableEntry LinuxRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric)
  152. {
  153. char metstr[128];
  154. if ((!gateway)&&((!device)||(!device[0])))
  155. return RoutingTableEntry();
  156. Utils::snprintf(metstr,sizeof(metstr),"%d",metric);
  157. if (metric < 0) {
  158. long pid = (long)vfork();
  159. if (pid == 0) {
  160. if (gateway) {
  161. if ((device)&&(device[0])) {
  162. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),"dev",device,(const char *)0);
  163. } else {
  164. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"via",gateway.toIpString().c_str(),(const char *)0);
  165. }
  166. } else {
  167. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","del",destination.toString().c_str(),"dev",device,(const char *)0);
  168. }
  169. ::_exit(-1);
  170. } else if (pid > 0) {
  171. int exitcode = -1;
  172. ::waitpid(pid,&exitcode,0);
  173. }
  174. } else {
  175. long pid = (long)vfork();
  176. if (pid == 0) {
  177. if (gateway) {
  178. if ((device)&&(device[0])) {
  179. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"via",gateway.toIpString().c_str(),"dev",device,(const char *)0);
  180. } else {
  181. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"via",gateway.toIpString().c_str(),(const char *)0);
  182. }
  183. } else {
  184. ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,"route","replace",destination.toString().c_str(),"metric",metstr,"dev",device,(const char *)0);
  185. }
  186. ::_exit(-1);
  187. } else if (pid > 0) {
  188. int exitcode = -1;
  189. ::waitpid(pid,&exitcode,0);
  190. }
  191. }
  192. std::vector<RoutingTableEntry> rtab(get(true,true));
  193. std::vector<RoutingTableEntry>::iterator bestEntry(rtab.end());
  194. for(std::vector<RoutingTableEntry>::iterator e(rtab.begin());e!=rtab.end();++e) {
  195. if ((e->destination == destination)&&(e->gateway.ipsEqual(gateway))) {
  196. if ((device)&&(device[0])) {
  197. if (!strcmp(device,e->device)) {
  198. if (metric == e->metric)
  199. bestEntry = e;
  200. }
  201. }
  202. if (bestEntry == rtab.end())
  203. bestEntry = e;
  204. }
  205. }
  206. if (bestEntry != rtab.end())
  207. return *bestEntry;
  208. return RoutingTableEntry();
  209. }
  210. } // namespace ZeroTier
  211. #endif // __LINUX__