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"
50 #include "hibernate.h"
56 #include "routerlist.h"
58 /** How many CELL_CREATE cells have we received, ever? */
59 uint64_t stats_n_create_cells_processed
= 0;
60 /** How many CELL_CREATED cells have we received, ever? */
61 uint64_t stats_n_created_cells_processed
= 0;
62 /** How many CELL_RELAY cells have we received, ever? */
63 uint64_t stats_n_relay_cells_processed
= 0;
64 /** How many CELL_DESTROY cells have we received, ever? */
65 uint64_t stats_n_destroy_cells_processed
= 0;
67 /* Handle an incoming channel */
68 static void command_handle_incoming_channel(channel_listener_t
*listener
,
71 /* These are the main functions for processing cells */
72 static void command_process_create_cell(cell_t
*cell
, channel_t
*chan
);
73 static void command_process_created_cell(cell_t
*cell
, channel_t
*chan
);
74 static void command_process_relay_cell(cell_t
*cell
, channel_t
*chan
);
75 static void command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
);
77 /** Convert the cell <b>command</b> into a lower-case, human-readable
80 cell_command_to_string(uint8_t command
)
83 case CELL_PADDING
: return "padding";
84 case CELL_CREATE
: return "create";
85 case CELL_CREATED
: return "created";
86 case CELL_RELAY
: return "relay";
87 case CELL_DESTROY
: return "destroy";
88 case CELL_CREATE_FAST
: return "create_fast";
89 case CELL_CREATED_FAST
: return "created_fast";
90 case CELL_VERSIONS
: return "versions";
91 case CELL_NETINFO
: return "netinfo";
92 case CELL_RELAY_EARLY
: return "relay_early";
93 case CELL_CREATE2
: return "create2";
94 case CELL_CREATED2
: return "created2";
95 case CELL_VPADDING
: return "vpadding";
96 case CELL_CERTS
: return "certs";
97 case CELL_AUTH_CHALLENGE
: return "auth_challenge";
98 case CELL_AUTHENTICATE
: return "authenticate";
99 case CELL_AUTHORIZE
: return "authorize";
100 default: return "unrecognized";
104 #ifdef KEEP_TIMING_STATS
105 /** This is a wrapper function around the actual function that processes the
106 * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
107 * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
110 command_time_process_cell(cell_t
*cell
, channel_t
*chan
, int *time
,
111 void (*func
)(cell_t
*, channel_t
*))
113 struct timeval start
, end
;
116 tor_gettimeofday(&start
);
120 tor_gettimeofday(&end
);
121 time_passed
= tv_udiff(&start
, &end
) ;
123 if (time_passed
> 10000) { /* more than 10ms */
124 log_debug(LD_OR
,"That call just took %ld ms.",time_passed
/1000);
126 if (time_passed
< 0) {
127 log_info(LD_GENERAL
,"That call took us back in time!");
130 *time
+= time_passed
;
132 #endif /* defined(KEEP_TIMING_STATS) */
134 /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
135 * statistics about how many of each cell we've processed so far
136 * this second, and the total number of microseconds it took to
137 * process each type of cell.
140 command_process_cell(channel_t
*chan
, cell_t
*cell
)
142 #ifdef KEEP_TIMING_STATS
143 /* how many of each cell have we seen so far this second? needs better
145 static int num_create
=0, num_created
=0, num_relay
=0, num_destroy
=0;
146 /* how long has it taken to process each type of cell? */
147 static int create_time
=0, created_time
=0, relay_time
=0, destroy_time
=0;
148 static time_t current_second
= 0; /* from previous calls to time */
150 time_t now
= time(NULL
);
152 if (now
> current_second
) { /* the second has rolled over */
155 "At end of second: %d creates (%d ms), %d createds (%d ms), "
156 "%d relays (%d ms), %d destroys (%d ms)",
157 num_create
, create_time
/1000,
158 num_created
, created_time
/1000,
159 num_relay
, relay_time
/1000,
160 num_destroy
, destroy_time
/1000);
163 num_create
= num_created
= num_relay
= num_destroy
= 0;
164 create_time
= created_time
= relay_time
= destroy_time
= 0;
166 /* remember which second it is, for next time */
167 current_second
= now
;
169 #endif /* defined(KEEP_TIMING_STATS) */
171 #ifdef KEEP_TIMING_STATS
172 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
174 command_time_process_cell(cl, cn, & tp ## time , \
175 command_process_ ## tp ## _cell); \
177 #else /* !(defined(KEEP_TIMING_STATS)) */
178 #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
179 #endif /* defined(KEEP_TIMING_STATS) */
181 switch (cell
->command
) {
183 case CELL_CREATE_FAST
:
185 ++stats_n_create_cells_processed
;
186 PROCESS_CELL(create
, cell
, chan
);
189 case CELL_CREATED_FAST
:
191 ++stats_n_created_cells_processed
;
192 PROCESS_CELL(created
, cell
, chan
);
195 case CELL_RELAY_EARLY
:
196 ++stats_n_relay_cells_processed
;
197 PROCESS_CELL(relay
, cell
, chan
);
200 ++stats_n_destroy_cells_processed
;
201 PROCESS_CELL(destroy
, cell
, chan
);
204 log_fn(LOG_INFO
, LD_PROTOCOL
,
205 "Cell of unknown or unexpected type (%d) received. "
212 /** Process an incoming var_cell from a channel; in the current protocol all
213 * the var_cells are handshake-related and handled below the channel layer,
214 * so this just logs a warning and drops the cell.
218 command_process_var_cell(channel_t
*chan
, var_cell_t
*var_cell
)
221 tor_assert(var_cell
);
223 log_info(LD_PROTOCOL
,
224 "Received unexpected var_cell above the channel layer of type %d"
229 /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
230 * new circuit with the p_circ_id specified in cell. Put the circuit in state
231 * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
232 * picked up again when the cpuworker finishes decrypting it.
235 command_process_create_cell(cell_t
*cell
, channel_t
*chan
)
238 const or_options_t
*options
= get_options();
240 create_cell_t
*create_cell
;
246 "Got a CREATE cell for circ_id %u on channel " U64_FORMAT
248 (unsigned)cell
->circ_id
,
249 U64_PRINTF_ARG(chan
->global_identifier
), chan
);
251 /* First thing we do, even though the cell might be invalid, is inform the
252 * DoS mitigation subsystem layer of this event. Validation is done by this
254 dos_cc_new_create_cell(chan
);
256 /* We check for the conditions that would make us drop the cell before
257 * we check for the conditions that would make us send a DESTROY back,
258 * since those conditions would make a DESTROY nonsensical. */
259 if (cell
->circ_id
== 0) {
260 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
261 "Received a create cell (type %d) from %s with zero circID; "
262 " ignoring.", (int)cell
->command
,
263 channel_get_actual_remote_descr(chan
));
267 if (circuit_id_in_use_on_channel(cell
->circ_id
, chan
)) {
268 const node_t
*node
= node_get_by_id(chan
->identity_digest
);
269 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
270 "Received CREATE cell (circID %u) for known circ. "
271 "Dropping (age %d).",
272 (unsigned)cell
->circ_id
,
273 (int)(time(NULL
) - channel_when_created(chan
)));
275 char *p
= esc_for_log(node_get_platform(node
));
276 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
277 "Details: router %s, platform %s.",
278 node_describe(node
), p
);
284 if (we_are_hibernating()) {
286 "Received create cell but we're shutting down. Sending back "
288 channel_send_destroy(cell
->circ_id
, chan
,
289 END_CIRC_REASON_HIBERNATING
);
293 /* Check if we should apply a defense for this channel. */
294 if (dos_cc_get_defense_type(chan
) == DOS_CC_DEFENSE_REFUSE_CELL
) {
295 channel_send_destroy(cell
->circ_id
, chan
,
296 END_CIRC_REASON_RESOURCELIMIT
);
300 if (!server_mode(options
) ||
301 (!public_server_mode(options
) && channel_is_outgoing(chan
))) {
302 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
303 "Received create cell (type %d) from %s, but we're connected "
304 "to it as a client. "
305 "Sending back a destroy.",
306 (int)cell
->command
, channel_get_canonical_remote_descr(chan
));
307 channel_send_destroy(cell
->circ_id
, chan
,
308 END_CIRC_REASON_TORPROTOCOL
);
312 /* If the high bit of the circuit ID is not as expected, close the
314 if (chan
->wide_circ_ids
)
315 id_is_high
= cell
->circ_id
& (1u<<31);
317 id_is_high
= cell
->circ_id
& (1u<<15);
319 chan
->circ_id_type
== CIRC_ID_TYPE_HIGHER
) ||
321 chan
->circ_id_type
== CIRC_ID_TYPE_LOWER
)) {
322 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
323 "Received create cell with unexpected circ_id %u. Closing.",
324 (unsigned)cell
->circ_id
);
325 channel_send_destroy(cell
->circ_id
, chan
,
326 END_CIRC_REASON_TORPROTOCOL
);
330 circ
= or_circuit_new(cell
->circ_id
, chan
);
331 circ
->base_
.purpose
= CIRCUIT_PURPOSE_OR
;
332 circuit_set_state(TO_CIRCUIT(circ
), CIRCUIT_STATE_ONIONSKIN_PENDING
);
333 create_cell
= tor_malloc_zero(sizeof(create_cell_t
));
334 if (create_cell_parse(create_cell
, cell
) < 0) {
335 tor_free(create_cell
);
336 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
337 "Bogus/unrecognized create cell; closing.");
338 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
342 if (connection_or_digest_is_known_relay(chan
->identity_digest
)) {
343 rep_hist_note_circuit_handshake_requested(create_cell
->handshake_type
);
346 if (create_cell
->handshake_type
!= ONION_HANDSHAKE_TYPE_FAST
) {
347 /* hand it off to the cpuworkers, and then return. */
349 if (assign_onionskin_to_cpuworker(circ
, create_cell
) < 0) {
350 log_debug(LD_GENERAL
,"Failed to hand off onionskin. Closing.");
351 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_RESOURCELIMIT
);
354 log_debug(LD_OR
,"success: handed off onionskin.");
356 /* This is a CREATE_FAST cell; we can handle it immediately without using
358 uint8_t keys
[CPATH_KEY_MATERIAL_LEN
];
359 uint8_t rend_circ_nonce
[DIGEST_LEN
];
361 created_cell_t created_cell
;
363 memset(&created_cell
, 0, sizeof(created_cell
));
364 len
= onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST
,
365 create_cell
->onionskin
,
366 create_cell
->handshake_len
,
369 keys
, CPATH_KEY_MATERIAL_LEN
,
371 tor_free(create_cell
);
373 log_warn(LD_OR
,"Failed to generate key material. Closing.");
374 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
377 created_cell
.cell_type
= CELL_CREATED_FAST
;
378 created_cell
.handshake_len
= len
;
380 if (onionskin_answer(circ
, &created_cell
,
381 (const char *)keys
, sizeof(keys
),
382 rend_circ_nonce
)<0) {
383 log_warn(LD_OR
,"Failed to reply to CREATE_FAST cell. Closing.");
384 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
387 memwipe(keys
, 0, sizeof(keys
));
391 /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
393 * that it's intended for. If we're not the origin of the circuit, package
394 * the 'created' cell in an 'extended' relay cell and pass it back. If we
395 * are the origin of the circuit, send it to circuit_finish_handshake() to
396 * finish processing keys, and then call circuit_send_next_onion_skin() to
397 * extend to the next hop in the circuit if necessary.
400 command_process_created_cell(cell_t
*cell
, channel_t
*chan
)
403 extended_cell_t extended_cell
;
405 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
409 "(circID %u) unknown circ (probably got a destroy earlier). "
410 "Dropping.", (unsigned)cell
->circ_id
);
414 if (circ
->n_circ_id
!= cell
->circ_id
|| circ
->n_chan
!= chan
) {
415 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,
416 "got created cell from Tor client? Closing.");
417 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
421 if (created_cell_parse(&extended_cell
.created_cell
, cell
) < 0) {
422 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Unparseable created cell.");
423 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
427 if (CIRCUIT_IS_ORIGIN(circ
)) { /* we're the OP. Handshake this. */
428 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
430 log_debug(LD_OR
,"at OP. Finishing handshake.");
431 if ((err_reason
= circuit_finish_handshake(origin_circ
,
432 &extended_cell
.created_cell
)) < 0) {
433 circuit_mark_for_close(circ
, -err_reason
);
436 log_debug(LD_OR
,"Moving to next skin.");
437 if ((err_reason
= circuit_send_next_onion_skin(origin_circ
)) < 0) {
438 log_info(LD_OR
,"circuit_send_next_onion_skin failed.");
439 /* XXX push this circuit_close lower */
440 circuit_mark_for_close(circ
, -err_reason
);
443 } else { /* pack it into an extended relay cell, and send it. */
446 uint8_t payload
[RELAY_PAYLOAD_SIZE
];
448 "Converting created cell to extended relay cell, sending.");
449 memset(payload
, 0, sizeof(payload
));
450 if (extended_cell
.created_cell
.cell_type
== CELL_CREATED2
)
451 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED2
;
453 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED
;
454 if (extended_cell_format(&command
, &len
, payload
, &extended_cell
) < 0) {
455 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Can't format extended cell.");
456 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
460 relay_send_command_from_edge(0, circ
, command
,
461 (const char*)payload
, len
, NULL
);
465 /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
466 * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
467 * circuit_receive_relay_cell() for actual processing.
470 command_process_relay_cell(cell_t
*cell
, channel_t
*chan
)
472 const or_options_t
*options
= get_options();
474 int reason
, direction
;
476 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
480 "unknown circuit %u on connection from %s. Dropping.",
481 (unsigned)cell
->circ_id
,
482 channel_get_canonical_remote_descr(chan
));
486 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
487 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit in create_wait. Closing.");
488 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
492 if (CIRCUIT_IS_ORIGIN(circ
)) {
493 /* if we're a relay and treating connections with recent local
494 * traffic better, then this is one of them. */
495 channel_timestamp_client(chan
);
498 if (!CIRCUIT_IS_ORIGIN(circ
) &&
499 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
500 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
)
501 direction
= CELL_DIRECTION_OUT
;
503 direction
= CELL_DIRECTION_IN
;
505 /* If we have a relay_early cell, make sure that it's outbound, and we've
506 * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
507 if (cell
->command
== CELL_RELAY_EARLY
) {
508 if (direction
== CELL_DIRECTION_IN
) {
509 /* Inbound early cells could once be encountered as a result of
510 * bug 1038; but relays running versions before 0.2.1.19 are long
511 * gone from the network, so any such cells now are surprising. */
513 "Received an inbound RELAY_EARLY cell on circuit %u."
514 " Closing circuit. Please report this event,"
515 " along with the following message.",
516 (unsigned)cell
->circ_id
);
517 if (CIRCUIT_IS_ORIGIN(circ
)) {
518 circuit_log_path(LOG_WARN
, LD_OR
, TO_ORIGIN_CIRCUIT(circ
));
519 } else if (circ
->n_chan
) {
520 log_warn(LD_OR
, " upstream=%s",
521 channel_get_actual_remote_descr(circ
->n_chan
));
523 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
526 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
527 if (or_circ
->remaining_relay_early_cells
== 0) {
528 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
529 "Received too many RELAY_EARLY cells on circ %u from %s."
531 (unsigned)cell
->circ_id
,
532 safe_str(channel_get_canonical_remote_descr(chan
)));
533 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
536 --or_circ
->remaining_relay_early_cells
;
540 if ((reason
= circuit_receive_relay_cell(cell
, circ
, direction
)) < 0) {
541 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit_receive_relay_cell "
542 "(%s) failed. Closing.",
543 direction
==CELL_DIRECTION_OUT
?"forward":"backward");
544 circuit_mark_for_close(circ
, -reason
);
547 /* If this is a cell in an RP circuit, count it as part of the
548 hidden service stats */
549 if (options
->HiddenServiceStatistics
&&
550 !CIRCUIT_IS_ORIGIN(circ
) &&
551 TO_OR_CIRCUIT(circ
)->circuit_carries_hs_traffic_stats
) {
552 rep_hist_seen_new_rp_cell();
556 /** Process a 'destroy' <b>cell</b> that just arrived from
557 * <b>chan</b>. Find the circ that it refers to (if any).
559 * If the circ is in state
560 * onionskin_pending, then call onion_pending_remove() to remove it
561 * from the pending onion list (note that if it's already being
562 * processed by the cpuworker, it won't be in the list anymore; but
563 * when the cpuworker returns it, the circuit will be gone, and the
564 * cpuworker response will be dropped).
566 * Then mark the circuit for close (which marks all edges for close,
567 * and passes the destroy cell onward if necessary).
570 command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
)
575 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
577 log_info(LD_OR
,"unknown circuit %u on connection from %s. Dropping.",
578 (unsigned)cell
->circ_id
,
579 channel_get_canonical_remote_descr(chan
));
582 log_debug(LD_OR
,"Received for circID %u.",(unsigned)cell
->circ_id
);
584 reason
= (uint8_t)cell
->payload
[0];
585 circ
->received_destroy
= 1;
587 if (!CIRCUIT_IS_ORIGIN(circ
) &&
588 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
589 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
) {
590 /* the destroy came from behind */
591 circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ
), 0, NULL
);
592 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
593 } else { /* the destroy came from ahead */
594 circuit_set_n_circid_chan(circ
, 0, NULL
);
595 if (CIRCUIT_IS_ORIGIN(circ
)) {
596 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
599 log_debug(LD_OR
, "Delivering 'truncated' back.");
600 payload
[0] = (char)reason
;
601 relay_send_command_from_edge(0, circ
, RELAY_COMMAND_TRUNCATED
,
602 payload
, sizeof(payload
), NULL
);
607 /** Callback to handle a new channel; call command_setup_channel() to give
608 * it the right cell handlers.
612 command_handle_incoming_channel(channel_listener_t
*listener
, channel_t
*chan
)
614 tor_assert(listener
);
617 command_setup_channel(chan
);
620 /** Given a channel, install the right handlers to process incoming
625 command_setup_channel(channel_t
*chan
)
629 channel_set_cell_handlers(chan
,
630 command_process_cell
,
631 command_process_var_cell
);
634 /** Given a listener, install the right handler to process incoming
639 command_setup_listener(channel_listener_t
*listener
)
641 tor_assert(listener
);
642 tor_assert(listener
->state
== CHANNEL_LISTENER_STATE_LISTENING
);
644 channel_listener_set_listener_fn(listener
, command_handle_incoming_channel
);