net_setup.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /*
  2. net_setup.c -- Setup.
  3. Copyright (C) 1998-2005 Ivo Timmermans,
  4. 2000-2017 Guus Sliepen <guus@tinc-vpn.org>
  5. 2006 Scott Lamb <slamb@slamb.org>
  6. 2010 Brandon Black <blblack@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/pem.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/rand.h>
  23. #include <openssl/err.h>
  24. #include <openssl/evp.h>
  25. #include <openssl/bn.h>
  26. #include "avl_tree.h"
  27. #include "conf.h"
  28. #include "connection.h"
  29. #include "device.h"
  30. #include "event.h"
  31. #include "graph.h"
  32. #include "logger.h"
  33. #include "net.h"
  34. #include "netutl.h"
  35. #include "process.h"
  36. #include "protocol.h"
  37. #include "proxy.h"
  38. #include "route.h"
  39. #include "subnet.h"
  40. #include "utils.h"
  41. #include "xalloc.h"
  42. char *myport;
  43. devops_t devops;
  44. bool read_rsa_public_key(connection_t *c) {
  45. FILE *fp;
  46. char *pubname;
  47. char *hcfname;
  48. char *key;
  49. BIGNUM *n = NULL;
  50. BIGNUM *e = NULL;
  51. if(!c->rsa_key) {
  52. c->rsa_key = RSA_new();
  53. // RSA_blinding_on(c->rsa_key, NULL);
  54. }
  55. /* First, check for simple PublicKey statement */
  56. if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key)) {
  57. if((size_t)BN_hex2bn(&n, key) != strlen(key)) {
  58. free(key);
  59. logger(LOG_ERR, "Invalid PublicKey for %s!", c->name);
  60. return false;
  61. }
  62. free(key);
  63. BN_hex2bn(&e, "FFFF");
  64. if(!n || !e || RSA_set0_key(c->rsa_key, n, e, NULL) != 1) {
  65. BN_free(e);
  66. BN_free(n);
  67. logger(LOG_ERR, "RSA_set0_key() failed with PublicKey for %s!", c->name);
  68. return false;
  69. }
  70. return true;
  71. }
  72. /* Else, check for PublicKeyFile statement and read it */
  73. if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &pubname)) {
  74. fp = fopen(pubname, "r");
  75. if(!fp) {
  76. logger(LOG_ERR, "Error reading RSA public key file `%s': %s", pubname, strerror(errno));
  77. free(pubname);
  78. return false;
  79. }
  80. c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
  81. fclose(fp);
  82. if(c->rsa_key) {
  83. free(pubname);
  84. return true; /* Woohoo. */
  85. }
  86. /* If it fails, try PEM_read_RSA_PUBKEY. */
  87. fp = fopen(pubname, "r");
  88. if(!fp) {
  89. logger(LOG_ERR, "Error reading RSA public key file `%s': %s", pubname, strerror(errno));
  90. free(pubname);
  91. return false;
  92. }
  93. c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
  94. fclose(fp);
  95. if(c->rsa_key) {
  96. // RSA_blinding_on(c->rsa_key, NULL);
  97. free(pubname);
  98. return true;
  99. }
  100. logger(LOG_ERR, "Reading RSA public key file `%s' failed: %s", pubname, strerror(errno));
  101. free(pubname);
  102. return false;
  103. }
  104. /* Else, check if a harnessed public key is in the config file */
  105. xasprintf(&hcfname, "%s/hosts/%s", confbase, c->name);
  106. fp = fopen(hcfname, "r");
  107. if(!fp) {
  108. logger(LOG_ERR, "Error reading RSA public key file `%s': %s", hcfname, strerror(errno));
  109. free(hcfname);
  110. return false;
  111. }
  112. c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
  113. fclose(fp);
  114. if(c->rsa_key) {
  115. free(hcfname);
  116. return true;
  117. }
  118. /* Try again with PEM_read_RSA_PUBKEY. */
  119. fp = fopen(hcfname, "r");
  120. if(!fp) {
  121. logger(LOG_ERR, "Error reading RSA public key file `%s': %s", hcfname, strerror(errno));
  122. free(hcfname);
  123. return false;
  124. }
  125. free(hcfname);
  126. c->rsa_key = PEM_read_RSA_PUBKEY(fp, &c->rsa_key, NULL, NULL);
  127. // RSA_blinding_on(c->rsa_key, NULL);
  128. fclose(fp);
  129. if(c->rsa_key) {
  130. return true;
  131. }
  132. logger(LOG_ERR, "No public key for %s specified!", c->name);
  133. return false;
  134. }
  135. static bool read_rsa_private_key(void) {
  136. FILE *fp;
  137. char *fname, *key, *pubkey;
  138. BIGNUM *n = NULL;
  139. BIGNUM *e = NULL;
  140. BIGNUM *d = NULL;
  141. if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
  142. myself->connection->rsa_key = RSA_new();
  143. // RSA_blinding_on(myself->connection->rsa_key, NULL);
  144. if((size_t)BN_hex2bn(&d, key) != strlen(key)) {
  145. logger(LOG_ERR, "Invalid PrivateKey for myself!");
  146. free(key);
  147. return false;
  148. }
  149. free(key);
  150. if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
  151. BN_free(d);
  152. logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
  153. return false;
  154. }
  155. if((size_t)BN_hex2bn(&n, pubkey) != strlen(pubkey)) {
  156. free(pubkey);
  157. BN_free(d);
  158. logger(LOG_ERR, "Invalid PublicKey for myself!");
  159. return false;
  160. }
  161. free(pubkey);
  162. BN_hex2bn(&e, "FFFF");
  163. if(!n || !e || !d || RSA_set0_key(myself->connection->rsa_key, n, e, d) != 1) {
  164. BN_free(d);
  165. BN_free(e);
  166. BN_free(n);
  167. logger(LOG_ERR, "RSA_set0_key() failed with PrivateKey for myself!");
  168. return false;
  169. }
  170. return true;
  171. }
  172. if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname)) {
  173. xasprintf(&fname, "%s/rsa_key.priv", confbase);
  174. }
  175. fp = fopen(fname, "r");
  176. if(!fp) {
  177. logger(LOG_ERR, "Error reading RSA private key file `%s': %s",
  178. fname, strerror(errno));
  179. free(fname);
  180. return false;
  181. }
  182. #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
  183. struct stat s;
  184. if(!fstat(fileno(fp), &s)) {
  185. if(s.st_mode & ~0100700) {
  186. logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
  187. }
  188. } else {
  189. logger(LOG_WARNING, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
  190. }
  191. #endif
  192. myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
  193. fclose(fp);
  194. if(!myself->connection->rsa_key) {
  195. logger(LOG_ERR, "Reading RSA private key file `%s' failed: %s",
  196. fname, strerror(errno));
  197. free(fname);
  198. return false;
  199. }
  200. free(fname);
  201. return true;
  202. }
  203. /*
  204. Read Subnets from all host config files
  205. */
  206. void load_all_subnets(void) {
  207. DIR *dir;
  208. struct dirent *ent;
  209. char *dname;
  210. char *fname;
  211. avl_tree_t *config_tree;
  212. config_t *cfg;
  213. subnet_t *s, *s2;
  214. node_t *n;
  215. xasprintf(&dname, "%s/hosts", confbase);
  216. dir = opendir(dname);
  217. if(!dir) {
  218. logger(LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
  219. free(dname);
  220. return;
  221. }
  222. while((ent = readdir(dir))) {
  223. if(!check_id(ent->d_name)) {
  224. continue;
  225. }
  226. n = lookup_node(ent->d_name);
  227. #ifdef _DIRENT_HAVE_D_TYPE
  228. //if(ent->d_type != DT_REG)
  229. // continue;
  230. #endif
  231. xasprintf(&fname, "%s/hosts/%s", confbase, ent->d_name);
  232. init_configuration(&config_tree);
  233. read_config_options(config_tree, ent->d_name);
  234. read_config_file(config_tree, fname);
  235. free(fname);
  236. if(!n) {
  237. n = new_node();
  238. n->name = xstrdup(ent->d_name);
  239. node_add(n);
  240. }
  241. for(cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) {
  242. if(!get_config_subnet(cfg, &s)) {
  243. continue;
  244. }
  245. if((s2 = lookup_subnet(n, s))) {
  246. s2->expires = -1;
  247. } else {
  248. subnet_add(n, s);
  249. }
  250. }
  251. exit_configuration(&config_tree);
  252. }
  253. closedir(dir);
  254. }
  255. char *get_name(void) {
  256. char *name = NULL;
  257. get_config_string(lookup_config(config_tree, "Name"), &name);
  258. if(!name) {
  259. return NULL;
  260. }
  261. if(*name == '$') {
  262. char *envname = getenv(name + 1);
  263. char hostname[32] = "";
  264. if(!envname) {
  265. if(strcmp(name + 1, "HOST")) {
  266. fprintf(stderr, "Invalid Name: environment variable %s does not exist\n", name + 1);
  267. free(name);
  268. return false;
  269. }
  270. if(gethostname(hostname, sizeof(hostname)) || !*hostname) {
  271. fprintf(stderr, "Could not get hostname: %s\n", strerror(errno));
  272. free(name);
  273. return false;
  274. }
  275. hostname[31] = 0;
  276. envname = hostname;
  277. }
  278. free(name);
  279. name = xstrdup(envname);
  280. for(char *c = name; *c; c++)
  281. if(!isalnum(*c)) {
  282. *c = '_';
  283. }
  284. }
  285. if(!check_id(name)) {
  286. logger(LOG_ERR, "Invalid name for myself!");
  287. free(name);
  288. return false;
  289. }
  290. return name;
  291. }
  292. /*
  293. Configure node_t myself and set up the local sockets (listen only)
  294. */
  295. static bool setup_myself(void) {
  296. config_t *cfg;
  297. subnet_t *subnet;
  298. char *name, *hostname, *mode, *afname, *cipher, *digest, *type;
  299. char *fname = NULL;
  300. char *address = NULL;
  301. char *proxy = NULL;
  302. char *space;
  303. char *envp[5] = {0};
  304. struct addrinfo *ai, *aip, hint = {0};
  305. bool choice;
  306. int i, err;
  307. int replaywin_int;
  308. bool port_specified = false;
  309. myself = new_node();
  310. myself->connection = new_connection();
  311. myself->hostname = xstrdup("MYSELF");
  312. myself->connection->hostname = xstrdup("MYSELF");
  313. myself->connection->options = 0;
  314. myself->connection->protocol_version = PROT_CURRENT;
  315. if(!(name = get_name())) {
  316. logger(LOG_ERR, "Name for tinc daemon required!");
  317. return false;
  318. }
  319. /* Read tinc.conf and our own host config file */
  320. myself->name = name;
  321. myself->connection->name = xstrdup(name);
  322. xasprintf(&fname, "%s/hosts/%s", confbase, name);
  323. read_config_options(config_tree, name);
  324. read_config_file(config_tree, fname);
  325. free(fname);
  326. if(!read_rsa_private_key()) {
  327. return false;
  328. }
  329. if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) {
  330. myport = xstrdup("655");
  331. } else {
  332. port_specified = true;
  333. }
  334. /* Ensure myport is numeric */
  335. if(!atoi(myport)) {
  336. struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
  337. sockaddr_t sa;
  338. if(!ai || !ai->ai_addr) {
  339. return false;
  340. }
  341. free(myport);
  342. memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
  343. sockaddr2str(&sa, NULL, &myport);
  344. }
  345. if(get_config_string(lookup_config(config_tree, "Proxy"), &proxy)) {
  346. if((space = strchr(proxy, ' '))) {
  347. *space++ = 0;
  348. }
  349. if(!strcasecmp(proxy, "none")) {
  350. proxytype = PROXY_NONE;
  351. } else if(!strcasecmp(proxy, "socks4")) {
  352. proxytype = PROXY_SOCKS4;
  353. } else if(!strcasecmp(proxy, "socks4a")) {
  354. proxytype = PROXY_SOCKS4A;
  355. } else if(!strcasecmp(proxy, "socks5")) {
  356. proxytype = PROXY_SOCKS5;
  357. } else if(!strcasecmp(proxy, "http")) {
  358. proxytype = PROXY_HTTP;
  359. } else if(!strcasecmp(proxy, "exec")) {
  360. proxytype = PROXY_EXEC;
  361. } else {
  362. logger(LOG_ERR, "Unknown proxy type %s!", proxy);
  363. free(proxy);
  364. return false;
  365. }
  366. switch(proxytype) {
  367. case PROXY_NONE:
  368. default:
  369. break;
  370. case PROXY_EXEC:
  371. if(!space || !*space) {
  372. logger(LOG_ERR, "Argument expected for proxy type exec!");
  373. free(proxy);
  374. return false;
  375. }
  376. proxyhost = xstrdup(space);
  377. break;
  378. case PROXY_SOCKS4:
  379. case PROXY_SOCKS4A:
  380. case PROXY_SOCKS5:
  381. case PROXY_HTTP:
  382. proxyhost = space;
  383. if(space && (space = strchr(space, ' '))) {
  384. *space++ = 0, proxyport = space;
  385. }
  386. if(space && (space = strchr(space, ' '))) {
  387. *space++ = 0, proxyuser = space;
  388. }
  389. if(space && (space = strchr(space, ' '))) {
  390. *space++ = 0, proxypass = space;
  391. }
  392. if(!proxyhost || !*proxyhost || !proxyport || !*proxyport) {
  393. logger(LOG_ERR, "Host and port argument expected for proxy!");
  394. free(proxy);
  395. return false;
  396. }
  397. proxyhost = xstrdup(proxyhost);
  398. proxyport = xstrdup(proxyport);
  399. if(proxyuser && *proxyuser) {
  400. proxyuser = xstrdup(proxyuser);
  401. }
  402. if(proxypass && *proxypass) {
  403. proxypass = xstrdup(proxypass);
  404. }
  405. break;
  406. }
  407. free(proxy);
  408. }
  409. /* Read in all the subnets specified in the host configuration file */
  410. cfg = lookup_config(config_tree, "Subnet");
  411. while(cfg) {
  412. if(!get_config_subnet(cfg, &subnet)) {
  413. return false;
  414. }
  415. subnet_add(myself, subnet);
  416. cfg = lookup_config_next(config_tree, cfg);
  417. }
  418. /* Check some options */
  419. if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) {
  420. myself->options |= OPTION_INDIRECT;
  421. }
  422. if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) {
  423. myself->options |= OPTION_TCPONLY;
  424. }
  425. if(myself->options & OPTION_TCPONLY) {
  426. myself->options |= OPTION_INDIRECT;
  427. }
  428. get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
  429. get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets);
  430. get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver);
  431. get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
  432. strictsubnets |= tunnelserver;
  433. if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
  434. if(!strcasecmp(mode, "router")) {
  435. routing_mode = RMODE_ROUTER;
  436. } else if(!strcasecmp(mode, "switch")) {
  437. routing_mode = RMODE_SWITCH;
  438. } else if(!strcasecmp(mode, "hub")) {
  439. routing_mode = RMODE_HUB;
  440. } else {
  441. logger(LOG_ERR, "Invalid routing mode!");
  442. free(mode);
  443. return false;
  444. }
  445. free(mode);
  446. }
  447. if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
  448. if(!strcasecmp(mode, "off")) {
  449. forwarding_mode = FMODE_OFF;
  450. } else if(!strcasecmp(mode, "internal")) {
  451. forwarding_mode = FMODE_INTERNAL;
  452. } else if(!strcasecmp(mode, "kernel")) {
  453. forwarding_mode = FMODE_KERNEL;
  454. } else {
  455. logger(LOG_ERR, "Invalid forwarding mode!");
  456. free(mode);
  457. return false;
  458. }
  459. free(mode);
  460. }
  461. choice = !(myself->options & OPTION_TCPONLY);
  462. get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice);
  463. if(choice) {
  464. myself->options |= OPTION_PMTU_DISCOVERY;
  465. }
  466. choice = true;
  467. get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice);
  468. if(choice) {
  469. myself->options |= OPTION_CLAMP_MSS;
  470. }
  471. get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
  472. get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
  473. if(get_config_string(lookup_config(config_tree, "Broadcast"), &mode)) {
  474. if(!strcasecmp(mode, "no")) {
  475. broadcast_mode = BMODE_NONE;
  476. } else if(!strcasecmp(mode, "yes") || !strcasecmp(mode, "mst")) {
  477. broadcast_mode = BMODE_MST;
  478. } else if(!strcasecmp(mode, "direct")) {
  479. broadcast_mode = BMODE_DIRECT;
  480. } else {
  481. logger(LOG_ERR, "Invalid broadcast mode!");
  482. free(mode);
  483. return false;
  484. }
  485. free(mode);
  486. }
  487. #if !defined(SOL_IP) || !defined(IP_TOS)
  488. if(priorityinheritance) {
  489. logger(LOG_WARNING, "%s not supported on this platform for IPv4 connection", "PriorityInheritance");
  490. }
  491. #endif
  492. #if !defined(IPPROTO_IPV6) || !defined(IPV6_TCLASS)
  493. if(priorityinheritance) {
  494. logger(LOG_WARNING, "%s not supported on this platform for IPv6 connection", "PriorityInheritance");
  495. }
  496. #endif
  497. if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) {
  498. macexpire = 600;
  499. }
  500. if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) {
  501. if(maxtimeout <= 0) {
  502. logger(LOG_ERR, "Bogus maximum timeout!");
  503. return false;
  504. }
  505. } else {
  506. maxtimeout = 900;
  507. }
  508. if(get_config_int(lookup_config(config_tree, "MinTimeout"), &mintimeout)) {
  509. if(mintimeout < 0) {
  510. logger(LOG_ERR, "Bogus minimum timeout!");
  511. return false;
  512. }
  513. if(mintimeout > maxtimeout) {
  514. logger(LOG_WARNING, "Minimum timeout (%d s) cannot be larger than maximum timeout (%d s). Correcting !", mintimeout, maxtimeout);
  515. mintimeout = maxtimeout;
  516. }
  517. } else {
  518. mintimeout = 0;
  519. }
  520. if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
  521. if(udp_rcvbuf <= 0) {
  522. logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
  523. return false;
  524. }
  525. }
  526. if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
  527. if(udp_sndbuf <= 0) {
  528. logger(LOG_ERR, "UDPSndBuf cannot be negative!");
  529. return false;
  530. }
  531. }
  532. if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
  533. if(replaywin_int < 0) {
  534. logger(LOG_ERR, "ReplayWindow cannot be negative!");
  535. return false;
  536. }
  537. replaywin = (unsigned)replaywin_int;
  538. }
  539. if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
  540. if(!strcasecmp(afname, "IPv4")) {
  541. addressfamily = AF_INET;
  542. } else if(!strcasecmp(afname, "IPv6")) {
  543. addressfamily = AF_INET6;
  544. } else if(!strcasecmp(afname, "any")) {
  545. addressfamily = AF_UNSPEC;
  546. } else {
  547. logger(LOG_ERR, "Invalid address family!");
  548. free(afname);
  549. return false;
  550. }
  551. free(afname);
  552. }
  553. get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
  554. /* Generate packet encryption key */
  555. if(get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) {
  556. if(!strcasecmp(cipher, "none")) {
  557. myself->incipher = NULL;
  558. } else {
  559. myself->incipher = EVP_get_cipherbyname(cipher);
  560. if(!myself->incipher) {
  561. logger(LOG_ERR, "Unrecognized cipher type!");
  562. free(cipher);
  563. return false;
  564. }
  565. }
  566. free(cipher);
  567. } else {
  568. myself->incipher = EVP_aes_256_cbc();
  569. }
  570. if(myself->incipher) {
  571. myself->inkeylength = EVP_CIPHER_key_length(myself->incipher) + EVP_CIPHER_iv_length(myself->incipher);
  572. } else {
  573. myself->inkeylength = 1;
  574. }
  575. /* We need to use a stream mode for the meta protocol. Use AES for this,
  576. but try to match the key size with the one from the cipher selected
  577. by Cipher.
  578. If Cipher is set to none, still use a low level of encryption for the
  579. meta protocol.
  580. */
  581. int keylen = myself->incipher ? EVP_CIPHER_key_length(myself->incipher) : 0;
  582. if(keylen <= 16) {
  583. myself->connection->outcipher = EVP_aes_128_cfb();
  584. } else if(keylen <= 24) {
  585. myself->connection->outcipher = EVP_aes_192_cfb();
  586. } else {
  587. myself->connection->outcipher = EVP_aes_256_cfb();
  588. }
  589. if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) {
  590. keylifetime = 3600;
  591. }
  592. keyexpires = now + keylifetime;
  593. /* Check if we want to use message authentication codes... */
  594. if(get_config_string(lookup_config(config_tree, "Digest"), &digest)) {
  595. if(!strcasecmp(digest, "none")) {
  596. myself->indigest = NULL;
  597. } else {
  598. myself->indigest = EVP_get_digestbyname(digest);
  599. if(!myself->indigest) {
  600. logger(LOG_ERR, "Unrecognized digest type!");
  601. free(digest);
  602. return false;
  603. }
  604. }
  605. free(digest);
  606. } else {
  607. myself->indigest = EVP_sha256();
  608. }
  609. myself->connection->outdigest = EVP_sha256();
  610. if(get_config_int(lookup_config(config_tree, "MACLength"), &myself->inmaclength)) {
  611. if(myself->indigest) {
  612. if(myself->inmaclength > EVP_MD_size(myself->indigest)) {
  613. logger(LOG_ERR, "MAC length exceeds size of digest!");
  614. return false;
  615. } else if(myself->inmaclength < 0) {
  616. logger(LOG_ERR, "Bogus MAC length!");
  617. return false;
  618. }
  619. }
  620. } else {
  621. myself->inmaclength = 4;
  622. }
  623. myself->connection->outmaclength = 0;
  624. /* Compression */
  625. if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
  626. if(myself->incompression < 0 || myself->incompression > 11) {
  627. logger(LOG_ERR, "Bogus compression level!");
  628. return false;
  629. }
  630. } else {
  631. myself->incompression = 0;
  632. }
  633. myself->connection->outcompression = 0;
  634. /* Done */
  635. myself->nexthop = myself;
  636. myself->via = myself;
  637. myself->status.reachable = true;
  638. node_add(myself);
  639. graph();
  640. if(strictsubnets) {
  641. load_all_subnets();
  642. }
  643. /* Open device */
  644. devops = os_devops;
  645. if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
  646. if(!strcasecmp(type, "dummy")) {
  647. devops = dummy_devops;
  648. } else if(!strcasecmp(type, "raw_socket")) {
  649. devops = raw_socket_devops;
  650. } else if(!strcasecmp(type, "multicast")) {
  651. devops = multicast_devops;
  652. }
  653. #ifdef ENABLE_UML
  654. else if(!strcasecmp(type, "uml")) {
  655. devops = uml_devops;
  656. }
  657. #endif
  658. #ifdef ENABLE_VDE
  659. else if(!strcasecmp(type, "vde")) {
  660. devops = vde_devops;
  661. }
  662. #endif
  663. free(type);
  664. }
  665. if(!devops.setup()) {
  666. return false;
  667. }
  668. /* Run tinc-up script to further initialize the tap interface */
  669. xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
  670. xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
  671. xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
  672. xasprintf(&envp[3], "NAME=%s", myself->name);
  673. #ifdef HAVE_MINGW
  674. Sleep(1000);
  675. #endif
  676. #ifdef HAVE_CYGWIN
  677. sleep(1);
  678. #endif
  679. execute_script("tinc-up", envp);
  680. for(i = 0; i < 4; i++) {
  681. free(envp[i]);
  682. }
  683. /* Run subnet-up scripts for our own subnets */
  684. subnet_update(myself, NULL, true);
  685. /* Open sockets */
  686. if(!do_detach && getenv("LISTEN_FDS")) {
  687. sockaddr_t sa;
  688. socklen_t salen;
  689. listen_sockets = atoi(getenv("LISTEN_FDS"));
  690. #ifdef HAVE_UNSETENV
  691. unsetenv("LISTEN_FDS");
  692. #endif
  693. if(listen_sockets > MAXSOCKETS) {
  694. logger(LOG_ERR, "Too many listening sockets");
  695. return false;
  696. }
  697. for(i = 0; i < listen_sockets; i++) {
  698. salen = sizeof(sa);
  699. if(getsockname(i + 3, &sa.sa, &salen) < 0) {
  700. logger(LOG_ERR, "Could not get address of listen fd %d: %s", i + 3, sockstrerror(errno));
  701. return false;
  702. }
  703. listen_socket[i].tcp = i + 3;
  704. #ifdef FD_CLOEXEC
  705. fcntl(i + 3, F_SETFD, FD_CLOEXEC);
  706. #endif
  707. listen_socket[i].udp = setup_vpn_in_socket(&sa);
  708. if(listen_socket[i].udp < 0) {
  709. return false;
  710. }
  711. ifdebug(CONNECTIONS) {
  712. hostname = sockaddr2hostname(&sa);
  713. logger(LOG_NOTICE, "Listening on %s", hostname);
  714. free(hostname);
  715. }
  716. memcpy(&listen_socket[i].sa, &sa, salen);
  717. }
  718. } else {
  719. listen_sockets = 0;
  720. cfg = lookup_config(config_tree, "BindToAddress");
  721. do {
  722. get_config_string(cfg, &address);
  723. if(cfg) {
  724. cfg = lookup_config_next(config_tree, cfg);
  725. }
  726. char *port = myport;
  727. if(address) {
  728. char *space = strchr(address, ' ');
  729. if(space) {
  730. *space++ = 0;
  731. port = space;
  732. }
  733. if(!strcmp(address, "*")) {
  734. *address = 0;
  735. }
  736. }
  737. hint.ai_family = addressfamily;
  738. hint.ai_socktype = SOCK_STREAM;
  739. hint.ai_protocol = IPPROTO_TCP;
  740. hint.ai_flags = AI_PASSIVE;
  741. #if HAVE_DECL_RES_INIT
  742. // ensure glibc reloads /etc/resolv.conf.
  743. res_init();
  744. #endif
  745. err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
  746. free(address);
  747. if(err || !ai) {
  748. logger(LOG_ERR, "System call `%s' failed: %s", "getaddrinfo",
  749. gai_strerror(err));
  750. return false;
  751. }
  752. for(aip = ai; aip; aip = aip->ai_next) {
  753. if(listen_sockets >= MAXSOCKETS) {
  754. logger(LOG_ERR, "Too many listening sockets");
  755. return false;
  756. }
  757. listen_socket[listen_sockets].tcp =
  758. setup_listen_socket((sockaddr_t *) aip->ai_addr);
  759. if(listen_socket[listen_sockets].tcp < 0) {
  760. continue;
  761. }
  762. listen_socket[listen_sockets].udp =
  763. setup_vpn_in_socket((sockaddr_t *) aip->ai_addr);
  764. if(listen_socket[listen_sockets].udp < 0) {
  765. continue;
  766. }
  767. ifdebug(CONNECTIONS) {
  768. hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
  769. logger(LOG_NOTICE, "Listening on %s", hostname);
  770. free(hostname);
  771. }
  772. memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
  773. listen_sockets++;
  774. }
  775. freeaddrinfo(ai);
  776. } while(cfg);
  777. }
  778. if(!listen_sockets) {
  779. logger(LOG_ERR, "Unable to create any listening socket!");
  780. return false;
  781. }
  782. /* If no Port option was specified, set myport to the port used by the first listening socket. */
  783. if(!port_specified) {
  784. sockaddr_t sa;
  785. socklen_t salen = sizeof(sa);
  786. if(!getsockname(listen_socket[0].udp, &sa.sa, &salen)) {
  787. free(myport);
  788. sockaddr2str(&sa, NULL, &myport);
  789. if(!myport) {
  790. myport = xstrdup("655");
  791. }
  792. }
  793. }
  794. /* Done. */
  795. logger(LOG_NOTICE, "Ready");
  796. return true;
  797. }
  798. /*
  799. initialize network
  800. */
  801. bool setup_network(void) {
  802. now = time(NULL);
  803. init_events();
  804. init_connections();
  805. init_subnets();
  806. init_nodes();
  807. init_edges();
  808. init_requests();
  809. if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) {
  810. if(pinginterval < 1) {
  811. pinginterval = 86400;
  812. }
  813. } else {
  814. pinginterval = 60;
  815. }
  816. if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) {
  817. pingtimeout = 5;
  818. }
  819. if(pingtimeout < 1 || pingtimeout > pinginterval) {
  820. pingtimeout = pinginterval;
  821. }
  822. if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) {
  823. maxoutbufsize = 10 * MTU;
  824. }
  825. if(!setup_myself()) {
  826. return false;
  827. }
  828. return true;
  829. }
  830. /*
  831. close all open network connections
  832. */
  833. void close_network_connections(void) {
  834. avl_node_t *node, *next;
  835. connection_t *c;
  836. char *envp[5] = {0};
  837. int i;
  838. for(node = connection_tree->head; node; node = next) {
  839. next = node->next;
  840. c = node->data;
  841. c->outgoing = NULL;
  842. terminate_connection(c, false);
  843. }
  844. for(list_node_t *node = outgoing_list->head; node; node = node->next) {
  845. outgoing_t *outgoing = node->data;
  846. if(outgoing->event) {
  847. event_del(outgoing->event);
  848. }
  849. }
  850. list_delete_list(outgoing_list);
  851. if(myself && myself->connection) {
  852. subnet_update(myself, NULL, false);
  853. terminate_connection(myself->connection, false);
  854. free_connection(myself->connection);
  855. }
  856. for(i = 0; i < listen_sockets; i++) {
  857. close(listen_socket[i].tcp);
  858. close(listen_socket[i].udp);
  859. }
  860. xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
  861. xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
  862. xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
  863. xasprintf(&envp[3], "NAME=%s", myself->name);
  864. exit_requests();
  865. exit_edges();
  866. exit_subnets();
  867. exit_nodes();
  868. exit_connections();
  869. exit_events();
  870. execute_script("tinc-down", envp);
  871. if(myport) {
  872. free(myport);
  873. }
  874. for(i = 0; i < 4; i++) {
  875. free(envp[i]);
  876. }
  877. devops.close();
  878. return;
  879. }