reasons.c 16 KB

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