net_setup.c 23 KB

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