router.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char router_c_id[] =
  7. "$Id$";
  8. #include "or.h"
  9. /**
  10. * \file router.c
  11. * \brief OR functionality, including key maintenance, generating
  12. * and uploading server descriptors, retrying OR connections.
  13. **/
  14. extern long stats_n_seconds_working;
  15. /* Exposed for test.c. */ void get_platform_str(char *platform, size_t len);
  16. /************************************************************/
  17. /*****
  18. * Key management: ORs only.
  19. *****/
  20. /** Private keys for this OR. There is also an SSL key managed by tortls.c.
  21. */
  22. static tor_mutex_t *key_lock=NULL;
  23. static time_t onionkey_set_at=0; /* When was onionkey last changed? */
  24. static crypto_pk_env_t *onionkey=NULL;
  25. static crypto_pk_env_t *lastonionkey=NULL;
  26. static crypto_pk_env_t *identitykey=NULL;
  27. /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
  28. * to update onionkey correctly, call rotate_onion_key().
  29. */
  30. void
  31. set_onion_key(crypto_pk_env_t *k)
  32. {
  33. tor_mutex_acquire(key_lock);
  34. onionkey = k;
  35. onionkey_set_at = time(NULL);
  36. tor_mutex_release(key_lock);
  37. mark_my_descriptor_dirty();
  38. }
  39. /** Return the current onion key. Requires that the onion key has been
  40. * loaded or generated. */
  41. crypto_pk_env_t *
  42. get_onion_key(void)
  43. {
  44. tor_assert(onionkey);
  45. return onionkey;
  46. }
  47. /** Store a copy of the current onion key into *<b>key</b>, and a copy
  48. * of the most recent onion key into *<b>last</b>.
  49. */
  50. void
  51. dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
  52. {
  53. tor_assert(key);
  54. tor_assert(last);
  55. tor_mutex_acquire(key_lock);
  56. tor_assert(onionkey);
  57. *key = crypto_pk_dup_key(onionkey);
  58. if (lastonionkey)
  59. *last = crypto_pk_dup_key(lastonionkey);
  60. else
  61. *last = NULL;
  62. tor_mutex_release(key_lock);
  63. }
  64. /** Return the time when the onion key was last set. This is either the time
  65. * when the process launched, or the time of the most recent key rotation since
  66. * the process launched.
  67. */
  68. time_t
  69. get_onion_key_set_at(void)
  70. {
  71. return onionkey_set_at;
  72. }
  73. /** Set the current identity key to k.
  74. */
  75. void
  76. set_identity_key(crypto_pk_env_t *k)
  77. {
  78. if (identitykey)
  79. crypto_free_pk_env(identitykey);
  80. identitykey = k;
  81. }
  82. /** Returns the current identity key; requires that the identity key has been
  83. * set.
  84. */
  85. crypto_pk_env_t *
  86. get_identity_key(void)
  87. {
  88. tor_assert(identitykey);
  89. return identitykey;
  90. }
  91. /** Return true iff the identity key has been set. */
  92. int
  93. identity_key_is_set(void)
  94. {
  95. return identitykey != NULL;
  96. }
  97. /** Replace the previous onion key with the current onion key, and generate
  98. * a new previous onion key. Immediately after calling this function,
  99. * the OR should:
  100. * - schedule all previous cpuworkers to shut down _after_ processing
  101. * pending work. (This will cause fresh cpuworkers to be generated.)
  102. * - generate and upload a fresh routerinfo.
  103. */
  104. void
  105. rotate_onion_key(void)
  106. {
  107. char fname[512];
  108. char fname_prev[512];
  109. crypto_pk_env_t *prkey;
  110. tor_snprintf(fname,sizeof(fname),
  111. "%s/keys/secret_onion_key",get_options()->DataDirectory);
  112. tor_snprintf(fname_prev,sizeof(fname_prev),
  113. "%s/keys/secret_onion_key.old",get_options()->DataDirectory);
  114. if (!(prkey = crypto_new_pk_env())) {
  115. err(LD_GENERAL,"Error creating crypto environment.");
  116. goto error;
  117. }
  118. if (crypto_pk_generate_key(prkey)) {
  119. err(LD_BUG,"Error generating onion key");
  120. goto error;
  121. }
  122. if (file_status(fname) == FN_FILE) {
  123. if (replace_file(fname, fname_prev))
  124. goto error;
  125. }
  126. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  127. err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
  128. goto error;
  129. }
  130. info(LD_GENERAL, "Rotating onion key");
  131. tor_mutex_acquire(key_lock);
  132. if (lastonionkey)
  133. crypto_free_pk_env(lastonionkey);
  134. lastonionkey = onionkey;
  135. onionkey = prkey;
  136. onionkey_set_at = time(NULL);
  137. tor_mutex_release(key_lock);
  138. mark_my_descriptor_dirty();
  139. return;
  140. error:
  141. warn(LD_GENERAL, "Couldn't rotate onion key.");
  142. }
  143. /* Read an RSA secret key key from a file that was once named fname_old,
  144. * but is now named fname_new. Rename the file from old to new as needed.
  145. */
  146. static crypto_pk_env_t *
  147. init_key_from_file_name_changed(const char *fname_old,
  148. const char *fname_new)
  149. {
  150. if (file_status(fname_new) == FN_FILE || file_status(fname_old) != FN_FILE)
  151. /* The new filename is there, or both are, or neither is. */
  152. return init_key_from_file(fname_new);
  153. /* The old filename exists, and the new one doesn't. Rename and load. */
  154. if (rename(fname_old, fname_new) < 0) {
  155. log_fn(LOG_ERR, LD_FS, "Couldn't rename \"%s\" to \"%s\": %s",
  156. fname_old, fname_new, strerror(errno));
  157. return NULL;
  158. }
  159. return init_key_from_file(fname_new);
  160. }
  161. /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
  162. * create a new RSA key and save it in <b>fname</b>. Return the read/created
  163. * key, or NULL on error.
  164. */
  165. crypto_pk_env_t *
  166. init_key_from_file(const char *fname)
  167. {
  168. crypto_pk_env_t *prkey = NULL;
  169. FILE *file = NULL;
  170. if (!(prkey = crypto_new_pk_env())) {
  171. err(LD_GENERAL,"Error creating crypto environment.");
  172. goto error;
  173. }
  174. switch (file_status(fname)) {
  175. case FN_DIR:
  176. case FN_ERROR:
  177. err(LD_FS,"Can't read key from \"%s\"", fname);
  178. goto error;
  179. case FN_NOENT:
  180. info(LD_GENERAL, "No key found in \"%s\"; generating fresh key.", fname);
  181. if (crypto_pk_generate_key(prkey)) {
  182. err(LD_GENERAL,"Error generating onion key");
  183. goto error;
  184. }
  185. if (crypto_pk_check_key(prkey) <= 0) {
  186. err(LD_GENERAL,"Generated key seems invalid");
  187. goto error;
  188. }
  189. info(LD_GENERAL, "Generated key seems valid");
  190. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  191. err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
  192. goto error;
  193. }
  194. return prkey;
  195. case FN_FILE:
  196. if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
  197. err(LD_GENERAL,"Error loading private key.");
  198. goto error;
  199. }
  200. return prkey;
  201. default:
  202. tor_assert(0);
  203. }
  204. error:
  205. if (prkey)
  206. crypto_free_pk_env(prkey);
  207. if (file)
  208. fclose(file);
  209. return NULL;
  210. }
  211. /** Initialize all OR private keys, and the TLS context, as necessary.
  212. * On OPs, this only initializes the tls context.
  213. */
  214. int
  215. init_keys(void)
  216. {
  217. /* XXX009 Two problems with how this is called:
  218. * 1. It should be idempotent for servers, so we can call init_keys
  219. * as much as we need to.
  220. */
  221. char keydir[512];
  222. char keydir2[512];
  223. char fingerprint[FINGERPRINT_LEN+1];
  224. /*nickname fp\n\0 */
  225. char fingerprint_line[FINGERPRINT_LEN+MAX_NICKNAME_LEN+3];
  226. char *cp;
  227. const char *mydesc, *datadir;
  228. crypto_pk_env_t *prkey;
  229. char digest[20];
  230. or_options_t *options = get_options();
  231. if (!key_lock)
  232. key_lock = tor_mutex_new();
  233. /* OP's don't need persistent keys; just make up an identity and
  234. * initialize the TLS context. */
  235. if (!server_mode(options)) {
  236. if (!(prkey = crypto_new_pk_env()))
  237. return -1;
  238. if (crypto_pk_generate_key(prkey))
  239. return -1;
  240. set_identity_key(prkey);
  241. /* Create a TLS context; default the client nickname to "client". */
  242. if (tor_tls_context_new(get_identity_key(), 1,
  243. options->Nickname ? options->Nickname : "client",
  244. MAX_SSL_KEY_LIFETIME) < 0) {
  245. err(LD_GENERAL,"Error creating TLS context for OP.");
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. /* Make sure DataDirectory exists, and is private. */
  251. datadir = options->DataDirectory;
  252. if (check_private_dir(datadir, CPD_CREATE)) {
  253. return -1;
  254. }
  255. /* Check the key directory. */
  256. tor_snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
  257. if (check_private_dir(keydir, CPD_CREATE)) {
  258. return -1;
  259. }
  260. cp = keydir + strlen(keydir); /* End of string. */
  261. /* 1. Read identity key. Make it if none is found. */
  262. tor_snprintf(keydir,sizeof(keydir),"%s/keys/identity.key",datadir);
  263. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_id_key",datadir);
  264. info(LD_GENERAL,"Reading/making identity key \"%s\"...",keydir2);
  265. prkey = init_key_from_file_name_changed(keydir,keydir2);
  266. if (!prkey) return -1;
  267. set_identity_key(prkey);
  268. /* 2. Read onion key. Make it if none is found. */
  269. tor_snprintf(keydir,sizeof(keydir),"%s/keys/onion.key",datadir);
  270. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_onion_key",datadir);
  271. info(LD_GENERAL,"Reading/making onion key \"%s\"...",keydir2);
  272. prkey = init_key_from_file_name_changed(keydir,keydir2);
  273. if (!prkey) return -1;
  274. set_onion_key(prkey);
  275. tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key.old",datadir);
  276. if (file_status(keydir) == FN_FILE) {
  277. prkey = init_key_from_file(keydir);
  278. if (prkey)
  279. lastonionkey = prkey;
  280. }
  281. /* 3. Initialize link key and TLS context. */
  282. if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
  283. MAX_SSL_KEY_LIFETIME) < 0) {
  284. err(LD_GENERAL,"Error initializing TLS context");
  285. return -1;
  286. }
  287. /* 4. Dump router descriptor to 'router.desc' */
  288. /* Must be called after keys are initialized. */
  289. mydesc = router_get_my_descriptor();
  290. if (!mydesc) {
  291. err(LD_GENERAL,"Error initializing descriptor.");
  292. return -1;
  293. }
  294. if (authdir_mode(options)) {
  295. const char *m;
  296. /* We need to add our own fingerprint so it gets recognized. */
  297. if (dirserv_add_own_fingerprint(options->Nickname, get_identity_key())) {
  298. err(LD_GENERAL,"Error adding own fingerprint to approved set");
  299. return -1;
  300. }
  301. if (dirserv_add_descriptor(mydesc, &m) < 0) {
  302. err(LD_GENERAL,"Unable to add own descriptor to directory: %s",
  303. m?m:"<unknown error>");
  304. return -1;
  305. }
  306. }
  307. tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", datadir);
  308. info(LD_GENERAL,"Dumping descriptor to \"%s\"...",keydir);
  309. if (write_str_to_file(keydir, mydesc,0)) {
  310. return -1;
  311. }
  312. /* 5. Dump fingerprint to 'fingerprint' */
  313. tor_snprintf(keydir,sizeof(keydir),"%s/fingerprint", datadir);
  314. info(LD_GENERAL,"Dumping fingerprint to \"%s\"...",keydir);
  315. if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint, 1)<0) {
  316. err(LD_GENERAL,"Error computing fingerprint");
  317. return -1;
  318. }
  319. tor_assert(strlen(options->Nickname) <= MAX_NICKNAME_LEN);
  320. if (tor_snprintf(fingerprint_line, sizeof(fingerprint_line),
  321. "%s %s\n",options->Nickname, fingerprint) < 0) {
  322. err(LD_GENERAL,"Error writing fingerprint line");
  323. return -1;
  324. }
  325. if (write_str_to_file(keydir, fingerprint_line, 0)) {
  326. err(LD_FS, "Error writing fingerprint line to file");
  327. return -1;
  328. }
  329. if (!authdir_mode(options))
  330. return 0;
  331. /* 6. [authdirserver only] load approved-routers file */
  332. tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", datadir);
  333. info(LD_DIRSERV,"Loading approved fingerprints from \"%s\"...",keydir);
  334. if (dirserv_parse_fingerprint_file(keydir) < 0) {
  335. err(LD_GENERAL,"Error loading fingerprints");
  336. return -1;
  337. }
  338. /* 6b. [authdirserver only] add own key to approved directories. */
  339. crypto_pk_get_digest(get_identity_key(), digest);
  340. if (!router_digest_is_trusted_dir(digest)) {
  341. add_trusted_dir_server(options->Nickname, NULL,
  342. (uint16_t)options->DirPort, digest,
  343. options->V1AuthoritativeDir);
  344. }
  345. /* success */
  346. return 0;
  347. }
  348. /* Keep track of whether we should upload our server descriptor,
  349. * and what type of server we are.
  350. */
  351. /** Whether we can reach our ORPort from the outside. */
  352. static int can_reach_or_port = 0;
  353. /** Whether we can reach our DirPort from the outside. */
  354. static int can_reach_dir_port = 0;
  355. /** Return 1 if or port is known reachable; else return 0. */
  356. int
  357. check_whether_orport_reachable(void)
  358. {
  359. or_options_t *options = get_options();
  360. return clique_mode(options) ||
  361. options->AssumeReachable ||
  362. can_reach_or_port;
  363. }
  364. /** Return 1 if we don't have a dirport configured, or if it's reachable. */
  365. int
  366. check_whether_dirport_reachable(void)
  367. {
  368. or_options_t *options = get_options();
  369. return !options->DirPort ||
  370. options->AssumeReachable ||
  371. we_are_hibernating() ||
  372. can_reach_dir_port;
  373. }
  374. /** Look at a variety of factors, and return 0 if we don't want to
  375. * advertise the fact that we have a DirPort open. Else return the
  376. * DirPort we want to advertise. */
  377. static int
  378. decide_to_advertise_dirport(or_options_t *options, routerinfo_t *router)
  379. {
  380. if (!router->dir_port) /* short circuit the rest of the function */
  381. return 0;
  382. if (authdir_mode(options)) /* always publish */
  383. return router->dir_port;
  384. if (we_are_hibernating())
  385. return 0;
  386. if (!check_whether_dirport_reachable())
  387. return 0;
  388. if (router->bandwidthcapacity >= router->bandwidthrate) {
  389. /* check if we might potentially hibernate. */
  390. if (options->AccountingMax != 0)
  391. return 0;
  392. /* also check if we're advertising a small amount, and have
  393. a "boring" DirPort. */
  394. if (router->bandwidthrate < 50000 && router->dir_port > 1024)
  395. return 0;
  396. }
  397. /* Sounds like a great idea. Let's publish it. */
  398. return router->dir_port;
  399. }
  400. /** Some time has passed, or we just got new directory information.
  401. * See if we currently believe our ORPort or DirPort to be
  402. * unreachable. If so, launch a new test for it.
  403. *
  404. * For ORPort, we simply try making a circuit that ends at ourselves.
  405. * Success is noticed in onionskin_answer().
  406. *
  407. * For DirPort, we make a connection via Tor to our DirPort and ask
  408. * for our own server descriptor.
  409. * Success is noticed in connection_dir_client_reached_eof().
  410. */
  411. void
  412. consider_testing_reachability(void)
  413. {
  414. routerinfo_t *me = router_get_my_routerinfo();
  415. if (!me) {
  416. warn(LD_BUG,"Bug: router_get_my_routerinfo() did not find my routerinfo?");
  417. return;
  418. }
  419. if (!check_whether_orport_reachable()) {
  420. circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, me, 0, 1, 1);
  421. }
  422. if (!check_whether_dirport_reachable()) {
  423. /* ask myself, via tor, for my server descriptor. */
  424. directory_initiate_command_router(me, DIR_PURPOSE_FETCH_SERVERDESC,
  425. 1, "authority", NULL, 0);
  426. }
  427. }
  428. /** Annotate that we found our ORPort reachable. */
  429. void
  430. router_orport_found_reachable(void)
  431. {
  432. if (!can_reach_or_port) {
  433. if (!clique_mode(get_options()))
  434. notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
  435. "the outside. Excellent.%s",
  436. get_options()->NoPublish ? "" : " Publishing server descriptor.");
  437. can_reach_or_port = 1;
  438. mark_my_descriptor_dirty();
  439. consider_publishable_server(time(NULL), 1);
  440. }
  441. }
  442. /** Annotate that we found our DirPort reachable. */
  443. void
  444. router_dirport_found_reachable(void)
  445. {
  446. if (!can_reach_dir_port) {
  447. notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
  448. "from the outside. Excellent.");
  449. can_reach_dir_port = 1;
  450. }
  451. }
  452. /** Our router has just moved to a new IP. Reset stats. */
  453. void
  454. server_has_changed_ip(void)
  455. {
  456. stats_n_seconds_working = 0;
  457. can_reach_or_port = 0;
  458. can_reach_dir_port = 0;
  459. mark_my_descriptor_dirty();
  460. }
  461. /** Return true iff we believe ourselves to be an authoritative
  462. * directory server.
  463. */
  464. int
  465. authdir_mode(or_options_t *options)
  466. {
  467. return options->AuthoritativeDir != 0;
  468. }
  469. /** Return true iff we try to stay connected to all ORs at once.
  470. */
  471. int
  472. clique_mode(or_options_t *options)
  473. {
  474. return authdir_mode(options);
  475. }
  476. /** Return true iff we are trying to be a server.
  477. */
  478. int
  479. server_mode(or_options_t *options)
  480. {
  481. if (options->ClientOnly) return 0;
  482. return (options->ORPort != 0 || options->ORListenAddress);
  483. }
  484. /** Remember if we've advertised ourselves to the dirservers. */
  485. static int server_is_advertised=0;
  486. /** Return true iff we have published our descriptor lately.
  487. */
  488. int
  489. advertised_server_mode(void)
  490. {
  491. return server_is_advertised;
  492. }
  493. /**
  494. * Called with a boolean: set whether we have recently published our
  495. * descriptor.
  496. */
  497. static void
  498. set_server_advertised(int s)
  499. {
  500. server_is_advertised = s;
  501. }
  502. /** Return true iff we are trying to be a socks proxy. */
  503. int
  504. proxy_mode(or_options_t *options)
  505. {
  506. return (options->SocksPort != 0 || options->SocksListenAddress);
  507. }
  508. /** Decide if we're a publishable server. We are a publishable server if:
  509. * - We don't have the ClientOnly option set
  510. * and
  511. * - We don't have the NoPublish option set
  512. * and
  513. * - We have ORPort set
  514. * and
  515. * - We believe we are reachable from the outside; or
  516. * - We have the AuthoritativeDirectory option set.
  517. */
  518. static int
  519. decide_if_publishable_server(time_t now)
  520. {
  521. or_options_t *options = get_options();
  522. if (options->ClientOnly)
  523. return 0;
  524. if (options->NoPublish)
  525. return 0;
  526. if (!server_mode(options))
  527. return 0;
  528. if (options->AuthoritativeDir)
  529. return 1;
  530. return check_whether_orport_reachable();
  531. }
  532. /** Initiate server descriptor upload as reasonable (if server is publishable,
  533. * etc). <b>force</b> is as for router_upload_dir_desc_to_dirservers.
  534. *
  535. * We need to rebuild the descriptor if it's dirty even if we're not
  536. * uploading, because our reachability testing *uses* our descriptor to
  537. * determine what IP address and ports to test.
  538. */
  539. void
  540. consider_publishable_server(time_t now, int force)
  541. {
  542. int rebuilt;
  543. if (!server_mode(get_options()))
  544. return;
  545. rebuilt = router_rebuild_descriptor(0);
  546. if (decide_if_publishable_server(now)) {
  547. set_server_advertised(1);
  548. if (rebuilt == 0)
  549. router_upload_dir_desc_to_dirservers(force);
  550. } else {
  551. set_server_advertised(0);
  552. }
  553. }
  554. /*
  555. * Clique maintenance
  556. */
  557. /** OR only: if in clique mode, try to open connections to all of the
  558. * other ORs we know about. Otherwise, open connections to those we
  559. * think are in clique mode.
  560. *
  561. * If <b>testing_reachability</b> is 0, try to open the connections
  562. * but only if we don't already have one. If it's 1, then we're an
  563. * auth dir server, and we should try to connect regardless of
  564. * whether we already have a connection open -- but if <b>try_all</b>
  565. * is 0, we want to load balance such that we only try a few connections
  566. * per call.
  567. */
  568. void
  569. router_retry_connections(int testing_reachability, int try_all)
  570. {
  571. time_t now = time(NULL);
  572. routerlist_t *rl = router_get_routerlist();
  573. or_options_t *options = get_options();
  574. static char ctr = 0;
  575. tor_assert(server_mode(options));
  576. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, router, {
  577. const char *id_digest = router->cache_info.identity_digest;
  578. if (router_is_me(router))
  579. continue;
  580. if (!clique_mode(options) && !router_is_clique_mode(router))
  581. continue;
  582. if ((testing_reachability &&
  583. (try_all || (((uint8_t)id_digest[0]) % 128) == ctr)) ||
  584. (!testing_reachability &&
  585. !connection_or_get_by_identity_digest(id_digest))) {
  586. debug(LD_OR,"%sconnecting to %s at %s:%u.",
  587. clique_mode(options) ? "(forced) " : "",
  588. router->nickname, router->address, router->or_port);
  589. /* Remember when we started trying to determine reachability */
  590. if (!router->testing_since)
  591. router->testing_since = now;
  592. connection_or_connect(router->addr, router->or_port,
  593. id_digest);
  594. }
  595. });
  596. if (testing_reachability && !try_all) /* increment ctr */
  597. ctr = (ctr + 1) % 128;
  598. }
  599. /** Return true iff this OR should try to keep connections open to all
  600. * other ORs. */
  601. int
  602. router_is_clique_mode(routerinfo_t *router)
  603. {
  604. if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
  605. return 1;
  606. return 0;
  607. }
  608. /*
  609. * OR descriptor generation.
  610. */
  611. /** My routerinfo. */
  612. static routerinfo_t *desc_routerinfo = NULL;
  613. /** Since when has our descriptor been "clean"? 0 if we need to regenerate it
  614. * now. */
  615. static time_t desc_clean_since = 0;
  616. /** Boolean: do we need to regenerate the above? */
  617. static int desc_needs_upload = 0;
  618. /** OR only: If <b>force</b> is true, or we haven't uploaded this
  619. * descriptor successfully yet, try to upload our signed descriptor to
  620. * all the directory servers we know about.
  621. */
  622. void
  623. router_upload_dir_desc_to_dirservers(int force)
  624. {
  625. const char *s;
  626. s = router_get_my_descriptor();
  627. if (!s) {
  628. warn(LD_GENERAL, "No descriptor; skipping upload");
  629. return;
  630. }
  631. if (!force && !desc_needs_upload)
  632. return;
  633. desc_needs_upload = 0;
  634. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  635. }
  636. /** OR only: Check whether my exit policy says to allow connection to
  637. * conn. Return 0 if we accept; non-0 if we reject.
  638. */
  639. int
  640. router_compare_to_my_exit_policy(connection_t *conn)
  641. {
  642. tor_assert(desc_routerinfo);
  643. /* make sure it's resolved to something. this way we can't get a
  644. 'maybe' below. */
  645. if (!conn->addr)
  646. return -1;
  647. return router_compare_addr_to_addr_policy(conn->addr, conn->port,
  648. desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
  649. }
  650. /** Return true iff I'm a server and <b>digest</b> is equal to
  651. * my identity digest. */
  652. int
  653. router_digest_is_me(const char *digest)
  654. {
  655. routerinfo_t *me = router_get_my_routerinfo();
  656. if (!me || memcmp(me->cache_info.identity_digest, digest, DIGEST_LEN))
  657. return 0;
  658. return 1;
  659. }
  660. /** A wrapper around router_digest_is_me(). */
  661. int
  662. router_is_me(routerinfo_t *router)
  663. {
  664. return router_digest_is_me(router->cache_info.identity_digest);
  665. }
  666. /** Return true iff <b>fp</b> is a hex fingerprint of my identity digest. */
  667. int
  668. router_fingerprint_is_me(const char *fp)
  669. {
  670. char digest[DIGEST_LEN];
  671. if (strlen(fp) == HEX_DIGEST_LEN &&
  672. base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
  673. return router_digest_is_me(digest);
  674. return 0;
  675. }
  676. /** Return a routerinfo for this OR, rebuilding a fresh one if
  677. * necessary. Return NULL on error, or if called on an OP. */
  678. routerinfo_t *
  679. router_get_my_routerinfo(void)
  680. {
  681. if (!server_mode(get_options()))
  682. return NULL;
  683. if (!desc_routerinfo) {
  684. if (router_rebuild_descriptor(1))
  685. return NULL;
  686. }
  687. return desc_routerinfo;
  688. }
  689. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  690. * one if necessary. Return NULL on error.
  691. */
  692. const char *
  693. router_get_my_descriptor(void)
  694. {
  695. const char *body;
  696. if (!desc_routerinfo) {
  697. if (router_rebuild_descriptor(1))
  698. return NULL;
  699. }
  700. body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
  701. debug(LD_GENERAL,"my desc is '%s'", body);
  702. return body;
  703. }
  704. /*DOCDOC*/
  705. static smartlist_t *warned_nonexistent_family = NULL;
  706. /** If <b>force</b> is true, or our descriptor is out-of-date, rebuild
  707. * a fresh routerinfo and signed server descriptor for this OR.
  708. * Return 0 on success, -1 on error.
  709. */
  710. int
  711. router_rebuild_descriptor(int force)
  712. {
  713. routerinfo_t *ri;
  714. uint32_t addr;
  715. char platform[256];
  716. int hibernating = we_are_hibernating();
  717. or_options_t *options = get_options();
  718. if (desc_clean_since && !force)
  719. return 0;
  720. if (resolve_my_address(options, &addr, NULL) < 0) {
  721. warn(LD_CONFIG,"options->Address didn't resolve into an IP.");
  722. return -1;
  723. }
  724. ri = tor_malloc_zero(sizeof(routerinfo_t));
  725. ri->address = tor_dup_addr(addr);
  726. ri->nickname = tor_strdup(options->Nickname);
  727. ri->addr = addr;
  728. ri->or_port = options->ORPort;
  729. ri->dir_port = options->DirPort;
  730. ri->cache_info.published_on = time(NULL);
  731. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from
  732. * main thread */
  733. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  734. if (crypto_pk_get_digest(ri->identity_pkey,
  735. ri->cache_info.identity_digest)<0) {
  736. routerinfo_free(ri);
  737. return -1;
  738. }
  739. get_platform_str(platform, sizeof(platform));
  740. ri->platform = tor_strdup(platform);
  741. ri->bandwidthrate = (int)options->BandwidthRate;
  742. ri->bandwidthburst = (int)options->BandwidthBurst;
  743. ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
  744. if (options->BandwidthRate > options->MaxAdvertisedBandwidth)
  745. ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
  746. config_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
  747. options->ExitPolicyRejectPrivate);
  748. if (desc_routerinfo) { /* inherit values */
  749. ri->is_verified = desc_routerinfo->is_verified;
  750. ri->is_running = desc_routerinfo->is_running;
  751. ri->is_named = desc_routerinfo->is_named;
  752. }
  753. if (authdir_mode(options))
  754. ri->is_verified = ri->is_named = 1; /* believe in yourself */
  755. if (options->MyFamily) {
  756. smartlist_t *family;
  757. if (!warned_nonexistent_family)
  758. warned_nonexistent_family = smartlist_create();
  759. family = smartlist_create();
  760. ri->declared_family = smartlist_create();
  761. smartlist_split_string(family, options->MyFamily, ",",
  762. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  763. SMARTLIST_FOREACH(family, char *, name,
  764. {
  765. routerinfo_t *member;
  766. if (!strcasecmp(name, options->Nickname))
  767. member = ri;
  768. else
  769. member = router_get_by_nickname(name, 1);
  770. if (!member) {
  771. if (!smartlist_string_isin(warned_nonexistent_family, name)) {
  772. warn(LD_CONFIG, "I have no descriptor for the router named \"%s\" "
  773. "in my declared family; I'll use the nickname as is, but "
  774. "this may confuse clients.", name);
  775. smartlist_add(warned_nonexistent_family, tor_strdup(name));
  776. }
  777. smartlist_add(ri->declared_family, name);
  778. name = NULL;
  779. } else {
  780. char *fp = tor_malloc(HEX_DIGEST_LEN+2);
  781. fp[0] = '$';
  782. base16_encode(fp+1,HEX_DIGEST_LEN+1,
  783. member->cache_info.identity_digest, DIGEST_LEN);
  784. smartlist_add(ri->declared_family, fp);
  785. if (smartlist_string_isin(warned_nonexistent_family, name))
  786. smartlist_string_remove(warned_nonexistent_family, name);
  787. }
  788. tor_free(name);
  789. });
  790. smartlist_free(family);
  791. }
  792. ri->cache_info.signed_descriptor_body = tor_malloc(8192);
  793. if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
  794. ri, get_identity_key())<0) {
  795. warn(LD_BUG, "Couldn't allocate string for descriptor.");
  796. return -1;
  797. }
  798. ri->cache_info.signed_descriptor_len =
  799. strlen(ri->cache_info.signed_descriptor_body);
  800. crypto_digest(ri->cache_info.signed_descriptor_digest,
  801. ri->cache_info.signed_descriptor_body,
  802. ri->cache_info.signed_descriptor_len);
  803. if (desc_routerinfo)
  804. routerinfo_free(desc_routerinfo);
  805. desc_routerinfo = ri;
  806. desc_clean_since = time(NULL);
  807. desc_needs_upload = 1;
  808. return 0;
  809. }
  810. /** Mark descriptor out of date if it's older than <b>when</b> */
  811. void
  812. mark_my_descriptor_dirty_if_older_than(time_t when)
  813. {
  814. if (desc_clean_since < when)
  815. mark_my_descriptor_dirty();
  816. }
  817. /** Call when the current descriptor is out of date. */
  818. void
  819. mark_my_descriptor_dirty(void)
  820. {
  821. desc_clean_since = 0;
  822. }
  823. #define MAX_BANDWIDTH_CHANGE_FREQ 20*60
  824. /** Check whether bandwidth has changed a lot since the last time we announced
  825. * bandwidth. If so, mark our descriptor dirty. */
  826. void
  827. check_descriptor_bandwidth_changed(time_t now)
  828. {
  829. static time_t last_changed = 0;
  830. uint64_t prev, cur;
  831. if (!desc_routerinfo)
  832. return;
  833. prev = desc_routerinfo->bandwidthcapacity;
  834. cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
  835. if ((prev != cur && (!prev || !cur)) ||
  836. cur > prev*2 ||
  837. cur < prev/2) {
  838. if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
  839. info(LD_GENERAL,
  840. "Measured bandwidth has changed; rebuilding descriptor.");
  841. mark_my_descriptor_dirty();
  842. last_changed = now;
  843. }
  844. }
  845. }
  846. /** Check whether our own address as defined by the Address configuration
  847. * has changed. This is for routers that get their address from a service
  848. * like dyndns. If our address has changed, mark our descriptor dirty. */
  849. void
  850. check_descriptor_ipaddress_changed(time_t now)
  851. {
  852. uint32_t prev, cur;
  853. or_options_t *options = get_options();
  854. if (!desc_routerinfo)
  855. return;
  856. prev = desc_routerinfo->addr;
  857. if (resolve_my_address(options, &cur, NULL) < 0) {
  858. warn(LD_CONFIG,"options->Address didn't resolve into an IP.");
  859. return;
  860. }
  861. if (prev != cur) {
  862. char addrbuf_prev[INET_NTOA_BUF_LEN];
  863. char addrbuf_cur[INET_NTOA_BUF_LEN];
  864. struct in_addr in_prev;
  865. struct in_addr in_cur;
  866. in_prev.s_addr = htonl(prev);
  867. tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
  868. in_cur.s_addr = htonl(cur);
  869. tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
  870. info(LD_GENERAL,
  871. "Our IP Address has changed from %s to %s; rebuilding descriptor.",
  872. addrbuf_prev, addrbuf_cur);
  873. mark_my_descriptor_dirty();
  874. }
  875. }
  876. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  877. * string describing the version of Tor and the operating system we're
  878. * currently running on.
  879. */
  880. void
  881. get_platform_str(char *platform, size_t len)
  882. {
  883. tor_snprintf(platform, len, "Tor %s on %s",
  884. VERSION, get_uname());
  885. return;
  886. }
  887. /* XXX need to audit this thing and count fenceposts. maybe
  888. * refactor so we don't have to keep asking if we're
  889. * near the end of maxlen?
  890. */
  891. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  892. /** OR only: Given a routerinfo for this router, and an identity key to sign
  893. * with, encode the routerinfo as a signed server descriptor and write the
  894. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  895. * failure, and the number of bytes used on success.
  896. */
  897. int
  898. router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  899. crypto_pk_env_t *ident_key)
  900. {
  901. char *onion_pkey; /* Onion key, PEM-encoded. */
  902. char *identity_pkey; /* Identity key, PEM-encoded. */
  903. char digest[DIGEST_LEN];
  904. char published[ISO_TIME_LEN+1];
  905. char fingerprint[FINGERPRINT_LEN+1];
  906. struct in_addr in;
  907. char addrbuf[INET_NTOA_BUF_LEN];
  908. size_t onion_pkeylen, identity_pkeylen;
  909. size_t written;
  910. int result=0;
  911. addr_policy_t *tmpe;
  912. char *bandwidth_usage;
  913. char *family_line;
  914. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  915. char *s_tmp, *s_dup;
  916. const char *cp;
  917. routerinfo_t *ri_tmp;
  918. #endif
  919. or_options_t *options = get_options();
  920. /* Make sure the identity key matches the one in the routerinfo. */
  921. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  922. warn(LD_BUG,"Tried to sign a router with a private key that didn't "
  923. "match router's public key!");
  924. return -1;
  925. }
  926. /* record our fingerprint, so we can include it in the descriptor */
  927. if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
  928. err(LD_BUG,"Error computing fingerprint");
  929. return -1;
  930. }
  931. /* PEM-encode the onion key */
  932. if (crypto_pk_write_public_key_to_string(router->onion_pkey,
  933. &onion_pkey,&onion_pkeylen)<0) {
  934. warn(LD_BUG,"write onion_pkey to string failed!");
  935. return -1;
  936. }
  937. /* PEM-encode the identity key key */
  938. if (crypto_pk_write_public_key_to_string(router->identity_pkey,
  939. &identity_pkey,&identity_pkeylen)<0) {
  940. warn(LD_BUG,"write identity_pkey to string failed!");
  941. tor_free(onion_pkey);
  942. return -1;
  943. }
  944. /* Encode the publication time. */
  945. format_iso_time(published, router->cache_info.published_on);
  946. /* How busy have we been? */
  947. bandwidth_usage = rep_hist_get_bandwidth_lines();
  948. if (router->declared_family && smartlist_len(router->declared_family)) {
  949. size_t n;
  950. char *s = smartlist_join_strings(router->declared_family, " ", 0, &n);
  951. n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */
  952. family_line = tor_malloc(n);
  953. tor_snprintf(family_line, n, "family %s\n", s);
  954. tor_free(s);
  955. } else {
  956. family_line = tor_strdup("");
  957. }
  958. /* Generate the easy portion of the router descriptor. */
  959. result = tor_snprintf(s, maxlen,
  960. "router %s %s %d 0 %d\n"
  961. "platform %s\n"
  962. "published %s\n"
  963. "opt fingerprint %s\n"
  964. "uptime %ld\n"
  965. "bandwidth %d %d %d\n"
  966. "onion-key\n%s"
  967. "signing-key\n%s%s%s%s",
  968. router->nickname,
  969. router->address,
  970. router->or_port,
  971. decide_to_advertise_dirport(options, router),
  972. router->platform,
  973. published,
  974. fingerprint,
  975. stats_n_seconds_working,
  976. (int) router->bandwidthrate,
  977. (int) router->bandwidthburst,
  978. (int) router->bandwidthcapacity,
  979. onion_pkey, identity_pkey,
  980. family_line, bandwidth_usage,
  981. we_are_hibernating() ? "opt hibernating 1\n" : "");
  982. tor_free(family_line);
  983. tor_free(onion_pkey);
  984. tor_free(identity_pkey);
  985. tor_free(bandwidth_usage);
  986. if (result < 0)
  987. return -1;
  988. /* From now on, we use 'written' to remember the current length of 's'. */
  989. written = result;
  990. if (options->ContactInfo && strlen(options->ContactInfo)) {
  991. result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
  992. options->ContactInfo);
  993. if (result<0)
  994. return -1;
  995. written += result;
  996. }
  997. /* Write the exit policy to the end of 's'. */
  998. for (tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  999. /* Write: "accept 1.2.3.4" */
  1000. in.s_addr = htonl(tmpe->addr);
  1001. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1002. result = tor_snprintf(s+written, maxlen-written, "%s %s",
  1003. tmpe->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
  1004. tmpe->msk == 0 ? "*" : addrbuf);
  1005. if (result < 0)
  1006. return -1;
  1007. written += result;
  1008. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  1009. /* Write "/255.255.0.0" */
  1010. in.s_addr = htonl(tmpe->msk);
  1011. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1012. result = tor_snprintf(s+written, maxlen-written, "/%s", addrbuf);
  1013. if (result<0)
  1014. return -1;
  1015. written += result;
  1016. }
  1017. if (tmpe->prt_min <= 1 && tmpe->prt_max == 65535) {
  1018. /* There is no port set; write ":*" */
  1019. if (written+4 > maxlen)
  1020. return -1;
  1021. strlcat(s+written, ":*\n", maxlen-written);
  1022. written += 3;
  1023. } else if (tmpe->prt_min == tmpe->prt_max) {
  1024. /* There is only one port; write ":80". */
  1025. result = tor_snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  1026. if (result<0)
  1027. return -1;
  1028. written += result;
  1029. } else {
  1030. /* There is a range of ports; write ":79-80". */
  1031. result = tor_snprintf(s+written, maxlen-written, ":%d-%d\n",
  1032. tmpe->prt_min, tmpe->prt_max);
  1033. if (result<0)
  1034. return -1;
  1035. written += result;
  1036. }
  1037. if (tmpe->msk == 0 && tmpe->prt_min <= 1 && tmpe->prt_max == 65535)
  1038. /* This was a catch-all rule, so future rules are irrelevant. */
  1039. break;
  1040. } /* end for */
  1041. if (written+256 > maxlen) /* Not enough room for signature. */
  1042. return -1;
  1043. /* Sign the directory */
  1044. strlcat(s+written, "router-signature\n", maxlen-written);
  1045. written += strlen(s+written);
  1046. s[written] = '\0';
  1047. if (router_get_router_hash(s, digest) < 0)
  1048. return -1;
  1049. if (router_append_dirobj_signature(s+written,maxlen-written,
  1050. digest,ident_key)<0) {
  1051. warn(LD_BUG, "Couldn't sign router descriptor");
  1052. return -1;
  1053. }
  1054. written += strlen(s+written);
  1055. if (written+2 > maxlen)
  1056. return -1;
  1057. /* include a last '\n' */
  1058. s[written] = '\n';
  1059. s[written+1] = 0;
  1060. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1061. cp = s_tmp = s_dup = tor_strdup(s);
  1062. ri_tmp = router_parse_entry_from_string(cp, NULL);
  1063. if (!ri_tmp) {
  1064. err(LD_BUG,"We just generated a router descriptor we can't parse: <<%s>>",
  1065. s);
  1066. return -1;
  1067. }
  1068. tor_free(s_dup);
  1069. routerinfo_free(ri_tmp);
  1070. #endif
  1071. return written+1;
  1072. }
  1073. /** Return true iff <b>s</b> is a legally valid server nickname. */
  1074. int
  1075. is_legal_nickname(const char *s)
  1076. {
  1077. size_t len;
  1078. tor_assert(s);
  1079. len = strlen(s);
  1080. return len > 0 && len <= MAX_NICKNAME_LEN &&
  1081. strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
  1082. }
  1083. /** Return true iff <b>s</b> is a legally valid server nickname or
  1084. * hex-encoded identity-key digest. */
  1085. int
  1086. is_legal_nickname_or_hexdigest(const char *s)
  1087. {
  1088. size_t len;
  1089. tor_assert(s);
  1090. if (*s!='$')
  1091. return is_legal_nickname(s);
  1092. len = strlen(s);
  1093. return len == HEX_DIGEST_LEN+1 && strspn(s+1,HEX_CHARACTERS)==len-1;
  1094. }
  1095. /** Forget that we have issued any router-related warnings, so that we'll
  1096. * warn again if we see the same errors. */
  1097. void
  1098. router_reset_warnings(void)
  1099. {
  1100. if (warned_nonexistent_family) {
  1101. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1102. smartlist_clear(warned_nonexistent_family);
  1103. }
  1104. }
  1105. /** Release all static resources held in router.c */
  1106. void
  1107. router_free_all(void)
  1108. {
  1109. if (onionkey)
  1110. crypto_free_pk_env(onionkey);
  1111. if (lastonionkey)
  1112. crypto_free_pk_env(lastonionkey);
  1113. if (identitykey)
  1114. crypto_free_pk_env(identitykey);
  1115. if (key_lock)
  1116. tor_mutex_free(key_lock);
  1117. if (desc_routerinfo)
  1118. routerinfo_free(desc_routerinfo);
  1119. if (warned_nonexistent_family) {
  1120. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1121. smartlist_free(warned_nonexistent_family);
  1122. }
  1123. }