command.c 25 KB

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