reasons.c 16 KB

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