circuit.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /* Copyright 2001,2002 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 void circuit_free_cpath(crypt_path_t *cpath);
  7. static void circuit_free_cpath_node(crypt_path_t *victim);
  8. static aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  9. unsigned long stats_n_relay_cells_relayed = 0;
  10. unsigned long stats_n_relay_cells_delivered = 0;
  11. /********* START VARIABLES **********/
  12. static circuit_t *global_circuitlist=NULL;
  13. char *circuit_state_to_string[] = {
  14. "receiving the onion", /* 0 */
  15. "waiting to process create", /* 1 */
  16. "connecting to firsthop", /* 2 */
  17. "open" /* 3 */
  18. };
  19. /********* END VARIABLES ************/
  20. void circuit_add(circuit_t *circ) {
  21. if(!global_circuitlist) { /* first one */
  22. global_circuitlist = circ;
  23. circ->next = NULL;
  24. } else {
  25. circ->next = global_circuitlist;
  26. global_circuitlist = circ;
  27. }
  28. }
  29. void circuit_remove(circuit_t *circ) {
  30. circuit_t *tmpcirc;
  31. assert(circ && global_circuitlist);
  32. if(global_circuitlist == circ) {
  33. global_circuitlist = global_circuitlist->next;
  34. return;
  35. }
  36. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  37. if(tmpcirc->next == circ) {
  38. tmpcirc->next = circ->next;
  39. return;
  40. }
  41. }
  42. }
  43. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  44. circuit_t *circ;
  45. struct timeval now;
  46. my_gettimeofday(&now);
  47. circ = (circuit_t *)tor_malloc(sizeof(circuit_t));
  48. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  49. circ->timestamp_created = now.tv_sec;
  50. circ->p_aci = p_aci;
  51. circ->p_conn = p_conn;
  52. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  53. /* ACIs */
  54. circ->p_aci = p_aci;
  55. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  56. circ->package_window = CIRCWINDOW_START;
  57. circ->deliver_window = CIRCWINDOW_START;
  58. circuit_add(circ);
  59. return circ;
  60. }
  61. void circuit_free(circuit_t *circ) {
  62. if (circ->n_crypto)
  63. crypto_free_cipher_env(circ->n_crypto);
  64. if (circ->p_crypto)
  65. crypto_free_cipher_env(circ->p_crypto);
  66. circuit_free_cpath(circ->cpath);
  67. free(circ);
  68. }
  69. static void circuit_free_cpath(crypt_path_t *cpath) {
  70. crypt_path_t *victim, *head=cpath;
  71. if(!cpath)
  72. return;
  73. /* it's a doubly linked list, so we have to notice when we've
  74. * gone through it once. */
  75. while(cpath->next && cpath->next != head) {
  76. victim = cpath;
  77. cpath = victim->next;
  78. circuit_free_cpath_node(victim);
  79. }
  80. circuit_free_cpath_node(cpath);
  81. }
  82. static void circuit_free_cpath_node(crypt_path_t *victim) {
  83. if(victim->f_crypto)
  84. crypto_free_cipher_env(victim->f_crypto);
  85. if(victim->b_crypto)
  86. crypto_free_cipher_env(victim->b_crypto);
  87. if(victim->handshake_state)
  88. crypto_dh_free(victim->handshake_state);
  89. free(victim);
  90. }
  91. /* return 0 if can't get a unique aci. */
  92. static aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  93. aci_t test_aci;
  94. connection_t *conn;
  95. uint16_t high_bit;
  96. high_bit = (aci_type == ACI_TYPE_HIGHER) ? 1<<15 : 0;
  97. conn = connection_exact_get_by_addr_port(addr,port);
  98. /* XXX race condition: if conn is marked_for_close it won't be noticed */
  99. if (!conn)
  100. return (1|high_bit); /* No connection exists; conflict is impossible. */
  101. do {
  102. /* Sequentially iterate over test_aci=1...1<<15-1 until we find an
  103. * aci such that (high_bit|test_aci) is not already used. */
  104. /* XXX Will loop forever if all aci's in our range are used.
  105. * This matters because it's an external DoS vulnerability. */
  106. test_aci = conn->next_aci++;
  107. if (test_aci == 0 || test_aci >= 1<<15) {
  108. test_aci = 1;
  109. conn->next_aci = 2;
  110. }
  111. test_aci |= high_bit;
  112. } while(circuit_get_by_aci_conn(test_aci, conn));
  113. return test_aci;
  114. }
  115. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  116. if(!circ) /* use circ if it's defined, else start from the beginning */
  117. circ = global_circuitlist;
  118. else
  119. circ = circ->next;
  120. for( ; circ; circ = circ->next) {
  121. if(circ->n_addr == naddr && circ->n_port == nport)
  122. return circ;
  123. }
  124. return NULL;
  125. }
  126. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  127. circuit_t *circ;
  128. connection_t *tmpconn;
  129. for(circ=global_circuitlist;circ;circ = circ->next) {
  130. if(circ->p_aci == aci) {
  131. if(circ->p_conn == conn)
  132. return circ;
  133. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  134. if(tmpconn == conn)
  135. return circ;
  136. }
  137. }
  138. if(circ->n_aci == aci) {
  139. if(circ->n_conn == conn)
  140. return circ;
  141. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  142. if(tmpconn == conn)
  143. return circ;
  144. }
  145. }
  146. }
  147. return NULL;
  148. }
  149. circuit_t *circuit_get_by_conn(connection_t *conn) {
  150. circuit_t *circ;
  151. connection_t *tmpconn;
  152. for(circ=global_circuitlist;circ;circ = circ->next) {
  153. if(circ->p_conn == conn)
  154. return circ;
  155. if(circ->n_conn == conn)
  156. return circ;
  157. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  158. if(tmpconn == conn)
  159. return circ;
  160. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  161. if(tmpconn == conn)
  162. return circ;
  163. }
  164. return NULL;
  165. }
  166. circuit_t *circuit_get_newest_open(void) {
  167. circuit_t *circ, *bestcirc=NULL;
  168. for(circ=global_circuitlist;circ;circ = circ->next) {
  169. if(circ->cpath && circ->state == CIRCUIT_STATE_OPEN && circ->n_conn && (!bestcirc ||
  170. bestcirc->timestamp_created < circ->timestamp_created)) {
  171. log_fn(LOG_DEBUG,"Choosing circuit %s:%d:%d.", circ->n_conn->address, circ->n_port, circ->n_aci);
  172. assert(circ->n_aci);
  173. bestcirc = circ;
  174. }
  175. }
  176. return bestcirc;
  177. }
  178. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  179. int cell_direction, crypt_path_t *layer_hint) {
  180. connection_t *conn=NULL;
  181. char recognized=0;
  182. char buf[256];
  183. assert(cell && circ);
  184. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  185. buf[0] = cell->length;
  186. memcpy(buf+1, cell->payload, CELL_PAYLOAD_SIZE);
  187. log_fn(LOG_DEBUG,"direction %d, streamid %d before crypt.", cell_direction, *(int*)(cell->payload+1));
  188. if(relay_crypt(circ, buf, 1+CELL_PAYLOAD_SIZE, cell_direction, &layer_hint, &recognized, &conn) < 0) {
  189. log_fn(LOG_WARNING,"relay crypt failed. Dropping connection.");
  190. return -1;
  191. }
  192. cell->length = buf[0];
  193. memcpy(cell->payload, buf+1, CELL_PAYLOAD_SIZE);
  194. if(recognized) {
  195. if(cell_direction == CELL_DIRECTION_OUT) {
  196. ++stats_n_relay_cells_delivered;
  197. log_fn(LOG_DEBUG,"Sending to exit.");
  198. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL);
  199. }
  200. if(cell_direction == CELL_DIRECTION_IN) {
  201. ++stats_n_relay_cells_delivered;
  202. log_fn(LOG_DEBUG,"Sending to AP.");
  203. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint);
  204. }
  205. }
  206. ++stats_n_relay_cells_relayed;
  207. /* not recognized. pass it on. */
  208. if(cell_direction == CELL_DIRECTION_OUT)
  209. conn = circ->n_conn;
  210. else
  211. conn = circ->p_conn;
  212. if(!conn) { //|| !connection_speaks_cells(conn)) {
  213. log_fn(LOG_INFO,"Didn't recognize cell (%d), but circ stops here! Dropping.", *(int *)(cell->payload+1));
  214. return 0;
  215. }
  216. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  217. connection_write_cell_to_buf(cell, conn);
  218. return 0;
  219. }
  220. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  221. crypt_path_t **layer_hint, char *recognized, connection_t **conn) {
  222. crypt_path_t *thishop;
  223. char out[256];
  224. assert(circ && in && recognized && conn);
  225. assert(inlen < 256);
  226. if(cell_direction == CELL_DIRECTION_IN) {
  227. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  228. thishop = circ->cpath;
  229. if(thishop->state != CPATH_STATE_OPEN) {
  230. log_fn(LOG_WARNING,"Relay cell before first created cell?");
  231. return -1;
  232. }
  233. do { /* Remember: cpath is in forward order, that is, first hop first. */
  234. assert(thishop);
  235. log_fn(LOG_DEBUG,"before decrypt: %d",*(int*)(in+2));
  236. /* decrypt */
  237. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  238. log_fn(LOG_WARNING,"Error performing onion decryption: %s", crypto_perror());
  239. return -1;
  240. }
  241. memcpy(in,out,inlen);
  242. log_fn(LOG_DEBUG,"after decrypt: %d",*(int*)(in+2));
  243. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn))) {
  244. *layer_hint = thishop;
  245. return 0;
  246. }
  247. thishop = thishop->next;
  248. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  249. log_fn(LOG_INFO,"in-cell at OP not recognized. Dropping.");
  250. return 0;
  251. } else { /* we're in the middle. Just one crypt. */
  252. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  253. if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
  254. log_fn(LOG_WARNING,"Onion encryption failed for ACI %u: %s",
  255. circ->p_aci, crypto_perror());
  256. return -1;
  257. }
  258. memcpy(in,out,inlen);
  259. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  260. log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  261. /* don't check for recognized. only the OP can recognize a stream on the way back. */
  262. }
  263. } else if(cell_direction == CELL_DIRECTION_OUT) {
  264. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  265. thishop = *layer_hint; /* we already know which layer, from when we package_raw_inbuf'ed */
  266. /* moving from last to first hop */
  267. do {
  268. assert(thishop);
  269. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  270. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
  271. log_fn(LOG_WARNING,"Error performing encryption: %s", crypto_perror());
  272. return -1;
  273. }
  274. memcpy(in,out,inlen);
  275. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  276. thishop = thishop->prev;
  277. } while(thishop != circ->cpath->prev);
  278. } else { /* we're in the middle. Just one crypt. */
  279. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  280. log_fn(LOG_WARNING,"Decryption failed for ACI %u: %s",
  281. circ->n_aci, crypto_perror());
  282. return -1;
  283. }
  284. memcpy(in,out,inlen);
  285. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
  286. return 0;
  287. }
  288. } else {
  289. log_fn(LOG_ERR,"unknown cell direction %d.", cell_direction);
  290. assert(0);
  291. }
  292. return 0;
  293. }
  294. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
  295. /* FIXME can optimize by passing thishop in */
  296. connection_t *tmpconn;
  297. if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE)) {
  298. log_fn(LOG_DEBUG,"It's the zero stream. Recognized.");
  299. return 1; /* the zero stream is always recognized */
  300. }
  301. log_fn(LOG_DEBUG,"not the zero stream.");
  302. if(cell_direction == CELL_DIRECTION_OUT)
  303. tmpconn = circ->n_streams;
  304. else
  305. tmpconn = circ->p_streams;
  306. if(!tmpconn) {
  307. log_fn(LOG_DEBUG,"No conns. Not recognized.");
  308. return 0;
  309. }
  310. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  311. if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
  312. log_fn(LOG_DEBUG,"recognized stream %d.", *(int*)stream);
  313. *conn = tmpconn;
  314. return 1;
  315. }
  316. log_fn(LOG_DEBUG,"considered stream %d, not it.",*(int*)tmpconn->stream_id);
  317. }
  318. log_fn(LOG_DEBUG,"Didn't recognize on this iteration of decryption.");
  319. return 0;
  320. }
  321. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  322. connection_t *conn;
  323. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  324. log_fn(LOG_DEBUG,"resuming");
  325. if(edge_type == EDGE_EXIT)
  326. conn = circ->n_streams;
  327. else
  328. conn = circ->p_streams;
  329. for( ; conn; conn=conn->next_stream) {
  330. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  331. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  332. connection_start_reading(conn);
  333. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  334. /* If the circuit won't accept any more data, return without looking
  335. * at any more of the streams. Any connections that should be stopped
  336. * have already been stopped by connection_package_raw_inbuf. */
  337. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  338. return;
  339. }
  340. }
  341. }
  342. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  343. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  344. connection_t *conn = NULL;
  345. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  346. assert(edge_type == EDGE_EXIT || layer_hint);
  347. log_fn(LOG_DEBUG,"considering");
  348. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  349. conn = circ->n_streams;
  350. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  351. conn = circ->p_streams;
  352. else
  353. return 0;
  354. for( ; conn; conn=conn->next_stream)
  355. if(!layer_hint || conn->cpath_layer == layer_hint)
  356. connection_stop_reading(conn);
  357. log_fn(LOG_DEBUG,"yes. stopped.");
  358. return 1;
  359. }
  360. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  361. cell_t cell;
  362. assert(circ);
  363. memset(&cell, 0, sizeof(cell_t));
  364. cell.command = CELL_RELAY;
  365. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  366. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  367. cell.length = RELAY_HEADER_SIZE;
  368. if(edge_type == EDGE_AP) { /* i'm the AP */
  369. cell.aci = circ->n_aci;
  370. while(layer_hint->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  371. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme forward.", layer_hint->deliver_window);
  372. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  373. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, layer_hint) < 0) {
  374. return -1;
  375. }
  376. }
  377. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  378. cell.aci = circ->p_aci;
  379. while(circ->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  380. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme back.", circ->deliver_window);
  381. circ->deliver_window += CIRCWINDOW_INCREMENT;
  382. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, layer_hint) < 0) {
  383. return -1;
  384. }
  385. }
  386. }
  387. return 0;
  388. }
  389. void circuit_close(circuit_t *circ) {
  390. connection_t *conn;
  391. circuit_t *youngest=NULL;
  392. assert(circ);
  393. if(options.APPort) {
  394. youngest = circuit_get_newest_open();
  395. log_fn(LOG_DEBUG,"youngest %d, circ %d.",(int)youngest, (int)circ);
  396. }
  397. circuit_remove(circ);
  398. if(circ->n_conn)
  399. connection_send_destroy(circ->n_aci, circ->n_conn);
  400. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  401. connection_send_destroy(circ->n_aci, conn);
  402. }
  403. if(circ->p_conn)
  404. connection_send_destroy(circ->n_aci, circ->p_conn);
  405. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  406. connection_send_destroy(circ->p_aci, conn);
  407. }
  408. if(options.APPort && youngest == circ) { /* check this after we've sent the destroys, to reduce races */
  409. /* our current circuit just died. Launch another one pronto. */
  410. log_fn(LOG_INFO,"Youngest circuit dying. Launching a replacement.");
  411. circuit_launch_new(1);
  412. }
  413. circuit_free(circ);
  414. }
  415. void circuit_about_to_close_connection(connection_t *conn) {
  416. /* send destroys for all circuits using conn */
  417. /* currently, we assume it's too late to flush conn's buf here.
  418. * down the road, maybe we'll consider that eof doesn't mean can't-write
  419. */
  420. circuit_t *circ;
  421. connection_t *prevconn;
  422. if(!connection_speaks_cells(conn)) {
  423. /* it's an edge conn. need to remove it from the linked list of
  424. * conn's for this circuit. Send an 'end' relay command.
  425. * But don't kill the circuit.
  426. */
  427. circ = circuit_get_by_conn(conn);
  428. if(!circ)
  429. return;
  430. if(conn == circ->p_streams) {
  431. circ->p_streams = conn->next_stream;
  432. goto send_end;
  433. }
  434. if(conn == circ->n_streams) {
  435. circ->n_streams = conn->next_stream;
  436. goto send_end;
  437. }
  438. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  439. if(prevconn && prevconn->next_stream) {
  440. prevconn->next_stream = conn->next_stream;
  441. goto send_end;
  442. }
  443. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  444. if(prevconn && prevconn->next_stream) {
  445. prevconn->next_stream = conn->next_stream;
  446. goto send_end;
  447. }
  448. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  449. assert(0); /* should never get here */
  450. send_end:
  451. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_END) < 0) {
  452. log_fn(LOG_DEBUG,"sending end failed. Closing.");
  453. circuit_close(circ);
  454. }
  455. return;
  456. }
  457. /* this connection speaks cells. We must close all the circuits on it. */
  458. while((circ = circuit_get_by_conn(conn))) {
  459. if(circ->n_conn == conn) /* it's closing in front of us */
  460. circ->n_conn = NULL;
  461. if(circ->p_conn == conn) /* it's closing behind us */
  462. circ->p_conn = NULL;
  463. circuit_close(circ);
  464. }
  465. }
  466. /* FIXME this now leaves some out */
  467. void circuit_dump_by_conn(connection_t *conn) {
  468. circuit_t *circ;
  469. connection_t *tmpconn;
  470. for(circ=global_circuitlist;circ;circ = circ->next) {
  471. if(circ->p_conn == conn)
  472. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  473. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  474. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  475. if(tmpconn == conn) {
  476. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  477. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  478. }
  479. }
  480. if(circ->n_conn == conn)
  481. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  482. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  483. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  484. if(tmpconn == conn) {
  485. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  486. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  487. }
  488. }
  489. }
  490. }
  491. void circuit_expire_unused_circuits(void) {
  492. circuit_t *circ, *tmpcirc;
  493. circuit_t *youngest;
  494. youngest = circuit_get_newest_open();
  495. circ = global_circuitlist;
  496. while(circ) {
  497. tmpcirc = circ;
  498. circ = circ->next;
  499. if(tmpcirc != youngest && !tmpcirc->p_conn && !tmpcirc->p_streams) {
  500. log_fn(LOG_DEBUG,"Closing n_aci %d",tmpcirc->n_aci);
  501. circuit_close(tmpcirc);
  502. }
  503. }
  504. }
  505. /* failure_status code: negative means reset failures to 0. Other values mean
  506. * add that value to the current number of failures, then if we don't have too
  507. * many failures on record, try to make a new circuit.
  508. */
  509. void circuit_launch_new(int failure_status) {
  510. static int failures=0;
  511. if(!options.APPort) /* we're not an application proxy. no need for circuits. */
  512. return;
  513. if(failure_status == -1) { /* I was called because a circuit succeeded */
  514. failures = 0;
  515. return;
  516. }
  517. failures += failure_status;
  518. retry_circuit:
  519. if(failures > 5) {
  520. log_fn(LOG_INFO,"Giving up for now, %d failures.", failures);
  521. return;
  522. }
  523. if(circuit_establish_circuit() < 0) {
  524. failures++;
  525. goto retry_circuit;
  526. }
  527. failures = 0;
  528. return;
  529. }
  530. int circuit_establish_circuit(void) {
  531. routerinfo_t *firsthop;
  532. connection_t *n_conn;
  533. circuit_t *circ;
  534. circ = circuit_new(0, NULL); /* sets circ->p_aci and circ->p_conn */
  535. circ->state = CIRCUIT_STATE_OR_WAIT;
  536. circ->cpath = onion_generate_cpath(&firsthop);
  537. if(!circ->cpath) {
  538. log_fn(LOG_INFO,"Generating cpath failed.");
  539. circuit_close(circ);
  540. return -1;
  541. }
  542. /* now see if we're already connected to the first OR in 'route' */
  543. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  544. firsthop->address,firsthop->or_port);
  545. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  546. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  547. circ->n_addr = firsthop->addr;
  548. circ->n_port = firsthop->or_port;
  549. if(options.OnionRouter) { /* we would be connected if he were up. but he's not. */
  550. log_fn(LOG_INFO,"Route's firsthop isn't connected.");
  551. circuit_close(circ);
  552. return -1;
  553. }
  554. if(!n_conn) { /* launch the connection */
  555. n_conn = connection_or_connect(firsthop);
  556. if(!n_conn) { /* connect failed, forget the whole thing */
  557. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  558. circuit_close(circ);
  559. return -1;
  560. }
  561. }
  562. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  563. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  564. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  565. */
  566. } else { /* it (or a twin) is already open. use it. */
  567. circ->n_addr = n_conn->addr;
  568. circ->n_port = n_conn->port;
  569. circ->n_conn = n_conn;
  570. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  571. if(circuit_send_next_onion_skin(circ) < 0) {
  572. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  573. circuit_close(circ);
  574. return -1;
  575. }
  576. }
  577. return 0;
  578. }
  579. /* find circuits that are waiting on me, if any, and get them to send the onion */
  580. void circuit_n_conn_open(connection_t *or_conn) {
  581. circuit_t *circ;
  582. log_fn(LOG_DEBUG,"Starting.");
  583. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  584. for(;;) {
  585. if(!circ)
  586. return;
  587. log_fn(LOG_DEBUG,"Found circ, sending onion skin.");
  588. circ->n_conn = or_conn;
  589. if(circuit_send_next_onion_skin(circ) < 0) {
  590. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  591. circuit_close(circ);
  592. return; /* FIXME will want to try the other circuits too? */
  593. }
  594. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  595. }
  596. }
  597. int circuit_send_next_onion_skin(circuit_t *circ) {
  598. cell_t cell;
  599. crypt_path_t *hop;
  600. routerinfo_t *router;
  601. assert(circ && circ->cpath);
  602. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  603. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  604. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  605. memset(&cell, 0, sizeof(cell_t));
  606. cell.command = CELL_CREATE;
  607. cell.aci = circ->n_aci;
  608. cell.length = DH_ONIONSKIN_LEN;
  609. if(onion_skin_create(circ->n_conn->onion_pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  610. log_fn(LOG_WARNING,"onion_skin_create (first hop) failed.");
  611. return -1;
  612. }
  613. connection_write_cell_to_buf(&cell, circ->n_conn);
  614. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  615. circ->state = CIRCUIT_STATE_BUILDING;
  616. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  617. } else {
  618. assert(circ->cpath->state == CPATH_STATE_OPEN);
  619. assert(circ->state == CIRCUIT_STATE_BUILDING);
  620. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  621. for(hop=circ->cpath->next;
  622. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  623. hop=hop->next) ;
  624. if(hop == circ->cpath) { /* done building the circuit. whew. */
  625. circ->state = CIRCUIT_STATE_OPEN;
  626. log_fn(LOG_INFO,"circuit built!");
  627. return 0;
  628. }
  629. router = router_get_by_addr_port(hop->addr,hop->port);
  630. if(!router) {
  631. log_fn(LOG_WARNING,"couldn't lookup router %d:%d",hop->addr,hop->port);
  632. return -1;
  633. }
  634. memset(&cell, 0, sizeof(cell_t));
  635. cell.command = CELL_RELAY;
  636. cell.aci = circ->n_aci;
  637. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_EXTEND);
  638. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  639. cell.length = RELAY_HEADER_SIZE + 6 + DH_ONIONSKIN_LEN;
  640. *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE) = htonl(hop->addr);
  641. *(uint16_t*)(cell.payload+RELAY_HEADER_SIZE+4) = htons(hop->port);
  642. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), cell.payload+RELAY_HEADER_SIZE+6) < 0) {
  643. log_fn(LOG_WARNING,"onion_skin_create failed.");
  644. return -1;
  645. }
  646. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  647. /* send it to hop->prev, because it will transfer it to a create cell and then send to hop */
  648. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, hop->prev) < 0) {
  649. log_fn(LOG_WARNING,"failed to deliver extend cell. Closing.");
  650. return -1;
  651. }
  652. hop->state = CPATH_STATE_AWAITING_KEYS;
  653. }
  654. return 0;
  655. }
  656. /* take the 'extend' cell, pull out addr/port plus the onion skin. Make
  657. * sure we're connected to the next hop, and pass it the onion skin in
  658. * a create cell.
  659. */
  660. int circuit_extend(cell_t *cell, circuit_t *circ) {
  661. connection_t *n_conn;
  662. aci_t aci_type;
  663. cell_t newcell;
  664. if(circ->n_conn) {
  665. log_fn(LOG_WARNING,"n_conn already set. Bug/attack. Closing.");
  666. return -1;
  667. }
  668. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  669. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  670. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  671. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  672. /* i've disabled making connections through OPs, but it's definitely
  673. * possible here. I'm not sure if it would be a bug or a feature. -RD
  674. */
  675. /* note also that this will close circuits where the onion has the same
  676. * router twice in a row in the path. i think that's ok. -RD
  677. */
  678. struct in_addr in;
  679. in.s_addr = htonl(circ->n_addr);
  680. log_fn(LOG_DEBUG,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  681. /* XXX later we should fail more gracefully here, like with a 'truncated' */
  682. return -1;
  683. }
  684. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  685. circ->n_port = n_conn->port;
  686. circ->n_conn = n_conn;
  687. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  688. aci_type = decide_aci_type(options.Nickname, n_conn->nickname);
  689. log_fn(LOG_DEBUG,"aci_type = %u.",aci_type);
  690. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  691. if(!circ->n_aci) {
  692. log_fn(LOG_WARNING,"failed to get unique aci.");
  693. return -1;
  694. }
  695. log_fn(LOG_DEBUG,"Chosen ACI %u.",circ->n_aci);
  696. memset(&newcell, 0, sizeof(cell_t));
  697. newcell.command = CELL_CREATE;
  698. newcell.aci = circ->n_aci;
  699. newcell.length = DH_ONIONSKIN_LEN;
  700. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
  701. connection_write_cell_to_buf(&newcell, circ->n_conn);
  702. return 0;
  703. }
  704. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  705. unsigned char iv[16];
  706. unsigned char keys[32];
  707. crypt_path_t *hop;
  708. memset(iv, 0, 16);
  709. assert(circ->cpath);
  710. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  711. hop = circ->cpath;
  712. else {
  713. for(hop=circ->cpath->next;
  714. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  715. hop=hop->next) ;
  716. if(hop == circ->cpath) { /* got an extended when we're all done? */
  717. log_fn(LOG_WARNING,"got extended when circ already built? Closing.");
  718. return -1;
  719. }
  720. }
  721. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  722. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 32) < 0) {
  723. log_fn(LOG_WARNING,"onion_skin_client_handshake failed.");
  724. return -1;
  725. }
  726. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  727. hop->handshake_state = NULL;
  728. log_fn(LOG_DEBUG,"hop %d init cipher forward %d, backward %d.", (uint32_t)hop, *(uint32_t*)keys, *(uint32_t*)(keys+16));
  729. if (!(hop->f_crypto =
  730. crypto_create_init_cipher(CIRCUIT_CIPHER,keys,iv,1))) {
  731. log(LOG_WARNING,"forward cipher initialization failed.");
  732. return -1;
  733. }
  734. if (!(hop->b_crypto =
  735. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+16,iv,0))) {
  736. log(LOG_WARNING,"backward cipher initialization failed.");
  737. return -1;
  738. }
  739. hop->state = CPATH_STATE_OPEN;
  740. log_fn(LOG_INFO,"finished");
  741. return 0;
  742. }
  743. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  744. crypt_path_t *victim;
  745. connection_t *stream;
  746. assert(circ);
  747. assert(layer);
  748. while(layer->next != circ->cpath) {
  749. /* we need to clear out layer->next */
  750. victim = layer->next;
  751. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  752. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  753. if(stream->cpath_layer == victim) {
  754. log_fn(LOG_INFO, "Marking stream %d for close.", *(int*)stream->stream_id);
  755. /*ENDCLOSE*/ stream->marked_for_close = 1;
  756. }
  757. }
  758. layer->next = victim->next;
  759. circuit_free_cpath_node(victim);
  760. }
  761. log_fn(LOG_INFO, "finished");
  762. return 0;
  763. }
  764. void assert_cpath_layer_ok(const crypt_path_t *cp)
  765. {
  766. assert(cp->f_crypto);
  767. assert(cp->b_crypto);
  768. assert(cp->addr);
  769. assert(cp->port);
  770. switch(cp->state)
  771. {
  772. case CPATH_STATE_CLOSED:
  773. case CPATH_STATE_OPEN:
  774. assert(!cp->handshake_state);
  775. break;
  776. case CPATH_STATE_AWAITING_KEYS:
  777. assert(cp->handshake_state);
  778. break;
  779. default:
  780. assert(0);
  781. }
  782. assert(cp->package_window >= 0);
  783. assert(cp->deliver_window >= 0);
  784. }
  785. void assert_cpath_ok(const crypt_path_t *cp)
  786. {
  787. while(cp->prev)
  788. cp = cp->prev;
  789. while(cp->next) {
  790. assert_cpath_layer_ok(cp);
  791. /* layers must be in sequence of: "open* awaiting? closed*" */
  792. if (cp->prev) {
  793. if (cp->prev->state == CPATH_STATE_OPEN) {
  794. assert(cp->state == CPATH_STATE_CLOSED ||
  795. cp->state == CPATH_STATE_AWAITING_KEYS);
  796. } else {
  797. assert(cp->state == CPATH_STATE_CLOSED);
  798. }
  799. }
  800. cp = cp->next;
  801. }
  802. }
  803. void assert_circuit_ok(const circuit_t *c)
  804. {
  805. connection_t *conn;
  806. assert(c->n_addr);
  807. assert(c->n_port);
  808. assert(c->n_conn);
  809. assert(c->n_conn->type == CONN_TYPE_OR);
  810. if (c->p_conn)
  811. assert(c->p_conn->type == CONN_TYPE_OR);
  812. for (conn = c->p_streams; conn; conn = conn->next_stream)
  813. assert(c->p_conn->type == CONN_TYPE_EXIT);
  814. for (conn = c->n_streams; conn; conn = conn->next_stream)
  815. assert(conn->type == CONN_TYPE_EXIT);
  816. assert(c->deliver_window >= 0);
  817. assert(c->package_window >= 0);
  818. if (c->state == CIRCUIT_STATE_OPEN) {
  819. if (c->cpath) {
  820. assert(!c->n_crypto);
  821. assert(!c->p_crypto);
  822. } else {
  823. assert(c->n_crypto);
  824. assert(c->p_crypto);
  825. }
  826. }
  827. if (c->cpath) {
  828. assert_cpath_ok(c->cpath);
  829. }
  830. }
  831. /*
  832. Local Variables:
  833. mode:c
  834. indent-tabs-mode:nil
  835. c-basic-offset:2
  836. End:
  837. */