circuitlist.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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 human-readable string for the circuit purpose <b>purpose</b>. */
  338. const char *
  339. circuit_purpose_to_string(uint8_t purpose)
  340. {
  341. static char buf[32];
  342. switch (purpose)
  343. {
  344. case CIRCUIT_PURPOSE_OR:
  345. return "Circuit at relay";
  346. case CIRCUIT_PURPOSE_INTRO_POINT:
  347. return "Acting as intro point";
  348. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  349. return "Acting as rendevous (pending)";
  350. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  351. return "Acting as rendevous (established)";
  352. case CIRCUIT_PURPOSE_C_GENERAL:
  353. return "General-purpose client";
  354. case CIRCUIT_PURPOSE_C_INTRODUCING:
  355. return "Hidden service client: Connecting to intro point";
  356. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  357. return "Hidden service client: Waiting for ack from intro point";
  358. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  359. return "Hidden service client: Received ack from intro point";
  360. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  361. return "Hidden service client: Establishing rendezvous point";
  362. case CIRCUIT_PURPOSE_C_REND_READY:
  363. return "Hidden service client: Pending rendezvous point";
  364. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  365. return "Hidden service client: Pending rendezvous point (ack received)";
  366. case CIRCUIT_PURPOSE_C_REND_JOINED:
  367. return "Hidden service client: Active rendezvous point";
  368. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  369. return "Measuring circuit timeout";
  370. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  371. return "Hidden service: Establishing introduction point";
  372. case CIRCUIT_PURPOSE_S_INTRO:
  373. return "Hidden service: Introduction point";
  374. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  375. return "Hidden service: Connecting to rendezvous point";
  376. case CIRCUIT_PURPOSE_S_REND_JOINED:
  377. return "Hidden service: Active rendezvous point";
  378. case CIRCUIT_PURPOSE_TESTING:
  379. return "Testing circuit";
  380. case CIRCUIT_PURPOSE_CONTROLLER:
  381. return "Circuit made by controller";
  382. default:
  383. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  384. return buf;
  385. }
  386. }
  387. /** Pick a reasonable package_window to start out for our circuits.
  388. * Originally this was hard-coded at 1000, but now the consensus votes
  389. * on the answer. See proposal 168. */
  390. int32_t
  391. circuit_initial_package_window(void)
  392. {
  393. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  394. CIRCWINDOW_START_MIN,
  395. CIRCWINDOW_START_MAX);
  396. /* If the consensus tells us a negative number, we'd assert. */
  397. if (num < 0)
  398. num = CIRCWINDOW_START;
  399. return num;
  400. }
  401. /** Initialize the common elements in a circuit_t, and add it to the global
  402. * list. */
  403. static void
  404. init_circuit_base(circuit_t *circ)
  405. {
  406. tor_gettimeofday(&circ->timestamp_created);
  407. circ->package_window = circuit_initial_package_window();
  408. circ->deliver_window = CIRCWINDOW_START;
  409. /* Initialize the cell_ewma_t structure */
  410. circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  411. circ->n_cell_ewma.cell_count = 0.0;
  412. circ->n_cell_ewma.heap_index = -1;
  413. circ->n_cell_ewma.is_for_p_conn = 0;
  414. circuit_add(circ);
  415. }
  416. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  417. * and <b>p_conn</b>. Add it to the global circuit list.
  418. */
  419. origin_circuit_t *
  420. origin_circuit_new(void)
  421. {
  422. origin_circuit_t *circ;
  423. /* never zero, since a global ID of 0 is treated specially by the
  424. * controller */
  425. static uint32_t n_circuits_allocated = 1;
  426. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  427. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  428. circ->next_stream_id = crypto_rand_int(1<<16);
  429. circ->global_identifier = n_circuits_allocated++;
  430. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  431. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  432. init_circuit_base(TO_CIRCUIT(circ));
  433. circ_times.last_circ_at = approx_time();
  434. return circ;
  435. }
  436. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  437. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  438. or_circuit_t *
  439. or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
  440. {
  441. /* CircIDs */
  442. or_circuit_t *circ;
  443. circ = tor_malloc_zero(sizeof(or_circuit_t));
  444. circ->_base.magic = OR_CIRCUIT_MAGIC;
  445. if (p_conn)
  446. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  447. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  448. init_circuit_base(TO_CIRCUIT(circ));
  449. /* Initialize the cell_ewma_t structure */
  450. /* Initialize the cell counts to 0 */
  451. circ->p_cell_ewma.cell_count = 0.0;
  452. circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  453. circ->p_cell_ewma.is_for_p_conn = 1;
  454. /* It's not in any heap yet. */
  455. circ->p_cell_ewma.heap_index = -1;
  456. return circ;
  457. }
  458. /** Deallocate space associated with circ.
  459. */
  460. static void
  461. circuit_free(circuit_t *circ)
  462. {
  463. void *mem;
  464. size_t memlen;
  465. if (!circ)
  466. return;
  467. if (CIRCUIT_IS_ORIGIN(circ)) {
  468. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  469. mem = ocirc;
  470. memlen = sizeof(origin_circuit_t);
  471. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  472. if (ocirc->build_state) {
  473. extend_info_free(ocirc->build_state->chosen_exit);
  474. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  475. }
  476. tor_free(ocirc->build_state);
  477. circuit_free_cpath(ocirc->cpath);
  478. crypto_free_pk_env(ocirc->intro_key);
  479. rend_data_free(ocirc->rend_data);
  480. tor_free(ocirc->dest_address);
  481. tor_free(ocirc->socks_username);
  482. tor_free(ocirc->socks_password);
  483. } else {
  484. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  485. /* Remember cell statistics for this circuit before deallocating. */
  486. if (get_options()->CellStatistics)
  487. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  488. mem = ocirc;
  489. memlen = sizeof(or_circuit_t);
  490. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  491. crypto_free_cipher_env(ocirc->p_crypto);
  492. crypto_free_digest_env(ocirc->p_digest);
  493. crypto_free_cipher_env(ocirc->n_crypto);
  494. crypto_free_digest_env(ocirc->n_digest);
  495. if (ocirc->rend_splice) {
  496. or_circuit_t *other = ocirc->rend_splice;
  497. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  498. other->rend_splice = NULL;
  499. }
  500. /* remove from map. */
  501. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  502. /* Clear cell queue _after_ removing it from the map. Otherwise our
  503. * "active" checks will be violated. */
  504. cell_queue_clear(&ocirc->p_conn_cells);
  505. }
  506. extend_info_free(circ->n_hop);
  507. tor_free(circ->n_conn_onionskin);
  508. /* Remove from map. */
  509. circuit_set_n_circid_orconn(circ, 0, NULL);
  510. /* Clear cell queue _after_ removing it from the map. Otherwise our
  511. * "active" checks will be violated. */
  512. cell_queue_clear(&circ->n_conn_cells);
  513. memset(mem, 0xAA, memlen); /* poison memory */
  514. tor_free(mem);
  515. }
  516. /** Deallocate space associated with the linked list <b>cpath</b>. */
  517. static void
  518. circuit_free_cpath(crypt_path_t *cpath)
  519. {
  520. crypt_path_t *victim, *head=cpath;
  521. if (!cpath)
  522. return;
  523. /* it's a doubly linked list, so we have to notice when we've
  524. * gone through it once. */
  525. while (cpath->next && cpath->next != head) {
  526. victim = cpath;
  527. cpath = victim->next;
  528. circuit_free_cpath_node(victim);
  529. }
  530. circuit_free_cpath_node(cpath);
  531. }
  532. /** Release all storage held by circuits. */
  533. void
  534. circuit_free_all(void)
  535. {
  536. circuit_t *next;
  537. while (global_circuitlist) {
  538. next = global_circuitlist->next;
  539. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  540. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  541. while (or_circ->resolving_streams) {
  542. edge_connection_t *next_conn;
  543. next_conn = or_circ->resolving_streams->next_stream;
  544. connection_free(TO_CONN(or_circ->resolving_streams));
  545. or_circ->resolving_streams = next_conn;
  546. }
  547. }
  548. circuit_free(global_circuitlist);
  549. global_circuitlist = next;
  550. }
  551. smartlist_free(circuits_pending_or_conns);
  552. circuits_pending_or_conns = NULL;
  553. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  554. }
  555. /** Deallocate space associated with the cpath node <b>victim</b>. */
  556. static void
  557. circuit_free_cpath_node(crypt_path_t *victim)
  558. {
  559. if (!victim)
  560. return;
  561. crypto_free_cipher_env(victim->f_crypto);
  562. crypto_free_cipher_env(victim->b_crypto);
  563. crypto_free_digest_env(victim->f_digest);
  564. crypto_free_digest_env(victim->b_digest);
  565. crypto_dh_free(victim->dh_handshake_state);
  566. extend_info_free(victim->extend_info);
  567. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  568. tor_free(victim);
  569. }
  570. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  571. * of information about circuit <b>circ</b>.
  572. */
  573. static void
  574. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  575. const char *type, int this_circid, int other_circid)
  576. {
  577. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  578. "state %d (%s), born %ld:",
  579. conn_array_index, type, this_circid, other_circid, circ->state,
  580. circuit_state_to_string(circ->state),
  581. (long)circ->timestamp_created.tv_sec);
  582. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  583. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  584. }
  585. }
  586. /** Log, at severity <b>severity</b>, information about each circuit
  587. * that is connected to <b>conn</b>.
  588. */
  589. void
  590. circuit_dump_by_conn(connection_t *conn, int severity)
  591. {
  592. circuit_t *circ;
  593. edge_connection_t *tmpconn;
  594. for (circ=global_circuitlist;circ;circ = circ->next) {
  595. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  596. if (circ->marked_for_close)
  597. continue;
  598. if (! CIRCUIT_IS_ORIGIN(circ))
  599. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  600. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  601. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  602. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  603. p_circ_id, n_circ_id);
  604. if (CIRCUIT_IS_ORIGIN(circ)) {
  605. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  606. tmpconn=tmpconn->next_stream) {
  607. if (TO_CONN(tmpconn) == conn) {
  608. circuit_dump_details(severity, circ, conn->conn_array_index,
  609. "App-ward", p_circ_id, n_circ_id);
  610. }
  611. }
  612. }
  613. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  614. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  615. n_circ_id, p_circ_id);
  616. if (! CIRCUIT_IS_ORIGIN(circ)) {
  617. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  618. tmpconn=tmpconn->next_stream) {
  619. if (TO_CONN(tmpconn) == conn) {
  620. circuit_dump_details(severity, circ, conn->conn_array_index,
  621. "Exit-ward", n_circ_id, p_circ_id);
  622. }
  623. }
  624. }
  625. if (!circ->n_conn && circ->n_hop &&
  626. tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
  627. circ->n_hop->port == conn->port &&
  628. conn->type == CONN_TYPE_OR &&
  629. tor_memeq(TO_OR_CONN(conn)->identity_digest,
  630. circ->n_hop->identity_digest, DIGEST_LEN)) {
  631. circuit_dump_details(severity, circ, conn->conn_array_index,
  632. (circ->state == CIRCUIT_STATE_OPEN &&
  633. !CIRCUIT_IS_ORIGIN(circ)) ?
  634. "Endpoint" : "Pending",
  635. n_circ_id, p_circ_id);
  636. }
  637. }
  638. }
  639. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  640. * such circuit exists. */
  641. origin_circuit_t *
  642. circuit_get_by_global_id(uint32_t id)
  643. {
  644. circuit_t *circ;
  645. for (circ=global_circuitlist;circ;circ = circ->next) {
  646. if (CIRCUIT_IS_ORIGIN(circ) &&
  647. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  648. if (circ->marked_for_close)
  649. return NULL;
  650. else
  651. return TO_ORIGIN_CIRCUIT(circ);
  652. }
  653. }
  654. return NULL;
  655. }
  656. /** Return a circ such that:
  657. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  658. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  659. * Return NULL if no such circuit exists.
  660. */
  661. static INLINE circuit_t *
  662. circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
  663. {
  664. orconn_circid_circuit_map_t search;
  665. orconn_circid_circuit_map_t *found;
  666. if (_last_circid_orconn_ent &&
  667. circ_id == _last_circid_orconn_ent->circ_id &&
  668. conn == _last_circid_orconn_ent->or_conn) {
  669. found = _last_circid_orconn_ent;
  670. } else {
  671. search.circ_id = circ_id;
  672. search.or_conn = conn;
  673. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  674. _last_circid_orconn_ent = found;
  675. }
  676. if (found && found->circuit)
  677. return found->circuit;
  678. return NULL;
  679. /* The rest of this checks for bugs. Disabled by default. */
  680. /* We comment it out because coverity complains otherwise.
  681. {
  682. circuit_t *circ;
  683. for (circ=global_circuitlist;circ;circ = circ->next) {
  684. if (! CIRCUIT_IS_ORIGIN(circ)) {
  685. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  686. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  687. log_warn(LD_BUG,
  688. "circuit matches p_conn, but not in hash table (Bug!)");
  689. return circ;
  690. }
  691. }
  692. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  693. log_warn(LD_BUG,
  694. "circuit matches n_conn, but not in hash table (Bug!)");
  695. return circ;
  696. }
  697. }
  698. return NULL;
  699. } */
  700. }
  701. /** Return a circ such that:
  702. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  703. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  704. * - circ is not marked for close.
  705. * Return NULL if no such circuit exists.
  706. */
  707. circuit_t *
  708. circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
  709. {
  710. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  711. if (!circ || circ->marked_for_close)
  712. return NULL;
  713. else
  714. return circ;
  715. }
  716. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  717. * circuit, marked or not, on <b>conn</b>. */
  718. int
  719. circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
  720. {
  721. return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
  722. }
  723. /** Return the circuit that a given edge connection is using. */
  724. circuit_t *
  725. circuit_get_by_edge_conn(edge_connection_t *conn)
  726. {
  727. circuit_t *circ;
  728. circ = conn->on_circuit;
  729. tor_assert(!circ ||
  730. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  731. : circ->magic == OR_CIRCUIT_MAGIC));
  732. return circ;
  733. }
  734. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  735. * circuit from the orconn,circid map, and mark it for close if it hasn't
  736. * been marked already.
  737. */
  738. void
  739. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  740. {
  741. circuit_t *circ;
  742. connection_or_unlink_all_active_circs(conn);
  743. for (circ = global_circuitlist; circ; circ = circ->next) {
  744. int mark = 0;
  745. if (circ->n_conn == conn) {
  746. circuit_set_n_circid_orconn(circ, 0, NULL);
  747. mark = 1;
  748. }
  749. if (! CIRCUIT_IS_ORIGIN(circ)) {
  750. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  751. if (or_circ->p_conn == conn) {
  752. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  753. mark = 1;
  754. }
  755. }
  756. if (mark && !circ->marked_for_close)
  757. circuit_mark_for_close(circ, reason);
  758. }
  759. }
  760. /** Return a circ such that:
  761. * - circ-\>rend_data-\>onion_address is equal to <b>rend_query</b>, and
  762. * - circ-\>purpose is equal to <b>purpose</b>.
  763. *
  764. * Return NULL if no such circuit exists.
  765. */
  766. origin_circuit_t *
  767. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  768. {
  769. circuit_t *circ;
  770. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  771. for (circ = global_circuitlist; circ; circ = circ->next) {
  772. if (!circ->marked_for_close &&
  773. circ->purpose == purpose) {
  774. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  775. if (ocirc->rend_data &&
  776. !rend_cmp_service_ids(rend_query,
  777. ocirc->rend_data->onion_address))
  778. return ocirc;
  779. }
  780. }
  781. return NULL;
  782. }
  783. /** Return the first circuit originating here in global_circuitlist after
  784. * <b>start</b> whose purpose is <b>purpose</b>, and where
  785. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  786. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  787. */
  788. origin_circuit_t *
  789. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  790. const char *digest, uint8_t purpose)
  791. {
  792. circuit_t *circ;
  793. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  794. if (start == NULL)
  795. circ = global_circuitlist;
  796. else
  797. circ = TO_CIRCUIT(start)->next;
  798. for ( ; circ; circ = circ->next) {
  799. if (circ->marked_for_close)
  800. continue;
  801. if (circ->purpose != purpose)
  802. continue;
  803. if (!digest)
  804. return TO_ORIGIN_CIRCUIT(circ);
  805. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  806. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  807. digest, DIGEST_LEN))
  808. return TO_ORIGIN_CIRCUIT(circ);
  809. }
  810. return NULL;
  811. }
  812. /** Return the first OR circuit in the global list whose purpose is
  813. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  814. * <b>token</b>. */
  815. static or_circuit_t *
  816. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  817. size_t len)
  818. {
  819. circuit_t *circ;
  820. for (circ = global_circuitlist; circ; circ = circ->next) {
  821. if (! circ->marked_for_close &&
  822. circ->purpose == purpose &&
  823. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  824. return TO_OR_CIRCUIT(circ);
  825. }
  826. return NULL;
  827. }
  828. /** Return the circuit waiting for a rendezvous with the provided cookie.
  829. * Return NULL if no such circuit is found.
  830. */
  831. or_circuit_t *
  832. circuit_get_rendezvous(const char *cookie)
  833. {
  834. return circuit_get_by_rend_token_and_purpose(
  835. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  836. cookie, REND_COOKIE_LEN);
  837. }
  838. /** Return the circuit waiting for intro cells of the given digest.
  839. * Return NULL if no such circuit is found.
  840. */
  841. or_circuit_t *
  842. circuit_get_intro_point(const char *digest)
  843. {
  844. return circuit_get_by_rend_token_and_purpose(
  845. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  846. DIGEST_LEN);
  847. }
  848. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  849. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  850. * flags in <b>flags</b>, and if info is defined, does not already use info
  851. * as any of its hops; or NULL if no circuit fits this description.
  852. *
  853. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  854. * the circuit we want to create, not the purpose of the circuit we want to
  855. * cannibalize.
  856. *
  857. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  858. */
  859. origin_circuit_t *
  860. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  861. int flags)
  862. {
  863. circuit_t *_circ;
  864. origin_circuit_t *best=NULL;
  865. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  866. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  867. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  868. const or_options_t *options = get_options();
  869. /* Make sure we're not trying to create a onehop circ by
  870. * cannibalization. */
  871. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  872. log_debug(LD_CIRC,
  873. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  874. "capacity %d, internal %d",
  875. purpose, need_uptime, need_capacity, internal);
  876. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  877. if (CIRCUIT_IS_ORIGIN(_circ) &&
  878. _circ->state == CIRCUIT_STATE_OPEN &&
  879. !_circ->marked_for_close &&
  880. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  881. !_circ->timestamp_dirty) {
  882. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  883. if ((!need_uptime || circ->build_state->need_uptime) &&
  884. (!need_capacity || circ->build_state->need_capacity) &&
  885. (internal == circ->build_state->is_internal) &&
  886. circ->remaining_relay_early_cells &&
  887. !circ->build_state->onehop_tunnel &&
  888. !circ->isolation_values_set) {
  889. if (info) {
  890. /* need to make sure we don't duplicate hops */
  891. crypt_path_t *hop = circ->cpath;
  892. const node_t *ri1 = node_get_by_id(info->identity_digest);
  893. do {
  894. const node_t *ri2;
  895. if (tor_memeq(hop->extend_info->identity_digest,
  896. info->identity_digest, DIGEST_LEN))
  897. goto next;
  898. if (ri1 &&
  899. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  900. && nodes_in_same_family(ri1, ri2))
  901. goto next;
  902. hop=hop->next;
  903. } while (hop!=circ->cpath);
  904. }
  905. if (options->ExcludeNodes) {
  906. /* Make sure no existing nodes in the circuit are excluded for
  907. * general use. (This may be possible if StrictNodes is 0, and we
  908. * thought we needed to use an otherwise excluded node for, say, a
  909. * directory operation.) */
  910. crypt_path_t *hop = circ->cpath;
  911. do {
  912. if (routerset_contains_extendinfo(options->ExcludeNodes,
  913. hop->extend_info))
  914. goto next;
  915. hop = hop->next;
  916. } while (hop != circ->cpath);
  917. }
  918. if (!best || (best->build_state->need_uptime && !need_uptime))
  919. best = circ;
  920. next: ;
  921. }
  922. }
  923. }
  924. return best;
  925. }
  926. /** Return the number of hops in circuit's path. */
  927. int
  928. circuit_get_cpath_len(origin_circuit_t *circ)
  929. {
  930. int n = 0;
  931. if (circ && circ->cpath) {
  932. crypt_path_t *cpath, *cpath_next = NULL;
  933. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  934. cpath_next = cpath->next;
  935. ++n;
  936. }
  937. }
  938. return n;
  939. }
  940. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  941. * aren't that many hops in the list. */
  942. crypt_path_t *
  943. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  944. {
  945. if (circ && circ->cpath && hopnum > 0) {
  946. crypt_path_t *cpath, *cpath_next = NULL;
  947. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  948. cpath_next = cpath->next;
  949. if (--hopnum <= 0)
  950. return cpath;
  951. }
  952. }
  953. return NULL;
  954. }
  955. /** Go through the circuitlist; mark-for-close each circuit that starts
  956. * at us but has not yet been used. */
  957. void
  958. circuit_mark_all_unused_circs(void)
  959. {
  960. circuit_t *circ;
  961. for (circ=global_circuitlist; circ; circ = circ->next) {
  962. if (CIRCUIT_IS_ORIGIN(circ) &&
  963. !circ->marked_for_close &&
  964. !circ->timestamp_dirty)
  965. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  966. }
  967. }
  968. /** Go through the circuitlist; for each circuit that starts at us
  969. * and is dirty, frob its timestamp_dirty so we won't use it for any
  970. * new streams.
  971. *
  972. * This is useful for letting the user change pseudonyms, so new
  973. * streams will not be linkable to old streams.
  974. */
  975. /* XXX023 this is a bad name for what this function does */
  976. void
  977. circuit_expire_all_dirty_circs(void)
  978. {
  979. circuit_t *circ;
  980. const or_options_t *options = get_options();
  981. for (circ=global_circuitlist; circ; circ = circ->next) {
  982. if (CIRCUIT_IS_ORIGIN(circ) &&
  983. !circ->marked_for_close &&
  984. circ->timestamp_dirty)
  985. /* XXXX023 This is a screwed-up way to say "This is too dirty
  986. * for new circuits. */
  987. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  988. }
  989. }
  990. /** Mark <b>circ</b> to be closed next time we call
  991. * circuit_close_all_marked(). Do any cleanup needed:
  992. * - If state is onionskin_pending, remove circ from the onion_pending
  993. * list.
  994. * - If circ isn't open yet: call circuit_build_failed() if we're
  995. * the origin, and in either case call circuit_rep_hist_note_result()
  996. * to note stats.
  997. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  998. * just tried from our list of intro points for that service
  999. * descriptor.
  1000. * - Send appropriate destroys and edge_destroys for conns and
  1001. * streams attached to circ.
  1002. * - If circ->rend_splice is set (we are the midpoint of a joined
  1003. * rendezvous stream), then mark the other circuit to close as well.
  1004. */
  1005. void
  1006. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  1007. const char *file)
  1008. {
  1009. int orig_reason = reason; /* Passed to the controller */
  1010. assert_circuit_ok(circ);
  1011. tor_assert(line);
  1012. tor_assert(file);
  1013. if (circ->marked_for_close) {
  1014. log(LOG_WARN,LD_BUG,
  1015. "Duplicate call to circuit_mark_for_close at %s:%d"
  1016. " (first at %s:%d)", file, line,
  1017. circ->marked_for_close_file, circ->marked_for_close);
  1018. return;
  1019. }
  1020. if (reason == END_CIRC_AT_ORIGIN) {
  1021. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1022. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1023. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1024. file, line, circ->purpose);
  1025. }
  1026. reason = END_CIRC_REASON_NONE;
  1027. }
  1028. if (CIRCUIT_IS_ORIGIN(circ)) {
  1029. /* We don't send reasons when closing circuits at the origin. */
  1030. reason = END_CIRC_REASON_NONE;
  1031. }
  1032. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1033. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1034. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  1035. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1036. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1037. reason = END_CIRC_REASON_NONE;
  1038. }
  1039. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1040. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1041. }
  1042. /* If the circuit ever became OPEN, we sent it to the reputation history
  1043. * module then. If it isn't OPEN, we send it there now to remember which
  1044. * links worked and which didn't.
  1045. */
  1046. if (circ->state != CIRCUIT_STATE_OPEN) {
  1047. if (CIRCUIT_IS_ORIGIN(circ)) {
  1048. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1049. circuit_build_failed(ocirc); /* take actions if necessary */
  1050. circuit_rep_hist_note_result(ocirc);
  1051. }
  1052. }
  1053. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  1054. if (circuits_pending_or_conns)
  1055. smartlist_remove(circuits_pending_or_conns, circ);
  1056. }
  1057. if (CIRCUIT_IS_ORIGIN(circ)) {
  1058. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1059. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1060. orig_reason);
  1061. }
  1062. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1063. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1064. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1065. tor_assert(ocirc->build_state->chosen_exit);
  1066. tor_assert(ocirc->rend_data);
  1067. /* treat this like getting a nack from it */
  1068. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  1069. "Removing from descriptor.",
  1070. safe_str_client(ocirc->rend_data->onion_address),
  1071. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1072. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  1073. ocirc->rend_data);
  1074. }
  1075. if (circ->n_conn) {
  1076. circuit_clear_cell_queue(circ, circ->n_conn);
  1077. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  1078. }
  1079. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1080. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1081. edge_connection_t *conn;
  1082. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1083. connection_edge_destroy(or_circ->p_circ_id, conn);
  1084. or_circ->n_streams = NULL;
  1085. while (or_circ->resolving_streams) {
  1086. conn = or_circ->resolving_streams;
  1087. or_circ->resolving_streams = conn->next_stream;
  1088. if (!conn->_base.marked_for_close) {
  1089. /* The client will see a DESTROY, and infer that the connections
  1090. * are closing because the circuit is getting torn down. No need
  1091. * to send an end cell. */
  1092. conn->edge_has_sent_end = 1;
  1093. conn->end_reason = END_STREAM_REASON_DESTROY;
  1094. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1095. connection_mark_for_close(TO_CONN(conn));
  1096. }
  1097. conn->on_circuit = NULL;
  1098. }
  1099. if (or_circ->p_conn) {
  1100. circuit_clear_cell_queue(circ, or_circ->p_conn);
  1101. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  1102. }
  1103. } else {
  1104. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1105. edge_connection_t *conn;
  1106. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1107. connection_edge_destroy(circ->n_circ_id, conn);
  1108. ocirc->p_streams = NULL;
  1109. }
  1110. circ->marked_for_close = line;
  1111. circ->marked_for_close_file = file;
  1112. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1113. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1114. if (or_circ->rend_splice) {
  1115. if (!or_circ->rend_splice->_base.marked_for_close) {
  1116. /* do this after marking this circuit, to avoid infinite recursion. */
  1117. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1118. }
  1119. or_circ->rend_splice = NULL;
  1120. }
  1121. }
  1122. }
  1123. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1124. * correct. Trigger an assert if anything is invalid.
  1125. */
  1126. void
  1127. assert_cpath_layer_ok(const crypt_path_t *cp)
  1128. {
  1129. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1130. // tor_assert(cp->port);
  1131. tor_assert(cp);
  1132. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1133. switch (cp->state)
  1134. {
  1135. case CPATH_STATE_OPEN:
  1136. tor_assert(cp->f_crypto);
  1137. tor_assert(cp->b_crypto);
  1138. /* fall through */
  1139. case CPATH_STATE_CLOSED:
  1140. tor_assert(!cp->dh_handshake_state);
  1141. break;
  1142. case CPATH_STATE_AWAITING_KEYS:
  1143. /* tor_assert(cp->dh_handshake_state); */
  1144. break;
  1145. default:
  1146. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1147. tor_assert(0);
  1148. }
  1149. tor_assert(cp->package_window >= 0);
  1150. tor_assert(cp->deliver_window >= 0);
  1151. }
  1152. /** Verify that cpath <b>cp</b> has all of its invariants
  1153. * correct. Trigger an assert if anything is invalid.
  1154. */
  1155. static void
  1156. assert_cpath_ok(const crypt_path_t *cp)
  1157. {
  1158. const crypt_path_t *start = cp;
  1159. do {
  1160. assert_cpath_layer_ok(cp);
  1161. /* layers must be in sequence of: "open* awaiting? closed*" */
  1162. if (cp != start) {
  1163. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1164. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1165. } else if (cp->state == CPATH_STATE_OPEN) {
  1166. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1167. }
  1168. }
  1169. cp = cp->next;
  1170. tor_assert(cp);
  1171. } while (cp != start);
  1172. }
  1173. /** Verify that circuit <b>c</b> has all of its invariants
  1174. * correct. Trigger an assert if anything is invalid.
  1175. */
  1176. void
  1177. assert_circuit_ok(const circuit_t *c)
  1178. {
  1179. edge_connection_t *conn;
  1180. const or_circuit_t *or_circ = NULL;
  1181. const origin_circuit_t *origin_circ = NULL;
  1182. tor_assert(c);
  1183. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1184. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1185. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1186. {
  1187. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1188. * never understand. -NM. */
  1189. circuit_t *nonconst_circ = (circuit_t*) c;
  1190. if (CIRCUIT_IS_ORIGIN(c))
  1191. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1192. else
  1193. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1194. }
  1195. if (c->n_conn) {
  1196. tor_assert(!c->n_hop);
  1197. if (c->n_circ_id) {
  1198. /* We use the _impl variant here to make sure we don't fail on marked
  1199. * circuits, which would not be returned by the regular function. */
  1200. circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
  1201. c->n_conn);
  1202. tor_assert(c == c2);
  1203. }
  1204. }
  1205. if (or_circ && or_circ->p_conn) {
  1206. if (or_circ->p_circ_id) {
  1207. /* ibid */
  1208. circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
  1209. or_circ->p_conn);
  1210. tor_assert(c == c2);
  1211. }
  1212. }
  1213. if (or_circ)
  1214. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1215. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1216. tor_assert(c->deliver_window >= 0);
  1217. tor_assert(c->package_window >= 0);
  1218. if (c->state == CIRCUIT_STATE_OPEN) {
  1219. tor_assert(!c->n_conn_onionskin);
  1220. if (or_circ) {
  1221. tor_assert(or_circ->n_crypto);
  1222. tor_assert(or_circ->p_crypto);
  1223. tor_assert(or_circ->n_digest);
  1224. tor_assert(or_circ->p_digest);
  1225. }
  1226. }
  1227. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1228. tor_assert(circuits_pending_or_conns &&
  1229. smartlist_isin(circuits_pending_or_conns, c));
  1230. } else {
  1231. tor_assert(!circuits_pending_or_conns ||
  1232. !smartlist_isin(circuits_pending_or_conns, c));
  1233. }
  1234. if (origin_circ && origin_circ->cpath) {
  1235. assert_cpath_ok(origin_circ->cpath);
  1236. }
  1237. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1238. tor_assert(or_circ);
  1239. if (!c->marked_for_close) {
  1240. tor_assert(or_circ->rend_splice);
  1241. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1242. }
  1243. tor_assert(or_circ->rend_splice != or_circ);
  1244. } else {
  1245. tor_assert(!or_circ || !or_circ->rend_splice);
  1246. }
  1247. }