Tor  0.4.4.0-alpha-dev
command.c
Go to the documentation of this file.
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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file command.c
9  * \brief Functions for processing incoming cells.
10  *
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()
16  * function.)
17  *
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.)
24  *
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.
30  **/
31 
32 /* In-points to command.c:
33  *
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
38  */
39 #include "core/or/or.h"
40 #include "app/config/config.h"
44 #include "core/or/channel.h"
45 #include "core/or/circuitbuild.h"
46 #include "core/or/circuitlist.h"
47 #include "core/or/command.h"
48 #include "core/or/connection_or.h"
49 #include "core/or/dos.h"
50 #include "core/or/onion.h"
51 #include "core/or/relay.h"
59 #include "feature/stats/rephist.h"
61 
62 #include "core/or/cell_st.h"
63 #include "core/or/or_circuit_st.h"
65 #include "core/or/var_cell_st.h"
66 
67 /** How many CELL_CREATE cells have we received, ever? */
69 /** How many CELL_CREATED cells have we received, ever? */
71 /** How many CELL_RELAY cells have we received, ever? */
73 /** How many CELL_DESTROY cells have we received, ever? */
75 
76 /* Handle an incoming channel */
78  channel_t *chan);
79 
80 /* These are the main functions for processing cells */
81 static void command_process_create_cell(cell_t *cell, channel_t *chan);
82 static void command_process_created_cell(cell_t *cell, channel_t *chan);
83 static void command_process_relay_cell(cell_t *cell, channel_t *chan);
84 static void command_process_destroy_cell(cell_t *cell, channel_t *chan);
85 
86 /** Convert the cell <b>command</b> into a lower-case, human-readable
87  * string. */
88 const char *
90 {
91  switch (command) {
92  case CELL_PADDING: return "padding";
93  case CELL_CREATE: return "create";
94  case CELL_CREATED: return "created";
95  case CELL_RELAY: return "relay";
96  case CELL_DESTROY: return "destroy";
97  case CELL_CREATE_FAST: return "create_fast";
98  case CELL_CREATED_FAST: return "created_fast";
99  case CELL_VERSIONS: return "versions";
100  case CELL_NETINFO: return "netinfo";
101  case CELL_RELAY_EARLY: return "relay_early";
102  case CELL_CREATE2: return "create2";
103  case CELL_CREATED2: return "created2";
104  case CELL_VPADDING: return "vpadding";
105  case CELL_CERTS: return "certs";
106  case CELL_AUTH_CHALLENGE: return "auth_challenge";
107  case CELL_AUTHENTICATE: return "authenticate";
108  case CELL_AUTHORIZE: return "authorize";
109  default: return "unrecognized";
110  }
111 }
112 
113 #ifdef KEEP_TIMING_STATS
114 /** This is a wrapper function around the actual function that processes the
115  * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
116  * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
117  */
118 static void
119 command_time_process_cell(cell_t *cell, channel_t *chan, int *time,
120  void (*func)(cell_t *, channel_t *))
121 {
122  struct timeval start, end;
123  long time_passed;
124 
125  tor_gettimeofday(&start);
126 
127  (*func)(cell, chan);
128 
129  tor_gettimeofday(&end);
130  time_passed = tv_udiff(&start, &end) ;
131 
132  if (time_passed > 10000) { /* more than 10ms */
133  log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
134  }
135  if (time_passed < 0) {
136  log_info(LD_GENERAL,"That call took us back in time!");
137  time_passed = 0;
138  }
139  *time += time_passed;
140 }
141 #endif /* defined(KEEP_TIMING_STATS) */
142 
143 /** Process a <b>cell</b> that was just received on <b>chan</b>. Keep internal
144  * statistics about how many of each cell we've processed so far
145  * this second, and the total number of microseconds it took to
146  * process each type of cell.
147  */
148 void
150 {
151 #ifdef KEEP_TIMING_STATS
152  /* how many of each cell have we seen so far this second? needs better
153  * name. */
154  static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
155  /* how long has it taken to process each type of cell? */
156  static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
157  static time_t current_second = 0; /* from previous calls to time */
158 
159  time_t now = time(NULL);
160 
161  if (now > current_second) { /* the second has rolled over */
162  /* print stats */
163  log_info(LD_OR,
164  "At end of second: %d creates (%d ms), %d createds (%d ms), "
165  "%d relays (%d ms), %d destroys (%d ms)",
166  num_create, create_time/1000,
167  num_created, created_time/1000,
168  num_relay, relay_time/1000,
169  num_destroy, destroy_time/1000);
170 
171  /* zero out stats */
172  num_create = num_created = num_relay = num_destroy = 0;
173  create_time = created_time = relay_time = destroy_time = 0;
174 
175  /* remember which second it is, for next time */
176  current_second = now;
177  }
178 #endif /* defined(KEEP_TIMING_STATS) */
179 
180 #ifdef KEEP_TIMING_STATS
181 #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
182  ++num ## tp; \
183  command_time_process_cell(cl, cn, & tp ## time , \
184  command_process_ ## tp ## _cell); \
185  } STMT_END
186 #else /* !defined(KEEP_TIMING_STATS) */
187 #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
188 #endif /* defined(KEEP_TIMING_STATS) */
189 
190  switch (cell->command) {
191  case CELL_CREATE:
192  case CELL_CREATE_FAST:
193  case CELL_CREATE2:
195  PROCESS_CELL(create, cell, chan);
196  break;
197  case CELL_CREATED:
198  case CELL_CREATED_FAST:
199  case CELL_CREATED2:
201  PROCESS_CELL(created, cell, chan);
202  break;
203  case CELL_RELAY:
204  case CELL_RELAY_EARLY:
206  PROCESS_CELL(relay, cell, chan);
207  break;
208  case CELL_DESTROY:
210  PROCESS_CELL(destroy, cell, chan);
211  break;
212  default:
214  "Cell of unknown or unexpected type (%d) received. "
215  "Dropping.",
216  cell->command);
217  break;
218  }
219 }
220 
221 /** Process a 'create' <b>cell</b> that just arrived from <b>chan</b>. Make a
222  * new circuit with the p_circ_id specified in cell. Put the circuit in state
223  * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
224  * picked up again when the cpuworker finishes decrypting it.
225  */
226 static void
228 {
229  or_circuit_t *circ;
230  const or_options_t *options = get_options();
231  int id_is_high;
232  create_cell_t *create_cell;
233 
234  tor_assert(cell);
235  tor_assert(chan);
236 
237  log_debug(LD_OR,
238  "Got a CREATE cell for circ_id %u on channel %"PRIu64
239  " (%p)",
240  (unsigned)cell->circ_id,
241  (chan->global_identifier), chan);
242 
243  /* First thing we do, even though the cell might be invalid, is inform the
244  * DoS mitigation subsystem layer of this event. Validation is done by this
245  * function. */
246  dos_cc_new_create_cell(chan);
247 
248  /* We check for the conditions that would make us drop the cell before
249  * we check for the conditions that would make us send a DESTROY back,
250  * since those conditions would make a DESTROY nonsensical. */
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,
256  return;
257  }
258 
259  if (circuit_id_in_use_on_channel(cell->circ_id, chan)) {
260  const node_t *node = node_get_by_id(chan->identity_digest);
261  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
262  "Received CREATE cell (circID %u) for known circ. "
263  "Dropping (age %d).",
264  (unsigned)cell->circ_id,
265  (int)(time(NULL) - channel_when_created(chan)));
266  if (node) {
267  char *p = esc_for_log(node_get_platform(node));
268  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
269  "Details: router %s, platform %s.",
270  node_describe(node), p);
271  tor_free(p);
272  }
273  return;
274  }
275 
276  if (we_are_hibernating()) {
277  log_info(LD_OR,
278  "Received create cell but we're shutting down. Sending back "
279  "destroy.");
280  channel_send_destroy(cell->circ_id, chan,
281  END_CIRC_REASON_HIBERNATING);
282  return;
283  }
284 
285  /* Check if we should apply a defense for this channel. */
286  if (dos_cc_get_defense_type(chan) == DOS_CC_DEFENSE_REFUSE_CELL) {
287  channel_send_destroy(cell->circ_id, chan,
288  END_CIRC_REASON_RESOURCELIMIT);
289  return;
290  }
291 
292  if (!server_mode(options) ||
293  (!public_server_mode(options) && channel_is_outgoing(chan))) {
294  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
295  "Received create cell (type %d) from %s, but we're connected "
296  "to it as a client. "
297  "Sending back a destroy.",
298  (int)cell->command, channel_get_canonical_remote_descr(chan));
299  channel_send_destroy(cell->circ_id, chan,
300  END_CIRC_REASON_TORPROTOCOL);
301  return;
302  }
303 
304  /* If the high bit of the circuit ID is not as expected, close the
305  * circ. */
306  if (chan->wide_circ_ids)
307  id_is_high = cell->circ_id & (1u<<31);
308  else
309  id_is_high = cell->circ_id & (1u<<15);
310  if ((id_is_high &&
311  chan->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
312  (!id_is_high &&
313  chan->circ_id_type == CIRC_ID_TYPE_LOWER)) {
314  log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
315  "Received create cell with unexpected circ_id %u. Closing.",
316  (unsigned)cell->circ_id);
317  channel_send_destroy(cell->circ_id, chan,
318  END_CIRC_REASON_TORPROTOCOL);
319  return;
320  }
321 
322  circ = or_circuit_new(cell->circ_id, chan);
323  circ->base_.purpose = CIRCUIT_PURPOSE_OR;
325  create_cell = tor_malloc_zero(sizeof(create_cell_t));
326  if (create_cell_parse(create_cell, cell) < 0) {
327  tor_free(create_cell);
328  log_fn(LOG_PROTOCOL_WARN, LD_OR,
329  "Bogus/unrecognized create cell; closing.");
330  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_TORPROTOCOL);
331  return;
332  }
333 
334  if (!channel_is_client(chan)) {
335  /* remember create types we've seen, but don't remember them from
336  * clients, to be extra conservative about client statistics. */
338  }
339 
340  if (create_cell->handshake_type != ONION_HANDSHAKE_TYPE_FAST) {
341  /* hand it off to the cpuworkers, and then return. */
342 
343  if (assign_onionskin_to_cpuworker(circ, create_cell) < 0) {
344  log_debug(LD_GENERAL,"Failed to hand off onionskin. Closing.");
345  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
346  return;
347  }
348  log_debug(LD_OR,"success: handed off onionskin.");
349  } else {
350  /* This is a CREATE_FAST cell; we can handle it immediately without using
351  * a CPU worker. */
352  uint8_t keys[CPATH_KEY_MATERIAL_LEN];
353  uint8_t rend_circ_nonce[DIGEST_LEN];
354  int len;
355  created_cell_t created_cell;
356 
357  memset(&created_cell, 0, sizeof(created_cell));
358  len = onion_skin_server_handshake(ONION_HANDSHAKE_TYPE_FAST,
359  create_cell->onionskin,
360  create_cell->handshake_len,
361  NULL,
362  created_cell.reply,
363  keys, CPATH_KEY_MATERIAL_LEN,
364  rend_circ_nonce);
365  tor_free(create_cell);
366  if (len < 0) {
367  log_warn(LD_OR,"Failed to generate key material. Closing.");
368  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
369  return;
370  }
371  created_cell.cell_type = CELL_CREATED_FAST;
372  created_cell.handshake_len = len;
373 
374  if (onionskin_answer(circ, &created_cell,
375  (const char *)keys, sizeof(keys),
376  rend_circ_nonce)<0) {
377  log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
378  circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
379  return;
380  }
381  memwipe(keys, 0, sizeof(keys));
382  }
383 }
384 
385 /** Process a 'created' <b>cell</b> that just arrived from <b>chan</b>.
386  * Find the circuit
387  * that it's intended for. If we're not the origin of the circuit, package
388  * the 'created' cell in an 'extended' relay cell and pass it back. If we
389  * are the origin of the circuit, send it to circuit_finish_handshake() to
390  * finish processing keys, and then call circuit_send_next_onion_skin() to
391  * extend to the next hop in the circuit if necessary.
392  */
393 static void
395 {
396  circuit_t *circ;
397  extended_cell_t extended_cell;
398 
399  circ = circuit_get_by_circid_channel(cell->circ_id, chan);
400 
401  if (!circ) {
402  log_info(LD_OR,
403  "(circID %u) unknown circ (probably got a destroy earlier). "
404  "Dropping.", (unsigned)cell->circ_id);
405  return;
406  }
407 
408  if (circ->n_circ_id != cell->circ_id || circ->n_chan != chan) {
409  log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
410  "got created cell from Tor client? Closing.");
411  circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
412  return;
413  }
414 
415  if (created_cell_parse(&extended_cell.created_cell, cell) < 0) {
416  log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unparseable created cell.");
417  circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
418  return;
419  }
420 
421  if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
422  origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
423  int err_reason = 0;
424  log_debug(LD_OR,"at OP. Finishing handshake.");
425  if ((err_reason = circuit_finish_handshake(origin_circ,
426  &extended_cell.created_cell)) < 0) {
427  circuit_mark_for_close(circ, -err_reason);
428  return;
429  }
430  log_debug(LD_OR,"Moving to next skin.");
431  if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
432  log_info(LD_OR,"circuit_send_next_onion_skin failed.");
433  /* XXX push this circuit_close lower */
434  circuit_mark_for_close(circ, -err_reason);
435  return;
436  }
437  } else { /* pack it into an extended relay cell, and send it. */
438  uint8_t command=0;
439  uint16_t len=0;
440  uint8_t payload[RELAY_PAYLOAD_SIZE];
441  log_debug(LD_OR,
442  "Converting created cell to extended relay cell, sending.");
443  memset(payload, 0, sizeof(payload));
444  if (extended_cell.created_cell.cell_type == CELL_CREATED2)
445  extended_cell.cell_type = RELAY_COMMAND_EXTENDED2;
446  else
447  extended_cell.cell_type = RELAY_COMMAND_EXTENDED;
448  if (extended_cell_format(&command, &len, payload, &extended_cell) < 0) {
449  log_fn(LOG_PROTOCOL_WARN, LD_OR, "Can't format extended cell.");
450  circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
451  return;
452  }
453 
454  relay_send_command_from_edge(0, circ, command,
455  (const char*)payload, len, NULL);
456  }
457 }
458 
459 /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
460  * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
461  * circuit_receive_relay_cell() for actual processing.
462  */
463 static void
465 {
466  const or_options_t *options = get_options();
467  circuit_t *circ;
468  int reason, direction;
469  uint32_t orig_delivered_bw = 0;
470  uint32_t orig_overhead_bw = 0;
471 
472  circ = circuit_get_by_circid_channel(cell->circ_id, chan);
473 
474  if (!circ) {
475  log_debug(LD_OR,
476  "unknown circuit %u on connection from %s. Dropping.",
477  (unsigned)cell->circ_id,
479  return;
480  }
481 
482  if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
483  log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
484  circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
485  return;
486  }
487 
488  if (CIRCUIT_IS_ORIGIN(circ)) {
489  /* if we're a relay and treating connections with recent local
490  * traffic better, then this is one of them. */
492 
493  /* Count all circuit bytes here for control port accuracy. We want
494  * to count even invalid/dropped relay cells, hence counting
495  * before the recognized check and the connection_edge_process_relay
496  * cell checks.
497  */
498  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
499 
500  /* Count the payload bytes only. We don't care about cell headers */
501  ocirc->n_read_circ_bw = tor_add_u32_nowrap(ocirc->n_read_circ_bw,
503 
504  /* Stash the original delivered and overhead values. These values are
505  * updated by circuit_read_valid_data() during cell processing by
506  * connection_edge_process_relay_cell(), called from
507  * circuit_receive_relay_cell() below. If they do not change, we inform
508  * the control port about dropped cells immediately after the call
509  * to circuit_receive_relay_cell() below. */
510  orig_delivered_bw = ocirc->n_delivered_read_circ_bw;
511  orig_overhead_bw = ocirc->n_overhead_read_circ_bw;
512  }
513 
514  if (!CIRCUIT_IS_ORIGIN(circ) &&
515  chan == TO_OR_CIRCUIT(circ)->p_chan &&
516  cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
517  direction = CELL_DIRECTION_OUT;
518  else
519  direction = CELL_DIRECTION_IN;
520 
521  /* If we have a relay_early cell, make sure that it's outbound, and we've
522  * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
523  if (cell->command == CELL_RELAY_EARLY) {
524  if (direction == CELL_DIRECTION_IN) {
525  /* Inbound early cells could once be encountered as a result of
526  * bug 1038; but relays running versions before 0.2.1.19 are long
527  * gone from the network, so any such cells now are surprising. */
528  log_warn(LD_OR,
529  "Received an inbound RELAY_EARLY cell on circuit %u."
530  " Closing circuit. Please report this event,"
531  " along with the following message.",
532  (unsigned)cell->circ_id);
533  if (CIRCUIT_IS_ORIGIN(circ)) {
535  /* Always emit a bandwidth event for closed circs */
537  } else if (circ->n_chan) {
538  log_warn(LD_OR, " upstream=%s",
540  }
541  circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
542  return;
543  } else {
544  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
545  if (or_circ->remaining_relay_early_cells == 0) {
546  log_fn(LOG_PROTOCOL_WARN, LD_OR,
547  "Received too many RELAY_EARLY cells on circ %u from %s."
548  " Closing circuit.",
549  (unsigned)cell->circ_id,
550  safe_str(channel_get_canonical_remote_descr(chan)));
551  circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
552  return;
553  }
554  --or_circ->remaining_relay_early_cells;
555  }
556  }
557 
558  if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
559  log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
560  "(%s) failed. Closing.",
561  direction==CELL_DIRECTION_OUT?"forward":"backward");
562  /* Always emit a bandwidth event for closed circs */
563  if (CIRCUIT_IS_ORIGIN(circ)) {
565  }
566  circuit_mark_for_close(circ, -reason);
567  }
568 
569  if (CIRCUIT_IS_ORIGIN(circ)) {
570  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
571 
572  /* If neither the delivered nor overhead values changed, this cell
573  * was dropped due to being invalid by one of the error codepaths in
574  * connection_edge_process_relay_cell(), called by
575  * circuit_receive_relay_cell().
576  *
577  * Valid cells, on the other hand, call circuit_read_valid_data()
578  * to update these values upon processing them.
579  *
580  * So, if the values are the same as those stored above,
581  * emit a control port event for CIRC_BW, so the controller can
582  * react quickly to invalid cells. */
583  if (orig_delivered_bw == ocirc->n_delivered_read_circ_bw &&
584  orig_overhead_bw == ocirc->n_overhead_read_circ_bw) {
586  }
587  }
588 
589  /* If this is a cell in an RP circuit, count it as part of the
590  hidden service stats */
591  if (options->HiddenServiceStatistics &&
592  !CIRCUIT_IS_ORIGIN(circ) &&
593  TO_OR_CIRCUIT(circ)->circuit_carries_hs_traffic_stats) {
595  }
596 }
597 
598 /** Process a 'destroy' <b>cell</b> that just arrived from
599  * <b>chan</b>. Find the circ that it refers to (if any).
600  *
601  * If the circ is in state
602  * onionskin_pending, then call onion_pending_remove() to remove it
603  * from the pending onion list (note that if it's already being
604  * processed by the cpuworker, it won't be in the list anymore; but
605  * when the cpuworker returns it, the circuit will be gone, and the
606  * cpuworker response will be dropped).
607  *
608  * Then mark the circuit for close (which marks all edges for close,
609  * and passes the destroy cell onward if necessary).
610  */
611 static void
613 {
614  circuit_t *circ;
615  int reason;
616 
617  circ = circuit_get_by_circid_channel(cell->circ_id, chan);
618  if (!circ) {
619  log_info(LD_OR,"unknown circuit %u on connection from %s. Dropping.",
620  (unsigned)cell->circ_id,
622  return;
623  }
624  log_debug(LD_OR,"Received for circID %u.",(unsigned)cell->circ_id);
625 
626  reason = (uint8_t)cell->payload[0];
627  circ->received_destroy = 1;
628 
629  if (!CIRCUIT_IS_ORIGIN(circ) &&
630  chan == TO_OR_CIRCUIT(circ)->p_chan &&
631  cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
632  /* the destroy came from behind */
634  circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
635  } else { /* the destroy came from ahead */
636  circuit_set_n_circid_chan(circ, 0, NULL);
637  if (CIRCUIT_IS_ORIGIN(circ)) {
638  circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
639  } else {
640  char payload[1];
641  log_debug(LD_OR, "Delivering 'truncated' back.");
642  payload[0] = (char)reason;
643  relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
644  payload, sizeof(payload), NULL);
645  }
646  }
647 }
648 
649 /** Callback to handle a new channel; call command_setup_channel() to give
650  * it the right cell handlers.
651  */
652 
653 static void
655 {
656  tor_assert(listener);
657  tor_assert(chan);
658 
659  command_setup_channel(chan);
660 }
661 
662 /** Given a channel, install the right handlers to process incoming
663  * cells on it.
664  */
665 
666 void
668 {
669  tor_assert(chan);
670 
673 }
674 
675 /** Given a listener, install the right handler to process incoming
676  * channels on it.
677  */
678 
679 void
681 {
682  tor_assert(listener);
684 
686 }
command_setup_channel
void command_setup_channel(channel_t *chan)
Definition: command.c:667
log_fn
#define log_fn(severity, domain, args,...)
Definition: log.h:287
circuit_get_by_circid_channel
circuit_t * circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1516
routermode.h
Header file for routermode.c.
tor_free
#define tor_free(p)
Definition: malloc.h:52
created_cell_t::cell_type
uint8_t cell_type
Definition: onion.h:38
channel_t::identity_digest
char identity_digest[DIGEST_LEN]
Definition: channel.h:383
circuit_t::purpose
uint8_t purpose
Definition: circuit_st.h:111
channel_is_client
int channel_is_client(const channel_t *chan)
Definition: channel.c:2912
memwipe
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:55
cell_command_to_string
const char * cell_command_to_string(uint8_t command)
Definition: command.c:89
created_cell_parse
int created_cell_parse(created_cell_t *cell_out, const cell_t *cell_in)
Definition: onion.c:209
circuit_send_next_onion_skin
int circuit_send_next_onion_skin(origin_circuit_t *circ)
Definition: circuitbuild.c:901
create_cell_t::handshake_type
uint16_t handshake_type
Definition: onion.h:28
channel_get_canonical_remote_descr
const char * channel_get_canonical_remote_descr(channel_t *chan)
Definition: channel.c:2835
extended_cell_t::cell_type
uint8_t cell_type
Definition: onion.h:67
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
circuit_set_p_circid_chan
void circuit_set_p_circid_chan(or_circuit_t *or_circ, circid_t id, channel_t *chan)
Definition: circuitlist.c:450
command_setup_listener
void command_setup_listener(channel_listener_t *listener)
Definition: command.c:680
channel.h
Header file for channel.c.
create_cell_t
Definition: onion.h:24
LD_GENERAL
#define LD_GENERAL
Definition: log.h:62
channel_get_actual_remote_descr
const char * channel_get_actual_remote_descr(channel_t *chan)
Definition: channel.c:2802
RELAY_PAYLOAD_SIZE
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
created_cell_t
Definition: onion.h:36
describe.h
Header file for describe.c.
node_get_by_id
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
extended_cell_t
Definition: onion.h:65
create_cell_parse
int create_cell_parse(create_cell_t *cell_out, const cell_t *cell_in)
Definition: onion.c:155
CIRCUIT_STATE_ONIONSKIN_PENDING
#define CIRCUIT_STATE_ONIONSKIN_PENDING
Definition: circuitlist.h:23
END_CIRC_REASON_FLAG_REMOTE
#define END_CIRC_REASON_FLAG_REMOTE
Definition: or.h:328
circuit_t::n_chan
channel_t * n_chan
Definition: circuit_st.h:69
channel_set_cell_handlers
void channel_set_cell_handlers(channel_t *chan, channel_cell_handler_fn_ptr cell_handler)
Definition: channel.c:1082
onion.h
Header file for onion.c.
we_are_hibernating
int we_are_hibernating(void)
Definition: hibernate.c:937
channel_listener_t::state
channel_listener_state_t state
Definition: channel.h:468
circuit_t::received_destroy
unsigned int received_destroy
Definition: circuit_st.h:104
CIRCUIT_IS_ORIGIN
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:146
stats_n_relay_cells_processed
uint64_t stats_n_relay_cells_processed
Definition: command.c:72
CELL_PAYLOAD_SIZE
#define CELL_PAYLOAD_SIZE
Definition: or.h:576
stats_n_created_cells_processed
uint64_t stats_n_created_cells_processed
Definition: command.c:70
channel_when_created
time_t channel_when_created(channel_t *chan)
Definition: channel.c:3265
circuit_receive_relay_cell
int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ, cell_direction_t cell_direction)
Definition: relay.c:224
command_process_created_cell
static void command_process_created_cell(cell_t *cell, channel_t *chan)
Definition: command.c:394
cell_t
Definition: cell_st.h:17
CIRCUIT_PURPOSE_OR
#define CIRCUIT_PURPOSE_OR
Definition: circuitlist.h:39
crypto_util.h
Common functions for cryptographic routines.
hibernate.h
Header file for hibernate.c.
create_cell_t::onionskin
uint8_t onionskin[CELL_PAYLOAD_SIZE - 4]
Definition: onion.h:32
or_circuit_t::p_chan
channel_t * p_chan
Definition: or_circuit_st.h:37
CELL_DIRECTION_OUT
@ CELL_DIRECTION_OUT
Definition: or.h:484
circuitlist.h
Header file for circuitlist.c.
circuit_log_path
void circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
Definition: circuitbuild.c:348
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
control_event_circ_bandwidth_used_for_circ
int control_event_circ_bandwidth_used_for_circ(origin_circuit_t *ocirc)
Definition: control_events.c:997
channel_timestamp_client
void channel_timestamp_client(channel_t *chan)
Definition: channel.c:3207
circuitbuild_relay.h
Header for feature/relay/circuitbuild_relay.c.
command.h
Header file for command.c.
or_circuit_t::remaining_relay_early_cells
unsigned int remaining_relay_early_cells
Definition: or_circuit_st.h:57
channel_listener_set_listener_fn
void channel_listener_set_listener_fn(channel_listener_t *chan_l, channel_listener_fn_ptr listener)
Definition: channel.c:1042
node_get_platform
const char * node_get_platform(const node_t *node)
Definition: nodelist.c:1622
node_t
Definition: node_st.h:34
origin_circuit_t
Definition: origin_circuit_st.h:79
LD_OR
#define LD_OR
Definition: log.h:92
channel_t::circ_id_type
circ_id_type_bitfield_t circ_id_type
Definition: channel.h:410
created_cell_t::handshake_len
uint16_t handshake_len
Definition: onion.h:40
circuit_t
Definition: circuit_st.h:61
nodelist.h
Header file for nodelist.c.
or_options_t::HiddenServiceStatistics
int HiddenServiceStatistics
Definition: or_options_st.h:632
circuit_finish_handshake
int circuit_finish_handshake(origin_circuit_t *circ, const created_cell_t *reply)
Definition: circuitbuild.c:1177
extended_cell_format
int extended_cell_format(uint8_t *command_out, uint16_t *len_out, uint8_t *payload_out, const extended_cell_t *cell_in)
Definition: onion.c:714
routerlist.h
Header file for routerlist.c.
command_handle_incoming_channel
static void command_handle_incoming_channel(channel_listener_t *listener, channel_t *chan)
Definition: command.c:654
tor_gettimeofday
void tor_gettimeofday(struct timeval *timeval)
Definition: tor_gettimeofday.c:42
or_circuit_new
or_circuit_t * or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
Definition: circuitlist.c:1087
cell_t::command
uint8_t command
Definition: cell_st.h:19
cell_t::payload
uint8_t payload[CELL_PAYLOAD_SIZE]
Definition: cell_st.h:21
command_process_create_cell
static void command_process_create_cell(cell_t *cell, channel_t *chan)
Definition: command.c:227
channel_t
Definition: channel.h:181
command
tor_cmdline_mode_t command
Definition: config.c:2444
circuit_t::n_circ_id
circid_t n_circ_id
Definition: circuit_st.h:78
command_process_relay_cell
static void command_process_relay_cell(cell_t *cell, channel_t *chan)
Definition: command.c:464
LOG_INFO
#define LOG_INFO
Definition: log.h:45
CIRC_ID_TYPE_LOWER
@ CIRC_ID_TYPE_LOWER
Definition: channel.h:40
control_events.h
Header file for control_events.c.
get_options
const or_options_t * get_options(void)
Definition: config.c:925
rep_hist_note_circuit_handshake_requested
void rep_hist_note_circuit_handshake_requested(uint16_t type)
Definition: rephist.c:2443
command_process_destroy_cell
static void command_process_destroy_cell(cell_t *cell, channel_t *chan)
Definition: command.c:612
var_cell_st.h
Variable-length cell structure.
circuitbuild.h
Header file for circuitbuild.c.
tv_udiff
long tv_udiff(const struct timeval *start, const struct timeval *end)
Definition: tvdiff.c:53
command_process_cell
void command_process_cell(channel_t *chan, cell_t *cell)
Definition: command.c:149
current_second
static time_t current_second
Definition: mainloop.c:2184
circuit_id_in_use_on_channel
int circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
Definition: circuitlist.c:1545
node_describe
const char * node_describe(const node_t *node)
Definition: describe.c:143
TO_ORIGIN_CIRCUIT
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:165
relay.h
Header file for relay.c.
connection.h
Header file for connection.c.
onionskin_answer
int onionskin_answer(struct or_circuit_t *circ, const created_cell_t *created_cell, const char *keys, size_t keys_len, const uint8_t *rend_circ_nonce)
Definition: circuitbuild_relay.c:484
circuit_set_n_circid_chan
void circuit_set_n_circid_chan(circuit_t *circ, circid_t id, channel_t *chan)
Definition: circuitlist.c:473
server_mode
int server_mode(const or_options_t *options)
Definition: routermode.c:34
timeval
Definition: compat_time.h:151
cpuworker.h
Header file for cpuworker.c.
channel_listener_t
Definition: channel.h:466
created_cell_t::reply
uint8_t reply[CELL_PAYLOAD_SIZE - 2]
Definition: onion.h:42
public_server_mode
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
origin_circuit_t::n_delivered_read_circ_bw
uint32_t n_delivered_read_circ_bw
Definition: origin_circuit_st.h:103
onion_crypto.h
Header file for onion_crypto.c.
LOG_WARN
#define LOG_WARN
Definition: log.h:53
origin_circuit_t::n_read_circ_bw
uint32_t n_read_circ_bw
Definition: origin_circuit_st.h:93
cell_st.h
Fixed-size cell structure.
rep_hist_seen_new_rp_cell
void rep_hist_seen_new_rp_cell(void)
Definition: rephist.c:2556
or_circuit_t
Definition: or_circuit_st.h:21
or_circuit_t::p_circ_id
circid_t p_circ_id
Definition: or_circuit_st.h:33
circuit_set_state
void circuit_set_state(circuit_t *circ, uint8_t state)
Definition: circuitlist.c:542
config.h
Header file for config.c.
extended_cell_t::created_cell
created_cell_t created_cell
Definition: onion.h:69
channel_t::global_identifier
uint64_t global_identifier
Definition: channel.h:197
esc_for_log
char * esc_for_log(const char *s)
Definition: escape.c:30
CIRC_ID_TYPE_HIGHER
@ CIRC_ID_TYPE_HIGHER
Definition: channel.h:41
TO_CIRCUIT
#define TO_CIRCUIT(x)
Definition: or.h:951
or_options_t
Definition: or_options_st.h:39
CHANNEL_LISTENER_STATE_LISTENING
@ CHANNEL_LISTENER_STATE_LISTENING
Definition: channel.h:146
onion_skin_server_handshake
int onion_skin_server_handshake(int type, const uint8_t *onion_skin, size_t onionskin_len, const server_onion_keys_t *keys, uint8_t *reply_out, uint8_t *keys_out, size_t keys_out_len, uint8_t *rend_nonce_out)
Definition: onion_crypto.c:174
origin_circuit_t::n_overhead_read_circ_bw
uint32_t n_overhead_read_circ_bw
Definition: origin_circuit_st.h:113
stats_n_create_cells_processed
uint64_t stats_n_create_cells_processed
Definition: command.c:68
channel_is_outgoing
int channel_is_outgoing(channel_t *chan)
Definition: channel.c:3059
stats_n_destroy_cells_processed
uint64_t stats_n_destroy_cells_processed
Definition: command.c:74
channel_send_destroy
int channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
Definition: channel.c:2011
TO_OR_CIRCUIT
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:153
cell_t::circ_id
circid_t circ_id
Definition: cell_st.h:18
origin_circuit_st.h
Origin circuit structure.
create_cell_t::handshake_len
uint16_t handshake_len
Definition: onion.h:30
assign_onionskin_to_cpuworker
int assign_onionskin_to_cpuworker(or_circuit_t *circ, create_cell_t *onionskin)
Definition: cpuworker.c:513
circuit_t::state
uint8_t state
Definition: circuit_st.h:110
CELL_DIRECTION_IN
@ CELL_DIRECTION_IN
Definition: or.h:483
rephist.h
Header file for rephist.c.
connection_or.h
Header file for connection_or.c.
or.h
Master header file for Tor-specific functionality.
LD_PROTOCOL
#define LD_PROTOCOL
Definition: log.h:72