routerset.c 14 KB

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