LinuxRoutingTable.cpp 7.0 KB

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