circuitlist.c 51 KB

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