circuitlist.c 48 KB

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