routerset.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. n * 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 */
  6. /**
  7. * \file routerset.c
  8. *
  9. * \brief Functions and structures to handle set-type selection of routers
  10. * by name, ID, address, etc.
  11. *
  12. * This module implements the routerset_t data structure, whose purpose
  13. * is to specify a set of relays based on a list of their identities or
  14. * properties. Routersets can restrict relays by IP address mask,
  15. * identity fingerprint, country codes, and nicknames (deprecated).
  16. *
  17. * Routersets are typically used for user-specified restrictions, and
  18. * are created by invoking routerset_new and routerset_parse from
  19. * config.c and confparse.c. To use a routerset, invoke one of
  20. * routerset_contains_...() functions , or use
  21. * routerstatus_get_all_nodes() / routerstatus_subtract_nodes() to
  22. * manipulate a smartlist of node_t pointers.
  23. *
  24. * Country-code restrictions are implemented in geoip.c.
  25. */
  26. #define ROUTERSET_PRIVATE
  27. #include "core/or/or.h"
  28. #include "core/or/policies.h"
  29. #include "feature/client/bridges.h"
  30. #include "feature/dirparse/policy_parse.h"
  31. #include "feature/nodelist/nickname.h"
  32. #include "feature/nodelist/nodelist.h"
  33. #include "feature/nodelist/routerset.h"
  34. #include "lib/conf/conftypes.h"
  35. #include "lib/confmgt/typedvar.h"
  36. #include "lib/encoding/confline.h"
  37. #include "lib/geoip/geoip.h"
  38. #include "core/or/addr_policy_st.h"
  39. #include "core/or/extend_info_st.h"
  40. #include "feature/nodelist/node_st.h"
  41. #include "feature/nodelist/routerinfo_st.h"
  42. #include "feature/nodelist/routerstatus_st.h"
  43. #include "lib/confmgt/var_type_def_st.h"
  44. /** Return a new empty routerset. */
  45. routerset_t *
  46. routerset_new(void)
  47. {
  48. routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
  49. result->list = smartlist_new();
  50. result->names = strmap_new();
  51. result->digests = digestmap_new();
  52. result->policies = smartlist_new();
  53. result->country_names = smartlist_new();
  54. return result;
  55. }
  56. /** If <b>c</b> is a country code in the form {cc}, return a newly allocated
  57. * string holding the "cc" part. Else, return NULL. */
  58. STATIC char *
  59. routerset_get_countryname(const char *c)
  60. {
  61. char *country;
  62. if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
  63. return NULL;
  64. country = tor_strndup(c+1, 2);
  65. tor_strlower(country);
  66. return country;
  67. }
  68. /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
  69. * the GeoIP IPv4 database is reloaded.
  70. */
  71. void
  72. routerset_refresh_countries(routerset_t *target)
  73. {
  74. int cc;
  75. bitarray_free(target->countries);
  76. if (!geoip_is_loaded(AF_INET)) {
  77. target->countries = NULL;
  78. target->n_countries = 0;
  79. return;
  80. }
  81. target->n_countries = geoip_get_n_countries();
  82. target->countries = bitarray_init_zero(target->n_countries);
  83. SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
  84. cc = geoip_get_country(country);
  85. if (cc >= 0) {
  86. tor_assert(cc < target->n_countries);
  87. bitarray_set(target->countries, cc);
  88. } else {
  89. log_warn(LD_CONFIG, "Country code '%s' is not recognized.",
  90. country);
  91. }
  92. } SMARTLIST_FOREACH_END(country);
  93. }
  94. /** Parse the string <b>s</b> to create a set of routerset entries, and add
  95. * them to <b>target</b>. In log messages, refer to the string as
  96. * <b>description</b>. Return 0 on success, -1 on failure.
  97. *
  98. * Three kinds of elements are allowed in routersets: nicknames, IP address
  99. * patterns, and fingerprints. They may be surrounded by optional space, and
  100. * must be separated by commas.
  101. */
  102. int
  103. routerset_parse(routerset_t *target, const char *s, const char *description)
  104. {
  105. int r = 0;
  106. int added_countries = 0;
  107. char *countryname;
  108. smartlist_t *list = smartlist_new();
  109. int malformed_list;
  110. smartlist_split_string(list, s, ",",
  111. SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
  112. SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
  113. addr_policy_t *p;
  114. /* if it doesn't pass our validation, assume it's malformed */
  115. malformed_list = 1;
  116. if (is_legal_hexdigest(nick)) {
  117. char d[DIGEST_LEN];
  118. if (*nick == '$')
  119. ++nick;
  120. log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
  121. base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
  122. digestmap_set(target->digests, d, (void*)1);
  123. } else if (is_legal_nickname(nick)) {
  124. log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
  125. strmap_set_lc(target->names, nick, (void*)1);
  126. } else if ((countryname = routerset_get_countryname(nick)) != NULL) {
  127. log_debug(LD_CONFIG, "Adding country %s to %s", nick,
  128. description);
  129. smartlist_add(target->country_names, countryname);
  130. added_countries = 1;
  131. } else if ((strchr(nick,'.') || strchr(nick, ':') || strchr(nick, '*'))
  132. && (p = router_parse_addr_policy_item_from_string(
  133. nick, ADDR_POLICY_REJECT,
  134. &malformed_list))) {
  135. /* IPv4 addresses contain '.', IPv6 addresses contain ':',
  136. * and wildcard addresses contain '*'. */
  137. log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
  138. smartlist_add(target->policies, p);
  139. } else if (malformed_list) {
  140. log_warn(LD_CONFIG, "Entry '%s' in %s is malformed. Discarding entire"
  141. " list.", nick, description);
  142. r = -1;
  143. tor_free(nick);
  144. SMARTLIST_DEL_CURRENT(list, nick);
  145. } else {
  146. log_notice(LD_CONFIG, "Entry '%s' in %s is ignored. Using the"
  147. " remainder of the list.", nick, description);
  148. tor_free(nick);
  149. SMARTLIST_DEL_CURRENT(list, nick);
  150. }
  151. } SMARTLIST_FOREACH_END(nick);
  152. policy_expand_unspec(&target->policies);
  153. smartlist_add_all(target->list, list);
  154. smartlist_free(list);
  155. if (added_countries)
  156. routerset_refresh_countries(target);
  157. return r;
  158. }
  159. /** Add all members of the set <b>source</b> to <b>target</b>. */
  160. void
  161. routerset_union(routerset_t *target, const routerset_t *source)
  162. {
  163. char *s;
  164. tor_assert(target);
  165. if (!source || !source->list)
  166. return;
  167. s = routerset_to_string(source);
  168. routerset_parse(target, s, "other routerset");
  169. tor_free(s);
  170. }
  171. /** Return true iff <b>set</b> lists only nicknames and digests, and includes
  172. * no IP ranges or countries. */
  173. int
  174. routerset_is_list(const routerset_t *set)
  175. {
  176. return smartlist_len(set->country_names) == 0 &&
  177. smartlist_len(set->policies) == 0;
  178. }
  179. /** Return true iff we need a GeoIP IP-to-country database to make sense of
  180. * <b>set</b>. */
  181. int
  182. routerset_needs_geoip(const routerset_t *set)
  183. {
  184. return set && smartlist_len(set->country_names);
  185. }
  186. /** Return true iff there are no entries in <b>set</b>. */
  187. int
  188. routerset_is_empty(const routerset_t *set)
  189. {
  190. return !set || smartlist_len(set->list) == 0;
  191. }
  192. /** Return the number of entries in <b>set</b>. This does NOT return a
  193. * negative value. */
  194. int
  195. routerset_len(const routerset_t *set)
  196. {
  197. if (!set) {
  198. return 0;
  199. }
  200. return smartlist_len(set->list);
  201. }
  202. /** Helper. Return true iff <b>set</b> contains a router based on the other
  203. * provided fields. Return higher values for more specific subentries: a
  204. * single router is more specific than an address range of routers, which is
  205. * more specific in turn than a country code.
  206. *
  207. * (If country is -1, then we take the country
  208. * from addr.) */
  209. STATIC int
  210. routerset_contains(const routerset_t *set, const tor_addr_t *addr,
  211. uint16_t orport,
  212. const char *nickname, const char *id_digest,
  213. country_t country)
  214. {
  215. if (!set || !set->list)
  216. return 0;
  217. if (nickname && strmap_get_lc(set->names, nickname))
  218. return 4;
  219. if (id_digest && digestmap_get(set->digests, id_digest))
  220. return 4;
  221. if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
  222. == ADDR_POLICY_REJECTED)
  223. return 3;
  224. if (set->countries) {
  225. if (country < 0 && addr)
  226. country = geoip_get_country_by_addr(addr);
  227. if (country >= 0 && country < set->n_countries &&
  228. bitarray_is_set(set->countries, country))
  229. return 2;
  230. }
  231. return 0;
  232. }
  233. /** If *<b>setp</b> includes at least one country code, or if
  234. * <b>only_some_cc_set</b> is 0, add the ?? and A1 country codes to
  235. * *<b>setp</b>, creating it as needed. Return true iff *<b>setp</b> changed.
  236. */
  237. int
  238. routerset_add_unknown_ccs(routerset_t **setp, int only_if_some_cc_set)
  239. {
  240. routerset_t *set;
  241. int add_unknown, add_a1;
  242. if (only_if_some_cc_set) {
  243. if (!*setp || smartlist_len((*setp)->country_names) == 0)
  244. return 0;
  245. }
  246. if (!*setp)
  247. *setp = routerset_new();
  248. set = *setp;
  249. add_unknown = ! smartlist_contains_string_case(set->country_names, "??") &&
  250. geoip_get_country("??") >= 0;
  251. add_a1 = ! smartlist_contains_string_case(set->country_names, "a1") &&
  252. geoip_get_country("A1") >= 0;
  253. if (add_unknown) {
  254. smartlist_add_strdup(set->country_names, "??");
  255. smartlist_add_strdup(set->list, "{??}");
  256. }
  257. if (add_a1) {
  258. smartlist_add_strdup(set->country_names, "a1");
  259. smartlist_add_strdup(set->list, "{a1}");
  260. }
  261. if (add_unknown || add_a1) {
  262. routerset_refresh_countries(set);
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. /** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
  268. int
  269. routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
  270. {
  271. return routerset_contains(set,
  272. &ei->addr,
  273. ei->port,
  274. ei->nickname,
  275. ei->identity_digest,
  276. -1 /*country*/);
  277. }
  278. /** Return true iff <b>ri</b> is in <b>set</b>. If country is <b>-1</b>, we
  279. * look up the country. */
  280. int
  281. routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
  282. country_t country)
  283. {
  284. tor_addr_t addr;
  285. tor_addr_from_ipv4h(&addr, ri->addr);
  286. return routerset_contains(set,
  287. &addr,
  288. ri->or_port,
  289. ri->nickname,
  290. ri->cache_info.identity_digest,
  291. country);
  292. }
  293. /** Return true iff <b>rs</b> is in <b>set</b>. If country is <b>-1</b>, we
  294. * look up the country. */
  295. int
  296. routerset_contains_routerstatus(const routerset_t *set,
  297. const routerstatus_t *rs,
  298. country_t country)
  299. {
  300. tor_addr_t addr;
  301. tor_addr_from_ipv4h(&addr, rs->addr);
  302. return routerset_contains(set,
  303. &addr,
  304. rs->or_port,
  305. rs->nickname,
  306. rs->identity_digest,
  307. country);
  308. }
  309. /** Return true iff <b>node</b> is in <b>set</b>. */
  310. int
  311. routerset_contains_node(const routerset_t *set, const node_t *node)
  312. {
  313. if (node->rs)
  314. return routerset_contains_routerstatus(set, node->rs, node->country);
  315. else if (node->ri)
  316. return routerset_contains_router(set, node->ri, node->country);
  317. else
  318. return 0;
  319. }
  320. /** Return true iff <b>routerset</b> contains the bridge <b>bridge</b>. */
  321. int
  322. routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
  323. {
  324. const char *id = (const char*)bridge_get_rsa_id_digest(bridge);
  325. const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
  326. tor_assert(addrport);
  327. return routerset_contains(set, &addrport->addr, addrport->port,
  328. NULL, id, -1);
  329. }
  330. /** Add every known node_t that is a member of <b>routerset</b> to
  331. * <b>out</b>, but never add any that are part of <b>excludeset</b>.
  332. * If <b>running_only</b>, only add the running ones. */
  333. void
  334. routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
  335. const routerset_t *excludeset, int running_only)
  336. {
  337. tor_assert(out);
  338. if (!routerset || !routerset->list)
  339. return;
  340. if (routerset_is_list(routerset)) {
  341. /* No routers are specified by type; all are given by name or digest.
  342. * we can do a lookup in O(len(routerset)). */
  343. SMARTLIST_FOREACH(routerset->list, const char *, name, {
  344. const node_t *node = node_get_by_nickname(name, 0);
  345. if (node) {
  346. if (!running_only || node->is_running)
  347. if (!routerset_contains_node(excludeset, node))
  348. smartlist_add(out, (void*)node);
  349. }
  350. });
  351. } else {
  352. /* We need to iterate over the routerlist to get all the ones of the
  353. * right kind. */
  354. const smartlist_t *nodes = nodelist_get_list();
  355. SMARTLIST_FOREACH(nodes, const node_t *, node, {
  356. if (running_only && !node->is_running)
  357. continue;
  358. if (routerset_contains_node(routerset, node) &&
  359. !routerset_contains_node(excludeset, node))
  360. smartlist_add(out, (void*)node);
  361. });
  362. }
  363. }
  364. /** Remove every node_t from <b>lst</b> that is in <b>routerset</b>. */
  365. void
  366. routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
  367. {
  368. tor_assert(lst);
  369. if (!routerset)
  370. return;
  371. SMARTLIST_FOREACH(lst, const node_t *, node, {
  372. if (routerset_contains_node(routerset, node)) {
  373. //log_debug(LD_DIR, "Subtracting %s",r->nickname);
  374. SMARTLIST_DEL_CURRENT(lst, node);
  375. }
  376. });
  377. }
  378. /** Return a new string that when parsed by routerset_parse_string() will
  379. * yield <b>set</b>. */
  380. char *
  381. routerset_to_string(const routerset_t *set)
  382. {
  383. if (!set || !set->list)
  384. return tor_strdup("");
  385. return smartlist_join_strings(set->list, ",", 0, NULL);
  386. }
  387. /** Helper: return true iff old and new are both NULL, or both non-NULL
  388. * equal routersets. */
  389. int
  390. routerset_equal(const routerset_t *old, const routerset_t *new)
  391. {
  392. if (routerset_is_empty(old) && routerset_is_empty(new)) {
  393. /* Two empty sets are equal */
  394. return 1;
  395. } else if (routerset_is_empty(old) || routerset_is_empty(new)) {
  396. /* An empty set is equal to nothing else. */
  397. return 0;
  398. }
  399. tor_assert(old != NULL);
  400. tor_assert(new != NULL);
  401. if (smartlist_len(old->list) != smartlist_len(new->list))
  402. return 0;
  403. SMARTLIST_FOREACH(old->list, const char *, cp1, {
  404. const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
  405. if (strcmp(cp1, cp2))
  406. return 0;
  407. });
  408. return 1;
  409. }
  410. /** Free all storage held in <b>routerset</b>. */
  411. void
  412. routerset_free_(routerset_t *routerset)
  413. {
  414. if (!routerset)
  415. return;
  416. SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
  417. smartlist_free(routerset->list);
  418. SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
  419. addr_policy_free(p));
  420. smartlist_free(routerset->policies);
  421. SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
  422. smartlist_free(routerset->country_names);
  423. strmap_free(routerset->names, NULL);
  424. digestmap_free(routerset->digests, NULL);
  425. bitarray_free(routerset->countries);
  426. tor_free(routerset);
  427. }
  428. static int
  429. routerset_kv_parse(void *target, const config_line_t *line, char **errmsg,
  430. const void *params)
  431. {
  432. (void)params;
  433. routerset_t **p = (routerset_t**)target;
  434. routerset_free(*p); // clear the old value, if any.
  435. routerset_t *rs = routerset_new();
  436. if (routerset_parse(rs, line->value, line->key) < 0) {
  437. routerset_free(rs);
  438. *errmsg = tor_strdup("Invalid router list.");
  439. return -1;
  440. } else {
  441. *p = rs;
  442. return 0;
  443. }
  444. }
  445. static char *
  446. routerset_encode(const void *value, const void *params)
  447. {
  448. (void)params;
  449. const routerset_t **p = (const routerset_t**)value;
  450. return routerset_to_string(*p);
  451. }
  452. static void
  453. routerset_clear(void *value, const void *params)
  454. {
  455. (void)params;
  456. routerset_t **p = (routerset_t**)value;
  457. routerset_free(*p); // sets *p to NULL.
  458. }
  459. static int
  460. routerset_copy(void *dest, const void *src, const void *params)
  461. {
  462. (void)params;
  463. routerset_t **output = (routerset_t**)dest;
  464. const routerset_t *input = *(routerset_t**)src;
  465. routerset_free(*output); // sets *output to NULL
  466. *output = routerset_new();
  467. routerset_union(*output, input);
  468. return 0;
  469. }
  470. static const var_type_fns_t routerset_type_fns = {
  471. .kv_parse = routerset_kv_parse,
  472. .encode = routerset_encode,
  473. .clear = routerset_clear,
  474. .copy = routerset_copy
  475. };
  476. const var_type_def_t ROUTERSET_type_defn = {
  477. .name = "RouterList",
  478. .fns = &routerset_type_fns
  479. };