Bump copyright date to 2019
[tor.git] / src / feature / nodelist / node_select.c
blobe31abb247f9b8de97442536a9da87e593454accb
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file node_select.c
9 * \brief Code to choose nodes randomly based on restrictions and
10 * weighted probabilities.
11 **/
13 #define NODE_SELECT_PRIVATE
14 #include "core/or/or.h"
16 #include "app/config/config.h"
17 #include "core/mainloop/connection.h"
18 #include "core/or/policies.h"
19 #include "core/or/reasons.h"
20 #include "feature/client/entrynodes.h"
21 #include "feature/dirclient/dirclient.h"
22 #include "feature/dircommon/directory.h"
23 #include "feature/nodelist/describe.h"
24 #include "feature/nodelist/dirlist.h"
25 #include "feature/nodelist/microdesc.h"
26 #include "feature/nodelist/networkstatus.h"
27 #include "feature/nodelist/node_select.h"
28 #include "feature/nodelist/nodelist.h"
29 #include "feature/nodelist/routerlist.h"
30 #include "feature/nodelist/routerset.h"
31 #include "feature/relay/router.h"
32 #include "feature/relay/routermode.h"
33 #include "lib/crypt_ops/crypto_rand.h"
34 #include "lib/math/fp.h"
36 #include "feature/dirclient/dir_server_st.h"
37 #include "feature/nodelist/networkstatus_st.h"
38 #include "feature/nodelist/node_st.h"
39 #include "feature/nodelist/routerinfo_st.h"
40 #include "feature/nodelist/routerstatus_st.h"
42 static int compute_weighted_bandwidths(const smartlist_t *sl,
43 bandwidth_weight_rule_t rule,
44 double **bandwidths_out,
45 double *total_bandwidth_out);
46 static const routerstatus_t *router_pick_trusteddirserver_impl(
47 const smartlist_t *sourcelist, dirinfo_type_t auth,
48 int flags, int *n_busy_out);
49 static const routerstatus_t *router_pick_dirserver_generic(
50 smartlist_t *sourcelist,
51 dirinfo_type_t type, int flags);
53 /** Try to find a running dirserver that supports operations of <b>type</b>.
55 * If there are no running dirservers in our routerlist and the
56 * <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the fallback ones
57 * (including authorities) as running again, and pick one.
59 * If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
60 * dirservers that we can't reach.
62 * If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
63 * (if we're a dirserver).
65 * Don't pick a fallback directory mirror if any non-fallback is viable;
66 * (the fallback directory mirrors include the authorities)
67 * try to avoid using servers that have returned 503 recently.
69 const routerstatus_t *
70 router_pick_directory_server(dirinfo_type_t type, int flags)
72 int busy = 0;
73 const routerstatus_t *choice;
75 choice = router_pick_directory_server_impl(type, flags, &busy);
76 if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
77 return choice;
79 if (busy) {
80 /* If the reason that we got no server is that servers are "busy",
81 * we must be excluding good servers because we already have serverdesc
82 * fetches with them. Do not mark down servers up because of this. */
83 tor_assert((flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
84 PDS_NO_EXISTING_MICRODESC_FETCH)));
85 return NULL;
88 log_info(LD_DIR,
89 "No reachable router entries for dirservers. "
90 "Trying them all again.");
91 /* mark all fallback directory mirrors as up again */
92 router_reset_status_download_failures();
93 /* try again */
94 choice = router_pick_directory_server_impl(type, flags, NULL);
95 return choice;
98 /** Try to find a running fallback directory. Flags are as for
99 * router_pick_directory_server.
101 const routerstatus_t *
102 router_pick_dirserver_generic(smartlist_t *sourcelist,
103 dirinfo_type_t type, int flags)
105 const routerstatus_t *choice;
106 int busy = 0;
108 if (smartlist_len(sourcelist) == 1) {
109 /* If there's only one choice, then we should disable the logic that
110 * would otherwise prevent us from choosing ourself. */
111 flags |= PDS_ALLOW_SELF;
114 choice = router_pick_trusteddirserver_impl(sourcelist, type, flags, &busy);
115 if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
116 return choice;
117 if (busy) {
118 /* If the reason that we got no server is that servers are "busy",
119 * we must be excluding good servers because we already have serverdesc
120 * fetches with them. Do not mark down servers up because of this. */
121 tor_assert((flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
122 PDS_NO_EXISTING_MICRODESC_FETCH)));
123 return NULL;
126 log_info(LD_DIR,
127 "No dirservers are reachable. Trying them all again.");
128 mark_all_dirservers_up(sourcelist);
129 return router_pick_trusteddirserver_impl(sourcelist, type, flags, NULL);
132 /* Common retry code for router_pick_directory_server_impl and
133 * router_pick_trusteddirserver_impl. Retry with the non-preferred IP version.
134 * Must be called before RETRY_WITHOUT_EXCLUDE().
136 * If we got no result, and we are applying IP preferences, and we are a
137 * client that could use an alternate IP version, try again with the
138 * opposite preferences. */
139 #define RETRY_ALTERNATE_IP_VERSION(retry_label) \
140 STMT_BEGIN \
141 if (result == NULL && try_ip_pref && options->ClientUseIPv4 \
142 && fascist_firewall_use_ipv6(options) && !server_mode(options) \
143 && !n_busy) { \
144 n_excluded = 0; \
145 n_busy = 0; \
146 try_ip_pref = 0; \
147 goto retry_label; \
149 STMT_END \
151 /* Common retry code for router_pick_directory_server_impl and
152 * router_pick_trusteddirserver_impl. Retry without excluding nodes, but with
153 * the preferred IP version. Must be called after RETRY_ALTERNATE_IP_VERSION().
155 * If we got no result, and we are excluding nodes, and StrictNodes is
156 * not set, try again without excluding nodes. */
157 #define RETRY_WITHOUT_EXCLUDE(retry_label) \
158 STMT_BEGIN \
159 if (result == NULL && try_excluding && !options->StrictNodes \
160 && n_excluded && !n_busy) { \
161 try_excluding = 0; \
162 n_excluded = 0; \
163 n_busy = 0; \
164 try_ip_pref = 1; \
165 goto retry_label; \
167 STMT_END
169 /* Common code used in the loop within router_pick_directory_server_impl and
170 * router_pick_trusteddirserver_impl.
172 * Check if the given <b>identity</b> supports extrainfo. If not, skip further
173 * checks.
175 #define SKIP_MISSING_TRUSTED_EXTRAINFO(type, identity) \
176 STMT_BEGIN \
177 int is_trusted_extrainfo = router_digest_is_trusted_dir_type( \
178 (identity), EXTRAINFO_DIRINFO); \
179 if (((type) & EXTRAINFO_DIRINFO) && \
180 !router_supports_extrainfo((identity), is_trusted_extrainfo)) \
181 continue; \
182 STMT_END
184 #ifndef LOG_FALSE_POSITIVES_DURING_BOOTSTRAP
185 #define LOG_FALSE_POSITIVES_DURING_BOOTSTRAP 0
186 #endif
188 /* Log a message if rs is not found or not a preferred address */
189 static void
190 router_picked_poor_directory_log(const routerstatus_t *rs)
192 const networkstatus_t *usable_consensus;
193 usable_consensus = networkstatus_get_reasonably_live_consensus(time(NULL),
194 usable_consensus_flavor());
196 #if !LOG_FALSE_POSITIVES_DURING_BOOTSTRAP
197 /* Don't log early in the bootstrap process, it's normal to pick from a
198 * small pool of nodes. Of course, this won't help if we're trying to
199 * diagnose bootstrap issues. */
200 if (!smartlist_len(nodelist_get_list()) || !usable_consensus
201 || !router_have_minimum_dir_info()) {
202 return;
204 #endif /* !LOG_FALSE_POSITIVES_DURING_BOOTSTRAP */
206 /* We couldn't find a node, or the one we have doesn't fit our preferences.
207 * Sometimes this is normal, sometimes it can be a reachability issue. */
208 if (!rs) {
209 /* This happens a lot, so it's at debug level */
210 log_debug(LD_DIR, "Wanted to make an outgoing directory connection, but "
211 "we couldn't find a directory that fit our criteria. "
212 "Perhaps we will succeed next time with less strict criteria.");
213 } else if (!fascist_firewall_allows_rs(rs, FIREWALL_OR_CONNECTION, 1)
214 && !fascist_firewall_allows_rs(rs, FIREWALL_DIR_CONNECTION, 1)
216 /* This is rare, and might be interesting to users trying to diagnose
217 * connection issues on dual-stack machines. */
218 log_info(LD_DIR, "Selected a directory %s with non-preferred OR and Dir "
219 "addresses for launching an outgoing connection: "
220 "IPv4 %s OR %d Dir %d IPv6 %s OR %d Dir %d",
221 routerstatus_describe(rs),
222 fmt_addr32(rs->addr), rs->or_port,
223 rs->dir_port, fmt_addr(&rs->ipv6_addr),
224 rs->ipv6_orport, rs->dir_port);
228 #undef LOG_FALSE_POSITIVES_DURING_BOOTSTRAP
230 /* Check if we already have a directory fetch from ap, for serverdesc
231 * (including extrainfo) or microdesc documents.
232 * If so, return 1, if not, return 0.
233 * Also returns 0 if addr is NULL, tor_addr_is_null(addr), or dir_port is 0.
235 STATIC int
236 router_is_already_dir_fetching(const tor_addr_port_t *ap, int serverdesc,
237 int microdesc)
239 if (!ap || tor_addr_is_null(&ap->addr) || !ap->port) {
240 return 0;
243 /* XX/teor - we're not checking tunnel connections here, see #17848
245 if (serverdesc && (
246 connection_get_by_type_addr_port_purpose(
247 CONN_TYPE_DIR, &ap->addr, ap->port, DIR_PURPOSE_FETCH_SERVERDESC)
248 || connection_get_by_type_addr_port_purpose(
249 CONN_TYPE_DIR, &ap->addr, ap->port, DIR_PURPOSE_FETCH_EXTRAINFO))) {
250 return 1;
253 if (microdesc && (
254 connection_get_by_type_addr_port_purpose(
255 CONN_TYPE_DIR, &ap->addr, ap->port, DIR_PURPOSE_FETCH_MICRODESC))) {
256 return 1;
259 return 0;
262 /* Check if we already have a directory fetch from the ipv4 or ipv6
263 * router, for serverdesc (including extrainfo) or microdesc documents.
264 * If so, return 1, if not, return 0.
266 static int
267 router_is_already_dir_fetching_(uint32_t ipv4_addr,
268 const tor_addr_t *ipv6_addr,
269 uint16_t dir_port,
270 int serverdesc,
271 int microdesc)
273 tor_addr_port_t ipv4_dir_ap, ipv6_dir_ap;
275 /* Assume IPv6 DirPort is the same as IPv4 DirPort */
276 tor_addr_from_ipv4h(&ipv4_dir_ap.addr, ipv4_addr);
277 ipv4_dir_ap.port = dir_port;
278 tor_addr_copy(&ipv6_dir_ap.addr, ipv6_addr);
279 ipv6_dir_ap.port = dir_port;
281 return (router_is_already_dir_fetching(&ipv4_dir_ap, serverdesc, microdesc)
282 || router_is_already_dir_fetching(&ipv6_dir_ap, serverdesc, microdesc));
285 /** Pick a random running valid directory server/mirror from our
286 * routerlist. Arguments are as for router_pick_directory_server(), except:
288 * If <b>n_busy_out</b> is provided, set *<b>n_busy_out</b> to the number of
289 * directories that we excluded for no other reason than
290 * PDS_NO_EXISTING_SERVERDESC_FETCH or PDS_NO_EXISTING_MICRODESC_FETCH.
292 STATIC const routerstatus_t *
293 router_pick_directory_server_impl(dirinfo_type_t type, int flags,
294 int *n_busy_out)
296 const or_options_t *options = get_options();
297 const node_t *result;
298 smartlist_t *direct, *tunnel;
299 smartlist_t *trusted_direct, *trusted_tunnel;
300 smartlist_t *overloaded_direct, *overloaded_tunnel;
301 time_t now = time(NULL);
302 const networkstatus_t *consensus = networkstatus_get_latest_consensus();
303 const int requireother = ! (flags & PDS_ALLOW_SELF);
304 const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
305 const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
306 const int no_microdesc_fetching = (flags & PDS_NO_EXISTING_MICRODESC_FETCH);
307 int try_excluding = 1, n_excluded = 0, n_busy = 0;
308 int try_ip_pref = 1;
310 if (!consensus)
311 return NULL;
313 retry_search:
315 direct = smartlist_new();
316 tunnel = smartlist_new();
317 trusted_direct = smartlist_new();
318 trusted_tunnel = smartlist_new();
319 overloaded_direct = smartlist_new();
320 overloaded_tunnel = smartlist_new();
322 const int skip_or_fw = router_skip_or_reachability(options, try_ip_pref);
323 const int skip_dir_fw = router_skip_dir_reachability(options, try_ip_pref);
324 const int must_have_or = directory_must_use_begindir(options);
326 /* Find all the running dirservers we know about. */
327 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
328 int is_trusted;
329 int is_overloaded;
330 const routerstatus_t *status = node->rs;
331 const country_t country = node->country;
332 if (!status)
333 continue;
335 if (!node->is_running || !node_is_dir(node) || !node->is_valid)
336 continue;
337 if (requireother && router_digest_is_me(node->identity))
338 continue;
340 SKIP_MISSING_TRUSTED_EXTRAINFO(type, node->identity);
342 if (try_excluding &&
343 routerset_contains_routerstatus(options->ExcludeNodes, status,
344 country)) {
345 ++n_excluded;
346 continue;
349 if (router_is_already_dir_fetching_(status->addr,
350 &status->ipv6_addr,
351 status->dir_port,
352 no_serverdesc_fetching,
353 no_microdesc_fetching)) {
354 ++n_busy;
355 continue;
358 is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;
359 is_trusted = router_digest_is_trusted_dir(node->identity);
361 /* Clients use IPv6 addresses if the server has one and the client
362 * prefers IPv6.
363 * Add the router if its preferred address and port are reachable.
364 * If we don't get any routers, we'll try again with the non-preferred
365 * address for each router (if any). (To ensure correct load-balancing
366 * we try routers that only have one address both times.)
368 if (!fascistfirewall || skip_or_fw ||
369 fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION,
370 try_ip_pref))
371 smartlist_add(is_trusted ? trusted_tunnel :
372 is_overloaded ? overloaded_tunnel : tunnel, (void*)node);
373 else if (!must_have_or && (skip_dir_fw ||
374 fascist_firewall_allows_node(node, FIREWALL_DIR_CONNECTION,
375 try_ip_pref)))
376 smartlist_add(is_trusted ? trusted_direct :
377 is_overloaded ? overloaded_direct : direct, (void*)node);
378 } SMARTLIST_FOREACH_END(node);
380 if (smartlist_len(tunnel)) {
381 result = node_sl_choose_by_bandwidth(tunnel, WEIGHT_FOR_DIR);
382 } else if (smartlist_len(overloaded_tunnel)) {
383 result = node_sl_choose_by_bandwidth(overloaded_tunnel,
384 WEIGHT_FOR_DIR);
385 } else if (smartlist_len(trusted_tunnel)) {
386 /* FFFF We don't distinguish between trusteds and overloaded trusteds
387 * yet. Maybe one day we should. */
388 /* FFFF We also don't load balance over authorities yet. I think this
389 * is a feature, but it could easily be a bug. -RD */
390 result = smartlist_choose(trusted_tunnel);
391 } else if (smartlist_len(direct)) {
392 result = node_sl_choose_by_bandwidth(direct, WEIGHT_FOR_DIR);
393 } else if (smartlist_len(overloaded_direct)) {
394 result = node_sl_choose_by_bandwidth(overloaded_direct,
395 WEIGHT_FOR_DIR);
396 } else {
397 result = smartlist_choose(trusted_direct);
399 smartlist_free(direct);
400 smartlist_free(tunnel);
401 smartlist_free(trusted_direct);
402 smartlist_free(trusted_tunnel);
403 smartlist_free(overloaded_direct);
404 smartlist_free(overloaded_tunnel);
406 RETRY_ALTERNATE_IP_VERSION(retry_search);
408 RETRY_WITHOUT_EXCLUDE(retry_search);
410 if (n_busy_out)
411 *n_busy_out = n_busy;
413 router_picked_poor_directory_log(result ? result->rs : NULL);
415 return result ? result->rs : NULL;
418 /** Given an array of double/uint64_t unions that are currently being used as
419 * doubles, convert them to uint64_t, and try to scale them linearly so as to
420 * much of the range of uint64_t. If <b>total_out</b> is provided, set it to
421 * the sum of all elements in the array _before_ scaling. */
422 STATIC void
423 scale_array_elements_to_u64(uint64_t *entries_out, const double *entries_in,
424 int n_entries,
425 uint64_t *total_out)
427 double total = 0.0;
428 double scale_factor = 0.0;
429 int i;
431 for (i = 0; i < n_entries; ++i)
432 total += entries_in[i];
434 if (total > 0.0) {
435 scale_factor = ((double)INT64_MAX) / total;
436 scale_factor /= 4.0; /* make sure we're very far away from overflowing */
439 for (i = 0; i < n_entries; ++i)
440 entries_out[i] = tor_llround(entries_in[i] * scale_factor);
442 if (total_out)
443 *total_out = (uint64_t) total;
446 /** Pick a random element of <b>n_entries</b>-element array <b>entries</b>,
447 * choosing each element with a probability proportional to its (uint64_t)
448 * value, and return the index of that element. If all elements are 0, choose
449 * an index at random. Return -1 on error.
451 STATIC int
452 choose_array_element_by_weight(const uint64_t *entries, int n_entries)
454 int i;
455 uint64_t rand_val;
456 uint64_t total = 0;
458 for (i = 0; i < n_entries; ++i)
459 total += entries[i];
461 if (n_entries < 1)
462 return -1;
464 if (total == 0)
465 return crypto_rand_int(n_entries);
467 tor_assert(total < INT64_MAX);
469 rand_val = crypto_rand_uint64(total);
471 return select_array_member_cumulative_timei(
472 entries, n_entries, total, rand_val);
475 /** Return bw*1000, unless bw*1000 would overflow, in which case return
476 * INT32_MAX. */
477 static inline int32_t
478 kb_to_bytes(uint32_t bw)
480 return (bw > (INT32_MAX/1000)) ? INT32_MAX : bw*1000;
483 /** Helper function:
484 * choose a random element of smartlist <b>sl</b> of nodes, weighted by
485 * the advertised bandwidth of each element using the consensus
486 * bandwidth weights.
488 * If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
489 * nodes' bandwidth equally regardless of their Exit status, since there may
490 * be some in the list because they exit to obscure ports. If
491 * <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
492 * exit-node's bandwidth less depending on the smallness of the fraction of
493 * Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, we're picking a
494 * guard node: consider all guard's bandwidth equally. Otherwise, weight
495 * guards proportionally less.
497 static const node_t *
498 smartlist_choose_node_by_bandwidth_weights(const smartlist_t *sl,
499 bandwidth_weight_rule_t rule)
501 double *bandwidths_dbl=NULL;
502 uint64_t *bandwidths_u64=NULL;
504 if (compute_weighted_bandwidths(sl, rule, &bandwidths_dbl, NULL) < 0)
505 return NULL;
507 bandwidths_u64 = tor_calloc(smartlist_len(sl), sizeof(uint64_t));
508 scale_array_elements_to_u64(bandwidths_u64, bandwidths_dbl,
509 smartlist_len(sl), NULL);
512 int idx = choose_array_element_by_weight(bandwidths_u64,
513 smartlist_len(sl));
514 tor_free(bandwidths_dbl);
515 tor_free(bandwidths_u64);
516 return idx < 0 ? NULL : smartlist_get(sl, idx);
520 /** When weighting bridges, enforce these values as lower and upper
521 * bound for believable bandwidth, because there is no way for us
522 * to verify a bridge's bandwidth currently. */
523 #define BRIDGE_MIN_BELIEVABLE_BANDWIDTH 20000 /* 20 kB/sec */
524 #define BRIDGE_MAX_BELIEVABLE_BANDWIDTH 100000 /* 100 kB/sec */
526 /** Return the smaller of the router's configured BandwidthRate
527 * and its advertised capacity, making sure to stay within the
528 * interval between bridge-min-believe-bw and
529 * bridge-max-believe-bw. */
530 static uint32_t
531 bridge_get_advertised_bandwidth_bounded(routerinfo_t *router)
533 uint32_t result = router->bandwidthcapacity;
534 if (result > router->bandwidthrate)
535 result = router->bandwidthrate;
536 if (result > BRIDGE_MAX_BELIEVABLE_BANDWIDTH)
537 result = BRIDGE_MAX_BELIEVABLE_BANDWIDTH;
538 else if (result < BRIDGE_MIN_BELIEVABLE_BANDWIDTH)
539 result = BRIDGE_MIN_BELIEVABLE_BANDWIDTH;
540 return result;
543 /** Given a list of routers and a weighting rule as in
544 * smartlist_choose_node_by_bandwidth_weights, compute weighted bandwidth
545 * values for each node and store them in a freshly allocated
546 * *<b>bandwidths_out</b> of the same length as <b>sl</b>, and holding results
547 * as doubles. If <b>total_bandwidth_out</b> is non-NULL, set it to the total
548 * of all the bandwidths.
549 * Return 0 on success, -1 on failure. */
550 static int
551 compute_weighted_bandwidths(const smartlist_t *sl,
552 bandwidth_weight_rule_t rule,
553 double **bandwidths_out,
554 double *total_bandwidth_out)
556 int64_t weight_scale;
557 double Wg = -1, Wm = -1, We = -1, Wd = -1;
558 double Wgb = -1, Wmb = -1, Web = -1, Wdb = -1;
559 guardfraction_bandwidth_t guardfraction_bw;
560 double *bandwidths = NULL;
561 double total_bandwidth = 0.0;
563 tor_assert(sl);
564 tor_assert(bandwidths_out);
566 /* Can't choose exit and guard at same time */
567 tor_assert(rule == NO_WEIGHTING ||
568 rule == WEIGHT_FOR_EXIT ||
569 rule == WEIGHT_FOR_GUARD ||
570 rule == WEIGHT_FOR_MID ||
571 rule == WEIGHT_FOR_DIR);
573 *bandwidths_out = NULL;
575 if (total_bandwidth_out) {
576 *total_bandwidth_out = 0.0;
579 if (smartlist_len(sl) == 0) {
580 log_info(LD_CIRC,
581 "Empty routerlist passed in to consensus weight node "
582 "selection for rule %s",
583 bandwidth_weight_rule_to_string(rule));
584 return -1;
587 weight_scale = networkstatus_get_weight_scale_param(NULL);
589 if (rule == WEIGHT_FOR_GUARD) {
590 Wg = networkstatus_get_bw_weight(NULL, "Wgg", -1);
591 Wm = networkstatus_get_bw_weight(NULL, "Wgm", -1); /* Bridges */
592 We = 0;
593 Wd = networkstatus_get_bw_weight(NULL, "Wgd", -1);
595 Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
596 Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
597 Web = networkstatus_get_bw_weight(NULL, "Web", -1);
598 Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
599 } else if (rule == WEIGHT_FOR_MID) {
600 Wg = networkstatus_get_bw_weight(NULL, "Wmg", -1);
601 Wm = networkstatus_get_bw_weight(NULL, "Wmm", -1);
602 We = networkstatus_get_bw_weight(NULL, "Wme", -1);
603 Wd = networkstatus_get_bw_weight(NULL, "Wmd", -1);
605 Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
606 Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
607 Web = networkstatus_get_bw_weight(NULL, "Web", -1);
608 Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
609 } else if (rule == WEIGHT_FOR_EXIT) {
610 // Guards CAN be exits if they have weird exit policies
611 // They are d then I guess...
612 We = networkstatus_get_bw_weight(NULL, "Wee", -1);
613 Wm = networkstatus_get_bw_weight(NULL, "Wem", -1); /* Odd exit policies */
614 Wd = networkstatus_get_bw_weight(NULL, "Wed", -1);
615 Wg = networkstatus_get_bw_weight(NULL, "Weg", -1); /* Odd exit policies */
617 Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
618 Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
619 Web = networkstatus_get_bw_weight(NULL, "Web", -1);
620 Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
621 } else if (rule == WEIGHT_FOR_DIR) {
622 We = networkstatus_get_bw_weight(NULL, "Wbe", -1);
623 Wm = networkstatus_get_bw_weight(NULL, "Wbm", -1);
624 Wd = networkstatus_get_bw_weight(NULL, "Wbd", -1);
625 Wg = networkstatus_get_bw_weight(NULL, "Wbg", -1);
627 Wgb = Wmb = Web = Wdb = weight_scale;
628 } else if (rule == NO_WEIGHTING) {
629 Wg = Wm = We = Wd = weight_scale;
630 Wgb = Wmb = Web = Wdb = weight_scale;
633 if (Wg < 0 || Wm < 0 || We < 0 || Wd < 0 || Wgb < 0 || Wmb < 0 || Wdb < 0
634 || Web < 0) {
635 log_debug(LD_CIRC,
636 "Got negative bandwidth weights. Defaulting to naive selection"
637 " algorithm.");
638 Wg = Wm = We = Wd = weight_scale;
639 Wgb = Wmb = Web = Wdb = weight_scale;
642 Wg /= weight_scale;
643 Wm /= weight_scale;
644 We /= weight_scale;
645 Wd /= weight_scale;
647 Wgb /= weight_scale;
648 Wmb /= weight_scale;
649 Web /= weight_scale;
650 Wdb /= weight_scale;
652 bandwidths = tor_calloc(smartlist_len(sl), sizeof(double));
654 // Cycle through smartlist and total the bandwidth.
655 static int warned_missing_bw = 0;
656 SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
657 int is_exit = 0, is_guard = 0, is_dir = 0, this_bw = 0;
658 double weight = 1;
659 double weight_without_guard_flag = 0; /* Used for guardfraction */
660 double final_weight = 0;
661 is_exit = node->is_exit && ! node->is_bad_exit;
662 is_guard = node->is_possible_guard;
663 is_dir = node_is_dir(node);
664 if (node->rs) {
665 if (!node->rs->has_bandwidth) {
666 /* This should never happen, unless all the authorities downgrade
667 * to 0.2.0 or rogue routerstatuses get inserted into our consensus. */
668 if (! warned_missing_bw) {
669 log_warn(LD_BUG,
670 "Consensus is missing some bandwidths. Using a naive "
671 "router selection algorithm");
672 warned_missing_bw = 1;
674 this_bw = 30000; /* Chosen arbitrarily */
675 } else {
676 this_bw = kb_to_bytes(node->rs->bandwidth_kb);
678 } else if (node->ri) {
679 /* bridge or other descriptor not in our consensus */
680 this_bw = bridge_get_advertised_bandwidth_bounded(node->ri);
681 } else {
682 /* We can't use this one. */
683 continue;
686 if (is_guard && is_exit) {
687 weight = (is_dir ? Wdb*Wd : Wd);
688 weight_without_guard_flag = (is_dir ? Web*We : We);
689 } else if (is_guard) {
690 weight = (is_dir ? Wgb*Wg : Wg);
691 weight_without_guard_flag = (is_dir ? Wmb*Wm : Wm);
692 } else if (is_exit) {
693 weight = (is_dir ? Web*We : We);
694 } else { // middle
695 weight = (is_dir ? Wmb*Wm : Wm);
697 /* These should be impossible; but overflows here would be bad, so let's
698 * make sure. */
699 if (this_bw < 0)
700 this_bw = 0;
701 if (weight < 0.0)
702 weight = 0.0;
703 if (weight_without_guard_flag < 0.0)
704 weight_without_guard_flag = 0.0;
706 /* If guardfraction information is available in the consensus, we
707 * want to calculate this router's bandwidth according to its
708 * guardfraction. Quoting from proposal236:
710 * Let Wpf denote the weight from the 'bandwidth-weights' line a
711 * client would apply to N for position p if it had the guard
712 * flag, Wpn the weight if it did not have the guard flag, and B the
713 * measured bandwidth of N in the consensus. Then instead of choosing
714 * N for position p proportionally to Wpf*B or Wpn*B, clients should
715 * choose N proportionally to F*Wpf*B + (1-F)*Wpn*B.
717 if (node->rs && node->rs->has_guardfraction && rule != WEIGHT_FOR_GUARD) {
718 /* XXX The assert should actually check for is_guard. However,
719 * that crashes dirauths because of #13297. This should be
720 * equivalent: */
721 tor_assert(node->rs->is_possible_guard);
723 guard_get_guardfraction_bandwidth(&guardfraction_bw,
724 this_bw,
725 node->rs->guardfraction_percentage);
727 /* Calculate final_weight = F*Wpf*B + (1-F)*Wpn*B */
728 final_weight =
729 guardfraction_bw.guard_bw * weight +
730 guardfraction_bw.non_guard_bw * weight_without_guard_flag;
732 log_debug(LD_GENERAL, "%s: Guardfraction weight %f instead of %f (%s)",
733 node->rs->nickname, final_weight, weight*this_bw,
734 bandwidth_weight_rule_to_string(rule));
735 } else { /* no guardfraction information. calculate the weight normally. */
736 final_weight = weight*this_bw;
739 bandwidths[node_sl_idx] = final_weight;
740 total_bandwidth += final_weight;
741 } SMARTLIST_FOREACH_END(node);
743 log_debug(LD_CIRC, "Generated weighted bandwidths for rule %s based "
744 "on weights "
745 "Wg=%f Wm=%f We=%f Wd=%f with total bw %f",
746 bandwidth_weight_rule_to_string(rule),
747 Wg, Wm, We, Wd, total_bandwidth);
749 *bandwidths_out = bandwidths;
751 if (total_bandwidth_out) {
752 *total_bandwidth_out = total_bandwidth;
755 return 0;
758 /** For all nodes in <b>sl</b>, return the fraction of those nodes, weighted
759 * by their weighted bandwidths with rule <b>rule</b>, for which we have
760 * descriptors.
762 * If <b>for_direct_connect</b> is true, we intend to connect to the node
763 * directly, as the first hop of a circuit; otherwise, we intend to connect
764 * to it indirectly, or use it as if we were connecting to it indirectly. */
765 double
766 frac_nodes_with_descriptors(const smartlist_t *sl,
767 bandwidth_weight_rule_t rule,
768 int for_direct_conn)
770 double *bandwidths = NULL;
771 double total, present;
773 if (smartlist_len(sl) == 0)
774 return 0.0;
776 if (compute_weighted_bandwidths(sl, rule, &bandwidths, &total) < 0 ||
777 total <= 0.0) {
778 int n_with_descs = 0;
779 SMARTLIST_FOREACH(sl, const node_t *, node, {
780 if (node_has_preferred_descriptor(node, for_direct_conn))
781 n_with_descs++;
783 tor_free(bandwidths);
784 return ((double)n_with_descs) / smartlist_len(sl);
787 present = 0.0;
788 SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
789 if (node_has_preferred_descriptor(node, for_direct_conn))
790 present += bandwidths[node_sl_idx];
791 } SMARTLIST_FOREACH_END(node);
793 tor_free(bandwidths);
795 return present / total;
798 /** Choose a random element of status list <b>sl</b>, weighted by
799 * the advertised bandwidth of each node */
800 const node_t *
801 node_sl_choose_by_bandwidth(const smartlist_t *sl,
802 bandwidth_weight_rule_t rule)
803 { /*XXXX MOVE */
804 return smartlist_choose_node_by_bandwidth_weights(sl, rule);
807 /** Given a <b>router</b>, add every node_t in its family (including the
808 * node itself!) to <b>sl</b>.
810 * Note the type mismatch: This function takes a routerinfo, but adds nodes
811 * to the smartlist!
813 static void
814 routerlist_add_node_and_family(smartlist_t *sl, const routerinfo_t *router)
816 /* XXXX MOVE ? */
817 node_t fake_node;
818 const node_t *node = node_get_by_id(router->cache_info.identity_digest);
819 if (node == NULL) {
820 memset(&fake_node, 0, sizeof(fake_node));
821 fake_node.ri = (routerinfo_t *)router;
822 memcpy(fake_node.identity, router->cache_info.identity_digest, DIGEST_LEN);
823 node = &fake_node;
825 nodelist_add_node_and_family(sl, node);
828 /** Return a random running node from the nodelist. Never
829 * pick a node that is in
830 * <b>excludedsmartlist</b>, or which matches <b>excludedset</b>,
831 * even if they are the only nodes available.
832 * If <b>CRN_NEED_UPTIME</b> is set in flags and any router has more than
833 * a minimum uptime, return one of those.
834 * If <b>CRN_NEED_CAPACITY</b> is set in flags, weight your choice by the
835 * advertised capacity of each router.
836 * If <b>CRN_NEED_GUARD</b> is set in flags, consider only Guard routers.
837 * If <b>CRN_WEIGHT_AS_EXIT</b> is set in flags, we weight bandwidths as if
838 * picking an exit node, otherwise we weight bandwidths for picking a relay
839 * node (that is, possibly discounting exit nodes).
840 * If <b>CRN_NEED_DESC</b> is set in flags, we only consider nodes that
841 * have a routerinfo or microdescriptor -- that is, enough info to be
842 * used to build a circuit.
843 * If <b>CRN_PREF_ADDR</b> is set in flags, we only consider nodes that
844 * have an address that is preferred by the ClientPreferIPv6ORPort setting
845 * (regardless of this flag, we exclude nodes that aren't allowed by the
846 * firewall, including ClientUseIPv4 0 and fascist_firewall_use_ipv6() == 0).
848 const node_t *
849 router_choose_random_node(smartlist_t *excludedsmartlist,
850 routerset_t *excludedset,
851 router_crn_flags_t flags)
852 { /* XXXX MOVE */
853 const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
854 const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
855 const int need_guard = (flags & CRN_NEED_GUARD) != 0;
856 const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0;
857 const int need_desc = (flags & CRN_NEED_DESC) != 0;
858 const int pref_addr = (flags & CRN_PREF_ADDR) != 0;
859 const int direct_conn = (flags & CRN_DIRECT_CONN) != 0;
860 const int rendezvous_v3 = (flags & CRN_RENDEZVOUS_V3) != 0;
862 smartlist_t *sl=smartlist_new(),
863 *excludednodes=smartlist_new();
864 const node_t *choice = NULL;
865 const routerinfo_t *r;
866 bandwidth_weight_rule_t rule;
868 tor_assert(!(weight_for_exit && need_guard));
869 rule = weight_for_exit ? WEIGHT_FOR_EXIT :
870 (need_guard ? WEIGHT_FOR_GUARD : WEIGHT_FOR_MID);
872 SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), node_t *, node) {
873 if (node_allows_single_hop_exits(node)) {
874 /* Exclude relays that allow single hop exit circuits. This is an
875 * obsolete option since 0.2.9.2-alpha and done by default in
876 * 0.3.1.0-alpha. */
877 smartlist_add(excludednodes, node);
878 } else if (rendezvous_v3 &&
879 !node_supports_v3_rendezvous_point(node)) {
880 /* Exclude relays that do not support to rendezvous for a hidden service
881 * version 3. */
882 smartlist_add(excludednodes, node);
884 } SMARTLIST_FOREACH_END(node);
886 /* If the node_t is not found we won't be to exclude ourself but we
887 * won't be able to pick ourself in router_choose_random_node() so
888 * this is fine to at least try with our routerinfo_t object. */
889 if ((r = router_get_my_routerinfo()))
890 routerlist_add_node_and_family(excludednodes, r);
892 router_add_running_nodes_to_smartlist(sl, need_uptime, need_capacity,
893 need_guard, need_desc, pref_addr,
894 direct_conn);
895 log_debug(LD_CIRC,
896 "We found %d running nodes.",
897 smartlist_len(sl));
899 smartlist_subtract(sl,excludednodes);
900 log_debug(LD_CIRC,
901 "We removed %d excludednodes, leaving %d nodes.",
902 smartlist_len(excludednodes),
903 smartlist_len(sl));
905 if (excludedsmartlist) {
906 smartlist_subtract(sl,excludedsmartlist);
907 log_debug(LD_CIRC,
908 "We removed %d excludedsmartlist, leaving %d nodes.",
909 smartlist_len(excludedsmartlist),
910 smartlist_len(sl));
912 if (excludedset) {
913 routerset_subtract_nodes(sl,excludedset);
914 log_debug(LD_CIRC,
915 "We removed excludedset, leaving %d nodes.",
916 smartlist_len(sl));
919 // Always weight by bandwidth
920 choice = node_sl_choose_by_bandwidth(sl, rule);
922 smartlist_free(sl);
923 if (!choice && (need_uptime || need_capacity || need_guard || pref_addr)) {
924 /* try once more -- recurse but with fewer restrictions. */
925 log_info(LD_CIRC,
926 "We couldn't find any live%s%s%s routers; falling back "
927 "to list of all routers.",
928 need_capacity?", fast":"",
929 need_uptime?", stable":"",
930 need_guard?", guard":"");
931 flags &= ~ (CRN_NEED_UPTIME|CRN_NEED_CAPACITY|CRN_NEED_GUARD|
932 CRN_PREF_ADDR);
933 choice = router_choose_random_node(
934 excludedsmartlist, excludedset, flags);
936 smartlist_free(excludednodes);
937 if (!choice) {
938 log_warn(LD_CIRC,
939 "No available nodes when trying to choose node. Failing.");
941 return choice;
944 /** Try to find a running directory authority. Flags are as for
945 * router_pick_directory_server.
947 const routerstatus_t *
948 router_pick_trusteddirserver(dirinfo_type_t type, int flags)
950 return router_pick_dirserver_generic(
951 router_get_trusted_dir_servers_mutable(),
952 type, flags);
955 /** Try to find a running fallback directory. Flags are as for
956 * router_pick_directory_server.
958 const routerstatus_t *
959 router_pick_fallback_dirserver(dirinfo_type_t type, int flags)
961 return router_pick_dirserver_generic(
962 router_get_fallback_dir_servers_mutable(),
963 type, flags);
966 /** Pick a random element from a list of dir_server_t, weighting by their
967 * <b>weight</b> field. */
968 static const dir_server_t *
969 dirserver_choose_by_weight(const smartlist_t *servers, double authority_weight)
971 int n = smartlist_len(servers);
972 int i;
973 double *weights_dbl;
974 uint64_t *weights_u64;
975 const dir_server_t *ds;
977 weights_dbl = tor_calloc(n, sizeof(double));
978 weights_u64 = tor_calloc(n, sizeof(uint64_t));
979 for (i = 0; i < n; ++i) {
980 ds = smartlist_get(servers, i);
981 weights_dbl[i] = ds->weight;
982 if (ds->is_authority)
983 weights_dbl[i] *= authority_weight;
986 scale_array_elements_to_u64(weights_u64, weights_dbl, n, NULL);
987 i = choose_array_element_by_weight(weights_u64, n);
988 tor_free(weights_dbl);
989 tor_free(weights_u64);
990 return (i < 0) ? NULL : smartlist_get(servers, i);
993 /** Choose randomly from among the dir_server_ts in sourcelist that
994 * are up. Flags are as for router_pick_directory_server_impl().
996 static const routerstatus_t *
997 router_pick_trusteddirserver_impl(const smartlist_t *sourcelist,
998 dirinfo_type_t type, int flags,
999 int *n_busy_out)
1001 const or_options_t *options = get_options();
1002 smartlist_t *direct, *tunnel;
1003 smartlist_t *overloaded_direct, *overloaded_tunnel;
1004 const routerinfo_t *me = router_get_my_routerinfo();
1005 const routerstatus_t *result = NULL;
1006 time_t now = time(NULL);
1007 const int requireother = ! (flags & PDS_ALLOW_SELF);
1008 const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
1009 const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
1010 const int no_microdesc_fetching =(flags & PDS_NO_EXISTING_MICRODESC_FETCH);
1011 const double auth_weight =
1012 (sourcelist == router_get_fallback_dir_servers()) ?
1013 options->DirAuthorityFallbackRate : 1.0;
1014 smartlist_t *pick_from;
1015 int n_busy = 0;
1016 int try_excluding = 1, n_excluded = 0;
1017 int try_ip_pref = 1;
1019 if (!sourcelist)
1020 return NULL;
1022 retry_search:
1024 direct = smartlist_new();
1025 tunnel = smartlist_new();
1026 overloaded_direct = smartlist_new();
1027 overloaded_tunnel = smartlist_new();
1029 const int skip_or_fw = router_skip_or_reachability(options, try_ip_pref);
1030 const int skip_dir_fw = router_skip_dir_reachability(options, try_ip_pref);
1031 const int must_have_or = directory_must_use_begindir(options);
1033 SMARTLIST_FOREACH_BEGIN(sourcelist, const dir_server_t *, d)
1035 int is_overloaded =
1036 d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
1037 if (!d->is_running) continue;
1038 if ((type & d->type) == 0)
1039 continue;
1041 SKIP_MISSING_TRUSTED_EXTRAINFO(type, d->digest);
1043 if (requireother && me && router_digest_is_me(d->digest))
1044 continue;
1045 if (try_excluding &&
1046 routerset_contains_routerstatus(options->ExcludeNodes,
1047 &d->fake_status, -1)) {
1048 ++n_excluded;
1049 continue;
1052 if (router_is_already_dir_fetching_(d->addr,
1053 &d->ipv6_addr,
1054 d->dir_port,
1055 no_serverdesc_fetching,
1056 no_microdesc_fetching)) {
1057 ++n_busy;
1058 continue;
1061 /* Clients use IPv6 addresses if the server has one and the client
1062 * prefers IPv6.
1063 * Add the router if its preferred address and port are reachable.
1064 * If we don't get any routers, we'll try again with the non-preferred
1065 * address for each router (if any). (To ensure correct load-balancing
1066 * we try routers that only have one address both times.)
1068 if (!fascistfirewall || skip_or_fw ||
1069 fascist_firewall_allows_dir_server(d, FIREWALL_OR_CONNECTION,
1070 try_ip_pref))
1071 smartlist_add(is_overloaded ? overloaded_tunnel : tunnel, (void*)d);
1072 else if (!must_have_or && (skip_dir_fw ||
1073 fascist_firewall_allows_dir_server(d, FIREWALL_DIR_CONNECTION,
1074 try_ip_pref)))
1075 smartlist_add(is_overloaded ? overloaded_direct : direct, (void*)d);
1077 SMARTLIST_FOREACH_END(d);
1079 if (smartlist_len(tunnel)) {
1080 pick_from = tunnel;
1081 } else if (smartlist_len(overloaded_tunnel)) {
1082 pick_from = overloaded_tunnel;
1083 } else if (smartlist_len(direct)) {
1084 pick_from = direct;
1085 } else {
1086 pick_from = overloaded_direct;
1090 const dir_server_t *selection =
1091 dirserver_choose_by_weight(pick_from, auth_weight);
1093 if (selection)
1094 result = &selection->fake_status;
1097 smartlist_free(direct);
1098 smartlist_free(tunnel);
1099 smartlist_free(overloaded_direct);
1100 smartlist_free(overloaded_tunnel);
1102 RETRY_ALTERNATE_IP_VERSION(retry_search);
1104 RETRY_WITHOUT_EXCLUDE(retry_search);
1106 router_picked_poor_directory_log(result);
1108 if (n_busy_out)
1109 *n_busy_out = n_busy;
1110 return result;