reasons.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  2. * Copyright (c) 2007-2013, 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. /* Note that 'DONE' usually indicates a successful close from the other
  100. * side of the stream... but if we receive it before a connected cell --
  101. * that is, before we have sent a SOCKS reply -- that means that the
  102. * other side of the circuit closed the connection before telling us it
  103. * was complete. */
  104. return SOCKS5_CONNECTION_REFUSED;
  105. case END_STREAM_REASON_TIMEOUT:
  106. return SOCKS5_TTL_EXPIRED;
  107. case END_STREAM_REASON_NOROUTE:
  108. return SOCKS5_HOST_UNREACHABLE;
  109. case END_STREAM_REASON_RESOURCELIMIT:
  110. return SOCKS5_GENERAL_ERROR;
  111. case END_STREAM_REASON_HIBERNATING:
  112. return SOCKS5_GENERAL_ERROR;
  113. case END_STREAM_REASON_INTERNAL:
  114. return SOCKS5_GENERAL_ERROR;
  115. case END_STREAM_REASON_CONNRESET:
  116. return SOCKS5_CONNECTION_REFUSED;
  117. case END_STREAM_REASON_TORPROTOCOL:
  118. return SOCKS5_GENERAL_ERROR;
  119. case END_STREAM_REASON_CANT_ATTACH:
  120. return SOCKS5_GENERAL_ERROR;
  121. case END_STREAM_REASON_NET_UNREACHABLE:
  122. return SOCKS5_NET_UNREACHABLE;
  123. case END_STREAM_REASON_SOCKSPROTOCOL:
  124. return SOCKS5_GENERAL_ERROR;
  125. case END_STREAM_REASON_PRIVATE_ADDR:
  126. return SOCKS5_GENERAL_ERROR;
  127. default:
  128. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  129. "Reason for ending (%d) not recognized; "
  130. "sending generic socks error.", reason);
  131. return SOCKS5_GENERAL_ERROR;
  132. }
  133. }
  134. /* We need to use a few macros to deal with the fact that Windows
  135. * decided that their sockets interface should be a permakludge.
  136. * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
  137. * version, and S_CASE is for errors where windows has only a WSAEFOO
  138. * version. (The E is for 'error', the S is for 'socket'). */
  139. #ifdef _WIN32
  140. #define E_CASE(s) case s: case WSA ## s
  141. #define S_CASE(s) case WSA ## s
  142. #else
  143. #define E_CASE(s) case s
  144. #define S_CASE(s) case s
  145. #endif
  146. /** Given an errno from a failed exit connection, return a reason code
  147. * appropriate for use in a RELAY END cell. */
  148. uint8_t
  149. errno_to_stream_end_reason(int e)
  150. {
  151. /* To add new errors here, find out if they exist on Windows, and if a WSA*
  152. * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
  153. * appropriate. */
  154. switch (e) {
  155. case EPIPE:
  156. return END_STREAM_REASON_DONE;
  157. E_CASE(EBADF):
  158. E_CASE(EFAULT):
  159. E_CASE(EINVAL):
  160. S_CASE(EISCONN):
  161. S_CASE(ENOTSOCK):
  162. S_CASE(EPROTONOSUPPORT):
  163. S_CASE(EAFNOSUPPORT):
  164. S_CASE(ENOTCONN):
  165. return END_STREAM_REASON_INTERNAL;
  166. S_CASE(ENETUNREACH):
  167. S_CASE(EHOSTUNREACH):
  168. E_CASE(EACCES):
  169. case EPERM:
  170. return END_STREAM_REASON_NOROUTE;
  171. S_CASE(ECONNREFUSED):
  172. return END_STREAM_REASON_CONNECTREFUSED;
  173. S_CASE(ECONNRESET):
  174. return END_STREAM_REASON_CONNRESET;
  175. S_CASE(ETIMEDOUT):
  176. return END_STREAM_REASON_TIMEOUT;
  177. S_CASE(ENOBUFS):
  178. case ENOMEM:
  179. case ENFILE:
  180. S_CASE(EADDRINUSE):
  181. S_CASE(EADDRNOTAVAIL):
  182. E_CASE(EMFILE):
  183. return END_STREAM_REASON_RESOURCELIMIT;
  184. default:
  185. log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
  186. "that we are ending a stream for 'misc' reason.",
  187. e, tor_socket_strerror(e));
  188. return END_STREAM_REASON_MISC;
  189. }
  190. }
  191. /***************************** ORConn reasons *****************************/
  192. /** Convert the reason for ending an OR connection <b>r</b> into the format
  193. * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
  194. const char *
  195. orconn_end_reason_to_control_string(int r)
  196. {
  197. /* To add new errors here, find out if they exist on Windows, and if a WSA*
  198. * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
  199. * appropriate. */
  200. switch (r) {
  201. case END_OR_CONN_REASON_DONE:
  202. return "DONE";
  203. case END_OR_CONN_REASON_REFUSED:
  204. return "CONNECTREFUSED";
  205. case END_OR_CONN_REASON_OR_IDENTITY:
  206. return "IDENTITY";
  207. case END_OR_CONN_REASON_CONNRESET:
  208. return "CONNECTRESET";
  209. case END_OR_CONN_REASON_TIMEOUT:
  210. return "TIMEOUT";
  211. case END_OR_CONN_REASON_NO_ROUTE:
  212. return "NOROUTE";
  213. case END_OR_CONN_REASON_IO_ERROR:
  214. return "IOERROR";
  215. case END_OR_CONN_REASON_RESOURCE_LIMIT:
  216. return "RESOURCELIMIT";
  217. case END_OR_CONN_REASON_MISC:
  218. return "MISC";
  219. case END_OR_CONN_REASON_PT_MISSING:
  220. return "PT_MISSING";
  221. case 0:
  222. return "";
  223. default:
  224. log_warn(LD_BUG, "Unrecognized or_conn reason code %d", r);
  225. return "UNKNOWN";
  226. }
  227. }
  228. /** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
  229. int
  230. tls_error_to_orconn_end_reason(int e)
  231. {
  232. switch (e) {
  233. case TOR_TLS_ERROR_IO:
  234. return END_OR_CONN_REASON_IO_ERROR;
  235. case TOR_TLS_ERROR_CONNREFUSED:
  236. return END_OR_CONN_REASON_REFUSED;
  237. case TOR_TLS_ERROR_CONNRESET:
  238. return END_OR_CONN_REASON_CONNRESET;
  239. case TOR_TLS_ERROR_NO_ROUTE:
  240. return END_OR_CONN_REASON_NO_ROUTE;
  241. case TOR_TLS_ERROR_TIMEOUT:
  242. return END_OR_CONN_REASON_TIMEOUT;
  243. case TOR_TLS_WANTREAD:
  244. case TOR_TLS_WANTWRITE:
  245. case TOR_TLS_CLOSE:
  246. case TOR_TLS_DONE:
  247. return END_OR_CONN_REASON_DONE;
  248. default:
  249. return END_OR_CONN_REASON_MISC;
  250. }
  251. }
  252. /** Given an errno from a failed ORConn connection, return a reason code
  253. * appropriate for use in the controller orconn events. */
  254. int
  255. errno_to_orconn_end_reason(int e)
  256. {
  257. switch (e) {
  258. case EPIPE:
  259. return END_OR_CONN_REASON_DONE;
  260. S_CASE(ENOTCONN):
  261. S_CASE(ENETUNREACH):
  262. S_CASE(ENETDOWN):
  263. S_CASE(EHOSTUNREACH):
  264. return END_OR_CONN_REASON_NO_ROUTE;
  265. S_CASE(ECONNREFUSED):
  266. return END_OR_CONN_REASON_REFUSED;
  267. S_CASE(ECONNRESET):
  268. return END_OR_CONN_REASON_CONNRESET;
  269. S_CASE(ETIMEDOUT):
  270. return END_OR_CONN_REASON_TIMEOUT;
  271. S_CASE(ENOBUFS):
  272. case ENOMEM:
  273. case ENFILE:
  274. E_CASE(EMFILE):
  275. E_CASE(EACCES):
  276. E_CASE(EBADF):
  277. E_CASE(EFAULT):
  278. E_CASE(EINVAL):
  279. return END_OR_CONN_REASON_RESOURCE_LIMIT;
  280. default:
  281. log_info(LD_OR, "Didn't recognize errno %d (%s).",
  282. e, tor_socket_strerror(e));
  283. return END_OR_CONN_REASON_MISC;
  284. }
  285. }
  286. /***************************** Circuit reasons *****************************/
  287. /** Convert a numeric reason for destroying a circuit into a string for a
  288. * CIRCUIT event. */
  289. const char *
  290. circuit_end_reason_to_control_string(int reason)
  291. {
  292. int is_remote = 0;
  293. if (reason >= 0 && reason & END_CIRC_REASON_FLAG_REMOTE) {
  294. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  295. is_remote = 1;
  296. }
  297. switch (reason) {
  298. case END_CIRC_AT_ORIGIN:
  299. /* This shouldn't get passed here; it's a catch-all reason. */
  300. return "ORIGIN";
  301. case END_CIRC_REASON_NONE:
  302. /* This shouldn't get passed here; it's a catch-all reason. */
  303. return "NONE";
  304. case END_CIRC_REASON_TORPROTOCOL:
  305. return "TORPROTOCOL";
  306. case END_CIRC_REASON_INTERNAL:
  307. return "INTERNAL";
  308. case END_CIRC_REASON_REQUESTED:
  309. return "REQUESTED";
  310. case END_CIRC_REASON_HIBERNATING:
  311. return "HIBERNATING";
  312. case END_CIRC_REASON_RESOURCELIMIT:
  313. return "RESOURCELIMIT";
  314. case END_CIRC_REASON_CONNECTFAILED:
  315. return "CONNECTFAILED";
  316. case END_CIRC_REASON_OR_IDENTITY:
  317. return "OR_IDENTITY";
  318. case END_CIRC_REASON_CHANNEL_CLOSED:
  319. return "CHANNEL_CLOSED";
  320. case END_CIRC_REASON_FINISHED:
  321. return "FINISHED";
  322. case END_CIRC_REASON_TIMEOUT:
  323. return "TIMEOUT";
  324. case END_CIRC_REASON_DESTROYED:
  325. return "DESTROYED";
  326. case END_CIRC_REASON_NOPATH:
  327. return "NOPATH";
  328. case END_CIRC_REASON_NOSUCHSERVICE:
  329. return "NOSUCHSERVICE";
  330. case END_CIRC_REASON_MEASUREMENT_EXPIRED:
  331. return "MEASUREMENT_EXPIRED";
  332. default:
  333. if (is_remote) {
  334. /*
  335. * If it's remote, it's not a bug *here*, so don't use LD_BUG, but
  336. * do note that the someone we're talking to is speaking the Tor
  337. * protocol with a weird accent.
  338. */
  339. log_warn(LD_PROTOCOL,
  340. "Remote server sent bogus reason code %d", reason);
  341. } else {
  342. log_warn(LD_BUG,
  343. "Unrecognized reason code %d", reason);
  344. }
  345. return NULL;
  346. }
  347. }
  348. /** Return a string corresponding to a SOCKS4 reponse code. */
  349. const char *
  350. socks4_response_code_to_string(uint8_t code)
  351. {
  352. switch (code) {
  353. case 0x5a:
  354. return "connection accepted";
  355. case 0x5b:
  356. return "server rejected connection";
  357. case 0x5c:
  358. return "server cannot connect to identd on this client";
  359. case 0x5d:
  360. return "user id does not match identd";
  361. default:
  362. return "invalid SOCKS 4 response code";
  363. }
  364. }
  365. /** Return a string corresponding to a SOCKS5 reponse code. */
  366. const char *
  367. socks5_response_code_to_string(uint8_t code)
  368. {
  369. switch (code) {
  370. case 0x00:
  371. return "connection accepted";
  372. case 0x01:
  373. return "general SOCKS server failure";
  374. case 0x02:
  375. return "connection not allowed by ruleset";
  376. case 0x03:
  377. return "Network unreachable";
  378. case 0x04:
  379. return "Host unreachable";
  380. case 0x05:
  381. return "Connection refused";
  382. case 0x06:
  383. return "TTL expired";
  384. case 0x07:
  385. return "Command not supported";
  386. case 0x08:
  387. return "Address type not supported";
  388. default:
  389. return "unknown reason";
  390. }
  391. }
  392. /** Return a string corresponding to a bandwidht_weight_rule_t */
  393. const char *
  394. bandwidth_weight_rule_to_string(bandwidth_weight_rule_t rule)
  395. {
  396. switch (rule)
  397. {
  398. case NO_WEIGHTING:
  399. return "no weighting";
  400. case WEIGHT_FOR_EXIT:
  401. return "weight as exit";
  402. case WEIGHT_FOR_MID:
  403. return "weight as middle node";
  404. case WEIGHT_FOR_GUARD:
  405. return "weight as guard";
  406. case WEIGHT_FOR_DIR:
  407. return "weight as directory";
  408. default:
  409. return "unknown rule";
  410. }
  411. }