policies.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file policies.c
  7. * \brief Code to parse and use address policies and exit policies.
  8. **/
  9. #include "or.h"
  10. #include "config.h"
  11. #include "dirserv.h"
  12. #include "nodelist.h"
  13. #include "policies.h"
  14. #include "routerparse.h"
  15. #include "geoip.h"
  16. #include "ht.h"
  17. /** Policy that addresses for incoming SOCKS connections must match. */
  18. static smartlist_t *socks_policy = NULL;
  19. /** Policy that addresses for incoming directory connections must match. */
  20. static smartlist_t *dir_policy = NULL;
  21. /** Policy that addresses for incoming router descriptors must match in order
  22. * to be published by us. */
  23. static smartlist_t *authdir_reject_policy = NULL;
  24. /** Policy that addresses for incoming router descriptors must match in order
  25. * to be marked as valid in our networkstatus. */
  26. static smartlist_t *authdir_invalid_policy = NULL;
  27. /** Policy that addresses for incoming router descriptors must <b>not</b>
  28. * match in order to not be marked as BadDirectory. */
  29. static smartlist_t *authdir_baddir_policy = NULL;
  30. /** Policy that addresses for incoming router descriptors must <b>not</b>
  31. * match in order to not be marked as BadExit. */
  32. static smartlist_t *authdir_badexit_policy = NULL;
  33. /** Parsed addr_policy_t describing which addresses we believe we can start
  34. * circuits at. */
  35. static smartlist_t *reachable_or_addr_policy = NULL;
  36. /** Parsed addr_policy_t describing which addresses we believe we can connect
  37. * to directories at. */
  38. static smartlist_t *reachable_dir_addr_policy = NULL;
  39. /** Element of an exit policy summary */
  40. typedef struct policy_summary_item_t {
  41. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  42. uint16_t prt_max; /**< Highest port number to accept/reject. */
  43. uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
  44. this port range. */
  45. unsigned int accepted:1; /** Has this port already been accepted */
  46. } policy_summary_item_t;
  47. /** Private networks. This list is used in two places, once to expand the
  48. * "private" keyword when parsing our own exit policy, secondly to ignore
  49. * just such networks when building exit policy summaries. It is important
  50. * that all authorities agree on that list when creating summaries, so don't
  51. * just change this without a proper migration plan and a proposal and stuff.
  52. */
  53. static const char *private_nets[] = {
  54. "0.0.0.0/8", "169.254.0.0/16",
  55. "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
  56. "[::]/8",
  57. "[fc00::]/7", "[fe80::]/10", "[fec0::]/10", "[ff00::]/8", "[::]/127",
  58. NULL
  59. };
  60. /** Replace all "private" entries in *<b>policy</b> with their expanded
  61. * equivalents. */
  62. void
  63. policy_expand_private(smartlist_t **policy)
  64. {
  65. uint16_t port_min, port_max;
  66. int i;
  67. smartlist_t *tmp;
  68. if (!*policy) /*XXXX disallow NULL policies? */
  69. return;
  70. tmp = smartlist_new();
  71. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
  72. if (! p->is_private) {
  73. smartlist_add(tmp, p);
  74. continue;
  75. }
  76. for (i = 0; private_nets[i]; ++i) {
  77. addr_policy_t newpolicy;
  78. memcpy(&newpolicy, p, sizeof(addr_policy_t));
  79. newpolicy.is_private = 0;
  80. newpolicy.is_canonical = 0;
  81. if (tor_addr_parse_mask_ports(private_nets[i], 0,
  82. &newpolicy.addr,
  83. &newpolicy.maskbits, &port_min, &port_max)<0) {
  84. tor_assert(0);
  85. }
  86. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy));
  87. }
  88. addr_policy_free(p);
  89. } SMARTLIST_FOREACH_END(p);
  90. smartlist_free(*policy);
  91. *policy = tmp;
  92. }
  93. /** Expand each of the AF_UNSPEC elements in *<b>policy</b> (which indicate
  94. * protocol-neutral wildcards) into a pair of wildcard elements: one IPv4-
  95. * specific and one IPv6-specific. */
  96. void
  97. policy_expand_unspec(smartlist_t **policy)
  98. {
  99. smartlist_t *tmp;
  100. if (!*policy)
  101. return;
  102. tmp = smartlist_new();
  103. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
  104. sa_family_t family = tor_addr_family(&p->addr);
  105. if (family == AF_INET6 || family == AF_INET || p->is_private) {
  106. smartlist_add(tmp, p);
  107. } else if (family == AF_UNSPEC) {
  108. addr_policy_t newpolicy_ipv4;
  109. addr_policy_t newpolicy_ipv6;
  110. memcpy(&newpolicy_ipv4, p, sizeof(addr_policy_t));
  111. memcpy(&newpolicy_ipv6, p, sizeof(addr_policy_t));
  112. newpolicy_ipv4.is_canonical = 0;
  113. newpolicy_ipv6.is_canonical = 0;
  114. if (p->maskbits != 0) {
  115. log_warn(LD_BUG, "AF_UNSPEC policy with maskbits==%d", p->maskbits);
  116. newpolicy_ipv4.maskbits = 0;
  117. newpolicy_ipv6.maskbits = 0;
  118. }
  119. tor_addr_from_ipv4h(&newpolicy_ipv4.addr, 0);
  120. tor_addr_from_ipv6_bytes(&newpolicy_ipv6.addr,
  121. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  122. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv4));
  123. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv6));
  124. addr_policy_free(p);
  125. } else {
  126. log_warn(LD_BUG, "Funny-looking address policy with family %d", family);
  127. smartlist_add(tmp, p);
  128. }
  129. } SMARTLIST_FOREACH_END(p);
  130. smartlist_free(*policy);
  131. *policy = tmp;
  132. }
  133. /**
  134. * Given a linked list of config lines containing "allow" and "deny"
  135. * tokens, parse them and append the result to <b>dest</b>. Return -1
  136. * if any tokens are malformed (and don't append any), else return 0.
  137. *
  138. * If <b>assume_action</b> is nonnegative, then insert its action
  139. * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
  140. * action.
  141. */
  142. static int
  143. parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
  144. int assume_action)
  145. {
  146. smartlist_t *result;
  147. smartlist_t *entries;
  148. addr_policy_t *item;
  149. int r = 0;
  150. if (!cfg)
  151. return 0;
  152. result = smartlist_new();
  153. entries = smartlist_new();
  154. for (; cfg; cfg = cfg->next) {
  155. smartlist_split_string(entries, cfg->value, ",",
  156. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  157. SMARTLIST_FOREACH_BEGIN(entries, const char *, ent) {
  158. log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
  159. item = router_parse_addr_policy_item_from_string(ent, assume_action);
  160. if (item) {
  161. smartlist_add(result, item);
  162. } else {
  163. log_warn(LD_CONFIG,"Malformed policy '%s'.", ent);
  164. r = -1;
  165. }
  166. } SMARTLIST_FOREACH_END(ent);
  167. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  168. smartlist_clear(entries);
  169. }
  170. smartlist_free(entries);
  171. if (r == -1) {
  172. addr_policy_list_free(result);
  173. } else {
  174. policy_expand_private(&result);
  175. policy_expand_unspec(&result);
  176. if (*dest) {
  177. smartlist_add_all(*dest, result);
  178. smartlist_free(result);
  179. } else {
  180. *dest = result;
  181. }
  182. }
  183. return r;
  184. }
  185. /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
  186. * reachable_(or|dir)_addr_policy. The options should already have
  187. * been validated by validate_addr_policies.
  188. */
  189. static int
  190. parse_reachable_addresses(void)
  191. {
  192. const or_options_t *options = get_options();
  193. int ret = 0;
  194. if (options->ReachableDirAddresses &&
  195. options->ReachableORAddresses &&
  196. options->ReachableAddresses) {
  197. log_warn(LD_CONFIG,
  198. "Both ReachableDirAddresses and ReachableORAddresses are set. "
  199. "ReachableAddresses setting will be ignored.");
  200. }
  201. addr_policy_list_free(reachable_or_addr_policy);
  202. reachable_or_addr_policy = NULL;
  203. if (!options->ReachableORAddresses && options->ReachableAddresses)
  204. log_info(LD_CONFIG,
  205. "Using ReachableAddresses as ReachableORAddresses.");
  206. if (parse_addr_policy(options->ReachableORAddresses ?
  207. options->ReachableORAddresses :
  208. options->ReachableAddresses,
  209. &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
  210. log_warn(LD_CONFIG,
  211. "Error parsing Reachable%sAddresses entry; ignoring.",
  212. options->ReachableORAddresses ? "OR" : "");
  213. ret = -1;
  214. }
  215. addr_policy_list_free(reachable_dir_addr_policy);
  216. reachable_dir_addr_policy = NULL;
  217. if (!options->ReachableDirAddresses && options->ReachableAddresses)
  218. log_info(LD_CONFIG,
  219. "Using ReachableAddresses as ReachableDirAddresses");
  220. if (parse_addr_policy(options->ReachableDirAddresses ?
  221. options->ReachableDirAddresses :
  222. options->ReachableAddresses,
  223. &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
  224. if (options->ReachableDirAddresses)
  225. log_warn(LD_CONFIG,
  226. "Error parsing ReachableDirAddresses entry; ignoring.");
  227. ret = -1;
  228. }
  229. return ret;
  230. }
  231. /** Return true iff the firewall options might block any address:port
  232. * combination.
  233. */
  234. int
  235. firewall_is_fascist_or(void)
  236. {
  237. return reachable_or_addr_policy != NULL;
  238. }
  239. /** Return true iff <b>policy</b> (possibly NULL) will allow a
  240. * connection to <b>addr</b>:<b>port</b>.
  241. */
  242. static int
  243. addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
  244. smartlist_t *policy)
  245. {
  246. addr_policy_result_t p;
  247. p = compare_tor_addr_to_addr_policy(addr, port, policy);
  248. switch (p) {
  249. case ADDR_POLICY_PROBABLY_ACCEPTED:
  250. case ADDR_POLICY_ACCEPTED:
  251. return 1;
  252. case ADDR_POLICY_PROBABLY_REJECTED:
  253. case ADDR_POLICY_REJECTED:
  254. return 0;
  255. default:
  256. log_warn(LD_BUG, "Unexpected result: %d", (int)p);
  257. return 0;
  258. }
  259. }
  260. /** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
  261. * <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
  262. * order. */
  263. /* XXXX deprecate when possible. */
  264. static int
  265. addr_policy_permits_address(uint32_t addr, uint16_t port,
  266. smartlist_t *policy)
  267. {
  268. tor_addr_t a;
  269. tor_addr_from_ipv4h(&a, addr);
  270. return addr_policy_permits_tor_addr(&a, port, policy);
  271. }
  272. /** Return true iff we think our firewall will let us make an OR connection to
  273. * addr:port. */
  274. int
  275. fascist_firewall_allows_address_or(const tor_addr_t *addr, uint16_t port)
  276. {
  277. return addr_policy_permits_tor_addr(addr, port,
  278. reachable_or_addr_policy);
  279. }
  280. /** Return true iff we think our firewall will let us make an OR connection to
  281. * <b>ri</b>. */
  282. int
  283. fascist_firewall_allows_or(const routerinfo_t *ri)
  284. {
  285. /* XXXX proposal 118 */
  286. tor_addr_t addr;
  287. tor_addr_from_ipv4h(&addr, ri->addr);
  288. return fascist_firewall_allows_address_or(&addr, ri->or_port);
  289. }
  290. /** Return true iff we think our firewall will let us make an OR connection to
  291. * <b>node</b>. */
  292. int
  293. fascist_firewall_allows_node(const node_t *node)
  294. {
  295. if (node->ri) {
  296. return fascist_firewall_allows_or(node->ri);
  297. } else if (node->rs) {
  298. tor_addr_t addr;
  299. tor_addr_from_ipv4h(&addr, node->rs->addr);
  300. return fascist_firewall_allows_address_or(&addr, node->rs->or_port);
  301. } else {
  302. return 1;
  303. }
  304. }
  305. /** Return true iff we think our firewall will let us make a directory
  306. * connection to addr:port. */
  307. int
  308. fascist_firewall_allows_address_dir(const tor_addr_t *addr, uint16_t port)
  309. {
  310. return addr_policy_permits_tor_addr(addr, port,
  311. reachable_dir_addr_policy);
  312. }
  313. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  314. * based on <b>dir_policy</b>. Else return 0.
  315. */
  316. int
  317. dir_policy_permits_address(const tor_addr_t *addr)
  318. {
  319. return addr_policy_permits_tor_addr(addr, 1, dir_policy);
  320. }
  321. /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
  322. * based on <b>socks_policy</b>. Else return 0.
  323. */
  324. int
  325. socks_policy_permits_address(const tor_addr_t *addr)
  326. {
  327. return addr_policy_permits_tor_addr(addr, 1, socks_policy);
  328. }
  329. /** Return true iff the address <b>addr</b> is in a country listed in the
  330. * case-insensitive list of country codes <b>cc_list</b>. */
  331. static int
  332. addr_is_in_cc_list(uint32_t addr, const smartlist_t *cc_list)
  333. {
  334. country_t country;
  335. const char *name;
  336. tor_addr_t tar;
  337. if (!cc_list)
  338. return 0;
  339. /* XXXXipv6 */
  340. tor_addr_from_ipv4h(&tar, addr);
  341. country = geoip_get_country_by_addr(&tar);
  342. name = geoip_get_country_name(country);
  343. return smartlist_contains_string_case(cc_list, name);
  344. }
  345. /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
  346. * directory, based on <b>authdir_reject_policy</b>. Else return 0.
  347. */
  348. int
  349. authdir_policy_permits_address(uint32_t addr, uint16_t port)
  350. {
  351. if (! addr_policy_permits_address(addr, port, authdir_reject_policy))
  352. return 0;
  353. return !addr_is_in_cc_list(addr, get_options()->AuthDirRejectCCs);
  354. }
  355. /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
  356. * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
  357. */
  358. int
  359. authdir_policy_valid_address(uint32_t addr, uint16_t port)
  360. {
  361. if (! addr_policy_permits_address(addr, port, authdir_invalid_policy))
  362. return 0;
  363. return !addr_is_in_cc_list(addr, get_options()->AuthDirInvalidCCs);
  364. }
  365. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad dir,
  366. * based on <b>authdir_baddir_policy</b>. Else return 0.
  367. */
  368. int
  369. authdir_policy_baddir_address(uint32_t addr, uint16_t port)
  370. {
  371. if (! addr_policy_permits_address(addr, port, authdir_baddir_policy))
  372. return 1;
  373. return addr_is_in_cc_list(addr, get_options()->AuthDirBadDirCCs);
  374. }
  375. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
  376. * based on <b>authdir_badexit_policy</b>. Else return 0.
  377. */
  378. int
  379. authdir_policy_badexit_address(uint32_t addr, uint16_t port)
  380. {
  381. if (! addr_policy_permits_address(addr, port, authdir_badexit_policy))
  382. return 1;
  383. return addr_is_in_cc_list(addr, get_options()->AuthDirBadExitCCs);
  384. }
  385. #define REJECT(arg) \
  386. STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
  387. /** Config helper: If there's any problem with the policy configuration
  388. * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
  389. * allocated description of the error. Else return 0. */
  390. int
  391. validate_addr_policies(const or_options_t *options, char **msg)
  392. {
  393. /* XXXX Maybe merge this into parse_policies_from_options, to make sure
  394. * that the two can't go out of sync. */
  395. smartlist_t *addr_policy=NULL;
  396. *msg = NULL;
  397. if (policies_parse_exit_policy(options->ExitPolicy, &addr_policy,
  398. options->IPv6Exit,
  399. options->ExitPolicyRejectPrivate, NULL,
  400. !options->BridgeRelay))
  401. REJECT("Error in ExitPolicy entry.");
  402. /* The rest of these calls *append* to addr_policy. So don't actually
  403. * use the results for anything other than checking if they parse! */
  404. if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
  405. REJECT("Error in DirPolicy entry.");
  406. if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
  407. REJECT("Error in SocksPolicy entry.");
  408. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  409. ADDR_POLICY_REJECT))
  410. REJECT("Error in AuthDirReject entry.");
  411. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  412. ADDR_POLICY_REJECT))
  413. REJECT("Error in AuthDirInvalid entry.");
  414. if (parse_addr_policy(options->AuthDirBadDir, &addr_policy,
  415. ADDR_POLICY_REJECT))
  416. REJECT("Error in AuthDirBadDir entry.");
  417. if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
  418. ADDR_POLICY_REJECT))
  419. REJECT("Error in AuthDirBadExit entry.");
  420. if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
  421. ADDR_POLICY_ACCEPT))
  422. REJECT("Error in ReachableAddresses entry.");
  423. if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
  424. ADDR_POLICY_ACCEPT))
  425. REJECT("Error in ReachableORAddresses entry.");
  426. if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
  427. ADDR_POLICY_ACCEPT))
  428. REJECT("Error in ReachableDirAddresses entry.");
  429. err:
  430. addr_policy_list_free(addr_policy);
  431. return *msg ? -1 : 0;
  432. #undef REJECT
  433. }
  434. /** Parse <b>string</b> in the same way that the exit policy
  435. * is parsed, and put the processed version in *<b>policy</b>.
  436. * Ignore port specifiers.
  437. */
  438. static int
  439. load_policy_from_option(config_line_t *config, smartlist_t **policy,
  440. int assume_action)
  441. {
  442. int r;
  443. addr_policy_list_free(*policy);
  444. *policy = NULL;
  445. r = parse_addr_policy(config, policy, assume_action);
  446. if (r < 0) {
  447. return -1;
  448. }
  449. if (*policy) {
  450. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, n) {
  451. /* ports aren't used in these. */
  452. if (n->prt_min > 1 || n->prt_max != 65535) {
  453. addr_policy_t newp, *c;
  454. memcpy(&newp, n, sizeof(newp));
  455. newp.prt_min = 1;
  456. newp.prt_max = 65535;
  457. newp.is_canonical = 0;
  458. c = addr_policy_get_canonical_entry(&newp);
  459. SMARTLIST_REPLACE_CURRENT(*policy, n, c);
  460. addr_policy_free(n);
  461. }
  462. } SMARTLIST_FOREACH_END(n);
  463. }
  464. return 0;
  465. }
  466. /** Set all policies based on <b>options</b>, which should have been validated
  467. * first by validate_addr_policies. */
  468. int
  469. policies_parse_from_options(const or_options_t *options)
  470. {
  471. int ret = 0;
  472. if (load_policy_from_option(options->SocksPolicy, &socks_policy, -1) < 0)
  473. ret = -1;
  474. if (load_policy_from_option(options->DirPolicy, &dir_policy, -1) < 0)
  475. ret = -1;
  476. if (load_policy_from_option(options->AuthDirReject,
  477. &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
  478. ret = -1;
  479. if (load_policy_from_option(options->AuthDirInvalid,
  480. &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
  481. ret = -1;
  482. if (load_policy_from_option(options->AuthDirBadDir,
  483. &authdir_baddir_policy, ADDR_POLICY_REJECT) < 0)
  484. ret = -1;
  485. if (load_policy_from_option(options->AuthDirBadExit,
  486. &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
  487. ret = -1;
  488. if (parse_reachable_addresses() < 0)
  489. ret = -1;
  490. return ret;
  491. }
  492. /** Compare two provided address policy items, and return -1, 0, or 1
  493. * if the first is less than, equal to, or greater than the second. */
  494. static int
  495. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  496. {
  497. int r;
  498. if ((r=((int)a->policy_type - (int)b->policy_type)))
  499. return r;
  500. if ((r=((int)a->is_private - (int)b->is_private)))
  501. return r;
  502. if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
  503. return r;
  504. if ((r=((int)a->maskbits - (int)b->maskbits)))
  505. return r;
  506. if ((r=((int)a->prt_min - (int)b->prt_min)))
  507. return r;
  508. if ((r=((int)a->prt_max - (int)b->prt_max)))
  509. return r;
  510. return 0;
  511. }
  512. /** Like cmp_single_addr_policy() above, but looks at the
  513. * whole set of policies in each case. */
  514. int
  515. cmp_addr_policies(smartlist_t *a, smartlist_t *b)
  516. {
  517. int r, i;
  518. int len_a = a ? smartlist_len(a) : 0;
  519. int len_b = b ? smartlist_len(b) : 0;
  520. for (i = 0; i < len_a && i < len_b; ++i) {
  521. if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
  522. return r;
  523. }
  524. if (i == len_a && i == len_b)
  525. return 0;
  526. if (i < len_a)
  527. return -1;
  528. else
  529. return 1;
  530. }
  531. /** Node in hashtable used to store address policy entries. */
  532. typedef struct policy_map_ent_t {
  533. HT_ENTRY(policy_map_ent_t) node;
  534. addr_policy_t *policy;
  535. } policy_map_ent_t;
  536. /* DOCDOC policy_root */
  537. static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
  538. /** Return true iff a and b are equal. */
  539. static INLINE int
  540. policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
  541. {
  542. return cmp_single_addr_policy(a->policy, b->policy) == 0;
  543. }
  544. /** Return a hashcode for <b>ent</b> */
  545. static unsigned int
  546. policy_hash(const policy_map_ent_t *ent)
  547. {
  548. const addr_policy_t *a = ent->policy;
  549. addr_policy_t aa;
  550. memset(&aa, 0, sizeof(aa));
  551. aa.prt_min = a->prt_min;
  552. aa.prt_max = a->prt_max;
  553. aa.maskbits = a->maskbits;
  554. aa.policy_type = a->policy_type;
  555. aa.is_private = a->is_private;
  556. if (a->is_private) {
  557. aa.is_private = 1;
  558. } else {
  559. tor_addr_copy_tight(&aa.addr, &a->addr);
  560. }
  561. return (unsigned) siphash24g(&aa, sizeof(aa));
  562. }
  563. HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
  564. policy_eq)
  565. HT_GENERATE(policy_map, policy_map_ent_t, node, policy_hash,
  566. policy_eq, 0.6, malloc, realloc, free)
  567. /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
  568. * "canonical" copy of that addr_policy_t; the canonical copy is a single
  569. * reference-counted object. */
  570. addr_policy_t *
  571. addr_policy_get_canonical_entry(addr_policy_t *e)
  572. {
  573. policy_map_ent_t search, *found;
  574. if (e->is_canonical)
  575. return e;
  576. search.policy = e;
  577. found = HT_FIND(policy_map, &policy_root, &search);
  578. if (!found) {
  579. found = tor_malloc_zero(sizeof(policy_map_ent_t));
  580. found->policy = tor_memdup(e, sizeof(addr_policy_t));
  581. found->policy->is_canonical = 1;
  582. found->policy->refcnt = 0;
  583. HT_INSERT(policy_map, &policy_root, found);
  584. }
  585. tor_assert(!cmp_single_addr_policy(found->policy, e));
  586. ++found->policy->refcnt;
  587. return found->policy;
  588. }
  589. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  590. * addr and port are both known. */
  591. static addr_policy_result_t
  592. compare_known_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  593. const smartlist_t *policy)
  594. {
  595. /* We know the address and port, and we know the policy, so we can just
  596. * compute an exact match. */
  597. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  598. /* Address is known */
  599. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  600. CMP_EXACT)) {
  601. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  602. /* Exact match for the policy */
  603. return tmpe->policy_type == ADDR_POLICY_ACCEPT ?
  604. ADDR_POLICY_ACCEPTED : ADDR_POLICY_REJECTED;
  605. }
  606. }
  607. } SMARTLIST_FOREACH_END(tmpe);
  608. /* accept all by default. */
  609. return ADDR_POLICY_ACCEPTED;
  610. }
  611. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  612. * addr is known but port is not. */
  613. static addr_policy_result_t
  614. compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t *addr,
  615. const smartlist_t *policy)
  616. {
  617. /* We look to see if there's a definite match. If so, we return that
  618. match's value, unless there's an intervening possible match that says
  619. something different. */
  620. int maybe_accept = 0, maybe_reject = 0;
  621. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  622. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  623. CMP_EXACT)) {
  624. if (tmpe->prt_min <= 1 && tmpe->prt_max >= 65535) {
  625. /* Definitely matches, since it covers all ports. */
  626. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  627. /* If we already hit a clause that might trigger a 'reject', than we
  628. * can't be sure of this certain 'accept'.*/
  629. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  630. ADDR_POLICY_ACCEPTED;
  631. } else {
  632. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  633. ADDR_POLICY_REJECTED;
  634. }
  635. } else {
  636. /* Might match. */
  637. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  638. maybe_reject = 1;
  639. else
  640. maybe_accept = 1;
  641. }
  642. }
  643. } SMARTLIST_FOREACH_END(tmpe);
  644. /* accept all by default. */
  645. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  646. }
  647. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  648. * port is known but address is not. */
  649. static addr_policy_result_t
  650. compare_unknown_tor_addr_to_addr_policy(uint16_t port,
  651. const smartlist_t *policy)
  652. {
  653. /* We look to see if there's a definite match. If so, we return that
  654. match's value, unless there's an intervening possible match that says
  655. something different. */
  656. int maybe_accept = 0, maybe_reject = 0;
  657. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  658. if (tmpe->prt_min <= port && port <= tmpe->prt_max) {
  659. if (tmpe->maskbits == 0) {
  660. /* Definitely matches, since it covers all addresses. */
  661. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  662. /* If we already hit a clause that might trigger a 'reject', than we
  663. * can't be sure of this certain 'accept'.*/
  664. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  665. ADDR_POLICY_ACCEPTED;
  666. } else {
  667. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  668. ADDR_POLICY_REJECTED;
  669. }
  670. } else {
  671. /* Might match. */
  672. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  673. maybe_reject = 1;
  674. else
  675. maybe_accept = 1;
  676. }
  677. }
  678. } SMARTLIST_FOREACH_END(tmpe);
  679. /* accept all by default. */
  680. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  681. }
  682. /** Decide whether a given addr:port is definitely accepted,
  683. * definitely rejected, probably accepted, or probably rejected by a
  684. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  685. * target address. If <b>port</b> is 0, we don't know the port of the
  686. * target address. (At least one of <b>addr</b> and <b>port</b> must be
  687. * provided. If you want to know whether a policy would definitely reject
  688. * an unknown address:port, use policy_is_reject_star().)
  689. *
  690. * We could do better by assuming that some ranges never match typical
  691. * addresses (127.0.0.1, and so on). But we'll try this for now.
  692. */
  693. addr_policy_result_t
  694. compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  695. const smartlist_t *policy)
  696. {
  697. if (!policy) {
  698. /* no policy? accept all. */
  699. return ADDR_POLICY_ACCEPTED;
  700. } else if (addr == NULL || tor_addr_is_null(addr)) {
  701. if (port == 0) {
  702. log_info(LD_BUG, "Rejecting null address with 0 port (family %d)",
  703. addr ? tor_addr_family(addr) : -1);
  704. return ADDR_POLICY_REJECTED;
  705. }
  706. return compare_unknown_tor_addr_to_addr_policy(port, policy);
  707. } else if (port == 0) {
  708. return compare_known_tor_addr_to_addr_policy_noport(addr, policy);
  709. } else {
  710. return compare_known_tor_addr_to_addr_policy(addr, port, policy);
  711. }
  712. }
  713. /** Return true iff the address policy <b>a</b> covers every case that
  714. * would be covered by <b>b</b>, so that a,b is redundant. */
  715. static int
  716. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  717. {
  718. if (tor_addr_family(&a->addr) != tor_addr_family(&b->addr)) {
  719. /* You can't cover a different family. */
  720. return 0;
  721. }
  722. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  723. * to "accept *:80". */
  724. if (a->maskbits > b->maskbits) {
  725. /* a has more fixed bits than b; it can't possibly cover b. */
  726. return 0;
  727. }
  728. if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_EXACT)) {
  729. /* There's a fixed bit in a that's set differently in b. */
  730. return 0;
  731. }
  732. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  733. }
  734. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  735. * that is, there exists an address/port that is covered by <b>a</b> that
  736. * is also covered by <b>b</b>.
  737. */
  738. static int
  739. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  740. {
  741. maskbits_t minbits;
  742. /* All the bits we care about are those that are set in both
  743. * netmasks. If they are equal in a and b's networkaddresses
  744. * then the networks intersect. If there is a difference,
  745. * then they do not. */
  746. if (a->maskbits < b->maskbits)
  747. minbits = a->maskbits;
  748. else
  749. minbits = b->maskbits;
  750. if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_EXACT))
  751. return 0;
  752. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  753. return 0;
  754. return 1;
  755. }
  756. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  757. */
  758. static void
  759. append_exit_policy_string(smartlist_t **policy, const char *more)
  760. {
  761. config_line_t tmp;
  762. tmp.key = NULL;
  763. tmp.value = (char*) more;
  764. tmp.next = NULL;
  765. if (parse_addr_policy(&tmp, policy, -1)<0) {
  766. log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
  767. }
  768. }
  769. /** Add "reject <b>addr</b>:*" to <b>dest</b>, creating the list as needed. */
  770. void
  771. addr_policy_append_reject_addr(smartlist_t **dest, const tor_addr_t *addr)
  772. {
  773. addr_policy_t p, *add;
  774. memset(&p, 0, sizeof(p));
  775. p.policy_type = ADDR_POLICY_REJECT;
  776. p.maskbits = tor_addr_family(addr) == AF_INET6 ? 128 : 32;
  777. tor_addr_copy(&p.addr, addr);
  778. p.prt_min = 1;
  779. p.prt_max = 65535;
  780. add = addr_policy_get_canonical_entry(&p);
  781. if (!*dest)
  782. *dest = smartlist_new();
  783. smartlist_add(*dest, add);
  784. }
  785. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  786. static void
  787. exit_policy_remove_redundancies(smartlist_t *dest)
  788. {
  789. addr_policy_t *ap, *tmp;
  790. int i, j;
  791. /* Step one: kill every ipv4 thing after *4:*, every IPv6 thing after *6:*
  792. */
  793. {
  794. int kill_v4=0, kill_v6=0;
  795. for (i = 0; i < smartlist_len(dest); ++i) {
  796. sa_family_t family;
  797. ap = smartlist_get(dest, i);
  798. family = tor_addr_family(&ap->addr);
  799. if ((family == AF_INET && kill_v4) ||
  800. (family == AF_INET6 && kill_v6)) {
  801. smartlist_del_keeporder(dest, i--);
  802. addr_policy_free(ap);
  803. continue;
  804. }
  805. if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  806. /* This is a catch-all line -- later lines are unreachable. */
  807. if (family == AF_INET) {
  808. kill_v4 = 1;
  809. } else if (family == AF_INET6) {
  810. kill_v6 = 1;
  811. }
  812. }
  813. }
  814. }
  815. /* Step two: for every entry, see if there's a redundant entry
  816. * later on, and remove it. */
  817. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  818. ap = smartlist_get(dest, i);
  819. for (j = i+1; j < smartlist_len(dest); ++j) {
  820. tmp = smartlist_get(dest, j);
  821. tor_assert(j > i);
  822. if (addr_policy_covers(ap, tmp)) {
  823. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  824. policy_write_item(p1, sizeof(p1), tmp, 0);
  825. policy_write_item(p2, sizeof(p2), ap, 0);
  826. log_debug(LD_CONFIG, "Removing exit policy %s (%d). It is made "
  827. "redundant by %s (%d).", p1, j, p2, i);
  828. smartlist_del_keeporder(dest, j--);
  829. addr_policy_free(tmp);
  830. }
  831. }
  832. }
  833. /* Step three: for every entry A, see if there's an entry B making this one
  834. * redundant later on. This is the case if A and B are of the same type
  835. * (accept/reject), A is a subset of B, and there is no other entry of
  836. * different type in between those two that intersects with A.
  837. *
  838. * Anybody want to double-check the logic here? XXX
  839. */
  840. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  841. ap = smartlist_get(dest, i);
  842. for (j = i+1; j < smartlist_len(dest); ++j) {
  843. // tor_assert(j > i); // j starts out at i+1; j only increases; i only
  844. // // decreases.
  845. tmp = smartlist_get(dest, j);
  846. if (ap->policy_type != tmp->policy_type) {
  847. if (addr_policy_intersects(ap, tmp))
  848. break;
  849. } else { /* policy_types are equal. */
  850. if (addr_policy_covers(tmp, ap)) {
  851. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  852. policy_write_item(p1, sizeof(p1), ap, 0);
  853. policy_write_item(p2, sizeof(p2), tmp, 0);
  854. log_debug(LD_CONFIG, "Removing exit policy %s. It is already "
  855. "covered by %s.", p1, p2);
  856. smartlist_del_keeporder(dest, i--);
  857. addr_policy_free(ap);
  858. break;
  859. }
  860. }
  861. }
  862. }
  863. }
  864. #define DEFAULT_EXIT_POLICY \
  865. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  866. "reject *:563,reject *:1214,reject *:4661-4666," \
  867. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  868. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
  869. * cfg doesn't end in an absolute accept or reject and if
  870. * <b>add_default_policy</b> is true, add the default exit
  871. * policy afterwards. If <b>rejectprivate</b> is true, prepend
  872. * "reject private:*" to the policy. Return -1 if we can't parse cfg,
  873. * else return 0.
  874. *
  875. * This function is used to parse the exit policy from our torrc. For
  876. * the functions used to parse the exit policy from a router descriptor,
  877. * see router_add_exit_policy.
  878. */
  879. int
  880. policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
  881. int ipv6_exit,
  882. int rejectprivate, const char *local_address,
  883. int add_default_policy)
  884. {
  885. if (!ipv6_exit) {
  886. append_exit_policy_string(dest, "reject *6:*");
  887. }
  888. if (rejectprivate) {
  889. append_exit_policy_string(dest, "reject private:*");
  890. if (local_address) {
  891. char buf[POLICY_BUF_LEN];
  892. tor_snprintf(buf, sizeof(buf), "reject %s:*", local_address);
  893. append_exit_policy_string(dest, buf);
  894. }
  895. }
  896. if (parse_addr_policy(cfg, dest, -1))
  897. return -1;
  898. if (add_default_policy) {
  899. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  900. } else {
  901. append_exit_policy_string(dest, "reject *4:*");
  902. append_exit_policy_string(dest, "reject *6:*");
  903. }
  904. exit_policy_remove_redundancies(*dest);
  905. return 0;
  906. }
  907. /** Add "reject *:*" to the end of the policy in *<b>dest</b>, allocating
  908. * *<b>dest</b> as needed. */
  909. void
  910. policies_exit_policy_append_reject_star(smartlist_t **dest)
  911. {
  912. append_exit_policy_string(dest, "reject *4:*");
  913. append_exit_policy_string(dest, "reject *6:*");
  914. }
  915. /** Replace the exit policy of <b>node</b> with reject *:* */
  916. void
  917. policies_set_node_exitpolicy_to_reject_all(node_t *node)
  918. {
  919. node->rejects_all = 1;
  920. }
  921. /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
  922. * allows exiting to <b>port</b>. Otherwise, return 0. */
  923. static int
  924. exit_policy_is_general_exit_helper(smartlist_t *policy, int port)
  925. {
  926. uint32_t mask, ip, i;
  927. /* Is this /8 rejected (1), or undecided (0)? */
  928. char subnet_status[256];
  929. memset(subnet_status, 0, sizeof(subnet_status));
  930. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  931. if (tor_addr_family(&p->addr) != AF_INET)
  932. continue; /* IPv4 only for now */
  933. if (p->prt_min > port || p->prt_max < port)
  934. continue; /* Doesn't cover our port. */
  935. mask = 0;
  936. tor_assert(p->maskbits <= 32);
  937. if (p->maskbits)
  938. mask = UINT32_MAX<<(32-p->maskbits);
  939. ip = tor_addr_to_ipv4h(&p->addr);
  940. /* Calculate the first and last subnet that this exit policy touches
  941. * and set it as loop boundaries. */
  942. for (i = ((mask & ip)>>24); i <= (~((mask & ip) ^ mask)>>24); ++i) {
  943. tor_addr_t addr;
  944. if (subnet_status[i] != 0)
  945. continue; /* We already reject some part of this /8 */
  946. tor_addr_from_ipv4h(&addr, i<<24);
  947. if (tor_addr_is_internal(&addr, 0))
  948. continue; /* Local or non-routable addresses */
  949. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  950. if (p->maskbits > 8)
  951. continue; /* Narrower than a /8. */
  952. /* We found an allowed subnet of at least size /8. Done
  953. * for this port! */
  954. return 1;
  955. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  956. subnet_status[i] = 1;
  957. }
  958. }
  959. } SMARTLIST_FOREACH_END(p);
  960. return 0;
  961. }
  962. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  963. * it allows exit to at least one /8 address space for at least
  964. * two of ports 80, 443, and 6667. */
  965. int
  966. exit_policy_is_general_exit(smartlist_t *policy)
  967. {
  968. static const int ports[] = { 80, 443, 6667 };
  969. int n_allowed = 0;
  970. int i;
  971. if (!policy) /*XXXX disallow NULL policies? */
  972. return 0;
  973. for (i = 0; i < 3; ++i) {
  974. n_allowed += exit_policy_is_general_exit_helper(policy, ports[i]);
  975. }
  976. return n_allowed >= 2;
  977. }
  978. /** Return false if <b>policy</b> might permit access to some addr:port;
  979. * otherwise if we are certain it rejects everything, return true. */
  980. int
  981. policy_is_reject_star(const smartlist_t *policy, sa_family_t family)
  982. {
  983. if (!policy) /*XXXX disallow NULL policies? */
  984. return 1;
  985. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  986. if (p->policy_type == ADDR_POLICY_ACCEPT &&
  987. (tor_addr_family(&p->addr) == family ||
  988. tor_addr_family(&p->addr) == AF_UNSPEC)) {
  989. return 0;
  990. } else if (p->policy_type == ADDR_POLICY_REJECT &&
  991. p->prt_min <= 1 && p->prt_max == 65535 &&
  992. p->maskbits == 0 &&
  993. (tor_addr_family(&p->addr) == family ||
  994. tor_addr_family(&p->addr) == AF_UNSPEC)) {
  995. return 1;
  996. }
  997. } SMARTLIST_FOREACH_END(p);
  998. return 1;
  999. }
  1000. /** Write a single address policy to the buf_len byte buffer at buf. Return
  1001. * the number of characters written, or -1 on failure. */
  1002. int
  1003. policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
  1004. int format_for_desc)
  1005. {
  1006. size_t written = 0;
  1007. char addrbuf[TOR_ADDR_BUF_LEN];
  1008. const char *addrpart;
  1009. int result;
  1010. const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
  1011. const sa_family_t family = tor_addr_family(&policy->addr);
  1012. const int is_ip6 = (family == AF_INET6);
  1013. tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
  1014. /* write accept/reject 1.2.3.4 */
  1015. if (policy->is_private) {
  1016. addrpart = "private";
  1017. } else if (policy->maskbits == 0) {
  1018. if (format_for_desc)
  1019. addrpart = "*";
  1020. else if (family == AF_INET6)
  1021. addrpart = "*6";
  1022. else if (family == AF_INET)
  1023. addrpart = "*4";
  1024. else
  1025. addrpart = "*";
  1026. } else {
  1027. addrpart = addrbuf;
  1028. }
  1029. result = tor_snprintf(buf, buflen, "%s%s %s",
  1030. is_accept ? "accept" : "reject",
  1031. (is_ip6&&format_for_desc)?"6":"",
  1032. addrpart);
  1033. if (result < 0)
  1034. return -1;
  1035. written += strlen(buf);
  1036. /* If the maskbits is 32 we don't need to give it. If the mask is 0,
  1037. * we already wrote "*". */
  1038. if (policy->maskbits < 32 && policy->maskbits > 0) {
  1039. if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
  1040. return -1;
  1041. written += strlen(buf+written);
  1042. }
  1043. if (policy->prt_min <= 1 && policy->prt_max == 65535) {
  1044. /* There is no port set; write ":*" */
  1045. if (written+4 > buflen)
  1046. return -1;
  1047. strlcat(buf+written, ":*", buflen-written);
  1048. written += 2;
  1049. } else if (policy->prt_min == policy->prt_max) {
  1050. /* There is only one port; write ":80". */
  1051. result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
  1052. if (result<0)
  1053. return -1;
  1054. written += result;
  1055. } else {
  1056. /* There is a range of ports; write ":79-80". */
  1057. result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
  1058. policy->prt_min, policy->prt_max);
  1059. if (result<0)
  1060. return -1;
  1061. written += result;
  1062. }
  1063. if (written < buflen)
  1064. buf[written] = '\0';
  1065. else
  1066. return -1;
  1067. return (int)written;
  1068. }
  1069. /** Create a new exit policy summary, initially only with a single
  1070. * port 1-64k item */
  1071. /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
  1072. * RB-tree if that turns out to matter. */
  1073. static smartlist_t *
  1074. policy_summary_create(void)
  1075. {
  1076. smartlist_t *summary;
  1077. policy_summary_item_t* item;
  1078. item = tor_malloc_zero(sizeof(policy_summary_item_t));
  1079. item->prt_min = 1;
  1080. item->prt_max = 65535;
  1081. item->reject_count = 0;
  1082. item->accepted = 0;
  1083. summary = smartlist_new();
  1084. smartlist_add(summary, item);
  1085. return summary;
  1086. }
  1087. /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
  1088. * The current item is changed to end at new-starts - 1, the new item
  1089. * copies reject_count and accepted from the old item,
  1090. * starts at new_starts and ends at the port where the original item
  1091. * previously ended.
  1092. */
  1093. static policy_summary_item_t*
  1094. policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts)
  1095. {
  1096. policy_summary_item_t* new;
  1097. new = tor_malloc_zero(sizeof(policy_summary_item_t));
  1098. new->prt_min = new_starts;
  1099. new->prt_max = old->prt_max;
  1100. new->reject_count = old->reject_count;
  1101. new->accepted = old->accepted;
  1102. old->prt_max = new_starts-1;
  1103. tor_assert(old->prt_min <= old->prt_max);
  1104. tor_assert(new->prt_min <= new->prt_max);
  1105. return new;
  1106. }
  1107. /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
  1108. * my immortal soul, he can clean it up himself. */
  1109. #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
  1110. #define REJECT_CUTOFF_COUNT (1<<25)
  1111. /** Split an exit policy summary so that prt_min and prt_max
  1112. * fall at exactly the start and end of an item respectively.
  1113. */
  1114. static int
  1115. policy_summary_split(smartlist_t *summary,
  1116. uint16_t prt_min, uint16_t prt_max)
  1117. {
  1118. int start_at_index;
  1119. int i = 0;
  1120. while (AT(i)->prt_max < prt_min)
  1121. i++;
  1122. if (AT(i)->prt_min != prt_min) {
  1123. policy_summary_item_t* new_item;
  1124. new_item = policy_summary_item_split(AT(i), prt_min);
  1125. smartlist_insert(summary, i+1, new_item);
  1126. i++;
  1127. }
  1128. start_at_index = i;
  1129. while (AT(i)->prt_max < prt_max)
  1130. i++;
  1131. if (AT(i)->prt_max != prt_max) {
  1132. policy_summary_item_t* new_item;
  1133. new_item = policy_summary_item_split(AT(i), prt_max+1);
  1134. smartlist_insert(summary, i+1, new_item);
  1135. }
  1136. return start_at_index;
  1137. }
  1138. /** Mark port ranges as accepted if they are below the reject_count */
  1139. static void
  1140. policy_summary_accept(smartlist_t *summary,
  1141. uint16_t prt_min, uint16_t prt_max)
  1142. {
  1143. int i = policy_summary_split(summary, prt_min, prt_max);
  1144. while (i < smartlist_len(summary) &&
  1145. AT(i)->prt_max <= prt_max) {
  1146. if (!AT(i)->accepted &&
  1147. AT(i)->reject_count <= REJECT_CUTOFF_COUNT)
  1148. AT(i)->accepted = 1;
  1149. i++;
  1150. }
  1151. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1152. }
  1153. /** Count the number of addresses in a network with prefixlen maskbits
  1154. * against the given portrange. */
  1155. static void
  1156. policy_summary_reject(smartlist_t *summary,
  1157. maskbits_t maskbits,
  1158. uint16_t prt_min, uint16_t prt_max)
  1159. {
  1160. int i = policy_summary_split(summary, prt_min, prt_max);
  1161. /* XXX: ipv4 specific */
  1162. uint64_t count = (U64_LITERAL(1) << (32-maskbits));
  1163. while (i < smartlist_len(summary) &&
  1164. AT(i)->prt_max <= prt_max) {
  1165. AT(i)->reject_count += count;
  1166. i++;
  1167. }
  1168. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1169. }
  1170. /** Add a single exit policy item to our summary:
  1171. * If it is an accept ignore it unless it is for all IP addresses
  1172. * ("*"), i.e. it's prefixlen/maskbits is 0, else call
  1173. * policy_summary_accept().
  1174. * If it's a reject ignore it if it is about one of the private
  1175. * networks, else call policy_summary_reject().
  1176. */
  1177. static void
  1178. policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
  1179. {
  1180. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  1181. if (p->maskbits == 0) {
  1182. policy_summary_accept(summary, p->prt_min, p->prt_max);
  1183. }
  1184. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  1185. int is_private = 0;
  1186. int i;
  1187. for (i = 0; private_nets[i]; ++i) {
  1188. tor_addr_t addr;
  1189. maskbits_t maskbits;
  1190. if (tor_addr_parse_mask_ports(private_nets[i], 0, &addr,
  1191. &maskbits, NULL, NULL)<0) {
  1192. tor_assert(0);
  1193. }
  1194. if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
  1195. p->maskbits == maskbits) {
  1196. is_private = 1;
  1197. break;
  1198. }
  1199. }
  1200. if (!is_private) {
  1201. policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
  1202. }
  1203. } else
  1204. tor_assert(0);
  1205. }
  1206. /** Create a string representing a summary for an exit policy.
  1207. * The summary will either be an "accept" plus a comma-separated list of port
  1208. * ranges or a "reject" plus port-ranges, depending on which is shorter.
  1209. *
  1210. * If no exits are allowed at all then NULL is returned, if no ports
  1211. * are blocked instead of "reject " we return "accept 1-65535" (this
  1212. * is an exception to the shorter-representation-wins rule).
  1213. */
  1214. char *
  1215. policy_summarize(smartlist_t *policy, sa_family_t family)
  1216. {
  1217. smartlist_t *summary = policy_summary_create();
  1218. smartlist_t *accepts, *rejects;
  1219. int i, last, start_prt;
  1220. size_t accepts_len, rejects_len;
  1221. char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
  1222. const char *prefix;
  1223. tor_assert(policy);
  1224. /* Create the summary list */
  1225. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  1226. sa_family_t f = tor_addr_family(&p->addr);
  1227. if (f != AF_INET && f != AF_INET6) {
  1228. log_warn(LD_BUG, "Weird family when summarizing address policy");
  1229. }
  1230. if (f != family)
  1231. continue;
  1232. /* XXXX-ipv6 More family work is needed */
  1233. policy_summary_add_item(summary, p);
  1234. } SMARTLIST_FOREACH_END(p);
  1235. /* Now create two lists of strings, one for accepted and one
  1236. * for rejected ports. We take care to merge ranges so that
  1237. * we avoid getting stuff like "1-4,5-9,10", instead we want
  1238. * "1-10"
  1239. */
  1240. i = 0;
  1241. start_prt = 1;
  1242. accepts = smartlist_new();
  1243. rejects = smartlist_new();
  1244. while (1) {
  1245. last = i == smartlist_len(summary)-1;
  1246. if (last ||
  1247. AT(i)->accepted != AT(i+1)->accepted) {
  1248. char buf[POLICY_BUF_LEN];
  1249. if (start_prt == AT(i)->prt_max)
  1250. tor_snprintf(buf, sizeof(buf), "%d", start_prt);
  1251. else
  1252. tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
  1253. if (AT(i)->accepted)
  1254. smartlist_add(accepts, tor_strdup(buf));
  1255. else
  1256. smartlist_add(rejects, tor_strdup(buf));
  1257. if (last)
  1258. break;
  1259. start_prt = AT(i+1)->prt_min;
  1260. };
  1261. i++;
  1262. };
  1263. /* Figure out which of the two stringlists will be shorter and use
  1264. * that to build the result
  1265. */
  1266. if (smartlist_len(accepts) == 0) { /* no exits at all */
  1267. result = tor_strdup("reject 1-65535");
  1268. goto cleanup;
  1269. }
  1270. if (smartlist_len(rejects) == 0) { /* no rejects at all */
  1271. result = tor_strdup("accept 1-65535");
  1272. goto cleanup;
  1273. }
  1274. accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
  1275. rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
  1276. if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
  1277. accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
  1278. char *c;
  1279. shorter_str = accepts_str;
  1280. prefix = "accept";
  1281. c = shorter_str + (MAX_EXITPOLICY_SUMMARY_LEN-strlen(prefix)-1);
  1282. while (*c != ',' && c >= shorter_str)
  1283. c--;
  1284. tor_assert(c >= shorter_str);
  1285. tor_assert(*c == ',');
  1286. *c = '\0';
  1287. } else if (rejects_len < accepts_len) {
  1288. shorter_str = rejects_str;
  1289. prefix = "reject";
  1290. } else {
  1291. shorter_str = accepts_str;
  1292. prefix = "accept";
  1293. }
  1294. tor_asprintf(&result, "%s %s", prefix, shorter_str);
  1295. cleanup:
  1296. /* cleanup */
  1297. SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
  1298. smartlist_free(summary);
  1299. tor_free(accepts_str);
  1300. SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
  1301. smartlist_free(accepts);
  1302. tor_free(rejects_str);
  1303. SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
  1304. smartlist_free(rejects);
  1305. return result;
  1306. }
  1307. /** Convert a summarized policy string into a short_policy_t. Return NULL
  1308. * if the string is not well-formed. */
  1309. short_policy_t *
  1310. parse_short_policy(const char *summary)
  1311. {
  1312. const char *orig_summary = summary;
  1313. short_policy_t *result;
  1314. int is_accept;
  1315. int n_entries;
  1316. short_policy_entry_t entries[MAX_EXITPOLICY_SUMMARY_LEN]; /* overkill */
  1317. const char *next;
  1318. if (!strcmpstart(summary, "accept ")) {
  1319. is_accept = 1;
  1320. summary += strlen("accept ");
  1321. } else if (!strcmpstart(summary, "reject ")) {
  1322. is_accept = 0;
  1323. summary += strlen("reject ");
  1324. } else {
  1325. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Unrecognized policy summary keyword");
  1326. return NULL;
  1327. }
  1328. n_entries = 0;
  1329. for ( ; *summary; summary = next) {
  1330. const char *comma = strchr(summary, ',');
  1331. unsigned low, high;
  1332. char dummy;
  1333. char ent_buf[32];
  1334. size_t len;
  1335. next = comma ? comma+1 : strchr(summary, '\0');
  1336. len = comma ? (size_t)(comma - summary) : strlen(summary);
  1337. if (n_entries == MAX_EXITPOLICY_SUMMARY_LEN) {
  1338. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Impossibly long policy summary %s",
  1339. escaped(orig_summary));
  1340. return NULL;
  1341. }
  1342. if (! TOR_ISDIGIT(*summary) || len > (sizeof(ent_buf)-1)) {
  1343. /* unrecognized entry format. skip it. */
  1344. continue;
  1345. }
  1346. if (len < 1) {
  1347. /* empty; skip it. */
  1348. /* XXX This happens to be unreachable, since if len==0, then *summary is
  1349. * ',' or '\0', and the TOR_ISDIGIT test above would have failed. */
  1350. continue;
  1351. }
  1352. memcpy(ent_buf, summary, len);
  1353. ent_buf[len] = '\0';
  1354. if (tor_sscanf(ent_buf, "%u-%u%c", &low, &high, &dummy) == 2) {
  1355. if (low<1 || low>65535 || high<1 || high>65535 || low>high) {
  1356. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1357. "Found bad entry in policy summary %s", escaped(orig_summary));
  1358. return NULL;
  1359. }
  1360. } else if (tor_sscanf(ent_buf, "%u%c", &low, &dummy) == 1) {
  1361. if (low<1 || low>65535) {
  1362. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1363. "Found bad entry in policy summary %s", escaped(orig_summary));
  1364. return NULL;
  1365. }
  1366. high = low;
  1367. } else {
  1368. log_fn(LOG_PROTOCOL_WARN, LD_DIR,"Found bad entry in policy summary %s",
  1369. escaped(orig_summary));
  1370. return NULL;
  1371. }
  1372. entries[n_entries].min_port = low;
  1373. entries[n_entries].max_port = high;
  1374. n_entries++;
  1375. }
  1376. if (n_entries == 0) {
  1377. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1378. "Found no port-range entries in summary %s", escaped(orig_summary));
  1379. return NULL;
  1380. }
  1381. {
  1382. size_t size = STRUCT_OFFSET(short_policy_t, entries) +
  1383. sizeof(short_policy_entry_t)*(n_entries);
  1384. result = tor_malloc_zero(size);
  1385. tor_assert( (char*)&result->entries[n_entries-1] < ((char*)result)+size);
  1386. }
  1387. result->is_accept = is_accept;
  1388. result->n_entries = n_entries;
  1389. memcpy(result->entries, entries, sizeof(short_policy_entry_t)*n_entries);
  1390. return result;
  1391. }
  1392. /** Write <b>policy</b> back out into a string. Used only for unit tests
  1393. * currently. */
  1394. char *
  1395. write_short_policy(const short_policy_t *policy)
  1396. {
  1397. int i;
  1398. char *answer;
  1399. smartlist_t *sl = smartlist_new();
  1400. smartlist_add_asprintf(sl, "%s", policy->is_accept ? "accept " : "reject ");
  1401. for (i=0; i < policy->n_entries; i++) {
  1402. const short_policy_entry_t *e = &policy->entries[i];
  1403. if (e->min_port == e->max_port) {
  1404. smartlist_add_asprintf(sl, "%d", e->min_port);
  1405. } else {
  1406. smartlist_add_asprintf(sl, "%d-%d", e->min_port, e->max_port);
  1407. }
  1408. if (i < policy->n_entries-1)
  1409. smartlist_add(sl, tor_strdup(","));
  1410. }
  1411. answer = smartlist_join_strings(sl, "", 0, NULL);
  1412. SMARTLIST_FOREACH(sl, char *, a, tor_free(a));
  1413. smartlist_free(sl);
  1414. return answer;
  1415. }
  1416. /** Release all storage held in <b>policy</b>. */
  1417. void
  1418. short_policy_free(short_policy_t *policy)
  1419. {
  1420. tor_free(policy);
  1421. }
  1422. /** See whether the <b>addr</b>:<b>port</b> address is likely to be accepted
  1423. * or rejected by the summarized policy <b>policy</b>. Return values are as
  1424. * for compare_tor_addr_to_addr_policy. Unlike the regular addr_policy
  1425. * functions, requires the <b>port</b> be specified. */
  1426. addr_policy_result_t
  1427. compare_tor_addr_to_short_policy(const tor_addr_t *addr, uint16_t port,
  1428. const short_policy_t *policy)
  1429. {
  1430. int i;
  1431. int found_match = 0;
  1432. int accept;
  1433. tor_assert(port != 0);
  1434. if (addr && tor_addr_is_null(addr))
  1435. addr = NULL; /* Unspec means 'no address at all,' in this context. */
  1436. if (addr && get_options()->ClientRejectInternalAddresses &&
  1437. (tor_addr_is_internal(addr, 0) || tor_addr_is_loopback(addr)))
  1438. return ADDR_POLICY_REJECTED;
  1439. for (i=0; i < policy->n_entries; ++i) {
  1440. const short_policy_entry_t *e = &policy->entries[i];
  1441. if (e->min_port <= port && port <= e->max_port) {
  1442. found_match = 1;
  1443. break;
  1444. }
  1445. }
  1446. if (found_match)
  1447. accept = policy->is_accept;
  1448. else
  1449. accept = ! policy->is_accept;
  1450. /* ???? are these right? -NM */
  1451. /* We should be sure not to return ADDR_POLICY_ACCEPTED in the accept
  1452. * case here, because it would cause clients to believe that the node
  1453. * allows exit enclaving. Trying it anyway would open up a cool attack
  1454. * where the node refuses due to exitpolicy, the client reacts in
  1455. * surprise by rewriting the node's exitpolicy to reject *:*, and then
  1456. * a bad guy targets users by causing them to attempt such connections
  1457. * to 98% of the exits.
  1458. *
  1459. * Once microdescriptors can handle addresses in special cases (e.g. if
  1460. * we ever solve ticket 1774), we can provide certainty here. -RD */
  1461. if (accept)
  1462. return ADDR_POLICY_PROBABLY_ACCEPTED;
  1463. else
  1464. return ADDR_POLICY_REJECTED;
  1465. }
  1466. /** Return true iff <b>policy</b> seems reject all ports */
  1467. int
  1468. short_policy_is_reject_star(const short_policy_t *policy)
  1469. {
  1470. /* This doesn't need to be as much on the lookout as policy_is_reject_star,
  1471. * since policy summaries are from the consensus or from consensus
  1472. * microdescs.
  1473. */
  1474. tor_assert(policy);
  1475. /* Check for an exact match of "reject 1-65535". */
  1476. return (policy->is_accept == 0 && policy->n_entries == 1 &&
  1477. policy->entries[0].min_port == 1 &&
  1478. policy->entries[0].max_port == 65535);
  1479. }
  1480. /** Decide whether addr:port is probably or definitely accepted or rejected by
  1481. * <b>node</b>. See compare_tor_addr_to_addr_policy for details on addr/port
  1482. * interpretation. */
  1483. addr_policy_result_t
  1484. compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port,
  1485. const node_t *node)
  1486. {
  1487. if (node->rejects_all)
  1488. return ADDR_POLICY_REJECTED;
  1489. if (addr && tor_addr_family(addr) == AF_INET6) {
  1490. const short_policy_t *p = NULL;
  1491. if (node->ri)
  1492. p = node->ri->ipv6_exit_policy;
  1493. else if (node->md)
  1494. p = node->md->ipv6_exit_policy;
  1495. if (p)
  1496. return compare_tor_addr_to_short_policy(addr, port, p);
  1497. else
  1498. return ADDR_POLICY_REJECTED;
  1499. }
  1500. if (node->ri) {
  1501. return compare_tor_addr_to_addr_policy(addr, port, node->ri->exit_policy);
  1502. } else if (node->md) {
  1503. if (node->md->exit_policy == NULL)
  1504. return ADDR_POLICY_REJECTED;
  1505. else
  1506. return compare_tor_addr_to_short_policy(addr, port,
  1507. node->md->exit_policy);
  1508. } else {
  1509. return ADDR_POLICY_PROBABLY_REJECTED;
  1510. }
  1511. }
  1512. /** Implementation for GETINFO control command: knows the answer for questions
  1513. * about "exit-policy/..." */
  1514. int
  1515. getinfo_helper_policies(control_connection_t *conn,
  1516. const char *question, char **answer,
  1517. const char **errmsg)
  1518. {
  1519. (void) conn;
  1520. (void) errmsg;
  1521. if (!strcmp(question, "exit-policy/default")) {
  1522. *answer = tor_strdup(DEFAULT_EXIT_POLICY);
  1523. }
  1524. return 0;
  1525. }
  1526. /** Release all storage held by <b>p</b>. */
  1527. void
  1528. addr_policy_list_free(smartlist_t *lst)
  1529. {
  1530. if (!lst)
  1531. return;
  1532. SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
  1533. smartlist_free(lst);
  1534. }
  1535. /** Release all storage held by <b>p</b>. */
  1536. void
  1537. addr_policy_free(addr_policy_t *p)
  1538. {
  1539. if (!p)
  1540. return;
  1541. if (--p->refcnt <= 0) {
  1542. if (p->is_canonical) {
  1543. policy_map_ent_t search, *found;
  1544. search.policy = p;
  1545. found = HT_REMOVE(policy_map, &policy_root, &search);
  1546. if (found) {
  1547. tor_assert(p == found->policy);
  1548. tor_free(found);
  1549. }
  1550. }
  1551. tor_free(p);
  1552. }
  1553. }
  1554. /** Release all storage held by policy variables. */
  1555. void
  1556. policies_free_all(void)
  1557. {
  1558. addr_policy_list_free(reachable_or_addr_policy);
  1559. reachable_or_addr_policy = NULL;
  1560. addr_policy_list_free(reachable_dir_addr_policy);
  1561. reachable_dir_addr_policy = NULL;
  1562. addr_policy_list_free(socks_policy);
  1563. socks_policy = NULL;
  1564. addr_policy_list_free(dir_policy);
  1565. dir_policy = NULL;
  1566. addr_policy_list_free(authdir_reject_policy);
  1567. authdir_reject_policy = NULL;
  1568. addr_policy_list_free(authdir_invalid_policy);
  1569. authdir_invalid_policy = NULL;
  1570. addr_policy_list_free(authdir_baddir_policy);
  1571. authdir_baddir_policy = NULL;
  1572. addr_policy_list_free(authdir_badexit_policy);
  1573. authdir_badexit_policy = NULL;
  1574. if (!HT_EMPTY(&policy_root)) {
  1575. policy_map_ent_t **ent;
  1576. int n = 0;
  1577. char buf[POLICY_BUF_LEN];
  1578. log_warn(LD_MM, "Still had %d address policies cached at shutdown.",
  1579. (int)HT_SIZE(&policy_root));
  1580. /* Note the first 10 cached policies to try to figure out where they
  1581. * might be coming from. */
  1582. HT_FOREACH(ent, policy_map, &policy_root) {
  1583. if (++n > 10)
  1584. break;
  1585. if (policy_write_item(buf, sizeof(buf), (*ent)->policy, 0) >= 0)
  1586. log_warn(LD_MM," %d [%d]: %s", n, (*ent)->policy->refcnt, buf);
  1587. }
  1588. }
  1589. HT_CLEAR(policy_map, &policy_root);
  1590. }