net.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. net.c -- most of the network code
  3. Copyright (C) 1998-2005 Ivo Timmermans,
  4. 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
  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 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include <openssl/rand.h>
  19. #include "utils.h"
  20. #include "avl_tree.h"
  21. #include "conf.h"
  22. #include "connection.h"
  23. #include "device.h"
  24. #include "event.h"
  25. #include "graph.h"
  26. #include "logger.h"
  27. #include "meta.h"
  28. #include "net.h"
  29. #include "netutl.h"
  30. #include "process.h"
  31. #include "protocol.h"
  32. #include "route.h"
  33. #include "subnet.h"
  34. #include "xalloc.h"
  35. bool do_purge = false;
  36. volatile bool running = false;
  37. time_t now = 0;
  38. /* Purge edges and subnets of unreachable nodes. Use carefully. */
  39. static void purge(void)
  40. {
  41. avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
  42. node_t *n;
  43. edge_t *e;
  44. subnet_t *s;
  45. cp();
  46. ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
  47. /* Remove all edges and subnets owned by unreachable nodes. */
  48. for(nnode = node_tree->head; nnode; nnode = nnext) {
  49. nnext = nnode->next;
  50. n = nnode->data;
  51. if(!n->status.reachable) {
  52. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
  53. n->hostname);
  54. for(snode = n->subnet_tree->head; snode; snode = snext) {
  55. snext = snode->next;
  56. s = snode->data;
  57. if(!tunnelserver)
  58. send_del_subnet(broadcast, s);
  59. subnet_del(n, s);
  60. }
  61. for(enode = n->edge_tree->head; enode; enode = enext) {
  62. enext = enode->next;
  63. e = enode->data;
  64. if(!tunnelserver)
  65. send_del_edge(broadcast, e);
  66. edge_del(e);
  67. }
  68. }
  69. }
  70. /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
  71. for(nnode = node_tree->head; nnode; nnode = nnext) {
  72. nnext = nnode->next;
  73. n = nnode->data;
  74. if(!n->status.reachable) {
  75. for(enode = edge_weight_tree->head; enode; enode = enext) {
  76. enext = enode->next;
  77. e = enode->data;
  78. if(e->to == n)
  79. break;
  80. }
  81. if(!enode)
  82. node_del(n);
  83. }
  84. }
  85. }
  86. /*
  87. put all file descriptors in an fd_set array
  88. While we're at it, purge stuff that needs to be removed.
  89. */
  90. static int build_fdset(fd_set *readset, fd_set *writeset)
  91. {
  92. avl_node_t *node, *next;
  93. connection_t *c;
  94. int i, max = 0;
  95. cp();
  96. FD_ZERO(readset);
  97. FD_ZERO(writeset);
  98. for(node = connection_tree->head; node; node = next) {
  99. next = node->next;
  100. c = node->data;
  101. if(c->status.remove) {
  102. connection_del(c);
  103. if(!connection_tree->head)
  104. purge();
  105. } else {
  106. FD_SET(c->socket, readset);
  107. if(c->outbuflen > 0)
  108. FD_SET(c->socket, writeset);
  109. if(c->socket > max)
  110. max = c->socket;
  111. }
  112. }
  113. for(i = 0; i < listen_sockets; i++) {
  114. FD_SET(listen_socket[i].tcp, readset);
  115. if(listen_socket[i].tcp > max)
  116. max = listen_socket[i].tcp;
  117. FD_SET(listen_socket[i].udp, readset);
  118. if(listen_socket[i].udp > max)
  119. max = listen_socket[i].udp;
  120. }
  121. if(device_fd >= 0)
  122. FD_SET(device_fd, readset);
  123. if(device_fd > max)
  124. max = device_fd;
  125. return max;
  126. }
  127. /*
  128. Terminate a connection:
  129. - Close the socket
  130. - Remove associated edge and tell other connections about it if report = true
  131. - Check if we need to retry making an outgoing connection
  132. - Deactivate the host
  133. */
  134. void terminate_connection(connection_t *c, bool report)
  135. {
  136. cp();
  137. if(c->status.remove)
  138. return;
  139. ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
  140. c->name, c->hostname);
  141. c->status.remove = true;
  142. c->status.active = false;
  143. if(c->node)
  144. c->node->connection = NULL;
  145. if(c->socket)
  146. closesocket(c->socket);
  147. if(c->edge) {
  148. if(report && !tunnelserver)
  149. send_del_edge(broadcast, c->edge);
  150. edge_del(c->edge);
  151. /* Run MST and SSSP algorithms */
  152. graph();
  153. /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
  154. if(report && !c->node->status.reachable) {
  155. edge_t *e;
  156. e = lookup_edge(c->node, myself);
  157. if(e) {
  158. if(!tunnelserver)
  159. send_del_edge(broadcast, e);
  160. edge_del(e);
  161. }
  162. }
  163. }
  164. /* Check if this was our outgoing connection */
  165. if(c->outgoing) {
  166. retry_outgoing(c->outgoing);
  167. c->outgoing = NULL;
  168. }
  169. free(c->outbuf);
  170. c->outbuf = NULL;
  171. c->outbuflen = 0;
  172. c->outbufsize = 0;
  173. c->outbufstart = 0;
  174. }
  175. /*
  176. Check if the other end is active.
  177. If we have sent packets, but didn't receive any,
  178. then possibly the other end is dead. We send a
  179. PING request over the meta connection. If the other
  180. end does not reply in time, we consider them dead
  181. and close the connection.
  182. */
  183. static void check_dead_connections(void)
  184. {
  185. avl_node_t *node, *next;
  186. connection_t *c;
  187. cp();
  188. for(node = connection_tree->head; node; node = next) {
  189. next = node->next;
  190. c = node->data;
  191. if(c->last_ping_time + pingtimeout < now) {
  192. if(c->status.active) {
  193. if(c->status.pinged) {
  194. ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
  195. c->name, c->hostname, now - c->last_ping_time);
  196. c->status.timeout = true;
  197. terminate_connection(c, true);
  198. } else if(c->last_ping_time + pinginterval < now) {
  199. send_ping(c);
  200. }
  201. } else {
  202. if(c->status.remove) {
  203. logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
  204. c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
  205. connection_del(c);
  206. continue;
  207. }
  208. ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
  209. c->name, c->hostname);
  210. if(c->status.connecting) {
  211. c->status.connecting = false;
  212. closesocket(c->socket);
  213. do_outgoing_connection(c);
  214. } else {
  215. terminate_connection(c, false);
  216. }
  217. }
  218. }
  219. if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
  220. if(c->status.active) {
  221. ifdebug(CONNECTIONS) logger(LOG_INFO,
  222. _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
  223. c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
  224. c->status.timeout = true;
  225. terminate_connection(c, true);
  226. }
  227. }
  228. }
  229. }
  230. /*
  231. check all connections to see if anything
  232. happened on their sockets
  233. */
  234. static void check_network_activity(fd_set * readset, fd_set * writeset)
  235. {
  236. connection_t *c;
  237. avl_node_t *node;
  238. int result, i;
  239. socklen_t len = sizeof(result);
  240. vpn_packet_t packet;
  241. cp();
  242. /* check input from kernel */
  243. if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
  244. if(read_packet(&packet)) {
  245. packet.priority = 0;
  246. route(myself, &packet);
  247. }
  248. }
  249. /* check meta connections */
  250. for(node = connection_tree->head; node; node = node->next) {
  251. c = node->data;
  252. if(c->status.remove)
  253. continue;
  254. if(FD_ISSET(c->socket, readset)) {
  255. if(c->status.connecting) {
  256. c->status.connecting = false;
  257. getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
  258. if(!result)
  259. finish_connecting(c);
  260. else {
  261. ifdebug(CONNECTIONS) logger(LOG_DEBUG,
  262. _("Error while connecting to %s (%s): %s"),
  263. c->name, c->hostname, strerror(result));
  264. closesocket(c->socket);
  265. do_outgoing_connection(c);
  266. continue;
  267. }
  268. }
  269. if(!receive_meta(c)) {
  270. terminate_connection(c, c->status.active);
  271. continue;
  272. }
  273. }
  274. if(FD_ISSET(c->socket, writeset)) {
  275. if(!flush_meta(c)) {
  276. terminate_connection(c, c->status.active);
  277. continue;
  278. }
  279. }
  280. }
  281. for(i = 0; i < listen_sockets; i++) {
  282. if(FD_ISSET(listen_socket[i].udp, readset))
  283. handle_incoming_vpn_data(listen_socket[i].udp);
  284. if(FD_ISSET(listen_socket[i].tcp, readset))
  285. handle_new_meta_connection(listen_socket[i].tcp);
  286. }
  287. }
  288. /*
  289. this is where it all happens...
  290. */
  291. int main_loop(void)
  292. {
  293. fd_set readset, writeset;
  294. struct timeval tv;
  295. int r, maxfd;
  296. time_t last_ping_check, last_config_check, last_graph_dump;
  297. event_t *event;
  298. cp();
  299. last_ping_check = now;
  300. last_config_check = now;
  301. last_graph_dump = now;
  302. srand(now);
  303. running = true;
  304. while(running) {
  305. now = time(NULL);
  306. // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
  307. tv.tv_sec = 1;
  308. tv.tv_usec = 0;
  309. maxfd = build_fdset(&readset, &writeset);
  310. #ifdef HAVE_MINGW
  311. LeaveCriticalSection(&mutex);
  312. #endif
  313. r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
  314. #ifdef HAVE_MINGW
  315. EnterCriticalSection(&mutex);
  316. #endif
  317. if(r < 0) {
  318. if(errno != EINTR && errno != EAGAIN) {
  319. logger(LOG_ERR, _("Error while waiting for input: %s"),
  320. strerror(errno));
  321. cp_trace();
  322. dump_connections();
  323. return 1;
  324. }
  325. continue;
  326. }
  327. check_network_activity(&readset, &writeset);
  328. if(do_purge) {
  329. purge();
  330. do_purge = false;
  331. }
  332. /* Let's check if everybody is still alive */
  333. if(last_ping_check + pingtimeout < now) {
  334. check_dead_connections();
  335. last_ping_check = now;
  336. if(routing_mode == RMODE_SWITCH)
  337. age_subnets();
  338. age_past_requests();
  339. /* Should we regenerate our key? */
  340. if(keyexpires < now) {
  341. avl_node_t *node;
  342. node_t *n;
  343. ifdebug(STATUS) logger(LOG_INFO, _("Expiring symmetric keys"));
  344. for(node = node_tree->head; node; node = node->next) {
  345. n = node->data;
  346. if(n->inkey) {
  347. free(n->inkey);
  348. n->inkey = NULL;
  349. }
  350. }
  351. send_key_changed(broadcast, myself);
  352. keyexpires = now + keylifetime;
  353. }
  354. }
  355. if(sigalrm) {
  356. logger(LOG_INFO, _("Flushing event queue"));
  357. expire_events();
  358. sigalrm = false;
  359. }
  360. while((event = get_expired_event())) {
  361. event->handler(event->data);
  362. free_event(event);
  363. }
  364. if(sighup) {
  365. connection_t *c;
  366. avl_node_t *node;
  367. char *fname;
  368. struct stat s;
  369. sighup = false;
  370. /* Reread our own configuration file */
  371. exit_configuration(&config_tree);
  372. init_configuration(&config_tree);
  373. if(!read_server_config()) {
  374. logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
  375. return 1;
  376. }
  377. /* Close connections to hosts that have a changed or deleted host config file */
  378. for(node = connection_tree->head; node; node = node->next) {
  379. c = node->data;
  380. xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
  381. if(stat(fname, &s) || s.st_mtime > last_config_check)
  382. terminate_connection(c, c->status.active);
  383. free(fname);
  384. }
  385. last_config_check = now;
  386. /* Try to make outgoing connections */
  387. try_outgoing_connections();
  388. }
  389. /* Dump graph if wanted every 60 seconds*/
  390. if(last_graph_dump + 60 < now) {
  391. dump_graph();
  392. last_graph_dump = now;
  393. }
  394. }
  395. return 0;
  396. }