circuitlist.c 49 KB

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