1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2017, 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
21 /***************************** Edge (stream) reasons **********************/
23 /** Convert the reason for ending a stream <b>reason</b> into the format used
24 * in STREAM events. Return NULL if the reason is unrecognized. */
26 stream_end_reason_to_control_string(int reason
)
28 reason
&= END_STREAM_REASON_MASK
;
30 case END_STREAM_REASON_MISC
: return "MISC";
31 case END_STREAM_REASON_RESOLVEFAILED
: return "RESOLVEFAILED";
32 case END_STREAM_REASON_CONNECTREFUSED
: return "CONNECTREFUSED";
33 case END_STREAM_REASON_EXITPOLICY
: return "EXITPOLICY";
34 case END_STREAM_REASON_DESTROY
: return "DESTROY";
35 case END_STREAM_REASON_DONE
: return "DONE";
36 case END_STREAM_REASON_TIMEOUT
: return "TIMEOUT";
37 case END_STREAM_REASON_NOROUTE
: return "NOROUTE";
38 case END_STREAM_REASON_HIBERNATING
: return "HIBERNATING";
39 case END_STREAM_REASON_INTERNAL
: return "INTERNAL";
40 case END_STREAM_REASON_RESOURCELIMIT
: return "RESOURCELIMIT";
41 case END_STREAM_REASON_CONNRESET
: return "CONNRESET";
42 case END_STREAM_REASON_TORPROTOCOL
: return "TORPROTOCOL";
43 case END_STREAM_REASON_NOTDIRECTORY
: return "NOTDIRECTORY";
45 case END_STREAM_REASON_CANT_ATTACH
: return "CANT_ATTACH";
46 case END_STREAM_REASON_NET_UNREACHABLE
: return "NET_UNREACHABLE";
47 case END_STREAM_REASON_SOCKSPROTOCOL
: return "SOCKS_PROTOCOL";
49 case END_STREAM_REASON_HTTPPROTOCOL
: return "HTTP_PROTOCOL";
51 case END_STREAM_REASON_PRIVATE_ADDR
: return "PRIVATE_ADDR";
57 /** Translate <b>reason</b>, which came from a relay 'end' cell,
58 * into a static const string describing why the stream is closing.
59 * <b>reason</b> is -1 if no reason was provided.
62 stream_end_reason_to_string(int reason
)
66 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
67 "End cell arrived with length 0. Should be at least 1.");
69 case END_STREAM_REASON_MISC
: return "misc error";
70 case END_STREAM_REASON_RESOLVEFAILED
: return "resolve failed";
71 case END_STREAM_REASON_CONNECTREFUSED
: return "connection refused";
72 case END_STREAM_REASON_EXITPOLICY
: return "exit policy failed";
73 case END_STREAM_REASON_DESTROY
: return "destroyed";
74 case END_STREAM_REASON_DONE
: return "closed normally";
75 case END_STREAM_REASON_TIMEOUT
: return "gave up (timeout)";
76 case END_STREAM_REASON_NOROUTE
: return "no route to host";
77 case END_STREAM_REASON_HIBERNATING
: return "server is hibernating";
78 case END_STREAM_REASON_INTERNAL
: return "internal error at server";
79 case END_STREAM_REASON_RESOURCELIMIT
: return "server out of resources";
80 case END_STREAM_REASON_CONNRESET
: return "connection reset";
81 case END_STREAM_REASON_TORPROTOCOL
: return "Tor protocol error";
82 case END_STREAM_REASON_NOTDIRECTORY
: return "not a directory";
84 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
85 "Reason for ending (%d) not recognized.",reason
);
90 /** Translate <b>reason</b> (as from a relay 'end' cell) into an
91 * appropriate SOCKS5 reply code.
93 * A reason of 0 means that we're not actually expecting to send
94 * this code back to the socks client; we just call it 'succeeded'
95 * to keep things simple.
98 stream_end_reason_to_socks5_response(int reason
)
100 switch (reason
& END_STREAM_REASON_MASK
) {
102 return SOCKS5_SUCCEEDED
;
103 case END_STREAM_REASON_MISC
:
104 return SOCKS5_GENERAL_ERROR
;
105 case END_STREAM_REASON_RESOLVEFAILED
:
106 return SOCKS5_HOST_UNREACHABLE
;
107 case END_STREAM_REASON_CONNECTREFUSED
:
108 return SOCKS5_CONNECTION_REFUSED
;
109 case END_STREAM_REASON_ENTRYPOLICY
:
110 return SOCKS5_NOT_ALLOWED
;
111 case END_STREAM_REASON_EXITPOLICY
:
112 return SOCKS5_NOT_ALLOWED
;
113 case END_STREAM_REASON_DESTROY
:
114 return SOCKS5_GENERAL_ERROR
;
115 case END_STREAM_REASON_DONE
:
116 /* Note that 'DONE' usually indicates a successful close from the other
117 * side of the stream... but if we receive it before a connected cell --
118 * that is, before we have sent a SOCKS reply -- that means that the
119 * other side of the circuit closed the connection before telling us it
121 return SOCKS5_CONNECTION_REFUSED
;
122 case END_STREAM_REASON_TIMEOUT
:
123 return SOCKS5_TTL_EXPIRED
;
124 case END_STREAM_REASON_NOROUTE
:
125 return SOCKS5_HOST_UNREACHABLE
;
126 case END_STREAM_REASON_RESOURCELIMIT
:
127 return SOCKS5_GENERAL_ERROR
;
128 case END_STREAM_REASON_HIBERNATING
:
129 return SOCKS5_GENERAL_ERROR
;
130 case END_STREAM_REASON_INTERNAL
:
131 return SOCKS5_GENERAL_ERROR
;
132 case END_STREAM_REASON_CONNRESET
:
133 return SOCKS5_CONNECTION_REFUSED
;
134 case END_STREAM_REASON_TORPROTOCOL
:
135 return SOCKS5_GENERAL_ERROR
;
137 case END_STREAM_REASON_CANT_ATTACH
:
138 return SOCKS5_GENERAL_ERROR
;
139 case END_STREAM_REASON_NET_UNREACHABLE
:
140 return SOCKS5_NET_UNREACHABLE
;
141 case END_STREAM_REASON_SOCKSPROTOCOL
:
142 return SOCKS5_GENERAL_ERROR
;
143 case END_STREAM_REASON_HTTPPROTOCOL
:
145 tor_assert_nonfatal_unreached();
146 return SOCKS5_GENERAL_ERROR
;
148 case END_STREAM_REASON_PRIVATE_ADDR
:
149 return SOCKS5_GENERAL_ERROR
;
152 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
153 "Reason for ending (%d) not recognized; "
154 "sending generic socks error.", reason
);
155 return SOCKS5_GENERAL_ERROR
;
159 /* We need to use a few macros to deal with the fact that Windows
160 * decided that their sockets interface should be a permakludge.
161 * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
162 * version, and S_CASE is for errors where windows has only a WSAEFOO
163 * version. (The E is for 'error', the S is for 'socket'). */
165 #define E_CASE(s) case s: case WSA ## s
166 #define S_CASE(s) case WSA ## s
168 #define E_CASE(s) case s
169 #define S_CASE(s) case s
170 #endif /* defined(_WIN32) */
172 /** Given an errno from a failed exit connection, return a reason code
173 * appropriate for use in a RELAY END cell. */
175 errno_to_stream_end_reason(int e
)
177 /* To add new errors here, find out if they exist on Windows, and if a WSA*
178 * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
182 return END_STREAM_REASON_DONE
;
188 S_CASE(EPROTONOSUPPORT
):
189 S_CASE(EAFNOSUPPORT
):
191 return END_STREAM_REASON_INTERNAL
;
193 S_CASE(EHOSTUNREACH
):
196 return END_STREAM_REASON_NOROUTE
;
197 S_CASE(ECONNREFUSED
):
198 return END_STREAM_REASON_CONNECTREFUSED
;
200 return END_STREAM_REASON_CONNRESET
;
202 return END_STREAM_REASON_TIMEOUT
;
207 S_CASE(EADDRNOTAVAIL
):
209 return END_STREAM_REASON_RESOURCELIMIT
;
211 log_info(LD_EXIT
, "Didn't recognize errno %d (%s); telling the client "
212 "that we are ending a stream for 'misc' reason.",
213 e
, tor_socket_strerror(e
));
214 return END_STREAM_REASON_MISC
;
218 /***************************** ORConn reasons *****************************/
220 /** Convert the reason for ending an OR connection <b>r</b> into the format
221 * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
223 orconn_end_reason_to_control_string(int r
)
225 /* To add new errors here, find out if they exist on Windows, and if a WSA*
226 * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
229 case END_OR_CONN_REASON_DONE
:
231 case END_OR_CONN_REASON_REFUSED
:
232 return "CONNECTREFUSED";
233 case END_OR_CONN_REASON_OR_IDENTITY
:
235 case END_OR_CONN_REASON_CONNRESET
:
236 return "CONNECTRESET";
237 case END_OR_CONN_REASON_TIMEOUT
:
239 case END_OR_CONN_REASON_NO_ROUTE
:
241 case END_OR_CONN_REASON_IO_ERROR
:
243 case END_OR_CONN_REASON_RESOURCE_LIMIT
:
244 return "RESOURCELIMIT";
245 case END_OR_CONN_REASON_MISC
:
247 case END_OR_CONN_REASON_PT_MISSING
:
252 log_warn(LD_BUG
, "Unrecognized or_conn reason code %d", r
);
257 /** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
259 tls_error_to_orconn_end_reason(int e
)
262 case TOR_TLS_ERROR_IO
:
263 return END_OR_CONN_REASON_IO_ERROR
;
264 case TOR_TLS_ERROR_CONNREFUSED
:
265 return END_OR_CONN_REASON_REFUSED
;
266 case TOR_TLS_ERROR_CONNRESET
:
267 return END_OR_CONN_REASON_CONNRESET
;
268 case TOR_TLS_ERROR_NO_ROUTE
:
269 return END_OR_CONN_REASON_NO_ROUTE
;
270 case TOR_TLS_ERROR_TIMEOUT
:
271 return END_OR_CONN_REASON_TIMEOUT
;
272 case TOR_TLS_WANTREAD
:
273 case TOR_TLS_WANTWRITE
:
276 return END_OR_CONN_REASON_DONE
;
278 return END_OR_CONN_REASON_MISC
;
282 /** Given an errno from a failed ORConn connection, return a reason code
283 * appropriate for use in the controller orconn events. */
285 errno_to_orconn_end_reason(int e
)
289 return END_OR_CONN_REASON_DONE
;
293 S_CASE(EHOSTUNREACH
):
294 return END_OR_CONN_REASON_NO_ROUTE
;
295 S_CASE(ECONNREFUSED
):
296 return END_OR_CONN_REASON_REFUSED
;
298 return END_OR_CONN_REASON_CONNRESET
;
300 return END_OR_CONN_REASON_TIMEOUT
;
309 return END_OR_CONN_REASON_RESOURCE_LIMIT
;
311 log_info(LD_OR
, "Didn't recognize errno %d (%s).",
312 e
, tor_socket_strerror(e
));
313 return END_OR_CONN_REASON_MISC
;
317 /***************************** Circuit reasons *****************************/
319 /** Convert a numeric reason for destroying a circuit into a string for a
322 circuit_end_reason_to_control_string(int reason
)
326 if (reason
>= 0 && reason
& END_CIRC_REASON_FLAG_REMOTE
) {
327 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
332 case END_CIRC_AT_ORIGIN
:
333 /* This shouldn't get passed here; it's a catch-all reason. */
335 case END_CIRC_REASON_NONE
:
336 /* This shouldn't get passed here; it's a catch-all reason. */
338 case END_CIRC_REASON_TORPROTOCOL
:
339 return "TORPROTOCOL";
340 case END_CIRC_REASON_INTERNAL
:
342 case END_CIRC_REASON_REQUESTED
:
344 case END_CIRC_REASON_HIBERNATING
:
345 return "HIBERNATING";
346 case END_CIRC_REASON_RESOURCELIMIT
:
347 return "RESOURCELIMIT";
348 case END_CIRC_REASON_CONNECTFAILED
:
349 return "CONNECTFAILED";
350 case END_CIRC_REASON_OR_IDENTITY
:
351 return "OR_IDENTITY";
352 case END_CIRC_REASON_CHANNEL_CLOSED
:
353 return "CHANNEL_CLOSED";
354 case END_CIRC_REASON_FINISHED
:
356 case END_CIRC_REASON_TIMEOUT
:
358 case END_CIRC_REASON_DESTROYED
:
360 case END_CIRC_REASON_NOPATH
:
362 case END_CIRC_REASON_NOSUCHSERVICE
:
363 return "NOSUCHSERVICE";
364 case END_CIRC_REASON_MEASUREMENT_EXPIRED
:
365 return "MEASUREMENT_EXPIRED";
366 case END_CIRC_REASON_IP_NOW_REDUNDANT
:
367 return "IP_NOW_REDUNDANT";
371 * If it's remote, it's not a bug *here*, so don't use LD_BUG, but
372 * do note that the someone we're talking to is speaking the Tor
373 * protocol with a weird accent.
375 log_warn(LD_PROTOCOL
,
376 "Remote server sent bogus reason code %d", reason
);
379 "Unrecognized reason code %d", reason
);
385 /** Return a string corresponding to a SOCKS4 response code. */
387 socks4_response_code_to_string(uint8_t code
)
391 return "connection accepted";
393 return "server rejected connection";
395 return "server cannot connect to identd on this client";
397 return "user id does not match identd";
399 return "invalid SOCKS 4 response code";
403 /** Return a string corresponding to a SOCKS5 response code. */
405 socks5_response_code_to_string(uint8_t code
)
409 return "connection accepted";
411 return "general SOCKS server failure";
413 return "connection not allowed by ruleset";
415 return "Network unreachable";
417 return "Host unreachable";
419 return "Connection refused";
421 return "TTL expired";
423 return "Command not supported";
425 return "Address type not supported";
427 return "unknown reason";
431 /** Return a string corresponding to a bandwidth_weight_rule_t */
433 bandwidth_weight_rule_to_string(bandwidth_weight_rule_t rule
)
438 return "no weighting";
439 case WEIGHT_FOR_EXIT
:
440 return "weight as exit";
442 return "weight as middle node";
443 case WEIGHT_FOR_GUARD
:
444 return "weight as guard";
446 return "weight as directory";
448 return "unknown rule";
452 /** Given a RELAY_END reason value, convert it to an HTTP response to be
453 * send over an HTTP tunnel connection. */
455 end_reason_to_http_connect_response_line(int endreason
)
457 endreason
&= END_STREAM_REASON_MASK
;
458 /* XXXX these are probably all wrong. Should they all be 502? */
461 return "HTTP/1.0 200 OK\r\n\r\n";
462 case END_STREAM_REASON_MISC
:
463 return "HTTP/1.0 500 Internal Server Error\r\n\r\n";
464 case END_STREAM_REASON_RESOLVEFAILED
:
465 return "HTTP/1.0 404 Not Found (resolve failed)\r\n\r\n";
466 case END_STREAM_REASON_NOROUTE
:
467 return "HTTP/1.0 404 Not Found (no route)\r\n\r\n";
468 case END_STREAM_REASON_CONNECTREFUSED
:
469 return "HTTP/1.0 403 Forbidden (connection refused)\r\n\r\n";
470 case END_STREAM_REASON_EXITPOLICY
:
471 return "HTTP/1.0 403 Forbidden (exit policy)\r\n\r\n";
472 case END_STREAM_REASON_DESTROY
:
473 return "HTTP/1.0 502 Bad Gateway (destroy cell received)\r\n\r\n";
474 case END_STREAM_REASON_DONE
:
475 return "HTTP/1.0 502 Bad Gateway (unexpected close)\r\n\r\n";
476 case END_STREAM_REASON_TIMEOUT
:
477 return "HTTP/1.0 504 Gateway Timeout\r\n\r\n";
478 case END_STREAM_REASON_HIBERNATING
:
479 return "HTTP/1.0 502 Bad Gateway (hibernating server)\r\n\r\n";
480 case END_STREAM_REASON_INTERNAL
:
481 return "HTTP/1.0 502 Bad Gateway (internal error)\r\n\r\n";
482 case END_STREAM_REASON_RESOURCELIMIT
:
483 return "HTTP/1.0 502 Bad Gateway (resource limit)\r\n\r\n";
484 case END_STREAM_REASON_CONNRESET
:
485 return "HTTP/1.0 403 Forbidden (connection reset)\r\n\r\n";
486 case END_STREAM_REASON_TORPROTOCOL
:
487 return "HTTP/1.0 502 Bad Gateway (tor protocol violation)\r\n\r\n";
488 case END_STREAM_REASON_ENTRYPOLICY
:
489 return "HTTP/1.0 403 Forbidden (entry policy violation)\r\n\r\n";
490 case END_STREAM_REASON_NOTDIRECTORY
: /* Fall Through */
492 tor_assert_nonfatal_unreached();
493 return "HTTP/1.0 500 Internal Server Error (weird end reason)\r\n\r\n";