policies.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, 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_string_isin_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->ExitPolicyRejectPrivate, NULL,
  399. !options->BridgeRelay))
  400. REJECT("Error in ExitPolicy entry.");
  401. /* The rest of these calls *append* to addr_policy. So don't actually
  402. * use the results for anything other than checking if they parse! */
  403. if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
  404. REJECT("Error in DirPolicy entry.");
  405. if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
  406. REJECT("Error in SocksPolicy entry.");
  407. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  408. ADDR_POLICY_REJECT))
  409. REJECT("Error in AuthDirReject entry.");
  410. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  411. ADDR_POLICY_REJECT))
  412. REJECT("Error in AuthDirInvalid entry.");
  413. if (parse_addr_policy(options->AuthDirBadDir, &addr_policy,
  414. ADDR_POLICY_REJECT))
  415. REJECT("Error in AuthDirBadDir entry.");
  416. if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
  417. ADDR_POLICY_REJECT))
  418. REJECT("Error in AuthDirBadExit entry.");
  419. if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
  420. ADDR_POLICY_ACCEPT))
  421. REJECT("Error in ReachableAddresses entry.");
  422. if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
  423. ADDR_POLICY_ACCEPT))
  424. REJECT("Error in ReachableORAddresses entry.");
  425. if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
  426. ADDR_POLICY_ACCEPT))
  427. REJECT("Error in ReachableDirAddresses entry.");
  428. err:
  429. addr_policy_list_free(addr_policy);
  430. return *msg ? -1 : 0;
  431. #undef REJECT
  432. }
  433. /** Parse <b>string</b> in the same way that the exit policy
  434. * is parsed, and put the processed version in *<b>policy</b>.
  435. * Ignore port specifiers.
  436. */
  437. static int
  438. load_policy_from_option(config_line_t *config, smartlist_t **policy,
  439. int assume_action)
  440. {
  441. int r;
  442. addr_policy_list_free(*policy);
  443. *policy = NULL;
  444. r = parse_addr_policy(config, policy, assume_action);
  445. if (r < 0) {
  446. return -1;
  447. }
  448. if (*policy) {
  449. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, n) {
  450. /* ports aren't used in these. */
  451. if (n->prt_min > 1 || n->prt_max != 65535) {
  452. addr_policy_t newp, *c;
  453. memcpy(&newp, n, sizeof(newp));
  454. newp.prt_min = 1;
  455. newp.prt_max = 65535;
  456. newp.is_canonical = 0;
  457. c = addr_policy_get_canonical_entry(&newp);
  458. SMARTLIST_REPLACE_CURRENT(*policy, n, c);
  459. addr_policy_free(n);
  460. }
  461. } SMARTLIST_FOREACH_END(n);
  462. }
  463. return 0;
  464. }
  465. /** Set all policies based on <b>options</b>, which should have been validated
  466. * first by validate_addr_policies. */
  467. int
  468. policies_parse_from_options(const or_options_t *options)
  469. {
  470. int ret = 0;
  471. if (load_policy_from_option(options->SocksPolicy, &socks_policy, -1) < 0)
  472. ret = -1;
  473. if (load_policy_from_option(options->DirPolicy, &dir_policy, -1) < 0)
  474. ret = -1;
  475. if (load_policy_from_option(options->AuthDirReject,
  476. &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
  477. ret = -1;
  478. if (load_policy_from_option(options->AuthDirInvalid,
  479. &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
  480. ret = -1;
  481. if (load_policy_from_option(options->AuthDirBadDir,
  482. &authdir_baddir_policy, ADDR_POLICY_REJECT) < 0)
  483. ret = -1;
  484. if (load_policy_from_option(options->AuthDirBadExit,
  485. &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
  486. ret = -1;
  487. if (parse_reachable_addresses() < 0)
  488. ret = -1;
  489. return ret;
  490. }
  491. /** Compare two provided address policy items, and return -1, 0, or 1
  492. * if the first is less than, equal to, or greater than the second. */
  493. static int
  494. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  495. {
  496. int r;
  497. if ((r=((int)a->policy_type - (int)b->policy_type)))
  498. return r;
  499. if ((r=((int)a->is_private - (int)b->is_private)))
  500. return r;
  501. if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
  502. return r;
  503. if ((r=((int)a->maskbits - (int)b->maskbits)))
  504. return r;
  505. if ((r=((int)a->prt_min - (int)b->prt_min)))
  506. return r;
  507. if ((r=((int)a->prt_max - (int)b->prt_max)))
  508. return r;
  509. return 0;
  510. }
  511. /** Like cmp_single_addr_policy() above, but looks at the
  512. * whole set of policies in each case. */
  513. int
  514. cmp_addr_policies(smartlist_t *a, smartlist_t *b)
  515. {
  516. int r, i;
  517. int len_a = a ? smartlist_len(a) : 0;
  518. int len_b = b ? smartlist_len(b) : 0;
  519. for (i = 0; i < len_a && i < len_b; ++i) {
  520. if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
  521. return r;
  522. }
  523. if (i == len_a && i == len_b)
  524. return 0;
  525. if (i < len_a)
  526. return -1;
  527. else
  528. return 1;
  529. }
  530. /** Node in hashtable used to store address policy entries. */
  531. typedef struct policy_map_ent_t {
  532. HT_ENTRY(policy_map_ent_t) node;
  533. addr_policy_t *policy;
  534. } policy_map_ent_t;
  535. /* DOCDOC policy_root */
  536. static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
  537. /** Return true iff a and b are equal. */
  538. static INLINE int
  539. policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
  540. {
  541. return cmp_single_addr_policy(a->policy, b->policy) == 0;
  542. }
  543. /** Return a hashcode for <b>ent</b> */
  544. static unsigned int
  545. policy_hash(policy_map_ent_t *ent)
  546. {
  547. addr_policy_t *a = ent->policy;
  548. unsigned int r;
  549. if (a->is_private)
  550. r = 0x1234abcd;
  551. else
  552. r = tor_addr_hash(&a->addr);
  553. r += a->prt_min << 8;
  554. r += a->prt_max << 16;
  555. r += a->maskbits;
  556. if (a->policy_type == ADDR_POLICY_REJECT)
  557. r ^= 0xffffffff;
  558. return r;
  559. }
  560. HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
  561. policy_eq)
  562. HT_GENERATE(policy_map, policy_map_ent_t, node, policy_hash,
  563. policy_eq, 0.6, malloc, realloc, free)
  564. /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
  565. * "canonical" copy of that addr_policy_t; the canonical copy is a single
  566. * reference-counted object. */
  567. addr_policy_t *
  568. addr_policy_get_canonical_entry(addr_policy_t *e)
  569. {
  570. policy_map_ent_t search, *found;
  571. if (e->is_canonical)
  572. return e;
  573. search.policy = e;
  574. found = HT_FIND(policy_map, &policy_root, &search);
  575. if (!found) {
  576. found = tor_malloc_zero(sizeof(policy_map_ent_t));
  577. found->policy = tor_memdup(e, sizeof(addr_policy_t));
  578. found->policy->is_canonical = 1;
  579. found->policy->refcnt = 0;
  580. HT_INSERT(policy_map, &policy_root, found);
  581. }
  582. tor_assert(!cmp_single_addr_policy(found->policy, e));
  583. ++found->policy->refcnt;
  584. return found->policy;
  585. }
  586. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  587. * addr and port are both known. */
  588. static addr_policy_result_t
  589. compare_known_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  590. const smartlist_t *policy)
  591. {
  592. /* We know the address and port, and we know the policy, so we can just
  593. * compute an exact match. */
  594. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  595. /* Address is known */
  596. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  597. CMP_EXACT)) {
  598. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  599. /* Exact match for the policy */
  600. return tmpe->policy_type == ADDR_POLICY_ACCEPT ?
  601. ADDR_POLICY_ACCEPTED : ADDR_POLICY_REJECTED;
  602. }
  603. }
  604. } SMARTLIST_FOREACH_END(tmpe);
  605. /* accept all by default. */
  606. return ADDR_POLICY_ACCEPTED;
  607. }
  608. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  609. * addr is known but port is not. */
  610. static addr_policy_result_t
  611. compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t *addr,
  612. const smartlist_t *policy)
  613. {
  614. /* We look to see if there's a definite match. If so, we return that
  615. match's value, unless there's an intervening possible match that says
  616. something different. */
  617. int maybe_accept = 0, maybe_reject = 0;
  618. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  619. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  620. CMP_EXACT)) {
  621. if (tmpe->prt_min <= 1 && tmpe->prt_max >= 65535) {
  622. /* Definitely matches, since it covers all ports. */
  623. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  624. /* If we already hit a clause that might trigger a 'reject', than we
  625. * can't be sure of this certain 'accept'.*/
  626. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  627. ADDR_POLICY_ACCEPTED;
  628. } else {
  629. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  630. ADDR_POLICY_REJECTED;
  631. }
  632. } else {
  633. /* Might match. */
  634. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  635. maybe_reject = 1;
  636. else
  637. maybe_accept = 1;
  638. }
  639. }
  640. } SMARTLIST_FOREACH_END(tmpe);
  641. /* accept all by default. */
  642. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  643. }
  644. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  645. * port is known but address is not. */
  646. static addr_policy_result_t
  647. compare_unknown_tor_addr_to_addr_policy(uint16_t port,
  648. const smartlist_t *policy)
  649. {
  650. /* We look to see if there's a definite match. If so, we return that
  651. match's value, unless there's an intervening possible match that says
  652. something different. */
  653. int maybe_accept = 0, maybe_reject = 0;
  654. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  655. if (tmpe->prt_min <= port && port <= tmpe->prt_max) {
  656. if (tmpe->maskbits == 0) {
  657. /* Definitely matches, since it covers all addresses. */
  658. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  659. /* If we already hit a clause that might trigger a 'reject', than we
  660. * can't be sure of this certain 'accept'.*/
  661. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  662. ADDR_POLICY_ACCEPTED;
  663. } else {
  664. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  665. ADDR_POLICY_REJECTED;
  666. }
  667. } else {
  668. /* Might match. */
  669. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  670. maybe_reject = 1;
  671. else
  672. maybe_accept = 1;
  673. }
  674. }
  675. } SMARTLIST_FOREACH_END(tmpe);
  676. /* accept all by default. */
  677. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  678. }
  679. /** Decide whether a given addr:port is definitely accepted,
  680. * definitely rejected, probably accepted, or probably rejected by a
  681. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  682. * target address. If <b>port</b> is 0, we don't know the port of the
  683. * target address. (At least one of <b>addr</b> and <b>port</b> must be
  684. * provided. If you want to know whether a policy would definitely reject
  685. * an unknown address:port, use policy_is_reject_star().)
  686. *
  687. * We could do better by assuming that some ranges never match typical
  688. * addresses (127.0.0.1, and so on). But we'll try this for now.
  689. */
  690. addr_policy_result_t
  691. compare_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  692. const smartlist_t *policy)
  693. {
  694. if (!policy) {
  695. /* no policy? accept all. */
  696. return ADDR_POLICY_ACCEPTED;
  697. } else if (addr == NULL || tor_addr_is_null(addr)) {
  698. if (port == 0) {
  699. log_info(LD_BUG, "Rejecting null address with 0 port (family %d)",
  700. addr ? tor_addr_family(addr) : -1);
  701. return ADDR_POLICY_REJECTED;
  702. }
  703. return compare_unknown_tor_addr_to_addr_policy(port, policy);
  704. } else if (port == 0) {
  705. return compare_known_tor_addr_to_addr_policy_noport(addr, policy);
  706. } else {
  707. return compare_known_tor_addr_to_addr_policy(addr, port, policy);
  708. }
  709. }
  710. /** Return true iff the address policy <b>a</b> covers every case that
  711. * would be covered by <b>b</b>, so that a,b is redundant. */
  712. static int
  713. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  714. {
  715. if (tor_addr_family(&a->addr) != tor_addr_family(&b->addr)) {
  716. /* You can't cover a different family. */
  717. return 0;
  718. }
  719. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  720. * to "accept *:80". */
  721. if (a->maskbits > b->maskbits) {
  722. /* a has more fixed bits than b; it can't possibly cover b. */
  723. return 0;
  724. }
  725. if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_EXACT)) {
  726. /* There's a fixed bit in a that's set differently in b. */
  727. return 0;
  728. }
  729. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  730. }
  731. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  732. * that is, there exists an address/port that is covered by <b>a</b> that
  733. * is also covered by <b>b</b>.
  734. */
  735. static int
  736. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  737. {
  738. maskbits_t minbits;
  739. /* All the bits we care about are those that are set in both
  740. * netmasks. If they are equal in a and b's networkaddresses
  741. * then the networks intersect. If there is a difference,
  742. * then they do not. */
  743. if (a->maskbits < b->maskbits)
  744. minbits = a->maskbits;
  745. else
  746. minbits = b->maskbits;
  747. if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_EXACT))
  748. return 0;
  749. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  750. return 0;
  751. return 1;
  752. }
  753. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  754. */
  755. static void
  756. append_exit_policy_string(smartlist_t **policy, const char *more)
  757. {
  758. config_line_t tmp;
  759. tmp.key = NULL;
  760. tmp.value = (char*) more;
  761. tmp.next = NULL;
  762. if (parse_addr_policy(&tmp, policy, -1)<0) {
  763. log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
  764. }
  765. }
  766. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  767. static void
  768. exit_policy_remove_redundancies(smartlist_t *dest)
  769. {
  770. addr_policy_t *ap, *tmp;
  771. int i, j;
  772. /* Step one: kill every ipv4 thing after *4:*, every IPv6 thing after *6:*
  773. */
  774. {
  775. int kill_v4=0, kill_v6=0;
  776. for (i = 0; i < smartlist_len(dest); ++i) {
  777. sa_family_t family;
  778. ap = smartlist_get(dest, i);
  779. family = tor_addr_family(&ap->addr);
  780. if ((family == AF_INET && kill_v4) ||
  781. (family == AF_INET6 && kill_v6)) {
  782. smartlist_del_keeporder(dest, i--);
  783. addr_policy_free(ap);
  784. continue;
  785. }
  786. if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  787. /* This is a catch-all line -- later lines are unreachable. */
  788. if (family == AF_INET) {
  789. kill_v4 = 1;
  790. } else if (family == AF_INET6) {
  791. kill_v6 = 1;
  792. }
  793. }
  794. }
  795. }
  796. /* Step two: for every entry, see if there's a redundant entry
  797. * later on, and remove it. */
  798. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  799. ap = smartlist_get(dest, i);
  800. for (j = i+1; j < smartlist_len(dest); ++j) {
  801. tmp = smartlist_get(dest, j);
  802. tor_assert(j > i);
  803. if (addr_policy_covers(ap, tmp)) {
  804. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  805. policy_write_item(p1, sizeof(p1), tmp, 0);
  806. policy_write_item(p2, sizeof(p2), ap, 0);
  807. log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s (%d). It is made "
  808. "redundant by %s (%d).", p1, j, p2, i);
  809. smartlist_del_keeporder(dest, j--);
  810. addr_policy_free(tmp);
  811. }
  812. }
  813. }
  814. /* Step three: for every entry A, see if there's an entry B making this one
  815. * redundant later on. This is the case if A and B are of the same type
  816. * (accept/reject), A is a subset of B, and there is no other entry of
  817. * different type in between those two that intersects with A.
  818. *
  819. * Anybody want to double-check the logic here? XXX
  820. */
  821. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  822. ap = smartlist_get(dest, i);
  823. for (j = i+1; j < smartlist_len(dest); ++j) {
  824. // tor_assert(j > i); // j starts out at i+1; j only increases; i only
  825. // // decreases.
  826. tmp = smartlist_get(dest, j);
  827. if (ap->policy_type != tmp->policy_type) {
  828. if (addr_policy_intersects(ap, tmp))
  829. break;
  830. } else { /* policy_types are equal. */
  831. if (addr_policy_covers(tmp, ap)) {
  832. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  833. policy_write_item(p1, sizeof(p1), ap, 0);
  834. policy_write_item(p2, sizeof(p2), tmp, 0);
  835. log(LOG_DEBUG, LD_CONFIG, "Removing exit policy %s. It is already "
  836. "covered by %s.", p1, p2);
  837. smartlist_del_keeporder(dest, i--);
  838. addr_policy_free(ap);
  839. break;
  840. }
  841. }
  842. }
  843. }
  844. }
  845. #define DEFAULT_EXIT_POLICY \
  846. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  847. "reject *:563,reject *:1214,reject *:4661-4666," \
  848. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  849. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>. If
  850. * cfg doesn't end in an absolute accept or reject and if
  851. * <b>add_default_policy</b> is true, add the default exit
  852. * policy afterwards. If <b>rejectprivate</b> is true, prepend
  853. * "reject private:*" to the policy. Return -1 if we can't parse cfg,
  854. * else return 0.
  855. *
  856. * This function is used to parse the exit policy from our torrc. For
  857. * the functions used to parse the exit policy from a router descriptor,
  858. * see router_add_exit_policy.
  859. */
  860. int
  861. policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
  862. int rejectprivate, const char *local_address,
  863. int add_default_policy)
  864. {
  865. if (rejectprivate) {
  866. append_exit_policy_string(dest, "reject private:*");
  867. if (local_address) {
  868. char buf[POLICY_BUF_LEN];
  869. tor_snprintf(buf, sizeof(buf), "reject %s:*", local_address);
  870. append_exit_policy_string(dest, buf);
  871. }
  872. }
  873. if (parse_addr_policy(cfg, dest, -1))
  874. return -1;
  875. if (add_default_policy) {
  876. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  877. } else {
  878. append_exit_policy_string(dest, "reject *4:*");
  879. append_exit_policy_string(dest, "reject *6:*");
  880. }
  881. exit_policy_remove_redundancies(*dest);
  882. return 0;
  883. }
  884. /** Add "reject *:*" to the end of the policy in *<b>dest</b>, allocating
  885. * *<b>dest</b> as needed. */
  886. void
  887. policies_exit_policy_append_reject_star(smartlist_t **dest)
  888. {
  889. append_exit_policy_string(dest, "reject *4:*");
  890. append_exit_policy_string(dest, "reject *6:*");
  891. }
  892. /** Replace the exit policy of <b>node</b> with reject *:* */
  893. void
  894. policies_set_node_exitpolicy_to_reject_all(node_t *node)
  895. {
  896. node->rejects_all = 1;
  897. }
  898. /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
  899. * allows exiting to <b>port</b>. Otherwise, return 0. */
  900. static int
  901. exit_policy_is_general_exit_helper(smartlist_t *policy, int port)
  902. {
  903. uint32_t mask, ip, i;
  904. /* Is this /8 rejected (1), or undecided (0)? */
  905. char subnet_status[256];
  906. memset(subnet_status, 0, sizeof(subnet_status));
  907. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  908. if (tor_addr_family(&p->addr) != AF_INET)
  909. continue; /* IPv4 only for now */
  910. if (p->prt_min > port || p->prt_max < port)
  911. continue; /* Doesn't cover our port. */
  912. mask = 0;
  913. tor_assert(p->maskbits <= 32);
  914. if (p->maskbits)
  915. mask = UINT32_MAX<<(32-p->maskbits);
  916. ip = tor_addr_to_ipv4h(&p->addr);
  917. /* Calculate the first and last subnet that this exit policy touches
  918. * and set it as loop boundaries. */
  919. for (i = ((mask & ip)>>24); i <= (~((mask & ip) ^ mask)>>24); ++i) {
  920. tor_addr_t addr;
  921. if (subnet_status[i] != 0)
  922. continue; /* We already reject some part of this /8 */
  923. tor_addr_from_ipv4h(&addr, i<<24);
  924. if (tor_addr_is_internal(&addr, 0))
  925. continue; /* Local or non-routable addresses */
  926. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  927. if (p->maskbits > 8)
  928. continue; /* Narrower than a /8. */
  929. /* We found an allowed subnet of at least size /8. Done
  930. * for this port! */
  931. return 1;
  932. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  933. subnet_status[i] = 1;
  934. }
  935. }
  936. } SMARTLIST_FOREACH_END(p);
  937. return 0;
  938. }
  939. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  940. * it allows exit to at least one /8 address space for at least
  941. * two of ports 80, 443, and 6667. */
  942. int
  943. exit_policy_is_general_exit(smartlist_t *policy)
  944. {
  945. static const int ports[] = { 80, 443, 6667 };
  946. int n_allowed = 0;
  947. int i;
  948. if (!policy) /*XXXX disallow NULL policies? */
  949. return 0;
  950. for (i = 0; i < 3; ++i) {
  951. n_allowed += exit_policy_is_general_exit_helper(policy, ports[i]);
  952. }
  953. return n_allowed >= 2;
  954. }
  955. /** Return false if <b>policy</b> might permit access to some addr:port;
  956. * otherwise if we are certain it rejects everything, return true. */
  957. int
  958. policy_is_reject_star(const smartlist_t *policy)
  959. {
  960. if (!policy) /*XXXX disallow NULL policies? */
  961. return 1;
  962. SMARTLIST_FOREACH(policy, addr_policy_t *, p, {
  963. if (p->policy_type == ADDR_POLICY_ACCEPT)
  964. return 0;
  965. else if (p->policy_type == ADDR_POLICY_REJECT &&
  966. p->prt_min <= 1 && p->prt_max == 65535 &&
  967. p->maskbits == 0)
  968. return 1;
  969. });
  970. return 1;
  971. }
  972. /** Write a single address policy to the buf_len byte buffer at buf. Return
  973. * the number of characters written, or -1 on failure. */
  974. int
  975. policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
  976. int format_for_desc)
  977. {
  978. size_t written = 0;
  979. char addrbuf[TOR_ADDR_BUF_LEN];
  980. const char *addrpart;
  981. int result;
  982. const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
  983. const sa_family_t family = tor_addr_family(&policy->addr);
  984. const int is_ip6 = (family == AF_INET6);
  985. tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
  986. /* write accept/reject 1.2.3.4 */
  987. if (policy->is_private) {
  988. addrpart = "private";
  989. } else if (policy->maskbits == 0) {
  990. if (format_for_desc)
  991. addrpart = "*";
  992. else if (family == AF_INET6)
  993. addrpart = "*6";
  994. else if (family == AF_INET)
  995. addrpart = "*4";
  996. else
  997. addrpart = "*";
  998. } else {
  999. addrpart = addrbuf;
  1000. }
  1001. result = tor_snprintf(buf, buflen, "%s%s %s",
  1002. is_accept ? "accept" : "reject",
  1003. (is_ip6&&format_for_desc)?"6":"",
  1004. addrpart);
  1005. if (result < 0)
  1006. return -1;
  1007. written += strlen(buf);
  1008. /* If the maskbits is 32 we don't need to give it. If the mask is 0,
  1009. * we already wrote "*". */
  1010. if (policy->maskbits < 32 && policy->maskbits > 0) {
  1011. if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
  1012. return -1;
  1013. written += strlen(buf+written);
  1014. }
  1015. if (policy->prt_min <= 1 && policy->prt_max == 65535) {
  1016. /* There is no port set; write ":*" */
  1017. if (written+4 > buflen)
  1018. return -1;
  1019. strlcat(buf+written, ":*", buflen-written);
  1020. written += 2;
  1021. } else if (policy->prt_min == policy->prt_max) {
  1022. /* There is only one port; write ":80". */
  1023. result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
  1024. if (result<0)
  1025. return -1;
  1026. written += result;
  1027. } else {
  1028. /* There is a range of ports; write ":79-80". */
  1029. result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
  1030. policy->prt_min, policy->prt_max);
  1031. if (result<0)
  1032. return -1;
  1033. written += result;
  1034. }
  1035. if (written < buflen)
  1036. buf[written] = '\0';
  1037. else
  1038. return -1;
  1039. return (int)written;
  1040. }
  1041. /** Create a new exit policy summary, initially only with a single
  1042. * port 1-64k item */
  1043. /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
  1044. * RB-tree if that turns out to matter. */
  1045. static smartlist_t *
  1046. policy_summary_create(void)
  1047. {
  1048. smartlist_t *summary;
  1049. policy_summary_item_t* item;
  1050. item = tor_malloc_zero(sizeof(policy_summary_item_t));
  1051. item->prt_min = 1;
  1052. item->prt_max = 65535;
  1053. item->reject_count = 0;
  1054. item->accepted = 0;
  1055. summary = smartlist_new();
  1056. smartlist_add(summary, item);
  1057. return summary;
  1058. }
  1059. /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
  1060. * The current item is changed to end at new-starts - 1, the new item
  1061. * copies reject_count and accepted from the old item,
  1062. * starts at new_starts and ends at the port where the original item
  1063. * previously ended.
  1064. */
  1065. static policy_summary_item_t*
  1066. policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts)
  1067. {
  1068. policy_summary_item_t* new;
  1069. new = tor_malloc_zero(sizeof(policy_summary_item_t));
  1070. new->prt_min = new_starts;
  1071. new->prt_max = old->prt_max;
  1072. new->reject_count = old->reject_count;
  1073. new->accepted = old->accepted;
  1074. old->prt_max = new_starts-1;
  1075. tor_assert(old->prt_min <= old->prt_max);
  1076. tor_assert(new->prt_min <= new->prt_max);
  1077. return new;
  1078. }
  1079. /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
  1080. * my immortal soul, he can clean it up himself. */
  1081. #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
  1082. #define REJECT_CUTOFF_COUNT (1<<25)
  1083. /** Split an exit policy summary so that prt_min and prt_max
  1084. * fall at exactly the start and end of an item respectively.
  1085. */
  1086. static int
  1087. policy_summary_split(smartlist_t *summary,
  1088. uint16_t prt_min, uint16_t prt_max)
  1089. {
  1090. int start_at_index;
  1091. int i = 0;
  1092. while (AT(i)->prt_max < prt_min)
  1093. i++;
  1094. if (AT(i)->prt_min != prt_min) {
  1095. policy_summary_item_t* new_item;
  1096. new_item = policy_summary_item_split(AT(i), prt_min);
  1097. smartlist_insert(summary, i+1, new_item);
  1098. i++;
  1099. }
  1100. start_at_index = i;
  1101. while (AT(i)->prt_max < prt_max)
  1102. i++;
  1103. if (AT(i)->prt_max != prt_max) {
  1104. policy_summary_item_t* new_item;
  1105. new_item = policy_summary_item_split(AT(i), prt_max+1);
  1106. smartlist_insert(summary, i+1, new_item);
  1107. }
  1108. return start_at_index;
  1109. }
  1110. /** Mark port ranges as accepted if they are below the reject_count */
  1111. static void
  1112. policy_summary_accept(smartlist_t *summary,
  1113. uint16_t prt_min, uint16_t prt_max)
  1114. {
  1115. int i = policy_summary_split(summary, prt_min, prt_max);
  1116. while (i < smartlist_len(summary) &&
  1117. AT(i)->prt_max <= prt_max) {
  1118. if (!AT(i)->accepted &&
  1119. AT(i)->reject_count <= REJECT_CUTOFF_COUNT)
  1120. AT(i)->accepted = 1;
  1121. i++;
  1122. }
  1123. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1124. }
  1125. /** Count the number of addresses in a network with prefixlen maskbits
  1126. * against the given portrange. */
  1127. static void
  1128. policy_summary_reject(smartlist_t *summary,
  1129. maskbits_t maskbits,
  1130. uint16_t prt_min, uint16_t prt_max)
  1131. {
  1132. int i = policy_summary_split(summary, prt_min, prt_max);
  1133. /* XXX: ipv4 specific */
  1134. uint64_t count = (U64_LITERAL(1) << (32-maskbits));
  1135. while (i < smartlist_len(summary) &&
  1136. AT(i)->prt_max <= prt_max) {
  1137. AT(i)->reject_count += count;
  1138. i++;
  1139. }
  1140. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1141. }
  1142. /** Add a single exit policy item to our summary:
  1143. * If it is an accept ignore it unless it is for all IP addresses
  1144. * ("*"), i.e. it's prefixlen/maskbits is 0, else call
  1145. * policy_summary_accept().
  1146. * If it's a reject ignore it if it is about one of the private
  1147. * networks, else call policy_summary_reject().
  1148. */
  1149. static void
  1150. policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
  1151. {
  1152. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  1153. if (p->maskbits == 0) {
  1154. policy_summary_accept(summary, p->prt_min, p->prt_max);
  1155. }
  1156. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  1157. int is_private = 0;
  1158. int i;
  1159. for (i = 0; private_nets[i]; ++i) {
  1160. tor_addr_t addr;
  1161. maskbits_t maskbits;
  1162. if (tor_addr_parse_mask_ports(private_nets[i], 0, &addr,
  1163. &maskbits, NULL, NULL)<0) {
  1164. tor_assert(0);
  1165. }
  1166. if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
  1167. p->maskbits == maskbits) {
  1168. is_private = 1;
  1169. break;
  1170. }
  1171. }
  1172. if (!is_private) {
  1173. policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
  1174. }
  1175. } else
  1176. tor_assert(0);
  1177. }
  1178. /** Create a string representing a summary for an exit policy.
  1179. * The summary will either be an "accept" plus a comma-separated list of port
  1180. * ranges or a "reject" plus port-ranges, depending on which is shorter.
  1181. *
  1182. * If no exits are allowed at all then NULL is returned, if no ports
  1183. * are blocked instead of "reject " we return "accept 1-65535" (this
  1184. * is an exception to the shorter-representation-wins rule).
  1185. */
  1186. char *
  1187. policy_summarize(smartlist_t *policy, sa_family_t family)
  1188. {
  1189. smartlist_t *summary = policy_summary_create();
  1190. smartlist_t *accepts, *rejects;
  1191. int i, last, start_prt;
  1192. size_t accepts_len, rejects_len;
  1193. char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
  1194. const char *prefix;
  1195. tor_assert(policy);
  1196. /* Create the summary list */
  1197. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  1198. sa_family_t f = tor_addr_family(&p->addr);
  1199. if (f != AF_INET && f != AF_INET6) {
  1200. log_warn(LD_BUG, "Weird family when summarizing address policy");
  1201. }
  1202. if (f != family)
  1203. continue;
  1204. /* XXXX-ipv6 More family work is needed */
  1205. policy_summary_add_item(summary, p);
  1206. } SMARTLIST_FOREACH_END(p);
  1207. /* Now create two lists of strings, one for accepted and one
  1208. * for rejected ports. We take care to merge ranges so that
  1209. * we avoid getting stuff like "1-4,5-9,10", instead we want
  1210. * "1-10"
  1211. */
  1212. i = 0;
  1213. start_prt = 1;
  1214. accepts = smartlist_new();
  1215. rejects = smartlist_new();
  1216. while (1) {
  1217. last = i == smartlist_len(summary)-1;
  1218. if (last ||
  1219. AT(i)->accepted != AT(i+1)->accepted) {
  1220. char buf[POLICY_BUF_LEN];
  1221. if (start_prt == AT(i)->prt_max)
  1222. tor_snprintf(buf, sizeof(buf), "%d", start_prt);
  1223. else
  1224. tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
  1225. if (AT(i)->accepted)
  1226. smartlist_add(accepts, tor_strdup(buf));
  1227. else
  1228. smartlist_add(rejects, tor_strdup(buf));
  1229. if (last)
  1230. break;
  1231. start_prt = AT(i+1)->prt_min;
  1232. };
  1233. i++;
  1234. };
  1235. /* Figure out which of the two stringlists will be shorter and use
  1236. * that to build the result
  1237. */
  1238. if (smartlist_len(accepts) == 0) { /* no exits at all */
  1239. result = tor_strdup("reject 1-65535");
  1240. goto cleanup;
  1241. }
  1242. if (smartlist_len(rejects) == 0) { /* no rejects at all */
  1243. result = tor_strdup("accept 1-65535");
  1244. goto cleanup;
  1245. }
  1246. accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
  1247. rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
  1248. if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
  1249. accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
  1250. char *c;
  1251. shorter_str = accepts_str;
  1252. prefix = "accept";
  1253. c = shorter_str + (MAX_EXITPOLICY_SUMMARY_LEN-strlen(prefix)-1);
  1254. while (*c != ',' && c >= shorter_str)
  1255. c--;
  1256. tor_assert(c >= shorter_str);
  1257. tor_assert(*c == ',');
  1258. *c = '\0';
  1259. } else if (rejects_len < accepts_len) {
  1260. shorter_str = rejects_str;
  1261. prefix = "reject";
  1262. } else {
  1263. shorter_str = accepts_str;
  1264. prefix = "accept";
  1265. }
  1266. tor_asprintf(&result, "%s %s", prefix, shorter_str);
  1267. cleanup:
  1268. /* cleanup */
  1269. SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
  1270. smartlist_free(summary);
  1271. tor_free(accepts_str);
  1272. SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
  1273. smartlist_free(accepts);
  1274. tor_free(rejects_str);
  1275. SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
  1276. smartlist_free(rejects);
  1277. return result;
  1278. }
  1279. /** Convert a summarized policy string into a short_policy_t. Return NULL
  1280. * if the string is not well-formed. */
  1281. short_policy_t *
  1282. parse_short_policy(const char *summary)
  1283. {
  1284. const char *orig_summary = summary;
  1285. short_policy_t *result;
  1286. int is_accept;
  1287. int n_entries;
  1288. short_policy_entry_t entries[MAX_EXITPOLICY_SUMMARY_LEN]; /* overkill */
  1289. const char *next;
  1290. if (!strcmpstart(summary, "accept ")) {
  1291. is_accept = 1;
  1292. summary += strlen("accept ");
  1293. } else if (!strcmpstart(summary, "reject ")) {
  1294. is_accept = 0;
  1295. summary += strlen("reject ");
  1296. } else {
  1297. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Unrecognized policy summary keyword");
  1298. return NULL;
  1299. }
  1300. n_entries = 0;
  1301. for ( ; *summary; summary = next) {
  1302. const char *comma = strchr(summary, ',');
  1303. unsigned low, high;
  1304. char dummy;
  1305. char ent_buf[32];
  1306. size_t len;
  1307. next = comma ? comma+1 : strchr(summary, '\0');
  1308. len = comma ? (size_t)(comma - summary) : strlen(summary);
  1309. if (n_entries == MAX_EXITPOLICY_SUMMARY_LEN) {
  1310. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Impossibly long policy summary %s",
  1311. escaped(orig_summary));
  1312. return NULL;
  1313. }
  1314. if (! TOR_ISDIGIT(*summary) || len > (sizeof(ent_buf)-1)) {
  1315. /* unrecognized entry format. skip it. */
  1316. continue;
  1317. }
  1318. if (len < 1) {
  1319. /* empty; skip it. */
  1320. /* XXX This happens to be unreachable, since if len==0, then *summary is
  1321. * ',' or '\0', and the TOR_ISDIGIT test above would have failed. */
  1322. continue;
  1323. }
  1324. memcpy(ent_buf, summary, len);
  1325. ent_buf[len] = '\0';
  1326. if (tor_sscanf(ent_buf, "%u-%u%c", &low, &high, &dummy) == 2) {
  1327. if (low<1 || low>65535 || high<1 || high>65535 || low>high) {
  1328. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1329. "Found bad entry in policy summary %s", escaped(orig_summary));
  1330. return NULL;
  1331. }
  1332. } else if (tor_sscanf(ent_buf, "%u%c", &low, &dummy) == 1) {
  1333. if (low<1 || low>65535) {
  1334. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1335. "Found bad entry in policy summary %s", escaped(orig_summary));
  1336. return NULL;
  1337. }
  1338. high = low;
  1339. } else {
  1340. log_fn(LOG_PROTOCOL_WARN, LD_DIR,"Found bad entry in policy summary %s",
  1341. escaped(orig_summary));
  1342. return NULL;
  1343. }
  1344. entries[n_entries].min_port = low;
  1345. entries[n_entries].max_port = high;
  1346. n_entries++;
  1347. }
  1348. if (n_entries == 0) {
  1349. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1350. "Found no port-range entries in summary %s", escaped(orig_summary));
  1351. return NULL;
  1352. }
  1353. {
  1354. size_t size = STRUCT_OFFSET(short_policy_t, entries) +
  1355. sizeof(short_policy_entry_t)*(n_entries);
  1356. result = tor_malloc_zero(size);
  1357. tor_assert( (char*)&result->entries[n_entries-1] < ((char*)result)+size);
  1358. }
  1359. result->is_accept = is_accept;
  1360. result->n_entries = n_entries;
  1361. memcpy(result->entries, entries, sizeof(short_policy_entry_t)*n_entries);
  1362. return result;
  1363. }
  1364. /** Write <b>policy</b> back out into a string. Used only for unit tests
  1365. * currently. */
  1366. char *
  1367. write_short_policy(const short_policy_t *policy)
  1368. {
  1369. int i;
  1370. char *answer;
  1371. smartlist_t *sl = smartlist_new();
  1372. smartlist_add_asprintf(sl, "%s", policy->is_accept ? "accept " : "reject ");
  1373. for (i=0; i < policy->n_entries; i++) {
  1374. const short_policy_entry_t *e = &policy->entries[i];
  1375. if (e->min_port == e->max_port) {
  1376. smartlist_add_asprintf(sl, "%d", e->min_port);
  1377. } else {
  1378. smartlist_add_asprintf(sl, "%d-%d", e->min_port, e->max_port);
  1379. }
  1380. if (i < policy->n_entries-1)
  1381. smartlist_add(sl, tor_strdup(","));
  1382. }
  1383. answer = smartlist_join_strings(sl, "", 0, NULL);
  1384. SMARTLIST_FOREACH(sl, char *, a, tor_free(a));
  1385. smartlist_free(sl);
  1386. return answer;
  1387. }
  1388. /** Release all storage held in <b>policy</b>. */
  1389. void
  1390. short_policy_free(short_policy_t *policy)
  1391. {
  1392. tor_free(policy);
  1393. }
  1394. /** See whether the <b>addr</b>:<b>port</b> address is likely to be accepted
  1395. * or rejected by the summarized policy <b>policy</b>. Return values are as
  1396. * for compare_tor_addr_to_addr_policy. Unlike the regular addr_policy
  1397. * functions, requires the <b>port</b> be specified. */
  1398. addr_policy_result_t
  1399. compare_tor_addr_to_short_policy(const tor_addr_t *addr, uint16_t port,
  1400. const short_policy_t *policy)
  1401. {
  1402. int i;
  1403. int found_match = 0;
  1404. int accept;
  1405. tor_assert(port != 0);
  1406. if (addr && tor_addr_is_null(addr))
  1407. addr = NULL; /* Unspec means 'no address at all,' in this context. */
  1408. if (addr && get_options()->ClientRejectInternalAddresses &&
  1409. (tor_addr_is_internal(addr, 0) || tor_addr_is_loopback(addr)))
  1410. return ADDR_POLICY_REJECTED;
  1411. for (i=0; i < policy->n_entries; ++i) {
  1412. const short_policy_entry_t *e = &policy->entries[i];
  1413. if (e->min_port <= port && port <= e->max_port) {
  1414. found_match = 1;
  1415. break;
  1416. }
  1417. }
  1418. if (found_match)
  1419. accept = policy->is_accept;
  1420. else
  1421. accept = ! policy->is_accept;
  1422. /* ???? are these right? -NM */
  1423. /* We should be sure not to return ADDR_POLICY_ACCEPTED in the accept
  1424. * case here, because it would cause clients to believe that the node
  1425. * allows exit enclaving. Trying it anyway would open up a cool attack
  1426. * where the node refuses due to exitpolicy, the client reacts in
  1427. * surprise by rewriting the node's exitpolicy to reject *:*, and then
  1428. * a bad guy targets users by causing them to attempt such connections
  1429. * to 98% of the exits.
  1430. *
  1431. * Once microdescriptors can handle addresses in special cases (e.g. if
  1432. * we ever solve ticket 1774), we can provide certainty here. -RD */
  1433. if (accept)
  1434. return ADDR_POLICY_PROBABLY_ACCEPTED;
  1435. else
  1436. return ADDR_POLICY_REJECTED;
  1437. }
  1438. /** Return true iff <b>policy</b> seems reject all ports */
  1439. int
  1440. short_policy_is_reject_star(const short_policy_t *policy)
  1441. {
  1442. /* This doesn't need to be as much on the lookout as policy_is_reject_star,
  1443. * since policy summaries are from the consensus or from consensus
  1444. * microdescs.
  1445. */
  1446. tor_assert(policy);
  1447. /* Check for an exact match of "reject 1-65535". */
  1448. return (policy->is_accept == 0 && policy->n_entries == 1 &&
  1449. policy->entries[0].min_port == 1 &&
  1450. policy->entries[0].max_port == 65535);
  1451. }
  1452. /** Decides whether addr:port is probably or definitely accepted or rejected by
  1453. * <b>node</b>. See compare_tor_addr_to_addr_policy for details on addr/port
  1454. * interpretation. */
  1455. addr_policy_result_t
  1456. compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port,
  1457. const node_t *node)
  1458. {
  1459. if (node->rejects_all)
  1460. return ADDR_POLICY_REJECTED;
  1461. if (node->ri)
  1462. return compare_tor_addr_to_addr_policy(addr, port, node->ri->exit_policy);
  1463. else if (node->md) {
  1464. if (node->md->exit_policy == NULL)
  1465. return ADDR_POLICY_REJECTED;
  1466. else
  1467. return compare_tor_addr_to_short_policy(addr, port,
  1468. node->md->exit_policy);
  1469. } else
  1470. return ADDR_POLICY_PROBABLY_REJECTED;
  1471. }
  1472. /** Implementation for GETINFO control command: knows the answer for questions
  1473. * about "exit-policy/..." */
  1474. int
  1475. getinfo_helper_policies(control_connection_t *conn,
  1476. const char *question, char **answer,
  1477. const char **errmsg)
  1478. {
  1479. (void) conn;
  1480. (void) errmsg;
  1481. if (!strcmp(question, "exit-policy/default")) {
  1482. *answer = tor_strdup(DEFAULT_EXIT_POLICY);
  1483. }
  1484. return 0;
  1485. }
  1486. /** Release all storage held by <b>p</b>. */
  1487. void
  1488. addr_policy_list_free(smartlist_t *lst)
  1489. {
  1490. if (!lst)
  1491. return;
  1492. SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
  1493. smartlist_free(lst);
  1494. }
  1495. /** Release all storage held by <b>p</b>. */
  1496. void
  1497. addr_policy_free(addr_policy_t *p)
  1498. {
  1499. if (!p)
  1500. return;
  1501. if (--p->refcnt <= 0) {
  1502. if (p->is_canonical) {
  1503. policy_map_ent_t search, *found;
  1504. search.policy = p;
  1505. found = HT_REMOVE(policy_map, &policy_root, &search);
  1506. if (found) {
  1507. tor_assert(p == found->policy);
  1508. tor_free(found);
  1509. }
  1510. }
  1511. tor_free(p);
  1512. }
  1513. }
  1514. /** Release all storage held by policy variables. */
  1515. void
  1516. policies_free_all(void)
  1517. {
  1518. addr_policy_list_free(reachable_or_addr_policy);
  1519. reachable_or_addr_policy = NULL;
  1520. addr_policy_list_free(reachable_dir_addr_policy);
  1521. reachable_dir_addr_policy = NULL;
  1522. addr_policy_list_free(socks_policy);
  1523. socks_policy = NULL;
  1524. addr_policy_list_free(dir_policy);
  1525. dir_policy = NULL;
  1526. addr_policy_list_free(authdir_reject_policy);
  1527. authdir_reject_policy = NULL;
  1528. addr_policy_list_free(authdir_invalid_policy);
  1529. authdir_invalid_policy = NULL;
  1530. addr_policy_list_free(authdir_baddir_policy);
  1531. authdir_baddir_policy = NULL;
  1532. addr_policy_list_free(authdir_badexit_policy);
  1533. authdir_badexit_policy = NULL;
  1534. if (!HT_EMPTY(&policy_root)) {
  1535. policy_map_ent_t **ent;
  1536. int n = 0;
  1537. char buf[POLICY_BUF_LEN];
  1538. log_warn(LD_MM, "Still had %d address policies cached at shutdown.",
  1539. (int)HT_SIZE(&policy_root));
  1540. /* Note the first 10 cached policies to try to figure out where they
  1541. * might be coming from. */
  1542. HT_FOREACH(ent, policy_map, &policy_root) {
  1543. if (++n > 10)
  1544. break;
  1545. if (policy_write_item(buf, sizeof(buf), (*ent)->policy, 0) >= 0)
  1546. log_warn(LD_MM," %d [%d]: %s", n, (*ent)->policy->refcnt, buf);
  1547. }
  1548. }
  1549. HT_CLEAR(policy_map, &policy_root);
  1550. }