net.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. net.c -- most of the network code
  3. Copyright (C) 1998-2005 Ivo Timmermans,
  4. 2000-2015 Guus Sliepen <guus@tinc-vpn.org>
  5. 2006 Scott Lamb <slamb@slamb.org>
  6. 2011 Loïc Grenié <loic.grenie@gmail.com>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "system.h"
  20. #include <openssl/rand.h>
  21. #include "utils.h"
  22. #include "avl_tree.h"
  23. #include "conf.h"
  24. #include "connection.h"
  25. #include "device.h"
  26. #include "event.h"
  27. #include "graph.h"
  28. #include "logger.h"
  29. #include "meta.h"
  30. #include "net.h"
  31. #include "netutl.h"
  32. #include "process.h"
  33. #include "protocol.h"
  34. #include "route.h"
  35. #include "subnet.h"
  36. #include "xalloc.h"
  37. bool do_purge = false;
  38. volatile bool running = false;
  39. #ifdef HAVE_PSELECT
  40. bool graph_dump = false;
  41. #endif
  42. time_t now = 0;
  43. int contradicting_add_edge = 0;
  44. int contradicting_del_edge = 0;
  45. static int sleeptime = 10;
  46. /* Purge edges and subnets of unreachable nodes. Use carefully. */
  47. static void purge(void) {
  48. avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
  49. node_t *n;
  50. edge_t *e;
  51. subnet_t *s;
  52. ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
  53. /* Remove all edges and subnets owned by unreachable nodes. */
  54. for(nnode = node_tree->head; nnode; nnode = nnext) {
  55. nnext = nnode->next;
  56. n = nnode->data;
  57. if(!n->status.reachable) {
  58. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
  59. n->hostname);
  60. for(snode = n->subnet_tree->head; snode; snode = snext) {
  61. snext = snode->next;
  62. s = snode->data;
  63. send_del_subnet(everyone, s);
  64. if(!strictsubnets) {
  65. subnet_del(n, s);
  66. }
  67. }
  68. for(enode = n->edge_tree->head; enode; enode = enext) {
  69. enext = enode->next;
  70. e = enode->data;
  71. if(!tunnelserver) {
  72. send_del_edge(everyone, e);
  73. }
  74. edge_del(e);
  75. }
  76. }
  77. }
  78. /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
  79. for(nnode = node_tree->head; nnode; nnode = nnext) {
  80. nnext = nnode->next;
  81. n = nnode->data;
  82. if(!n->status.reachable) {
  83. for(enode = edge_weight_tree->head; enode; enode = enext) {
  84. enext = enode->next;
  85. e = enode->data;
  86. if(e->to == n) {
  87. break;
  88. }
  89. }
  90. if(!enode && (!strictsubnets || !n->subnet_tree->head))
  91. /* in strictsubnets mode do not delete nodes with subnets */
  92. {
  93. node_del(n);
  94. }
  95. }
  96. }
  97. }
  98. /*
  99. put all file descriptors in an fd_set array
  100. While we're at it, purge stuff that needs to be removed.
  101. */
  102. static int build_fdset(fd_set *readset, fd_set *writeset) {
  103. avl_node_t *node, *next;
  104. connection_t *c;
  105. int i, max = 0;
  106. FD_ZERO(readset);
  107. FD_ZERO(writeset);
  108. for(node = connection_tree->head; node; node = next) {
  109. next = node->next;
  110. c = node->data;
  111. if(c->status.remove) {
  112. connection_del(c);
  113. if(!connection_tree->head) {
  114. purge();
  115. }
  116. } else {
  117. FD_SET(c->socket, readset);
  118. if(c->outbuflen > 0 || c->status.connecting) {
  119. FD_SET(c->socket, writeset);
  120. }
  121. if(c->socket > max) {
  122. max = c->socket;
  123. }
  124. }
  125. }
  126. for(i = 0; i < listen_sockets; i++) {
  127. FD_SET(listen_socket[i].tcp, readset);
  128. if(listen_socket[i].tcp > max) {
  129. max = listen_socket[i].tcp;
  130. }
  131. FD_SET(listen_socket[i].udp, readset);
  132. if(listen_socket[i].udp > max) {
  133. max = listen_socket[i].udp;
  134. }
  135. }
  136. if(device_fd >= 0) {
  137. FD_SET(device_fd, readset);
  138. }
  139. if(device_fd > max) {
  140. max = device_fd;
  141. }
  142. return max;
  143. }
  144. /*
  145. Terminate a connection:
  146. - Close the socket
  147. - Remove associated edge and tell other connections about it if report = true
  148. - Check if we need to retry making an outgoing connection
  149. - Deactivate the host
  150. */
  151. void terminate_connection(connection_t *c, bool report) {
  152. if(c->status.remove) {
  153. return;
  154. }
  155. ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
  156. c->name, c->hostname);
  157. c->status.remove = true;
  158. c->status.active = false;
  159. if(c->node) {
  160. c->node->connection = NULL;
  161. }
  162. if(c->socket) {
  163. closesocket(c->socket);
  164. }
  165. if(c->edge) {
  166. if(!c->node) {
  167. logger(LOG_ERR, "Connection to %s (%s) has an edge but node is NULL!", c->name, c->hostname);
  168. // And that should never happen.
  169. abort();
  170. }
  171. if(report && !tunnelserver) {
  172. send_del_edge(everyone, c->edge);
  173. }
  174. edge_del(c->edge);
  175. /* Run MST and SSSP algorithms */
  176. graph();
  177. /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
  178. if(report && !c->node->status.reachable) {
  179. edge_t *e;
  180. e = lookup_edge(c->node, myself);
  181. if(e) {
  182. if(!tunnelserver) {
  183. send_del_edge(everyone, e);
  184. }
  185. edge_del(e);
  186. }
  187. }
  188. }
  189. free_connection_partially(c);
  190. /* Check if this was our outgoing connection */
  191. if(c->outgoing) {
  192. c->status.remove = false;
  193. do_outgoing_connection(c);
  194. }
  195. #ifndef HAVE_MINGW
  196. /* Clean up dead proxy processes */
  197. while(waitpid(-1, NULL, WNOHANG) > 0);
  198. #endif
  199. }
  200. /*
  201. Check if the other end is active.
  202. If we have sent packets, but didn't receive any,
  203. then possibly the other end is dead. We send a
  204. PING request over the meta connection. If the other
  205. end does not reply in time, we consider them dead
  206. and close the connection.
  207. */
  208. static void check_dead_connections(void) {
  209. avl_node_t *node, *next;
  210. connection_t *c;
  211. for(node = connection_tree->head; node; node = next) {
  212. next = node->next;
  213. c = node->data;
  214. if(c->last_ping_time + pingtimeout <= now) {
  215. if(c->status.active) {
  216. if(c->status.pinged) {
  217. ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
  218. c->name, c->hostname, (long)(now - c->last_ping_time));
  219. c->status.timeout = true;
  220. terminate_connection(c, true);
  221. } else if(c->last_ping_time + pinginterval <= now) {
  222. send_ping(c);
  223. }
  224. } else {
  225. if(c->status.remove) {
  226. logger(LOG_WARNING, "Old connection_t for %s (%s) status %04x still lingering, deleting...",
  227. c->name, c->hostname, bitfield_to_int(&c->status, sizeof(c->status)));
  228. connection_del(c);
  229. continue;
  230. }
  231. ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication",
  232. c->name, c->hostname);
  233. if(c->status.connecting) {
  234. c->status.connecting = false;
  235. closesocket(c->socket);
  236. do_outgoing_connection(c);
  237. } else {
  238. terminate_connection(c, false);
  239. }
  240. }
  241. }
  242. if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout <= now) {
  243. if(c->status.active) {
  244. ifdebug(CONNECTIONS) logger(LOG_INFO,
  245. "%s (%s) could not flush for %ld seconds (%d bytes remaining)",
  246. c->name, c->hostname, (long)(now - c->last_flushed_time), c->outbuflen);
  247. c->status.timeout = true;
  248. terminate_connection(c, true);
  249. }
  250. }
  251. }
  252. }
  253. /*
  254. check all connections to see if anything
  255. happened on their sockets
  256. */
  257. static void check_network_activity(fd_set *readset, fd_set *writeset) {
  258. connection_t *c;
  259. avl_node_t *node;
  260. int result, i;
  261. socklen_t len = sizeof(result);
  262. vpn_packet_t packet;
  263. static int errors = 0;
  264. /* check input from kernel */
  265. if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
  266. if(devops.read(&packet)) {
  267. if(packet.len) {
  268. errors = 0;
  269. packet.priority = 0;
  270. route(myself, &packet);
  271. }
  272. } else {
  273. usleep(errors * 50000);
  274. errors++;
  275. if(errors > 10) {
  276. logger(LOG_ERR, "Too many errors from %s, exiting!", device);
  277. running = false;
  278. }
  279. }
  280. }
  281. /* check meta connections */
  282. for(node = connection_tree->head; node; node = node->next) {
  283. c = node->data;
  284. if(c->status.remove) {
  285. continue;
  286. }
  287. if(FD_ISSET(c->socket, writeset)) {
  288. if(c->status.connecting) {
  289. c->status.connecting = false;
  290. getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len);
  291. if(!result) {
  292. finish_connecting(c);
  293. } else {
  294. ifdebug(CONNECTIONS) logger(LOG_DEBUG,
  295. "Error while connecting to %s (%s): %s",
  296. c->name, c->hostname, sockstrerror(result));
  297. closesocket(c->socket);
  298. do_outgoing_connection(c);
  299. continue;
  300. }
  301. }
  302. if(!flush_meta(c)) {
  303. terminate_connection(c, c->status.active);
  304. continue;
  305. }
  306. }
  307. if(FD_ISSET(c->socket, readset)) {
  308. if(!receive_meta(c)) {
  309. terminate_connection(c, c->status.active);
  310. continue;
  311. }
  312. }
  313. }
  314. for(i = 0; i < listen_sockets; i++) {
  315. if(FD_ISSET(listen_socket[i].udp, readset)) {
  316. handle_incoming_vpn_data(i);
  317. }
  318. if(FD_ISSET(listen_socket[i].tcp, readset)) {
  319. handle_new_meta_connection(listen_socket[i].tcp);
  320. }
  321. }
  322. }
  323. /*
  324. this is where it all happens...
  325. */
  326. int main_loop(void) {
  327. fd_set readset, writeset;
  328. #ifdef HAVE_PSELECT
  329. struct timespec tv;
  330. sigset_t omask, block_mask;
  331. time_t next_event;
  332. #else
  333. struct timeval tv;
  334. #endif
  335. int r, maxfd;
  336. time_t last_ping_check, last_config_check, last_graph_dump;
  337. event_t *event;
  338. last_ping_check = now;
  339. last_config_check = now;
  340. last_graph_dump = now;
  341. srand(now);
  342. #ifdef HAVE_PSELECT
  343. if(lookup_config(config_tree, "GraphDumpFile")) {
  344. graph_dump = true;
  345. }
  346. /* Block SIGHUP & SIGALRM */
  347. sigemptyset(&block_mask);
  348. sigaddset(&block_mask, SIGHUP);
  349. sigaddset(&block_mask, SIGALRM);
  350. sigprocmask(SIG_BLOCK, &block_mask, &omask);
  351. #endif
  352. running = true;
  353. while(running) {
  354. #ifdef HAVE_PSELECT
  355. next_event = last_ping_check + pingtimeout;
  356. if(graph_dump && next_event > last_graph_dump + 60) {
  357. next_event = last_graph_dump + 60;
  358. }
  359. if((event = peek_next_event()) && next_event > event->time) {
  360. next_event = event->time;
  361. }
  362. if(next_event <= now) {
  363. tv.tv_sec = 0;
  364. } else {
  365. tv.tv_sec = next_event - now;
  366. }
  367. tv.tv_nsec = 0;
  368. #else
  369. tv.tv_sec = 1;
  370. tv.tv_usec = 0;
  371. #endif
  372. maxfd = build_fdset(&readset, &writeset);
  373. #ifdef HAVE_MINGW
  374. LeaveCriticalSection(&mutex);
  375. #endif
  376. #ifdef HAVE_PSELECT
  377. r = pselect(maxfd + 1, &readset, &writeset, NULL, &tv, &omask);
  378. #else
  379. r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
  380. #endif
  381. now = time(NULL);
  382. #ifdef HAVE_MINGW
  383. EnterCriticalSection(&mutex);
  384. #endif
  385. if(r < 0) {
  386. if(!sockwouldblock(sockerrno)) {
  387. logger(LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno));
  388. dump_connections();
  389. return 1;
  390. }
  391. }
  392. if(r > 0) {
  393. check_network_activity(&readset, &writeset);
  394. }
  395. if(do_purge) {
  396. purge();
  397. do_purge = false;
  398. }
  399. /* Let's check if everybody is still alive */
  400. if(last_ping_check + pingtimeout <= now) {
  401. check_dead_connections();
  402. last_ping_check = now;
  403. if(routing_mode == RMODE_SWITCH) {
  404. age_subnets();
  405. }
  406. age_past_requests();
  407. /* Should we regenerate our key? */
  408. if(keyexpires <= now) {
  409. avl_node_t *node;
  410. node_t *n;
  411. ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
  412. for(node = node_tree->head; node; node = node->next) {
  413. n = node->data;
  414. if(n->inkey) {
  415. free(n->inkey);
  416. n->inkey = NULL;
  417. }
  418. }
  419. send_key_changed();
  420. keyexpires = now + keylifetime;
  421. }
  422. /* Detect ADD_EDGE/DEL_EDGE storms that are caused when
  423. * two tinc daemons with the same name are on the VPN.
  424. * If so, sleep a while. If this happens multiple times
  425. * in a row, sleep longer. */
  426. if(contradicting_del_edge > 100 && contradicting_add_edge > 100) {
  427. logger(LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime);
  428. usleep(sleeptime * 1000000LL);
  429. sleeptime *= 2;
  430. if(sleeptime < 0) {
  431. sleeptime = 3600;
  432. }
  433. } else {
  434. sleeptime /= 2;
  435. if(sleeptime < 10) {
  436. sleeptime = 10;
  437. }
  438. }
  439. contradicting_add_edge = 0;
  440. contradicting_del_edge = 0;
  441. }
  442. if(sigalrm) {
  443. avl_node_t *node;
  444. logger(LOG_INFO, "Flushing event queue");
  445. expire_events();
  446. for(node = connection_tree->head; node; node = node->next) {
  447. connection_t *c = node->data;
  448. if(c->status.active) {
  449. send_ping(c);
  450. }
  451. }
  452. sigalrm = false;
  453. }
  454. while((event = get_expired_event())) {
  455. event->handler(event->data);
  456. free_event(event);
  457. }
  458. if(sighup) {
  459. connection_t *c;
  460. avl_node_t *node, *next;
  461. char *fname;
  462. struct stat s;
  463. sighup = false;
  464. reopenlogger();
  465. /* Reread our own configuration file */
  466. exit_configuration(&config_tree);
  467. init_configuration(&config_tree);
  468. if(!read_server_config()) {
  469. logger(LOG_ERR, "Unable to reread configuration file, exitting.");
  470. return 1;
  471. }
  472. /* Cancel non-active outgoing connections */
  473. for(node = connection_tree->head; node; node = next) {
  474. next = node->next;
  475. c = node->data;
  476. c->outgoing = NULL;
  477. if(c->status.connecting) {
  478. terminate_connection(c, false);
  479. connection_del(c);
  480. }
  481. }
  482. /* Wipe list of outgoing connections */
  483. for(list_node_t *node = outgoing_list->head; node; node = node->next) {
  484. outgoing_t *outgoing = node->data;
  485. if(outgoing->event) {
  486. event_del(outgoing->event);
  487. }
  488. }
  489. list_delete_list(outgoing_list);
  490. /* Close connections to hosts that have a changed or deleted host config file */
  491. for(node = connection_tree->head; node; node = node->next) {
  492. c = node->data;
  493. xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
  494. if(stat(fname, &s) || s.st_mtime > last_config_check) {
  495. terminate_connection(c, c->status.active);
  496. }
  497. free(fname);
  498. }
  499. last_config_check = now;
  500. /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
  501. if(strictsubnets) {
  502. subnet_t *subnet;
  503. for(node = subnet_tree->head; node; node = node->next) {
  504. subnet = node->data;
  505. subnet->expires = 1;
  506. }
  507. load_all_subnets();
  508. for(node = subnet_tree->head; node; node = next) {
  509. next = node->next;
  510. subnet = node->data;
  511. if(subnet->expires == 1) {
  512. send_del_subnet(everyone, subnet);
  513. if(subnet->owner->status.reachable) {
  514. subnet_update(subnet->owner, subnet, false);
  515. }
  516. subnet_del(subnet->owner, subnet);
  517. } else if(subnet->expires == -1) {
  518. subnet->expires = 0;
  519. } else {
  520. send_add_subnet(everyone, subnet);
  521. if(subnet->owner->status.reachable) {
  522. subnet_update(subnet->owner, subnet, true);
  523. }
  524. }
  525. }
  526. }
  527. /* Try to make outgoing connections */
  528. try_outgoing_connections();
  529. }
  530. /* Dump graph if wanted every 60 seconds*/
  531. if(last_graph_dump + 60 <= now) {
  532. dump_graph();
  533. last_graph_dump = now;
  534. }
  535. }
  536. #ifdef HAVE_PSELECT
  537. /* Restore SIGHUP & SIGALARM mask */
  538. sigprocmask(SIG_SETMASK, &omask, NULL);
  539. #endif
  540. return 0;
  541. }