policies.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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. #define POLICIES_PRIVATE
  10. #include "or.h"
  11. #include "config.h"
  12. #include "dirserv.h"
  13. #include "nodelist.h"
  14. #include "policies.h"
  15. #include "router.h"
  16. #include "routerparse.h"
  17. #include "geoip.h"
  18. #include "ht.h"
  19. /** Policy that addresses for incoming SOCKS connections must match. */
  20. static smartlist_t *socks_policy = NULL;
  21. /** Policy that addresses for incoming directory connections must match. */
  22. static smartlist_t *dir_policy = NULL;
  23. /** Policy that addresses for incoming router descriptors must match in order
  24. * to be published by us. */
  25. static smartlist_t *authdir_reject_policy = NULL;
  26. /** Policy that addresses for incoming router descriptors must match in order
  27. * to be marked as valid in our networkstatus. */
  28. static smartlist_t *authdir_invalid_policy = NULL;
  29. /** Policy that addresses for incoming router descriptors must <b>not</b>
  30. * match in order to not be marked as BadExit. */
  31. static smartlist_t *authdir_badexit_policy = NULL;
  32. /** Parsed addr_policy_t describing which addresses we believe we can start
  33. * circuits at. */
  34. static smartlist_t *reachable_or_addr_policy = NULL;
  35. /** Parsed addr_policy_t describing which addresses we believe we can connect
  36. * to directories at. */
  37. static smartlist_t *reachable_dir_addr_policy = NULL;
  38. /** Element of an exit policy summary */
  39. typedef struct policy_summary_item_t {
  40. uint16_t prt_min; /**< Lowest port number to accept/reject. */
  41. uint16_t prt_max; /**< Highest port number to accept/reject. */
  42. uint64_t reject_count; /**< Number of IP-Addresses that are rejected to
  43. this port range. */
  44. unsigned int accepted:1; /** Has this port already been accepted */
  45. } policy_summary_item_t;
  46. /** Private networks. This list is used in two places, once to expand the
  47. * "private" keyword when parsing our own exit policy, secondly to ignore
  48. * just such networks when building exit policy summaries. It is important
  49. * that all authorities agree on that list when creating summaries, so don't
  50. * just change this without a proper migration plan and a proposal and stuff.
  51. */
  52. static const char *private_nets[] = {
  53. "0.0.0.0/8", "169.254.0.0/16",
  54. "127.0.0.0/8", "192.168.0.0/16", "10.0.0.0/8", "172.16.0.0/12",
  55. "[::]/8",
  56. "[fc00::]/7", "[fe80::]/10", "[fec0::]/10", "[ff00::]/8", "[::]/127",
  57. NULL
  58. };
  59. static int policies_parse_exit_policy_internal(
  60. config_line_t *cfg,
  61. smartlist_t **dest,
  62. int ipv6_exit,
  63. int rejectprivate,
  64. const smartlist_t *configured_addresses,
  65. int reject_interface_addresses,
  66. int reject_configured_port_addresses,
  67. int add_default_policy);
  68. /** Replace all "private" entries in *<b>policy</b> with their expanded
  69. * equivalents. */
  70. void
  71. policy_expand_private(smartlist_t **policy)
  72. {
  73. uint16_t port_min, port_max;
  74. int i;
  75. smartlist_t *tmp;
  76. if (!*policy) /*XXXX disallow NULL policies? */
  77. return;
  78. tmp = smartlist_new();
  79. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
  80. if (! p->is_private) {
  81. smartlist_add(tmp, p);
  82. continue;
  83. }
  84. for (i = 0; private_nets[i]; ++i) {
  85. addr_policy_t newpolicy;
  86. memcpy(&newpolicy, p, sizeof(addr_policy_t));
  87. newpolicy.is_private = 0;
  88. newpolicy.is_canonical = 0;
  89. if (tor_addr_parse_mask_ports(private_nets[i], 0,
  90. &newpolicy.addr,
  91. &newpolicy.maskbits, &port_min, &port_max)<0) {
  92. tor_assert(0);
  93. }
  94. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy));
  95. }
  96. addr_policy_free(p);
  97. } SMARTLIST_FOREACH_END(p);
  98. smartlist_free(*policy);
  99. *policy = tmp;
  100. }
  101. /** Expand each of the AF_UNSPEC elements in *<b>policy</b> (which indicate
  102. * protocol-neutral wildcards) into a pair of wildcard elements: one IPv4-
  103. * specific and one IPv6-specific. */
  104. void
  105. policy_expand_unspec(smartlist_t **policy)
  106. {
  107. smartlist_t *tmp;
  108. if (!*policy)
  109. return;
  110. tmp = smartlist_new();
  111. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, p) {
  112. sa_family_t family = tor_addr_family(&p->addr);
  113. if (family == AF_INET6 || family == AF_INET || p->is_private) {
  114. smartlist_add(tmp, p);
  115. } else if (family == AF_UNSPEC) {
  116. addr_policy_t newpolicy_ipv4;
  117. addr_policy_t newpolicy_ipv6;
  118. memcpy(&newpolicy_ipv4, p, sizeof(addr_policy_t));
  119. memcpy(&newpolicy_ipv6, p, sizeof(addr_policy_t));
  120. newpolicy_ipv4.is_canonical = 0;
  121. newpolicy_ipv6.is_canonical = 0;
  122. if (p->maskbits != 0) {
  123. log_warn(LD_BUG, "AF_UNSPEC policy with maskbits==%d", p->maskbits);
  124. newpolicy_ipv4.maskbits = 0;
  125. newpolicy_ipv6.maskbits = 0;
  126. }
  127. tor_addr_from_ipv4h(&newpolicy_ipv4.addr, 0);
  128. tor_addr_from_ipv6_bytes(&newpolicy_ipv6.addr,
  129. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  130. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv4));
  131. smartlist_add(tmp, addr_policy_get_canonical_entry(&newpolicy_ipv6));
  132. addr_policy_free(p);
  133. } else {
  134. log_warn(LD_BUG, "Funny-looking address policy with family %d", family);
  135. smartlist_add(tmp, p);
  136. }
  137. } SMARTLIST_FOREACH_END(p);
  138. smartlist_free(*policy);
  139. *policy = tmp;
  140. }
  141. /**
  142. * Given a linked list of config lines containing "accept[6]" and "reject[6]"
  143. * tokens, parse them and append the result to <b>dest</b>. Return -1
  144. * if any tokens are malformed (and don't append any), else return 0.
  145. *
  146. * If <b>assume_action</b> is nonnegative, then insert its action
  147. * (ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT) for items that specify no
  148. * action.
  149. */
  150. static int
  151. parse_addr_policy(config_line_t *cfg, smartlist_t **dest,
  152. int assume_action)
  153. {
  154. smartlist_t *result;
  155. smartlist_t *entries;
  156. addr_policy_t *item;
  157. int malformed_list;
  158. int r = 0;
  159. if (!cfg)
  160. return 0;
  161. result = smartlist_new();
  162. entries = smartlist_new();
  163. for (; cfg; cfg = cfg->next) {
  164. smartlist_split_string(entries, cfg->value, ",",
  165. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  166. SMARTLIST_FOREACH_BEGIN(entries, const char *, ent) {
  167. log_debug(LD_CONFIG,"Adding new entry '%s'",ent);
  168. malformed_list = 0;
  169. item = router_parse_addr_policy_item_from_string(ent, assume_action,
  170. &malformed_list);
  171. if (item) {
  172. smartlist_add(result, item);
  173. } else if (malformed_list) {
  174. /* the error is so severe the entire list should be discarded */
  175. log_warn(LD_CONFIG, "Malformed policy '%s'. Discarding entire policy "
  176. "list.", ent);
  177. r = -1;
  178. } else {
  179. /* the error is minor: don't add the item, but keep processing the
  180. * rest of the policies in the list */
  181. log_debug(LD_CONFIG, "Ignored policy '%s' due to non-fatal error. "
  182. "The remainder of the policy list will be used.",
  183. ent);
  184. }
  185. } SMARTLIST_FOREACH_END(ent);
  186. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  187. smartlist_clear(entries);
  188. }
  189. smartlist_free(entries);
  190. if (r == -1) {
  191. addr_policy_list_free(result);
  192. } else {
  193. policy_expand_private(&result);
  194. policy_expand_unspec(&result);
  195. if (*dest) {
  196. smartlist_add_all(*dest, result);
  197. smartlist_free(result);
  198. } else {
  199. *dest = result;
  200. }
  201. }
  202. return r;
  203. }
  204. /** Helper: parse the Reachable(Dir|OR)?Addresses fields into
  205. * reachable_(or|dir)_addr_policy. The options should already have
  206. * been validated by validate_addr_policies.
  207. */
  208. static int
  209. parse_reachable_addresses(void)
  210. {
  211. const or_options_t *options = get_options();
  212. int ret = 0;
  213. if (options->ReachableDirAddresses &&
  214. options->ReachableORAddresses &&
  215. options->ReachableAddresses) {
  216. log_warn(LD_CONFIG,
  217. "Both ReachableDirAddresses and ReachableORAddresses are set. "
  218. "ReachableAddresses setting will be ignored.");
  219. }
  220. addr_policy_list_free(reachable_or_addr_policy);
  221. reachable_or_addr_policy = NULL;
  222. if (!options->ReachableORAddresses && options->ReachableAddresses)
  223. log_info(LD_CONFIG,
  224. "Using ReachableAddresses as ReachableORAddresses.");
  225. if (parse_addr_policy(options->ReachableORAddresses ?
  226. options->ReachableORAddresses :
  227. options->ReachableAddresses,
  228. &reachable_or_addr_policy, ADDR_POLICY_ACCEPT)) {
  229. log_warn(LD_CONFIG,
  230. "Error parsing Reachable%sAddresses entry; ignoring.",
  231. options->ReachableORAddresses ? "OR" : "");
  232. ret = -1;
  233. }
  234. addr_policy_list_free(reachable_dir_addr_policy);
  235. reachable_dir_addr_policy = NULL;
  236. if (!options->ReachableDirAddresses && options->ReachableAddresses)
  237. log_info(LD_CONFIG,
  238. "Using ReachableAddresses as ReachableDirAddresses");
  239. if (parse_addr_policy(options->ReachableDirAddresses ?
  240. options->ReachableDirAddresses :
  241. options->ReachableAddresses,
  242. &reachable_dir_addr_policy, ADDR_POLICY_ACCEPT)) {
  243. if (options->ReachableDirAddresses)
  244. log_warn(LD_CONFIG,
  245. "Error parsing ReachableDirAddresses entry; ignoring.");
  246. ret = -1;
  247. }
  248. return ret;
  249. }
  250. /** Return true iff the firewall options might block any address:port
  251. * combination.
  252. */
  253. int
  254. firewall_is_fascist_or(void)
  255. {
  256. return reachable_or_addr_policy != NULL;
  257. }
  258. /** Return true iff <b>policy</b> (possibly NULL) will allow a
  259. * connection to <b>addr</b>:<b>port</b>.
  260. */
  261. static int
  262. addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
  263. smartlist_t *policy)
  264. {
  265. addr_policy_result_t p;
  266. p = compare_tor_addr_to_addr_policy(addr, port, policy);
  267. switch (p) {
  268. case ADDR_POLICY_PROBABLY_ACCEPTED:
  269. case ADDR_POLICY_ACCEPTED:
  270. return 1;
  271. case ADDR_POLICY_PROBABLY_REJECTED:
  272. case ADDR_POLICY_REJECTED:
  273. return 0;
  274. default:
  275. log_warn(LD_BUG, "Unexpected result: %d", (int)p);
  276. return 0;
  277. }
  278. }
  279. /** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
  280. * <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
  281. * order. */
  282. /* XXXX deprecate when possible. */
  283. static int
  284. addr_policy_permits_address(uint32_t addr, uint16_t port,
  285. smartlist_t *policy)
  286. {
  287. tor_addr_t a;
  288. tor_addr_from_ipv4h(&a, addr);
  289. return addr_policy_permits_tor_addr(&a, port, policy);
  290. }
  291. /** Return true iff we think our firewall will let us make an OR connection to
  292. * addr:port. */
  293. int
  294. fascist_firewall_allows_address_or(const tor_addr_t *addr, uint16_t port)
  295. {
  296. return addr_policy_permits_tor_addr(addr, port,
  297. reachable_or_addr_policy);
  298. }
  299. /** Return true iff we think our firewall will let us make an OR connection to
  300. * <b>ri</b>. */
  301. int
  302. fascist_firewall_allows_or(const routerinfo_t *ri)
  303. {
  304. /* XXXX proposal 118 */
  305. tor_addr_t addr;
  306. tor_addr_from_ipv4h(&addr, ri->addr);
  307. return fascist_firewall_allows_address_or(&addr, ri->or_port);
  308. }
  309. /** Return true iff we think our firewall will let us make an OR connection to
  310. * <b>node</b>. */
  311. int
  312. fascist_firewall_allows_node(const node_t *node)
  313. {
  314. if (node->ri) {
  315. return fascist_firewall_allows_or(node->ri);
  316. } else if (node->rs) {
  317. tor_addr_t addr;
  318. tor_addr_from_ipv4h(&addr, node->rs->addr);
  319. return fascist_firewall_allows_address_or(&addr, node->rs->or_port);
  320. } else {
  321. return 1;
  322. }
  323. }
  324. /** Return true iff we think our firewall will let us make a directory
  325. * connection to addr:port. */
  326. int
  327. fascist_firewall_allows_address_dir(const tor_addr_t *addr, uint16_t port)
  328. {
  329. return addr_policy_permits_tor_addr(addr, port,
  330. reachable_dir_addr_policy);
  331. }
  332. /** Return 1 if <b>addr</b> is permitted to connect to our dir port,
  333. * based on <b>dir_policy</b>. Else return 0.
  334. */
  335. int
  336. dir_policy_permits_address(const tor_addr_t *addr)
  337. {
  338. return addr_policy_permits_tor_addr(addr, 1, dir_policy);
  339. }
  340. /** Return 1 if <b>addr</b> is permitted to connect to our socks port,
  341. * based on <b>socks_policy</b>. Else return 0.
  342. */
  343. int
  344. socks_policy_permits_address(const tor_addr_t *addr)
  345. {
  346. return addr_policy_permits_tor_addr(addr, 1, socks_policy);
  347. }
  348. /** Return true iff the address <b>addr</b> is in a country listed in the
  349. * case-insensitive list of country codes <b>cc_list</b>. */
  350. static int
  351. addr_is_in_cc_list(uint32_t addr, const smartlist_t *cc_list)
  352. {
  353. country_t country;
  354. const char *name;
  355. tor_addr_t tar;
  356. if (!cc_list)
  357. return 0;
  358. /* XXXXipv6 */
  359. tor_addr_from_ipv4h(&tar, addr);
  360. country = geoip_get_country_by_addr(&tar);
  361. name = geoip_get_country_name(country);
  362. return smartlist_contains_string_case(cc_list, name);
  363. }
  364. /** Return 1 if <b>addr</b>:<b>port</b> is permitted to publish to our
  365. * directory, based on <b>authdir_reject_policy</b>. Else return 0.
  366. */
  367. int
  368. authdir_policy_permits_address(uint32_t addr, uint16_t port)
  369. {
  370. if (! addr_policy_permits_address(addr, port, authdir_reject_policy))
  371. return 0;
  372. return !addr_is_in_cc_list(addr, get_options()->AuthDirRejectCCs);
  373. }
  374. /** Return 1 if <b>addr</b>:<b>port</b> is considered valid in our
  375. * directory, based on <b>authdir_invalid_policy</b>. Else return 0.
  376. */
  377. int
  378. authdir_policy_valid_address(uint32_t addr, uint16_t port)
  379. {
  380. if (! addr_policy_permits_address(addr, port, authdir_invalid_policy))
  381. return 0;
  382. return !addr_is_in_cc_list(addr, get_options()->AuthDirInvalidCCs);
  383. }
  384. /** Return 1 if <b>addr</b>:<b>port</b> should be marked as a bad exit,
  385. * based on <b>authdir_badexit_policy</b>. Else return 0.
  386. */
  387. int
  388. authdir_policy_badexit_address(uint32_t addr, uint16_t port)
  389. {
  390. if (! addr_policy_permits_address(addr, port, authdir_badexit_policy))
  391. return 1;
  392. return addr_is_in_cc_list(addr, get_options()->AuthDirBadExitCCs);
  393. }
  394. #define REJECT(arg) \
  395. STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
  396. /** Config helper: If there's any problem with the policy configuration
  397. * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
  398. * allocated description of the error. Else return 0. */
  399. int
  400. validate_addr_policies(const or_options_t *options, char **msg)
  401. {
  402. /* XXXX Maybe merge this into parse_policies_from_options, to make sure
  403. * that the two can't go out of sync. */
  404. smartlist_t *addr_policy=NULL;
  405. *msg = NULL;
  406. if (policies_parse_exit_policy_from_options(options,0,NULL,&addr_policy)) {
  407. REJECT("Error in ExitPolicy entry.");
  408. }
  409. static int warned_about_exitrelay = 0;
  410. const int exitrelay_setting_is_auto = options->ExitRelay == -1;
  411. const int policy_accepts_something =
  412. ! (policy_is_reject_star(addr_policy, AF_INET) &&
  413. policy_is_reject_star(addr_policy, AF_INET6));
  414. if (server_mode(options) &&
  415. ! warned_about_exitrelay &&
  416. exitrelay_setting_is_auto &&
  417. policy_accepts_something) {
  418. /* Policy accepts something */
  419. warned_about_exitrelay = 1;
  420. log_warn(LD_CONFIG,
  421. "Tor is running as an exit relay%s. If you did not want this "
  422. "behavior, please set the ExitRelay option to 0. If you do "
  423. "want to run an exit Relay, please set the ExitRelay option "
  424. "to 1 to disable this warning, and for forward compatibility.",
  425. options->ExitPolicy == NULL ?
  426. " with the default exit policy" : "");
  427. if (options->ExitPolicy == NULL) {
  428. log_warn(LD_CONFIG,
  429. "In a future version of Tor, ExitRelay 0 may become the "
  430. "default when no ExitPolicy is given.");
  431. }
  432. }
  433. /* The rest of these calls *append* to addr_policy. So don't actually
  434. * use the results for anything other than checking if they parse! */
  435. if (parse_addr_policy(options->DirPolicy, &addr_policy, -1))
  436. REJECT("Error in DirPolicy entry.");
  437. if (parse_addr_policy(options->SocksPolicy, &addr_policy, -1))
  438. REJECT("Error in SocksPolicy entry.");
  439. if (parse_addr_policy(options->AuthDirReject, &addr_policy,
  440. ADDR_POLICY_REJECT))
  441. REJECT("Error in AuthDirReject entry.");
  442. if (parse_addr_policy(options->AuthDirInvalid, &addr_policy,
  443. ADDR_POLICY_REJECT))
  444. REJECT("Error in AuthDirInvalid entry.");
  445. if (parse_addr_policy(options->AuthDirBadExit, &addr_policy,
  446. ADDR_POLICY_REJECT))
  447. REJECT("Error in AuthDirBadExit entry.");
  448. if (parse_addr_policy(options->ReachableAddresses, &addr_policy,
  449. ADDR_POLICY_ACCEPT))
  450. REJECT("Error in ReachableAddresses entry.");
  451. if (parse_addr_policy(options->ReachableORAddresses, &addr_policy,
  452. ADDR_POLICY_ACCEPT))
  453. REJECT("Error in ReachableORAddresses entry.");
  454. if (parse_addr_policy(options->ReachableDirAddresses, &addr_policy,
  455. ADDR_POLICY_ACCEPT))
  456. REJECT("Error in ReachableDirAddresses entry.");
  457. err:
  458. addr_policy_list_free(addr_policy);
  459. return *msg ? -1 : 0;
  460. #undef REJECT
  461. }
  462. /** Parse <b>string</b> in the same way that the exit policy
  463. * is parsed, and put the processed version in *<b>policy</b>.
  464. * Ignore port specifiers.
  465. */
  466. static int
  467. load_policy_from_option(config_line_t *config, const char *option_name,
  468. smartlist_t **policy,
  469. int assume_action)
  470. {
  471. int r;
  472. int killed_any_ports = 0;
  473. addr_policy_list_free(*policy);
  474. *policy = NULL;
  475. r = parse_addr_policy(config, policy, assume_action);
  476. if (r < 0) {
  477. return -1;
  478. }
  479. if (*policy) {
  480. SMARTLIST_FOREACH_BEGIN(*policy, addr_policy_t *, n) {
  481. /* ports aren't used in these. */
  482. if (n->prt_min > 1 || n->prt_max != 65535) {
  483. addr_policy_t newp, *c;
  484. memcpy(&newp, n, sizeof(newp));
  485. newp.prt_min = 1;
  486. newp.prt_max = 65535;
  487. newp.is_canonical = 0;
  488. c = addr_policy_get_canonical_entry(&newp);
  489. SMARTLIST_REPLACE_CURRENT(*policy, n, c);
  490. addr_policy_free(n);
  491. killed_any_ports = 1;
  492. }
  493. } SMARTLIST_FOREACH_END(n);
  494. }
  495. if (killed_any_ports) {
  496. log_warn(LD_CONFIG, "Ignoring ports in %s option.", option_name);
  497. }
  498. return 0;
  499. }
  500. /** Set all policies based on <b>options</b>, which should have been validated
  501. * first by validate_addr_policies. */
  502. int
  503. policies_parse_from_options(const or_options_t *options)
  504. {
  505. int ret = 0;
  506. if (load_policy_from_option(options->SocksPolicy, "SocksPolicy",
  507. &socks_policy, -1) < 0)
  508. ret = -1;
  509. if (load_policy_from_option(options->DirPolicy, "DirPolicy",
  510. &dir_policy, -1) < 0)
  511. ret = -1;
  512. if (load_policy_from_option(options->AuthDirReject, "AuthDirReject",
  513. &authdir_reject_policy, ADDR_POLICY_REJECT) < 0)
  514. ret = -1;
  515. if (load_policy_from_option(options->AuthDirInvalid, "AuthDirInvalid",
  516. &authdir_invalid_policy, ADDR_POLICY_REJECT) < 0)
  517. ret = -1;
  518. if (load_policy_from_option(options->AuthDirBadExit, "AuthDirBadExit",
  519. &authdir_badexit_policy, ADDR_POLICY_REJECT) < 0)
  520. ret = -1;
  521. if (parse_reachable_addresses() < 0)
  522. ret = -1;
  523. return ret;
  524. }
  525. /** Compare two provided address policy items, and return -1, 0, or 1
  526. * if the first is less than, equal to, or greater than the second. */
  527. static int
  528. cmp_single_addr_policy(addr_policy_t *a, addr_policy_t *b)
  529. {
  530. int r;
  531. if ((r=((int)a->policy_type - (int)b->policy_type)))
  532. return r;
  533. if ((r=((int)a->is_private - (int)b->is_private)))
  534. return r;
  535. /* refcnt and is_canonical are irrelevant to equality,
  536. * they are hash table implementation details */
  537. if ((r=tor_addr_compare(&a->addr, &b->addr, CMP_EXACT)))
  538. return r;
  539. if ((r=((int)a->maskbits - (int)b->maskbits)))
  540. return r;
  541. if ((r=((int)a->prt_min - (int)b->prt_min)))
  542. return r;
  543. if ((r=((int)a->prt_max - (int)b->prt_max)))
  544. return r;
  545. return 0;
  546. }
  547. /** Like cmp_single_addr_policy() above, but looks at the
  548. * whole set of policies in each case. */
  549. int
  550. cmp_addr_policies(smartlist_t *a, smartlist_t *b)
  551. {
  552. int r, i;
  553. int len_a = a ? smartlist_len(a) : 0;
  554. int len_b = b ? smartlist_len(b) : 0;
  555. for (i = 0; i < len_a && i < len_b; ++i) {
  556. if ((r = cmp_single_addr_policy(smartlist_get(a, i), smartlist_get(b, i))))
  557. return r;
  558. }
  559. if (i == len_a && i == len_b)
  560. return 0;
  561. if (i < len_a)
  562. return -1;
  563. else
  564. return 1;
  565. }
  566. /** Node in hashtable used to store address policy entries. */
  567. typedef struct policy_map_ent_t {
  568. HT_ENTRY(policy_map_ent_t) node;
  569. addr_policy_t *policy;
  570. } policy_map_ent_t;
  571. /* DOCDOC policy_root */
  572. static HT_HEAD(policy_map, policy_map_ent_t) policy_root = HT_INITIALIZER();
  573. /** Return true iff a and b are equal. */
  574. static INLINE int
  575. policy_eq(policy_map_ent_t *a, policy_map_ent_t *b)
  576. {
  577. return cmp_single_addr_policy(a->policy, b->policy) == 0;
  578. }
  579. /** Return a hashcode for <b>ent</b> */
  580. static unsigned int
  581. policy_hash(const policy_map_ent_t *ent)
  582. {
  583. const addr_policy_t *a = ent->policy;
  584. addr_policy_t aa;
  585. memset(&aa, 0, sizeof(aa));
  586. aa.prt_min = a->prt_min;
  587. aa.prt_max = a->prt_max;
  588. aa.maskbits = a->maskbits;
  589. aa.policy_type = a->policy_type;
  590. aa.is_private = a->is_private;
  591. if (a->is_private) {
  592. aa.is_private = 1;
  593. } else {
  594. tor_addr_copy_tight(&aa.addr, &a->addr);
  595. }
  596. return (unsigned) siphash24g(&aa, sizeof(aa));
  597. }
  598. HT_PROTOTYPE(policy_map, policy_map_ent_t, node, policy_hash,
  599. policy_eq)
  600. HT_GENERATE2(policy_map, policy_map_ent_t, node, policy_hash,
  601. policy_eq, 0.6, tor_reallocarray_, tor_free_)
  602. /** Given a pointer to an addr_policy_t, return a copy of the pointer to the
  603. * "canonical" copy of that addr_policy_t; the canonical copy is a single
  604. * reference-counted object. */
  605. addr_policy_t *
  606. addr_policy_get_canonical_entry(addr_policy_t *e)
  607. {
  608. policy_map_ent_t search, *found;
  609. if (e->is_canonical)
  610. return e;
  611. search.policy = e;
  612. found = HT_FIND(policy_map, &policy_root, &search);
  613. if (!found) {
  614. found = tor_malloc_zero(sizeof(policy_map_ent_t));
  615. found->policy = tor_memdup(e, sizeof(addr_policy_t));
  616. found->policy->is_canonical = 1;
  617. found->policy->refcnt = 0;
  618. HT_INSERT(policy_map, &policy_root, found);
  619. }
  620. tor_assert(!cmp_single_addr_policy(found->policy, e));
  621. ++found->policy->refcnt;
  622. return found->policy;
  623. }
  624. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  625. * addr and port are both known. */
  626. static addr_policy_result_t
  627. compare_known_tor_addr_to_addr_policy(const tor_addr_t *addr, uint16_t port,
  628. const smartlist_t *policy)
  629. {
  630. /* We know the address and port, and we know the policy, so we can just
  631. * compute an exact match. */
  632. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  633. /* Address is known */
  634. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  635. CMP_EXACT)) {
  636. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  637. /* Exact match for the policy */
  638. return tmpe->policy_type == ADDR_POLICY_ACCEPT ?
  639. ADDR_POLICY_ACCEPTED : ADDR_POLICY_REJECTED;
  640. }
  641. }
  642. } SMARTLIST_FOREACH_END(tmpe);
  643. /* accept all by default. */
  644. return ADDR_POLICY_ACCEPTED;
  645. }
  646. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  647. * addr is known but port is not. */
  648. static addr_policy_result_t
  649. compare_known_tor_addr_to_addr_policy_noport(const tor_addr_t *addr,
  650. const smartlist_t *policy)
  651. {
  652. /* We look to see if there's a definite match. If so, we return that
  653. match's value, unless there's an intervening possible match that says
  654. something different. */
  655. int maybe_accept = 0, maybe_reject = 0;
  656. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  657. if (!tor_addr_compare_masked(addr, &tmpe->addr, tmpe->maskbits,
  658. CMP_EXACT)) {
  659. if (tmpe->prt_min <= 1 && tmpe->prt_max >= 65535) {
  660. /* Definitely matches, since it covers all ports. */
  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. /** Helper for compare_tor_addr_to_addr_policy. Implements the case where
  683. * port is known but address is not. */
  684. static addr_policy_result_t
  685. compare_unknown_tor_addr_to_addr_policy(uint16_t port,
  686. const smartlist_t *policy)
  687. {
  688. /* We look to see if there's a definite match. If so, we return that
  689. match's value, unless there's an intervening possible match that says
  690. something different. */
  691. int maybe_accept = 0, maybe_reject = 0;
  692. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, tmpe) {
  693. if (tmpe->prt_min <= port && port <= tmpe->prt_max) {
  694. if (tmpe->maskbits == 0) {
  695. /* Definitely matches, since it covers all addresses. */
  696. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  697. /* If we already hit a clause that might trigger a 'reject', than we
  698. * can't be sure of this certain 'accept'.*/
  699. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED :
  700. ADDR_POLICY_ACCEPTED;
  701. } else {
  702. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED :
  703. ADDR_POLICY_REJECTED;
  704. }
  705. } else {
  706. /* Might match. */
  707. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  708. maybe_reject = 1;
  709. else
  710. maybe_accept = 1;
  711. }
  712. }
  713. } SMARTLIST_FOREACH_END(tmpe);
  714. /* accept all by default. */
  715. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  716. }
  717. /** Decide whether a given addr:port is definitely accepted,
  718. * definitely rejected, probably accepted, or probably rejected by a
  719. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  720. * target address. If <b>port</b> is 0, we don't know the port of the
  721. * target address. (At least one of <b>addr</b> and <b>port</b> must be
  722. * provided. If you want to know whether a policy would definitely reject
  723. * an unknown address:port, use policy_is_reject_star().)
  724. *
  725. * We could do better by assuming that some ranges never match typical
  726. * addresses (127.0.0.1, and so on). But we'll try this for now.
  727. */
  728. MOCK_IMPL(addr_policy_result_t,
  729. compare_tor_addr_to_addr_policy,(const tor_addr_t *addr, uint16_t port,
  730. const smartlist_t *policy))
  731. {
  732. if (!policy) {
  733. /* no policy? accept all. */
  734. return ADDR_POLICY_ACCEPTED;
  735. } else if (addr == NULL || tor_addr_is_null(addr)) {
  736. if (port == 0) {
  737. log_info(LD_BUG, "Rejecting null address with 0 port (family %d)",
  738. addr ? tor_addr_family(addr) : -1);
  739. return ADDR_POLICY_REJECTED;
  740. }
  741. return compare_unknown_tor_addr_to_addr_policy(port, policy);
  742. } else if (port == 0) {
  743. return compare_known_tor_addr_to_addr_policy_noport(addr, policy);
  744. } else {
  745. return compare_known_tor_addr_to_addr_policy(addr, port, policy);
  746. }
  747. }
  748. /** Return true iff the address policy <b>a</b> covers every case that
  749. * would be covered by <b>b</b>, so that a,b is redundant. */
  750. static int
  751. addr_policy_covers(addr_policy_t *a, addr_policy_t *b)
  752. {
  753. if (tor_addr_family(&a->addr) != tor_addr_family(&b->addr)) {
  754. /* You can't cover a different family. */
  755. return 0;
  756. }
  757. /* We can ignore accept/reject, since "accept *:80, reject *:80" reduces
  758. * to "accept *:80". */
  759. if (a->maskbits > b->maskbits) {
  760. /* a has more fixed bits than b; it can't possibly cover b. */
  761. return 0;
  762. }
  763. if (tor_addr_compare_masked(&a->addr, &b->addr, a->maskbits, CMP_EXACT)) {
  764. /* There's a fixed bit in a that's set differently in b. */
  765. return 0;
  766. }
  767. return (a->prt_min <= b->prt_min && a->prt_max >= b->prt_max);
  768. }
  769. /** Return true iff the address policies <b>a</b> and <b>b</b> intersect,
  770. * that is, there exists an address/port that is covered by <b>a</b> that
  771. * is also covered by <b>b</b>.
  772. */
  773. static int
  774. addr_policy_intersects(addr_policy_t *a, addr_policy_t *b)
  775. {
  776. maskbits_t minbits;
  777. /* All the bits we care about are those that are set in both
  778. * netmasks. If they are equal in a and b's networkaddresses
  779. * then the networks intersect. If there is a difference,
  780. * then they do not. */
  781. if (a->maskbits < b->maskbits)
  782. minbits = a->maskbits;
  783. else
  784. minbits = b->maskbits;
  785. if (tor_addr_compare_masked(&a->addr, &b->addr, minbits, CMP_EXACT))
  786. return 0;
  787. if (a->prt_max < b->prt_min || b->prt_max < a->prt_min)
  788. return 0;
  789. return 1;
  790. }
  791. /** Add the exit policy described by <b>more</b> to <b>policy</b>.
  792. */
  793. STATIC void
  794. append_exit_policy_string(smartlist_t **policy, const char *more)
  795. {
  796. config_line_t tmp;
  797. tmp.key = NULL;
  798. tmp.value = (char*) more;
  799. tmp.next = NULL;
  800. if (parse_addr_policy(&tmp, policy, -1)<0) {
  801. log_warn(LD_BUG, "Unable to parse internally generated policy %s",more);
  802. }
  803. }
  804. /** Add "reject <b>addr</b>:*" to <b>dest</b>, creating the list as needed. */
  805. void
  806. addr_policy_append_reject_addr(smartlist_t **dest, const tor_addr_t *addr)
  807. {
  808. tor_assert(dest);
  809. tor_assert(addr);
  810. addr_policy_t p, *add;
  811. memset(&p, 0, sizeof(p));
  812. p.policy_type = ADDR_POLICY_REJECT;
  813. p.maskbits = tor_addr_family(addr) == AF_INET6 ? 128 : 32;
  814. tor_addr_copy(&p.addr, addr);
  815. p.prt_min = 1;
  816. p.prt_max = 65535;
  817. add = addr_policy_get_canonical_entry(&p);
  818. if (!*dest)
  819. *dest = smartlist_new();
  820. smartlist_add(*dest, add);
  821. log_debug(LD_CONFIG, "Adding a reject ExitPolicy 'reject %s:*'",
  822. fmt_addr(addr));
  823. }
  824. /* Is addr public for the purposes of rejection? */
  825. static int
  826. tor_addr_is_public_for_reject(const tor_addr_t *addr)
  827. {
  828. return !tor_addr_is_null(addr) && !tor_addr_is_internal(addr, 0);
  829. }
  830. /* Add "reject <b>addr</b>:*" to <b>dest</b>, creating the list as needed.
  831. * Filter the address, only adding an IPv4 reject rule if ipv4_rules
  832. * is true, and similarly for ipv6_rules. Check each address returns true for
  833. * tor_addr_is_public_for_reject before adding it.
  834. */
  835. static void
  836. addr_policy_append_reject_addr_filter(smartlist_t **dest,
  837. const tor_addr_t *addr,
  838. int ipv4_rules,
  839. int ipv6_rules)
  840. {
  841. tor_assert(dest);
  842. tor_assert(addr);
  843. /* Only reject IP addresses which are public */
  844. if (tor_addr_is_public_for_reject(addr)) {
  845. /* Reject IPv4 addresses and IPv6 addresses based on the filters */
  846. int is_ipv4 = tor_addr_is_v4(addr);
  847. if ((is_ipv4 && ipv4_rules) || (!is_ipv4 && ipv6_rules)) {
  848. addr_policy_append_reject_addr(dest, addr);
  849. }
  850. }
  851. }
  852. /** Add "reject addr:*" to <b>dest</b>, for each addr in addrs, creating the
  853. * list as needed. */
  854. void
  855. addr_policy_append_reject_addr_list(smartlist_t **dest,
  856. const smartlist_t *addrs)
  857. {
  858. tor_assert(dest);
  859. tor_assert(addrs);
  860. SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, addr) {
  861. addr_policy_append_reject_addr(dest, addr);
  862. } SMARTLIST_FOREACH_END(addr);
  863. }
  864. /** Add "reject addr:*" to <b>dest</b>, for each addr in addrs, creating the
  865. * list as needed. Filter using */
  866. static void
  867. addr_policy_append_reject_addr_list_filter(smartlist_t **dest,
  868. const smartlist_t *addrs,
  869. int ipv4_rules,
  870. int ipv6_rules)
  871. {
  872. tor_assert(dest);
  873. tor_assert(addrs);
  874. SMARTLIST_FOREACH_BEGIN(addrs, tor_addr_t *, addr) {
  875. addr_policy_append_reject_addr_filter(dest, addr, ipv4_rules, ipv6_rules);
  876. } SMARTLIST_FOREACH_END(addr);
  877. }
  878. /** Detect and excise "dead code" from the policy *<b>dest</b>. */
  879. static void
  880. exit_policy_remove_redundancies(smartlist_t *dest)
  881. {
  882. addr_policy_t *ap, *tmp;
  883. int i, j;
  884. /* Step one: kill every ipv4 thing after *4:*, every IPv6 thing after *6:*
  885. */
  886. {
  887. int kill_v4=0, kill_v6=0;
  888. for (i = 0; i < smartlist_len(dest); ++i) {
  889. sa_family_t family;
  890. ap = smartlist_get(dest, i);
  891. family = tor_addr_family(&ap->addr);
  892. if ((family == AF_INET && kill_v4) ||
  893. (family == AF_INET6 && kill_v6)) {
  894. smartlist_del_keeporder(dest, i--);
  895. addr_policy_free(ap);
  896. continue;
  897. }
  898. if (ap->maskbits == 0 && ap->prt_min <= 1 && ap->prt_max >= 65535) {
  899. /* This is a catch-all line -- later lines are unreachable. */
  900. if (family == AF_INET) {
  901. kill_v4 = 1;
  902. } else if (family == AF_INET6) {
  903. kill_v6 = 1;
  904. }
  905. }
  906. }
  907. }
  908. /* Step two: for every entry, see if there's a redundant entry
  909. * later on, and remove it. */
  910. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  911. ap = smartlist_get(dest, i);
  912. for (j = i+1; j < smartlist_len(dest); ++j) {
  913. tmp = smartlist_get(dest, j);
  914. tor_assert(j > i);
  915. if (addr_policy_covers(ap, tmp)) {
  916. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  917. policy_write_item(p1, sizeof(p1), tmp, 0);
  918. policy_write_item(p2, sizeof(p2), ap, 0);
  919. log_debug(LD_CONFIG, "Removing exit policy %s (%d). It is made "
  920. "redundant by %s (%d).", p1, j, p2, i);
  921. smartlist_del_keeporder(dest, j--);
  922. addr_policy_free(tmp);
  923. }
  924. }
  925. }
  926. /* Step three: for every entry A, see if there's an entry B making this one
  927. * redundant later on. This is the case if A and B are of the same type
  928. * (accept/reject), A is a subset of B, and there is no other entry of
  929. * different type in between those two that intersects with A.
  930. *
  931. * Anybody want to double-check the logic here? XXX
  932. */
  933. for (i = 0; i < smartlist_len(dest)-1; ++i) {
  934. ap = smartlist_get(dest, i);
  935. for (j = i+1; j < smartlist_len(dest); ++j) {
  936. // tor_assert(j > i); // j starts out at i+1; j only increases; i only
  937. // // decreases.
  938. tmp = smartlist_get(dest, j);
  939. if (ap->policy_type != tmp->policy_type) {
  940. if (addr_policy_intersects(ap, tmp))
  941. break;
  942. } else { /* policy_types are equal. */
  943. if (addr_policy_covers(tmp, ap)) {
  944. char p1[POLICY_BUF_LEN], p2[POLICY_BUF_LEN];
  945. policy_write_item(p1, sizeof(p1), ap, 0);
  946. policy_write_item(p2, sizeof(p2), tmp, 0);
  947. log_debug(LD_CONFIG, "Removing exit policy %s. It is already "
  948. "covered by %s.", p1, p2);
  949. smartlist_del_keeporder(dest, i--);
  950. addr_policy_free(ap);
  951. break;
  952. }
  953. }
  954. }
  955. }
  956. }
  957. /** Reject private helper for policies_parse_exit_policy_internal: rejects
  958. * publicly routable addresses on this exit relay.
  959. *
  960. * Add reject entries to the linked list *dest:
  961. * - if configured_addresses is non-NULL, add entries that reject each
  962. * tor_addr_t* in the list as a destination.
  963. * - if reject_interface_addresses is true, add entries that reject each
  964. * public IPv4 and IPv6 address of each interface on this machine.
  965. * - if reject_configured_port_addresses is true, add entries that reject
  966. * each IPv4 and IPv6 address configured for a port.
  967. *
  968. * IPv6 entries are only added if ipv6_exit is true. (All IPv6 addresses are
  969. * already blocked by policies_parse_exit_policy_internal if ipv6_exit is
  970. * false.)
  971. *
  972. * The list *dest is created as needed.
  973. */
  974. void
  975. policies_parse_exit_policy_reject_private(
  976. smartlist_t **dest,
  977. int ipv6_exit,
  978. const smartlist_t *configured_addresses,
  979. int reject_interface_addresses,
  980. int reject_configured_port_addresses)
  981. {
  982. tor_assert(dest);
  983. /* Reject configured addresses, if they are from public netblocks. */
  984. if (configured_addresses) {
  985. addr_policy_append_reject_addr_list_filter(dest, configured_addresses,
  986. 1, ipv6_exit);
  987. }
  988. /* Reject configured port addresses, if they are from public netblocks. */
  989. if (reject_configured_port_addresses) {
  990. const smartlist_t *port_addrs = get_configured_ports();
  991. SMARTLIST_FOREACH_BEGIN(port_addrs, port_cfg_t *, port) {
  992. /* Only reject port IP addresses, not port unix sockets */
  993. if (!port->is_unix_addr) {
  994. addr_policy_append_reject_addr_filter(dest, &port->addr, 1, ipv6_exit);
  995. }
  996. } SMARTLIST_FOREACH_END(port);
  997. }
  998. /* Reject local addresses from public netblocks on any interface. */
  999. if (reject_interface_addresses) {
  1000. smartlist_t *public_addresses = NULL;
  1001. /* Reject public IPv4 addresses on any interface */
  1002. public_addresses = get_interface_address6_list(LOG_INFO, AF_INET, 0);
  1003. addr_policy_append_reject_addr_list_filter(dest, public_addresses, 1, 0);
  1004. free_interface_address6_list(public_addresses);
  1005. /* Don't look for IPv6 addresses if we're configured as IPv4-only */
  1006. if (ipv6_exit) {
  1007. /* Reject public IPv6 addresses on any interface */
  1008. public_addresses = get_interface_address6_list(LOG_INFO, AF_INET6, 0);
  1009. addr_policy_append_reject_addr_list_filter(dest, public_addresses, 0, 1);
  1010. free_interface_address6_list(public_addresses);
  1011. }
  1012. }
  1013. /* If addresses were added multiple times, remove all but one of them. */
  1014. if (*dest) {
  1015. exit_policy_remove_redundancies(*dest);
  1016. }
  1017. }
  1018. #define DEFAULT_EXIT_POLICY \
  1019. "reject *:25,reject *:119,reject *:135-139,reject *:445," \
  1020. "reject *:563,reject *:1214,reject *:4661-4666," \
  1021. "reject *:6346-6429,reject *:6699,reject *:6881-6999,accept *:*"
  1022. /** Parse the exit policy <b>cfg</b> into the linked list *<b>dest</b>.
  1023. *
  1024. * If <b>ipv6_exit</b> is false, prepend "reject *6:*" to the policy.
  1025. *
  1026. * If <b>rejectprivate</b> is true:
  1027. * - prepend "reject private:*" to the policy.
  1028. * - prepend entries that reject publicly routable addresses on this exit
  1029. * relay by calling policies_parse_exit_policy_reject_private
  1030. *
  1031. * If cfg doesn't end in an absolute accept or reject and if
  1032. * <b>add_default_policy</b> is true, add the default exit
  1033. * policy afterwards.
  1034. *
  1035. * Return -1 if we can't parse cfg, else return 0.
  1036. *
  1037. * This function is used to parse the exit policy from our torrc. For
  1038. * the functions used to parse the exit policy from a router descriptor,
  1039. * see router_add_exit_policy.
  1040. */
  1041. static int
  1042. policies_parse_exit_policy_internal(config_line_t *cfg,
  1043. smartlist_t **dest,
  1044. int ipv6_exit,
  1045. int rejectprivate,
  1046. const smartlist_t *configured_addresses,
  1047. int reject_interface_addresses,
  1048. int reject_configured_port_addresses,
  1049. int add_default_policy)
  1050. {
  1051. if (!ipv6_exit) {
  1052. append_exit_policy_string(dest, "reject *6:*");
  1053. }
  1054. if (rejectprivate) {
  1055. /* Reject IPv4 and IPv6 reserved private netblocks */
  1056. append_exit_policy_string(dest, "reject private:*");
  1057. /* Reject IPv4 and IPv6 publicly routable addresses on this exit relay */
  1058. policies_parse_exit_policy_reject_private(
  1059. dest, ipv6_exit,
  1060. configured_addresses,
  1061. reject_interface_addresses,
  1062. reject_configured_port_addresses);
  1063. }
  1064. if (parse_addr_policy(cfg, dest, -1))
  1065. return -1;
  1066. /* Before we add the default policy and final rejects, check to see if
  1067. * there are any lines after accept *:* or reject *:*. These lines have no
  1068. * effect, and are most likely an error. */
  1069. int found_final_effective_entry = 0;
  1070. int first_redundant_entry = 0;
  1071. for (int i = 0; i < smartlist_len(*dest); ++i) {
  1072. sa_family_t family;
  1073. addr_policy_t *p;
  1074. int found_ipv4_wildcard = 0, found_ipv6_wildcard = 0;
  1075. p = smartlist_get(*dest, i);
  1076. /* Look for accept/reject *[4|6|]:* entires */
  1077. if (p->prt_min <= 1 && p->prt_max == 65535 && p->maskbits == 0) {
  1078. family = tor_addr_family(&p->addr);
  1079. /* accept/reject *:* may have already been expanded into
  1080. * accept/reject *4:*,accept/reject *6:*
  1081. * But handle both forms.
  1082. */
  1083. if (family == AF_INET || family == AF_UNSPEC) {
  1084. found_ipv4_wildcard = 1;
  1085. }
  1086. if (family == AF_INET6 || family == AF_UNSPEC) {
  1087. found_ipv6_wildcard = 1;
  1088. }
  1089. }
  1090. /* We also find accept *4:*,reject *6:* ; and
  1091. * accept *4:*,<other policies>,accept *6:* ; and similar.
  1092. * That's ok, because they make any subsequent entries redundant. */
  1093. if (found_ipv4_wildcard && found_ipv6_wildcard) {
  1094. found_final_effective_entry = 1;
  1095. /* if we're not on the final entry in the list */
  1096. if (i < smartlist_len(*dest) - 1) {
  1097. first_redundant_entry = i + 1;
  1098. }
  1099. break;
  1100. }
  1101. }
  1102. /* Work out if there are redundant trailing entries in the policy list */
  1103. if (found_final_effective_entry && first_redundant_entry > 0) {
  1104. addr_policy_t *p;
  1105. /* Longest possible policy is
  1106. * "accept6 ffff:ffff:..255/128:10000-65535",
  1107. * which contains a max-length IPv6 address, plus 24 characters. */
  1108. char line[TOR_ADDR_BUF_LEN + 32];
  1109. tor_assert(first_redundant_entry < smartlist_len(*dest));
  1110. p = smartlist_get(*dest, first_redundant_entry);
  1111. /* since we've already parsed the policy into an addr_policy_t struct,
  1112. * we might not log exactly what the user typed in */
  1113. policy_write_item(line, TOR_ADDR_BUF_LEN + 32, p, 0);
  1114. log_warn(LD_DIR, "Exit policy '%s' and all following policies are "
  1115. "redundant, as it follows accept/reject *:* rules for both "
  1116. "IPv4 and IPv6. They will be removed from the exit policy. (Use "
  1117. "accept/reject *:* as the last entry in any exit policy.)",
  1118. line);
  1119. }
  1120. if (add_default_policy) {
  1121. append_exit_policy_string(dest, DEFAULT_EXIT_POLICY);
  1122. } else {
  1123. append_exit_policy_string(dest, "reject *4:*");
  1124. append_exit_policy_string(dest, "reject *6:*");
  1125. }
  1126. exit_policy_remove_redundancies(*dest);
  1127. return 0;
  1128. }
  1129. /** Parse exit policy in <b>cfg</b> into <b>dest</b> smartlist.
  1130. *
  1131. * Prepend an entry that rejects all IPv6 destinations unless
  1132. * <b>EXIT_POLICY_IPV6_ENABLED</b> bit is set in <b>options</b> bitmask.
  1133. *
  1134. * If <b>EXIT_POLICY_REJECT_PRIVATE</b> bit is set in <b>options</b>:
  1135. * - prepend an entry that rejects all destinations in all netblocks
  1136. * reserved for private use.
  1137. * - prepend entries that reject publicly routable addresses on this exit
  1138. * relay by calling policies_parse_exit_policy_internal
  1139. *
  1140. * If <b>EXIT_POLICY_ADD_DEFAULT</b> bit is set in <b>options</b>, append
  1141. * default exit policy entries to <b>result</b> smartlist.
  1142. */
  1143. int
  1144. policies_parse_exit_policy(config_line_t *cfg, smartlist_t **dest,
  1145. exit_policy_parser_cfg_t options,
  1146. const smartlist_t *configured_addresses)
  1147. {
  1148. int ipv6_enabled = (options & EXIT_POLICY_IPV6_ENABLED) ? 1 : 0;
  1149. int reject_private = (options & EXIT_POLICY_REJECT_PRIVATE) ? 1 : 0;
  1150. int add_default = (options & EXIT_POLICY_ADD_DEFAULT) ? 1 : 0;
  1151. return policies_parse_exit_policy_internal(cfg,dest,ipv6_enabled,
  1152. reject_private,
  1153. configured_addresses,
  1154. reject_private,
  1155. reject_private,
  1156. add_default);
  1157. }
  1158. /** Helper function that adds addr to a smartlist as long as it is non-NULL
  1159. * and not tor_addr_is_null(). */
  1160. static void
  1161. policies_add_addr_to_smartlist(smartlist_t *addr_list, const tor_addr_t *addr)
  1162. {
  1163. if (addr && !tor_addr_is_null(addr)) {
  1164. smartlist_add(addr_list, (void *)addr);
  1165. }
  1166. }
  1167. /** Helper function that adds ipv4h_addr to a smartlist as a tor_addr_t *,
  1168. * by converting it to a tor_addr_t and passing it to
  1169. * policies_add_addr_to_smartlist. */
  1170. static void
  1171. policies_add_ipv4h_to_smartlist(smartlist_t *addr_list, uint32_t ipv4h_addr)
  1172. {
  1173. if (ipv4h_addr) {
  1174. tor_addr_t ipv4_tor_addr;
  1175. tor_addr_from_ipv4h(&ipv4_tor_addr, ipv4h_addr);
  1176. policies_add_addr_to_smartlist(addr_list, (void *)&ipv4_tor_addr);
  1177. }
  1178. }
  1179. /** Helper function that adds or_options->OutboundBindAddressIPv[4|6]_ to a
  1180. * smartlist as a tor_addr_t *, as long as or_options is non-NULL,
  1181. * by passing them to policies_add_addr_to_smartlist. */
  1182. static void
  1183. policies_add_outbound_addresses_to_smartlist(smartlist_t *addr_list,
  1184. const or_options_t *or_options)
  1185. {
  1186. if (or_options) {
  1187. policies_add_addr_to_smartlist(addr_list,
  1188. &or_options->OutboundBindAddressIPv4_);
  1189. policies_add_addr_to_smartlist(addr_list,
  1190. &or_options->OutboundBindAddressIPv6_);
  1191. }
  1192. }
  1193. /** Parse <b>ExitPolicy</b> member of <b>or_options</b> into <b>result</b>
  1194. * smartlist.
  1195. * If <b>or_options->IPv6Exit</b> is false, prepend an entry that
  1196. * rejects all IPv6 destinations.
  1197. *
  1198. * If <b>or_options->ExitPolicyRejectPrivate</b> is true:
  1199. * - prepend an entry that rejects all destinations in all netblocks reserved
  1200. * for private use.
  1201. * - if local_address is non-zero, treat it as a host-order IPv4 address, and
  1202. * add it to the list of configured addresses.
  1203. * - if ipv6_local_address is non-NULL, and not the null tor_addr_t, add it
  1204. * to the list of configured addresses.
  1205. * - if or_options->OutboundBindAddressIPv4_ is not the null tor_addr_t, add
  1206. * it to the list of configured addresses.
  1207. * - if or_options->OutboundBindAddressIPv6_ is not the null tor_addr_t, add
  1208. * it to the list of configured addresses.
  1209. *
  1210. * If <b>or_options->BridgeRelay</b> is false, append entries of default
  1211. * Tor exit policy into <b>result</b> smartlist.
  1212. *
  1213. * If or_options->ExitRelay is false, then make our exit policy into
  1214. * "reject *:*" regardless.
  1215. */
  1216. int
  1217. policies_parse_exit_policy_from_options(const or_options_t *or_options,
  1218. uint32_t local_address,
  1219. const tor_addr_t *ipv6_local_address,
  1220. smartlist_t **result)
  1221. {
  1222. exit_policy_parser_cfg_t parser_cfg = 0;
  1223. smartlist_t *configured_addresses = smartlist_new();
  1224. int rv = 0;
  1225. /* Short-circuit for non-exit relays */
  1226. if (or_options->ExitRelay == 0) {
  1227. append_exit_policy_string(result, "reject *4:*");
  1228. append_exit_policy_string(result, "reject *6:*");
  1229. return 0;
  1230. }
  1231. /* Configure the parser */
  1232. if (or_options->IPv6Exit) {
  1233. parser_cfg |= EXIT_POLICY_IPV6_ENABLED;
  1234. }
  1235. if (or_options->ExitPolicyRejectPrivate) {
  1236. parser_cfg |= EXIT_POLICY_REJECT_PRIVATE;
  1237. }
  1238. if (!or_options->BridgeRelay) {
  1239. parser_cfg |= EXIT_POLICY_ADD_DEFAULT;
  1240. }
  1241. /* Add the configured addresses to the tor_addr_t* list */
  1242. policies_add_ipv4h_to_smartlist(configured_addresses, local_address);
  1243. policies_add_addr_to_smartlist(configured_addresses, ipv6_local_address);
  1244. policies_add_outbound_addresses_to_smartlist(configured_addresses,
  1245. or_options);
  1246. rv = policies_parse_exit_policy(or_options->ExitPolicy, result, parser_cfg,
  1247. configured_addresses);
  1248. /* We don't need to free the pointers in this list, they are either constant
  1249. * or locally scoped. */
  1250. smartlist_free(configured_addresses);
  1251. return rv;
  1252. }
  1253. /** Add "reject *:*" to the end of the policy in *<b>dest</b>, allocating
  1254. * *<b>dest</b> as needed. */
  1255. void
  1256. policies_exit_policy_append_reject_star(smartlist_t **dest)
  1257. {
  1258. append_exit_policy_string(dest, "reject *4:*");
  1259. append_exit_policy_string(dest, "reject *6:*");
  1260. }
  1261. /** Replace the exit policy of <b>node</b> with reject *:* */
  1262. void
  1263. policies_set_node_exitpolicy_to_reject_all(node_t *node)
  1264. {
  1265. node->rejects_all = 1;
  1266. }
  1267. /** Return 1 if there is at least one /8 subnet in <b>policy</b> that
  1268. * allows exiting to <b>port</b>. Otherwise, return 0. */
  1269. static int
  1270. exit_policy_is_general_exit_helper(smartlist_t *policy, int port)
  1271. {
  1272. uint32_t mask, ip, i;
  1273. /* Is this /8 rejected (1), or undecided (0)? */
  1274. char subnet_status[256];
  1275. memset(subnet_status, 0, sizeof(subnet_status));
  1276. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  1277. if (tor_addr_family(&p->addr) != AF_INET)
  1278. continue; /* IPv4 only for now */
  1279. if (p->prt_min > port || p->prt_max < port)
  1280. continue; /* Doesn't cover our port. */
  1281. mask = 0;
  1282. tor_assert(p->maskbits <= 32);
  1283. if (p->maskbits)
  1284. mask = UINT32_MAX<<(32-p->maskbits);
  1285. ip = tor_addr_to_ipv4h(&p->addr);
  1286. /* Calculate the first and last subnet that this exit policy touches
  1287. * and set it as loop boundaries. */
  1288. for (i = ((mask & ip)>>24); i <= (~((mask & ip) ^ mask)>>24); ++i) {
  1289. tor_addr_t addr;
  1290. if (subnet_status[i] != 0)
  1291. continue; /* We already reject some part of this /8 */
  1292. tor_addr_from_ipv4h(&addr, i<<24);
  1293. if (tor_addr_is_internal(&addr, 0))
  1294. continue; /* Local or non-routable addresses */
  1295. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  1296. if (p->maskbits > 8)
  1297. continue; /* Narrower than a /8. */
  1298. /* We found an allowed subnet of at least size /8. Done
  1299. * for this port! */
  1300. return 1;
  1301. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  1302. subnet_status[i] = 1;
  1303. }
  1304. }
  1305. } SMARTLIST_FOREACH_END(p);
  1306. return 0;
  1307. }
  1308. /** Return true iff <b>ri</b> is "useful as an exit node", meaning
  1309. * it allows exit to at least one /8 address space for at least
  1310. * two of ports 80, 443, and 6667. */
  1311. int
  1312. exit_policy_is_general_exit(smartlist_t *policy)
  1313. {
  1314. static const int ports[] = { 80, 443, 6667 };
  1315. int n_allowed = 0;
  1316. int i;
  1317. if (!policy) /*XXXX disallow NULL policies? */
  1318. return 0;
  1319. for (i = 0; i < 3; ++i) {
  1320. n_allowed += exit_policy_is_general_exit_helper(policy, ports[i]);
  1321. }
  1322. return n_allowed >= 2;
  1323. }
  1324. /** Return false if <b>policy</b> might permit access to some addr:port;
  1325. * otherwise if we are certain it rejects everything, return true. */
  1326. int
  1327. policy_is_reject_star(const smartlist_t *policy, sa_family_t family)
  1328. {
  1329. if (!policy) /*XXXX disallow NULL policies? */
  1330. return 1;
  1331. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  1332. if (p->policy_type == ADDR_POLICY_ACCEPT &&
  1333. (tor_addr_family(&p->addr) == family ||
  1334. tor_addr_family(&p->addr) == AF_UNSPEC)) {
  1335. return 0;
  1336. } else if (p->policy_type == ADDR_POLICY_REJECT &&
  1337. p->prt_min <= 1 && p->prt_max == 65535 &&
  1338. p->maskbits == 0 &&
  1339. (tor_addr_family(&p->addr) == family ||
  1340. tor_addr_family(&p->addr) == AF_UNSPEC)) {
  1341. return 1;
  1342. }
  1343. } SMARTLIST_FOREACH_END(p);
  1344. return 1;
  1345. }
  1346. /** Write a single address policy to the buf_len byte buffer at buf. Return
  1347. * the number of characters written, or -1 on failure. */
  1348. int
  1349. policy_write_item(char *buf, size_t buflen, addr_policy_t *policy,
  1350. int format_for_desc)
  1351. {
  1352. size_t written = 0;
  1353. char addrbuf[TOR_ADDR_BUF_LEN];
  1354. const char *addrpart;
  1355. int result;
  1356. const int is_accept = policy->policy_type == ADDR_POLICY_ACCEPT;
  1357. const sa_family_t family = tor_addr_family(&policy->addr);
  1358. const int is_ip6 = (family == AF_INET6);
  1359. tor_addr_to_str(addrbuf, &policy->addr, sizeof(addrbuf), 1);
  1360. /* write accept/reject 1.2.3.4 */
  1361. if (policy->is_private) {
  1362. addrpart = "private";
  1363. } else if (policy->maskbits == 0) {
  1364. if (format_for_desc)
  1365. addrpart = "*";
  1366. else if (family == AF_INET6)
  1367. addrpart = "*6";
  1368. else if (family == AF_INET)
  1369. addrpart = "*4";
  1370. else
  1371. addrpart = "*";
  1372. } else {
  1373. addrpart = addrbuf;
  1374. }
  1375. result = tor_snprintf(buf, buflen, "%s%s %s",
  1376. is_accept ? "accept" : "reject",
  1377. (is_ip6&&format_for_desc)?"6":"",
  1378. addrpart);
  1379. if (result < 0)
  1380. return -1;
  1381. written += strlen(buf);
  1382. /* If the maskbits is 32 we don't need to give it. If the mask is 0,
  1383. * we already wrote "*". */
  1384. if (policy->maskbits < 32 && policy->maskbits > 0) {
  1385. if (tor_snprintf(buf+written, buflen-written, "/%d", policy->maskbits)<0)
  1386. return -1;
  1387. written += strlen(buf+written);
  1388. }
  1389. if (policy->prt_min <= 1 && policy->prt_max == 65535) {
  1390. /* There is no port set; write ":*" */
  1391. if (written+4 > buflen)
  1392. return -1;
  1393. strlcat(buf+written, ":*", buflen-written);
  1394. written += 2;
  1395. } else if (policy->prt_min == policy->prt_max) {
  1396. /* There is only one port; write ":80". */
  1397. result = tor_snprintf(buf+written, buflen-written, ":%d", policy->prt_min);
  1398. if (result<0)
  1399. return -1;
  1400. written += result;
  1401. } else {
  1402. /* There is a range of ports; write ":79-80". */
  1403. result = tor_snprintf(buf+written, buflen-written, ":%d-%d",
  1404. policy->prt_min, policy->prt_max);
  1405. if (result<0)
  1406. return -1;
  1407. written += result;
  1408. }
  1409. if (written < buflen)
  1410. buf[written] = '\0';
  1411. else
  1412. return -1;
  1413. return (int)written;
  1414. }
  1415. /** Create a new exit policy summary, initially only with a single
  1416. * port 1-64k item */
  1417. /* XXXX This entire thing will do most stuff in O(N^2), or worse. Use an
  1418. * RB-tree if that turns out to matter. */
  1419. static smartlist_t *
  1420. policy_summary_create(void)
  1421. {
  1422. smartlist_t *summary;
  1423. policy_summary_item_t* item;
  1424. item = tor_malloc_zero(sizeof(policy_summary_item_t));
  1425. item->prt_min = 1;
  1426. item->prt_max = 65535;
  1427. item->reject_count = 0;
  1428. item->accepted = 0;
  1429. summary = smartlist_new();
  1430. smartlist_add(summary, item);
  1431. return summary;
  1432. }
  1433. /** Split the summary item in <b>item</b> at the port <b>new_starts</b>.
  1434. * The current item is changed to end at new-starts - 1, the new item
  1435. * copies reject_count and accepted from the old item,
  1436. * starts at new_starts and ends at the port where the original item
  1437. * previously ended.
  1438. */
  1439. static policy_summary_item_t*
  1440. policy_summary_item_split(policy_summary_item_t* old, uint16_t new_starts)
  1441. {
  1442. policy_summary_item_t* new;
  1443. new = tor_malloc_zero(sizeof(policy_summary_item_t));
  1444. new->prt_min = new_starts;
  1445. new->prt_max = old->prt_max;
  1446. new->reject_count = old->reject_count;
  1447. new->accepted = old->accepted;
  1448. old->prt_max = new_starts-1;
  1449. tor_assert(old->prt_min <= old->prt_max);
  1450. tor_assert(new->prt_min <= new->prt_max);
  1451. return new;
  1452. }
  1453. /* XXXX Nick says I'm going to hell for this. If he feels charitably towards
  1454. * my immortal soul, he can clean it up himself. */
  1455. #define AT(x) ((policy_summary_item_t*)smartlist_get(summary, x))
  1456. #define REJECT_CUTOFF_COUNT (1<<25)
  1457. /** Split an exit policy summary so that prt_min and prt_max
  1458. * fall at exactly the start and end of an item respectively.
  1459. */
  1460. static int
  1461. policy_summary_split(smartlist_t *summary,
  1462. uint16_t prt_min, uint16_t prt_max)
  1463. {
  1464. int start_at_index;
  1465. int i = 0;
  1466. while (AT(i)->prt_max < prt_min)
  1467. i++;
  1468. if (AT(i)->prt_min != prt_min) {
  1469. policy_summary_item_t* new_item;
  1470. new_item = policy_summary_item_split(AT(i), prt_min);
  1471. smartlist_insert(summary, i+1, new_item);
  1472. i++;
  1473. }
  1474. start_at_index = i;
  1475. while (AT(i)->prt_max < prt_max)
  1476. i++;
  1477. if (AT(i)->prt_max != prt_max) {
  1478. policy_summary_item_t* new_item;
  1479. new_item = policy_summary_item_split(AT(i), prt_max+1);
  1480. smartlist_insert(summary, i+1, new_item);
  1481. }
  1482. return start_at_index;
  1483. }
  1484. /** Mark port ranges as accepted if they are below the reject_count */
  1485. static void
  1486. policy_summary_accept(smartlist_t *summary,
  1487. uint16_t prt_min, uint16_t prt_max)
  1488. {
  1489. int i = policy_summary_split(summary, prt_min, prt_max);
  1490. while (i < smartlist_len(summary) &&
  1491. AT(i)->prt_max <= prt_max) {
  1492. if (!AT(i)->accepted &&
  1493. AT(i)->reject_count <= REJECT_CUTOFF_COUNT)
  1494. AT(i)->accepted = 1;
  1495. i++;
  1496. }
  1497. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1498. }
  1499. /** Count the number of addresses in a network with prefixlen maskbits
  1500. * against the given portrange. */
  1501. static void
  1502. policy_summary_reject(smartlist_t *summary,
  1503. maskbits_t maskbits,
  1504. uint16_t prt_min, uint16_t prt_max)
  1505. {
  1506. int i = policy_summary_split(summary, prt_min, prt_max);
  1507. /* XXX: ipv4 specific */
  1508. uint64_t count = (U64_LITERAL(1) << (32-maskbits));
  1509. while (i < smartlist_len(summary) &&
  1510. AT(i)->prt_max <= prt_max) {
  1511. AT(i)->reject_count += count;
  1512. i++;
  1513. }
  1514. tor_assert(i < smartlist_len(summary) || prt_max==65535);
  1515. }
  1516. /** Add a single exit policy item to our summary:
  1517. * If it is an accept ignore it unless it is for all IP addresses
  1518. * ("*"), i.e. it's prefixlen/maskbits is 0, else call
  1519. * policy_summary_accept().
  1520. * If it's a reject ignore it if it is about one of the private
  1521. * networks, else call policy_summary_reject().
  1522. */
  1523. static void
  1524. policy_summary_add_item(smartlist_t *summary, addr_policy_t *p)
  1525. {
  1526. if (p->policy_type == ADDR_POLICY_ACCEPT) {
  1527. if (p->maskbits == 0) {
  1528. policy_summary_accept(summary, p->prt_min, p->prt_max);
  1529. }
  1530. } else if (p->policy_type == ADDR_POLICY_REJECT) {
  1531. int is_private = 0;
  1532. int i;
  1533. for (i = 0; private_nets[i]; ++i) {
  1534. tor_addr_t addr;
  1535. maskbits_t maskbits;
  1536. if (tor_addr_parse_mask_ports(private_nets[i], 0, &addr,
  1537. &maskbits, NULL, NULL)<0) {
  1538. tor_assert(0);
  1539. }
  1540. if (tor_addr_compare(&p->addr, &addr, CMP_EXACT) == 0 &&
  1541. p->maskbits == maskbits) {
  1542. is_private = 1;
  1543. break;
  1544. }
  1545. }
  1546. if (!is_private) {
  1547. policy_summary_reject(summary, p->maskbits, p->prt_min, p->prt_max);
  1548. }
  1549. } else
  1550. tor_assert(0);
  1551. }
  1552. /** Create a string representing a summary for an exit policy.
  1553. * The summary will either be an "accept" plus a comma-separated list of port
  1554. * ranges or a "reject" plus port-ranges, depending on which is shorter.
  1555. *
  1556. * If no exits are allowed at all then "reject 1-65535" is returned. If no
  1557. * ports are blocked instead of "reject " we return "accept 1-65535". (These
  1558. * are an exception to the shorter-representation-wins rule).
  1559. */
  1560. char *
  1561. policy_summarize(smartlist_t *policy, sa_family_t family)
  1562. {
  1563. smartlist_t *summary = policy_summary_create();
  1564. smartlist_t *accepts, *rejects;
  1565. int i, last, start_prt;
  1566. size_t accepts_len, rejects_len;
  1567. char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
  1568. const char *prefix;
  1569. tor_assert(policy);
  1570. /* Create the summary list */
  1571. SMARTLIST_FOREACH_BEGIN(policy, addr_policy_t *, p) {
  1572. sa_family_t f = tor_addr_family(&p->addr);
  1573. if (f != AF_INET && f != AF_INET6) {
  1574. log_warn(LD_BUG, "Weird family when summarizing address policy");
  1575. }
  1576. if (f != family)
  1577. continue;
  1578. /* XXXX-ipv6 More family work is needed */
  1579. policy_summary_add_item(summary, p);
  1580. } SMARTLIST_FOREACH_END(p);
  1581. /* Now create two lists of strings, one for accepted and one
  1582. * for rejected ports. We take care to merge ranges so that
  1583. * we avoid getting stuff like "1-4,5-9,10", instead we want
  1584. * "1-10"
  1585. */
  1586. i = 0;
  1587. start_prt = 1;
  1588. accepts = smartlist_new();
  1589. rejects = smartlist_new();
  1590. while (1) {
  1591. last = i == smartlist_len(summary)-1;
  1592. if (last ||
  1593. AT(i)->accepted != AT(i+1)->accepted) {
  1594. char buf[POLICY_BUF_LEN];
  1595. if (start_prt == AT(i)->prt_max)
  1596. tor_snprintf(buf, sizeof(buf), "%d", start_prt);
  1597. else
  1598. tor_snprintf(buf, sizeof(buf), "%d-%d", start_prt, AT(i)->prt_max);
  1599. if (AT(i)->accepted)
  1600. smartlist_add(accepts, tor_strdup(buf));
  1601. else
  1602. smartlist_add(rejects, tor_strdup(buf));
  1603. if (last)
  1604. break;
  1605. start_prt = AT(i+1)->prt_min;
  1606. };
  1607. i++;
  1608. };
  1609. /* Figure out which of the two stringlists will be shorter and use
  1610. * that to build the result
  1611. */
  1612. if (smartlist_len(accepts) == 0) { /* no exits at all */
  1613. result = tor_strdup("reject 1-65535");
  1614. goto cleanup;
  1615. }
  1616. if (smartlist_len(rejects) == 0) { /* no rejects at all */
  1617. result = tor_strdup("accept 1-65535");
  1618. goto cleanup;
  1619. }
  1620. accepts_str = smartlist_join_strings(accepts, ",", 0, &accepts_len);
  1621. rejects_str = smartlist_join_strings(rejects, ",", 0, &rejects_len);
  1622. if (rejects_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("reject")-1 &&
  1623. accepts_len > MAX_EXITPOLICY_SUMMARY_LEN-strlen("accept")-1) {
  1624. char *c;
  1625. shorter_str = accepts_str;
  1626. prefix = "accept";
  1627. c = shorter_str + (MAX_EXITPOLICY_SUMMARY_LEN-strlen(prefix)-1);
  1628. while (*c != ',' && c >= shorter_str)
  1629. c--;
  1630. tor_assert(c >= shorter_str);
  1631. tor_assert(*c == ',');
  1632. *c = '\0';
  1633. } else if (rejects_len < accepts_len) {
  1634. shorter_str = rejects_str;
  1635. prefix = "reject";
  1636. } else {
  1637. shorter_str = accepts_str;
  1638. prefix = "accept";
  1639. }
  1640. tor_asprintf(&result, "%s %s", prefix, shorter_str);
  1641. cleanup:
  1642. /* cleanup */
  1643. SMARTLIST_FOREACH(summary, policy_summary_item_t *, s, tor_free(s));
  1644. smartlist_free(summary);
  1645. tor_free(accepts_str);
  1646. SMARTLIST_FOREACH(accepts, char *, s, tor_free(s));
  1647. smartlist_free(accepts);
  1648. tor_free(rejects_str);
  1649. SMARTLIST_FOREACH(rejects, char *, s, tor_free(s));
  1650. smartlist_free(rejects);
  1651. return result;
  1652. }
  1653. /** Convert a summarized policy string into a short_policy_t. Return NULL
  1654. * if the string is not well-formed. */
  1655. short_policy_t *
  1656. parse_short_policy(const char *summary)
  1657. {
  1658. const char *orig_summary = summary;
  1659. short_policy_t *result;
  1660. int is_accept;
  1661. int n_entries;
  1662. short_policy_entry_t entries[MAX_EXITPOLICY_SUMMARY_LEN]; /* overkill */
  1663. const char *next;
  1664. if (!strcmpstart(summary, "accept ")) {
  1665. is_accept = 1;
  1666. summary += strlen("accept ");
  1667. } else if (!strcmpstart(summary, "reject ")) {
  1668. is_accept = 0;
  1669. summary += strlen("reject ");
  1670. } else {
  1671. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Unrecognized policy summary keyword");
  1672. return NULL;
  1673. }
  1674. n_entries = 0;
  1675. for ( ; *summary; summary = next) {
  1676. const char *comma = strchr(summary, ',');
  1677. unsigned low, high;
  1678. char dummy;
  1679. char ent_buf[32];
  1680. size_t len;
  1681. next = comma ? comma+1 : strchr(summary, '\0');
  1682. len = comma ? (size_t)(comma - summary) : strlen(summary);
  1683. if (n_entries == MAX_EXITPOLICY_SUMMARY_LEN) {
  1684. log_fn(LOG_PROTOCOL_WARN, LD_DIR, "Impossibly long policy summary %s",
  1685. escaped(orig_summary));
  1686. return NULL;
  1687. }
  1688. if (! TOR_ISDIGIT(*summary) || len > (sizeof(ent_buf)-1)) {
  1689. /* unrecognized entry format. skip it. */
  1690. continue;
  1691. }
  1692. if (len < 1) {
  1693. /* empty; skip it. */
  1694. /* XXX This happens to be unreachable, since if len==0, then *summary is
  1695. * ',' or '\0', and the TOR_ISDIGIT test above would have failed. */
  1696. continue;
  1697. }
  1698. memcpy(ent_buf, summary, len);
  1699. ent_buf[len] = '\0';
  1700. if (tor_sscanf(ent_buf, "%u-%u%c", &low, &high, &dummy) == 2) {
  1701. if (low<1 || low>65535 || high<1 || high>65535 || low>high) {
  1702. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1703. "Found bad entry in policy summary %s", escaped(orig_summary));
  1704. return NULL;
  1705. }
  1706. } else if (tor_sscanf(ent_buf, "%u%c", &low, &dummy) == 1) {
  1707. if (low<1 || low>65535) {
  1708. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1709. "Found bad entry in policy summary %s", escaped(orig_summary));
  1710. return NULL;
  1711. }
  1712. high = low;
  1713. } else {
  1714. log_fn(LOG_PROTOCOL_WARN, LD_DIR,"Found bad entry in policy summary %s",
  1715. escaped(orig_summary));
  1716. return NULL;
  1717. }
  1718. entries[n_entries].min_port = low;
  1719. entries[n_entries].max_port = high;
  1720. n_entries++;
  1721. }
  1722. if (n_entries == 0) {
  1723. log_fn(LOG_PROTOCOL_WARN, LD_DIR,
  1724. "Found no port-range entries in summary %s", escaped(orig_summary));
  1725. return NULL;
  1726. }
  1727. {
  1728. size_t size = STRUCT_OFFSET(short_policy_t, entries) +
  1729. sizeof(short_policy_entry_t)*(n_entries);
  1730. result = tor_malloc_zero(size);
  1731. tor_assert( (char*)&result->entries[n_entries-1] < ((char*)result)+size);
  1732. }
  1733. result->is_accept = is_accept;
  1734. result->n_entries = n_entries;
  1735. memcpy(result->entries, entries, sizeof(short_policy_entry_t)*n_entries);
  1736. return result;
  1737. }
  1738. /** Write <b>policy</b> back out into a string. Used only for unit tests
  1739. * currently. */
  1740. char *
  1741. write_short_policy(const short_policy_t *policy)
  1742. {
  1743. int i;
  1744. char *answer;
  1745. smartlist_t *sl = smartlist_new();
  1746. smartlist_add_asprintf(sl, "%s", policy->is_accept ? "accept " : "reject ");
  1747. for (i=0; i < policy->n_entries; i++) {
  1748. const short_policy_entry_t *e = &policy->entries[i];
  1749. if (e->min_port == e->max_port) {
  1750. smartlist_add_asprintf(sl, "%d", e->min_port);
  1751. } else {
  1752. smartlist_add_asprintf(sl, "%d-%d", e->min_port, e->max_port);
  1753. }
  1754. if (i < policy->n_entries-1)
  1755. smartlist_add(sl, tor_strdup(","));
  1756. }
  1757. answer = smartlist_join_strings(sl, "", 0, NULL);
  1758. SMARTLIST_FOREACH(sl, char *, a, tor_free(a));
  1759. smartlist_free(sl);
  1760. return answer;
  1761. }
  1762. /** Release all storage held in <b>policy</b>. */
  1763. void
  1764. short_policy_free(short_policy_t *policy)
  1765. {
  1766. tor_free(policy);
  1767. }
  1768. /** See whether the <b>addr</b>:<b>port</b> address is likely to be accepted
  1769. * or rejected by the summarized policy <b>policy</b>. Return values are as
  1770. * for compare_tor_addr_to_addr_policy. Unlike the regular addr_policy
  1771. * functions, requires the <b>port</b> be specified. */
  1772. addr_policy_result_t
  1773. compare_tor_addr_to_short_policy(const tor_addr_t *addr, uint16_t port,
  1774. const short_policy_t *policy)
  1775. {
  1776. int i;
  1777. int found_match = 0;
  1778. int accept;
  1779. tor_assert(port != 0);
  1780. if (addr && tor_addr_is_null(addr))
  1781. addr = NULL; /* Unspec means 'no address at all,' in this context. */
  1782. if (addr && get_options()->ClientRejectInternalAddresses &&
  1783. (tor_addr_is_internal(addr, 0) || tor_addr_is_loopback(addr)))
  1784. return ADDR_POLICY_REJECTED;
  1785. for (i=0; i < policy->n_entries; ++i) {
  1786. const short_policy_entry_t *e = &policy->entries[i];
  1787. if (e->min_port <= port && port <= e->max_port) {
  1788. found_match = 1;
  1789. break;
  1790. }
  1791. }
  1792. if (found_match)
  1793. accept = policy->is_accept;
  1794. else
  1795. accept = ! policy->is_accept;
  1796. /* ???? are these right? -NM */
  1797. /* We should be sure not to return ADDR_POLICY_ACCEPTED in the accept
  1798. * case here, because it would cause clients to believe that the node
  1799. * allows exit enclaving. Trying it anyway would open up a cool attack
  1800. * where the node refuses due to exitpolicy, the client reacts in
  1801. * surprise by rewriting the node's exitpolicy to reject *:*, and then
  1802. * a bad guy targets users by causing them to attempt such connections
  1803. * to 98% of the exits.
  1804. *
  1805. * Once microdescriptors can handle addresses in special cases (e.g. if
  1806. * we ever solve ticket 1774), we can provide certainty here. -RD */
  1807. if (accept)
  1808. return ADDR_POLICY_PROBABLY_ACCEPTED;
  1809. else
  1810. return ADDR_POLICY_REJECTED;
  1811. }
  1812. /** Return true iff <b>policy</b> seems reject all ports */
  1813. int
  1814. short_policy_is_reject_star(const short_policy_t *policy)
  1815. {
  1816. /* This doesn't need to be as much on the lookout as policy_is_reject_star,
  1817. * since policy summaries are from the consensus or from consensus
  1818. * microdescs.
  1819. */
  1820. tor_assert(policy);
  1821. /* Check for an exact match of "reject 1-65535". */
  1822. return (policy->is_accept == 0 && policy->n_entries == 1 &&
  1823. policy->entries[0].min_port == 1 &&
  1824. policy->entries[0].max_port == 65535);
  1825. }
  1826. /** Decide whether addr:port is probably or definitely accepted or rejected by
  1827. * <b>node</b>. See compare_tor_addr_to_addr_policy for details on addr/port
  1828. * interpretation. */
  1829. addr_policy_result_t
  1830. compare_tor_addr_to_node_policy(const tor_addr_t *addr, uint16_t port,
  1831. const node_t *node)
  1832. {
  1833. if (node->rejects_all)
  1834. return ADDR_POLICY_REJECTED;
  1835. if (addr && tor_addr_family(addr) == AF_INET6) {
  1836. const short_policy_t *p = NULL;
  1837. if (node->ri)
  1838. p = node->ri->ipv6_exit_policy;
  1839. else if (node->md)
  1840. p = node->md->ipv6_exit_policy;
  1841. if (p)
  1842. return compare_tor_addr_to_short_policy(addr, port, p);
  1843. else
  1844. return ADDR_POLICY_REJECTED;
  1845. }
  1846. if (node->ri) {
  1847. return compare_tor_addr_to_addr_policy(addr, port, node->ri->exit_policy);
  1848. } else if (node->md) {
  1849. if (node->md->exit_policy == NULL)
  1850. return ADDR_POLICY_REJECTED;
  1851. else
  1852. return compare_tor_addr_to_short_policy(addr, port,
  1853. node->md->exit_policy);
  1854. } else {
  1855. return ADDR_POLICY_PROBABLY_REJECTED;
  1856. }
  1857. }
  1858. /**
  1859. * Given <b>policy_list</b>, a list of addr_policy_t, produce a string
  1860. * representation of the list.
  1861. * If <b>include_ipv4</b> is true, include IPv4 entries.
  1862. * If <b>include_ipv6</b> is true, include IPv6 entries.
  1863. */
  1864. char *
  1865. policy_dump_to_string(const smartlist_t *policy_list,
  1866. int include_ipv4,
  1867. int include_ipv6)
  1868. {
  1869. smartlist_t *policy_string_list;
  1870. char *policy_string = NULL;
  1871. policy_string_list = smartlist_new();
  1872. SMARTLIST_FOREACH_BEGIN(policy_list, addr_policy_t *, tmpe) {
  1873. char *pbuf;
  1874. int bytes_written_to_pbuf;
  1875. if ((tor_addr_family(&tmpe->addr) == AF_INET6) && (!include_ipv6)) {
  1876. continue; /* Don't include IPv6 parts of address policy */
  1877. }
  1878. if ((tor_addr_family(&tmpe->addr) == AF_INET) && (!include_ipv4)) {
  1879. continue; /* Don't include IPv4 parts of address policy */
  1880. }
  1881. pbuf = tor_malloc(POLICY_BUF_LEN);
  1882. bytes_written_to_pbuf = policy_write_item(pbuf,POLICY_BUF_LEN, tmpe, 1);
  1883. if (bytes_written_to_pbuf < 0) {
  1884. log_warn(LD_BUG, "policy_dump_to_string ran out of room!");
  1885. tor_free(pbuf);
  1886. goto done;
  1887. }
  1888. smartlist_add(policy_string_list,pbuf);
  1889. } SMARTLIST_FOREACH_END(tmpe);
  1890. policy_string = smartlist_join_strings(policy_string_list, "\n", 0, NULL);
  1891. done:
  1892. SMARTLIST_FOREACH(policy_string_list, char *, str, tor_free(str));
  1893. smartlist_free(policy_string_list);
  1894. return policy_string;
  1895. }
  1896. /** Implementation for GETINFO control command: knows the answer for questions
  1897. * about "exit-policy/..." */
  1898. int
  1899. getinfo_helper_policies(control_connection_t *conn,
  1900. const char *question, char **answer,
  1901. const char **errmsg)
  1902. {
  1903. (void) conn;
  1904. (void) errmsg;
  1905. if (!strcmp(question, "exit-policy/default")) {
  1906. *answer = tor_strdup(DEFAULT_EXIT_POLICY);
  1907. } else if (!strcmp(question, "exit-policy/reject-private/default")) {
  1908. smartlist_t *private_policy_strings;
  1909. const char **priv = private_nets;
  1910. private_policy_strings = smartlist_new();
  1911. while (*priv != NULL) {
  1912. /* IPv6 addresses are in "[]" and contain ":",
  1913. * IPv4 addresses are not in "[]" and contain "." */
  1914. smartlist_add_asprintf(private_policy_strings, "reject %s:*", *priv);
  1915. priv++;
  1916. }
  1917. *answer = smartlist_join_strings(private_policy_strings,
  1918. ",", 0, NULL);
  1919. SMARTLIST_FOREACH(private_policy_strings, char *, str, tor_free(str));
  1920. smartlist_free(private_policy_strings);
  1921. } else if (!strcmp(question, "exit-policy/reject-private/relay")) {
  1922. const or_options_t *options = get_options();
  1923. const routerinfo_t *me = router_get_my_routerinfo();
  1924. smartlist_t *private_policy_list = smartlist_new();
  1925. smartlist_t *configured_addresses = smartlist_new();
  1926. if (!me) {
  1927. *errmsg = "router_get_my_routerinfo returned NULL";
  1928. return -1;
  1929. }
  1930. /* Add the configured addresses to the tor_addr_t* list */
  1931. policies_add_ipv4h_to_smartlist(configured_addresses, me->addr);
  1932. policies_add_addr_to_smartlist(configured_addresses, &me->ipv6_addr);
  1933. policies_add_outbound_addresses_to_smartlist(configured_addresses,
  1934. options);
  1935. policies_parse_exit_policy_reject_private(
  1936. &private_policy_list,
  1937. options->IPv6Exit,
  1938. configured_addresses,
  1939. 1, 1);
  1940. *answer = policy_dump_to_string(private_policy_list, 1, 1);
  1941. addr_policy_list_free(private_policy_list);
  1942. } else if (!strcmpstart(question, "exit-policy/")) {
  1943. const routerinfo_t *me = router_get_my_routerinfo();
  1944. int include_ipv4 = 0;
  1945. int include_ipv6 = 0;
  1946. if (!strcmp(question, "exit-policy/ipv4")) {
  1947. include_ipv4 = 1;
  1948. } else if (!strcmp(question, "exit-policy/ipv6")) {
  1949. include_ipv6 = 1;
  1950. } else if (!strcmp(question, "exit-policy/full")) {
  1951. include_ipv4 = include_ipv6 = 1;
  1952. } else {
  1953. return 0; /* No such key. */
  1954. }
  1955. if (!me) {
  1956. *errmsg = "router_get_my_routerinfo returned NULL";
  1957. return -1;
  1958. }
  1959. *answer = router_dump_exit_policy_to_string(me,include_ipv4,include_ipv6);
  1960. }
  1961. return 0;
  1962. }
  1963. /** Release all storage held by <b>p</b>. */
  1964. void
  1965. addr_policy_list_free(smartlist_t *lst)
  1966. {
  1967. if (!lst)
  1968. return;
  1969. SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
  1970. smartlist_free(lst);
  1971. }
  1972. /** Release all storage held by <b>p</b>. */
  1973. void
  1974. addr_policy_free(addr_policy_t *p)
  1975. {
  1976. if (!p)
  1977. return;
  1978. if (--p->refcnt <= 0) {
  1979. if (p->is_canonical) {
  1980. policy_map_ent_t search, *found;
  1981. search.policy = p;
  1982. found = HT_REMOVE(policy_map, &policy_root, &search);
  1983. if (found) {
  1984. tor_assert(p == found->policy);
  1985. tor_free(found);
  1986. }
  1987. }
  1988. tor_free(p);
  1989. }
  1990. }
  1991. /** Release all storage held by policy variables. */
  1992. void
  1993. policies_free_all(void)
  1994. {
  1995. addr_policy_list_free(reachable_or_addr_policy);
  1996. reachable_or_addr_policy = NULL;
  1997. addr_policy_list_free(reachable_dir_addr_policy);
  1998. reachable_dir_addr_policy = NULL;
  1999. addr_policy_list_free(socks_policy);
  2000. socks_policy = NULL;
  2001. addr_policy_list_free(dir_policy);
  2002. dir_policy = NULL;
  2003. addr_policy_list_free(authdir_reject_policy);
  2004. authdir_reject_policy = NULL;
  2005. addr_policy_list_free(authdir_invalid_policy);
  2006. authdir_invalid_policy = NULL;
  2007. addr_policy_list_free(authdir_badexit_policy);
  2008. authdir_badexit_policy = NULL;
  2009. if (!HT_EMPTY(&policy_root)) {
  2010. policy_map_ent_t **ent;
  2011. int n = 0;
  2012. char buf[POLICY_BUF_LEN];
  2013. log_warn(LD_MM, "Still had %d address policies cached at shutdown.",
  2014. (int)HT_SIZE(&policy_root));
  2015. /* Note the first 10 cached policies to try to figure out where they
  2016. * might be coming from. */
  2017. HT_FOREACH(ent, policy_map, &policy_root) {
  2018. if (++n > 10)
  2019. break;
  2020. if (policy_write_item(buf, sizeof(buf), (*ent)->policy, 0) >= 0)
  2021. log_warn(LD_MM," %d [%d]: %s", n, (*ent)->policy->refcnt, buf);
  2022. }
  2023. }
  2024. HT_CLEAR(policy_map, &policy_root);
  2025. }