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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Functions for processing incoming cells.
11 * When we receive a cell from a client or a relay, it arrives on some
12 * channel, and tells us what to do with it. In this module, we dispatch based
13 * on the cell type using the functions command_process_cell() and
14 * command_process_var_cell(), and deal with the cell accordingly. (These
15 * handlers are installed on a channel with the command_setup_channel()
18 * Channels have a chance to handle some cell types on their own before they
19 * are ever passed here --- typically, they do this for cells that are
20 * specific to a given channel type. For example, in channeltls.c, the cells
21 * for the initial connection handshake are handled before we get here. (Of
22 * course, the fact that there _is_ only one channel type for now means that
23 * we may have gotten the factoring wrong here.)
25 * Handling other cell types is mainly farmed off to other modules, after
26 * initial sanity-checking. CREATE* cells are handled ultimately in onion.c,
27 * CREATED* cells trigger circuit creation in circuitbuild.c, DESTROY cells
28 * are handled here (since they're simple), and RELAY cells, in all their
29 * complexity, are passed off to relay.c.
32 /* In-points to command.c:
34 * - command_process_cell(), called from
35 * incoming cell handlers of channel_t instances;
36 * callbacks registered in command_setup_channel(),
37 * called when channels are created in circuitbuild.c
41 #include "circuitbuild.h"
42 #include "circuitlist.h"
44 #include "connection.h"
45 #include "connection_or.h"
48 #include "cpuworker.h"
49 #include "hibernate.h"
55 #include "routerlist.h"
57 /** How many CELL_CREATE cells have we received, ever? */
58 uint64_t stats_n_create_cells_processed
= 0;
59 /** How many CELL_CREATED cells have we received, ever? */
60 uint64_t stats_n_created_cells_processed
= 0;
61 /** How many CELL_RELAY cells have we received, ever? */
62 uint64_t stats_n_relay_cells_processed
= 0;
63 /** How many CELL_DESTROY cells have we received, ever? */
64 uint64_t stats_n_destroy_cells_processed
= 0;
66 /* Handle an incoming channel */
67 static void command_handle_incoming_channel(channel_listener_t
*listener
,
70 /* These are the main functions for processing cells */
71 static void command_process_create_cell(cell_t
*cell
, channel_t
*chan
);
72 static void command_process_created_cell(cell_t
*cell
, channel_t
*chan
);
73 static void command_process_relay_cell(cell_t
*cell
, channel_t
*chan
);
74 static void command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
);
76 /** Convert the cell <b>command</b> into a lower-case, human-readable
79 cell_command_to_string(uint8_t command
)
82 case CELL_PADDING
: return "padding";
83 case CELL_CREATE
: return "create";
84 case CELL_CREATED
: return "created";
85 case CELL_RELAY
: return "relay";
86 case CELL_DESTROY
: return "destroy";
87 case CELL_CREATE_FAST
: return "create_fast";
88 case CELL_CREATED_FAST
: return "created_fast";
89 case CELL_VERSIONS
: return "versions";
90 case CELL_NETINFO
: return "netinfo";
91 case CELL_RELAY_EARLY
: return "relay_early";
92 case CELL_CREATE2
: return "create2";
93 case CELL_CREATED2
: return "created2";
94 case CELL_VPADDING
: return "vpadding";
95 case CELL_CERTS
: return "certs";
96 case CELL_AUTH_CHALLENGE
: return "auth_challenge";
97 case CELL_AUTHENTICATE
: return "authenticate";
98 case CELL_AUTHORIZE
: return "authorize";
99 default: return "unrecognized";
103 #ifdef KEEP_TIMING_STATS
104 /** This is a wrapper function around the actual function that processes the
105 * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
106 * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
109 command_time_process_cell(cell_t
*cell
, channel_t
*chan
, int *time
,
110 void (*func
)(cell_t
*, channel_t
*))
112 struct timeval start
, end
;
115 tor_gettimeofday(&start
);
119 tor_gettimeofday(&end
);
120 time_passed
= tv_udiff(&start
, &end
) ;
122 if (time_passed
> 10000) { /* more than 10ms */
123 log_debug(LD_OR
,"That call just took %ld ms.",time_passed
/1000);
125 if (time_passed
< 0) {
126 log_info(LD_GENERAL
,"That call took us back in time!");
129 *time
+= time_passed
;
131 #endif /* defined(KEEP_TIMING_STATS) */
133 /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
134 * statistics about how many of each cell we've processed so far
135 * this second, and the total number of microseconds it took to
136 * process each type of cell.
139 command_process_cell(channel_t
*chan
, cell_t
*cell
)
141 #ifdef KEEP_TIMING_STATS
142 /* how many of each cell have we seen so far this second? needs better
144 static int num_create
=0, num_created
=0, num_relay
=0, num_destroy
=0;
145 /* how long has it taken to process each type of cell? */
146 static int create_time
=0, created_time
=0, relay_time
=0, destroy_time
=0;
147 static time_t current_second
= 0; /* from previous calls to time */
149 time_t now
= time(NULL
);
151 if (now
> current_second
) { /* the second has rolled over */
154 "At end of second: %d creates (%d ms), %d createds (%d ms), "
155 "%d relays (%d ms), %d destroys (%d ms)",
156 num_create
, create_time
/1000,
157 num_created
, created_time
/1000,
158 num_relay
, relay_time
/1000,
159 num_destroy
, destroy_time
/1000);
162 num_create
= num_created
= num_relay
= num_destroy
= 0;
163 create_time
= created_time
= relay_time
= destroy_time
= 0;
165 /* remember which second it is, for next time */
166 current_second
= now
;
168 #endif /* defined(KEEP_TIMING_STATS) */
170 #ifdef KEEP_TIMING_STATS
171 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
173 command_time_process_cell(cl, cn, & tp ## time , \
174 command_process_ ## tp ## _cell); \
176 #else /* !(defined(KEEP_TIMING_STATS)) */
177 #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
178 #endif /* defined(KEEP_TIMING_STATS) */
180 switch (cell
->command
) {
182 case CELL_CREATE_FAST
:
184 ++stats_n_create_cells_processed
;
185 PROCESS_CELL(create
, cell
, chan
);
188 case CELL_CREATED_FAST
:
190 ++stats_n_created_cells_processed
;
191 PROCESS_CELL(created
, cell
, chan
);
194 case CELL_RELAY_EARLY
:
195 ++stats_n_relay_cells_processed
;
196 PROCESS_CELL(relay
, cell
, chan
);
199 ++stats_n_destroy_cells_processed
;
200 PROCESS_CELL(destroy
, cell
, chan
);
203 log_fn(LOG_INFO
, LD_PROTOCOL
,
204 "Cell of unknown or unexpected type (%d) received. "
211 /** Process an incoming var_cell from a channel; in the current protocol all
212 * the var_cells are handshake-related and handled below the channel layer,
213 * so this just logs a warning and drops the cell.
217 command_process_var_cell(channel_t
*chan
, var_cell_t
*var_cell
)
220 tor_assert(var_cell
);
222 log_info(LD_PROTOCOL
,
223 "Received unexpected var_cell above the channel layer of type %d"
228 /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
229 * new circuit with the p_circ_id specified in cell. Put the circuit in state
230 * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
231 * picked up again when the cpuworker finishes decrypting it.
234 command_process_create_cell(cell_t
*cell
, channel_t
*chan
)
237 const or_options_t
*options
= get_options();
239 create_cell_t
*create_cell
;
245 "Got a CREATE cell for circ_id %u on channel " U64_FORMAT
247 (unsigned)cell
->circ_id
,
248 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
250 /* We check for the conditions that would make us drop the cell before
251 * we check for the conditions that would make us send a DESTROY back,
252 * since those conditions would make a DESTROY nonsensical. */
253 if (cell
->circ_id
== 0) {
254 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
255 "Received a create cell (type %d) from %s with zero circID; "
256 " ignoring.", (int)cell
->command
,
257 channel_get_actual_remote_descr(chan
));
261 if (circuit_id_in_use_on_channel(cell
->circ_id
, chan
)) {
262 const node_t
*node
= node_get_by_id(chan
->identity_digest
);
263 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
264 "Received CREATE cell (circID %u) for known circ. "
265 "Dropping (age %d).",
266 (unsigned)cell
->circ_id
,
267 (int)(time(NULL
) - channel_when_created(chan
)));
269 char *p
= esc_for_log(node_get_platform(node
));
270 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
271 "Details: router %s, platform %s.",
272 node_describe(node
), p
);
278 if (we_are_hibernating()) {
280 "Received create cell but we're shutting down. Sending back "
282 channel_send_destroy(cell
->circ_id
, chan
,
283 END_CIRC_REASON_HIBERNATING
);
287 if (!server_mode(options
) ||
288 (!public_server_mode(options
) && channel_is_outgoing(chan
))) {
289 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
290 "Received create cell (type %d) from %s, but we're connected "
291 "to it as a client. "
292 "Sending back a destroy.",
293 (int)cell
->command
, channel_get_canonical_remote_descr(chan
));
294 channel_send_destroy(cell
->circ_id
, chan
,
295 END_CIRC_REASON_TORPROTOCOL
);
299 /* If the high bit of the circuit ID is not as expected, close the
301 if (chan
->wide_circ_ids
)
302 id_is_high
= cell
->circ_id
& (1u<<31);
304 id_is_high
= cell
->circ_id
& (1u<<15);
306 chan
->circ_id_type
== CIRC_ID_TYPE_HIGHER
) ||
308 chan
->circ_id_type
== CIRC_ID_TYPE_LOWER
)) {
309 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
310 "Received create cell with unexpected circ_id %u. Closing.",
311 (unsigned)cell
->circ_id
);
312 channel_send_destroy(cell
->circ_id
, chan
,
313 END_CIRC_REASON_TORPROTOCOL
);
317 circ
= or_circuit_new(cell
->circ_id
, chan
);
318 circ
->base_
.purpose
= CIRCUIT_PURPOSE_OR
;
319 circuit_set_state(TO_CIRCUIT(circ
), CIRCUIT_STATE_ONIONSKIN_PENDING
);
320 create_cell
= tor_malloc_zero(sizeof(create_cell_t
));
321 if (create_cell_parse(create_cell
, cell
) < 0) {
322 tor_free(create_cell
);
323 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
324 "Bogus/unrecognized create cell; closing.");
325 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
329 if (connection_or_digest_is_known_relay(chan
->identity_digest
)) {
330 rep_hist_note_circuit_handshake_requested(create_cell
->handshake_type
);
331 // Needed for chutney: Sometimes relays aren't in the consensus yet, and
332 // get marked as clients. This resets their channels once they appear.
333 // Probably useful for normal operation wrt relay flapping, too.
334 channel_clear_client(chan
);
336 channel_mark_client(chan
);
339 if (create_cell
->handshake_type
!= ONION_HANDSHAKE_TYPE_FAST
) {
340 /* hand it off to the cpuworkers, and then return. */
342 if (assign_onionskin_to_cpuworker(circ
, create_cell
) < 0) {
343 log_debug(LD_GENERAL
,"Failed to hand off onionskin. Closing.");
344 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_RESOURCELIMIT
);
347 log_debug(LD_OR
,"success: handed off onionskin.");
349 /* This is a CREATE_FAST cell; we can handle it immediately without using
351 uint8_t keys
[CPATH_KEY_MATERIAL_LEN
];
352 uint8_t rend_circ_nonce
[DIGEST_LEN
];
354 created_cell_t created_cell
;
356 memset(&created_cell
, 0, sizeof(created_cell
));
357 len
= onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST
,
358 create_cell
->onionskin
,
359 create_cell
->handshake_len
,
362 keys
, CPATH_KEY_MATERIAL_LEN
,
364 tor_free(create_cell
);
366 log_warn(LD_OR
,"Failed to generate key material. Closing.");
367 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
370 created_cell
.cell_type
= CELL_CREATED_FAST
;
371 created_cell
.handshake_len
= len
;
373 if (onionskin_answer(circ
, &created_cell
,
374 (const char *)keys
, sizeof(keys
),
375 rend_circ_nonce
)<0) {
376 log_warn(LD_OR
,"Failed to reply to CREATE_FAST cell. Closing.");
377 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
380 memwipe(keys
, 0, sizeof(keys
));
384 /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
386 * that it's intended for. If we're not the origin of the circuit, package
387 * the 'created' cell in an 'extended' relay cell and pass it back. If we
388 * are the origin of the circuit, send it to circuit_finish_handshake() to
389 * finish processing keys, and then call circuit_send_next_onion_skin() to
390 * extend to the next hop in the circuit if necessary.
393 command_process_created_cell(cell_t
*cell
, channel_t
*chan
)
396 extended_cell_t extended_cell
;
398 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
402 "(circID %u) unknown circ (probably got a destroy earlier). "
403 "Dropping.", (unsigned)cell
->circ_id
);
407 if (circ
->n_circ_id
!= cell
->circ_id
|| circ
->n_chan
!= chan
) {
408 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,
409 "got created cell from Tor client? Closing.");
410 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
414 if (created_cell_parse(&extended_cell
.created_cell
, cell
) < 0) {
415 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Unparseable created cell.");
416 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
420 if (CIRCUIT_IS_ORIGIN(circ
)) { /* we're the OP. Handshake this. */
421 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
423 log_debug(LD_OR
,"at OP. Finishing handshake.");
424 if ((err_reason
= circuit_finish_handshake(origin_circ
,
425 &extended_cell
.created_cell
)) < 0) {
426 circuit_mark_for_close(circ
, -err_reason
);
429 log_debug(LD_OR
,"Moving to next skin.");
430 if ((err_reason
= circuit_send_next_onion_skin(origin_circ
)) < 0) {
431 log_info(LD_OR
,"circuit_send_next_onion_skin failed.");
432 /* XXX push this circuit_close lower */
433 circuit_mark_for_close(circ
, -err_reason
);
436 } else { /* pack it into an extended relay cell, and send it. */
439 uint8_t payload
[RELAY_PAYLOAD_SIZE
];
441 "Converting created cell to extended relay cell, sending.");
442 memset(payload
, 0, sizeof(payload
));
443 if (extended_cell
.created_cell
.cell_type
== CELL_CREATED2
)
444 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED2
;
446 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED
;
447 if (extended_cell_format(&command
, &len
, payload
, &extended_cell
) < 0) {
448 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Can't format extended cell.");
449 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
453 relay_send_command_from_edge(0, circ
, command
,
454 (const char*)payload
, len
, NULL
);
458 /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
459 * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
460 * circuit_receive_relay_cell() for actual processing.
463 command_process_relay_cell(cell_t
*cell
, channel_t
*chan
)
465 const or_options_t
*options
= get_options();
467 int reason
, direction
;
469 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
473 "unknown circuit %u on connection from %s. Dropping.",
474 (unsigned)cell
->circ_id
,
475 channel_get_canonical_remote_descr(chan
));
479 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
480 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit in create_wait. Closing.");
481 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
485 if (CIRCUIT_IS_ORIGIN(circ
)) {
486 /* if we're a relay and treating connections with recent local
487 * traffic better, then this is one of them. */
488 channel_timestamp_client(chan
);
491 if (!CIRCUIT_IS_ORIGIN(circ
) &&
492 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
493 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
)
494 direction
= CELL_DIRECTION_OUT
;
496 direction
= CELL_DIRECTION_IN
;
498 /* If we have a relay_early cell, make sure that it's outbound, and we've
499 * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
500 if (cell
->command
== CELL_RELAY_EARLY
) {
501 if (direction
== CELL_DIRECTION_IN
) {
502 /* Inbound early cells could once be encountered as a result of
503 * bug 1038; but relays running versions before 0.2.1.19 are long
504 * gone from the network, so any such cells now are surprising. */
506 "Received an inbound RELAY_EARLY cell on circuit %u."
507 " Closing circuit. Please report this event,"
508 " along with the following message.",
509 (unsigned)cell
->circ_id
);
510 if (CIRCUIT_IS_ORIGIN(circ
)) {
511 circuit_log_path(LOG_WARN
, LD_OR
, TO_ORIGIN_CIRCUIT(circ
));
512 } else if (circ
->n_chan
) {
513 log_warn(LD_OR
, " upstream=%s",
514 channel_get_actual_remote_descr(circ
->n_chan
));
516 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
519 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
520 if (or_circ
->remaining_relay_early_cells
== 0) {
521 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
522 "Received too many RELAY_EARLY cells on circ %u from %s."
524 (unsigned)cell
->circ_id
,
525 safe_str(channel_get_canonical_remote_descr(chan
)));
526 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
529 --or_circ
->remaining_relay_early_cells
;
533 if ((reason
= circuit_receive_relay_cell(cell
, circ
, direction
)) < 0) {
534 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit_receive_relay_cell "
535 "(%s) failed. Closing.",
536 direction
==CELL_DIRECTION_OUT
?"forward":"backward");
537 circuit_mark_for_close(circ
, -reason
);
540 /* If this is a cell in an RP circuit, count it as part of the
541 hidden service stats */
542 if (options
->HiddenServiceStatistics
&&
543 !CIRCUIT_IS_ORIGIN(circ
) &&
544 TO_OR_CIRCUIT(circ
)->circuit_carries_hs_traffic_stats
) {
545 rep_hist_seen_new_rp_cell();
549 /** Process a 'destroy' <b>cell</b> that just arrived from
550 * <b>chan</b>. Find the circ that it refers to (if any).
552 * If the circ is in state
553 * onionskin_pending, then call onion_pending_remove() to remove it
554 * from the pending onion list (note that if it's already being
555 * processed by the cpuworker, it won't be in the list anymore; but
556 * when the cpuworker returns it, the circuit will be gone, and the
557 * cpuworker response will be dropped).
559 * Then mark the circuit for close (which marks all edges for close,
560 * and passes the destroy cell onward if necessary).
563 command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
)
568 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
570 log_info(LD_OR
,"unknown circuit %u on connection from %s. Dropping.",
571 (unsigned)cell
->circ_id
,
572 channel_get_canonical_remote_descr(chan
));
575 log_debug(LD_OR
,"Received for circID %u.",(unsigned)cell
->circ_id
);
577 reason
= (uint8_t)cell
->payload
[0];
578 circ
->received_destroy
= 1;
580 if (!CIRCUIT_IS_ORIGIN(circ
) &&
581 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
582 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
) {
583 /* the destroy came from behind */
584 circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ
), 0, NULL
);
585 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
586 } else { /* the destroy came from ahead */
587 circuit_set_n_circid_chan(circ
, 0, NULL
);
588 if (CIRCUIT_IS_ORIGIN(circ
)) {
589 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
592 log_debug(LD_OR
, "Delivering 'truncated' back.");
593 payload
[0] = (char)reason
;
594 relay_send_command_from_edge(0, circ
, RELAY_COMMAND_TRUNCATED
,
595 payload
, sizeof(payload
), NULL
);
600 /** Callback to handle a new channel; call command_setup_channel() to give
601 * it the right cell handlers.
605 command_handle_incoming_channel(channel_listener_t
*listener
, channel_t
*chan
)
607 tor_assert(listener
);
610 command_setup_channel(chan
);
613 /** Given a channel, install the right handlers to process incoming
618 command_setup_channel(channel_t
*chan
)
622 channel_set_cell_handlers(chan
,
623 command_process_cell
,
624 command_process_var_cell
);
627 /** Given a listener, install the right handler to process incoming
632 command_setup_listener(channel_listener_t
*listener
)
634 tor_assert(listener
);
635 tor_assert(listener
->state
== CHANNEL_LISTENER_STATE_LISTENING
);
637 channel_listener_set_listener_fn(listener
, command_handle_incoming_channel
);