1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2021, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Convert circuit, stream, and orconn error reasons to and/or from
8 * strings and errno values.
10 * This module is just a bunch of functions full of case statements that
11 * convert from one representation of our error codes to another. These are
12 * mainly used in generating log messages, in sending messages to the
13 * controller in control.c, and in converting errors from one protocol layer
17 #include "core/or/or.h"
18 #include "app/config/config.h"
19 #include "core/or/reasons.h"
20 #include "feature/nodelist/node_select.h"
21 #include "lib/tls/tortls.h"
23 /***************************** Edge (stream) reasons **********************/
25 /** Convert the reason for ending a stream <b>reason</b> into the format used
26 * in STREAM events. Return NULL if the reason is unrecognized. */
28 stream_end_reason_to_control_string(int reason
)
30 reason
&= END_STREAM_REASON_MASK
;
32 case END_STREAM_REASON_MISC
: return "MISC";
33 case END_STREAM_REASON_RESOLVEFAILED
: return "RESOLVEFAILED";
34 case END_STREAM_REASON_CONNECTREFUSED
: return "CONNECTREFUSED";
35 case END_STREAM_REASON_EXITPOLICY
: return "EXITPOLICY";
36 case END_STREAM_REASON_DESTROY
: return "DESTROY";
37 case END_STREAM_REASON_DONE
: return "DONE";
38 case END_STREAM_REASON_TIMEOUT
: return "TIMEOUT";
39 case END_STREAM_REASON_NOROUTE
: return "NOROUTE";
40 case END_STREAM_REASON_HIBERNATING
: return "HIBERNATING";
41 case END_STREAM_REASON_INTERNAL
: return "INTERNAL";
42 case END_STREAM_REASON_RESOURCELIMIT
: return "RESOURCELIMIT";
43 case END_STREAM_REASON_CONNRESET
: return "CONNRESET";
44 case END_STREAM_REASON_TORPROTOCOL
: return "TORPROTOCOL";
45 case END_STREAM_REASON_NOTDIRECTORY
: return "NOTDIRECTORY";
47 case END_STREAM_REASON_CANT_ATTACH
: return "CANT_ATTACH";
48 case END_STREAM_REASON_NET_UNREACHABLE
: return "NET_UNREACHABLE";
49 case END_STREAM_REASON_SOCKSPROTOCOL
: return "SOCKS_PROTOCOL";
51 case END_STREAM_REASON_HTTPPROTOCOL
: return "HTTP_PROTOCOL";
53 case END_STREAM_REASON_PRIVATE_ADDR
: return "PRIVATE_ADDR";
59 /** Translate <b>reason</b>, which came from a relay 'end' cell,
60 * into a static const string describing why the stream is closing.
61 * <b>reason</b> is -1 if no reason was provided.
64 stream_end_reason_to_string(int reason
)
68 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
69 "End cell arrived with length 0. Should be at least 1.");
71 case END_STREAM_REASON_MISC
: return "misc error";
72 case END_STREAM_REASON_RESOLVEFAILED
: return "resolve failed";
73 case END_STREAM_REASON_CONNECTREFUSED
: return "connection refused";
74 case END_STREAM_REASON_EXITPOLICY
: return "exit policy failed";
75 case END_STREAM_REASON_DESTROY
: return "destroyed";
76 case END_STREAM_REASON_DONE
: return "closed normally";
77 case END_STREAM_REASON_TIMEOUT
: return "gave up (timeout)";
78 case END_STREAM_REASON_NOROUTE
: return "no route to host";
79 case END_STREAM_REASON_HIBERNATING
: return "server is hibernating";
80 case END_STREAM_REASON_INTERNAL
: return "internal error at server";
81 case END_STREAM_REASON_RESOURCELIMIT
: return "server out of resources";
82 case END_STREAM_REASON_CONNRESET
: return "connection reset";
83 case END_STREAM_REASON_TORPROTOCOL
: return "Tor protocol error";
84 case END_STREAM_REASON_NOTDIRECTORY
: return "not a directory";
86 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
87 "Reason for ending (%d) not recognized.",reason
);
92 /** Translate <b>reason</b> (as from a relay 'end' cell) into an
93 * appropriate SOCKS5 reply code.
95 * A reason of 0 means that we're not actually expecting to send
96 * this code back to the socks client; we just call it 'succeeded'
97 * to keep things simple.
100 stream_end_reason_to_socks5_response(int reason
)
102 switch (reason
& END_STREAM_REASON_MASK
) {
104 return SOCKS5_SUCCEEDED
;
105 case END_STREAM_REASON_MISC
:
106 return SOCKS5_GENERAL_ERROR
;
107 case END_STREAM_REASON_RESOLVEFAILED
:
108 return SOCKS5_HOST_UNREACHABLE
;
109 case END_STREAM_REASON_CONNECTREFUSED
:
110 return SOCKS5_CONNECTION_REFUSED
;
111 case END_STREAM_REASON_ENTRYPOLICY
:
112 return SOCKS5_NOT_ALLOWED
;
113 case END_STREAM_REASON_EXITPOLICY
:
114 return SOCKS5_NOT_ALLOWED
;
115 case END_STREAM_REASON_DESTROY
:
116 return SOCKS5_GENERAL_ERROR
;
117 case END_STREAM_REASON_DONE
:
118 /* Note that 'DONE' usually indicates a successful close from the other
119 * side of the stream... but if we receive it before a connected cell --
120 * that is, before we have sent a SOCKS reply -- that means that the
121 * other side of the circuit closed the connection before telling us it
123 return SOCKS5_CONNECTION_REFUSED
;
124 case END_STREAM_REASON_TIMEOUT
:
125 return SOCKS5_TTL_EXPIRED
;
126 case END_STREAM_REASON_NOROUTE
:
127 return SOCKS5_HOST_UNREACHABLE
;
128 case END_STREAM_REASON_RESOURCELIMIT
:
129 return SOCKS5_GENERAL_ERROR
;
130 case END_STREAM_REASON_HIBERNATING
:
131 return SOCKS5_GENERAL_ERROR
;
132 case END_STREAM_REASON_INTERNAL
:
133 return SOCKS5_GENERAL_ERROR
;
134 case END_STREAM_REASON_CONNRESET
:
135 return SOCKS5_CONNECTION_REFUSED
;
136 case END_STREAM_REASON_TORPROTOCOL
:
137 return SOCKS5_GENERAL_ERROR
;
139 case END_STREAM_REASON_CANT_ATTACH
:
140 return SOCKS5_GENERAL_ERROR
;
141 case END_STREAM_REASON_NET_UNREACHABLE
:
142 return SOCKS5_NET_UNREACHABLE
;
143 case END_STREAM_REASON_SOCKSPROTOCOL
:
144 return SOCKS5_GENERAL_ERROR
;
145 case END_STREAM_REASON_HTTPPROTOCOL
:
147 tor_assert_nonfatal_unreached();
148 return SOCKS5_GENERAL_ERROR
;
150 case END_STREAM_REASON_PRIVATE_ADDR
:
151 return SOCKS5_GENERAL_ERROR
;
154 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
155 "Reason for ending (%d) not recognized; "
156 "sending generic socks error.", reason
);
157 return SOCKS5_GENERAL_ERROR
;
161 /* We need to use a few macros to deal with the fact that Windows
162 * decided that their sockets interface should be a permakludge.
163 * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
164 * version, and S_CASE is for errors where windows has only a WSAEFOO
165 * version. (The E is for 'error', the S is for 'socket'). */
167 #define E_CASE(s) case s: case WSA ## s
168 #define S_CASE(s) case WSA ## s
170 #define E_CASE(s) case s
171 #define S_CASE(s) case s
172 #endif /* defined(_WIN32) */
174 /** Given an errno from a failed exit connection, return a reason code
175 * appropriate for use in a RELAY END cell. */
177 errno_to_stream_end_reason(int e
)
179 /* To add new errors here, find out if they exist on Windows, and if a WSA*
180 * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
184 return END_STREAM_REASON_DONE
;
190 S_CASE(EPROTONOSUPPORT
):
191 S_CASE(EAFNOSUPPORT
):
193 return END_STREAM_REASON_INTERNAL
;
195 S_CASE(EHOSTUNREACH
):
198 return END_STREAM_REASON_NOROUTE
;
199 S_CASE(ECONNREFUSED
):
200 return END_STREAM_REASON_CONNECTREFUSED
;
202 return END_STREAM_REASON_CONNRESET
;
204 return END_STREAM_REASON_TIMEOUT
;
209 S_CASE(EADDRNOTAVAIL
):
211 return END_STREAM_REASON_RESOURCELIMIT
;
213 log_info(LD_EXIT
, "Didn't recognize errno %d (%s); telling the client "
214 "that we are ending a stream for 'misc' reason.",
215 e
, tor_socket_strerror(e
));
216 return END_STREAM_REASON_MISC
;
220 /***************************** ORConn reasons *****************************/
222 /** Convert the reason for ending an OR connection <b>r</b> into the format
223 * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
225 orconn_end_reason_to_control_string(int r
)
227 /* To add new errors here, find out if they exist on Windows, and if a WSA*
228 * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
231 case END_OR_CONN_REASON_DONE
:
233 case END_OR_CONN_REASON_REFUSED
:
234 return "CONNECTREFUSED";
235 case END_OR_CONN_REASON_OR_IDENTITY
:
237 case END_OR_CONN_REASON_CONNRESET
:
238 return "CONNECTRESET";
239 case END_OR_CONN_REASON_TIMEOUT
:
241 case END_OR_CONN_REASON_NO_ROUTE
:
243 case END_OR_CONN_REASON_IO_ERROR
:
245 case END_OR_CONN_REASON_RESOURCE_LIMIT
:
246 return "RESOURCELIMIT";
247 case END_OR_CONN_REASON_TLS_ERROR
:
249 case END_OR_CONN_REASON_MISC
:
251 case END_OR_CONN_REASON_PT_MISSING
:
256 log_warn(LD_BUG
, "Unrecognized or_conn reason code %d", r
);
261 /** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
263 tls_error_to_orconn_end_reason(int e
)
266 case TOR_TLS_ERROR_IO
:
267 return END_OR_CONN_REASON_IO_ERROR
;
268 case TOR_TLS_ERROR_CONNREFUSED
:
269 return END_OR_CONN_REASON_REFUSED
;
270 case TOR_TLS_ERROR_CONNRESET
:
271 return END_OR_CONN_REASON_CONNRESET
;
272 case TOR_TLS_ERROR_NO_ROUTE
:
273 return END_OR_CONN_REASON_NO_ROUTE
;
274 case TOR_TLS_ERROR_TIMEOUT
:
275 return END_OR_CONN_REASON_TIMEOUT
;
276 case TOR_TLS_WANTREAD
:
277 case TOR_TLS_WANTWRITE
:
280 return END_OR_CONN_REASON_DONE
;
281 case TOR_TLS_ERROR_MISC
:
282 return END_OR_CONN_REASON_TLS_ERROR
;
284 return END_OR_CONN_REASON_MISC
;
288 /** Given an errno from a failed ORConn connection, return a reason code
289 * appropriate for use in the controller orconn events. */
291 errno_to_orconn_end_reason(int e
)
295 return END_OR_CONN_REASON_DONE
;
299 S_CASE(EHOSTUNREACH
):
300 return END_OR_CONN_REASON_NO_ROUTE
;
301 S_CASE(ECONNREFUSED
):
302 return END_OR_CONN_REASON_REFUSED
;
304 return END_OR_CONN_REASON_CONNRESET
;
306 return END_OR_CONN_REASON_TIMEOUT
;
315 return END_OR_CONN_REASON_RESOURCE_LIMIT
;
317 log_info(LD_OR
, "Didn't recognize errno %d (%s).",
318 e
, tor_socket_strerror(e
));
319 return END_OR_CONN_REASON_MISC
;
323 /***************************** Circuit reasons *****************************/
325 /** Convert a numeric reason for destroying a circuit into a string for a
328 circuit_end_reason_to_control_string(int reason
)
332 if (reason
>= 0 && reason
& END_CIRC_REASON_FLAG_REMOTE
) {
333 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
338 case END_CIRC_AT_ORIGIN
:
339 /* This shouldn't get passed here; it's a catch-all reason. */
341 case END_CIRC_REASON_NONE
:
342 /* This shouldn't get passed here; it's a catch-all reason. */
344 case END_CIRC_REASON_TORPROTOCOL
:
345 return "TORPROTOCOL";
346 case END_CIRC_REASON_INTERNAL
:
348 case END_CIRC_REASON_REQUESTED
:
350 case END_CIRC_REASON_HIBERNATING
:
351 return "HIBERNATING";
352 case END_CIRC_REASON_RESOURCELIMIT
:
353 return "RESOURCELIMIT";
354 case END_CIRC_REASON_CONNECTFAILED
:
355 return "CONNECTFAILED";
356 case END_CIRC_REASON_OR_IDENTITY
:
357 return "OR_IDENTITY";
358 case END_CIRC_REASON_CHANNEL_CLOSED
:
359 return "CHANNEL_CLOSED";
360 case END_CIRC_REASON_FINISHED
:
362 case END_CIRC_REASON_TIMEOUT
:
364 case END_CIRC_REASON_DESTROYED
:
366 case END_CIRC_REASON_NOPATH
:
368 case END_CIRC_REASON_NOSUCHSERVICE
:
369 return "NOSUCHSERVICE";
370 case END_CIRC_REASON_MEASUREMENT_EXPIRED
:
371 return "MEASUREMENT_EXPIRED";
372 case END_CIRC_REASON_IP_NOW_REDUNDANT
:
373 return "IP_NOW_REDUNDANT";
377 * If it's remote, it's not a bug *here*, so don't use LD_BUG, but
378 * do note that the someone we're talking to is speaking the Tor
379 * protocol with a weird accent.
381 log_warn(LD_PROTOCOL
,
382 "Remote server sent bogus reason code %d", reason
);
385 "Unrecognized reason code %d", reason
);
391 /** Return a string corresponding to a SOCKS4 response code. */
393 socks4_response_code_to_string(uint8_t code
)
397 return "connection accepted";
399 return "server rejected connection";
401 return "server cannot connect to identd on this client";
403 return "user id does not match identd";
405 return "invalid SOCKS 4 response code";
409 /** Return a string corresponding to a SOCKS5 response code. */
411 socks5_response_code_to_string(uint8_t code
)
415 return "connection accepted";
417 return "general SOCKS server failure";
419 return "connection not allowed by ruleset";
421 return "Network unreachable";
423 return "Host unreachable";
425 return "Connection refused";
427 return "TTL expired";
429 return "Command not supported";
431 return "Address type not supported";
433 return "unknown reason";
437 /** Return a string corresponding to a bandwidth_weight_rule_t */
439 bandwidth_weight_rule_to_string(bandwidth_weight_rule_t rule
)
444 return "no weighting";
445 case WEIGHT_FOR_EXIT
:
446 return "weight as exit";
448 return "weight as middle node";
449 case WEIGHT_FOR_GUARD
:
450 return "weight as guard";
452 return "weight as directory";
454 return "unknown rule";
458 /** Given a RELAY_END reason value, convert it to an HTTP response to be
459 * send over an HTTP tunnel connection. */
461 end_reason_to_http_connect_response_line(int endreason
)
463 endreason
&= END_STREAM_REASON_MASK
;
464 /* XXXX these are probably all wrong. Should they all be 502? */
467 return "HTTP/1.0 200 OK\r\n\r\n";
468 case END_STREAM_REASON_MISC
:
469 return "HTTP/1.0 500 Internal Server Error\r\n\r\n";
470 case END_STREAM_REASON_RESOLVEFAILED
:
471 return "HTTP/1.0 404 Not Found (resolve failed)\r\n\r\n";
472 case END_STREAM_REASON_NOROUTE
:
473 return "HTTP/1.0 404 Not Found (no route)\r\n\r\n";
474 case END_STREAM_REASON_CONNECTREFUSED
:
475 return "HTTP/1.0 403 Forbidden (connection refused)\r\n\r\n";
476 case END_STREAM_REASON_EXITPOLICY
:
477 return "HTTP/1.0 403 Forbidden (exit policy)\r\n\r\n";
478 case END_STREAM_REASON_DESTROY
:
479 return "HTTP/1.0 502 Bad Gateway (destroy cell received)\r\n\r\n";
480 case END_STREAM_REASON_DONE
:
481 return "HTTP/1.0 502 Bad Gateway (unexpected close)\r\n\r\n";
482 case END_STREAM_REASON_TIMEOUT
:
483 return "HTTP/1.0 504 Gateway Timeout\r\n\r\n";
484 case END_STREAM_REASON_HIBERNATING
:
485 return "HTTP/1.0 502 Bad Gateway (hibernating server)\r\n\r\n";
486 case END_STREAM_REASON_INTERNAL
:
487 return "HTTP/1.0 502 Bad Gateway (internal error)\r\n\r\n";
488 case END_STREAM_REASON_RESOURCELIMIT
:
489 return "HTTP/1.0 502 Bad Gateway (resource limit)\r\n\r\n";
490 case END_STREAM_REASON_CONNRESET
:
491 return "HTTP/1.0 403 Forbidden (connection reset)\r\n\r\n";
492 case END_STREAM_REASON_TORPROTOCOL
:
493 return "HTTP/1.0 502 Bad Gateway (tor protocol violation)\r\n\r\n";
494 case END_STREAM_REASON_ENTRYPOLICY
:
495 return "HTTP/1.0 403 Forbidden (entry policy violation)\r\n\r\n";
496 case END_STREAM_REASON_NOTDIRECTORY
: FALLTHROUGH
;
498 tor_assert_nonfatal_unreached();
499 return "HTTP/1.0 500 Internal Server Error (weird end reason)\r\n\r\n";