command.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2010, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file command.c
  8. * \brief Functions for processing incoming cells.
  9. **/
  10. /* In-points to command.c:
  11. *
  12. * - command_process_cell(), called from
  13. * connection_or_process_cells_from_inbuf() in connection_or.c
  14. */
  15. #include "or.h"
  16. #include "circuitbuild.h"
  17. #include "circuitlist.h"
  18. #include "command.h"
  19. #include "connection.h"
  20. #include "connection_or.h"
  21. #include "config.h"
  22. #include "router.h"
  23. #include "routerlist.h"
  24. /** How many CELL_PADDING cells have we received, ever? */
  25. uint64_t stats_n_padding_cells_processed = 0;
  26. /** How many CELL_CREATE cells have we received, ever? */
  27. uint64_t stats_n_create_cells_processed = 0;
  28. /** How many CELL_CREATED cells have we received, ever? */
  29. uint64_t stats_n_created_cells_processed = 0;
  30. /** How many CELL_RELAY cells have we received, ever? */
  31. uint64_t stats_n_relay_cells_processed = 0;
  32. /** How many CELL_DESTROY cells have we received, ever? */
  33. uint64_t stats_n_destroy_cells_processed = 0;
  34. /** How many CELL_VERSIONS cells have we received, ever? */
  35. uint64_t stats_n_versions_cells_processed = 0;
  36. /** How many CELL_NETINFO cells have we received, ever? */
  37. uint64_t stats_n_netinfo_cells_processed = 0;
  38. /* These are the main functions for processing cells */
  39. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  40. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  41. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  42. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  43. static void command_process_versions_cell(var_cell_t *cell,
  44. or_connection_t *conn);
  45. static void command_process_netinfo_cell(cell_t *cell, or_connection_t *conn);
  46. #ifdef KEEP_TIMING_STATS
  47. /** This is a wrapper function around the actual function that processes the
  48. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  49. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  50. */
  51. static void
  52. command_time_process_cell(cell_t *cell, or_connection_t *conn, int *time,
  53. void (*func)(cell_t *, or_connection_t *))
  54. {
  55. struct timeval start, end;
  56. long time_passed;
  57. tor_gettimeofday(&start);
  58. (*func)(cell, conn);
  59. tor_gettimeofday(&end);
  60. time_passed = tv_udiff(&start, &end) ;
  61. if (time_passed > 10000) { /* more than 10ms */
  62. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  63. }
  64. if (time_passed < 0) {
  65. log_info(LD_GENERAL,"That call took us back in time!");
  66. time_passed = 0;
  67. }
  68. *time += time_passed;
  69. }
  70. #endif
  71. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  72. * statistics about how many of each cell we've processed so far
  73. * this second, and the total number of microseconds it took to
  74. * process each type of cell.
  75. */
  76. void
  77. command_process_cell(cell_t *cell, or_connection_t *conn)
  78. {
  79. int handshaking = (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING);
  80. #ifdef KEEP_TIMING_STATS
  81. /* how many of each cell have we seen so far this second? needs better
  82. * name. */
  83. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  84. /* how long has it taken to process each type of cell? */
  85. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  86. static time_t current_second = 0; /* from previous calls to time */
  87. time_t now = time(NULL);
  88. if (now > current_second) { /* the second has rolled over */
  89. /* print stats */
  90. log_info(LD_OR,
  91. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  92. "%d relays (%d ms), %d destroys (%d ms)",
  93. num_create, create_time/1000,
  94. num_created, created_time/1000,
  95. num_relay, relay_time/1000,
  96. num_destroy, destroy_time/1000);
  97. /* zero out stats */
  98. num_create = num_created = num_relay = num_destroy = 0;
  99. create_time = created_time = relay_time = destroy_time = 0;
  100. /* remember which second it is, for next time */
  101. current_second = now;
  102. }
  103. #endif
  104. #ifdef KEEP_TIMING_STATS
  105. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  106. ++num ## tp; \
  107. command_time_process_cell(cl, cn, & tp ## time , \
  108. command_process_ ## tp ## _cell); \
  109. } STMT_END
  110. #else
  111. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  112. #endif
  113. /* Reject all but VERSIONS and NETINFO when handshaking. */
  114. if (handshaking && cell->command != CELL_VERSIONS &&
  115. cell->command != CELL_NETINFO)
  116. return;
  117. switch (cell->command) {
  118. case CELL_PADDING:
  119. ++stats_n_padding_cells_processed;
  120. /* do nothing */
  121. break;
  122. case CELL_CREATE:
  123. case CELL_CREATE_FAST:
  124. ++stats_n_create_cells_processed;
  125. PROCESS_CELL(create, cell, conn);
  126. break;
  127. case CELL_CREATED:
  128. case CELL_CREATED_FAST:
  129. ++stats_n_created_cells_processed;
  130. PROCESS_CELL(created, cell, conn);
  131. break;
  132. case CELL_RELAY:
  133. case CELL_RELAY_EARLY:
  134. ++stats_n_relay_cells_processed;
  135. PROCESS_CELL(relay, cell, conn);
  136. break;
  137. case CELL_DESTROY:
  138. ++stats_n_destroy_cells_processed;
  139. PROCESS_CELL(destroy, cell, conn);
  140. break;
  141. case CELL_VERSIONS:
  142. tor_fragile_assert();
  143. break;
  144. case CELL_NETINFO:
  145. ++stats_n_netinfo_cells_processed;
  146. PROCESS_CELL(netinfo, cell, conn);
  147. break;
  148. default:
  149. log_fn(LOG_INFO, LD_PROTOCOL,
  150. "Cell of unknown type (%d) received. Dropping.", cell->command);
  151. break;
  152. }
  153. }
  154. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  155. * statistics about how many of each cell we've processed so far
  156. * this second, and the total number of microseconds it took to
  157. * process each type of cell.
  158. */
  159. void
  160. command_process_var_cell(var_cell_t *cell, or_connection_t *conn)
  161. {
  162. #ifdef KEEP_TIMING_STATS
  163. /* how many of each cell have we seen so far this second? needs better
  164. * name. */
  165. static int num_versions=0, num_cert=0;
  166. time_t now = time(NULL);
  167. if (now > current_second) { /* the second has rolled over */
  168. /* print stats */
  169. log_info(LD_OR,
  170. "At end of second: %d versions (%d ms), %d cert (%d ms)",
  171. num_versions, versions_time/1000,
  172. cert, cert_time/1000);
  173. num_versions = num_cert = 0;
  174. versions_time = cert_time = 0;
  175. /* remember which second it is, for next time */
  176. current_second = now;
  177. }
  178. #endif
  179. /* reject all when not handshaking. */
  180. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING)
  181. return;
  182. switch (cell->command) {
  183. case CELL_VERSIONS:
  184. ++stats_n_versions_cells_processed;
  185. PROCESS_CELL(versions, cell, conn);
  186. break;
  187. default:
  188. log_warn(LD_BUG,
  189. "Variable-length cell of unknown type (%d) received.",
  190. cell->command);
  191. tor_fragile_assert();
  192. break;
  193. }
  194. }
  195. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  196. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  197. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  198. * picked up again when the cpuworker finishes decrypting it.
  199. */
  200. static void
  201. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  202. {
  203. or_circuit_t *circ;
  204. int id_is_high;
  205. if (we_are_hibernating()) {
  206. log_info(LD_OR,
  207. "Received create cell but we're shutting down. Sending back "
  208. "destroy.");
  209. connection_or_send_destroy(cell->circ_id, conn,
  210. END_CIRC_REASON_HIBERNATING);
  211. return;
  212. }
  213. if (!server_mode(get_options())) {
  214. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  215. "Received create cell (type %d) from %s:%d, but we're a client. "
  216. "Sending back a destroy.",
  217. (int)cell->command, conn->_base.address, conn->_base.port);
  218. connection_or_send_destroy(cell->circ_id, conn,
  219. END_CIRC_REASON_TORPROTOCOL);
  220. return;
  221. }
  222. /* If the high bit of the circuit ID is not as expected, close the
  223. * circ. */
  224. id_is_high = cell->circ_id & (1<<15);
  225. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  226. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  227. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  228. "Received create cell with unexpected circ_id %d. Closing.",
  229. cell->circ_id);
  230. connection_or_send_destroy(cell->circ_id, conn,
  231. END_CIRC_REASON_TORPROTOCOL);
  232. return;
  233. }
  234. if (circuit_id_in_use_on_orconn(cell->circ_id, conn)) {
  235. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  236. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  237. "Received CREATE cell (circID %d) for known circ. "
  238. "Dropping (age %d).",
  239. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  240. if (router)
  241. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  242. "Details: nickname \"%s\", platform %s.",
  243. router->nickname, escaped(router->platform));
  244. return;
  245. }
  246. circ = or_circuit_new(cell->circ_id, conn);
  247. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  248. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  249. if (cell->command == CELL_CREATE) {
  250. char *onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  251. memcpy(onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  252. /* hand it off to the cpuworkers, and then return. */
  253. if (assign_onionskin_to_cpuworker(NULL, circ, onionskin) < 0) {
  254. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  255. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  256. return;
  257. }
  258. log_debug(LD_OR,"success: handed off onionskin.");
  259. } else {
  260. /* This is a CREATE_FAST cell; we can handle it immediately without using
  261. * a CPU worker. */
  262. char keys[CPATH_KEY_MATERIAL_LEN];
  263. char reply[DIGEST_LEN*2];
  264. tor_assert(cell->command == CELL_CREATE_FAST);
  265. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  266. log_warn(LD_OR,"Failed to generate key material. Closing.");
  267. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  268. return;
  269. }
  270. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  271. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  272. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  273. return;
  274. }
  275. }
  276. }
  277. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  278. * Find the circuit
  279. * that it's intended for. If we're not the origin of the circuit, package
  280. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  281. * are the origin of the circuit, send it to circuit_finish_handshake() to
  282. * finish processing keys, and then call circuit_send_next_onion_skin() to
  283. * extend to the next hop in the circuit if necessary.
  284. */
  285. static void
  286. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  287. {
  288. circuit_t *circ;
  289. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  290. if (!circ) {
  291. log_info(LD_OR,
  292. "(circID %d) unknown circ (probably got a destroy earlier). "
  293. "Dropping.", cell->circ_id);
  294. return;
  295. }
  296. if (circ->n_circ_id != cell->circ_id) {
  297. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  298. "got created cell from Tor client? Closing.");
  299. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  300. return;
  301. }
  302. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  303. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  304. int err_reason = 0;
  305. log_debug(LD_OR,"at OP. Finishing handshake.");
  306. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  307. cell->payload)) < 0) {
  308. log_warn(LD_OR,"circuit_finish_handshake failed.");
  309. circuit_mark_for_close(circ, -err_reason);
  310. return;
  311. }
  312. log_debug(LD_OR,"Moving to next skin.");
  313. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  314. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  315. /* XXX push this circuit_close lower */
  316. circuit_mark_for_close(circ, -err_reason);
  317. return;
  318. }
  319. } else { /* pack it into an extended relay cell, and send it. */
  320. log_debug(LD_OR,
  321. "Converting created cell to extended relay cell, sending.");
  322. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  323. cell->payload, ONIONSKIN_REPLY_LEN,
  324. NULL);
  325. }
  326. }
  327. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  328. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  329. * circuit_receive_relay_cell() for actual processing.
  330. */
  331. static void
  332. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  333. {
  334. circuit_t *circ;
  335. int reason, direction;
  336. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  337. if (!circ) {
  338. log_debug(LD_OR,
  339. "unknown circuit %d on connection from %s:%d. Dropping.",
  340. cell->circ_id, conn->_base.address, conn->_base.port);
  341. return;
  342. }
  343. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  344. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  345. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  346. return;
  347. }
  348. if (CIRCUIT_IS_ORIGIN(circ)) {
  349. /* if we're a relay and treating connections with recent local
  350. * traffic better, then this is one of them. */
  351. conn->client_used = time(NULL);
  352. }
  353. if (!CIRCUIT_IS_ORIGIN(circ) &&
  354. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  355. direction = CELL_DIRECTION_OUT;
  356. else
  357. direction = CELL_DIRECTION_IN;
  358. /* If we have a relay_early cell, make sure that it's outbound, and we've
  359. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  360. if (cell->command == CELL_RELAY_EARLY) {
  361. if (direction == CELL_DIRECTION_IN) {
  362. /* Allow an unlimited number of inbound relay_early cells,
  363. * for hidden service compatibility. There isn't any way to make
  364. * a long circuit through inbound relay_early cells anyway. See
  365. * bug 1038. -RD */
  366. } else {
  367. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  368. if (or_circ->remaining_relay_early_cells == 0) {
  369. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  370. "Received too many RELAY_EARLY cells on circ %d from %s:%d."
  371. " Closing circuit.",
  372. cell->circ_id, safe_str(conn->_base.address),
  373. conn->_base.port);
  374. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  375. return;
  376. }
  377. --or_circ->remaining_relay_early_cells;
  378. }
  379. }
  380. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  381. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  382. "(%s) failed. Closing.",
  383. direction==CELL_DIRECTION_OUT?"forward":"backward");
  384. circuit_mark_for_close(circ, -reason);
  385. }
  386. }
  387. /** Process a 'destroy' <b>cell</b> that just arrived from
  388. * <b>conn</b>. Find the circ that it refers to (if any).
  389. *
  390. * If the circ is in state
  391. * onionskin_pending, then call onion_pending_remove() to remove it
  392. * from the pending onion list (note that if it's already being
  393. * processed by the cpuworker, it won't be in the list anymore; but
  394. * when the cpuworker returns it, the circuit will be gone, and the
  395. * cpuworker response will be dropped).
  396. *
  397. * Then mark the circuit for close (which marks all edges for close,
  398. * and passes the destroy cell onward if necessary).
  399. */
  400. static void
  401. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  402. {
  403. circuit_t *circ;
  404. int reason;
  405. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  406. reason = (uint8_t)cell->payload[0];
  407. if (!circ) {
  408. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  409. cell->circ_id, conn->_base.address, conn->_base.port);
  410. return;
  411. }
  412. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  413. if (!CIRCUIT_IS_ORIGIN(circ) &&
  414. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  415. /* the destroy came from behind */
  416. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  417. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  418. } else { /* the destroy came from ahead */
  419. circuit_set_n_circid_orconn(circ, 0, NULL);
  420. if (CIRCUIT_IS_ORIGIN(circ)) {
  421. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  422. } else {
  423. char payload[1];
  424. log_debug(LD_OR, "Delivering 'truncated' back.");
  425. payload[0] = (char)reason;
  426. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  427. payload, sizeof(payload), NULL);
  428. }
  429. }
  430. }
  431. /** Process a 'versions' cell. The current link protocol version must be 0
  432. * to indicate that no version has yet been negotiated. We compare the
  433. * versions in the cell to the list of versions we support, pick the
  434. * highest version we have in common, and continue the negotiation from
  435. * there.
  436. */
  437. static void
  438. command_process_versions_cell(var_cell_t *cell, or_connection_t *conn)
  439. {
  440. int highest_supported_version = 0;
  441. const char *cp, *end;
  442. if (conn->link_proto != 0 ||
  443. conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING ||
  444. (conn->handshake_state && conn->handshake_state->received_versions)) {
  445. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  446. "Received a VERSIONS cell on a connection with its version "
  447. "already set to %d; dropping", (int) conn->link_proto);
  448. return;
  449. }
  450. tor_assert(conn->handshake_state);
  451. end = cell->payload + cell->payload_len;
  452. for (cp = cell->payload; cp+1 < end; ++cp) {
  453. uint16_t v = ntohs(get_uint16(cp));
  454. if (is_or_protocol_version_known(v) && v > highest_supported_version)
  455. highest_supported_version = v;
  456. }
  457. if (!highest_supported_version) {
  458. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  459. "Couldn't find a version in common between my version list and the "
  460. "list in the VERSIONS cell; closing connection.");
  461. connection_mark_for_close(TO_CONN(conn));
  462. return;
  463. } else if (highest_supported_version == 1) {
  464. /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
  465. * cells. */
  466. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  467. "Used version negotiation protocol to negotiate a v1 connection. "
  468. "That's crazily non-compliant. Closing connection.");
  469. connection_mark_for_close(TO_CONN(conn));
  470. return;
  471. }
  472. conn->link_proto = highest_supported_version;
  473. conn->handshake_state->received_versions = 1;
  474. log_info(LD_OR, "Negotiated version %d with %s:%d; sending NETINFO.",
  475. highest_supported_version,
  476. safe_str_client(conn->_base.address),
  477. conn->_base.port);
  478. tor_assert(conn->link_proto >= 2);
  479. if (connection_or_send_netinfo(conn) < 0) {
  480. connection_mark_for_close(TO_CONN(conn));
  481. return;
  482. }
  483. }
  484. /** Process a 'netinfo' cell: read and act on its contents, and set the
  485. * connection state to "open". */
  486. static void
  487. command_process_netinfo_cell(cell_t *cell, or_connection_t *conn)
  488. {
  489. time_t timestamp;
  490. uint8_t my_addr_type;
  491. uint8_t my_addr_len;
  492. const char *my_addr_ptr;
  493. const char *cp, *end;
  494. uint8_t n_other_addrs;
  495. time_t now = time(NULL);
  496. long apparent_skew = 0;
  497. uint32_t my_apparent_addr = 0;
  498. if (conn->link_proto < 2) {
  499. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  500. "Received a NETINFO cell on %s connection; dropping.",
  501. conn->link_proto == 0 ? "non-versioned" : "a v1");
  502. return;
  503. }
  504. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  505. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  506. "Received a NETINFO cell on non-handshaking connection; dropping.");
  507. return;
  508. }
  509. tor_assert(conn->handshake_state &&
  510. conn->handshake_state->received_versions);
  511. /* Decode the cell. */
  512. timestamp = ntohl(get_uint32(cell->payload));
  513. if (labs(now - conn->handshake_state->sent_versions_at) < 180) {
  514. apparent_skew = now - timestamp;
  515. }
  516. my_addr_type = (uint8_t) cell->payload[4];
  517. my_addr_len = (uint8_t) cell->payload[5];
  518. my_addr_ptr = cell->payload + 6;
  519. end = cell->payload + CELL_PAYLOAD_SIZE;
  520. cp = cell->payload + 6 + my_addr_len;
  521. if (cp >= end) {
  522. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  523. "Addresses too long in netinfo cell; closing connection.");
  524. connection_mark_for_close(TO_CONN(conn));
  525. return;
  526. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  527. my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  528. }
  529. n_other_addrs = (uint8_t) *cp++;
  530. while (n_other_addrs && cp < end-2) {
  531. /* Consider all the other addresses; if any matches, this connection is
  532. * "canonical." */
  533. tor_addr_t addr;
  534. const char *next = decode_address_from_payload(&addr, cp, (int)(end-cp));
  535. if (next == NULL) {
  536. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  537. "Bad address in netinfo cell; closing connection.");
  538. connection_mark_for_close(TO_CONN(conn));
  539. return;
  540. }
  541. if (tor_addr_eq(&addr, &conn->real_addr)) {
  542. conn->is_canonical = 1;
  543. break;
  544. }
  545. cp = next;
  546. --n_other_addrs;
  547. }
  548. /* Act on apparent skew. */
  549. /** Warn when we get a netinfo skew with at least this value. */
  550. #define NETINFO_NOTICE_SKEW 3600
  551. if (labs(apparent_skew) > NETINFO_NOTICE_SKEW &&
  552. router_get_by_digest(conn->identity_digest)) {
  553. char dbuf[64];
  554. int severity;
  555. /*XXXX be smarter about when everybody says we are skewed. */
  556. if (router_digest_is_trusted_dir(conn->identity_digest))
  557. severity = LOG_WARN;
  558. else
  559. severity = LOG_INFO;
  560. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  561. log_fn(severity, LD_GENERAL, "Received NETINFO cell with skewed time from "
  562. "server at %s:%d. It seems that our clock is %s by %s, or "
  563. "that theirs is %s. Tor requires an accurate clock to work: "
  564. "please check your time and date settings.",
  565. conn->_base.address, (int)conn->_base.port,
  566. apparent_skew>0 ? "ahead" : "behind", dbuf,
  567. apparent_skew>0 ? "behind" : "ahead");
  568. if (severity == LOG_WARN) /* only tell the controller if an authority */
  569. control_event_general_status(LOG_WARN,
  570. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  571. apparent_skew,
  572. conn->_base.address, conn->_base.port);
  573. }
  574. /* XXX maybe act on my_apparent_addr, if the source is sufficiently
  575. * trustworthy. */
  576. if (connection_or_set_state_open(conn)<0)
  577. connection_mark_for_close(TO_CONN(conn));
  578. else
  579. log_info(LD_OR, "Got good NETINFO cell from %s:%d; OR connection is now "
  580. "open, using protocol version %d",
  581. safe_str_client(conn->_base.address),
  582. conn->_base.port, (int)conn->link_proto);
  583. assert_connection_ok(TO_CONN(conn),time(NULL));
  584. }