reasons.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file reasons.c
  6. * \brief Convert circuit, stream, and orconn error reasons to and/or from
  7. * strings and errno values.
  8. *
  9. * This module is just a bunch of functions full of case statements that
  10. * convert from one representation of our error codes to another. These are
  11. * mainly used in generating log messages, in sending messages to the
  12. * controller in control.c, and in converting errors from one protocol layer
  13. * to another.
  14. **/
  15. #include "or.h"
  16. #include "config.h"
  17. #include "reasons.h"
  18. /***************************** Edge (stream) reasons **********************/
  19. /** Convert the reason for ending a stream <b>reason</b> into the format used
  20. * in STREAM events. Return NULL if the reason is unrecognized. */
  21. const char *
  22. stream_end_reason_to_control_string(int reason)
  23. {
  24. reason &= END_STREAM_REASON_MASK;
  25. switch (reason) {
  26. case END_STREAM_REASON_MISC: return "MISC";
  27. case END_STREAM_REASON_RESOLVEFAILED: return "RESOLVEFAILED";
  28. case END_STREAM_REASON_CONNECTREFUSED: return "CONNECTREFUSED";
  29. case END_STREAM_REASON_EXITPOLICY: return "EXITPOLICY";
  30. case END_STREAM_REASON_DESTROY: return "DESTROY";
  31. case END_STREAM_REASON_DONE: return "DONE";
  32. case END_STREAM_REASON_TIMEOUT: return "TIMEOUT";
  33. case END_STREAM_REASON_NOROUTE: return "NOROUTE";
  34. case END_STREAM_REASON_HIBERNATING: return "HIBERNATING";
  35. case END_STREAM_REASON_INTERNAL: return "INTERNAL";
  36. case END_STREAM_REASON_RESOURCELIMIT: return "RESOURCELIMIT";
  37. case END_STREAM_REASON_CONNRESET: return "CONNRESET";
  38. case END_STREAM_REASON_TORPROTOCOL: return "TORPROTOCOL";
  39. case END_STREAM_REASON_NOTDIRECTORY: return "NOTDIRECTORY";
  40. case END_STREAM_REASON_CANT_ATTACH: return "CANT_ATTACH";
  41. case END_STREAM_REASON_NET_UNREACHABLE: return "NET_UNREACHABLE";
  42. case END_STREAM_REASON_SOCKSPROTOCOL: return "SOCKS_PROTOCOL";
  43. case END_STREAM_REASON_PRIVATE_ADDR: return "PRIVATE_ADDR";
  44. default: return NULL;
  45. }
  46. }
  47. /** Translate <b>reason</b>, which came from a relay 'end' cell,
  48. * into a static const string describing why the stream is closing.
  49. * <b>reason</b> is -1 if no reason was provided.
  50. */
  51. const char *
  52. stream_end_reason_to_string(int reason)
  53. {
  54. switch (reason) {
  55. case -1:
  56. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  57. "End cell arrived with length 0. Should be at least 1.");
  58. return "MALFORMED";
  59. case END_STREAM_REASON_MISC: return "misc error";
  60. case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
  61. case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
  62. case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
  63. case END_STREAM_REASON_DESTROY: return "destroyed";
  64. case END_STREAM_REASON_DONE: return "closed normally";
  65. case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
  66. case END_STREAM_REASON_NOROUTE: return "no route to host";
  67. case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
  68. case END_STREAM_REASON_INTERNAL: return "internal error at server";
  69. case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
  70. case END_STREAM_REASON_CONNRESET: return "connection reset";
  71. case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
  72. case END_STREAM_REASON_NOTDIRECTORY: return "not a directory";
  73. default:
  74. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  75. "Reason for ending (%d) not recognized.",reason);
  76. return "unknown";
  77. }
  78. }
  79. /** Translate <b>reason</b> (as from a relay 'end' cell) into an
  80. * appropriate SOCKS5 reply code.
  81. *
  82. * A reason of 0 means that we're not actually expecting to send
  83. * this code back to the socks client; we just call it 'succeeded'
  84. * to keep things simple.
  85. */
  86. socks5_reply_status_t
  87. stream_end_reason_to_socks5_response(int reason)
  88. {
  89. switch (reason & END_STREAM_REASON_MASK) {
  90. case 0:
  91. return SOCKS5_SUCCEEDED;
  92. case END_STREAM_REASON_MISC:
  93. return SOCKS5_GENERAL_ERROR;
  94. case END_STREAM_REASON_RESOLVEFAILED:
  95. return SOCKS5_HOST_UNREACHABLE;
  96. case END_STREAM_REASON_CONNECTREFUSED:
  97. return SOCKS5_CONNECTION_REFUSED;
  98. case END_STREAM_REASON_ENTRYPOLICY:
  99. return SOCKS5_NOT_ALLOWED;
  100. case END_STREAM_REASON_EXITPOLICY:
  101. return SOCKS5_NOT_ALLOWED;
  102. case END_STREAM_REASON_DESTROY:
  103. return SOCKS5_GENERAL_ERROR;
  104. case END_STREAM_REASON_DONE:
  105. /* Note that 'DONE' usually indicates a successful close from the other
  106. * side of the stream... but if we receive it before a connected cell --
  107. * that is, before we have sent a SOCKS reply -- that means that the
  108. * other side of the circuit closed the connection before telling us it
  109. * was complete. */
  110. return SOCKS5_CONNECTION_REFUSED;
  111. case END_STREAM_REASON_TIMEOUT:
  112. return SOCKS5_TTL_EXPIRED;
  113. case END_STREAM_REASON_NOROUTE:
  114. return SOCKS5_HOST_UNREACHABLE;
  115. case END_STREAM_REASON_RESOURCELIMIT:
  116. return SOCKS5_GENERAL_ERROR;
  117. case END_STREAM_REASON_HIBERNATING:
  118. return SOCKS5_GENERAL_ERROR;
  119. case END_STREAM_REASON_INTERNAL:
  120. return SOCKS5_GENERAL_ERROR;
  121. case END_STREAM_REASON_CONNRESET:
  122. return SOCKS5_CONNECTION_REFUSED;
  123. case END_STREAM_REASON_TORPROTOCOL:
  124. return SOCKS5_GENERAL_ERROR;
  125. case END_STREAM_REASON_CANT_ATTACH:
  126. return SOCKS5_GENERAL_ERROR;
  127. case END_STREAM_REASON_NET_UNREACHABLE:
  128. return SOCKS5_NET_UNREACHABLE;
  129. case END_STREAM_REASON_SOCKSPROTOCOL:
  130. return SOCKS5_GENERAL_ERROR;
  131. case END_STREAM_REASON_PRIVATE_ADDR:
  132. return SOCKS5_GENERAL_ERROR;
  133. default:
  134. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  135. "Reason for ending (%d) not recognized; "
  136. "sending generic socks error.", reason);
  137. return SOCKS5_GENERAL_ERROR;
  138. }
  139. }
  140. /* We need to use a few macros to deal with the fact that Windows
  141. * decided that their sockets interface should be a permakludge.
  142. * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
  143. * version, and S_CASE is for errors where windows has only a WSAEFOO
  144. * version. (The E is for 'error', the S is for 'socket'). */
  145. #ifdef _WIN32
  146. #define E_CASE(s) case s: case WSA ## s
  147. #define S_CASE(s) case WSA ## s
  148. #else
  149. #define E_CASE(s) case s
  150. #define S_CASE(s) case s
  151. #endif
  152. /** Given an errno from a failed exit connection, return a reason code
  153. * appropriate for use in a RELAY END cell. */
  154. uint8_t
  155. errno_to_stream_end_reason(int e)
  156. {
  157. /* To add new errors here, find out if they exist on Windows, and if a WSA*
  158. * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
  159. * appropriate. */
  160. switch (e) {
  161. case EPIPE:
  162. return END_STREAM_REASON_DONE;
  163. E_CASE(EBADF):
  164. E_CASE(EFAULT):
  165. E_CASE(EINVAL):
  166. S_CASE(EISCONN):
  167. S_CASE(ENOTSOCK):
  168. S_CASE(EPROTONOSUPPORT):
  169. S_CASE(EAFNOSUPPORT):
  170. S_CASE(ENOTCONN):
  171. return END_STREAM_REASON_INTERNAL;
  172. S_CASE(ENETUNREACH):
  173. S_CASE(EHOSTUNREACH):
  174. E_CASE(EACCES):
  175. case EPERM:
  176. return END_STREAM_REASON_NOROUTE;
  177. S_CASE(ECONNREFUSED):
  178. return END_STREAM_REASON_CONNECTREFUSED;
  179. S_CASE(ECONNRESET):
  180. return END_STREAM_REASON_CONNRESET;
  181. S_CASE(ETIMEDOUT):
  182. return END_STREAM_REASON_TIMEOUT;
  183. S_CASE(ENOBUFS):
  184. case ENOMEM:
  185. case ENFILE:
  186. S_CASE(EADDRINUSE):
  187. S_CASE(EADDRNOTAVAIL):
  188. E_CASE(EMFILE):
  189. return END_STREAM_REASON_RESOURCELIMIT;
  190. default:
  191. log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
  192. "that we are ending a stream for 'misc' reason.",
  193. e, tor_socket_strerror(e));
  194. return END_STREAM_REASON_MISC;
  195. }
  196. }
  197. /***************************** ORConn reasons *****************************/
  198. /** Convert the reason for ending an OR connection <b>r</b> into the format
  199. * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
  200. const char *
  201. orconn_end_reason_to_control_string(int r)
  202. {
  203. /* To add new errors here, find out if they exist on Windows, and if a WSA*
  204. * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
  205. * appropriate. */
  206. switch (r) {
  207. case END_OR_CONN_REASON_DONE:
  208. return "DONE";
  209. case END_OR_CONN_REASON_REFUSED:
  210. return "CONNECTREFUSED";
  211. case END_OR_CONN_REASON_OR_IDENTITY:
  212. return "IDENTITY";
  213. case END_OR_CONN_REASON_CONNRESET:
  214. return "CONNECTRESET";
  215. case END_OR_CONN_REASON_TIMEOUT:
  216. return "TIMEOUT";
  217. case END_OR_CONN_REASON_NO_ROUTE:
  218. return "NOROUTE";
  219. case END_OR_CONN_REASON_IO_ERROR:
  220. return "IOERROR";
  221. case END_OR_CONN_REASON_RESOURCE_LIMIT:
  222. return "RESOURCELIMIT";
  223. case END_OR_CONN_REASON_MISC:
  224. return "MISC";
  225. case END_OR_CONN_REASON_PT_MISSING:
  226. return "PT_MISSING";
  227. case 0:
  228. return "";
  229. default:
  230. log_warn(LD_BUG, "Unrecognized or_conn reason code %d", r);
  231. return "UNKNOWN";
  232. }
  233. }
  234. /** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
  235. int
  236. tls_error_to_orconn_end_reason(int e)
  237. {
  238. switch (e) {
  239. case TOR_TLS_ERROR_IO:
  240. return END_OR_CONN_REASON_IO_ERROR;
  241. case TOR_TLS_ERROR_CONNREFUSED:
  242. return END_OR_CONN_REASON_REFUSED;
  243. case TOR_TLS_ERROR_CONNRESET:
  244. return END_OR_CONN_REASON_CONNRESET;
  245. case TOR_TLS_ERROR_NO_ROUTE:
  246. return END_OR_CONN_REASON_NO_ROUTE;
  247. case TOR_TLS_ERROR_TIMEOUT:
  248. return END_OR_CONN_REASON_TIMEOUT;
  249. case TOR_TLS_WANTREAD:
  250. case TOR_TLS_WANTWRITE:
  251. case TOR_TLS_CLOSE:
  252. case TOR_TLS_DONE:
  253. return END_OR_CONN_REASON_DONE;
  254. default:
  255. return END_OR_CONN_REASON_MISC;
  256. }
  257. }
  258. /** Given an errno from a failed ORConn connection, return a reason code
  259. * appropriate for use in the controller orconn events. */
  260. int
  261. errno_to_orconn_end_reason(int e)
  262. {
  263. switch (e) {
  264. case EPIPE:
  265. return END_OR_CONN_REASON_DONE;
  266. S_CASE(ENOTCONN):
  267. S_CASE(ENETUNREACH):
  268. S_CASE(ENETDOWN):
  269. S_CASE(EHOSTUNREACH):
  270. return END_OR_CONN_REASON_NO_ROUTE;
  271. S_CASE(ECONNREFUSED):
  272. return END_OR_CONN_REASON_REFUSED;
  273. S_CASE(ECONNRESET):
  274. return END_OR_CONN_REASON_CONNRESET;
  275. S_CASE(ETIMEDOUT):
  276. return END_OR_CONN_REASON_TIMEOUT;
  277. S_CASE(ENOBUFS):
  278. case ENOMEM:
  279. case ENFILE:
  280. E_CASE(EMFILE):
  281. E_CASE(EACCES):
  282. E_CASE(EBADF):
  283. E_CASE(EFAULT):
  284. E_CASE(EINVAL):
  285. return END_OR_CONN_REASON_RESOURCE_LIMIT;
  286. default:
  287. log_info(LD_OR, "Didn't recognize errno %d (%s).",
  288. e, tor_socket_strerror(e));
  289. return END_OR_CONN_REASON_MISC;
  290. }
  291. }
  292. /***************************** Circuit reasons *****************************/
  293. /** Convert a numeric reason for destroying a circuit into a string for a
  294. * CIRCUIT event. */
  295. const char *
  296. circuit_end_reason_to_control_string(int reason)
  297. {
  298. int is_remote = 0;
  299. if (reason >= 0 && reason & END_CIRC_REASON_FLAG_REMOTE) {
  300. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  301. is_remote = 1;
  302. }
  303. switch (reason) {
  304. case END_CIRC_AT_ORIGIN:
  305. /* This shouldn't get passed here; it's a catch-all reason. */
  306. return "ORIGIN";
  307. case END_CIRC_REASON_NONE:
  308. /* This shouldn't get passed here; it's a catch-all reason. */
  309. return "NONE";
  310. case END_CIRC_REASON_TORPROTOCOL:
  311. return "TORPROTOCOL";
  312. case END_CIRC_REASON_INTERNAL:
  313. return "INTERNAL";
  314. case END_CIRC_REASON_REQUESTED:
  315. return "REQUESTED";
  316. case END_CIRC_REASON_HIBERNATING:
  317. return "HIBERNATING";
  318. case END_CIRC_REASON_RESOURCELIMIT:
  319. return "RESOURCELIMIT";
  320. case END_CIRC_REASON_CONNECTFAILED:
  321. return "CONNECTFAILED";
  322. case END_CIRC_REASON_OR_IDENTITY:
  323. return "OR_IDENTITY";
  324. case END_CIRC_REASON_CHANNEL_CLOSED:
  325. return "CHANNEL_CLOSED";
  326. case END_CIRC_REASON_FINISHED:
  327. return "FINISHED";
  328. case END_CIRC_REASON_TIMEOUT:
  329. return "TIMEOUT";
  330. case END_CIRC_REASON_DESTROYED:
  331. return "DESTROYED";
  332. case END_CIRC_REASON_NOPATH:
  333. return "NOPATH";
  334. case END_CIRC_REASON_NOSUCHSERVICE:
  335. return "NOSUCHSERVICE";
  336. case END_CIRC_REASON_MEASUREMENT_EXPIRED:
  337. return "MEASUREMENT_EXPIRED";
  338. case END_CIRC_REASON_IP_NOW_REDUNDANT:
  339. return "IP_NOW_REDUNDANT";
  340. default:
  341. if (is_remote) {
  342. /*
  343. * If it's remote, it's not a bug *here*, so don't use LD_BUG, but
  344. * do note that the someone we're talking to is speaking the Tor
  345. * protocol with a weird accent.
  346. */
  347. log_warn(LD_PROTOCOL,
  348. "Remote server sent bogus reason code %d", reason);
  349. } else {
  350. log_warn(LD_BUG,
  351. "Unrecognized reason code %d", reason);
  352. }
  353. return NULL;
  354. }
  355. }
  356. /** Return a string corresponding to a SOCKS4 response code. */
  357. const char *
  358. socks4_response_code_to_string(uint8_t code)
  359. {
  360. switch (code) {
  361. case 0x5a:
  362. return "connection accepted";
  363. case 0x5b:
  364. return "server rejected connection";
  365. case 0x5c:
  366. return "server cannot connect to identd on this client";
  367. case 0x5d:
  368. return "user id does not match identd";
  369. default:
  370. return "invalid SOCKS 4 response code";
  371. }
  372. }
  373. /** Return a string corresponding to a SOCKS5 response code. */
  374. const char *
  375. socks5_response_code_to_string(uint8_t code)
  376. {
  377. switch (code) {
  378. case 0x00:
  379. return "connection accepted";
  380. case 0x01:
  381. return "general SOCKS server failure";
  382. case 0x02:
  383. return "connection not allowed by ruleset";
  384. case 0x03:
  385. return "Network unreachable";
  386. case 0x04:
  387. return "Host unreachable";
  388. case 0x05:
  389. return "Connection refused";
  390. case 0x06:
  391. return "TTL expired";
  392. case 0x07:
  393. return "Command not supported";
  394. case 0x08:
  395. return "Address type not supported";
  396. default:
  397. return "unknown reason";
  398. }
  399. }
  400. /** Return a string corresponding to a bandwidht_weight_rule_t */
  401. const char *
  402. bandwidth_weight_rule_to_string(bandwidth_weight_rule_t rule)
  403. {
  404. switch (rule)
  405. {
  406. case NO_WEIGHTING:
  407. return "no weighting";
  408. case WEIGHT_FOR_EXIT:
  409. return "weight as exit";
  410. case WEIGHT_FOR_MID:
  411. return "weight as middle node";
  412. case WEIGHT_FOR_GUARD:
  413. return "weight as guard";
  414. case WEIGHT_FOR_DIR:
  415. return "weight as directory";
  416. default:
  417. return "unknown rule";
  418. }
  419. }