reasons.c 13 KB

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