command.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char command_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file command.c
  10. * \brief Functions for processing incoming cells.
  11. **/
  12. /* In-points to command.c:
  13. *
  14. * - command_process_cell(), called from
  15. * connection_or_process_cells_from_inbuf() in connection_or.c
  16. */
  17. #include "or.h"
  18. /** Keep statistics about how many of each type of cell we've received. */
  19. uint64_t stats_n_padding_cells_processed = 0;
  20. uint64_t stats_n_create_cells_processed = 0;
  21. uint64_t stats_n_created_cells_processed = 0;
  22. uint64_t stats_n_relay_cells_processed = 0;
  23. uint64_t stats_n_destroy_cells_processed = 0;
  24. uint64_t stats_n_versions_cells_processed = 0;
  25. uint64_t stats_n_netinfo_cells_processed = 0;
  26. uint64_t stats_n_cert_cells_processed = 0;
  27. uint64_t stats_n_link_auth_cells_processed = 0;
  28. /* These are the main functions for processing cells */
  29. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  30. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  31. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  32. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  33. static void command_process_versions_cell(var_cell_t *cell,
  34. or_connection_t *conn);
  35. static void command_process_netinfo_cell(cell_t *cell, or_connection_t *conn);
  36. static void command_process_cert_cell(var_cell_t *cell, or_connection_t *conn);
  37. static void command_process_link_auth_cell(cell_t *cell,or_connection_t *conn);
  38. #ifdef KEEP_TIMING_STATS
  39. /** This is a wrapper function around the actual function that processes the
  40. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  41. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  42. */
  43. static void
  44. command_time_process_cell(cell_t *cell, or_connection_t *conn, int *time,
  45. void (*func)(cell_t *, or_connection_t *))
  46. {
  47. struct timeval start, end;
  48. long time_passed;
  49. tor_gettimeofday(&start);
  50. (*func)(cell, conn);
  51. tor_gettimeofday(&end);
  52. time_passed = tv_udiff(&start, &end) ;
  53. if (time_passed > 10000) { /* more than 10ms */
  54. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  55. }
  56. if (time_passed < 0) {
  57. log_info(LD_GENERAL,"That call took us back in time!");
  58. time_passed = 0;
  59. }
  60. *time += time_passed;
  61. }
  62. #endif
  63. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  64. * statistics about how many of each cell we've processed so far
  65. * this second, and the total number of microseconds it took to
  66. * process each type of cell.
  67. */
  68. void
  69. command_process_cell(cell_t *cell, or_connection_t *conn)
  70. {
  71. #ifdef KEEP_TIMING_STATS
  72. /* how many of each cell have we seen so far this second? needs better
  73. * name. */
  74. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  75. /* how long has it taken to process each type of cell? */
  76. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  77. static time_t current_second = 0; /* from previous calls to time */
  78. time_t now = time(NULL);
  79. if (now > current_second) { /* the second has rolled over */
  80. /* print stats */
  81. log_info(LD_OR,
  82. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  83. "%d relays (%d ms), %d destroys (%d ms)",
  84. num_create, create_time/1000,
  85. num_created, created_time/1000,
  86. num_relay, relay_time/1000,
  87. num_destroy, destroy_time/1000);
  88. /* zero out stats */
  89. num_create = num_created = num_relay = num_destroy = 0;
  90. create_time = created_time = relay_time = destroy_time = 0;
  91. /* remember which second it is, for next time */
  92. current_second = now;
  93. }
  94. #endif
  95. #ifdef KEEP_TIMING_STATS
  96. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  97. ++num ## tp; \
  98. command_time_process_cell(cl, cn, & tp ## time , \
  99. command_process_ ## tp ## _cell); \
  100. } STMT_END
  101. #else
  102. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  103. #endif
  104. /*XXXX020 reject all but VERSIONS, NETINFO, CERT, LINK_AUTH when
  105. * handshaking. */
  106. switch (cell->command) {
  107. case CELL_PADDING:
  108. ++stats_n_padding_cells_processed;
  109. /* do nothing */
  110. break;
  111. case CELL_CREATE:
  112. case CELL_CREATE_FAST:
  113. ++stats_n_create_cells_processed;
  114. PROCESS_CELL(create, cell, conn);
  115. break;
  116. case CELL_CREATED:
  117. case CELL_CREATED_FAST:
  118. ++stats_n_created_cells_processed;
  119. PROCESS_CELL(created, cell, conn);
  120. break;
  121. case CELL_RELAY:
  122. case CELL_RELAY_EARLY:
  123. ++stats_n_relay_cells_processed;
  124. PROCESS_CELL(relay, cell, conn);
  125. break;
  126. case CELL_DESTROY:
  127. ++stats_n_destroy_cells_processed;
  128. PROCESS_CELL(destroy, cell, conn);
  129. break;
  130. case CELL_VERSIONS:
  131. tor_fragile_assert();
  132. break;
  133. case CELL_NETINFO:
  134. ++stats_n_netinfo_cells_processed;
  135. PROCESS_CELL(netinfo, cell, conn);
  136. break;
  137. case CELL_CERT:
  138. tor_fragile_assert();
  139. break;
  140. case CELL_LINK_AUTH:
  141. ++stats_n_link_auth_cells_processed;
  142. PROCESS_CELL(link_auth, cell, conn);
  143. break;
  144. default:
  145. log_fn(LOG_INFO, LD_PROTOCOL,
  146. "Cell of unknown type (%d) received. Dropping.", cell->command);
  147. break;
  148. }
  149. }
  150. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  151. * statistics about how many of each cell we've processed so far
  152. * this second, and the total number of microseconds it took to
  153. * process each type of cell.
  154. */
  155. void
  156. command_process_var_cell(var_cell_t *cell, or_connection_t *conn)
  157. {
  158. #ifdef KEEP_TIMING_STATS
  159. /* how many of each cell have we seen so far this second? needs better
  160. * name. */
  161. static int num_versions=0, num_cert=0;
  162. time_t now = time(NULL);
  163. if (now > current_second) { /* the second has rolled over */
  164. /* print stats */
  165. log_info(LD_OR,
  166. "At end of second: %d versions (%d ms), %d cert (%d ms)",
  167. num_versions, versions_time/1000,
  168. cert, cert_time/1000);
  169. num_versions = num_cert = 0;
  170. versions_time = cert_time = 0;
  171. /* remember which second it is, for next time */
  172. current_second = now;
  173. }
  174. #endif
  175. /*XXXX020 reject all when not handshaking. */
  176. switch (cell->command) {
  177. case CELL_VERSIONS:
  178. ++stats_n_versions_cells_processed;
  179. PROCESS_CELL(versions, cell, conn);
  180. break;
  181. case CELL_CERT:
  182. ++stats_n_cert_cells_processed;
  183. PROCESS_CELL(cert, cell, conn);
  184. break;
  185. default:
  186. log_warn(LD_BUG,
  187. "Variable-length cell of unknown type (%d) received.",
  188. cell->command);
  189. tor_fragile_assert();
  190. break;
  191. }
  192. }
  193. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  194. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  195. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  196. * picked up again when the cpuworker finishes decrypting it.
  197. */
  198. static void
  199. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  200. {
  201. or_circuit_t *circ;
  202. int id_is_high;
  203. if (we_are_hibernating()) {
  204. log_info(LD_OR,
  205. "Received create cell but we're shutting down. Sending back "
  206. "destroy.");
  207. connection_or_send_destroy(cell->circ_id, conn,
  208. END_CIRC_REASON_HIBERNATING);
  209. return;
  210. }
  211. if (!server_mode(get_options())) {
  212. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  213. "Received create cell (type %d) from %s:%d, but we're a client. "
  214. "Sending back a destroy.",
  215. (int)cell->command, conn->_base.address, conn->_base.port);
  216. connection_or_send_destroy(cell->circ_id, conn,
  217. END_CIRC_REASON_TORPROTOCOL);
  218. return;
  219. }
  220. /* If the high bit of the circuit ID is not as expected, close the
  221. * circ. */
  222. id_is_high = cell->circ_id & (1<<15);
  223. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  224. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  225. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  226. "Received create cell with unexpected circ_id %d. Closing.",
  227. cell->circ_id);
  228. connection_or_send_destroy(cell->circ_id, conn,
  229. END_CIRC_REASON_TORPROTOCOL);
  230. return;
  231. }
  232. if (circuit_get_by_circid_orconn(cell->circ_id, conn)) {
  233. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  234. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  235. "Received CREATE cell (circID %d) for known circ. "
  236. "Dropping (age %d).",
  237. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  238. if (router)
  239. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  240. "Details: nickname \"%s\", platform %s.",
  241. router->nickname, escaped(router->platform));
  242. return;
  243. }
  244. circ = or_circuit_new(cell->circ_id, conn);
  245. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  246. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  247. if (cell->command == CELL_CREATE) {
  248. circ->_base.onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  249. memcpy(circ->_base.onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  250. /* hand it off to the cpuworkers, and then return. */
  251. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  252. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  253. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  254. return;
  255. }
  256. log_debug(LD_OR,"success: handed off onionskin.");
  257. } else {
  258. /* This is a CREATE_FAST cell; we can handle it immediately without using
  259. * a CPU worker. */
  260. char keys[CPATH_KEY_MATERIAL_LEN];
  261. char reply[DIGEST_LEN*2];
  262. tor_assert(cell->command == CELL_CREATE_FAST);
  263. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  264. log_warn(LD_OR,"Failed to generate key material. Closing.");
  265. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  266. return;
  267. }
  268. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  269. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  270. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  271. return;
  272. }
  273. }
  274. }
  275. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  276. * Find the circuit
  277. * that it's intended for. If we're not the origin of the circuit, package
  278. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  279. * are the origin of the circuit, send it to circuit_finish_handshake() to
  280. * finish processing keys, and then call circuit_send_next_onion_skin() to
  281. * extend to the next hop in the circuit if necessary.
  282. */
  283. static void
  284. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  285. {
  286. circuit_t *circ;
  287. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  288. if (!circ) {
  289. log_info(LD_OR,
  290. "(circID %d) unknown circ (probably got a destroy earlier). "
  291. "Dropping.", cell->circ_id);
  292. return;
  293. }
  294. if (circ->n_circ_id != cell->circ_id) {
  295. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  296. "got created cell from Tor client? Closing.");
  297. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  298. return;
  299. }
  300. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  301. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  302. int err_reason = 0;
  303. log_debug(LD_OR,"at OP. Finishing handshake.");
  304. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  305. cell->payload)) < 0) {
  306. log_warn(LD_OR,"circuit_finish_handshake failed.");
  307. circuit_mark_for_close(circ, -err_reason);
  308. return;
  309. }
  310. log_debug(LD_OR,"Moving to next skin.");
  311. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  312. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  313. /* XXX push this circuit_close lower */
  314. circuit_mark_for_close(circ, -err_reason);
  315. return;
  316. }
  317. } else { /* pack it into an extended relay cell, and send it. */
  318. log_debug(LD_OR,
  319. "Converting created cell to extended relay cell, sending.");
  320. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  321. cell->payload, ONIONSKIN_REPLY_LEN,
  322. NULL);
  323. }
  324. }
  325. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  326. * it came in with a recognized circ_id. Pass it on to
  327. * circuit_receive_relay_cell() for actual processing.
  328. */
  329. static void
  330. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  331. {
  332. circuit_t *circ;
  333. int reason, direction;
  334. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  335. if (!circ) {
  336. log_debug(LD_OR,
  337. "unknown circuit %d on connection from %s:%d. Dropping.",
  338. cell->circ_id, conn->_base.address, conn->_base.port);
  339. return;
  340. }
  341. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  342. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  343. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  344. return;
  345. }
  346. if (CIRCUIT_IS_ORIGIN(circ)) {
  347. /* if we're a server and treating connections with recent local
  348. * traffic better, then this is one of them. */
  349. conn->client_used = time(NULL);
  350. }
  351. if (!CIRCUIT_IS_ORIGIN(circ) &&
  352. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  353. direction = CELL_DIRECTION_OUT;
  354. else
  355. direction = CELL_DIRECTION_IN;
  356. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  357. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  358. "(%s) failed. Closing.",
  359. direction==CELL_DIRECTION_OUT?"forward":"backward");
  360. circuit_mark_for_close(circ, -reason);
  361. }
  362. }
  363. /** Process a 'destroy' <b>cell</b> that just arrived from
  364. * <b>conn</b>. Find the circ that it refers to (if any).
  365. *
  366. * If the circ is in state
  367. * onionskin_pending, then call onion_pending_remove() to remove it
  368. * from the pending onion list (note that if it's already being
  369. * processed by the cpuworker, it won't be in the list anymore; but
  370. * when the cpuworker returns it, the circuit will be gone, and the
  371. * cpuworker response will be dropped).
  372. *
  373. * Then mark the circuit for close (which marks all edges for close,
  374. * and passes the destroy cell onward if necessary).
  375. */
  376. static void
  377. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  378. {
  379. circuit_t *circ;
  380. int reason;
  381. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  382. reason = (uint8_t)cell->payload[0];
  383. if (!circ) {
  384. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  385. cell->circ_id, conn->_base.address, conn->_base.port);
  386. return;
  387. }
  388. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  389. if (!CIRCUIT_IS_ORIGIN(circ) &&
  390. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  391. /* the destroy came from behind */
  392. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  393. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  394. } else { /* the destroy came from ahead */
  395. circuit_set_n_circid_orconn(circ, 0, NULL);
  396. if (CIRCUIT_IS_ORIGIN(circ)) {
  397. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  398. } else {
  399. char payload[1];
  400. log_debug(LD_OR, "Delivering 'truncated' back.");
  401. payload[0] = (char)reason;
  402. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  403. payload, sizeof(payload), NULL);
  404. }
  405. }
  406. }
  407. /** Process a 'versions' cell. The current link protocol version must be 0
  408. * to indicate that no version has yet been negotiated. DOCDOC say more. */
  409. static void
  410. command_process_versions_cell(var_cell_t *cell, or_connection_t *conn)
  411. {
  412. int highest_supported_version = 0;
  413. const char *cp, *end;
  414. if (conn->link_proto != 0 ||
  415. conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING ||
  416. (conn->handshake_state && conn->handshake_state->received_versions)) {
  417. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  418. "Received a VERSIONS cell on a connection with its version "
  419. "already set to %d; dropping", (int) conn->link_proto);
  420. return;
  421. }
  422. tor_assert(conn->handshake_state);
  423. end = cell->payload + cell->payload_len;
  424. for (cp = cell->payload; cp+1 < end; ++cp) {
  425. uint16_t v = ntohs(get_uint16(cp));
  426. if (v == 1 || v == 2) {
  427. if (v > highest_supported_version)
  428. highest_supported_version = v;
  429. }
  430. }
  431. if (!highest_supported_version) {
  432. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  433. "Couldn't find a version in common between my version list and the "
  434. "list in the VERSIONS cell; closing connection.");
  435. connection_mark_for_close(TO_CONN(conn));
  436. return;
  437. }
  438. conn->link_proto = highest_supported_version;
  439. conn->handshake_state->received_versions = 1;
  440. if (highest_supported_version >= 2) {
  441. if (connection_or_send_netinfo(conn) < 0 ||
  442. connection_or_send_cert(conn) < 0) {
  443. connection_mark_for_close(TO_CONN(conn));
  444. return;
  445. }
  446. if (conn->handshake_state->started_here)
  447. connection_or_send_link_auth(conn);
  448. } else {
  449. /* XXXX020 finish v1 verification. */
  450. }
  451. }
  452. /** Process a 'netinfo' cell. DOCDOC say more. */
  453. static void
  454. command_process_netinfo_cell(cell_t *cell, or_connection_t *conn)
  455. {
  456. time_t timestamp;
  457. uint8_t my_addr_type;
  458. uint8_t my_addr_len;
  459. const char *my_addr_ptr;
  460. const char *cp, *end;
  461. uint8_t n_other_addrs;
  462. time_t now = time(NULL);
  463. if (conn->link_proto < 2) {
  464. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  465. "Received a NETINFO cell on %s connection; dropping.",
  466. conn->link_proto == 0 ? "non-versioned" : "a v1");
  467. return;
  468. }
  469. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  470. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  471. "Received a NETINFO cell on a non-handshaking; dropping.");
  472. return;
  473. }
  474. tor_assert(conn->handshake_state &&
  475. conn->handshake_state->received_versions);
  476. if (conn->handshake_state->received_netinfo) {
  477. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  478. "Received a duplicate NETINFO cell; dropping.");
  479. return;
  480. }
  481. /* Decode the cell. */
  482. timestamp = ntohl(get_uint32(cell->payload));
  483. if (abs(now - conn->handshake_state->sent_versions_at) < 180) {
  484. conn->handshake_state->apparent_skew = now - timestamp;
  485. }
  486. my_addr_type = (uint8_t) cell->payload[4];
  487. my_addr_len = (uint8_t) cell->payload[5];
  488. my_addr_ptr = cell->payload + 6;
  489. end = cell->payload + CELL_PAYLOAD_SIZE;
  490. cp = cell->payload + 6 + my_addr_len;
  491. if (cp >= end) {
  492. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  493. "Addresses too long in netinfo cell; closing connection.");
  494. connection_mark_for_close(TO_CONN(conn));
  495. return;
  496. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  497. conn->handshake_state->my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  498. }
  499. n_other_addrs = (uint8_t) *cp++;
  500. while (n_other_addrs && cp < end-2) {
  501. /* Consider all the other addresses; if any matches, this connection is
  502. * "canonical." */
  503. uint8_t other_addr_type = (uint8_t) *cp++;
  504. uint8_t other_addr_len = (uint8_t) *cp++;
  505. if (cp + other_addr_len >= end) {
  506. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  507. "Address too long in netinfo cell; closing connection.");
  508. connection_mark_for_close(TO_CONN(conn));
  509. return;
  510. }
  511. if (other_addr_type == RESOLVED_TYPE_IPV4 && other_addr_len == 4) {
  512. uint32_t addr = ntohl(get_uint32(cp));
  513. if (addr == conn->real_addr) {
  514. conn->handshake_state->apparently_canonical = 1;
  515. break;
  516. }
  517. }
  518. cp += other_addr_len;
  519. --n_other_addrs;
  520. }
  521. conn->handshake_state->received_netinfo = 1;
  522. }
  523. /*XXXX020 move to connection_or.c */
  524. /** DOCDOC Called when we're done authenticating; act on stuff we
  525. * learned in netinfo. */
  526. int
  527. connection_or_act_on_netinfo(or_connection_t *conn)
  528. {
  529. long delta;
  530. if (!conn->handshake_state)
  531. return -1;
  532. tor_assert(conn->handshake_state->authenticated != 0);
  533. delta = conn->handshake_state->apparent_skew;
  534. /*XXXX020 magic number 3600 */
  535. if (abs(delta) > 3600 &&
  536. router_get_by_digest(conn->identity_digest)) {
  537. char dbuf[64];
  538. /*XXXX020 not always warn!*/
  539. format_time_interval(dbuf, sizeof(dbuf), delta);
  540. log_fn(LOG_WARN, LD_HTTP, "Received NETINFO cell with skewed time from "
  541. "server at %s:%d. It seems that our clock is %s by %s, or "
  542. "that theirs is %s. Tor requires an accurate clock to work: "
  543. "please check your time and date settings.",
  544. conn->_base.address, (int)conn->_base.port,
  545. delta>0 ? "ahead" : "behind", dbuf,
  546. delta>0 ? "behind" : "ahead");
  547. control_event_general_status(LOG_WARN,
  548. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  549. delta, conn->_base.address, conn->_base.port);
  550. }
  551. /* XXX020 possibly, learn my address from my_apparent_addr */
  552. if (conn->handshake_state->apparently_canonical) {
  553. conn->is_canonical = 1;
  554. }
  555. return 0;
  556. }
  557. /*DOCDOC*/
  558. static void
  559. command_process_cert_cell(var_cell_t *cell, or_connection_t *conn)
  560. {
  561. int n_certs = 0;
  562. uint16_t conn_cert_len = 0, id_cert_len = 0;
  563. const char *conn_cert = NULL, *id_cert = NULL;
  564. const char *cp, *end;
  565. int done = 0;
  566. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  567. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got CERT cell when not handshaking. "
  568. "Ignoring.");
  569. return;
  570. }
  571. tor_assert(conn->handshake_state);
  572. if (!conn->handshake_state->received_versions ||
  573. !conn->handshake_state->received_netinfo) {
  574. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got CERT cell before VERSIONS and "
  575. "NETINFO. Closing the connection.");
  576. goto err;
  577. }
  578. if (conn->handshake_state->received_certs) {
  579. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got duplicate CERT cell. "
  580. "Closing the connection.");
  581. goto err;
  582. }
  583. cp = cell->payload;
  584. end = cell->payload + cell->payload_len;
  585. while (cp < end) {
  586. uint16_t len;
  587. if (end-cp == 1)
  588. goto err;
  589. len = ntohs(get_uint16(cp));
  590. cp += 2;
  591. if (end-cp < len)
  592. goto err;
  593. if (n_certs == 0) {
  594. id_cert = cp;
  595. id_cert_len = len;
  596. } else if (n_certs == 1) {
  597. conn_cert = id_cert;
  598. conn_cert_len = id_cert_len;
  599. id_cert = cp;
  600. id_cert_len = len;
  601. } else {
  602. goto err;
  603. }
  604. cp += len;
  605. ++n_certs;
  606. }
  607. /* Now we have 0, 1, or 2 certs. */
  608. if (n_certs == 0) {
  609. /* The other side is unauthenticated. */
  610. done = 1;
  611. } else {
  612. int r;
  613. r = tor_tls_verify_certs_v2(LOG_PROTOCOL_WARN, conn->tls,
  614. conn_cert, conn_cert_len,
  615. id_cert, id_cert_len,
  616. &conn->handshake_state->signing_key,
  617. (conn->handshake_state->started_here ?
  618. conn->handshake_state->server_cert_digest :
  619. conn->handshake_state->client_cert_digest),
  620. &conn->handshake_state->identity_key,
  621. conn->handshake_state->cert_id_digest);
  622. if (r < 0)
  623. goto err;
  624. if (r == 1) {
  625. done = 1;
  626. conn->handshake_state->authenticated = 1;
  627. }
  628. }
  629. conn->handshake_state->received_certs = 1;
  630. if (done) {
  631. if (connection_or_finish_or_handshake(conn) < 0)
  632. goto err;
  633. }
  634. if (! conn->handshake_state->signing_key)
  635. goto err;
  636. return;
  637. err:
  638. connection_mark_for_close(TO_CONN(conn));
  639. }
  640. #define LINK_AUTH_STRING "Tor initiator certificate verification"
  641. /** DOCDOC */
  642. static void
  643. command_process_link_auth_cell(cell_t *cell, or_connection_t *conn)
  644. {
  645. or_handshake_state_t *s;
  646. char hmac[DIGEST_LEN];
  647. uint16_t len;
  648. size_t sig_len;
  649. const char *sig;
  650. char *checked = NULL;
  651. int checked_len;
  652. tor_assert(conn);
  653. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  654. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  655. "Received a LINK_AUTH cell on connection in the wrong state; "
  656. "dropping.");
  657. return;
  658. }
  659. s = conn->handshake_state;
  660. tor_assert(s);
  661. if (s->started_here) {
  662. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  663. "Got a LINK_AUTH cell from a server; closing the connection.");
  664. goto err;
  665. }
  666. if (!s->received_netinfo || !s->received_versions || !s->received_certs) {
  667. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got a LINK_AUTH cell too early; "
  668. "closing the connection");
  669. goto err;
  670. }
  671. len = ntohs(get_uint16(cell->payload));
  672. if (len < 2 || len > CELL_PAYLOAD_SIZE - 2) {
  673. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad length field (%d) on LINK_AUTH cell;"
  674. " closing the connection", (int)len);
  675. goto err;
  676. }
  677. if (cell->payload[2] != 0x00) {
  678. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unrecognized LINK_AUTH signature "
  679. "version; closing the connection");
  680. goto err;
  681. }
  682. connection_or_compute_link_auth_hmac(conn, hmac);
  683. tor_assert(s->signing_key);
  684. sig = cell->payload+3;
  685. sig_len = len-1;
  686. checked = tor_malloc(crypto_pk_keysize(s->signing_key));
  687. checked_len = crypto_pk_public_checksig(s->signing_key,checked,sig,sig_len);
  688. if (checked_len < 0) {
  689. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad signature on LINK_AUTH cell; "
  690. "closing the connection");
  691. goto err;
  692. }
  693. if (checked_len != DIGEST_LEN) {
  694. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad length (%d) of signed material in "
  695. "LINK_AUTH cell; closing the connection", checked_len);
  696. goto err;
  697. }
  698. if (memcmp(checked, hmac, DIGEST_LEN) != 0) {
  699. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad signed data in LINK_AUTH cell; "
  700. "closing the connection.");
  701. goto err;
  702. }
  703. s->authenticated = 1;
  704. if (connection_or_finish_or_handshake(conn)<0)
  705. goto err;
  706. tor_free(checked);
  707. return;
  708. err:
  709. tor_free(checked);
  710. connection_mark_for_close(TO_CONN(conn));
  711. }