circuit.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  7. crypt_path_t **layer_hint, char *recognized);
  8. static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction);
  9. static void circuit_free_cpath_node(crypt_path_t *victim);
  10. static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
  11. static void circuit_rep_hist_note_result(circuit_t *circ);
  12. static void circuit_is_open(circuit_t *circ);
  13. static void circuit_build_failed(circuit_t *circ);
  14. static circuit_t *circuit_establish_circuit(uint8_t purpose, const char *exit_nickname);
  15. unsigned long stats_n_relay_cells_relayed = 0;
  16. unsigned long stats_n_relay_cells_delivered = 0;
  17. /********* START VARIABLES **********/
  18. static int circuitlist_len=0;
  19. static circuit_t *global_circuitlist=NULL;
  20. char *circuit_state_to_string[] = {
  21. "doing handshakes", /* 0 */
  22. "processing the onion", /* 1 */
  23. "connecting to firsthop", /* 2 */
  24. "open" /* 3 */
  25. };
  26. /********* END VARIABLES ************/
  27. void circuit_add(circuit_t *circ) {
  28. if(!global_circuitlist) { /* first one */
  29. global_circuitlist = circ;
  30. circ->next = NULL;
  31. } else {
  32. circ->next = global_circuitlist;
  33. global_circuitlist = circ;
  34. }
  35. ++circuitlist_len;
  36. }
  37. void circuit_remove(circuit_t *circ) {
  38. circuit_t *tmpcirc;
  39. assert(circ && global_circuitlist);
  40. if(global_circuitlist == circ) {
  41. global_circuitlist = global_circuitlist->next;
  42. --circuitlist_len;
  43. return;
  44. }
  45. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  46. if(tmpcirc->next == circ) {
  47. tmpcirc->next = circ->next;
  48. --circuitlist_len;
  49. return;
  50. }
  51. }
  52. }
  53. void circuit_close_all_marked()
  54. {
  55. circuit_t *tmp,*m;
  56. while (global_circuitlist && global_circuitlist->marked_for_close) {
  57. tmp = global_circuitlist->next;
  58. circuit_free(global_circuitlist);
  59. global_circuitlist = tmp;
  60. }
  61. tmp = global_circuitlist;
  62. while (tmp && tmp->next) {
  63. if (tmp->next->marked_for_close) {
  64. m = tmp->next->next;
  65. circuit_free(tmp->next);
  66. tmp->next = m;
  67. /* Need to check new tmp->next; don't advance tmp. */
  68. } else {
  69. /* Advance tmp. */
  70. tmp = tmp->next;
  71. }
  72. }
  73. }
  74. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
  75. circuit_t *circ;
  76. circ = tor_malloc_zero(sizeof(circuit_t));
  77. circ->magic = CIRCUIT_MAGIC;
  78. circ->timestamp_created = time(NULL);
  79. circ->p_circ_id = p_circ_id;
  80. circ->p_conn = p_conn;
  81. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  82. /* CircIDs */
  83. circ->p_circ_id = p_circ_id;
  84. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  85. circ->package_window = CIRCWINDOW_START;
  86. circ->deliver_window = CIRCWINDOW_START;
  87. circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
  88. circuit_add(circ);
  89. return circ;
  90. }
  91. void circuit_free(circuit_t *circ) {
  92. assert(circ);
  93. assert(circ->magic == CIRCUIT_MAGIC);
  94. if (circ->n_crypto)
  95. crypto_free_cipher_env(circ->n_crypto);
  96. if (circ->p_crypto)
  97. crypto_free_cipher_env(circ->p_crypto);
  98. if (circ->n_digest)
  99. crypto_free_digest_env(circ->n_digest);
  100. if (circ->p_digest)
  101. crypto_free_digest_env(circ->p_digest);
  102. if(circ->build_state) {
  103. tor_free(circ->build_state->chosen_exit);
  104. if (circ->build_state->pending_final_cpath)
  105. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  106. }
  107. tor_free(circ->build_state);
  108. circuit_free_cpath(circ->cpath);
  109. if (circ->rend_splice) {
  110. circ->rend_splice->rend_splice = NULL;
  111. }
  112. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  113. free(circ);
  114. }
  115. void circuit_free_cpath(crypt_path_t *cpath) {
  116. crypt_path_t *victim, *head=cpath;
  117. if(!cpath)
  118. return;
  119. /* it's a doubly linked list, so we have to notice when we've
  120. * gone through it once. */
  121. while(cpath->next && cpath->next != head) {
  122. victim = cpath;
  123. cpath = victim->next;
  124. circuit_free_cpath_node(victim);
  125. }
  126. circuit_free_cpath_node(cpath);
  127. }
  128. static void circuit_free_cpath_node(crypt_path_t *victim) {
  129. if(victim->f_crypto)
  130. crypto_free_cipher_env(victim->f_crypto);
  131. if(victim->b_crypto)
  132. crypto_free_cipher_env(victim->b_crypto);
  133. if(victim->f_digest)
  134. crypto_free_digest_env(victim->f_digest);
  135. if(victim->b_digest)
  136. crypto_free_digest_env(victim->b_digest);
  137. if(victim->handshake_state)
  138. crypto_dh_free(victim->handshake_state);
  139. free(victim);
  140. }
  141. /* return 0 if can't get a unique circ_id. */
  142. static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
  143. uint16_t test_circ_id;
  144. int attempts=0;
  145. uint16_t high_bit;
  146. assert(conn && conn->type == CONN_TYPE_OR);
  147. high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
  148. do {
  149. /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
  150. * circID such that (high_bit|test_circ_id) is not already used. */
  151. test_circ_id = conn->next_circ_id++;
  152. if (test_circ_id == 0 || test_circ_id >= 1<<15) {
  153. test_circ_id = 1;
  154. conn->next_circ_id = 2;
  155. }
  156. if(++attempts > 1<<15) {
  157. /* Make sure we don't loop forever if all circ_id's are used. This
  158. * matters because it's an external DoS vulnerability.
  159. */
  160. log_fn(LOG_WARN,"No unused circ IDs. Failing.");
  161. return 0;
  162. }
  163. test_circ_id |= high_bit;
  164. } while(circuit_get_by_circ_id_conn(test_circ_id, conn));
  165. return test_circ_id;
  166. }
  167. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
  168. circuit_t *circ;
  169. connection_t *tmpconn;
  170. for(circ=global_circuitlist;circ;circ = circ->next) {
  171. if (circ->marked_for_close)
  172. continue;
  173. if(circ->p_circ_id == circ_id) {
  174. if(circ->p_conn == conn)
  175. return circ;
  176. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  177. if(tmpconn == conn)
  178. return circ;
  179. }
  180. }
  181. if(circ->n_circ_id == circ_id) {
  182. if(circ->n_conn == conn)
  183. return circ;
  184. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  185. if(tmpconn == conn)
  186. return circ;
  187. }
  188. }
  189. }
  190. return NULL;
  191. }
  192. circuit_t *circuit_get_by_conn(connection_t *conn) {
  193. circuit_t *circ;
  194. connection_t *tmpconn;
  195. for(circ=global_circuitlist;circ;circ = circ->next) {
  196. if (circ->marked_for_close)
  197. continue;
  198. if(circ->p_conn == conn)
  199. return circ;
  200. if(circ->n_conn == conn)
  201. return circ;
  202. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  203. if(tmpconn == conn)
  204. return circ;
  205. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  206. if(tmpconn == conn)
  207. return circ;
  208. }
  209. return NULL;
  210. }
  211. /* Return 1 iff 'c' could be returned by circuit_get_best.
  212. */
  213. static int circuit_is_acceptable(circuit_t *circ,
  214. connection_t *conn,
  215. int must_be_open,
  216. uint8_t purpose,
  217. time_t now)
  218. {
  219. routerinfo_t *exitrouter;
  220. if (!CIRCUIT_IS_ORIGIN(circ))
  221. return 0; /* this circ doesn't start at us */
  222. if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  223. return 0; /* ignore non-open circs */
  224. if (circ->marked_for_close)
  225. return 0;
  226. /* if this circ isn't our purpose, skip. */
  227. if(purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
  228. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  229. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  230. circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
  231. return 0;
  232. } else {
  233. if(purpose != circ->purpose)
  234. return 0;
  235. }
  236. if(purpose == CIRCUIT_PURPOSE_C_GENERAL)
  237. if(circ->timestamp_dirty &&
  238. circ->timestamp_dirty+options.NewCircuitPeriod < now)
  239. return 0;
  240. if(conn) {
  241. /* decide if this circ is suitable for this conn */
  242. // if(circ->state == CIRCUIT_STATE_OPEN && circ->n_conn) /* open */
  243. // exitrouter = router_get_by_addr_port(circ->cpath->prev->addr,
  244. // circ->cpath->prev->port);
  245. // else /* not open */
  246. /* for rend circs, circ->cpath->prev is not the last router in the
  247. * circuit, it's the magical extra bob hop. so just check the nickname
  248. * of the one we meant to finish at.
  249. */
  250. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  251. if(!exitrouter) {
  252. log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
  253. return 0; /* this circuit is screwed and doesn't know it yet */
  254. }
  255. if(purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  256. if(connection_ap_can_use_exit(conn, exitrouter) == ADDR_POLICY_REJECTED) {
  257. /* can't exit from this router */
  258. return 0;
  259. }
  260. } else { /* not general */
  261. if(rend_cmp_service_ids(conn->rend_query, circ->rend_query)) {
  262. /* this circ is not for this conn */
  263. return 0;
  264. }
  265. }
  266. }
  267. return 1;
  268. }
  269. /* Return 1 iff circuit 'a' is better than circuit 'b' for purpose. Used by
  270. * circuit_get_best
  271. */
  272. static int circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
  273. {
  274. switch(purpose) {
  275. case CIRCUIT_PURPOSE_C_GENERAL:
  276. /* if it's used but less dirty it's best;
  277. * else if it's more recently created it's best
  278. */
  279. if(b->timestamp_dirty) {
  280. if(a->timestamp_dirty &&
  281. a->timestamp_dirty > b->timestamp_dirty)
  282. return 1;
  283. } else {
  284. if(a->timestamp_dirty ||
  285. a->timestamp_created > b->timestamp_created)
  286. return 1;
  287. }
  288. break;
  289. case CIRCUIT_PURPOSE_C_INTRODUCING:
  290. /* more recently created is best */
  291. if(a->timestamp_created > b->timestamp_created)
  292. return 1;
  293. break;
  294. case CIRCUIT_PURPOSE_C_REND_JOINED:
  295. /* the closer it is to rend_joined the better it is */
  296. if(a->purpose > b->purpose)
  297. return 1;
  298. break;
  299. }
  300. return 0;
  301. }
  302. /* Find the best circ that conn can use, preferably one which is
  303. * dirty. Circ must not be too old.
  304. * If !conn, return newest.
  305. *
  306. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  307. *
  308. * circ_purpose specifies what sort of circuit we must have.
  309. * It can be C_GENERAL, C_INTRODUCING, or C_REND_JOINED.
  310. *
  311. * If it's REND_JOINED and must_be_open==0, then return the closest
  312. * rendezvous-purposed circuit that you can find.
  313. *
  314. * If circ_purpose is not GENERAL, then conn must be defined.
  315. */
  316. circuit_t *circuit_get_best(connection_t *conn,
  317. int must_be_open, uint8_t purpose) {
  318. circuit_t *circ, *best=NULL;
  319. time_t now = time(NULL);
  320. assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  321. purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  322. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  323. for (circ=global_circuitlist;circ;circ = circ->next) {
  324. if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,now))
  325. continue;
  326. /* now this is an acceptable circ to hand back. but that doesn't
  327. * mean it's the *best* circ to hand back. try to decide.
  328. */
  329. if(!best || circuit_is_better(circ,best,purpose))
  330. best = circ;
  331. }
  332. return best;
  333. }
  334. /* Return the first circuit in global_circuitlist after 'start' whose
  335. * rend_pk_digest field is 'digest' and whose purpose is purpose. Returns
  336. * NULL if no circuit is found. If 'start' is null, begin at the start of
  337. * the list.
  338. */
  339. circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
  340. const char *digest, uint8_t purpose)
  341. {
  342. circuit_t *circ;
  343. if (start == NULL)
  344. circ = global_circuitlist;
  345. else
  346. circ = start->next;
  347. for( ; circ; circ = circ->next) {
  348. if (circ->marked_for_close)
  349. continue;
  350. if (circ->purpose != purpose)
  351. continue;
  352. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  353. return circ;
  354. }
  355. return NULL;
  356. }
  357. /* Return the circuit waiting for a rendezvous with the provided cookie.
  358. * Return NULL if no such circuit is found.
  359. */
  360. circuit_t *circuit_get_rendezvous(const char *cookie)
  361. {
  362. circuit_t *circ;
  363. for (circ = global_circuitlist; circ; circ = circ->next) {
  364. if (! circ->marked_for_close &&
  365. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  366. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  367. return circ;
  368. }
  369. return NULL;
  370. }
  371. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 20
  372. /* circuits that were born at the end of their second might be expired
  373. * after 10.1 seconds; circuits born at the beginning might be expired
  374. * after closer to 11 seconds.
  375. */
  376. /* close all circuits that start at us, aren't open, and were born
  377. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago */
  378. void circuit_expire_building(void) {
  379. int now = time(NULL);
  380. circuit_t *victim, *circ = global_circuitlist;
  381. while(circ) {
  382. victim = circ;
  383. circ = circ->next;
  384. if(!CIRCUIT_IS_ORIGIN(victim))
  385. continue; /* didn't originate here */
  386. if(victim->marked_for_close)
  387. continue; /* don't mess with marked circs */
  388. if(victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)
  389. continue; /* it's young still, don't mess with it */
  390. /* if circ is !open, or if it's open but purpose is est intro or est rend,
  391. * then mark it for close */
  392. if(victim->state != CIRCUIT_STATE_OPEN ||
  393. victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  394. victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO) {
  395. if(victim->n_conn)
  396. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s)",
  397. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  398. victim->state, circuit_state_to_string[victim->state]);
  399. else
  400. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s)", victim->n_circ_id,
  401. victim->state, circuit_state_to_string[victim->state]);
  402. circuit_log_path(LOG_INFO,victim);
  403. circuit_mark_for_close(victim);
  404. }
  405. }
  406. }
  407. /* count the number of circs starting at us that aren't open */
  408. int circuit_count_building(void) {
  409. circuit_t *circ;
  410. int num=0;
  411. for(circ=global_circuitlist;circ;circ = circ->next) {
  412. if(CIRCUIT_IS_ORIGIN(circ)
  413. && circ->state != CIRCUIT_STATE_OPEN
  414. && !circ->marked_for_close)
  415. num++;
  416. }
  417. return num;
  418. }
  419. #define MIN_CIRCUITS_HANDLING_STREAM 2
  420. /* return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open
  421. * general-purpose circuits will have an acceptable exit node for
  422. * conn. Else return 0.
  423. */
  424. int circuit_stream_is_being_handled(connection_t *conn) {
  425. circuit_t *circ;
  426. routerinfo_t *exitrouter;
  427. int num=0;
  428. time_t now = time(NULL);
  429. for(circ=global_circuitlist;circ;circ = circ->next) {
  430. if(CIRCUIT_IS_ORIGIN(circ) && circ->state != CIRCUIT_STATE_OPEN &&
  431. !circ->marked_for_close && circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  432. (!circ->timestamp_dirty ||
  433. circ->timestamp_dirty + options.NewCircuitPeriod < now)) {
  434. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  435. if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) != ADDR_POLICY_REJECTED)
  436. if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
  437. return 1;
  438. }
  439. }
  440. return 0;
  441. }
  442. /* update digest from the payload of cell. assign integrity part to cell. */
  443. static void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
  444. char integrity[4];
  445. relay_header_t rh;
  446. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  447. crypto_digest_get_digest(digest, integrity, 4);
  448. // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  449. // integrity[0], integrity[1], integrity[2], integrity[3]);
  450. relay_header_unpack(&rh, cell->payload);
  451. memcpy(rh.integrity, integrity, 4);
  452. relay_header_pack(cell->payload, &rh);
  453. }
  454. /* update digest from the payload of cell (with the integrity part set
  455. * to 0). If the integrity part is valid return 1, else restore digest
  456. * and cell to their original state and return 0.
  457. */
  458. static int relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell) {
  459. char received_integrity[4], calculated_integrity[4];
  460. relay_header_t rh;
  461. crypto_digest_env_t *backup_digest=NULL;
  462. backup_digest = crypto_digest_dup(digest);
  463. relay_header_unpack(&rh, cell->payload);
  464. memcpy(received_integrity, rh.integrity, 4);
  465. memset(rh.integrity, 0, 4);
  466. relay_header_pack(cell->payload, &rh);
  467. // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  468. // received_integrity[0], received_integrity[1],
  469. // received_integrity[2], received_integrity[3]);
  470. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  471. crypto_digest_get_digest(digest, calculated_integrity, 4);
  472. if(memcmp(received_integrity, calculated_integrity, 4)) {
  473. // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  474. // (%d vs %d).", received_integrity, calculated_integrity);
  475. /* restore digest to its old form */
  476. crypto_digest_assign(digest, backup_digest);
  477. /* restore the relay header */
  478. memcpy(rh.integrity, received_integrity, 4);
  479. relay_header_pack(cell->payload, &rh);
  480. crypto_free_digest_env(backup_digest);
  481. return 0;
  482. }
  483. crypto_free_digest_env(backup_digest);
  484. return 1;
  485. }
  486. static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
  487. int encrypt_mode) {
  488. char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */
  489. relay_header_t rh;
  490. relay_header_unpack(&rh, in);
  491. // log_fn(LOG_DEBUG,"before crypt: %d",rh.recognized);
  492. if(( encrypt_mode && crypto_cipher_encrypt(cipher, in, CELL_PAYLOAD_SIZE, out)) ||
  493. (!encrypt_mode && crypto_cipher_decrypt(cipher, in, CELL_PAYLOAD_SIZE, out))) {
  494. log_fn(LOG_WARN,"Error during crypt: %s", crypto_perror());
  495. return -1;
  496. }
  497. memcpy(in,out,CELL_PAYLOAD_SIZE);
  498. relay_header_unpack(&rh, in);
  499. // log_fn(LOG_DEBUG,"after crypt: %d",rh.recognized);
  500. return 0;
  501. }
  502. /*
  503. receive a relay cell:
  504. - crypt it (encrypt APward, decrypt at AP, decrypt exitward)
  505. - check if recognized (if exitward)
  506. - if recognized, check digest, find right conn, deliver to edge.
  507. - else connection_or_write_cell_to_buf to the right conn
  508. */
  509. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  510. int cell_direction) {
  511. connection_t *conn=NULL;
  512. crypt_path_t *layer_hint=NULL;
  513. char recognized=0;
  514. assert(cell && circ);
  515. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  516. if (circ->marked_for_close)
  517. return 0;
  518. if(relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
  519. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  520. return -1;
  521. }
  522. if(recognized) {
  523. conn = relay_lookup_conn(circ, cell, cell_direction);
  524. if(cell_direction == CELL_DIRECTION_OUT) {
  525. ++stats_n_relay_cells_delivered;
  526. log_fn(LOG_DEBUG,"Sending to exit.");
  527. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL) < 0) {
  528. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at exit) failed.");
  529. return -1;
  530. }
  531. }
  532. if(cell_direction == CELL_DIRECTION_IN) {
  533. ++stats_n_relay_cells_delivered;
  534. log_fn(LOG_DEBUG,"Sending to AP.");
  535. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint) < 0) {
  536. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at AP) failed.");
  537. return -1;
  538. }
  539. }
  540. return 0;
  541. }
  542. /* not recognized. pass it on. */
  543. if(cell_direction == CELL_DIRECTION_OUT) {
  544. cell->circ_id = circ->n_circ_id; /* switch it */
  545. conn = circ->n_conn;
  546. } else {
  547. cell->circ_id = circ->p_circ_id; /* switch it */
  548. conn = circ->p_conn;
  549. }
  550. if(!conn) {
  551. if (circ->rend_splice && cell_direction == CELL_DIRECTION_OUT) {
  552. assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  553. assert(circ->rend_splice->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  554. cell->circ_id = circ->rend_splice->p_circ_id;
  555. if (circuit_receive_relay_cell(cell, circ->rend_splice, CELL_DIRECTION_IN)<0) {
  556. log_fn(LOG_WARN, "Error relaying cell across rendezvous; closing circuits");
  557. circuit_mark_for_close(circ); /* XXXX Do this here, or just return -1? */
  558. return -1;
  559. }
  560. return 0;
  561. }
  562. log_fn(LOG_WARN,"Didn't recognize cell, but circ stops here! Closing circ.");
  563. return -1;
  564. }
  565. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  566. ++stats_n_relay_cells_relayed;
  567. connection_or_write_cell_to_buf(cell, conn);
  568. return 0;
  569. }
  570. /* wrap this into receive_relay_cell one day */
  571. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  572. crypt_path_t **layer_hint, char *recognized) {
  573. crypt_path_t *thishop;
  574. relay_header_t rh;
  575. assert(circ && cell && recognized);
  576. assert(cell_direction == CELL_DIRECTION_IN || cell_direction == CELL_DIRECTION_OUT);
  577. if(cell_direction == CELL_DIRECTION_IN) {
  578. if(CIRCUIT_IS_ORIGIN(circ)) { /* we're at the beginning of the circuit.
  579. We'll want to do layered crypts. */
  580. assert(circ->cpath);
  581. thishop = circ->cpath;
  582. if(thishop->state != CPATH_STATE_OPEN) {
  583. log_fn(LOG_WARN,"Relay cell before first created cell? Closing.");
  584. return -1;
  585. }
  586. do { /* Remember: cpath is in forward order, that is, first hop first. */
  587. assert(thishop);
  588. if(relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
  589. return -1;
  590. relay_header_unpack(&rh, cell->payload);
  591. if(rh.recognized == 0) {
  592. /* it's possibly recognized. have to check digest to be sure. */
  593. if(relay_digest_matches(thishop->b_digest, cell)) {
  594. *recognized = 1;
  595. *layer_hint = thishop;
  596. return 0;
  597. }
  598. }
  599. thishop = thishop->next;
  600. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  601. log_fn(LOG_WARN,"in-cell at OP not recognized. Closing.");
  602. return -1;
  603. } else { /* we're in the middle. Just one crypt. */
  604. if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  605. return -1;
  606. // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  607. }
  608. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  609. /* we're in the middle. Just one crypt. */
  610. if(relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
  611. return -1;
  612. relay_header_unpack(&rh, cell->payload);
  613. if (rh.recognized == 0) {
  614. /* it's possibly recognized. have to check digest to be sure. */
  615. if(relay_digest_matches(circ->n_digest, cell)) {
  616. *recognized = 1;
  617. return 0;
  618. }
  619. }
  620. }
  621. return 0;
  622. }
  623. /*
  624. package a relay cell:
  625. 1) encrypt it to the right conn
  626. 2) connection_or_write_cell_to_buf to the right conn
  627. */
  628. int
  629. circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  630. int cell_direction,
  631. crypt_path_t *layer_hint)
  632. {
  633. connection_t *conn; /* where to send the cell */
  634. crypt_path_t *thishop; /* counter for repeated crypts */
  635. if(cell_direction == CELL_DIRECTION_OUT) {
  636. conn = circ->n_conn;
  637. if(!conn) {
  638. log_fn(LOG_WARN,"outgoing relay cell has n_conn==NULL. Dropping.");
  639. return 0; /* just drop it */
  640. }
  641. relay_set_digest(layer_hint->f_digest, cell);
  642. thishop = layer_hint;
  643. /* moving from farthest to nearest hop */
  644. do {
  645. assert(thishop);
  646. log_fn(LOG_DEBUG,"crypting a layer of the relay cell.");
  647. if(relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
  648. return -1;
  649. }
  650. thishop = thishop->prev;
  651. } while (thishop != circ->cpath->prev);
  652. } else { /* incoming cell */
  653. conn = circ->p_conn;
  654. if(!conn) {
  655. log_fn(LOG_WARN,"incoming relay cell has p_conn==NULL. Dropping.");
  656. return 0; /* just drop it */
  657. }
  658. relay_set_digest(circ->p_digest, cell);
  659. if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  660. return -1;
  661. }
  662. ++stats_n_relay_cells_relayed;
  663. connection_or_write_cell_to_buf(cell, conn);
  664. return 0;
  665. }
  666. static connection_t *
  667. relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
  668. {
  669. connection_t *tmpconn;
  670. relay_header_t rh;
  671. relay_header_unpack(&rh, cell->payload);
  672. if(!rh.stream_id)
  673. return NULL;
  674. /* IN or OUT cells could have come from either direction, now
  675. * that we allow rendezvous *to* an OP.
  676. */
  677. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  678. if(rh.stream_id == tmpconn->stream_id) {
  679. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  680. if(cell_direction == CELL_DIRECTION_OUT ||
  681. connection_edge_is_rendezvous_stream(tmpconn))
  682. return tmpconn;
  683. }
  684. }
  685. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  686. if(rh.stream_id == tmpconn->stream_id) {
  687. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  688. return tmpconn;
  689. }
  690. }
  691. return NULL; /* probably a begin relay cell */
  692. }
  693. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  694. connection_t *conn;
  695. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  696. log_fn(LOG_DEBUG,"resuming");
  697. if(edge_type == EDGE_EXIT)
  698. conn = circ->n_streams;
  699. else
  700. conn = circ->p_streams;
  701. for( ; conn; conn=conn->next_stream) {
  702. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  703. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  704. connection_start_reading(conn);
  705. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  706. /* If the circuit won't accept any more data, return without looking
  707. * at any more of the streams. Any connections that should be stopped
  708. * have already been stopped by connection_edge_package_raw_inbuf. */
  709. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  710. return;
  711. }
  712. }
  713. }
  714. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  715. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  716. connection_t *conn = NULL;
  717. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  718. assert(edge_type == EDGE_EXIT || layer_hint);
  719. log_fn(LOG_DEBUG,"considering");
  720. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  721. conn = circ->n_streams;
  722. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  723. conn = circ->p_streams;
  724. else
  725. return 0;
  726. for( ; conn; conn=conn->next_stream)
  727. if(!layer_hint || conn->cpath_layer == layer_hint)
  728. connection_stop_reading(conn);
  729. log_fn(LOG_DEBUG,"yes. stopped.");
  730. return 1;
  731. }
  732. void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  733. while((edge_type == EDGE_AP ? layer_hint->deliver_window : circ->deliver_window) <
  734. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  735. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  736. if(edge_type == EDGE_AP)
  737. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  738. else
  739. circ->deliver_window += CIRCWINDOW_INCREMENT;
  740. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  741. NULL, 0, layer_hint) < 0) {
  742. log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
  743. return; /* the circuit's closed, don't continue */
  744. }
  745. }
  746. }
  747. int _circuit_mark_for_close(circuit_t *circ) {
  748. connection_t *conn;
  749. assert_circuit_ok(circ);
  750. if (circ->marked_for_close < 0)
  751. return -1;
  752. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  753. onion_pending_remove(circ);
  754. }
  755. /* If the circuit ever became OPEN, we sent it to the reputation history
  756. * module then. If it isn't OPEN, we send it there now to remember which
  757. * links worked and which didn't.
  758. */
  759. if (circ->state != CIRCUIT_STATE_OPEN) {
  760. if(circ->cpath) //XXX
  761. circuit_build_failed(circ); /* take actions if necessary */
  762. circuit_rep_hist_note_result(circ);
  763. }
  764. if(circ->n_conn)
  765. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  766. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  767. connection_edge_destroy(circ->n_circ_id, conn);
  768. }
  769. if(circ->p_conn)
  770. connection_send_destroy(circ->n_circ_id, circ->p_conn);
  771. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  772. connection_edge_destroy(circ->p_circ_id, conn);
  773. }
  774. circ->marked_for_close = 1;
  775. if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
  776. /* do this after marking this circuit, to avoid infinite recursion. */
  777. circuit_mark_for_close(circ->rend_splice);
  778. circ->rend_splice = NULL;
  779. }
  780. return 0;
  781. }
  782. void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
  783. connection_t *prevconn;
  784. assert(circ);
  785. assert(conn);
  786. if(conn == circ->p_streams) {
  787. circ->p_streams = conn->next_stream;
  788. return;
  789. }
  790. if(conn == circ->n_streams) {
  791. circ->n_streams = conn->next_stream;
  792. return;
  793. }
  794. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  795. if(prevconn && prevconn->next_stream) {
  796. prevconn->next_stream = conn->next_stream;
  797. return;
  798. }
  799. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  800. if(prevconn && prevconn->next_stream) {
  801. prevconn->next_stream = conn->next_stream;
  802. return;
  803. }
  804. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  805. assert(0); /* should never get here */
  806. }
  807. void circuit_about_to_close_connection(connection_t *conn) {
  808. /* send destroys for all circuits using conn */
  809. /* currently, we assume it's too late to flush conn's buf here.
  810. * down the road, maybe we'll consider that eof doesn't mean can't-write
  811. */
  812. circuit_t *circ;
  813. switch(conn->type) {
  814. case CONN_TYPE_OR:
  815. /* We must close all the circuits on it. */
  816. while((circ = circuit_get_by_conn(conn))) {
  817. if(circ->n_conn == conn) /* it's closing in front of us */
  818. circ->n_conn = NULL;
  819. if(circ->p_conn == conn) /* it's closing behind us */
  820. circ->p_conn = NULL;
  821. circuit_mark_for_close(circ);
  822. }
  823. return;
  824. case CONN_TYPE_AP:
  825. case CONN_TYPE_EXIT:
  826. /* It's an edge conn. Need to remove it from the linked list of
  827. * conn's for this circuit. Confirm that 'end' relay command has
  828. * been sent. But don't kill the circuit.
  829. */
  830. circ = circuit_get_by_conn(conn);
  831. if(!circ)
  832. return;
  833. if(!conn->has_sent_end) {
  834. log_fn(LOG_WARN,"Edge connection hasn't sent end yet? Bug.");
  835. connection_mark_for_close(conn, END_STREAM_REASON_MISC);
  836. }
  837. circuit_detach_stream(circ, conn);
  838. } /* end switch */
  839. }
  840. void circuit_log_path(int severity, circuit_t *circ) {
  841. char buf[1024];
  842. char *s = buf;
  843. struct crypt_path_t *hop;
  844. char *states[] = {"closed", "waiting for keys", "open"};
  845. routerinfo_t *router;
  846. assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath);
  847. snprintf(s, sizeof(buf)-1, "circ (length %d, exit %s): ",
  848. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  849. hop=circ->cpath;
  850. do {
  851. s = buf + strlen(buf);
  852. router = router_get_by_addr_port(hop->addr,hop->port);
  853. if(router) {
  854. snprintf(s, sizeof(buf) - (s - buf), "%s(%s) ",
  855. router->nickname, states[hop->state]);
  856. } else {
  857. if(circ->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
  858. snprintf(s, sizeof(buf) - (s - buf), "(rendjoin hop)");
  859. } else {
  860. snprintf(s, sizeof(buf) - (s - buf), "UNKNOWN ");
  861. }
  862. }
  863. hop=hop->next;
  864. } while(hop!=circ->cpath);
  865. log_fn(severity,"%s",buf);
  866. }
  867. /* Tell the rep(utation)hist(ory) module about the status of the links
  868. * in circ. Hops that have become OPEN are marked as successfully
  869. * extended; the _first_ hop that isn't open (if any) is marked as
  870. * unable to extend.
  871. */
  872. static void
  873. circuit_rep_hist_note_result(circuit_t *circ)
  874. {
  875. struct crypt_path_t *hop;
  876. char *prev_nickname = NULL;
  877. routerinfo_t *router;
  878. hop = circ->cpath;
  879. if(!hop) {
  880. /* XXX
  881. * if !hop, then we're not the beginning of this circuit.
  882. * for now, just forget about it. later, we should remember when
  883. * extends-through-us failed, too.
  884. */
  885. return;
  886. }
  887. if (options.ORPort) {
  888. prev_nickname = options.Nickname;
  889. }
  890. do {
  891. router = router_get_by_addr_port(hop->addr,hop->port);
  892. if (router) {
  893. if (prev_nickname) {
  894. if (hop->state == CPATH_STATE_OPEN)
  895. rep_hist_note_extend_succeeded(prev_nickname, router->nickname);
  896. else {
  897. rep_hist_note_extend_failed(prev_nickname, router->nickname);
  898. break;
  899. }
  900. }
  901. prev_nickname = router->nickname;
  902. } else {
  903. prev_nickname = NULL;
  904. }
  905. hop=hop->next;
  906. } while (hop!=circ->cpath);
  907. }
  908. static void
  909. circuit_dump_details(int severity, circuit_t *circ, int poll_index,
  910. char *type, int this_circid, int other_circid) {
  911. struct crypt_path_t *hop;
  912. log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
  913. poll_index, type, this_circid, other_circid, circ->state,
  914. circuit_state_to_string[circ->state], (int)circ->timestamp_created);
  915. if(CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  916. if(circ->state == CIRCUIT_STATE_BUILDING)
  917. log(severity,"Building: desired len %d, planned exit node %s.",
  918. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  919. for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
  920. log(severity,"hop: state %d, addr 0x%.8x, port %d", hop->state,
  921. (unsigned int)hop->addr,
  922. (int)hop->port);
  923. }
  924. }
  925. void circuit_dump_by_conn(connection_t *conn, int severity) {
  926. circuit_t *circ;
  927. connection_t *tmpconn;
  928. for(circ=global_circuitlist;circ;circ = circ->next) {
  929. if(circ->p_conn == conn)
  930. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  931. circ->p_circ_id, circ->n_circ_id);
  932. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  933. if(tmpconn == conn) {
  934. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  935. circ->p_circ_id, circ->n_circ_id);
  936. }
  937. }
  938. if(circ->n_conn == conn)
  939. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  940. circ->n_circ_id, circ->p_circ_id);
  941. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  942. if(tmpconn == conn) {
  943. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  944. circ->n_circ_id, circ->p_circ_id);
  945. }
  946. }
  947. }
  948. }
  949. /* Don't keep more than 10 unused open circuits around. */
  950. #define MAX_UNUSED_OPEN_CIRCUITS 10
  951. void circuit_expire_unused_circuits(void) {
  952. circuit_t *circ;
  953. time_t now = time(NULL);
  954. smartlist_t *unused_open_circs;
  955. int i;
  956. unused_open_circs = smartlist_create();
  957. for (circ = global_circuitlist; circ; circ = circ->next) {
  958. if (circ->marked_for_close)
  959. continue;
  960. /* If the circuit has been dirty for too long, and there are no streams
  961. * on it, mark it for close.
  962. */
  963. if (circ->timestamp_dirty &&
  964. circ->timestamp_dirty + options.NewCircuitPeriod < now &&
  965. !circ->p_conn &&
  966. !circ->p_streams) {
  967. log_fn(LOG_DEBUG,"Closing n_circ_id %d",circ->n_circ_id);
  968. circuit_mark_for_close(circ);
  969. } else if (!circ->timestamp_dirty && CIRCUIT_IS_ORIGIN(circ) &&
  970. circ->state == CIRCUIT_STATE_OPEN) {
  971. /* Also, gather a list of open unused circuits that we created.
  972. * Because we add elements to the front of global_circuitlist,
  973. * the last elements of unused_open_circs will be the oldest
  974. * ones.
  975. */
  976. smartlist_add(unused_open_circs, circ);
  977. }
  978. }
  979. for (i = MAX_UNUSED_OPEN_CIRCUITS; i < smartlist_len(unused_open_circs); ++i) {
  980. circuit_t *circ = smartlist_get(unused_open_circs, i);
  981. circuit_mark_for_close(circ);
  982. }
  983. smartlist_free(unused_open_circs);
  984. }
  985. static void circuit_is_open(circuit_t *circ) {
  986. switch(circ->purpose) {
  987. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  988. rend_client_rendcirc_is_open(circ);
  989. break;
  990. case CIRCUIT_PURPOSE_C_INTRODUCING:
  991. rend_client_introcirc_is_open(circ);
  992. break;
  993. case CIRCUIT_PURPOSE_C_GENERAL:
  994. /* Tell any AP connections that have been waiting for a new
  995. * circuit that one is ready. */
  996. connection_ap_attach_pending();
  997. break;
  998. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  999. /* at Bob, waiting for introductions */
  1000. rend_service_intro_is_ready(circ);
  1001. break;
  1002. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1003. /* at Bob, connecting to rend point */
  1004. rend_service_rendezvous_is_ready(circ);
  1005. break;
  1006. default:
  1007. log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
  1008. assert(0);
  1009. }
  1010. }
  1011. /* Called whenever a circuit could not be successfully built.
  1012. */
  1013. static void circuit_build_failed(circuit_t *circ) {
  1014. /* we should examine circ and see if it failed because of
  1015. * the last hop or an earlier hop. then use this info below.
  1016. */
  1017. //int failed_at_last_hop;
  1018. switch(circ->purpose) {
  1019. case CIRCUIT_PURPOSE_C_GENERAL:
  1020. if (circ->state != CIRCUIT_STATE_OPEN) {
  1021. /* If we never built the circuit, note it as a failure. */
  1022. /* Note that we can't just check circ->cpath here, because if
  1023. * circuit-building failed immediately, it won't be set yet. */
  1024. circuit_increment_failure_count();
  1025. }
  1026. break;
  1027. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1028. /* at Bob, waiting for introductions */
  1029. if (circ->state != CIRCUIT_STATE_OPEN) {
  1030. circuit_increment_failure_count();
  1031. }
  1032. /* no need to care here, because bob will rebuild intro
  1033. * points periodically. */
  1034. break;
  1035. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1036. /* at Alice, connecting to intro point */
  1037. /* Don't increment failure count, since Bob may have picked
  1038. * the introduction point maliciously */
  1039. /* Alice will pick a new intro point when this one dies, if
  1040. * the stream in question still cares. No need to act here. */
  1041. break;
  1042. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1043. /* at Alice, waiting for Bob */
  1044. if (circ->state != CIRCUIT_STATE_OPEN) {
  1045. circuit_increment_failure_count();
  1046. }
  1047. /* Alice will pick a new rend point when this one dies, if
  1048. * the stream in question still cares. No need to act here. */
  1049. break;
  1050. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1051. /* at Bob, connecting to rend point */
  1052. /* Don't increment failure count, since Alice may have picked
  1053. * the rendezvous point maliciously */
  1054. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s. Sucks to be Alice.", circ->build_state->chosen_exit);
  1055. break;
  1056. default:
  1057. /* Other cases are impossible, since this function is only called with
  1058. * unbuilt circuits. */
  1059. assert(0);
  1060. }
  1061. }
  1062. /* Number of consecutive failures so far; should only be touched by
  1063. * circuit_launch_new and circuit_*_failure_count.
  1064. */
  1065. static int n_circuit_failures = 0;
  1066. /* Don't retry launching a new circuit if we try this many times with no
  1067. * success. */
  1068. #define MAX_CIRCUIT_FAILURES 5
  1069. /* Launch a new circuit and return a pointer to it. Return NULL if you failed. */
  1070. circuit_t *circuit_launch_new(uint8_t purpose, const char *exit_nickname) {
  1071. if (n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  1072. /* too many failed circs in a row. don't try. */
  1073. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  1074. return NULL;
  1075. }
  1076. /* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
  1077. return circuit_establish_circuit(purpose, exit_nickname);
  1078. }
  1079. void circuit_increment_failure_count(void) {
  1080. ++n_circuit_failures;
  1081. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  1082. }
  1083. void circuit_reset_failure_count(void) {
  1084. n_circuit_failures = 0;
  1085. }
  1086. static circuit_t *circuit_establish_circuit(uint8_t purpose,
  1087. const char *exit_nickname) {
  1088. routerinfo_t *firsthop;
  1089. connection_t *n_conn;
  1090. circuit_t *circ;
  1091. circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
  1092. circ->state = CIRCUIT_STATE_OR_WAIT;
  1093. circ->build_state = onion_new_cpath_build_state(purpose, exit_nickname);
  1094. circ->purpose = purpose;
  1095. if (! circ->build_state) {
  1096. log_fn(LOG_INFO,"Generating cpath failed.");
  1097. circuit_mark_for_close(circ);
  1098. return NULL;
  1099. }
  1100. onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
  1101. if(!CIRCUIT_IS_ORIGIN(circ)) {
  1102. log_fn(LOG_INFO,"Generating first cpath hop failed.");
  1103. circuit_mark_for_close(circ);
  1104. return NULL;
  1105. }
  1106. /* now see if we're already connected to the first OR in 'route' */
  1107. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  1108. firsthop->address,firsthop->or_port);
  1109. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  1110. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  1111. circ->n_addr = firsthop->addr;
  1112. circ->n_port = firsthop->or_port;
  1113. if(!n_conn) { /* launch the connection */
  1114. n_conn = connection_or_connect(firsthop);
  1115. if(!n_conn) { /* connect failed, forget the whole thing */
  1116. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  1117. circuit_mark_for_close(circ);
  1118. return NULL;
  1119. }
  1120. }
  1121. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  1122. /* return success. The onion/circuit/etc will be taken care of automatically
  1123. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  1124. */
  1125. return circ;
  1126. } else { /* it (or a twin) is already open. use it. */
  1127. circ->n_addr = n_conn->addr;
  1128. circ->n_port = n_conn->port;
  1129. circ->n_conn = n_conn;
  1130. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  1131. if(circuit_send_next_onion_skin(circ) < 0) {
  1132. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  1133. circuit_mark_for_close(circ);
  1134. return NULL;
  1135. }
  1136. }
  1137. return circ;
  1138. }
  1139. /* find circuits that are waiting on me, if any, and get them to send the onion */
  1140. void circuit_n_conn_open(connection_t *or_conn) {
  1141. circuit_t *circ;
  1142. for(circ=global_circuitlist;circ;circ = circ->next) {
  1143. if (circ->marked_for_close)
  1144. continue;
  1145. if(CIRCUIT_IS_ORIGIN(circ) && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
  1146. assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  1147. log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
  1148. circ->n_conn = or_conn;
  1149. if(circuit_send_next_onion_skin(circ) < 0) {
  1150. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  1151. circuit_mark_for_close(circ);
  1152. continue;
  1153. /* XXX could this be bad, eg if next_onion_skin failed because conn died? */
  1154. }
  1155. }
  1156. }
  1157. }
  1158. extern int has_completed_circuit;
  1159. int circuit_send_next_onion_skin(circuit_t *circ) {
  1160. cell_t cell;
  1161. crypt_path_t *hop;
  1162. routerinfo_t *router;
  1163. int r;
  1164. int circ_id_type;
  1165. char payload[2+4+ONIONSKIN_CHALLENGE_LEN];
  1166. assert(circ && CIRCUIT_IS_ORIGIN(circ));
  1167. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  1168. assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
  1169. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  1170. circ_id_type = decide_circ_id_type(options.Nickname,
  1171. circ->n_conn->nickname);
  1172. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  1173. memset(&cell, 0, sizeof(cell_t));
  1174. cell.command = CELL_CREATE;
  1175. cell.circ_id = circ->n_circ_id;
  1176. if(onion_skin_create(circ->n_conn->onion_pkey,
  1177. &(circ->cpath->handshake_state),
  1178. cell.payload) < 0) {
  1179. log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
  1180. return -1;
  1181. }
  1182. connection_or_write_cell_to_buf(&cell, circ->n_conn);
  1183. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  1184. circ->state = CIRCUIT_STATE_BUILDING;
  1185. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  1186. } else {
  1187. assert(circ->cpath->state == CPATH_STATE_OPEN);
  1188. assert(circ->state == CIRCUIT_STATE_BUILDING);
  1189. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  1190. r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
  1191. if (r==1) {
  1192. /* done building the circuit. whew. */
  1193. circ->state = CIRCUIT_STATE_OPEN;
  1194. log_fn(LOG_INFO,"circuit built!");
  1195. circuit_reset_failure_count();
  1196. if(!has_completed_circuit) {
  1197. has_completed_circuit=1;
  1198. log_fn(LOG_NOTICE,"Tor has successfully opened a circuit. Looks like it's working.");
  1199. }
  1200. circuit_rep_hist_note_result(circ);
  1201. circuit_is_open(circ); /* do other actions as necessary */
  1202. return 0;
  1203. } else if (r<0) {
  1204. log_fn(LOG_INFO,"Unable to extend circuit path.");
  1205. return -1;
  1206. }
  1207. hop = circ->cpath->prev;
  1208. *(uint32_t*)payload = htonl(hop->addr);
  1209. *(uint16_t*)(payload+4) = htons(hop->port);
  1210. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), payload+6) < 0) {
  1211. log_fn(LOG_WARN,"onion_skin_create failed.");
  1212. return -1;
  1213. }
  1214. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  1215. /* send it to hop->prev, because it will transfer
  1216. * it to a create cell and then send to hop */
  1217. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTEND,
  1218. payload, sizeof(payload), hop->prev) < 0)
  1219. return 0; /* circuit is closed */
  1220. hop->state = CPATH_STATE_AWAITING_KEYS;
  1221. }
  1222. return 0;
  1223. }
  1224. /* take the 'extend' cell, pull out addr/port plus the onion skin. Make
  1225. * sure we're connected to the next hop, and pass it the onion skin in
  1226. * a create cell.
  1227. */
  1228. int circuit_extend(cell_t *cell, circuit_t *circ) {
  1229. connection_t *n_conn;
  1230. int circ_id_type;
  1231. cell_t newcell;
  1232. if(circ->n_conn) {
  1233. log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
  1234. return -1;
  1235. }
  1236. circ->n_addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
  1237. circ->n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
  1238. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  1239. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  1240. /* I've disabled making connections through OPs, but it's definitely
  1241. * possible here. I'm not sure if it would be a bug or a feature.
  1242. *
  1243. * Note also that this will close circuits where the onion has the same
  1244. * router twice in a row in the path. I think that's ok.
  1245. */
  1246. struct in_addr in;
  1247. in.s_addr = htonl(circ->n_addr);
  1248. log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  1249. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  1250. NULL, 0, NULL);
  1251. return 0;
  1252. }
  1253. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  1254. circ->n_port = n_conn->port;
  1255. circ->n_conn = n_conn;
  1256. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  1257. circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
  1258. // log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
  1259. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  1260. if(!circ->n_circ_id) {
  1261. log_fn(LOG_WARN,"failed to get unique circID.");
  1262. return -1;
  1263. }
  1264. log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
  1265. memset(&newcell, 0, sizeof(cell_t));
  1266. newcell.command = CELL_CREATE;
  1267. newcell.circ_id = circ->n_circ_id;
  1268. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+2+4,
  1269. ONIONSKIN_CHALLENGE_LEN);
  1270. connection_or_write_cell_to_buf(&newcell, circ->n_conn);
  1271. return 0;
  1272. }
  1273. /* Initialize cpath->{f|b}_{crypto|digest} from the key material in
  1274. * key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
  1275. * used as follows:
  1276. * 20 to initialize f_digest
  1277. * 20 to initialize b_digest
  1278. * 16 to key f_crypto
  1279. * 16 to key b_crypto
  1280. *
  1281. * (If 'reverse' is true, then f_XX and b_XX are swapped.)
  1282. */
  1283. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse)
  1284. {
  1285. unsigned char iv[CIPHER_IV_LEN];
  1286. crypto_digest_env_t *tmp_digest;
  1287. crypto_cipher_env_t *tmp_crypto;
  1288. assert(cpath && key_data);
  1289. assert(!(cpath->f_crypto || cpath->b_crypto ||
  1290. cpath->f_digest || cpath->b_digest));
  1291. memset(iv, 0, CIPHER_IV_LEN);
  1292. log_fn(LOG_DEBUG,"hop init digest forward 0x%.8x, backward 0x%.8x.",
  1293. (unsigned int)*(uint32_t*)key_data, (unsigned int)*(uint32_t*)(key_data+20));
  1294. cpath->f_digest = crypto_new_digest_env();
  1295. crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
  1296. cpath->b_digest = crypto_new_digest_env();
  1297. crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
  1298. log_fn(LOG_DEBUG,"hop init cipher forward 0x%.8x, backward 0x%.8x.",
  1299. (unsigned int)*(uint32_t*)(key_data+40), (unsigned int)*(uint32_t*)(key_data+40+16));
  1300. if (!(cpath->f_crypto =
  1301. crypto_create_init_cipher(key_data+(2*DIGEST_LEN),iv,1))) {
  1302. log(LOG_WARN,"forward cipher initialization failed.");
  1303. return -1;
  1304. }
  1305. if (!(cpath->b_crypto =
  1306. crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,iv,0))) {
  1307. log(LOG_WARN,"backward cipher initialization failed.");
  1308. return -1;
  1309. }
  1310. if (reverse) {
  1311. tmp_digest = cpath->f_digest;
  1312. cpath->f_digest = cpath->b_digest;
  1313. cpath->b_digest = tmp_digest;
  1314. tmp_crypto = cpath->f_crypto;
  1315. cpath->f_crypto = cpath->b_crypto;
  1316. cpath->b_crypto = tmp_crypto;
  1317. }
  1318. return 0;
  1319. }
  1320. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  1321. unsigned char keys[CPATH_KEY_MATERIAL_LEN];
  1322. crypt_path_t *hop;
  1323. assert(CIRCUIT_IS_ORIGIN(circ));
  1324. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  1325. hop = circ->cpath;
  1326. else {
  1327. for(hop=circ->cpath->next;
  1328. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  1329. hop=hop->next) ;
  1330. if(hop == circ->cpath) { /* got an extended when we're all done? */
  1331. log_fn(LOG_WARN,"got extended when circ already built? Closing.");
  1332. return -1;
  1333. }
  1334. }
  1335. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  1336. if(onion_skin_client_handshake(hop->handshake_state, reply, keys,
  1337. DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
  1338. log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
  1339. return -1;
  1340. }
  1341. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  1342. hop->handshake_state = NULL;
  1343. /* Remember hash of g^xy */
  1344. memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
  1345. if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
  1346. return -1;
  1347. }
  1348. hop->state = CPATH_STATE_OPEN;
  1349. log_fn(LOG_INFO,"finished");
  1350. circuit_log_path(LOG_INFO,circ);
  1351. return 0;
  1352. }
  1353. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  1354. crypt_path_t *victim;
  1355. connection_t *stream;
  1356. assert(circ && CIRCUIT_IS_ORIGIN(circ));
  1357. assert(layer);
  1358. /* XXX Since we don't ask for truncates currently, getting a truncated
  1359. * means that a connection broke or an extend failed. For now,
  1360. * just give up.
  1361. */
  1362. circuit_mark_for_close(circ);
  1363. return 0;
  1364. while(layer->next != circ->cpath) {
  1365. /* we need to clear out layer->next */
  1366. victim = layer->next;
  1367. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  1368. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  1369. if(stream->cpath_layer == victim) {
  1370. log_fn(LOG_INFO, "Marking stream %d for close.", stream->stream_id);
  1371. /* no need to send 'end' relay cells,
  1372. * because the other side's already dead
  1373. */
  1374. connection_mark_for_close(stream,0);
  1375. }
  1376. }
  1377. layer->next = victim->next;
  1378. circuit_free_cpath_node(victim);
  1379. }
  1380. log_fn(LOG_INFO, "finished");
  1381. return 0;
  1382. }
  1383. void assert_cpath_layer_ok(const crypt_path_t *cp)
  1384. {
  1385. assert(cp->f_crypto);
  1386. assert(cp->b_crypto);
  1387. // assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1388. // assert(cp->port);
  1389. switch(cp->state)
  1390. {
  1391. case CPATH_STATE_CLOSED:
  1392. case CPATH_STATE_OPEN:
  1393. assert(!cp->handshake_state);
  1394. break;
  1395. case CPATH_STATE_AWAITING_KEYS:
  1396. assert(cp->handshake_state);
  1397. break;
  1398. default:
  1399. assert(0);
  1400. }
  1401. assert(cp->package_window >= 0);
  1402. assert(cp->deliver_window >= 0);
  1403. }
  1404. void assert_cpath_ok(const crypt_path_t *cp)
  1405. {
  1406. while(cp->prev)
  1407. cp = cp->prev;
  1408. while(cp->next) {
  1409. assert_cpath_layer_ok(cp);
  1410. /* layers must be in sequence of: "open* awaiting? closed*" */
  1411. if (cp->prev) {
  1412. if (cp->prev->state == CPATH_STATE_OPEN) {
  1413. assert(cp->state == CPATH_STATE_CLOSED ||
  1414. cp->state == CPATH_STATE_AWAITING_KEYS);
  1415. } else {
  1416. assert(cp->state == CPATH_STATE_CLOSED);
  1417. }
  1418. }
  1419. cp = cp->next;
  1420. }
  1421. }
  1422. void assert_circuit_ok(const circuit_t *c)
  1423. {
  1424. connection_t *conn;
  1425. assert(c);
  1426. assert(c->magic == CIRCUIT_MAGIC);
  1427. assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1428. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1429. if (c->n_conn)
  1430. assert(c->n_conn->type == CONN_TYPE_OR);
  1431. if (c->p_conn)
  1432. assert(c->p_conn->type == CONN_TYPE_OR);
  1433. for (conn = c->p_streams; conn; conn = conn->next_stream)
  1434. assert(conn->type == CONN_TYPE_AP);
  1435. for (conn = c->n_streams; conn; conn = conn->next_stream)
  1436. assert(conn->type == CONN_TYPE_EXIT);
  1437. assert(c->deliver_window >= 0);
  1438. assert(c->package_window >= 0);
  1439. if (c->state == CIRCUIT_STATE_OPEN) {
  1440. if (c->cpath) {
  1441. assert(CIRCUIT_IS_ORIGIN(c));
  1442. assert(!c->n_crypto);
  1443. assert(!c->p_crypto);
  1444. assert(!c->n_digest);
  1445. assert(!c->p_digest);
  1446. } else {
  1447. assert(!CIRCUIT_IS_ORIGIN(c));
  1448. assert(c->n_crypto);
  1449. assert(c->p_crypto);
  1450. assert(c->n_digest);
  1451. assert(c->p_digest);
  1452. }
  1453. }
  1454. if (c->cpath) {
  1455. //XXX assert_cpath_ok(c->cpath);
  1456. }
  1457. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1458. if (!c->marked_for_close) {
  1459. assert(c->rend_splice);
  1460. assert(c->rend_splice->rend_splice == c);
  1461. }
  1462. assert(c->rend_splice != c);
  1463. } else {
  1464. assert(!c->rend_splice);
  1465. }
  1466. }
  1467. /*
  1468. Local Variables:
  1469. mode:c
  1470. indent-tabs-mode:nil
  1471. c-basic-offset:2
  1472. End:
  1473. */