circuitlist.c 49 KB

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