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-2013, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Functions for processing incoming cells.
12 /* In-points to command.c:
14 * - command_process_cell(), called from
15 * incoming cell handlers of channel_t instances;
16 * callbacks registered in command_setup_channel(),
17 * called when channels are created in circuitbuild.c
21 #include "circuitbuild.h"
22 #include "circuitlist.h"
24 #include "connection.h"
25 #include "connection_or.h"
28 #include "cpuworker.h"
29 #include "hibernate.h"
35 #include "routerlist.h"
37 /** How many CELL_CREATE cells have we received, ever? */
38 uint64_t stats_n_create_cells_processed
= 0;
39 /** How many CELL_CREATED cells have we received, ever? */
40 uint64_t stats_n_created_cells_processed
= 0;
41 /** How many CELL_RELAY cells have we received, ever? */
42 uint64_t stats_n_relay_cells_processed
= 0;
43 /** How many CELL_DESTROY cells have we received, ever? */
44 uint64_t stats_n_destroy_cells_processed
= 0;
46 /* Handle an incoming channel */
47 static void command_handle_incoming_channel(channel_listener_t
*listener
,
50 /* These are the main functions for processing cells */
51 static void command_process_create_cell(cell_t
*cell
, channel_t
*chan
);
52 static void command_process_created_cell(cell_t
*cell
, channel_t
*chan
);
53 static void command_process_relay_cell(cell_t
*cell
, channel_t
*chan
);
54 static void command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
);
56 /** Convert the cell <b>command</b> into a lower-case, human-readable
59 cell_command_to_string(uint8_t command
)
62 case CELL_PADDING
: return "padding";
63 case CELL_CREATE
: return "create";
64 case CELL_CREATED
: return "created";
65 case CELL_RELAY
: return "relay";
66 case CELL_DESTROY
: return "destroy";
67 case CELL_CREATE_FAST
: return "create_fast";
68 case CELL_CREATED_FAST
: return "created_fast";
69 case CELL_VERSIONS
: return "versions";
70 case CELL_NETINFO
: return "netinfo";
71 case CELL_RELAY_EARLY
: return "relay_early";
72 case CELL_CREATE2
: return "create2";
73 case CELL_CREATED2
: return "created2";
74 case CELL_VPADDING
: return "vpadding";
75 case CELL_CERTS
: return "certs";
76 case CELL_AUTH_CHALLENGE
: return "auth_challenge";
77 case CELL_AUTHENTICATE
: return "authenticate";
78 case CELL_AUTHORIZE
: return "authorize";
79 default: return "unrecognized";
83 #ifdef KEEP_TIMING_STATS
84 /** This is a wrapper function around the actual function that processes the
85 * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
86 * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
89 command_time_process_cell(cell_t
*cell
, channel_t
*chan
, int *time
,
90 void (*func
)(cell_t
*, channel_t
*))
92 struct timeval start
, end
;
95 tor_gettimeofday(&start
);
99 tor_gettimeofday(&end
);
100 time_passed
= tv_udiff(&start
, &end
) ;
102 if (time_passed
> 10000) { /* more than 10ms */
103 log_debug(LD_OR
,"That call just took %ld ms.",time_passed
/1000);
105 if (time_passed
< 0) {
106 log_info(LD_GENERAL
,"That call took us back in time!");
109 *time
+= time_passed
;
113 /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
114 * statistics about how many of each cell we've processed so far
115 * this second, and the total number of microseconds it took to
116 * process each type of cell.
119 command_process_cell(channel_t
*chan
, cell_t
*cell
)
121 #ifdef KEEP_TIMING_STATS
122 /* how many of each cell have we seen so far this second? needs better
124 static int num_create
=0, num_created
=0, num_relay
=0, num_destroy
=0;
125 /* how long has it taken to process each type of cell? */
126 static int create_time
=0, created_time
=0, relay_time
=0, destroy_time
=0;
127 static time_t current_second
= 0; /* from previous calls to time */
129 time_t now
= time(NULL
);
131 if (now
> current_second
) { /* the second has rolled over */
134 "At end of second: %d creates (%d ms), %d createds (%d ms), "
135 "%d relays (%d ms), %d destroys (%d ms)",
136 num_create
, create_time
/1000,
137 num_created
, created_time
/1000,
138 num_relay
, relay_time
/1000,
139 num_destroy
, destroy_time
/1000);
142 num_create
= num_created
= num_relay
= num_destroy
= 0;
143 create_time
= created_time
= relay_time
= destroy_time
= 0;
145 /* remember which second it is, for next time */
146 current_second
= now
;
150 #ifdef KEEP_TIMING_STATS
151 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
153 command_time_process_cell(cl, cn, & tp ## time , \
154 command_process_ ## tp ## _cell); \
157 #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
160 switch (cell
->command
) {
162 case CELL_CREATE_FAST
:
164 ++stats_n_create_cells_processed
;
165 PROCESS_CELL(create
, cell
, chan
);
168 case CELL_CREATED_FAST
:
170 ++stats_n_created_cells_processed
;
171 PROCESS_CELL(created
, cell
, chan
);
174 case CELL_RELAY_EARLY
:
175 ++stats_n_relay_cells_processed
;
176 PROCESS_CELL(relay
, cell
, chan
);
179 ++stats_n_destroy_cells_processed
;
180 PROCESS_CELL(destroy
, cell
, chan
);
183 log_fn(LOG_INFO
, LD_PROTOCOL
,
184 "Cell of unknown or unexpected type (%d) received. "
191 /** Process an incoming var_cell from a channel; in the current protocol all
192 * the var_cells are handshake-related and handled below the channel layer,
193 * so this just logs a warning and drops the cell.
197 command_process_var_cell(channel_t
*chan
, var_cell_t
*var_cell
)
200 tor_assert(var_cell
);
202 log_info(LD_PROTOCOL
,
203 "Received unexpected var_cell above the channel layer of type %d"
208 /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
209 * new circuit with the p_circ_id specified in cell. Put the circuit in state
210 * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
211 * picked up again when the cpuworker finishes decrypting it.
214 command_process_create_cell(cell_t
*cell
, channel_t
*chan
)
217 const or_options_t
*options
= get_options();
219 create_cell_t
*create_cell
;
225 "Got a CREATE cell for circ_id %u on channel " U64_FORMAT
227 (unsigned)cell
->circ_id
,
228 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
230 /* We check for the conditions that would make us drop the cell before
231 * we check for the conditions that would make us send a DESTROY back,
232 * since those conditions would make a DESTROY nonsensical. */
233 if (cell
->circ_id
== 0) {
234 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
235 "Received a create cell (type %d) from %s with zero circID; "
236 " ignoring.", (int)cell
->command
,
237 channel_get_actual_remote_descr(chan
));
241 if (circuit_id_in_use_on_channel(cell
->circ_id
, chan
)) {
242 const node_t
*node
= node_get_by_id(chan
->identity_digest
);
243 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
244 "Received CREATE cell (circID %u) for known circ. "
245 "Dropping (age %d).",
246 (unsigned)cell
->circ_id
,
247 (int)(time(NULL
) - channel_when_created(chan
)));
249 char *p
= esc_for_log(node_get_platform(node
));
250 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
251 "Details: router %s, platform %s.",
252 node_describe(node
), p
);
258 if (we_are_hibernating()) {
260 "Received create cell but we're shutting down. Sending back "
262 channel_send_destroy(cell
->circ_id
, chan
,
263 END_CIRC_REASON_HIBERNATING
);
267 if (!server_mode(options
) ||
268 (!public_server_mode(options
) && channel_is_outgoing(chan
))) {
269 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
270 "Received create cell (type %d) from %s, but we're connected "
271 "to it as a client. "
272 "Sending back a destroy.",
273 (int)cell
->command
, channel_get_canonical_remote_descr(chan
));
274 channel_send_destroy(cell
->circ_id
, chan
,
275 END_CIRC_REASON_TORPROTOCOL
);
279 /* If the high bit of the circuit ID is not as expected, close the
281 if (chan
->wide_circ_ids
)
282 id_is_high
= cell
->circ_id
& (1u<<31);
284 id_is_high
= cell
->circ_id
& (1u<<15);
286 chan
->circ_id_type
== CIRC_ID_TYPE_HIGHER
) ||
288 chan
->circ_id_type
== CIRC_ID_TYPE_LOWER
)) {
289 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
290 "Received create cell with unexpected circ_id %u. Closing.",
291 (unsigned)cell
->circ_id
);
292 channel_send_destroy(cell
->circ_id
, chan
,
293 END_CIRC_REASON_TORPROTOCOL
);
297 circ
= or_circuit_new(cell
->circ_id
, chan
);
298 circ
->base_
.purpose
= CIRCUIT_PURPOSE_OR
;
299 circuit_set_state(TO_CIRCUIT(circ
), CIRCUIT_STATE_ONIONSKIN_PENDING
);
300 create_cell
= tor_malloc_zero(sizeof(create_cell_t
));
301 if (create_cell_parse(create_cell
, cell
) < 0) {
302 tor_free(create_cell
);
303 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
304 "Bogus/unrecognized create cell; closing.");
305 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
309 if (create_cell
->handshake_type
!= ONION_HANDSHAKE_TYPE_FAST
) {
310 /* hand it off to the cpuworkers, and then return. */
311 if (connection_or_digest_is_known_relay(chan
->identity_digest
))
312 rep_hist_note_circuit_handshake_requested(create_cell
->handshake_type
);
313 if (assign_onionskin_to_cpuworker(NULL
, circ
, create_cell
) < 0) {
314 log_debug(LD_GENERAL
,"Failed to hand off onionskin. Closing.");
315 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_RESOURCELIMIT
);
318 log_debug(LD_OR
,"success: handed off onionskin.");
320 /* This is a CREATE_FAST cell; we can handle it immediately without using
322 uint8_t keys
[CPATH_KEY_MATERIAL_LEN
];
323 uint8_t rend_circ_nonce
[DIGEST_LEN
];
325 created_cell_t created_cell
;
327 /* Make sure we never try to use the OR connection on which we
328 * received this cell to satisfy an EXTEND request, */
329 channel_mark_client(chan
);
331 memset(&created_cell
, 0, sizeof(created_cell
));
332 len
= onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST
,
333 create_cell
->onionskin
,
334 create_cell
->handshake_len
,
337 keys
, CPATH_KEY_MATERIAL_LEN
,
339 tor_free(create_cell
);
341 log_warn(LD_OR
,"Failed to generate key material. Closing.");
342 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
343 tor_free(create_cell
);
346 created_cell
.cell_type
= CELL_CREATED_FAST
;
347 created_cell
.handshake_len
= len
;
349 if (onionskin_answer(circ
, &created_cell
,
350 (const char *)keys
, rend_circ_nonce
)<0) {
351 log_warn(LD_OR
,"Failed to reply to CREATE_FAST cell. Closing.");
352 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
355 memwipe(keys
, 0, sizeof(keys
));
359 /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
361 * that it's intended for. If we're not the origin of the circuit, package
362 * the 'created' cell in an 'extended' relay cell and pass it back. If we
363 * are the origin of the circuit, send it to circuit_finish_handshake() to
364 * finish processing keys, and then call circuit_send_next_onion_skin() to
365 * extend to the next hop in the circuit if necessary.
368 command_process_created_cell(cell_t
*cell
, channel_t
*chan
)
371 extended_cell_t extended_cell
;
373 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
377 "(circID %u) unknown circ (probably got a destroy earlier). "
378 "Dropping.", (unsigned)cell
->circ_id
);
382 if (circ
->n_circ_id
!= cell
->circ_id
|| circ
->n_chan
!= chan
) {
383 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,
384 "got created cell from Tor client? Closing.");
385 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
389 if (created_cell_parse(&extended_cell
.created_cell
, cell
) < 0) {
390 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Unparseable created cell.");
391 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
395 if (CIRCUIT_IS_ORIGIN(circ
)) { /* we're the OP. Handshake this. */
396 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
398 log_debug(LD_OR
,"at OP. Finishing handshake.");
399 if ((err_reason
= circuit_finish_handshake(origin_circ
,
400 &extended_cell
.created_cell
)) < 0) {
401 log_warn(LD_OR
,"circuit_finish_handshake failed.");
402 circuit_mark_for_close(circ
, -err_reason
);
405 log_debug(LD_OR
,"Moving to next skin.");
406 if ((err_reason
= circuit_send_next_onion_skin(origin_circ
)) < 0) {
407 log_info(LD_OR
,"circuit_send_next_onion_skin failed.");
408 /* XXX push this circuit_close lower */
409 circuit_mark_for_close(circ
, -err_reason
);
412 } else { /* pack it into an extended relay cell, and send it. */
415 uint8_t payload
[RELAY_PAYLOAD_SIZE
];
417 "Converting created cell to extended relay cell, sending.");
418 memset(payload
, 0, sizeof(payload
));
419 if (extended_cell
.created_cell
.cell_type
== CELL_CREATED2
)
420 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED2
;
422 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED
;
423 if (extended_cell_format(&command
, &len
, payload
, &extended_cell
) < 0) {
424 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Can't format extended cell.");
425 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
429 relay_send_command_from_edge(0, circ
, command
,
430 (const char*)payload
, len
, NULL
);
434 /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
435 * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
436 * circuit_receive_relay_cell() for actual processing.
439 command_process_relay_cell(cell_t
*cell
, channel_t
*chan
)
442 int reason
, direction
;
444 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
448 "unknown circuit %u on connection from %s. Dropping.",
449 (unsigned)cell
->circ_id
,
450 channel_get_canonical_remote_descr(chan
));
454 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
455 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit in create_wait. Closing.");
456 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
460 if (CIRCUIT_IS_ORIGIN(circ
)) {
461 /* if we're a relay and treating connections with recent local
462 * traffic better, then this is one of them. */
463 channel_timestamp_client(chan
);
466 if (!CIRCUIT_IS_ORIGIN(circ
) &&
467 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
468 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
)
469 direction
= CELL_DIRECTION_OUT
;
471 direction
= CELL_DIRECTION_IN
;
473 /* If we have a relay_early cell, make sure that it's outbound, and we've
474 * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
475 if (cell
->command
== CELL_RELAY_EARLY
) {
476 if (direction
== CELL_DIRECTION_IN
) {
477 /* Inbound early cells could once be encountered as a result of
478 * bug 1038; but relays running versions before 0.2.1.19 are long
479 * gone from the network, so any such cells now are surprising. */
481 "Received an inbound RELAY_EARLY cell on circuit %u."
482 " Closing circuit. Please report this event,"
483 " along with the following message.",
484 (unsigned)cell
->circ_id
);
485 if (CIRCUIT_IS_ORIGIN(circ
)) {
486 circuit_log_path(LOG_WARN
, LD_OR
, TO_ORIGIN_CIRCUIT(circ
));
487 } else if (circ
->n_chan
) {
488 log_warn(LD_OR
, " upstream=%s",
489 channel_get_actual_remote_descr(circ
->n_chan
));
491 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
494 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
495 if (or_circ
->remaining_relay_early_cells
== 0) {
496 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
497 "Received too many RELAY_EARLY cells on circ %u from %s."
499 (unsigned)cell
->circ_id
,
500 safe_str(channel_get_canonical_remote_descr(chan
)));
501 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
504 --or_circ
->remaining_relay_early_cells
;
508 if ((reason
= circuit_receive_relay_cell(cell
, circ
, direction
)) < 0) {
509 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit_receive_relay_cell "
510 "(%s) failed. Closing.",
511 direction
==CELL_DIRECTION_OUT
?"forward":"backward");
512 circuit_mark_for_close(circ
, -reason
);
516 /** Process a 'destroy' <b>cell</b> that just arrived from
517 * <b>chan</b>. Find the circ that it refers to (if any).
519 * If the circ is in state
520 * onionskin_pending, then call onion_pending_remove() to remove it
521 * from the pending onion list (note that if it's already being
522 * processed by the cpuworker, it won't be in the list anymore; but
523 * when the cpuworker returns it, the circuit will be gone, and the
524 * cpuworker response will be dropped).
526 * Then mark the circuit for close (which marks all edges for close,
527 * and passes the destroy cell onward if necessary).
530 command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
)
535 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
537 log_info(LD_OR
,"unknown circuit %u on connection from %s. Dropping.",
538 (unsigned)cell
->circ_id
,
539 channel_get_canonical_remote_descr(chan
));
542 log_debug(LD_OR
,"Received for circID %u.",(unsigned)cell
->circ_id
);
544 reason
= (uint8_t)cell
->payload
[0];
545 circ
->received_destroy
= 1;
547 if (!CIRCUIT_IS_ORIGIN(circ
) &&
548 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
549 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
) {
550 /* the destroy came from behind */
551 circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ
), 0, NULL
);
552 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
553 } else { /* the destroy came from ahead */
554 circuit_set_n_circid_chan(circ
, 0, NULL
);
555 if (CIRCUIT_IS_ORIGIN(circ
)) {
556 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
559 log_debug(LD_OR
, "Delivering 'truncated' back.");
560 payload
[0] = (char)reason
;
561 relay_send_command_from_edge(0, circ
, RELAY_COMMAND_TRUNCATED
,
562 payload
, sizeof(payload
), NULL
);
567 /** Callback to handle a new channel; call command_setup_channel() to give
568 * it the right cell handlers.
572 command_handle_incoming_channel(channel_listener_t
*listener
, channel_t
*chan
)
574 tor_assert(listener
);
577 command_setup_channel(chan
);
580 /** Given a channel, install the right handlers to process incoming
585 command_setup_channel(channel_t
*chan
)
589 channel_set_cell_handlers(chan
,
590 command_process_cell
,
591 command_process_var_cell
);
594 /** Given a listener, install the right handler to process incoming
599 command_setup_listener(channel_listener_t
*listener
)
601 tor_assert(listener
);
602 tor_assert(listener
->state
== CHANNEL_LISTENER_STATE_LISTENING
);
604 channel_listener_set_listener_fn(listener
, command_handle_incoming_channel
);