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