|
@@ -4,8 +4,9 @@
|
|
|
|
|
|
* \file hs_circuitmap.c
|
|
|
*
|
|
|
- * \brief Manage the hidden service circuitmap: A hash table that maps binary
|
|
|
- * tokens to introduction and rendezvous circuits.
|
|
|
+ * \brief Hidden service circuitmap: A hash table that maps binary tokens to
|
|
|
+ * introduction and rendezvous circuits; it's used both by relays acting as
|
|
|
+ * intro points and rendezvous points, and also by hidden services themselves.
|
|
|
**/
|
|
|
|
|
|
#define HS_CIRCUITMAP_PRIVATE
|
|
@@ -25,8 +26,8 @@ static struct hs_circuitmap_ht *the_hs_circuitmap = NULL;
|
|
|
|
|
|
* two circuits have the same HS token. */
|
|
|
static int
|
|
|
-hs_circuits_have_same_token(const or_circuit_t *first_circuit,
|
|
|
- const or_circuit_t *second_circuit)
|
|
|
+hs_circuits_have_same_token(const circuit_t *first_circuit,
|
|
|
+ const circuit_t *second_circuit)
|
|
|
{
|
|
|
const hs_token_t *first_token;
|
|
|
const hs_token_t *second_token;
|
|
@@ -57,7 +58,7 @@ hs_circuits_have_same_token(const or_circuit_t *first_circuit,
|
|
|
|
|
|
* HS token into an unsigned int for use as a key by the hash table routines.*/
|
|
|
static inline unsigned int
|
|
|
-hs_circuit_hash_token(const or_circuit_t *circuit)
|
|
|
+hs_circuit_hash_token(const circuit_t *circuit)
|
|
|
{
|
|
|
tor_assert(circuit->hs_token);
|
|
|
|
|
@@ -67,11 +68,11 @@ hs_circuit_hash_token(const or_circuit_t *circuit)
|
|
|
|
|
|
|
|
|
HT_PROTOTYPE(hs_circuitmap_ht,
|
|
|
- or_circuit_t,
|
|
|
+ circuit_t,
|
|
|
hs_circuitmap_node,
|
|
|
hs_circuit_hash_token, hs_circuits_have_same_token)
|
|
|
|
|
|
-HT_GENERATE2(hs_circuitmap_ht, or_circuit_t, hs_circuitmap_node,
|
|
|
+HT_GENERATE2(hs_circuitmap_ht, circuit_t, hs_circuitmap_node,
|
|
|
hs_circuit_hash_token, hs_circuits_have_same_token,
|
|
|
0.6, tor_reallocarray, tor_free_)
|
|
|
|
|
@@ -116,13 +117,13 @@ hs_token_free(hs_token_t *hs_token)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static or_circuit_t *
|
|
|
+static circuit_t *
|
|
|
get_circuit_with_token(hs_token_t *search_token)
|
|
|
{
|
|
|
tor_assert(the_hs_circuitmap);
|
|
|
|
|
|
|
|
|
- or_circuit_t search_circ;
|
|
|
+ circuit_t search_circ;
|
|
|
search_circ.hs_token = search_token;
|
|
|
return HT_FIND(hs_circuitmap_ht, the_hs_circuitmap, &search_circ);
|
|
|
}
|
|
@@ -130,7 +131,7 @@ get_circuit_with_token(hs_token_t *search_token)
|
|
|
|
|
|
circuitmap. This function steals reference of <b>token</b>. */
|
|
|
static void
|
|
|
-hs_circuitmap_register_impl(or_circuit_t *circ, hs_token_t *token)
|
|
|
+hs_circuitmap_register_impl(circuit_t *circ, hs_token_t *token)
|
|
|
{
|
|
|
tor_assert(circ);
|
|
|
tor_assert(token);
|
|
@@ -145,13 +146,12 @@ hs_circuitmap_register_impl(or_circuit_t *circ, hs_token_t *token)
|
|
|
take precedence over old ones, so that HSes and clients and reestablish
|
|
|
killed circuits without changing the HS token. */
|
|
|
{
|
|
|
- or_circuit_t *found_circ;
|
|
|
+ circuit_t *found_circ;
|
|
|
found_circ = get_circuit_with_token(token);
|
|
|
if (found_circ) {
|
|
|
hs_circuitmap_remove_circuit(found_circ);
|
|
|
- if (!found_circ->base_.marked_for_close) {
|
|
|
- circuit_mark_for_close(TO_CIRCUIT(found_circ),
|
|
|
- END_CIRC_REASON_FINISHED);
|
|
|
+ if (!found_circ->marked_for_close) {
|
|
|
+ circuit_mark_for_close(found_circ, END_CIRC_REASON_FINISHED);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -165,7 +165,7 @@ hs_circuitmap_register_impl(or_circuit_t *circ, hs_token_t *token)
|
|
|
* circuitmap. Use the HS <b>token</b> as the key to the hash table. If
|
|
|
* <b>token</b> is not set, clear the circuit of any HS tokens. */
|
|
|
static void
|
|
|
-hs_circuitmap_register_circuit(or_circuit_t *circ,
|
|
|
+hs_circuitmap_register_circuit(circuit_t *circ,
|
|
|
hs_token_type_t type, size_t token_len,
|
|
|
const uint8_t *token)
|
|
|
{
|
|
@@ -178,17 +178,19 @@ hs_circuitmap_register_circuit(or_circuit_t *circ,
|
|
|
hs_circuitmap_register_impl(circ, hs_token);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * Only returns a circuit with purpose equal to the <b>wanted_circ_purpose</b>
|
|
|
- * parameter and if it is NOT marked for close. Return NULL if no such circuit
|
|
|
- * is found. */
|
|
|
-static or_circuit_t *
|
|
|
-hs_circuitmap_get_circuit(hs_token_type_t type,
|
|
|
- size_t token_len,
|
|
|
- const uint8_t *token,
|
|
|
- uint8_t wanted_circ_purpose)
|
|
|
+
|
|
|
+ * hs_circuitmap_get_or_circuit(). Because only circuit_t are indexed in the
|
|
|
+ * circuitmap, this function returns object type so the specialized functions
|
|
|
+ * using this helper can upcast it to the right type.
|
|
|
+ *
|
|
|
+ * Return NULL if not such circuit is found. */
|
|
|
+static circuit_t *
|
|
|
+hs_circuitmap_get_circuit_impl(hs_token_type_t type,
|
|
|
+ size_t token_len,
|
|
|
+ const uint8_t *token,
|
|
|
+ uint8_t wanted_circ_purpose)
|
|
|
{
|
|
|
- or_circuit_t *found_circ = NULL;
|
|
|
+ circuit_t *found_circ = NULL;
|
|
|
|
|
|
tor_assert(the_hs_circuitmap);
|
|
|
|
|
@@ -202,87 +204,247 @@ hs_circuitmap_get_circuit(hs_token_type_t type,
|
|
|
|
|
|
|
|
|
if (!found_circ ||
|
|
|
- found_circ->base_.purpose != wanted_circ_purpose ||
|
|
|
- found_circ->base_.marked_for_close) {
|
|
|
+ found_circ->purpose != wanted_circ_purpose ||
|
|
|
+ found_circ->marked_for_close) {
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
return found_circ;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ * size <b>token_len</b> and <b>type</b>. Only returns a circuit with purpose
|
|
|
+ * equal to the <b>wanted_circ_purpose</b> parameter and if it is NOT marked
|
|
|
+ * for close. Return NULL if no such circuit is found. */
|
|
|
+static origin_circuit_t *
|
|
|
+hs_circuitmap_get_origin_circuit(hs_token_type_t type,
|
|
|
+ size_t token_len,
|
|
|
+ const uint8_t *token,
|
|
|
+ uint8_t wanted_circ_purpose)
|
|
|
+{
|
|
|
+ circuit_t *circ;
|
|
|
+ tor_assert(token);
|
|
|
+ tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(wanted_circ_purpose));
|
|
|
|
|
|
-
|
|
|
- * NULL if no such circuit is found in the circuitmap. */
|
|
|
-or_circuit_t *
|
|
|
-hs_circuitmap_get_intro_circ_v3(const ed25519_public_key_t *auth_key)
|
|
|
+ circ = hs_circuitmap_get_circuit_impl(type, token_len, token,
|
|
|
+ wanted_circ_purpose);
|
|
|
+ if (!circ) {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
|
|
+ return TO_ORIGIN_CIRCUIT(circ);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * <b>token_len</b> and <b>type</b>. Only returns a circuit with purpose equal
|
|
|
+ * to the <b>wanted_circ_purpose</b> parameter and if it is NOT marked for
|
|
|
+ * close. Return NULL if no such circuit is found. */
|
|
|
+static or_circuit_t *
|
|
|
+hs_circuitmap_get_or_circuit(hs_token_type_t type,
|
|
|
+ size_t token_len,
|
|
|
+ const uint8_t *token,
|
|
|
+ uint8_t wanted_circ_purpose)
|
|
|
{
|
|
|
- tor_assert(auth_key);
|
|
|
+ circuit_t *circ;
|
|
|
+ tor_assert(token);
|
|
|
+ tor_assert(!CIRCUIT_PURPOSE_IS_ORIGIN(wanted_circ_purpose));
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_circuit_impl(type, token_len, token,
|
|
|
+ wanted_circ_purpose);
|
|
|
+ if (!circ) {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
|
|
|
- return hs_circuitmap_get_circuit(HS_TOKEN_INTRO_V3,
|
|
|
- ED25519_PUBKEY_LEN, auth_key->pubkey,
|
|
|
- CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
+ tor_assert(CIRCUIT_IS_ORCIRC(circ));
|
|
|
+ return TO_OR_CIRCUIT(circ);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * NULL if no such circuit is found in the circuitmap. */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * <b>auth_key</b>. Return NULL if no such circuit is found in the
|
|
|
+ * circuitmap. */
|
|
|
or_circuit_t *
|
|
|
-hs_circuitmap_get_intro_circ_v2(const uint8_t *digest)
|
|
|
+hs_circuitmap_get_intro_circ_v3_relay_side(
|
|
|
+ const ed25519_public_key_t *auth_key)
|
|
|
{
|
|
|
- tor_assert(digest);
|
|
|
+ return hs_circuitmap_get_or_circuit(HS_TOKEN_INTRO_V3_RELAY_SIDE,
|
|
|
+ ED25519_PUBKEY_LEN, auth_key->pubkey,
|
|
|
+ CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
+}
|
|
|
|
|
|
- return hs_circuitmap_get_circuit(HS_TOKEN_INTRO_V2,
|
|
|
- REND_TOKEN_LEN, digest,
|
|
|
- CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
+
|
|
|
+ * <b>digest</b>. Return NULL if no such circuit is found in the circuitmap. */
|
|
|
+or_circuit_t *
|
|
|
+hs_circuitmap_get_intro_circ_v2_relay_side(const uint8_t *digest)
|
|
|
+{
|
|
|
+ return hs_circuitmap_get_or_circuit(HS_TOKEN_INTRO_V2_RELAY_SIDE,
|
|
|
+ REND_TOKEN_LEN, digest,
|
|
|
+ CIRCUIT_PURPOSE_INTRO_POINT);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* <b>cookie</b>. Return NULL if no such circuit is found in the circuitmap. */
|
|
|
or_circuit_t *
|
|
|
-hs_circuitmap_get_rend_circ(const uint8_t *cookie)
|
|
|
+hs_circuitmap_get_rend_circ_relay_side(const uint8_t *cookie)
|
|
|
{
|
|
|
- tor_assert(cookie);
|
|
|
-
|
|
|
- return hs_circuitmap_get_circuit(HS_TOKEN_REND,
|
|
|
- REND_TOKEN_LEN, cookie,
|
|
|
- CIRCUIT_PURPOSE_REND_POINT_WAITING);
|
|
|
+ return hs_circuitmap_get_or_circuit(HS_TOKEN_REND_RELAY_SIDE,
|
|
|
+ REND_TOKEN_LEN, cookie,
|
|
|
+ CIRCUIT_PURPOSE_REND_POINT_WAITING);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
|
|
|
* circuitmap. */
|
|
|
void
|
|
|
-hs_circuitmap_register_rend_circ(or_circuit_t *circ, const uint8_t *cookie)
|
|
|
+hs_circuitmap_register_rend_circ_relay_side(or_circuit_t *circ,
|
|
|
+ const uint8_t *cookie)
|
|
|
{
|
|
|
- hs_circuitmap_register_circuit(circ,
|
|
|
- HS_TOKEN_REND,
|
|
|
+ hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
|
|
|
+ HS_TOKEN_REND_RELAY_SIDE,
|
|
|
REND_TOKEN_LEN, cookie);
|
|
|
}
|
|
|
+
|
|
|
+ * circuitmap. */
|
|
|
+void
|
|
|
+hs_circuitmap_register_intro_circ_v2_relay_side(or_circuit_t *circ,
|
|
|
+ const uint8_t *digest)
|
|
|
+{
|
|
|
+ hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
|
|
|
+ HS_TOKEN_INTRO_V2_RELAY_SIDE,
|
|
|
+ REND_TOKEN_LEN, digest);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * circuitmap. */
|
|
|
+void
|
|
|
+hs_circuitmap_register_intro_circ_v3_relay_side(or_circuit_t *circ,
|
|
|
+ const ed25519_public_key_t *auth_key)
|
|
|
+{
|
|
|
+ hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
|
|
|
+ HS_TOKEN_INTRO_V3_RELAY_SIDE,
|
|
|
+ ED25519_PUBKEY_LEN, auth_key->pubkey);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * originating from this hidden service. Return NULL if no such circuit is
|
|
|
+ * found in the circuitmap. */
|
|
|
+origin_circuit_t *
|
|
|
+hs_circuitmap_get_intro_circ_v3_service_side(const
|
|
|
+ ed25519_public_key_t *auth_key)
|
|
|
+{
|
|
|
+ origin_circuit_t *circ = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_origin_circuit(HS_TOKEN_INTRO_V3_SERVICE_SIDE,
|
|
|
+ ED25519_PUBKEY_LEN, auth_key->pubkey,
|
|
|
+ CIRCUIT_PURPOSE_S_INTRO);
|
|
|
+ if (circ) {
|
|
|
+ return circ;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_origin_circuit(HS_TOKEN_INTRO_V3_SERVICE_SIDE,
|
|
|
+ ED25519_PUBKEY_LEN, auth_key->pubkey,
|
|
|
+ CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
|
|
|
+
|
|
|
+ return circ;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * service with <b>digest</b>. Return NULL if no such circuit is found in the
|
|
|
+ * circuitmap. */
|
|
|
+origin_circuit_t *
|
|
|
+hs_circuitmap_get_intro_circ_v2_service_side(const uint8_t *digest)
|
|
|
+{
|
|
|
+ origin_circuit_t *circ = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_origin_circuit(HS_TOKEN_INTRO_V2_SERVICE_SIDE,
|
|
|
+ REND_TOKEN_LEN, digest,
|
|
|
+ CIRCUIT_PURPOSE_S_INTRO);
|
|
|
+ if (circ) {
|
|
|
+ return circ;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_origin_circuit(HS_TOKEN_INTRO_V2_SERVICE_SIDE,
|
|
|
+ REND_TOKEN_LEN, digest,
|
|
|
+ CIRCUIT_PURPOSE_S_ESTABLISH_INTRO);
|
|
|
+
|
|
|
+ return circ;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * service with rendezvous <b>cookie</b>. Return NULL if no such circuit is
|
|
|
+ * found in the circuitmap. */
|
|
|
+origin_circuit_t *
|
|
|
+hs_circuitmap_get_rend_circ_service_side(const uint8_t *cookie)
|
|
|
+{
|
|
|
+ origin_circuit_t *circ = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_origin_circuit(HS_TOKEN_REND_SERVICE_SIDE,
|
|
|
+ REND_TOKEN_LEN, cookie,
|
|
|
+ CIRCUIT_PURPOSE_S_CONNECT_REND);
|
|
|
+ if (circ) {
|
|
|
+ return circ;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ circ = hs_circuitmap_get_origin_circuit(HS_TOKEN_REND_SERVICE_SIDE,
|
|
|
+ REND_TOKEN_LEN, cookie,
|
|
|
+ CIRCUIT_PURPOSE_S_REND_JOINED);
|
|
|
+ return circ;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
* circuitmap. */
|
|
|
void
|
|
|
-hs_circuitmap_register_intro_circ_v2(or_circuit_t *circ, const uint8_t *digest)
|
|
|
+hs_circuitmap_register_intro_circ_v2_service_side(origin_circuit_t *circ,
|
|
|
+ const uint8_t *digest)
|
|
|
{
|
|
|
- hs_circuitmap_register_circuit(circ,
|
|
|
- HS_TOKEN_INTRO_V2,
|
|
|
+ hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
|
|
|
+ HS_TOKEN_INTRO_V2_SERVICE_SIDE,
|
|
|
REND_TOKEN_LEN, digest);
|
|
|
}
|
|
|
|
|
|
|
|
|
* circuitmap. */
|
|
|
void
|
|
|
-hs_circuitmap_register_intro_circ_v3(or_circuit_t *circ,
|
|
|
- const ed25519_public_key_t *auth_key)
|
|
|
+hs_circuitmap_register_intro_circ_v3_service_side(origin_circuit_t *circ,
|
|
|
+ const ed25519_public_key_t *auth_key)
|
|
|
{
|
|
|
- hs_circuitmap_register_circuit(circ,
|
|
|
- HS_TOKEN_INTRO_V3,
|
|
|
+ hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
|
|
|
+ HS_TOKEN_INTRO_V3_SERVICE_SIDE,
|
|
|
ED25519_PUBKEY_LEN, auth_key->pubkey);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * it from the hashtable. */
|
|
|
+
|
|
|
+ * circuitmap. */
|
|
|
+void
|
|
|
+hs_circuitmap_register_rend_circ_service_side(origin_circuit_t *circ,
|
|
|
+ const uint8_t *cookie)
|
|
|
+{
|
|
|
+ hs_circuitmap_register_circuit(TO_CIRCUIT(circ),
|
|
|
+ HS_TOKEN_REND_SERVICE_SIDE,
|
|
|
+ REND_TOKEN_LEN, cookie);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * token, and remove it from the hashtable. */
|
|
|
void
|
|
|
-hs_circuitmap_remove_circuit(or_circuit_t *circ)
|
|
|
+hs_circuitmap_remove_circuit(circuit_t *circ)
|
|
|
{
|
|
|
tor_assert(the_hs_circuitmap);
|
|
|
|
|
@@ -291,14 +453,14 @@ hs_circuitmap_remove_circuit(or_circuit_t *circ)
|
|
|
}
|
|
|
|
|
|
|
|
|
- or_circuit_t *tmp;
|
|
|
+ circuit_t *tmp;
|
|
|
tmp = HT_REMOVE(hs_circuitmap_ht, the_hs_circuitmap, circ);
|
|
|
|
|
|
if (tmp) {
|
|
|
tor_assert(tmp == circ);
|
|
|
} else {
|
|
|
log_warn(LD_BUG, "Could not find circuit (%u) in circuitmap.",
|
|
|
- circ->p_circ_id);
|
|
|
+ circ->n_circ_id);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -306,7 +468,7 @@ hs_circuitmap_remove_circuit(or_circuit_t *circ)
|
|
|
circ->hs_token = NULL;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
void
|
|
|
hs_circuitmap_init(void)
|
|
|
{
|
|
@@ -316,7 +478,7 @@ hs_circuitmap_init(void)
|
|
|
HT_INIT(hs_circuitmap_ht, the_hs_circuitmap);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
void
|
|
|
hs_circuitmap_free_all(void)
|
|
|
{
|