routerset.c 14 KB

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