circuitlist.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file circuitlist.c
  8. * \brief Manage the global circuit list.
  9. **/
  10. #include "or.h"
  11. #include "circuitbuild.h"
  12. #include "circuitlist.h"
  13. #include "circuituse.h"
  14. #include "connection.h"
  15. #include "config.h"
  16. #include "connection_edge.h"
  17. #include "connection_or.h"
  18. #include "control.h"
  19. #include "networkstatus.h"
  20. #include "nodelist.h"
  21. #include "onion.h"
  22. #include "relay.h"
  23. #include "rendclient.h"
  24. #include "rendcommon.h"
  25. #include "rephist.h"
  26. #include "routerlist.h"
  27. #include "ht.h"
  28. /********* START VARIABLES **********/
  29. /** A global list of all circuits at this hop. */
  30. circuit_t *global_circuitlist=NULL;
  31. /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
  32. static smartlist_t *circuits_pending_or_conns=NULL;
  33. static void circuit_free(circuit_t *circ);
  34. static void circuit_free_cpath(crypt_path_t *cpath);
  35. static void circuit_free_cpath_node(crypt_path_t *victim);
  36. /********* END VARIABLES ************/
  37. /** A map from OR connection and circuit ID to circuit. (Lookup performance is
  38. * very important here, since we need to do it every time a cell arrives.) */
  39. typedef struct orconn_circid_circuit_map_t {
  40. HT_ENTRY(orconn_circid_circuit_map_t) node;
  41. or_connection_t *or_conn;
  42. circid_t circ_id;
  43. circuit_t *circuit;
  44. } orconn_circid_circuit_map_t;
  45. /** Helper for hash tables: compare the OR connection and circuit ID for a and
  46. * b, and return less than, equal to, or greater than zero appropriately.
  47. */
  48. static INLINE int
  49. _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
  50. orconn_circid_circuit_map_t *b)
  51. {
  52. return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
  53. }
  54. /** Helper: return a hash based on circuit ID and the pointer value of
  55. * or_conn in <b>a</b>. */
  56. static INLINE unsigned int
  57. _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
  58. {
  59. return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->or_conn);
  60. }
  61. /** Map from [orconn,circid] to circuit. */
  62. static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
  63. orconn_circid_circuit_map = HT_INITIALIZER();
  64. HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  65. _orconn_circid_entry_hash, _orconn_circid_entries_eq)
  66. HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  67. _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
  68. malloc, realloc, free)
  69. /** The most recently returned entry from circuit_get_by_circid_orconn;
  70. * used to improve performance when many cells arrive in a row from the
  71. * same circuit.
  72. */
  73. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  74. /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
  75. * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
  76. * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
  77. * the old entry (if any) and adding a new one. */
  78. static void
  79. circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
  80. circid_t id,
  81. or_connection_t *conn)
  82. {
  83. orconn_circid_circuit_map_t search;
  84. orconn_circid_circuit_map_t *found;
  85. or_connection_t *old_conn, **conn_ptr;
  86. circid_t old_id, *circid_ptr;
  87. int was_active, make_active;
  88. if (direction == CELL_DIRECTION_OUT) {
  89. conn_ptr = &circ->n_conn;
  90. circid_ptr = &circ->n_circ_id;
  91. was_active = circ->next_active_on_n_conn != NULL;
  92. make_active = circ->n_conn_cells.n > 0;
  93. } else {
  94. or_circuit_t *c = TO_OR_CIRCUIT(circ);
  95. conn_ptr = &c->p_conn;
  96. circid_ptr = &c->p_circ_id;
  97. was_active = c->next_active_on_p_conn != NULL;
  98. make_active = c->p_conn_cells.n > 0;
  99. }
  100. old_conn = *conn_ptr;
  101. old_id = *circid_ptr;
  102. if (id == old_id && conn == old_conn)
  103. return;
  104. if (_last_circid_orconn_ent &&
  105. ((old_id == _last_circid_orconn_ent->circ_id &&
  106. old_conn == _last_circid_orconn_ent->or_conn) ||
  107. (id == _last_circid_orconn_ent->circ_id &&
  108. conn == _last_circid_orconn_ent->or_conn))) {
  109. _last_circid_orconn_ent = NULL;
  110. }
  111. if (old_conn) { /* we may need to remove it from the conn-circid map */
  112. tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
  113. search.circ_id = old_id;
  114. search.or_conn = old_conn;
  115. found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
  116. if (found) {
  117. tor_free(found);
  118. --old_conn->n_circuits;
  119. }
  120. if (was_active && old_conn != conn)
  121. make_circuit_inactive_on_conn(circ,old_conn);
  122. }
  123. /* Change the values only after we have possibly made the circuit inactive
  124. * on the previous conn. */
  125. *conn_ptr = conn;
  126. *circid_ptr = id;
  127. if (conn == NULL)
  128. return;
  129. /* now add the new one to the conn-circid map */
  130. search.circ_id = id;
  131. search.or_conn = conn;
  132. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  133. if (found) {
  134. found->circuit = circ;
  135. } else {
  136. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  137. found->circ_id = id;
  138. found->or_conn = conn;
  139. found->circuit = circ;
  140. HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
  141. }
  142. if (make_active && old_conn != conn)
  143. make_circuit_active_on_conn(circ,conn);
  144. ++conn->n_circuits;
  145. }
  146. /** Set the p_conn field of a circuit <b>circ</b>, along
  147. * with the corresponding circuit ID, and add the circuit as appropriate
  148. * to the (orconn,id)-\>circuit map. */
  149. void
  150. circuit_set_p_circid_orconn(or_circuit_t *circ, circid_t id,
  151. or_connection_t *conn)
  152. {
  153. circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
  154. id, conn);
  155. if (conn)
  156. tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
  157. }
  158. /** Set the n_conn field of a circuit <b>circ</b>, along
  159. * with the corresponding circuit ID, and add the circuit as appropriate
  160. * to the (orconn,id)-\>circuit map. */
  161. void
  162. circuit_set_n_circid_orconn(circuit_t *circ, circid_t id,
  163. or_connection_t *conn)
  164. {
  165. circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
  166. if (conn)
  167. tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
  168. }
  169. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  170. * it from lists as appropriate. */
  171. void
  172. circuit_set_state(circuit_t *circ, uint8_t state)
  173. {
  174. tor_assert(circ);
  175. if (state == circ->state)
  176. return;
  177. if (!circuits_pending_or_conns)
  178. circuits_pending_or_conns = smartlist_create();
  179. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  180. /* remove from waiting-circuit list. */
  181. smartlist_remove(circuits_pending_or_conns, circ);
  182. }
  183. if (state == CIRCUIT_STATE_OR_WAIT) {
  184. /* add to waiting-circuit list. */
  185. smartlist_add(circuits_pending_or_conns, circ);
  186. }
  187. if (state == CIRCUIT_STATE_OPEN)
  188. tor_assert(!circ->n_conn_onionskin);
  189. circ->state = state;
  190. }
  191. /** Add <b>circ</b> to the global list of circuits. This is called only from
  192. * within circuit_new.
  193. */
  194. static void
  195. circuit_add(circuit_t *circ)
  196. {
  197. if (!global_circuitlist) { /* first one */
  198. global_circuitlist = circ;
  199. circ->next = NULL;
  200. } else {
  201. circ->next = global_circuitlist;
  202. global_circuitlist = circ;
  203. }
  204. }
  205. /** Append to <b>out</b> all circuits in state OR_WAIT waiting for
  206. * the given connection. */
  207. void
  208. circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
  209. {
  210. tor_assert(out);
  211. tor_assert(or_conn);
  212. if (!circuits_pending_or_conns)
  213. return;
  214. SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns, circuit_t *, circ) {
  215. if (circ->marked_for_close)
  216. continue;
  217. if (!circ->n_hop)
  218. continue;
  219. tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  220. if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
  221. /* Look at addr/port. This is an unkeyed connection. */
  222. if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
  223. circ->n_hop->port != or_conn->_base.port)
  224. continue;
  225. } else {
  226. /* We expected a key. See if it's the right one. */
  227. if (tor_memneq(or_conn->identity_digest,
  228. circ->n_hop->identity_digest, DIGEST_LEN))
  229. continue;
  230. }
  231. smartlist_add(out, circ);
  232. } SMARTLIST_FOREACH_END(circ);
  233. }
  234. /** Return the number of circuits in state OR_WAIT, waiting for the given
  235. * connection. */
  236. int
  237. circuit_count_pending_on_or_conn(or_connection_t *or_conn)
  238. {
  239. int cnt;
  240. smartlist_t *sl = smartlist_create();
  241. circuit_get_all_pending_on_or_conn(sl, or_conn);
  242. cnt = smartlist_len(sl);
  243. smartlist_free(sl);
  244. log_debug(LD_CIRC,"or_conn to %s at %s, %d pending circs",
  245. or_conn->nickname ? or_conn->nickname : "NULL",
  246. or_conn->_base.address,
  247. cnt);
  248. return cnt;
  249. }
  250. /** Detach from the global circuit list, and deallocate, all
  251. * circuits that have been marked for close.
  252. */
  253. void
  254. circuit_close_all_marked(void)
  255. {
  256. circuit_t *tmp,*m;
  257. while (global_circuitlist && global_circuitlist->marked_for_close) {
  258. tmp = global_circuitlist->next;
  259. circuit_free(global_circuitlist);
  260. global_circuitlist = tmp;
  261. }
  262. tmp = global_circuitlist;
  263. while (tmp && tmp->next) {
  264. if (tmp->next->marked_for_close) {
  265. m = tmp->next->next;
  266. circuit_free(tmp->next);
  267. tmp->next = m;
  268. /* Need to check new tmp->next; don't advance tmp. */
  269. } else {
  270. /* Advance tmp. */
  271. tmp = tmp->next;
  272. }
  273. }
  274. }
  275. /** Return the head of the global linked list of circuits. */
  276. circuit_t *
  277. _circuit_get_global_list(void)
  278. {
  279. return global_circuitlist;
  280. }
  281. /** Function to make circ-\>state human-readable */
  282. const char *
  283. circuit_state_to_string(int state)
  284. {
  285. static char buf[64];
  286. switch (state) {
  287. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  288. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  289. case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
  290. case CIRCUIT_STATE_OPEN: return "open";
  291. default:
  292. log_warn(LD_BUG, "Unknown circuit state %d", state);
  293. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  294. return buf;
  295. }
  296. }
  297. /** Map a circuit purpose to a string suitable to be displayed to a
  298. * controller. */
  299. const char *
  300. circuit_purpose_to_controller_string(uint8_t purpose)
  301. {
  302. static char buf[32];
  303. switch (purpose) {
  304. case CIRCUIT_PURPOSE_OR:
  305. case CIRCUIT_PURPOSE_INTRO_POINT:
  306. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  307. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  308. return "SERVER"; /* A controller should never see these, actually. */
  309. case CIRCUIT_PURPOSE_C_GENERAL:
  310. return "GENERAL";
  311. case CIRCUIT_PURPOSE_C_INTRODUCING:
  312. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  313. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  314. return "HS_CLIENT_INTRO";
  315. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  316. case CIRCUIT_PURPOSE_C_REND_READY:
  317. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  318. case CIRCUIT_PURPOSE_C_REND_JOINED:
  319. return "HS_CLIENT_REND";
  320. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  321. case CIRCUIT_PURPOSE_S_INTRO:
  322. return "HS_SERVICE_INTRO";
  323. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  324. case CIRCUIT_PURPOSE_S_REND_JOINED:
  325. return "HS_SERVICE_REND";
  326. case CIRCUIT_PURPOSE_TESTING:
  327. return "TESTING";
  328. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  329. return "MEASURE_TIMEOUT";
  330. case CIRCUIT_PURPOSE_CONTROLLER:
  331. return "CONTROLLER";
  332. default:
  333. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  334. return buf;
  335. }
  336. }
  337. /** Return a string specifying the state of the hidden-service circuit
  338. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  339. * hidden-service-related circuit purpose. */
  340. const char *
  341. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  342. {
  343. switch (purpose)
  344. {
  345. default:
  346. log_fn(LOG_WARN, LD_BUG,
  347. "Unrecognized circuit purpose: %d",
  348. (int)purpose);
  349. tor_fragile_assert();
  350. /* fall through */
  351. case CIRCUIT_PURPOSE_OR:
  352. case CIRCUIT_PURPOSE_C_GENERAL:
  353. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  354. case CIRCUIT_PURPOSE_TESTING:
  355. case CIRCUIT_PURPOSE_CONTROLLER:
  356. return NULL;
  357. case CIRCUIT_PURPOSE_INTRO_POINT:
  358. return "OR_HSSI_ESTABLISHED";
  359. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  360. return "OR_HSCR_ESTABLISHED";
  361. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  362. return "OR_HS_R_JOINED";
  363. case CIRCUIT_PURPOSE_C_INTRODUCING:
  364. return "HSCI_CONNECTING";
  365. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  366. return "HSCI_INTRO_SENT";
  367. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  368. return "HSCI_DONE";
  369. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  370. return "HSCR_CONNECTING";
  371. case CIRCUIT_PURPOSE_C_REND_READY:
  372. return "HSCR_ESTABLISHED_IDLE";
  373. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  374. return "HSCR_ESTABLISHED_WAITING";
  375. case CIRCUIT_PURPOSE_C_REND_JOINED:
  376. return "HSCR_JOINED";
  377. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  378. return "HSSI_CONNECTING";
  379. case CIRCUIT_PURPOSE_S_INTRO:
  380. return "HSSI_ESTABLISHED";
  381. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  382. return "HSSR_CONNECTING";
  383. case CIRCUIT_PURPOSE_S_REND_JOINED:
  384. return "HSSR_JOINED";
  385. }
  386. }
  387. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  388. const char *
  389. circuit_purpose_to_string(uint8_t purpose)
  390. {
  391. static char buf[32];
  392. switch (purpose)
  393. {
  394. case CIRCUIT_PURPOSE_OR:
  395. return "Circuit at relay";
  396. case CIRCUIT_PURPOSE_INTRO_POINT:
  397. return "Acting as intro point";
  398. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  399. return "Acting as rendevous (pending)";
  400. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  401. return "Acting as rendevous (established)";
  402. case CIRCUIT_PURPOSE_C_GENERAL:
  403. return "General-purpose client";
  404. case CIRCUIT_PURPOSE_C_INTRODUCING:
  405. return "Hidden service client: Connecting to intro point";
  406. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  407. return "Hidden service client: Waiting for ack from intro point";
  408. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  409. return "Hidden service client: Received ack from intro point";
  410. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  411. return "Hidden service client: Establishing rendezvous point";
  412. case CIRCUIT_PURPOSE_C_REND_READY:
  413. return "Hidden service client: Pending rendezvous point";
  414. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  415. return "Hidden service client: Pending rendezvous point (ack received)";
  416. case CIRCUIT_PURPOSE_C_REND_JOINED:
  417. return "Hidden service client: Active rendezvous point";
  418. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  419. return "Measuring circuit timeout";
  420. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  421. return "Hidden service: Establishing introduction point";
  422. case CIRCUIT_PURPOSE_S_INTRO:
  423. return "Hidden service: Introduction point";
  424. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  425. return "Hidden service: Connecting to rendezvous point";
  426. case CIRCUIT_PURPOSE_S_REND_JOINED:
  427. return "Hidden service: Active rendezvous point";
  428. case CIRCUIT_PURPOSE_TESTING:
  429. return "Testing circuit";
  430. case CIRCUIT_PURPOSE_CONTROLLER:
  431. return "Circuit made by controller";
  432. default:
  433. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  434. return buf;
  435. }
  436. }
  437. /** Pick a reasonable package_window to start out for our circuits.
  438. * Originally this was hard-coded at 1000, but now the consensus votes
  439. * on the answer. See proposal 168. */
  440. int32_t
  441. circuit_initial_package_window(void)
  442. {
  443. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  444. CIRCWINDOW_START_MIN,
  445. CIRCWINDOW_START_MAX);
  446. /* If the consensus tells us a negative number, we'd assert. */
  447. if (num < 0)
  448. num = CIRCWINDOW_START;
  449. return num;
  450. }
  451. /** Initialize the common elements in a circuit_t, and add it to the global
  452. * list. */
  453. static void
  454. init_circuit_base(circuit_t *circ)
  455. {
  456. tor_gettimeofday(&circ->timestamp_created);
  457. circ->package_window = circuit_initial_package_window();
  458. circ->deliver_window = CIRCWINDOW_START;
  459. /* Initialize the cell_ewma_t structure */
  460. circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  461. circ->n_cell_ewma.cell_count = 0.0;
  462. circ->n_cell_ewma.heap_index = -1;
  463. circ->n_cell_ewma.is_for_p_conn = 0;
  464. circuit_add(circ);
  465. }
  466. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  467. * and <b>p_conn</b>. Add it to the global circuit list.
  468. */
  469. origin_circuit_t *
  470. origin_circuit_new(void)
  471. {
  472. origin_circuit_t *circ;
  473. /* never zero, since a global ID of 0 is treated specially by the
  474. * controller */
  475. static uint32_t n_circuits_allocated = 1;
  476. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  477. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  478. circ->next_stream_id = crypto_rand_int(1<<16);
  479. circ->global_identifier = n_circuits_allocated++;
  480. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  481. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  482. init_circuit_base(TO_CIRCUIT(circ));
  483. circ_times.last_circ_at = approx_time();
  484. return circ;
  485. }
  486. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  487. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  488. or_circuit_t *
  489. or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
  490. {
  491. /* CircIDs */
  492. or_circuit_t *circ;
  493. circ = tor_malloc_zero(sizeof(or_circuit_t));
  494. circ->_base.magic = OR_CIRCUIT_MAGIC;
  495. if (p_conn)
  496. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  497. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  498. init_circuit_base(TO_CIRCUIT(circ));
  499. /* Initialize the cell_ewma_t structure */
  500. /* Initialize the cell counts to 0 */
  501. circ->p_cell_ewma.cell_count = 0.0;
  502. circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  503. circ->p_cell_ewma.is_for_p_conn = 1;
  504. /* It's not in any heap yet. */
  505. circ->p_cell_ewma.heap_index = -1;
  506. return circ;
  507. }
  508. /** Deallocate space associated with circ.
  509. */
  510. static void
  511. circuit_free(circuit_t *circ)
  512. {
  513. void *mem;
  514. size_t memlen;
  515. if (!circ)
  516. return;
  517. if (CIRCUIT_IS_ORIGIN(circ)) {
  518. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  519. mem = ocirc;
  520. memlen = sizeof(origin_circuit_t);
  521. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  522. if (ocirc->build_state) {
  523. extend_info_free(ocirc->build_state->chosen_exit);
  524. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  525. }
  526. tor_free(ocirc->build_state);
  527. circuit_free_cpath(ocirc->cpath);
  528. crypto_free_pk_env(ocirc->intro_key);
  529. rend_data_free(ocirc->rend_data);
  530. tor_free(ocirc->dest_address);
  531. if (ocirc->socks_username) {
  532. memset(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  533. tor_free(ocirc->socks_username);
  534. }
  535. if (ocirc->socks_password) {
  536. memset(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  537. tor_free(ocirc->socks_password);
  538. }
  539. } else {
  540. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  541. /* Remember cell statistics for this circuit before deallocating. */
  542. if (get_options()->CellStatistics)
  543. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  544. mem = ocirc;
  545. memlen = sizeof(or_circuit_t);
  546. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  547. crypto_free_cipher_env(ocirc->p_crypto);
  548. crypto_free_digest_env(ocirc->p_digest);
  549. crypto_free_cipher_env(ocirc->n_crypto);
  550. crypto_free_digest_env(ocirc->n_digest);
  551. if (ocirc->rend_splice) {
  552. or_circuit_t *other = ocirc->rend_splice;
  553. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  554. other->rend_splice = NULL;
  555. }
  556. /* remove from map. */
  557. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  558. /* Clear cell queue _after_ removing it from the map. Otherwise our
  559. * "active" checks will be violated. */
  560. cell_queue_clear(&ocirc->p_conn_cells);
  561. }
  562. extend_info_free(circ->n_hop);
  563. tor_free(circ->n_conn_onionskin);
  564. /* Remove from map. */
  565. circuit_set_n_circid_orconn(circ, 0, NULL);
  566. /* Clear cell queue _after_ removing it from the map. Otherwise our
  567. * "active" checks will be violated. */
  568. cell_queue_clear(&circ->n_conn_cells);
  569. memset(mem, 0xAA, memlen); /* poison memory */
  570. tor_free(mem);
  571. }
  572. /** Deallocate space associated with the linked list <b>cpath</b>. */
  573. static void
  574. circuit_free_cpath(crypt_path_t *cpath)
  575. {
  576. crypt_path_t *victim, *head=cpath;
  577. if (!cpath)
  578. return;
  579. /* it's a doubly linked list, so we have to notice when we've
  580. * gone through it once. */
  581. while (cpath->next && cpath->next != head) {
  582. victim = cpath;
  583. cpath = victim->next;
  584. circuit_free_cpath_node(victim);
  585. }
  586. circuit_free_cpath_node(cpath);
  587. }
  588. /** Release all storage held by circuits. */
  589. void
  590. circuit_free_all(void)
  591. {
  592. circuit_t *next;
  593. while (global_circuitlist) {
  594. next = global_circuitlist->next;
  595. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  596. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  597. while (or_circ->resolving_streams) {
  598. edge_connection_t *next_conn;
  599. next_conn = or_circ->resolving_streams->next_stream;
  600. connection_free(TO_CONN(or_circ->resolving_streams));
  601. or_circ->resolving_streams = next_conn;
  602. }
  603. }
  604. circuit_free(global_circuitlist);
  605. global_circuitlist = next;
  606. }
  607. smartlist_free(circuits_pending_or_conns);
  608. circuits_pending_or_conns = NULL;
  609. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  610. }
  611. /** Deallocate space associated with the cpath node <b>victim</b>. */
  612. static void
  613. circuit_free_cpath_node(crypt_path_t *victim)
  614. {
  615. if (!victim)
  616. return;
  617. crypto_free_cipher_env(victim->f_crypto);
  618. crypto_free_cipher_env(victim->b_crypto);
  619. crypto_free_digest_env(victim->f_digest);
  620. crypto_free_digest_env(victim->b_digest);
  621. crypto_dh_free(victim->dh_handshake_state);
  622. extend_info_free(victim->extend_info);
  623. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  624. tor_free(victim);
  625. }
  626. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  627. * of information about circuit <b>circ</b>.
  628. */
  629. static void
  630. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  631. const char *type, int this_circid, int other_circid)
  632. {
  633. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  634. "state %d (%s), born %ld:",
  635. conn_array_index, type, this_circid, other_circid, circ->state,
  636. circuit_state_to_string(circ->state),
  637. (long)circ->timestamp_created.tv_sec);
  638. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  639. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  640. }
  641. }
  642. /** Log, at severity <b>severity</b>, information about each circuit
  643. * that is connected to <b>conn</b>.
  644. */
  645. void
  646. circuit_dump_by_conn(connection_t *conn, int severity)
  647. {
  648. circuit_t *circ;
  649. edge_connection_t *tmpconn;
  650. for (circ=global_circuitlist;circ;circ = circ->next) {
  651. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  652. if (circ->marked_for_close)
  653. continue;
  654. if (! CIRCUIT_IS_ORIGIN(circ))
  655. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  656. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  657. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  658. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  659. p_circ_id, n_circ_id);
  660. if (CIRCUIT_IS_ORIGIN(circ)) {
  661. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  662. tmpconn=tmpconn->next_stream) {
  663. if (TO_CONN(tmpconn) == conn) {
  664. circuit_dump_details(severity, circ, conn->conn_array_index,
  665. "App-ward", p_circ_id, n_circ_id);
  666. }
  667. }
  668. }
  669. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  670. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  671. n_circ_id, p_circ_id);
  672. if (! CIRCUIT_IS_ORIGIN(circ)) {
  673. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  674. tmpconn=tmpconn->next_stream) {
  675. if (TO_CONN(tmpconn) == conn) {
  676. circuit_dump_details(severity, circ, conn->conn_array_index,
  677. "Exit-ward", n_circ_id, p_circ_id);
  678. }
  679. }
  680. }
  681. if (!circ->n_conn && circ->n_hop &&
  682. tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
  683. circ->n_hop->port == conn->port &&
  684. conn->type == CONN_TYPE_OR &&
  685. tor_memeq(TO_OR_CONN(conn)->identity_digest,
  686. circ->n_hop->identity_digest, DIGEST_LEN)) {
  687. circuit_dump_details(severity, circ, conn->conn_array_index,
  688. (circ->state == CIRCUIT_STATE_OPEN &&
  689. !CIRCUIT_IS_ORIGIN(circ)) ?
  690. "Endpoint" : "Pending",
  691. n_circ_id, p_circ_id);
  692. }
  693. }
  694. }
  695. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  696. * such circuit exists. */
  697. origin_circuit_t *
  698. circuit_get_by_global_id(uint32_t id)
  699. {
  700. circuit_t *circ;
  701. for (circ=global_circuitlist;circ;circ = circ->next) {
  702. if (CIRCUIT_IS_ORIGIN(circ) &&
  703. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  704. if (circ->marked_for_close)
  705. return NULL;
  706. else
  707. return TO_ORIGIN_CIRCUIT(circ);
  708. }
  709. }
  710. return NULL;
  711. }
  712. /** Return a circ such that:
  713. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  714. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  715. * Return NULL if no such circuit exists.
  716. */
  717. static INLINE circuit_t *
  718. circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
  719. {
  720. orconn_circid_circuit_map_t search;
  721. orconn_circid_circuit_map_t *found;
  722. if (_last_circid_orconn_ent &&
  723. circ_id == _last_circid_orconn_ent->circ_id &&
  724. conn == _last_circid_orconn_ent->or_conn) {
  725. found = _last_circid_orconn_ent;
  726. } else {
  727. search.circ_id = circ_id;
  728. search.or_conn = conn;
  729. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  730. _last_circid_orconn_ent = found;
  731. }
  732. if (found && found->circuit)
  733. return found->circuit;
  734. return NULL;
  735. /* The rest of this checks for bugs. Disabled by default. */
  736. /* We comment it out because coverity complains otherwise.
  737. {
  738. circuit_t *circ;
  739. for (circ=global_circuitlist;circ;circ = circ->next) {
  740. if (! CIRCUIT_IS_ORIGIN(circ)) {
  741. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  742. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  743. log_warn(LD_BUG,
  744. "circuit matches p_conn, but not in hash table (Bug!)");
  745. return circ;
  746. }
  747. }
  748. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  749. log_warn(LD_BUG,
  750. "circuit matches n_conn, but not in hash table (Bug!)");
  751. return circ;
  752. }
  753. }
  754. return NULL;
  755. } */
  756. }
  757. /** Return a circ such that:
  758. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  759. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  760. * - circ is not marked for close.
  761. * Return NULL if no such circuit exists.
  762. */
  763. circuit_t *
  764. circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
  765. {
  766. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  767. if (!circ || circ->marked_for_close)
  768. return NULL;
  769. else
  770. return circ;
  771. }
  772. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  773. * circuit, marked or not, on <b>conn</b>. */
  774. int
  775. circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
  776. {
  777. return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
  778. }
  779. /** Return the circuit that a given edge connection is using. */
  780. circuit_t *
  781. circuit_get_by_edge_conn(edge_connection_t *conn)
  782. {
  783. circuit_t *circ;
  784. circ = conn->on_circuit;
  785. tor_assert(!circ ||
  786. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  787. : circ->magic == OR_CIRCUIT_MAGIC));
  788. return circ;
  789. }
  790. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  791. * circuit from the orconn,circid map, and mark it for close if it hasn't
  792. * been marked already.
  793. */
  794. void
  795. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  796. {
  797. circuit_t *circ;
  798. connection_or_unlink_all_active_circs(conn);
  799. for (circ = global_circuitlist; circ; circ = circ->next) {
  800. int mark = 0;
  801. if (circ->n_conn == conn) {
  802. circuit_set_n_circid_orconn(circ, 0, NULL);
  803. mark = 1;
  804. }
  805. if (! CIRCUIT_IS_ORIGIN(circ)) {
  806. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  807. if (or_circ->p_conn == conn) {
  808. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  809. mark = 1;
  810. }
  811. }
  812. if (mark && !circ->marked_for_close)
  813. circuit_mark_for_close(circ, reason);
  814. }
  815. }
  816. /** Return a circ such that
  817. * - circ-\>rend_data-\>onion_address is equal to
  818. * <b>rend_data</b>-\>onion_address,
  819. * - circ-\>rend_data-\>rend_cookie is equal to
  820. * <b>rend_data</b>-\>rend_cookie, and
  821. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  822. *
  823. * Return NULL if no such circuit exists.
  824. */
  825. origin_circuit_t *
  826. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  827. {
  828. circuit_t *circ;
  829. for (circ = global_circuitlist; circ; circ = circ->next) {
  830. if (!circ->marked_for_close &&
  831. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  832. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  833. if (ocirc->rend_data &&
  834. !rend_cmp_service_ids(rend_data->onion_address,
  835. ocirc->rend_data->onion_address) &&
  836. tor_memeq(ocirc->rend_data->rend_cookie,
  837. rend_data->rend_cookie,
  838. REND_COOKIE_LEN))
  839. return ocirc;
  840. }
  841. }
  842. return NULL;
  843. }
  844. /** Return the first circuit originating here in global_circuitlist after
  845. * <b>start</b> whose purpose is <b>purpose</b>, and where
  846. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  847. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  848. */
  849. origin_circuit_t *
  850. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  851. const char *digest, uint8_t purpose)
  852. {
  853. circuit_t *circ;
  854. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  855. if (start == NULL)
  856. circ = global_circuitlist;
  857. else
  858. circ = TO_CIRCUIT(start)->next;
  859. for ( ; circ; circ = circ->next) {
  860. if (circ->marked_for_close)
  861. continue;
  862. if (circ->purpose != purpose)
  863. continue;
  864. if (!digest)
  865. return TO_ORIGIN_CIRCUIT(circ);
  866. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  867. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  868. digest, DIGEST_LEN))
  869. return TO_ORIGIN_CIRCUIT(circ);
  870. }
  871. return NULL;
  872. }
  873. /** Return the first OR circuit in the global list whose purpose is
  874. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  875. * <b>token</b>. */
  876. static or_circuit_t *
  877. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  878. size_t len)
  879. {
  880. circuit_t *circ;
  881. for (circ = global_circuitlist; circ; circ = circ->next) {
  882. if (! circ->marked_for_close &&
  883. circ->purpose == purpose &&
  884. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  885. return TO_OR_CIRCUIT(circ);
  886. }
  887. return NULL;
  888. }
  889. /** Return the circuit waiting for a rendezvous with the provided cookie.
  890. * Return NULL if no such circuit is found.
  891. */
  892. or_circuit_t *
  893. circuit_get_rendezvous(const char *cookie)
  894. {
  895. return circuit_get_by_rend_token_and_purpose(
  896. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  897. cookie, REND_COOKIE_LEN);
  898. }
  899. /** Return the circuit waiting for intro cells of the given digest.
  900. * Return NULL if no such circuit is found.
  901. */
  902. or_circuit_t *
  903. circuit_get_intro_point(const char *digest)
  904. {
  905. return circuit_get_by_rend_token_and_purpose(
  906. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  907. DIGEST_LEN);
  908. }
  909. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  910. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  911. * flags in <b>flags</b>, and if info is defined, does not already use info
  912. * as any of its hops; or NULL if no circuit fits this description.
  913. *
  914. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  915. * the circuit we want to create, not the purpose of the circuit we want to
  916. * cannibalize.
  917. *
  918. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  919. */
  920. origin_circuit_t *
  921. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  922. int flags)
  923. {
  924. circuit_t *_circ;
  925. origin_circuit_t *best=NULL;
  926. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  927. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  928. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  929. const or_options_t *options = get_options();
  930. /* Make sure we're not trying to create a onehop circ by
  931. * cannibalization. */
  932. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  933. log_debug(LD_CIRC,
  934. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  935. "capacity %d, internal %d",
  936. purpose, need_uptime, need_capacity, internal);
  937. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  938. if (CIRCUIT_IS_ORIGIN(_circ) &&
  939. _circ->state == CIRCUIT_STATE_OPEN &&
  940. !_circ->marked_for_close &&
  941. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  942. !_circ->timestamp_dirty) {
  943. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  944. if ((!need_uptime || circ->build_state->need_uptime) &&
  945. (!need_capacity || circ->build_state->need_capacity) &&
  946. (internal == circ->build_state->is_internal) &&
  947. circ->remaining_relay_early_cells &&
  948. !circ->build_state->onehop_tunnel &&
  949. !circ->isolation_values_set) {
  950. if (info) {
  951. /* need to make sure we don't duplicate hops */
  952. crypt_path_t *hop = circ->cpath;
  953. const node_t *ri1 = node_get_by_id(info->identity_digest);
  954. do {
  955. const node_t *ri2;
  956. if (tor_memeq(hop->extend_info->identity_digest,
  957. info->identity_digest, DIGEST_LEN))
  958. goto next;
  959. if (ri1 &&
  960. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  961. && nodes_in_same_family(ri1, ri2))
  962. goto next;
  963. hop=hop->next;
  964. } while (hop!=circ->cpath);
  965. }
  966. if (options->ExcludeNodes) {
  967. /* Make sure no existing nodes in the circuit are excluded for
  968. * general use. (This may be possible if StrictNodes is 0, and we
  969. * thought we needed to use an otherwise excluded node for, say, a
  970. * directory operation.) */
  971. crypt_path_t *hop = circ->cpath;
  972. do {
  973. if (routerset_contains_extendinfo(options->ExcludeNodes,
  974. hop->extend_info))
  975. goto next;
  976. hop = hop->next;
  977. } while (hop != circ->cpath);
  978. }
  979. if (!best || (best->build_state->need_uptime && !need_uptime))
  980. best = circ;
  981. next: ;
  982. }
  983. }
  984. }
  985. return best;
  986. }
  987. /** Return the number of hops in circuit's path. */
  988. int
  989. circuit_get_cpath_len(origin_circuit_t *circ)
  990. {
  991. int n = 0;
  992. if (circ && circ->cpath) {
  993. crypt_path_t *cpath, *cpath_next = NULL;
  994. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  995. cpath_next = cpath->next;
  996. ++n;
  997. }
  998. }
  999. return n;
  1000. }
  1001. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1002. * aren't that many hops in the list. */
  1003. crypt_path_t *
  1004. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1005. {
  1006. if (circ && circ->cpath && hopnum > 0) {
  1007. crypt_path_t *cpath, *cpath_next = NULL;
  1008. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1009. cpath_next = cpath->next;
  1010. if (--hopnum <= 0)
  1011. return cpath;
  1012. }
  1013. }
  1014. return NULL;
  1015. }
  1016. /** Go through the circuitlist; mark-for-close each circuit that starts
  1017. * at us but has not yet been used. */
  1018. void
  1019. circuit_mark_all_unused_circs(void)
  1020. {
  1021. circuit_t *circ;
  1022. for (circ=global_circuitlist; circ; circ = circ->next) {
  1023. if (CIRCUIT_IS_ORIGIN(circ) &&
  1024. !circ->marked_for_close &&
  1025. !circ->timestamp_dirty)
  1026. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1027. }
  1028. }
  1029. /** Go through the circuitlist; for each circuit that starts at us
  1030. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1031. * new streams.
  1032. *
  1033. * This is useful for letting the user change pseudonyms, so new
  1034. * streams will not be linkable to old streams.
  1035. */
  1036. /* XXX023 this is a bad name for what this function does */
  1037. void
  1038. circuit_expire_all_dirty_circs(void)
  1039. {
  1040. circuit_t *circ;
  1041. const or_options_t *options = get_options();
  1042. for (circ=global_circuitlist; circ; circ = circ->next) {
  1043. if (CIRCUIT_IS_ORIGIN(circ) &&
  1044. !circ->marked_for_close &&
  1045. circ->timestamp_dirty)
  1046. /* XXXX023 This is a screwed-up way to say "This is too dirty
  1047. * for new circuits. */
  1048. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  1049. }
  1050. }
  1051. /** Mark <b>circ</b> to be closed next time we call
  1052. * circuit_close_all_marked(). Do any cleanup needed:
  1053. * - If state is onionskin_pending, remove circ from the onion_pending
  1054. * list.
  1055. * - If circ isn't open yet: call circuit_build_failed() if we're
  1056. * the origin, and in either case call circuit_rep_hist_note_result()
  1057. * to note stats.
  1058. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1059. * failure we just had to the hidden service client module.
  1060. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1061. * report to the hidden service client module that the intro point
  1062. * we just tried may be unreachable.
  1063. * - Send appropriate destroys and edge_destroys for conns and
  1064. * streams attached to circ.
  1065. * - If circ->rend_splice is set (we are the midpoint of a joined
  1066. * rendezvous stream), then mark the other circuit to close as well.
  1067. */
  1068. void
  1069. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  1070. const char *file)
  1071. {
  1072. int orig_reason = reason; /* Passed to the controller */
  1073. assert_circuit_ok(circ);
  1074. tor_assert(line);
  1075. tor_assert(file);
  1076. if (circ->marked_for_close) {
  1077. log(LOG_WARN,LD_BUG,
  1078. "Duplicate call to circuit_mark_for_close at %s:%d"
  1079. " (first at %s:%d)", file, line,
  1080. circ->marked_for_close_file, circ->marked_for_close);
  1081. return;
  1082. }
  1083. if (reason == END_CIRC_AT_ORIGIN) {
  1084. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1085. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1086. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1087. file, line, circ->purpose);
  1088. }
  1089. reason = END_CIRC_REASON_NONE;
  1090. }
  1091. if (CIRCUIT_IS_ORIGIN(circ)) {
  1092. /* We don't send reasons when closing circuits at the origin. */
  1093. reason = END_CIRC_REASON_NONE;
  1094. }
  1095. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1096. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1097. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  1098. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1099. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1100. reason = END_CIRC_REASON_NONE;
  1101. }
  1102. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1103. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1104. }
  1105. /* If the circuit ever became OPEN, we sent it to the reputation history
  1106. * module then. If it isn't OPEN, we send it there now to remember which
  1107. * links worked and which didn't.
  1108. */
  1109. if (circ->state != CIRCUIT_STATE_OPEN) {
  1110. if (CIRCUIT_IS_ORIGIN(circ)) {
  1111. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1112. circuit_build_failed(ocirc); /* take actions if necessary */
  1113. circuit_rep_hist_note_result(ocirc);
  1114. }
  1115. }
  1116. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  1117. if (circuits_pending_or_conns)
  1118. smartlist_remove(circuits_pending_or_conns, circ);
  1119. }
  1120. if (CIRCUIT_IS_ORIGIN(circ)) {
  1121. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1122. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1123. orig_reason);
  1124. }
  1125. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1126. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1127. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1128. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1129. tor_assert(ocirc->build_state->chosen_exit);
  1130. tor_assert(ocirc->rend_data);
  1131. /* treat this like getting a nack from it */
  1132. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1133. safe_str_client(ocirc->rend_data->onion_address),
  1134. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1135. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1136. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1137. ocirc->rend_data,
  1138. timed_out ?
  1139. INTRO_POINT_FAILURE_TIMEOUT :
  1140. INTRO_POINT_FAILURE_GENERIC);
  1141. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1142. reason != END_CIRC_REASON_TIMEOUT) {
  1143. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1144. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1145. log_info(LD_REND, "Failed intro circ %s to %s "
  1146. "(building circuit to intro point). "
  1147. "Marking intro point as possibly unreachable.",
  1148. safe_str_client(ocirc->rend_data->onion_address),
  1149. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1150. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1151. ocirc->rend_data,
  1152. INTRO_POINT_FAILURE_UNREACHABLE);
  1153. }
  1154. }
  1155. if (circ->n_conn) {
  1156. circuit_clear_cell_queue(circ, circ->n_conn);
  1157. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  1158. }
  1159. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1160. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1161. edge_connection_t *conn;
  1162. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1163. connection_edge_destroy(or_circ->p_circ_id, conn);
  1164. or_circ->n_streams = NULL;
  1165. while (or_circ->resolving_streams) {
  1166. conn = or_circ->resolving_streams;
  1167. or_circ->resolving_streams = conn->next_stream;
  1168. if (!conn->_base.marked_for_close) {
  1169. /* The client will see a DESTROY, and infer that the connections
  1170. * are closing because the circuit is getting torn down. No need
  1171. * to send an end cell. */
  1172. conn->edge_has_sent_end = 1;
  1173. conn->end_reason = END_STREAM_REASON_DESTROY;
  1174. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1175. connection_mark_for_close(TO_CONN(conn));
  1176. }
  1177. conn->on_circuit = NULL;
  1178. }
  1179. if (or_circ->p_conn) {
  1180. circuit_clear_cell_queue(circ, or_circ->p_conn);
  1181. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  1182. }
  1183. } else {
  1184. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1185. edge_connection_t *conn;
  1186. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1187. connection_edge_destroy(circ->n_circ_id, conn);
  1188. ocirc->p_streams = NULL;
  1189. }
  1190. circ->marked_for_close = line;
  1191. circ->marked_for_close_file = file;
  1192. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1193. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1194. if (or_circ->rend_splice) {
  1195. if (!or_circ->rend_splice->_base.marked_for_close) {
  1196. /* do this after marking this circuit, to avoid infinite recursion. */
  1197. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1198. }
  1199. or_circ->rend_splice = NULL;
  1200. }
  1201. }
  1202. }
  1203. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1204. * correct. Trigger an assert if anything is invalid.
  1205. */
  1206. void
  1207. assert_cpath_layer_ok(const crypt_path_t *cp)
  1208. {
  1209. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1210. // tor_assert(cp->port);
  1211. tor_assert(cp);
  1212. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1213. switch (cp->state)
  1214. {
  1215. case CPATH_STATE_OPEN:
  1216. tor_assert(cp->f_crypto);
  1217. tor_assert(cp->b_crypto);
  1218. /* fall through */
  1219. case CPATH_STATE_CLOSED:
  1220. tor_assert(!cp->dh_handshake_state);
  1221. break;
  1222. case CPATH_STATE_AWAITING_KEYS:
  1223. /* tor_assert(cp->dh_handshake_state); */
  1224. break;
  1225. default:
  1226. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1227. tor_assert(0);
  1228. }
  1229. tor_assert(cp->package_window >= 0);
  1230. tor_assert(cp->deliver_window >= 0);
  1231. }
  1232. /** Verify that cpath <b>cp</b> has all of its invariants
  1233. * correct. Trigger an assert if anything is invalid.
  1234. */
  1235. static void
  1236. assert_cpath_ok(const crypt_path_t *cp)
  1237. {
  1238. const crypt_path_t *start = cp;
  1239. do {
  1240. assert_cpath_layer_ok(cp);
  1241. /* layers must be in sequence of: "open* awaiting? closed*" */
  1242. if (cp != start) {
  1243. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1244. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1245. } else if (cp->state == CPATH_STATE_OPEN) {
  1246. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1247. }
  1248. }
  1249. cp = cp->next;
  1250. tor_assert(cp);
  1251. } while (cp != start);
  1252. }
  1253. /** Verify that circuit <b>c</b> has all of its invariants
  1254. * correct. Trigger an assert if anything is invalid.
  1255. */
  1256. void
  1257. assert_circuit_ok(const circuit_t *c)
  1258. {
  1259. edge_connection_t *conn;
  1260. const or_circuit_t *or_circ = NULL;
  1261. const origin_circuit_t *origin_circ = NULL;
  1262. tor_assert(c);
  1263. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1264. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1265. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1266. {
  1267. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1268. * never understand. -NM. */
  1269. circuit_t *nonconst_circ = (circuit_t*) c;
  1270. if (CIRCUIT_IS_ORIGIN(c))
  1271. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1272. else
  1273. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1274. }
  1275. if (c->n_conn) {
  1276. tor_assert(!c->n_hop);
  1277. if (c->n_circ_id) {
  1278. /* We use the _impl variant here to make sure we don't fail on marked
  1279. * circuits, which would not be returned by the regular function. */
  1280. circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
  1281. c->n_conn);
  1282. tor_assert(c == c2);
  1283. }
  1284. }
  1285. if (or_circ && or_circ->p_conn) {
  1286. if (or_circ->p_circ_id) {
  1287. /* ibid */
  1288. circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
  1289. or_circ->p_conn);
  1290. tor_assert(c == c2);
  1291. }
  1292. }
  1293. if (or_circ)
  1294. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1295. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1296. tor_assert(c->deliver_window >= 0);
  1297. tor_assert(c->package_window >= 0);
  1298. if (c->state == CIRCUIT_STATE_OPEN) {
  1299. tor_assert(!c->n_conn_onionskin);
  1300. if (or_circ) {
  1301. tor_assert(or_circ->n_crypto);
  1302. tor_assert(or_circ->p_crypto);
  1303. tor_assert(or_circ->n_digest);
  1304. tor_assert(or_circ->p_digest);
  1305. }
  1306. }
  1307. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1308. tor_assert(circuits_pending_or_conns &&
  1309. smartlist_isin(circuits_pending_or_conns, c));
  1310. } else {
  1311. tor_assert(!circuits_pending_or_conns ||
  1312. !smartlist_isin(circuits_pending_or_conns, c));
  1313. }
  1314. if (origin_circ && origin_circ->cpath) {
  1315. assert_cpath_ok(origin_circ->cpath);
  1316. }
  1317. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1318. tor_assert(or_circ);
  1319. if (!c->marked_for_close) {
  1320. tor_assert(or_circ->rend_splice);
  1321. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1322. }
  1323. tor_assert(or_circ->rend_splice != or_circ);
  1324. } else {
  1325. tor_assert(!or_circ || !or_circ->rend_splice);
  1326. }
  1327. }