circuit.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START VARIABLES **********/
  6. static circuit_t *global_circuitlist=NULL;
  7. char *circuit_state_to_string[] = {
  8. "receiving the onion", /* 0 */
  9. "connecting to firsthop", /* 1 */
  10. "open" /* 2 */
  11. };
  12. /********* END VARIABLES ************/
  13. void circuit_add(circuit_t *circ) {
  14. if(!global_circuitlist) { /* first one */
  15. global_circuitlist = circ;
  16. circ->next = NULL;
  17. } else {
  18. circ->next = global_circuitlist;
  19. global_circuitlist = circ;
  20. }
  21. }
  22. void circuit_remove(circuit_t *circ) {
  23. circuit_t *tmpcirc;
  24. assert(circ && global_circuitlist);
  25. if(global_circuitlist == circ) {
  26. global_circuitlist = global_circuitlist->next;
  27. return;
  28. }
  29. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  30. if(tmpcirc->next == circ) {
  31. tmpcirc->next = circ->next;
  32. return;
  33. }
  34. }
  35. }
  36. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  37. circuit_t *circ;
  38. circ = (circuit_t *)malloc(sizeof(circuit_t));
  39. if(!circ)
  40. return NULL;
  41. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  42. circ->p_aci = p_aci;
  43. circ->p_conn = p_conn;
  44. circ->state = CIRCUIT_STATE_OPEN_WAIT;
  45. /* ACIs */
  46. circ->p_aci = p_aci;
  47. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  48. circ->n_receive_window = RECEIVE_WINDOW_START;
  49. circ->p_receive_window = RECEIVE_WINDOW_START;
  50. circuit_add(circ);
  51. return circ;
  52. }
  53. void circuit_free(circuit_t *circ) {
  54. if (circ->n_crypto)
  55. crypto_free_cipher_env(circ->n_crypto);
  56. if (circ->p_crypto)
  57. crypto_free_cipher_env(circ->p_crypto);
  58. if(circ->onion)
  59. free(circ->onion);
  60. if(circ->cpath)
  61. circuit_free_cpath(circ->cpath, circ->cpathlen);
  62. free(circ);
  63. }
  64. void circuit_free_cpath(crypt_path_t **cpath, int cpathlen) {
  65. int i;
  66. for(i=0;i<cpathlen;i++)
  67. free(cpath[i]);
  68. free(cpath);
  69. }
  70. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  71. aci_t test_aci;
  72. connection_t *conn;
  73. log(LOG_DEBUG,"get_unique_aci_by_addr_port() trying to get a unique aci");
  74. crypto_pseudo_rand(2, (unsigned char *)&test_aci);
  75. if(aci_type == ACI_TYPE_LOWER)
  76. test_aci &= htons(0x00FF);
  77. if(aci_type == ACI_TYPE_HIGHER)
  78. test_aci &= htons(0xFF00);
  79. /* if aci_type == ACI_BOTH, don't filter any of it */
  80. if(test_aci == 0)
  81. return get_unique_aci_by_addr_port(addr, port, aci_type); /* try again */
  82. conn = connection_exact_get_by_addr_port(addr,port);
  83. if(!conn) /* there can't be a conflict -- no connection of that sort yet */
  84. return test_aci;
  85. if(circuit_get_by_aci_conn(test_aci, conn))
  86. return get_unique_aci_by_addr_port(addr, port, aci_type); /* try again */
  87. return test_aci;
  88. }
  89. int circuit_init(circuit_t *circ, int aci_type) {
  90. onion_layer_t *ol;
  91. unsigned char iv[16];
  92. unsigned char digest1[20];
  93. unsigned char digest2[20];
  94. assert(circ);
  95. ol = (onion_layer_t *)circ->onion;
  96. assert(ol);
  97. log(LOG_DEBUG,"circuit_init(): starting");
  98. circ->n_addr = ol->addr;
  99. circ->n_port = ol->port;
  100. log(LOG_DEBUG,"circuit_init(): Set port to %u.",ol->port);
  101. circ->p_f = ol->backf;
  102. log(LOG_DEBUG,"circuit_init(): Set BACKF to %u.",ol->backf);
  103. circ->n_f = ol->forwf;
  104. log(LOG_DEBUG,"circuit_init(): Set FORWF to %u.",ol->forwf);
  105. circ->state = CIRCUIT_STATE_OPEN;
  106. log(LOG_DEBUG,"circuit_init(): aci_type = %u.",aci_type);
  107. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  108. log(LOG_DEBUG,"circuit_init(): Chosen ACI %u.",circ->n_aci);
  109. /* keys */
  110. memset((void *)iv, 0, 16);
  111. crypto_SHA_digest(ol->keyseed,16,digest1);
  112. crypto_SHA_digest(digest1,20,digest2);
  113. crypto_SHA_digest(digest2,20,digest1);
  114. log(LOG_DEBUG,"circuit_init(): Computed keys.");
  115. if (!(circ->p_crypto = create_onion_cipher(circ->p_f,digest2,iv,1))) {
  116. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  117. return -1;
  118. }
  119. if (!(circ->n_crypto = create_onion_cipher(circ->n_f, digest1, iv, 0))) {
  120. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  121. return -1;
  122. }
  123. log(LOG_DEBUG,"circuit_init(): Cipher initialization complete.");
  124. circ->expire = ol->expire;
  125. return 0;
  126. }
  127. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  128. if(!circ) /* use circ if it's defined, else start from the beginning */
  129. circ = global_circuitlist;
  130. else
  131. circ = circ->next;
  132. for( ;circ;circ = circ->next) {
  133. if(circ->n_addr == naddr && circ->n_port == nport)
  134. return circ;
  135. }
  136. return NULL;
  137. }
  138. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  139. circuit_t *circ;
  140. for(circ=global_circuitlist;circ;circ = circ->next) {
  141. if(circ->p_conn == conn && circ->p_aci == aci)
  142. return circ;
  143. if(circ->n_conn == conn && circ->n_aci == aci)
  144. return circ;
  145. }
  146. return NULL;
  147. }
  148. circuit_t *circuit_get_by_conn(connection_t *conn) {
  149. circuit_t *circ;
  150. for(circ=global_circuitlist;circ;circ = circ->next) {
  151. if(circ->p_conn == conn)
  152. return circ;
  153. if(circ->n_conn == conn)
  154. return circ;
  155. }
  156. return NULL;
  157. }
  158. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int crypt_type) {
  159. /* first decrypt cell->length */
  160. if(circuit_crypt(circ, &(cell->length), 1, crypt_type) < 0) {
  161. log(LOG_DEBUG,"circuit_deliver_data_cell(): length decryption failed. Dropping connection.");
  162. return -1;
  163. }
  164. /* then decrypt the payload */
  165. if(circuit_crypt(circ, (char *)&(cell->payload), CELL_PAYLOAD_SIZE, crypt_type) < 0) {
  166. log(LOG_DEBUG,"circuit_deliver_data_cell(): payload decryption failed. Dropping connection.");
  167. return -1;
  168. }
  169. if(conn->type == CONN_TYPE_EXIT) { /* send payload directly */
  170. // log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to exit.");
  171. return connection_exit_process_data_cell(cell, conn);
  172. }
  173. if(conn->type == CONN_TYPE_AP) { /* send payload directly */
  174. // log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to AP.");
  175. return connection_ap_process_data_cell(cell, conn);
  176. }
  177. /* else send it as a cell */
  178. // log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to connection.");
  179. return connection_write_cell_to_buf(cell, conn);
  180. }
  181. int circuit_crypt(circuit_t *circ, char *in, int inlen, char crypt_type) {
  182. char *out;
  183. int i;
  184. crypt_path_t *thishop;
  185. assert(circ && in);
  186. out = (char *)malloc(inlen);
  187. if(!out)
  188. return -1;
  189. if(crypt_type == 'e') {
  190. // log(LOG_DEBUG,"circuit_crypt(): Encrypting %d bytes.",inlen);
  191. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  192. /* 'e' means we're preparing to send it out. */
  193. for (i=0; i < circ->cpathlen; i++) /* moving from last to first hop
  194. * Remember : cpath is in reverse order, i.e. last hop first
  195. */
  196. {
  197. // log(LOG_DEBUG,"circuit_crypt() : Encrypting via cpath: Processing hop %u",circ->cpathlen-i);
  198. thishop = circ->cpath[i];
  199. /* encrypt */
  200. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, (unsigned char *)out)) {
  201. log(LOG_ERR,"Error performing encryption:%s",crypto_perror());
  202. free(out);
  203. return -1;
  204. }
  205. /* copy ciphertext back to buf */
  206. memcpy(in,out,inlen);
  207. }
  208. } else { /* we're in the middle. Just one crypt. */
  209. if(crypto_cipher_encrypt(circ->p_crypto,in, inlen, out)) {
  210. log(LOG_ERR,"circuit_encrypt(): Encryption failed for ACI : %u (%s).",
  211. circ->p_aci, crypto_perror());
  212. free(out);
  213. return -1;
  214. }
  215. memcpy(in,out,inlen);
  216. }
  217. } else if(crypt_type == 'd') {
  218. // log(LOG_DEBUG,"circuit_crypt(): Decrypting %d bytes.",inlen);
  219. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  220. for (i=circ->cpathlen-1; i >= 0; i--) /* moving from first to last hop
  221. * Remember : cpath is in reverse order, i.e. last hop first
  222. */
  223. {
  224. // log(LOG_DEBUG,"circuit_crypt() : Decrypting via cpath: Processing hop %u",circ->cpathlen-i);
  225. thishop = circ->cpath[i];
  226. /* encrypt */
  227. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  228. log(LOG_ERR,"Error performing decryption:%s",crypto_perror());
  229. free(out);
  230. return -1;
  231. }
  232. /* copy ciphertext back to buf */
  233. memcpy(in,out,inlen);
  234. }
  235. } else { /* we're in the middle. Just one crypt. */
  236. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  237. log(LOG_ERR,"circuit_crypt(): Decryption failed for ACI : %u (%s).",
  238. circ->n_aci, crypto_perror());
  239. free(out);
  240. return -1;
  241. }
  242. memcpy(in,out,inlen);
  243. }
  244. }
  245. free(out);
  246. return 0;
  247. }
  248. void circuit_close(circuit_t *circ) {
  249. circuit_remove(circ);
  250. if(circ->n_conn)
  251. connection_send_destroy(circ->n_aci, circ->n_conn);
  252. if(circ->p_conn)
  253. connection_send_destroy(circ->p_aci, circ->p_conn);
  254. circuit_free(circ);
  255. }
  256. void circuit_about_to_close_connection(connection_t *conn) {
  257. /* send destroys for all circuits using conn */
  258. /* currently, we assume it's too late to flush conn's buf here.
  259. * down the road, maybe we'll consider that eof doesn't mean can't-write
  260. */
  261. circuit_t *circ;
  262. while((circ = circuit_get_by_conn(conn))) {
  263. circuit_remove(circ);
  264. if(circ->n_conn == conn) /* it's closing in front of us */
  265. connection_send_destroy(circ->p_aci, circ->p_conn);
  266. if(circ->p_conn == conn) /* it's closing behind us */
  267. connection_send_destroy(circ->n_aci, circ->n_conn);
  268. circuit_free(circ);
  269. }
  270. }
  271. void circuit_dump_by_conn(connection_t *conn) {
  272. circuit_t *circ;
  273. for(circ=global_circuitlist;circ;circ = circ->next) {
  274. if(circ->p_conn == conn) {
  275. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  276. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  277. }
  278. if(circ->n_conn == conn) {
  279. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  280. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  281. }
  282. }
  283. }