command.c 22 KB

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