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 if (we_are_hibernating()) {
232 "Received create cell but we're shutting down. Sending back "
234 channel_send_destroy(cell
->circ_id
, chan
,
235 END_CIRC_REASON_HIBERNATING
);
239 if (!server_mode(options
) ||
240 (!public_server_mode(options
) && channel_is_outgoing(chan
))) {
241 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
242 "Received create cell (type %d) from %s, but we're connected "
243 "to it as a client. "
244 "Sending back a destroy.",
245 (int)cell
->command
, channel_get_canonical_remote_descr(chan
));
246 channel_send_destroy(cell
->circ_id
, chan
,
247 END_CIRC_REASON_TORPROTOCOL
);
251 if (cell
->circ_id
== 0) {
252 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
253 "Received a create cell (type %d) from %s with zero circID; "
254 " ignoring.", (int)cell
->command
,
255 channel_get_actual_remote_descr(chan
));
259 /* If the high bit of the circuit ID is not as expected, close the
261 if (chan
->wide_circ_ids
)
262 id_is_high
= cell
->circ_id
& (1u<<31);
264 id_is_high
= cell
->circ_id
& (1u<<15);
266 chan
->circ_id_type
== CIRC_ID_TYPE_HIGHER
) ||
268 chan
->circ_id_type
== CIRC_ID_TYPE_LOWER
)) {
269 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
270 "Received create cell with unexpected circ_id %u. Closing.",
271 (unsigned)cell
->circ_id
);
272 channel_send_destroy(cell
->circ_id
, chan
,
273 END_CIRC_REASON_TORPROTOCOL
);
277 if (circuit_id_in_use_on_channel(cell
->circ_id
, chan
)) {
278 const node_t
*node
= node_get_by_id(chan
->identity_digest
);
279 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
280 "Received CREATE cell (circID %u) for known circ. "
281 "Dropping (age %d).",
282 (unsigned)cell
->circ_id
,
283 (int)(time(NULL
) - channel_when_created(chan
)));
285 char *p
= esc_for_log(node_get_platform(node
));
286 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
287 "Details: router %s, platform %s.",
288 node_describe(node
), p
);
294 circ
= or_circuit_new(cell
->circ_id
, chan
);
295 circ
->base_
.purpose
= CIRCUIT_PURPOSE_OR
;
296 circuit_set_state(TO_CIRCUIT(circ
), CIRCUIT_STATE_ONIONSKIN_PENDING
);
297 create_cell
= tor_malloc_zero(sizeof(create_cell_t
));
298 if (create_cell_parse(create_cell
, cell
) < 0) {
299 tor_free(create_cell
);
300 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
301 "Bogus/unrecognized create cell; closing.");
302 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_TORPROTOCOL
);
306 if (create_cell
->handshake_type
!= ONION_HANDSHAKE_TYPE_FAST
) {
307 /* hand it off to the cpuworkers, and then return. */
308 if (connection_or_digest_is_known_relay(chan
->identity_digest
))
309 rep_hist_note_circuit_handshake_requested(create_cell
->handshake_type
);
310 if (assign_onionskin_to_cpuworker(NULL
, circ
, create_cell
) < 0) {
311 log_debug(LD_GENERAL
,"Failed to hand off onionskin. Closing.");
312 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_RESOURCELIMIT
);
315 log_debug(LD_OR
,"success: handed off onionskin.");
317 /* This is a CREATE_FAST cell; we can handle it immediately without using
319 uint8_t keys
[CPATH_KEY_MATERIAL_LEN
];
320 uint8_t rend_circ_nonce
[DIGEST_LEN
];
322 created_cell_t created_cell
;
324 /* Make sure we never try to use the OR connection on which we
325 * received this cell to satisfy an EXTEND request, */
326 channel_mark_client(chan
);
328 memset(&created_cell
, 0, sizeof(created_cell
));
329 len
= onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST
,
330 create_cell
->onionskin
,
331 create_cell
->handshake_len
,
334 keys
, CPATH_KEY_MATERIAL_LEN
,
336 tor_free(create_cell
);
338 log_warn(LD_OR
,"Failed to generate key material. Closing.");
339 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
340 tor_free(create_cell
);
343 created_cell
.cell_type
= CELL_CREATED_FAST
;
344 created_cell
.handshake_len
= len
;
346 if (onionskin_answer(circ
, &created_cell
,
347 (const char *)keys
, rend_circ_nonce
)<0) {
348 log_warn(LD_OR
,"Failed to reply to CREATE_FAST cell. Closing.");
349 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_INTERNAL
);
352 memwipe(keys
, 0, sizeof(keys
));
356 /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
358 * that it's intended for. If we're not the origin of the circuit, package
359 * the 'created' cell in an 'extended' relay cell and pass it back. If we
360 * are the origin of the circuit, send it to circuit_finish_handshake() to
361 * finish processing keys, and then call circuit_send_next_onion_skin() to
362 * extend to the next hop in the circuit if necessary.
365 command_process_created_cell(cell_t
*cell
, channel_t
*chan
)
368 extended_cell_t extended_cell
;
370 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
374 "(circID %u) unknown circ (probably got a destroy earlier). "
375 "Dropping.", (unsigned)cell
->circ_id
);
379 if (circ
->n_circ_id
!= cell
->circ_id
|| circ
->n_chan
!= chan
) {
380 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,
381 "got created cell from Tor client? Closing.");
382 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
386 if (created_cell_parse(&extended_cell
.created_cell
, cell
) < 0) {
387 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Unparseable created cell.");
388 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
392 if (CIRCUIT_IS_ORIGIN(circ
)) { /* we're the OP. Handshake this. */
393 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
395 log_debug(LD_OR
,"at OP. Finishing handshake.");
396 if ((err_reason
= circuit_finish_handshake(origin_circ
,
397 &extended_cell
.created_cell
)) < 0) {
398 log_warn(LD_OR
,"circuit_finish_handshake failed.");
399 circuit_mark_for_close(circ
, -err_reason
);
402 log_debug(LD_OR
,"Moving to next skin.");
403 if ((err_reason
= circuit_send_next_onion_skin(origin_circ
)) < 0) {
404 log_info(LD_OR
,"circuit_send_next_onion_skin failed.");
405 /* XXX push this circuit_close lower */
406 circuit_mark_for_close(circ
, -err_reason
);
409 } else { /* pack it into an extended relay cell, and send it. */
412 uint8_t payload
[RELAY_PAYLOAD_SIZE
];
414 "Converting created cell to extended relay cell, sending.");
415 memset(payload
, 0, sizeof(payload
));
416 if (extended_cell
.created_cell
.cell_type
== CELL_CREATED2
)
417 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED2
;
419 extended_cell
.cell_type
= RELAY_COMMAND_EXTENDED
;
420 if (extended_cell_format(&command
, &len
, payload
, &extended_cell
) < 0) {
421 log_fn(LOG_PROTOCOL_WARN
, LD_OR
, "Can't format extended cell.");
422 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
426 relay_send_command_from_edge(0, circ
, command
,
427 (const char*)payload
, len
, NULL
);
431 /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
432 * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
433 * circuit_receive_relay_cell() for actual processing.
436 command_process_relay_cell(cell_t
*cell
, channel_t
*chan
)
439 int reason
, direction
;
441 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
445 "unknown circuit %u on connection from %s. Dropping.",
446 (unsigned)cell
->circ_id
,
447 channel_get_canonical_remote_descr(chan
));
451 if (circ
->state
== CIRCUIT_STATE_ONIONSKIN_PENDING
) {
452 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit in create_wait. Closing.");
453 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
457 if (CIRCUIT_IS_ORIGIN(circ
)) {
458 /* if we're a relay and treating connections with recent local
459 * traffic better, then this is one of them. */
460 channel_timestamp_client(chan
);
463 if (!CIRCUIT_IS_ORIGIN(circ
) &&
464 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
465 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
)
466 direction
= CELL_DIRECTION_OUT
;
468 direction
= CELL_DIRECTION_IN
;
470 /* If we have a relay_early cell, make sure that it's outbound, and we've
471 * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
472 if (cell
->command
== CELL_RELAY_EARLY
) {
473 if (direction
== CELL_DIRECTION_IN
) {
474 /* Allow an unlimited number of inbound relay_early cells,
475 * for hidden service compatibility. There isn't any way to make
476 * a long circuit through inbound relay_early cells anyway. See
479 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
480 if (or_circ
->remaining_relay_early_cells
== 0) {
481 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
482 "Received too many RELAY_EARLY cells on circ %u from %s."
484 (unsigned)cell
->circ_id
,
485 safe_str(channel_get_canonical_remote_descr(chan
)));
486 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
489 --or_circ
->remaining_relay_early_cells
;
493 if ((reason
= circuit_receive_relay_cell(cell
, circ
, direction
)) < 0) {
494 log_fn(LOG_PROTOCOL_WARN
,LD_PROTOCOL
,"circuit_receive_relay_cell "
495 "(%s) failed. Closing.",
496 direction
==CELL_DIRECTION_OUT
?"forward":"backward");
497 circuit_mark_for_close(circ
, -reason
);
501 /** Process a 'destroy' <b>cell</b> that just arrived from
502 * <b>chan</b>. Find the circ that it refers to (if any).
504 * If the circ is in state
505 * onionskin_pending, then call onion_pending_remove() to remove it
506 * from the pending onion list (note that if it's already being
507 * processed by the cpuworker, it won't be in the list anymore; but
508 * when the cpuworker returns it, the circuit will be gone, and the
509 * cpuworker response will be dropped).
511 * Then mark the circuit for close (which marks all edges for close,
512 * and passes the destroy cell onward if necessary).
515 command_process_destroy_cell(cell_t
*cell
, channel_t
*chan
)
520 circ
= circuit_get_by_circid_channel(cell
->circ_id
, chan
);
522 log_info(LD_OR
,"unknown circuit %u on connection from %s. Dropping.",
523 (unsigned)cell
->circ_id
,
524 channel_get_canonical_remote_descr(chan
));
527 log_debug(LD_OR
,"Received for circID %u.",(unsigned)cell
->circ_id
);
529 reason
= (uint8_t)cell
->payload
[0];
530 circ
->received_destroy
= 1;
532 if (!CIRCUIT_IS_ORIGIN(circ
) &&
533 chan
== TO_OR_CIRCUIT(circ
)->p_chan
&&
534 cell
->circ_id
== TO_OR_CIRCUIT(circ
)->p_circ_id
) {
535 /* the destroy came from behind */
536 circuit_set_p_circid_chan(TO_OR_CIRCUIT(circ
), 0, NULL
);
537 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
538 } else { /* the destroy came from ahead */
539 circuit_set_n_circid_chan(circ
, 0, NULL
);
540 if (CIRCUIT_IS_ORIGIN(circ
)) {
541 circuit_mark_for_close(circ
, reason
|END_CIRC_REASON_FLAG_REMOTE
);
544 log_debug(LD_OR
, "Delivering 'truncated' back.");
545 payload
[0] = (char)reason
;
546 relay_send_command_from_edge(0, circ
, RELAY_COMMAND_TRUNCATED
,
547 payload
, sizeof(payload
), NULL
);
552 /** Callback to handle a new channel; call command_setup_channel() to give
553 * it the right cell handlers.
557 command_handle_incoming_channel(channel_listener_t
*listener
, channel_t
*chan
)
559 tor_assert(listener
);
562 command_setup_channel(chan
);
565 /** Given a channel, install the right handlers to process incoming
570 command_setup_channel(channel_t
*chan
)
574 channel_set_cell_handlers(chan
,
575 command_process_cell
,
576 command_process_var_cell
);
579 /** Given a listener, install the right handler to process incoming
584 command_setup_listener(channel_listener_t
*listener
)
586 tor_assert(listener
);
587 tor_assert(listener
->state
== CHANNEL_LISTENER_STATE_LISTENING
);
589 channel_listener_set_listener_fn(listener
, command_handle_incoming_channel
);