In routerlist_assert_ok(), check r2 before taking &(r2->cache_info)
[tor.git] / src / or / circpathbias.c
blob51a75cf50233178204dfa0e577e6fe311e16c7af
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 */
7 #include "or.h"
8 #include "channel.h"
9 #include "circpathbias.h"
10 #include "circuitbuild.h"
11 #include "circuitlist.h"
12 #include "circuituse.h"
13 #include "circuitstats.h"
14 #include "connection_edge.h"
15 #include "config.h"
16 #include "entrynodes.h"
17 #include "networkstatus.h"
18 #include "relay.h"
20 static void pathbias_count_successful_close(origin_circuit_t *circ);
21 static void pathbias_count_collapse(origin_circuit_t *circ);
22 static void pathbias_count_use_failed(origin_circuit_t *circ);
23 static void pathbias_measure_use_rate(entry_guard_t *guard);
24 static void pathbias_measure_close_rate(entry_guard_t *guard);
25 static void pathbias_scale_use_rates(entry_guard_t *guard);
26 static void pathbias_scale_close_rates(entry_guard_t *guard);
27 static int entry_guard_inc_circ_attempt_count(entry_guard_t *guard);
29 /** Increment the number of times we successfully extended a circuit to
30 * <b>guard</b>, first checking if the failure rate is high enough that
31 * we should eliminate the guard. Return -1 if the guard looks no good;
32 * return 0 if the guard looks fine.
34 static int
35 entry_guard_inc_circ_attempt_count(entry_guard_t *guard)
37 entry_guards_changed();
39 pathbias_measure_close_rate(guard);
41 if (guard->path_bias_disabled)
42 return -1;
44 pathbias_scale_close_rates(guard);
45 guard->circ_attempts++;
47 log_info(LD_CIRC, "Got success count %f/%f for guard %s ($%s)",
48 guard->circ_successes, guard->circ_attempts, guard->nickname,
49 hex_str(guard->identity, DIGEST_LEN));
50 return 0;
53 /** The minimum number of circuit attempts before we start
54 * thinking about warning about path bias and dropping guards */
55 static int
56 pathbias_get_min_circs(const or_options_t *options)
58 #define DFLT_PATH_BIAS_MIN_CIRC 150
59 if (options->PathBiasCircThreshold >= 5)
60 return options->PathBiasCircThreshold;
61 else
62 return networkstatus_get_param(NULL, "pb_mincircs",
63 DFLT_PATH_BIAS_MIN_CIRC,
64 5, INT32_MAX);
67 /** The circuit success rate below which we issue a notice */
68 static double
69 pathbias_get_notice_rate(const or_options_t *options)
71 #define DFLT_PATH_BIAS_NOTICE_PCT 70
72 if (options->PathBiasNoticeRate >= 0.0)
73 return options->PathBiasNoticeRate;
74 else
75 return networkstatus_get_param(NULL, "pb_noticepct",
76 DFLT_PATH_BIAS_NOTICE_PCT, 0, 100)/100.0;
79 /* XXXX024 I'd like to have this be static again, but entrynodes.c needs it. */
80 /** The circuit success rate below which we issue a warn */
81 static double
82 pathbias_get_warn_rate(const or_options_t *options)
84 #define DFLT_PATH_BIAS_WARN_PCT 50
85 if (options->PathBiasWarnRate >= 0.0)
86 return options->PathBiasWarnRate;
87 else
88 return networkstatus_get_param(NULL, "pb_warnpct",
89 DFLT_PATH_BIAS_WARN_PCT, 0, 100)/100.0;
92 /* XXXX024 I'd like to have this be static again, but entrynodes.c needs it. */
93 /**
94 * The extreme rate is the rate at which we would drop the guard,
95 * if pb_dropguard is also set. Otherwise we just warn.
97 double
98 pathbias_get_extreme_rate(const or_options_t *options)
100 #define DFLT_PATH_BIAS_EXTREME_PCT 30
101 if (options->PathBiasExtremeRate >= 0.0)
102 return options->PathBiasExtremeRate;
103 else
104 return networkstatus_get_param(NULL, "pb_extremepct",
105 DFLT_PATH_BIAS_EXTREME_PCT, 0, 100)/100.0;
108 /* XXXX024 I'd like to have this be static again, but entrynodes.c needs it. */
110 * If 1, we actually disable use of guards that fall below
111 * the extreme_pct.
114 pathbias_get_dropguards(const or_options_t *options)
116 #define DFLT_PATH_BIAS_DROP_GUARDS 0
117 if (options->PathBiasDropGuards >= 0)
118 return options->PathBiasDropGuards;
119 else
120 return networkstatus_get_param(NULL, "pb_dropguards",
121 DFLT_PATH_BIAS_DROP_GUARDS, 0, 1);
125 * This is the number of circuits at which we scale our
126 * counts by mult_factor/scale_factor. Note, this count is
127 * not exact, as we only perform the scaling in the event
128 * of no integer truncation.
130 static int
131 pathbias_get_scale_threshold(const or_options_t *options)
133 #define DFLT_PATH_BIAS_SCALE_THRESHOLD 300
134 if (options->PathBiasScaleThreshold >= 10)
135 return options->PathBiasScaleThreshold;
136 else
137 return networkstatus_get_param(NULL, "pb_scalecircs",
138 DFLT_PATH_BIAS_SCALE_THRESHOLD, 10,
139 INT32_MAX);
143 * Compute the path bias scaling ratio from the consensus
144 * parameters pb_multfactor/pb_scalefactor.
146 * Returns a value in (0, 1.0] which we multiply our pathbias
147 * counts with to scale them down.
149 static double
150 pathbias_get_scale_ratio(const or_options_t *options)
153 * The scale factor is the denominator for our scaling
154 * of circuit counts for our path bias window.
156 * Note that our use of doubles for the path bias state
157 * file means that powers of 2 work best here.
159 int denominator = networkstatus_get_param(NULL, "pb_scalefactor",
160 2, 2, INT32_MAX);
161 (void) options;
163 * The mult factor is the numerator for our scaling
164 * of circuit counts for our path bias window. It
165 * allows us to scale by fractions.
167 return networkstatus_get_param(NULL, "pb_multfactor",
168 1, 1, denominator)/((double)denominator);
171 /** The minimum number of circuit usage attempts before we start
172 * thinking about warning about path use bias and dropping guards */
173 static int
174 pathbias_get_min_use(const or_options_t *options)
176 #define DFLT_PATH_BIAS_MIN_USE 20
177 if (options->PathBiasUseThreshold >= 3)
178 return options->PathBiasUseThreshold;
179 else
180 return networkstatus_get_param(NULL, "pb_minuse",
181 DFLT_PATH_BIAS_MIN_USE,
182 3, INT32_MAX);
185 /** The circuit use success rate below which we issue a notice */
186 static double
187 pathbias_get_notice_use_rate(const or_options_t *options)
189 #define DFLT_PATH_BIAS_NOTICE_USE_PCT 80
190 if (options->PathBiasNoticeUseRate >= 0.0)
191 return options->PathBiasNoticeUseRate;
192 else
193 return networkstatus_get_param(NULL, "pb_noticeusepct",
194 DFLT_PATH_BIAS_NOTICE_USE_PCT,
195 0, 100)/100.0;
199 * The extreme use rate is the rate at which we would drop the guard,
200 * if pb_dropguard is also set. Otherwise we just warn.
202 double
203 pathbias_get_extreme_use_rate(const or_options_t *options)
205 #define DFLT_PATH_BIAS_EXTREME_USE_PCT 60
206 if (options->PathBiasExtremeUseRate >= 0.0)
207 return options->PathBiasExtremeUseRate;
208 else
209 return networkstatus_get_param(NULL, "pb_extremeusepct",
210 DFLT_PATH_BIAS_EXTREME_USE_PCT,
211 0, 100)/100.0;
215 * This is the number of circuits at which we scale our
216 * use counts by mult_factor/scale_factor. Note, this count is
217 * not exact, as we only perform the scaling in the event
218 * of no integer truncation.
220 static int
221 pathbias_get_scale_use_threshold(const or_options_t *options)
223 #define DFLT_PATH_BIAS_SCALE_USE_THRESHOLD 100
224 if (options->PathBiasScaleUseThreshold >= 10)
225 return options->PathBiasScaleUseThreshold;
226 else
227 return networkstatus_get_param(NULL, "pb_scaleuse",
228 DFLT_PATH_BIAS_SCALE_USE_THRESHOLD,
229 10, INT32_MAX);
233 * Convert a Guard's path state to string.
235 const char *
236 pathbias_state_to_string(path_state_t state)
238 switch (state) {
239 case PATH_STATE_NEW_CIRC:
240 return "new";
241 case PATH_STATE_BUILD_ATTEMPTED:
242 return "build attempted";
243 case PATH_STATE_BUILD_SUCCEEDED:
244 return "build succeeded";
245 case PATH_STATE_USE_ATTEMPTED:
246 return "use attempted";
247 case PATH_STATE_USE_SUCCEEDED:
248 return "use succeeded";
249 case PATH_STATE_USE_FAILED:
250 return "use failed";
251 case PATH_STATE_ALREADY_COUNTED:
252 return "already counted";
255 return "unknown";
259 * This function decides if a circuit has progressed far enough to count
260 * as a circuit "attempt". As long as end-to-end tagging is possible,
261 * we assume the adversary will use it over hop-to-hop failure. Therefore,
262 * we only need to account bias for the last hop. This should make us
263 * much more resilient to ambient circuit failure, and also make that
264 * failure easier to measure (we only need to measure Exit failure rates).
266 static int
267 pathbias_is_new_circ_attempt(origin_circuit_t *circ)
269 #define N2N_TAGGING_IS_POSSIBLE
270 #ifdef N2N_TAGGING_IS_POSSIBLE
271 /* cpath is a circular list. We want circs with more than one hop,
272 * and the second hop must be waiting for keys still (it's just
273 * about to get them). */
274 return circ->cpath &&
275 circ->cpath->next != circ->cpath &&
276 circ->cpath->next->state == CPATH_STATE_AWAITING_KEYS;
277 #else
278 /* If tagging attacks are no longer possible, we probably want to
279 * count bias from the first hop. However, one could argue that
280 * timing-based tagging is still more useful than per-hop failure.
281 * In which case, we'd never want to use this.
283 return circ->cpath &&
284 circ->cpath->state == CPATH_STATE_AWAITING_KEYS;
285 #endif
289 * Decide if the path bias code should count a circuit.
291 * @returns 1 if we should count it, 0 otherwise.
293 static int
294 pathbias_should_count(origin_circuit_t *circ)
296 #define PATHBIAS_COUNT_INTERVAL (600)
297 static ratelim_t count_limit =
298 RATELIM_INIT(PATHBIAS_COUNT_INTERVAL);
299 char *rate_msg = NULL;
301 /* We can't do path bias accounting without entry guards.
302 * Testing and controller circuits also have no guards.
304 * We also don't count server-side rends, because their
305 * endpoint could be chosen maliciously.
306 * Similarly, we can't count client-side intro attempts,
307 * because clients can be manipulated into connecting to
308 * malicious intro points. */
309 if (get_options()->UseEntryGuards == 0 ||
310 circ->base_.purpose == CIRCUIT_PURPOSE_TESTING ||
311 circ->base_.purpose == CIRCUIT_PURPOSE_CONTROLLER ||
312 circ->base_.purpose == CIRCUIT_PURPOSE_S_CONNECT_REND ||
313 circ->base_.purpose == CIRCUIT_PURPOSE_S_REND_JOINED ||
314 (circ->base_.purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
315 circ->base_.purpose <= CIRCUIT_PURPOSE_C_INTRODUCE_ACKED)) {
317 /* Check to see if the shouldcount result has changed due to a
318 * unexpected purpose change that would affect our results.
320 * The reason we check the path state too here is because for the
321 * cannibalized versions of these purposes, we count them as successful
322 * before their purpose change.
324 if (circ->pathbias_shouldcount == PATHBIAS_SHOULDCOUNT_COUNTED
325 && circ->path_state != PATH_STATE_ALREADY_COUNTED) {
326 log_info(LD_BUG,
327 "Circuit %d is now being ignored despite being counted "
328 "in the past. Purpose is %s, path state is %s",
329 circ->global_identifier,
330 circuit_purpose_to_string(circ->base_.purpose),
331 pathbias_state_to_string(circ->path_state));
333 circ->pathbias_shouldcount = PATHBIAS_SHOULDCOUNT_IGNORED;
334 return 0;
337 /* Completely ignore one hop circuits */
338 if (circ->build_state->onehop_tunnel ||
339 circ->build_state->desired_path_len == 1) {
340 /* Check for inconsistency */
341 if (circ->build_state->desired_path_len != 1 ||
342 !circ->build_state->onehop_tunnel) {
343 if ((rate_msg = rate_limit_log(&count_limit, approx_time()))) {
344 log_info(LD_BUG,
345 "One-hop circuit has length %d. Path state is %s. "
346 "Circuit is a %s currently %s.%s",
347 circ->build_state->desired_path_len,
348 pathbias_state_to_string(circ->path_state),
349 circuit_purpose_to_string(circ->base_.purpose),
350 circuit_state_to_string(circ->base_.state),
351 rate_msg);
352 tor_free(rate_msg);
354 tor_fragile_assert();
357 /* Check to see if the shouldcount result has changed due to a
358 * unexpected change that would affect our results */
359 if (circ->pathbias_shouldcount == PATHBIAS_SHOULDCOUNT_COUNTED) {
360 log_info(LD_BUG,
361 "One-hop circuit %d is now being ignored despite being counted "
362 "in the past. Purpose is %s, path state is %s",
363 circ->global_identifier,
364 circuit_purpose_to_string(circ->base_.purpose),
365 pathbias_state_to_string(circ->path_state));
367 circ->pathbias_shouldcount = PATHBIAS_SHOULDCOUNT_IGNORED;
368 return 0;
371 /* Check to see if the shouldcount result has changed due to a
372 * unexpected purpose change that would affect our results */
373 if (circ->pathbias_shouldcount == PATHBIAS_SHOULDCOUNT_IGNORED) {
374 log_info(LD_BUG,
375 "Circuit %d is now being counted despite being ignored "
376 "in the past. Purpose is %s, path state is %s",
377 circ->global_identifier,
378 circuit_purpose_to_string(circ->base_.purpose),
379 pathbias_state_to_string(circ->path_state));
381 circ->pathbias_shouldcount = PATHBIAS_SHOULDCOUNT_COUNTED;
383 return 1;
387 * Check our circuit state to see if this is a successful circuit attempt.
388 * If so, record it in the current guard's path bias circ_attempt count.
390 * Also check for several potential error cases for bug #6475.
393 pathbias_count_build_attempt(origin_circuit_t *circ)
395 #define CIRC_ATTEMPT_NOTICE_INTERVAL (600)
396 static ratelim_t circ_attempt_notice_limit =
397 RATELIM_INIT(CIRC_ATTEMPT_NOTICE_INTERVAL);
398 char *rate_msg = NULL;
400 if (!pathbias_should_count(circ)) {
401 return 0;
404 if (pathbias_is_new_circ_attempt(circ)) {
405 /* Help track down the real cause of bug #6475: */
406 if (circ->has_opened && circ->path_state != PATH_STATE_BUILD_ATTEMPTED) {
407 if ((rate_msg = rate_limit_log(&circ_attempt_notice_limit,
408 approx_time()))) {
409 log_info(LD_BUG,
410 "Opened circuit is in strange path state %s. "
411 "Circuit is a %s currently %s.%s",
412 pathbias_state_to_string(circ->path_state),
413 circuit_purpose_to_string(circ->base_.purpose),
414 circuit_state_to_string(circ->base_.state),
415 rate_msg);
416 tor_free(rate_msg);
420 /* Don't re-count cannibalized circs.. */
421 if (!circ->has_opened) {
422 entry_guard_t *guard = NULL;
424 if (circ->cpath && circ->cpath->extend_info) {
425 guard = entry_guard_get_by_id_digest(
426 circ->cpath->extend_info->identity_digest);
427 } else if (circ->base_.n_chan) {
428 guard =
429 entry_guard_get_by_id_digest(circ->base_.n_chan->identity_digest);
432 if (guard) {
433 if (circ->path_state == PATH_STATE_NEW_CIRC) {
434 circ->path_state = PATH_STATE_BUILD_ATTEMPTED;
436 if (entry_guard_inc_circ_attempt_count(guard) < 0) {
437 /* Bogus guard; we already warned. */
438 return -END_CIRC_REASON_TORPROTOCOL;
440 } else {
441 if ((rate_msg = rate_limit_log(&circ_attempt_notice_limit,
442 approx_time()))) {
443 log_info(LD_BUG,
444 "Unopened circuit has strange path state %s. "
445 "Circuit is a %s currently %s.%s",
446 pathbias_state_to_string(circ->path_state),
447 circuit_purpose_to_string(circ->base_.purpose),
448 circuit_state_to_string(circ->base_.state),
449 rate_msg);
450 tor_free(rate_msg);
453 } else {
454 if ((rate_msg = rate_limit_log(&circ_attempt_notice_limit,
455 approx_time()))) {
456 log_info(LD_CIRC,
457 "Unopened circuit has no known guard. "
458 "Circuit is a %s currently %s.%s",
459 circuit_purpose_to_string(circ->base_.purpose),
460 circuit_state_to_string(circ->base_.state),
461 rate_msg);
462 tor_free(rate_msg);
468 return 0;
472 * Check our circuit state to see if this is a successful circuit
473 * completion. If so, record it in the current guard's path bias
474 * success count.
476 * Also check for several potential error cases for bug #6475.
478 void
479 pathbias_count_build_success(origin_circuit_t *circ)
481 #define SUCCESS_NOTICE_INTERVAL (600)
482 static ratelim_t success_notice_limit =
483 RATELIM_INIT(SUCCESS_NOTICE_INTERVAL);
484 char *rate_msg = NULL;
485 entry_guard_t *guard = NULL;
487 if (!pathbias_should_count(circ)) {
488 return;
491 /* Don't count cannibalized/reused circs for path bias
492 * "build" success, since they get counted under "use" success. */
493 if (!circ->has_opened) {
494 if (circ->cpath && circ->cpath->extend_info) {
495 guard = entry_guard_get_by_id_digest(
496 circ->cpath->extend_info->identity_digest);
499 if (guard) {
500 if (circ->path_state == PATH_STATE_BUILD_ATTEMPTED) {
501 circ->path_state = PATH_STATE_BUILD_SUCCEEDED;
502 guard->circ_successes++;
503 entry_guards_changed();
505 log_info(LD_CIRC, "Got success count %f/%f for guard %s ($%s)",
506 guard->circ_successes, guard->circ_attempts,
507 guard->nickname, hex_str(guard->identity, DIGEST_LEN));
508 } else {
509 if ((rate_msg = rate_limit_log(&success_notice_limit,
510 approx_time()))) {
511 log_info(LD_BUG,
512 "Succeeded circuit is in strange path state %s. "
513 "Circuit is a %s currently %s.%s",
514 pathbias_state_to_string(circ->path_state),
515 circuit_purpose_to_string(circ->base_.purpose),
516 circuit_state_to_string(circ->base_.state),
517 rate_msg);
518 tor_free(rate_msg);
522 if (guard->circ_attempts < guard->circ_successes) {
523 log_notice(LD_BUG, "Unexpectedly high successes counts (%f/%f) "
524 "for guard %s ($%s)",
525 guard->circ_successes, guard->circ_attempts,
526 guard->nickname, hex_str(guard->identity, DIGEST_LEN));
528 /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
529 * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
530 * No need to log that case. */
531 } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
532 if ((rate_msg = rate_limit_log(&success_notice_limit,
533 approx_time()))) {
534 log_info(LD_CIRC,
535 "Completed circuit has no known guard. "
536 "Circuit is a %s currently %s.%s",
537 circuit_purpose_to_string(circ->base_.purpose),
538 circuit_state_to_string(circ->base_.state),
539 rate_msg);
540 tor_free(rate_msg);
543 } else {
544 if (circ->path_state < PATH_STATE_BUILD_SUCCEEDED) {
545 if ((rate_msg = rate_limit_log(&success_notice_limit,
546 approx_time()))) {
547 log_info(LD_BUG,
548 "Opened circuit is in strange path state %s. "
549 "Circuit is a %s currently %s.%s",
550 pathbias_state_to_string(circ->path_state),
551 circuit_purpose_to_string(circ->base_.purpose),
552 circuit_state_to_string(circ->base_.state),
553 rate_msg);
554 tor_free(rate_msg);
561 * Record an attempt to use a circuit. Changes the circuit's
562 * path state and update its guard's usage counter.
564 * Used for path bias usage accounting.
566 void
567 pathbias_count_use_attempt(origin_circuit_t *circ)
569 entry_guard_t *guard;
571 if (!pathbias_should_count(circ)) {
572 return;
575 if (circ->path_state < PATH_STATE_BUILD_SUCCEEDED) {
576 log_notice(LD_BUG,
577 "Used circuit is in strange path state %s. "
578 "Circuit is a %s currently %s.",
579 pathbias_state_to_string(circ->path_state),
580 circuit_purpose_to_string(circ->base_.purpose),
581 circuit_state_to_string(circ->base_.state));
582 } else if (circ->path_state < PATH_STATE_USE_ATTEMPTED) {
583 guard = entry_guard_get_by_id_digest(
584 circ->cpath->extend_info->identity_digest);
585 if (guard) {
586 pathbias_measure_use_rate(guard);
587 pathbias_scale_use_rates(guard);
588 guard->use_attempts++;
589 entry_guards_changed();
591 log_debug(LD_CIRC,
592 "Marked circuit %d (%f/%f) as used for guard %s ($%s).",
593 circ->global_identifier,
594 guard->use_successes, guard->use_attempts,
595 guard->nickname, hex_str(guard->identity, DIGEST_LEN));
598 circ->path_state = PATH_STATE_USE_ATTEMPTED;
599 } else {
600 /* Harmless but educational log message */
601 log_info(LD_CIRC,
602 "Used circuit %d is already in path state %s. "
603 "Circuit is a %s currently %s.",
604 circ->global_identifier,
605 pathbias_state_to_string(circ->path_state),
606 circuit_purpose_to_string(circ->base_.purpose),
607 circuit_state_to_string(circ->base_.state));
610 return;
614 * Check the circuit's path state is appropriate and mark it as
615 * successfully used. Used for path bias usage accounting.
617 * We don't actually increment the guard's counters until
618 * pathbias_check_close(), because the circuit can still transition
619 * back to PATH_STATE_USE_ATTEMPTED if a stream fails later (this
620 * is done so we can probe the circuit for liveness at close).
622 void
623 pathbias_mark_use_success(origin_circuit_t *circ)
625 if (!pathbias_should_count(circ)) {
626 return;
629 if (circ->path_state < PATH_STATE_USE_ATTEMPTED) {
630 log_notice(LD_BUG,
631 "Used circuit %d is in strange path state %s. "
632 "Circuit is a %s currently %s.",
633 circ->global_identifier,
634 pathbias_state_to_string(circ->path_state),
635 circuit_purpose_to_string(circ->base_.purpose),
636 circuit_state_to_string(circ->base_.state));
638 pathbias_count_use_attempt(circ);
641 /* We don't do any accounting at the guard until actual circuit close */
642 circ->path_state = PATH_STATE_USE_SUCCEEDED;
644 return;
648 * If a stream ever detatches from a circuit in a retriable way,
649 * we need to mark this circuit as still needing either another
650 * successful stream, or in need of a probe.
652 * An adversary could let the first stream request succeed (ie the
653 * resolve), but then tag and timeout the remainder (via cell
654 * dropping), forcing them on new circuits.
656 * Rolling back the state will cause us to probe such circuits, which
657 * should lead to probe failures in the event of such tagging due to
658 * either unrecognized cells coming in while we wait for the probe,
659 * or the cipher state getting out of sync in the case of dropped cells.
661 void
662 pathbias_mark_use_rollback(origin_circuit_t *circ)
664 if (circ->path_state == PATH_STATE_USE_SUCCEEDED) {
665 log_info(LD_CIRC,
666 "Rolling back pathbias use state to 'attempted' for detached "
667 "circuit %d", circ->global_identifier);
668 circ->path_state = PATH_STATE_USE_ATTEMPTED;
673 * Actually count a circuit success towards a guard's usage counters
674 * if the path state is appropriate.
676 static void
677 pathbias_count_use_success(origin_circuit_t *circ)
679 entry_guard_t *guard;
681 if (!pathbias_should_count(circ)) {
682 return;
685 if (circ->path_state != PATH_STATE_USE_SUCCEEDED) {
686 log_notice(LD_BUG,
687 "Successfully used circuit %d is in strange path state %s. "
688 "Circuit is a %s currently %s.",
689 circ->global_identifier,
690 pathbias_state_to_string(circ->path_state),
691 circuit_purpose_to_string(circ->base_.purpose),
692 circuit_state_to_string(circ->base_.state));
693 } else {
694 guard = entry_guard_get_by_id_digest(
695 circ->cpath->extend_info->identity_digest);
696 if (guard) {
697 guard->use_successes++;
698 entry_guards_changed();
700 if (guard->use_attempts < guard->use_successes) {
701 log_notice(LD_BUG, "Unexpectedly high use successes counts (%f/%f) "
702 "for guard %s=%s",
703 guard->use_successes, guard->use_attempts,
704 guard->nickname, hex_str(guard->identity, DIGEST_LEN));
707 log_debug(LD_CIRC,
708 "Marked circuit %d (%f/%f) as used successfully for guard "
709 "%s ($%s).",
710 circ->global_identifier, guard->use_successes,
711 guard->use_attempts, guard->nickname,
712 hex_str(guard->identity, DIGEST_LEN));
716 return;
720 * Send a probe down a circuit that the client attempted to use,
721 * but for which the stream timed out/failed. The probe is a
722 * RELAY_BEGIN cell with a 0.a.b.c destination address, which
723 * the exit will reject and reply back, echoing that address.
725 * The reason for such probes is because it is possible to bias
726 * a user's paths simply by causing timeouts, and these timeouts
727 * are not possible to differentiate from unresponsive servers.
729 * The probe is sent at the end of the circuit lifetime for two
730 * reasons: to prevent cryptographic taggers from being able to
731 * drop cells to cause timeouts, and to prevent easy recognition
732 * of probes before any real client traffic happens.
734 * Returns -1 if we couldn't probe, 0 otherwise.
736 static int
737 pathbias_send_usable_probe(circuit_t *circ)
739 /* Based on connection_ap_handshake_send_begin() */
740 char payload[CELL_PAYLOAD_SIZE];
741 int payload_len;
742 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
743 crypt_path_t *cpath_layer = NULL;
744 char *probe_nonce = NULL;
746 tor_assert(ocirc);
748 cpath_layer = ocirc->cpath->prev;
750 if (cpath_layer->state != CPATH_STATE_OPEN) {
751 /* This can happen for cannibalized circuits. Their
752 * last hop isn't yet open */
753 log_info(LD_CIRC,
754 "Got pathbias probe request for unopened circuit %d. "
755 "Opened %d, len %d", ocirc->global_identifier,
756 ocirc->has_opened, ocirc->build_state->desired_path_len);
757 return -1;
760 /* We already went down this road. */
761 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING &&
762 ocirc->pathbias_probe_id) {
763 log_info(LD_CIRC,
764 "Got pathbias probe request for circuit %d with "
765 "outstanding probe", ocirc->global_identifier);
766 return -1;
769 /* Can't probe if the channel isn't open */
770 if (circ->n_chan == NULL ||
771 (circ->n_chan->state != CHANNEL_STATE_OPEN
772 && circ->n_chan->state != CHANNEL_STATE_MAINT)) {
773 log_info(LD_CIRC,
774 "Skipping pathbias probe for circuit %d: Channel is not open.",
775 ocirc->global_identifier);
776 return -1;
779 circuit_change_purpose(circ, CIRCUIT_PURPOSE_PATH_BIAS_TESTING);
781 /* Update timestamp for when circuit_expire_building() should kill us */
782 tor_gettimeofday(&circ->timestamp_began);
784 /* Generate a random address for the nonce */
785 crypto_rand((char*)&ocirc->pathbias_probe_nonce,
786 sizeof(ocirc->pathbias_probe_nonce));
787 ocirc->pathbias_probe_nonce &= 0x00ffffff;
788 probe_nonce = tor_dup_ip(ocirc->pathbias_probe_nonce);
790 tor_snprintf(payload,RELAY_PAYLOAD_SIZE, "%s:25", probe_nonce);
791 payload_len = (int)strlen(payload)+1;
793 // XXX: need this? Can we assume ipv4 will always be supported?
794 // If not, how do we tell?
795 //if (payload_len <= RELAY_PAYLOAD_SIZE - 4 && edge_conn->begincell_flags) {
796 // set_uint32(payload + payload_len, htonl(edge_conn->begincell_flags));
797 // payload_len += 4;
800 /* Generate+Store stream id, make sure it's non-zero */
801 ocirc->pathbias_probe_id = get_unique_stream_id_by_circ(ocirc);
803 if (ocirc->pathbias_probe_id==0) {
804 log_warn(LD_CIRC,
805 "Ran out of stream IDs on circuit %u during "
806 "pathbias probe attempt.", ocirc->global_identifier);
807 tor_free(probe_nonce);
808 return -1;
811 log_info(LD_CIRC,
812 "Sending pathbias testing cell to %s:25 on stream %d for circ %d.",
813 probe_nonce, ocirc->pathbias_probe_id, ocirc->global_identifier);
814 tor_free(probe_nonce);
816 /* Send a test relay cell */
817 if (relay_send_command_from_edge(ocirc->pathbias_probe_id, circ,
818 RELAY_COMMAND_BEGIN, payload,
819 payload_len, cpath_layer) < 0) {
820 log_notice(LD_CIRC,
821 "Failed to send pathbias probe cell on circuit %d.",
822 ocirc->global_identifier);
823 return -1;
826 /* Mark it freshly dirty so it doesn't get expired in the meantime */
827 circ->timestamp_dirty = time(NULL);
829 return 0;
833 * Check the response to a pathbias probe, to ensure the
834 * cell is recognized and the nonce and other probe
835 * characteristics are as expected.
837 * If the response is valid, return 0. Otherwise return < 0.
840 pathbias_check_probe_response(circuit_t *circ, const cell_t *cell)
842 /* Based on connection_edge_process_relay_cell() */
843 relay_header_t rh;
844 int reason;
845 uint32_t ipv4_host;
846 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
848 tor_assert(cell);
849 tor_assert(ocirc);
850 tor_assert(circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING);
852 relay_header_unpack(&rh, cell->payload);
854 reason = rh.length > 0 ?
855 get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
857 if (rh.command == RELAY_COMMAND_END &&
858 reason == END_STREAM_REASON_EXITPOLICY &&
859 ocirc->pathbias_probe_id == rh.stream_id) {
861 /* Check length+extract host: It is in network order after the reason code.
862 * See connection_edge_end(). */
863 if (rh.length < 9) { /* reason+ipv4+dns_ttl */
864 log_notice(LD_PROTOCOL,
865 "Short path bias probe response length field (%d).", rh.length);
866 return - END_CIRC_REASON_TORPROTOCOL;
869 ipv4_host = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
871 /* Check nonce */
872 if (ipv4_host == ocirc->pathbias_probe_nonce) {
873 pathbias_mark_use_success(ocirc);
874 circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
875 log_info(LD_CIRC,
876 "Got valid path bias probe back for circ %d, stream %d.",
877 ocirc->global_identifier, ocirc->pathbias_probe_id);
878 return 0;
879 } else {
880 log_notice(LD_CIRC,
881 "Got strange probe value 0x%x vs 0x%x back for circ %d, "
882 "stream %d.", ipv4_host, ocirc->pathbias_probe_nonce,
883 ocirc->global_identifier, ocirc->pathbias_probe_id);
884 return -1;
887 log_info(LD_CIRC,
888 "Got another cell back back on pathbias probe circuit %d: "
889 "Command: %d, Reason: %d, Stream-id: %d",
890 ocirc->global_identifier, rh.command, reason, rh.stream_id);
891 return -1;
895 * Check if a circuit was used and/or closed successfully.
897 * If we attempted to use the circuit to carry a stream but failed
898 * for whatever reason, or if the circuit mysteriously died before
899 * we could attach any streams, record these two cases.
901 * If we *have* successfully used the circuit, or it appears to
902 * have been closed by us locally, count it as a success.
904 * Returns 0 if we're done making decisions with the circ,
905 * or -1 if we want to probe it first.
908 pathbias_check_close(origin_circuit_t *ocirc, int reason)
910 circuit_t *circ = &ocirc->base_;
912 if (!pathbias_should_count(ocirc)) {
913 return 0;
916 switch (ocirc->path_state) {
917 /* If the circuit was closed after building, but before use, we need
918 * to ensure we were the ones who tried to close it (and not a remote
919 * actor). */
920 case PATH_STATE_BUILD_SUCCEEDED:
921 if (reason & END_CIRC_REASON_FLAG_REMOTE) {
922 /* Remote circ close reasons on an unused circuit all could be bias */
923 log_info(LD_CIRC,
924 "Circuit %d remote-closed without successful use for reason %d. "
925 "Circuit purpose %d currently %d,%s. Len %d.",
926 ocirc->global_identifier,
927 reason, circ->purpose, ocirc->has_opened,
928 circuit_state_to_string(circ->state),
929 ocirc->build_state->desired_path_len);
930 pathbias_count_collapse(ocirc);
931 } else if ((reason & ~END_CIRC_REASON_FLAG_REMOTE)
932 == END_CIRC_REASON_CHANNEL_CLOSED &&
933 circ->n_chan &&
934 circ->n_chan->reason_for_closing
935 != CHANNEL_CLOSE_REQUESTED) {
936 /* If we didn't close the channel ourselves, it could be bias */
937 /* XXX: Only count bias if the network is live?
938 * What about clock jumps/suspends? */
939 log_info(LD_CIRC,
940 "Circuit %d's channel closed without successful use for reason "
941 "%d, channel reason %d. Circuit purpose %d currently %d,%s. Len "
942 "%d.", ocirc->global_identifier,
943 reason, circ->n_chan->reason_for_closing,
944 circ->purpose, ocirc->has_opened,
945 circuit_state_to_string(circ->state),
946 ocirc->build_state->desired_path_len);
947 pathbias_count_collapse(ocirc);
948 } else {
949 pathbias_count_successful_close(ocirc);
951 break;
953 /* If we tried to use a circuit but failed, we should probe it to ensure
954 * it has not been tampered with. */
955 case PATH_STATE_USE_ATTEMPTED:
956 /* XXX: Only probe and/or count failure if the network is live?
957 * What about clock jumps/suspends? */
958 if (pathbias_send_usable_probe(circ) == 0)
959 return -1;
960 else
961 pathbias_count_use_failed(ocirc);
963 /* Any circuit where there were attempted streams but no successful
964 * streams could be bias */
965 log_info(LD_CIRC,
966 "Circuit %d closed without successful use for reason %d. "
967 "Circuit purpose %d currently %d,%s. Len %d.",
968 ocirc->global_identifier,
969 reason, circ->purpose, ocirc->has_opened,
970 circuit_state_to_string(circ->state),
971 ocirc->build_state->desired_path_len);
972 break;
974 case PATH_STATE_USE_SUCCEEDED:
975 pathbias_count_successful_close(ocirc);
976 pathbias_count_use_success(ocirc);
977 break;
979 case PATH_STATE_USE_FAILED:
980 pathbias_count_use_failed(ocirc);
981 break;
983 case PATH_STATE_NEW_CIRC:
984 case PATH_STATE_BUILD_ATTEMPTED:
985 case PATH_STATE_ALREADY_COUNTED:
986 default:
987 // Other states are uninteresting. No stats to count.
988 break;
991 ocirc->path_state = PATH_STATE_ALREADY_COUNTED;
993 return 0;
997 * Count a successfully closed circuit.
999 static void
1000 pathbias_count_successful_close(origin_circuit_t *circ)
1002 entry_guard_t *guard = NULL;
1003 if (!pathbias_should_count(circ)) {
1004 return;
1007 if (circ->cpath && circ->cpath->extend_info) {
1008 guard = entry_guard_get_by_id_digest(
1009 circ->cpath->extend_info->identity_digest);
1012 if (guard) {
1013 /* In the long run: circuit_success ~= successful_circuit_close +
1014 * circ_failure + stream_failure */
1015 guard->successful_circuits_closed++;
1016 entry_guards_changed();
1017 } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1018 /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
1019 * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
1020 * No need to log that case. */
1021 log_info(LD_CIRC,
1022 "Successfully closed circuit has no known guard. "
1023 "Circuit is a %s currently %s",
1024 circuit_purpose_to_string(circ->base_.purpose),
1025 circuit_state_to_string(circ->base_.state));
1030 * Count a circuit that fails after it is built, but before it can
1031 * carry any traffic.
1033 * This is needed because there are ways to destroy a
1034 * circuit after it has successfully completed. Right now, this is
1035 * used for purely informational/debugging purposes.
1037 static void
1038 pathbias_count_collapse(origin_circuit_t *circ)
1040 entry_guard_t *guard = NULL;
1042 if (!pathbias_should_count(circ)) {
1043 return;
1046 if (circ->cpath && circ->cpath->extend_info) {
1047 guard = entry_guard_get_by_id_digest(
1048 circ->cpath->extend_info->identity_digest);
1051 if (guard) {
1052 guard->collapsed_circuits++;
1053 entry_guards_changed();
1054 } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1055 /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
1056 * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
1057 * No need to log that case. */
1058 log_info(LD_CIRC,
1059 "Destroyed circuit has no known guard. "
1060 "Circuit is a %s currently %s",
1061 circuit_purpose_to_string(circ->base_.purpose),
1062 circuit_state_to_string(circ->base_.state));
1067 * Count a known failed circuit (because we could not probe it).
1069 * This counter is informational.
1071 static void
1072 pathbias_count_use_failed(origin_circuit_t *circ)
1074 entry_guard_t *guard = NULL;
1075 if (!pathbias_should_count(circ)) {
1076 return;
1079 if (circ->cpath && circ->cpath->extend_info) {
1080 guard = entry_guard_get_by_id_digest(
1081 circ->cpath->extend_info->identity_digest);
1084 if (guard) {
1085 guard->unusable_circuits++;
1086 entry_guards_changed();
1087 } else if (circ->base_.purpose != CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT) {
1088 /* In rare cases, CIRCUIT_PURPOSE_TESTING can get converted to
1089 * CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT and have no guards here.
1090 * No need to log that case. */
1091 /* XXX note cut-and-paste code in this function compared to nearby
1092 * functions. Would be nice to refactor. -RD */
1093 log_info(LD_CIRC,
1094 "Stream-failing circuit has no known guard. "
1095 "Circuit is a %s currently %s",
1096 circuit_purpose_to_string(circ->base_.purpose),
1097 circuit_state_to_string(circ->base_.state));
1102 * Count timeouts for path bias log messages.
1104 * These counts are purely informational.
1106 void
1107 pathbias_count_timeout(origin_circuit_t *circ)
1109 entry_guard_t *guard = NULL;
1111 if (!pathbias_should_count(circ)) {
1112 return;
1115 /* For hidden service circs, they can actually be used
1116 * successfully and then time out later (because
1117 * the other side declines to use them). */
1118 if (circ->path_state == PATH_STATE_USE_SUCCEEDED) {
1119 return;
1122 if (circ->cpath && circ->cpath->extend_info) {
1123 guard = entry_guard_get_by_id_digest(
1124 circ->cpath->extend_info->identity_digest);
1127 if (guard) {
1128 guard->timeouts++;
1129 entry_guards_changed();
1134 * Helper function to count all of the currently opened circuits
1135 * for a guard that are in a given path state range. The state
1136 * range is inclusive on both ends.
1138 static int
1139 pathbias_count_circs_in_states(entry_guard_t *guard,
1140 path_state_t from,
1141 path_state_t to)
1143 circuit_t *circ;
1144 int open_circuits = 0;
1146 /* Count currently open circuits. Give them the benefit of the doubt. */
1147 TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
1148 origin_circuit_t *ocirc = NULL;
1149 if (!CIRCUIT_IS_ORIGIN(circ) || /* didn't originate here */
1150 circ->marked_for_close) /* already counted */
1151 continue;
1153 ocirc = TO_ORIGIN_CIRCUIT(circ);
1155 if (!ocirc->cpath || !ocirc->cpath->extend_info)
1156 continue;
1158 if (ocirc->path_state >= from &&
1159 ocirc->path_state <= to &&
1160 pathbias_should_count(ocirc) &&
1161 fast_memeq(guard->identity,
1162 ocirc->cpath->extend_info->identity_digest,
1163 DIGEST_LEN)) {
1164 log_debug(LD_CIRC, "Found opened circuit %d in path_state %s",
1165 ocirc->global_identifier,
1166 pathbias_state_to_string(ocirc->path_state));
1167 open_circuits++;
1171 return open_circuits;
1175 * Return the number of circuits counted as successfully closed for
1176 * this guard.
1178 * Also add in the currently open circuits to give them the benefit
1179 * of the doubt.
1181 double
1182 pathbias_get_close_success_count(entry_guard_t *guard)
1184 return guard->successful_circuits_closed +
1185 pathbias_count_circs_in_states(guard,
1186 PATH_STATE_BUILD_SUCCEEDED,
1187 PATH_STATE_USE_SUCCEEDED);
1191 * Return the number of circuits counted as successfully used
1192 * this guard.
1194 * Also add in the currently open circuits that we are attempting
1195 * to use to give them the benefit of the doubt.
1197 double
1198 pathbias_get_use_success_count(entry_guard_t *guard)
1200 return guard->use_successes +
1201 pathbias_count_circs_in_states(guard,
1202 PATH_STATE_USE_ATTEMPTED,
1203 PATH_STATE_USE_SUCCEEDED);
1207 * Check the path bias use rate against our consensus parameter limits.
1209 * Emits a log message if the use success rates are too low.
1211 * If pathbias_get_dropguards() is set, we also disable the use of
1212 * very failure prone guards.
1214 static void
1215 pathbias_measure_use_rate(entry_guard_t *guard)
1217 const or_options_t *options = get_options();
1219 if (guard->use_attempts > pathbias_get_min_use(options)) {
1220 /* Note: We rely on the < comparison here to allow us to set a 0
1221 * rate and disable the feature entirely. If refactoring, don't
1222 * change to <= */
1223 if (pathbias_get_use_success_count(guard)/guard->use_attempts
1224 < pathbias_get_extreme_use_rate(options)) {
1225 /* Dropping is currently disabled by default. */
1226 if (pathbias_get_dropguards(options)) {
1227 if (!guard->path_bias_disabled) {
1228 log_warn(LD_CIRC,
1229 "Your Guard %s ($%s) is failing to carry an extremely large "
1230 "amount of stream on its circuits. "
1231 "To avoid potential route manipulation attacks, Tor has "
1232 "disabled use of this guard. "
1233 "Use counts are %ld/%ld. Success counts are %ld/%ld. "
1234 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1235 "and %ld timed out. "
1236 "For reference, your timeout cutoff is %ld seconds.",
1237 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1238 tor_lround(pathbias_get_use_success_count(guard)),
1239 tor_lround(guard->use_attempts),
1240 tor_lround(pathbias_get_close_success_count(guard)),
1241 tor_lround(guard->circ_attempts),
1242 tor_lround(guard->circ_successes),
1243 tor_lround(guard->unusable_circuits),
1244 tor_lround(guard->collapsed_circuits),
1245 tor_lround(guard->timeouts),
1246 tor_lround(get_circuit_build_close_time_ms()/1000));
1247 guard->path_bias_disabled = 1;
1248 guard->bad_since = approx_time();
1249 entry_guards_changed();
1250 return;
1252 } else if (!guard->path_bias_use_extreme) {
1253 guard->path_bias_use_extreme = 1;
1254 log_warn(LD_CIRC,
1255 "Your Guard %s ($%s) is failing to carry an extremely large "
1256 "amount of streams on its circuits. "
1257 "This could indicate a route manipulation attack, network "
1258 "overload, bad local network connectivity, or a bug. "
1259 "Use counts are %ld/%ld. Success counts are %ld/%ld. "
1260 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1261 "and %ld timed out. "
1262 "For reference, your timeout cutoff is %ld seconds.",
1263 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1264 tor_lround(pathbias_get_use_success_count(guard)),
1265 tor_lround(guard->use_attempts),
1266 tor_lround(pathbias_get_close_success_count(guard)),
1267 tor_lround(guard->circ_attempts),
1268 tor_lround(guard->circ_successes),
1269 tor_lround(guard->unusable_circuits),
1270 tor_lround(guard->collapsed_circuits),
1271 tor_lround(guard->timeouts),
1272 tor_lround(get_circuit_build_close_time_ms()/1000));
1274 } else if (pathbias_get_use_success_count(guard)/guard->use_attempts
1275 < pathbias_get_notice_use_rate(options)) {
1276 if (!guard->path_bias_use_noticed) {
1277 guard->path_bias_use_noticed = 1;
1278 log_notice(LD_CIRC,
1279 "Your Guard %s ($%s) is failing to carry more streams on its "
1280 "circuits than usual. "
1281 "Most likely this means the Tor network is overloaded "
1282 "or your network connection is poor. "
1283 "Use counts are %ld/%ld. Success counts are %ld/%ld. "
1284 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1285 "and %ld timed out. "
1286 "For reference, your timeout cutoff is %ld seconds.",
1287 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1288 tor_lround(pathbias_get_use_success_count(guard)),
1289 tor_lround(guard->use_attempts),
1290 tor_lround(pathbias_get_close_success_count(guard)),
1291 tor_lround(guard->circ_attempts),
1292 tor_lround(guard->circ_successes),
1293 tor_lround(guard->unusable_circuits),
1294 tor_lround(guard->collapsed_circuits),
1295 tor_lround(guard->timeouts),
1296 tor_lround(get_circuit_build_close_time_ms()/1000));
1303 * Check the path bias circuit close status rates against our consensus
1304 * parameter limits.
1306 * Emits a log message if the use success rates are too low.
1308 * If pathbias_get_dropguards() is set, we also disable the use of
1309 * very failure prone guards.
1311 * XXX: This function shares similar log messages and checks to
1312 * pathbias_measure_use_rate(). It may be possible to combine them
1313 * eventually, especially if we can ever remove the need for 3
1314 * levels of closure warns (if the overall circuit failure rate
1315 * goes down with ntor). One way to do so would be to multiply
1316 * the build rate with the use rate to get an idea of the total
1317 * fraction of the total network paths the user is able to use.
1318 * See ticket #8159.
1320 static void
1321 pathbias_measure_close_rate(entry_guard_t *guard)
1323 const or_options_t *options = get_options();
1325 if (guard->circ_attempts > pathbias_get_min_circs(options)) {
1326 /* Note: We rely on the < comparison here to allow us to set a 0
1327 * rate and disable the feature entirely. If refactoring, don't
1328 * change to <= */
1329 if (pathbias_get_close_success_count(guard)/guard->circ_attempts
1330 < pathbias_get_extreme_rate(options)) {
1331 /* Dropping is currently disabled by default. */
1332 if (pathbias_get_dropguards(options)) {
1333 if (!guard->path_bias_disabled) {
1334 log_warn(LD_CIRC,
1335 "Your Guard %s ($%s) is failing an extremely large "
1336 "amount of circuits. "
1337 "To avoid potential route manipulation attacks, Tor has "
1338 "disabled use of this guard. "
1339 "Success counts are %ld/%ld. Use counts are %ld/%ld. "
1340 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1341 "and %ld timed out. "
1342 "For reference, your timeout cutoff is %ld seconds.",
1343 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1344 tor_lround(pathbias_get_close_success_count(guard)),
1345 tor_lround(guard->circ_attempts),
1346 tor_lround(pathbias_get_use_success_count(guard)),
1347 tor_lround(guard->use_attempts),
1348 tor_lround(guard->circ_successes),
1349 tor_lround(guard->unusable_circuits),
1350 tor_lround(guard->collapsed_circuits),
1351 tor_lround(guard->timeouts),
1352 tor_lround(get_circuit_build_close_time_ms()/1000));
1353 guard->path_bias_disabled = 1;
1354 guard->bad_since = approx_time();
1355 entry_guards_changed();
1356 return;
1358 } else if (!guard->path_bias_extreme) {
1359 guard->path_bias_extreme = 1;
1360 log_warn(LD_CIRC,
1361 "Your Guard %s ($%s) is failing an extremely large "
1362 "amount of circuits. "
1363 "This could indicate a route manipulation attack, "
1364 "extreme network overload, or a bug. "
1365 "Success counts are %ld/%ld. Use counts are %ld/%ld. "
1366 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1367 "and %ld timed out. "
1368 "For reference, your timeout cutoff is %ld seconds.",
1369 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1370 tor_lround(pathbias_get_close_success_count(guard)),
1371 tor_lround(guard->circ_attempts),
1372 tor_lround(pathbias_get_use_success_count(guard)),
1373 tor_lround(guard->use_attempts),
1374 tor_lround(guard->circ_successes),
1375 tor_lround(guard->unusable_circuits),
1376 tor_lround(guard->collapsed_circuits),
1377 tor_lround(guard->timeouts),
1378 tor_lround(get_circuit_build_close_time_ms()/1000));
1380 } else if (pathbias_get_close_success_count(guard)/guard->circ_attempts
1381 < pathbias_get_warn_rate(options)) {
1382 if (!guard->path_bias_warned) {
1383 guard->path_bias_warned = 1;
1384 log_warn(LD_CIRC,
1385 "Your Guard %s ($%s) is failing a very large "
1386 "amount of circuits. "
1387 "Most likely this means the Tor network is "
1388 "overloaded, but it could also mean an attack against "
1389 "you or potentially the guard itself. "
1390 "Success counts are %ld/%ld. Use counts are %ld/%ld. "
1391 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1392 "and %ld timed out. "
1393 "For reference, your timeout cutoff is %ld seconds.",
1394 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1395 tor_lround(pathbias_get_close_success_count(guard)),
1396 tor_lround(guard->circ_attempts),
1397 tor_lround(pathbias_get_use_success_count(guard)),
1398 tor_lround(guard->use_attempts),
1399 tor_lround(guard->circ_successes),
1400 tor_lround(guard->unusable_circuits),
1401 tor_lround(guard->collapsed_circuits),
1402 tor_lround(guard->timeouts),
1403 tor_lround(get_circuit_build_close_time_ms()/1000));
1405 } else if (pathbias_get_close_success_count(guard)/guard->circ_attempts
1406 < pathbias_get_notice_rate(options)) {
1407 if (!guard->path_bias_noticed) {
1408 guard->path_bias_noticed = 1;
1409 log_notice(LD_CIRC,
1410 "Your Guard %s ($%s) is failing more circuits than "
1411 "usual. "
1412 "Most likely this means the Tor network is overloaded. "
1413 "Success counts are %ld/%ld. Use counts are %ld/%ld. "
1414 "%ld circuits completed, %ld were unusable, %ld collapsed, "
1415 "and %ld timed out. "
1416 "For reference, your timeout cutoff is %ld seconds.",
1417 guard->nickname, hex_str(guard->identity, DIGEST_LEN),
1418 tor_lround(pathbias_get_close_success_count(guard)),
1419 tor_lround(guard->circ_attempts),
1420 tor_lround(pathbias_get_use_success_count(guard)),
1421 tor_lround(guard->use_attempts),
1422 tor_lround(guard->circ_successes),
1423 tor_lround(guard->unusable_circuits),
1424 tor_lround(guard->collapsed_circuits),
1425 tor_lround(guard->timeouts),
1426 tor_lround(get_circuit_build_close_time_ms()/1000));
1433 * This function scales the path bias use rates if we have
1434 * more data than the scaling threshold. This allows us to
1435 * be more sensitive to recent measurements.
1437 * XXX: The attempt count transfer stuff here might be done
1438 * better by keeping separate pending counters that get
1439 * transfered at circuit close. See ticket #8160.
1441 static void
1442 pathbias_scale_close_rates(entry_guard_t *guard)
1444 const or_options_t *options = get_options();
1446 /* If we get a ton of circuits, just scale everything down */
1447 if (guard->circ_attempts > pathbias_get_scale_threshold(options)) {
1448 double scale_ratio = pathbias_get_scale_ratio(options);
1449 int opened_attempts = pathbias_count_circs_in_states(guard,
1450 PATH_STATE_BUILD_ATTEMPTED, PATH_STATE_BUILD_ATTEMPTED);
1451 int opened_built = pathbias_count_circs_in_states(guard,
1452 PATH_STATE_BUILD_SUCCEEDED,
1453 PATH_STATE_USE_FAILED);
1454 /* Verify that the counts are sane before and after scaling */
1455 int counts_are_sane = (guard->circ_attempts >= guard->circ_successes);
1457 guard->circ_attempts -= (opened_attempts+opened_built);
1458 guard->circ_successes -= opened_built;
1460 guard->circ_attempts *= scale_ratio;
1461 guard->circ_successes *= scale_ratio;
1462 guard->timeouts *= scale_ratio;
1463 guard->successful_circuits_closed *= scale_ratio;
1464 guard->collapsed_circuits *= scale_ratio;
1465 guard->unusable_circuits *= scale_ratio;
1467 guard->circ_attempts += (opened_attempts+opened_built);
1468 guard->circ_successes += opened_built;
1470 entry_guards_changed();
1472 log_info(LD_CIRC,
1473 "Scaled pathbias counts to (%f,%f)/%f (%d/%d open) for guard "
1474 "%s ($%s)",
1475 guard->circ_successes, guard->successful_circuits_closed,
1476 guard->circ_attempts, opened_built, opened_attempts,
1477 guard->nickname, hex_str(guard->identity, DIGEST_LEN));
1479 /* Have the counts just become invalid by this scaling attempt? */
1480 if (counts_are_sane && guard->circ_attempts < guard->circ_successes) {
1481 log_notice(LD_BUG,
1482 "Scaling has mangled pathbias counts to %f/%f (%d/%d open) "
1483 "for guard %s ($%s)",
1484 guard->circ_successes, guard->circ_attempts, opened_built,
1485 opened_attempts, guard->nickname,
1486 hex_str(guard->identity, DIGEST_LEN));
1492 * This function scales the path bias circuit close rates if we have
1493 * more data than the scaling threshold. This allows us to be more
1494 * sensitive to recent measurements.
1496 * XXX: The attempt count transfer stuff here might be done
1497 * better by keeping separate pending counters that get
1498 * transfered at circuit close. See ticket #8160.
1500 void
1501 pathbias_scale_use_rates(entry_guard_t *guard)
1503 const or_options_t *options = get_options();
1505 /* If we get a ton of circuits, just scale everything down */
1506 if (guard->use_attempts > pathbias_get_scale_use_threshold(options)) {
1507 double scale_ratio = pathbias_get_scale_ratio(options);
1508 int opened_attempts = pathbias_count_circs_in_states(guard,
1509 PATH_STATE_USE_ATTEMPTED, PATH_STATE_USE_SUCCEEDED);
1510 /* Verify that the counts are sane before and after scaling */
1511 int counts_are_sane = (guard->use_attempts >= guard->use_successes);
1513 guard->use_attempts -= opened_attempts;
1515 guard->use_attempts *= scale_ratio;
1516 guard->use_successes *= scale_ratio;
1518 guard->use_attempts += opened_attempts;
1520 log_info(LD_CIRC,
1521 "Scaled pathbias use counts to %f/%f (%d open) for guard %s ($%s)",
1522 guard->use_successes, guard->use_attempts, opened_attempts,
1523 guard->nickname, hex_str(guard->identity, DIGEST_LEN));
1525 /* Have the counts just become invalid by this scaling attempt? */
1526 if (counts_are_sane && guard->use_attempts < guard->use_successes) {
1527 log_notice(LD_BUG,
1528 "Scaling has mangled pathbias usage counts to %f/%f "
1529 "(%d open) for guard %s ($%s)",
1530 guard->circ_successes, guard->circ_attempts,
1531 opened_attempts, guard->nickname,
1532 hex_str(guard->identity, DIGEST_LEN));
1535 entry_guards_changed();