Bug 24946: Fix a warning message caused by a missed purpose check.
[tor.git] / src / or / hs_circuit.c
blob2a41c1cccf54caa42fb7de4c786eebcd50631568
1 /* Copyright (c) 2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file hs_circuit.c
6 **/
8 #define HS_CIRCUIT_PRIVATE
10 #include "or.h"
11 #include "circpathbias.h"
12 #include "circuitbuild.h"
13 #include "circuitlist.h"
14 #include "circuituse.h"
15 #include "config.h"
16 #include "nodelist.h"
17 #include "policies.h"
18 #include "relay.h"
19 #include "rendservice.h"
20 #include "rephist.h"
21 #include "router.h"
23 #include "hs_cell.h"
24 #include "hs_ident.h"
25 #include "hs_ntor.h"
26 #include "hs_service.h"
27 #include "hs_circuit.h"
29 /* Trunnel. */
30 #include "ed25519_cert.h"
31 #include "hs/cell_common.h"
32 #include "hs/cell_establish_intro.h"
34 /* A circuit is about to become an e2e rendezvous circuit. Check
35 * <b>circ_purpose</b> and ensure that it's properly set. Return true iff
36 * circuit purpose is properly set, otherwise return false. */
37 static int
38 circuit_purpose_is_correct_for_rend(unsigned int circ_purpose,
39 int is_service_side)
41 if (is_service_side) {
42 if (circ_purpose != CIRCUIT_PURPOSE_S_CONNECT_REND) {
43 log_warn(LD_BUG,
44 "HS e2e circuit setup with wrong purpose (%d)", circ_purpose);
45 return 0;
49 if (!is_service_side) {
50 if (circ_purpose != CIRCUIT_PURPOSE_C_REND_READY &&
51 circ_purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
52 log_warn(LD_BUG,
53 "Client e2e circuit setup with wrong purpose (%d)", circ_purpose);
54 return 0;
58 return 1;
61 /* Create and return a crypt path for the final hop of a v3 prop224 rendezvous
62 * circuit. Initialize the crypt path crypto using the output material from the
63 * ntor key exchange at <b>ntor_key_seed</b>.
65 * If <b>is_service_side</b> is set, we are the hidden service and the final
66 * hop of the rendezvous circuit is the client on the other side. */
67 static crypt_path_t *
68 create_rend_cpath(const uint8_t *ntor_key_seed, size_t seed_len,
69 int is_service_side)
71 uint8_t keys[HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN];
72 crypt_path_t *cpath = NULL;
74 /* Do the key expansion */
75 if (hs_ntor_circuit_key_expansion(ntor_key_seed, seed_len,
76 keys, sizeof(keys)) < 0) {
77 goto err;
80 /* Setup the cpath */
81 cpath = tor_malloc_zero(sizeof(crypt_path_t));
82 cpath->magic = CRYPT_PATH_MAGIC;
84 if (circuit_init_cpath_crypto(cpath, (char*)keys, sizeof(keys),
85 is_service_side, 1) < 0) {
86 tor_free(cpath);
87 goto err;
90 err:
91 memwipe(keys, 0, sizeof(keys));
92 return cpath;
95 /* We are a v2 legacy HS client: Create and return a crypt path for the hidden
96 * service on the other side of the rendezvous circuit <b>circ</b>. Initialize
97 * the crypt path crypto using the body of the RENDEZVOUS1 cell at
98 * <b>rend_cell_body</b> (which must be at least DH_KEY_LEN+DIGEST_LEN bytes).
100 static crypt_path_t *
101 create_rend_cpath_legacy(origin_circuit_t *circ, const uint8_t *rend_cell_body)
103 crypt_path_t *hop = NULL;
104 char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
106 /* first DH_KEY_LEN bytes are g^y from the service. Finish the dh
107 * handshake...*/
108 tor_assert(circ->build_state);
109 tor_assert(circ->build_state->pending_final_cpath);
110 hop = circ->build_state->pending_final_cpath;
112 tor_assert(hop->rend_dh_handshake_state);
113 if (crypto_dh_compute_secret(LOG_PROTOCOL_WARN, hop->rend_dh_handshake_state,
114 (char*)rend_cell_body, DH_KEY_LEN,
115 keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN)<0) {
116 log_warn(LD_GENERAL, "Couldn't complete DH handshake.");
117 goto err;
119 /* ... and set up cpath. */
120 if (circuit_init_cpath_crypto(hop,
121 keys+DIGEST_LEN, sizeof(keys)-DIGEST_LEN,
122 0, 0) < 0)
123 goto err;
125 /* Check whether the digest is right... */
126 if (tor_memneq(keys, rend_cell_body+DH_KEY_LEN, DIGEST_LEN)) {
127 log_warn(LD_PROTOCOL, "Incorrect digest of key material.");
128 goto err;
131 /* clean up the crypto stuff we just made */
132 crypto_dh_free(hop->rend_dh_handshake_state);
133 hop->rend_dh_handshake_state = NULL;
135 goto done;
137 err:
138 hop = NULL;
140 done:
141 memwipe(keys, 0, sizeof(keys));
142 return hop;
145 /* Append the final <b>hop</b> to the cpath of the rend <b>circ</b>, and mark
146 * <b>circ</b> ready for use to transfer HS relay cells. */
147 static void
148 finalize_rend_circuit(origin_circuit_t *circ, crypt_path_t *hop,
149 int is_service_side)
151 tor_assert(circ);
152 tor_assert(hop);
154 /* Notify the circuit state machine that we are splicing this circuit */
155 int new_circ_purpose = is_service_side ?
156 CIRCUIT_PURPOSE_S_REND_JOINED : CIRCUIT_PURPOSE_C_REND_JOINED;
157 circuit_change_purpose(TO_CIRCUIT(circ), new_circ_purpose);
159 /* All is well. Extend the circuit. */
160 hop->state = CPATH_STATE_OPEN;
161 /* Set the windows to default. */
162 hop->package_window = circuit_initial_package_window();
163 hop->deliver_window = CIRCWINDOW_START;
165 /* Now that this circuit has finished connecting to its destination,
166 * make sure circuit_get_open_circ_or_launch is willing to return it
167 * so we can actually use it. */
168 circ->hs_circ_has_timed_out = 0;
170 /* Append the hop to the cpath of this circuit */
171 onion_append_to_cpath(&circ->cpath, hop);
173 /* In legacy code, 'pending_final_cpath' points to the final hop we just
174 * appended to the cpath. We set the original pointer to NULL so that we
175 * don't double free it. */
176 if (circ->build_state) {
177 circ->build_state->pending_final_cpath = NULL;
180 /* Finally, mark circuit as ready to be used for client streams */
181 if (!is_service_side) {
182 circuit_try_attaching_streams(circ);
186 /* For a given circuit and a service introduction point object, register the
187 * intro circuit to the circuitmap. This supports legacy intro point. */
188 static void
189 register_intro_circ(const hs_service_intro_point_t *ip,
190 origin_circuit_t *circ)
192 tor_assert(ip);
193 tor_assert(circ);
195 if (ip->base.is_only_legacy) {
196 uint8_t digest[DIGEST_LEN];
197 if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
198 return;
200 hs_circuitmap_register_intro_circ_v2_service_side(circ, digest);
201 } else {
202 hs_circuitmap_register_intro_circ_v3_service_side(circ,
203 &ip->auth_key_kp.pubkey);
207 /* Return the number of opened introduction circuit for the given circuit that
208 * is matching its identity key. */
209 static unsigned int
210 count_opened_desc_intro_point_circuits(const hs_service_t *service,
211 const hs_service_descriptor_t *desc)
213 unsigned int count = 0;
215 tor_assert(service);
216 tor_assert(desc);
218 DIGEST256MAP_FOREACH(desc->intro_points.map, key,
219 const hs_service_intro_point_t *, ip) {
220 const circuit_t *circ;
221 const origin_circuit_t *ocirc = hs_circ_service_get_intro_circ(ip);
222 if (ocirc == NULL) {
223 continue;
225 circ = TO_CIRCUIT(ocirc);
226 tor_assert(circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
227 circ->purpose == CIRCUIT_PURPOSE_S_INTRO);
228 /* Having a circuit not for the requested service is really bad. */
229 tor_assert(ed25519_pubkey_eq(&service->keys.identity_pk,
230 &ocirc->hs_ident->identity_pk));
231 /* Only count opened circuit and skip circuit that will be closed. */
232 if (!circ->marked_for_close && circ->state == CIRCUIT_STATE_OPEN) {
233 count++;
235 } DIGEST256MAP_FOREACH_END;
236 return count;
239 /* From a given service, rendezvous cookie and handshake info, create a
240 * rendezvous point circuit identifier. This can't fail. */
241 STATIC hs_ident_circuit_t *
242 create_rp_circuit_identifier(const hs_service_t *service,
243 const uint8_t *rendezvous_cookie,
244 const curve25519_public_key_t *server_pk,
245 const hs_ntor_rend_cell_keys_t *keys)
247 hs_ident_circuit_t *ident;
248 uint8_t handshake_info[CURVE25519_PUBKEY_LEN + DIGEST256_LEN];
250 tor_assert(service);
251 tor_assert(rendezvous_cookie);
252 tor_assert(server_pk);
253 tor_assert(keys);
255 ident = hs_ident_circuit_new(&service->keys.identity_pk,
256 HS_IDENT_CIRCUIT_RENDEZVOUS);
257 /* Copy the RENDEZVOUS_COOKIE which is the unique identifier. */
258 memcpy(ident->rendezvous_cookie, rendezvous_cookie,
259 sizeof(ident->rendezvous_cookie));
260 /* Build the HANDSHAKE_INFO which looks like this:
261 * SERVER_PK [32 bytes]
262 * AUTH_INPUT_MAC [32 bytes]
264 memcpy(handshake_info, server_pk->public_key, CURVE25519_PUBKEY_LEN);
265 memcpy(handshake_info + CURVE25519_PUBKEY_LEN, keys->rend_cell_auth_mac,
266 DIGEST256_LEN);
267 tor_assert(sizeof(ident->rendezvous_handshake_info) ==
268 sizeof(handshake_info));
269 memcpy(ident->rendezvous_handshake_info, handshake_info,
270 sizeof(ident->rendezvous_handshake_info));
271 /* Finally copy the NTOR_KEY_SEED for e2e encryption on the circuit. */
272 tor_assert(sizeof(ident->rendezvous_ntor_key_seed) ==
273 sizeof(keys->ntor_key_seed));
274 memcpy(ident->rendezvous_ntor_key_seed, keys->ntor_key_seed,
275 sizeof(ident->rendezvous_ntor_key_seed));
276 return ident;
279 /* From a given service and service intro point, create an introduction point
280 * circuit identifier. This can't fail. */
281 static hs_ident_circuit_t *
282 create_intro_circuit_identifier(const hs_service_t *service,
283 const hs_service_intro_point_t *ip)
285 hs_ident_circuit_t *ident;
287 tor_assert(service);
288 tor_assert(ip);
290 ident = hs_ident_circuit_new(&service->keys.identity_pk,
291 HS_IDENT_CIRCUIT_INTRO);
292 ed25519_pubkey_copy(&ident->intro_auth_pk, &ip->auth_key_kp.pubkey);
294 return ident;
297 /* For a given introduction point and an introduction circuit, send the
298 * ESTABLISH_INTRO cell. The service object is used for logging. This can fail
299 * and if so, the circuit is closed and the intro point object is flagged
300 * that the circuit is not established anymore which is important for the
301 * retry mechanism. */
302 static void
303 send_establish_intro(const hs_service_t *service,
304 hs_service_intro_point_t *ip, origin_circuit_t *circ)
306 ssize_t cell_len;
307 uint8_t payload[RELAY_PAYLOAD_SIZE];
309 tor_assert(service);
310 tor_assert(ip);
311 tor_assert(circ);
313 /* Encode establish intro cell. */
314 cell_len = hs_cell_build_establish_intro(circ->cpath->prev->rend_circ_nonce,
315 ip, payload);
316 if (cell_len < 0) {
317 log_warn(LD_REND, "Unable to encode ESTABLISH_INTRO cell for service %s "
318 "on circuit %u. Closing circuit.",
319 safe_str_client(service->onion_address),
320 TO_CIRCUIT(circ)->n_circ_id);
321 goto err;
324 /* Send the cell on the circuit. */
325 if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
326 RELAY_COMMAND_ESTABLISH_INTRO,
327 (char *) payload, cell_len,
328 circ->cpath->prev) < 0) {
329 log_info(LD_REND, "Unable to send ESTABLISH_INTRO cell for service %s "
330 "on circuit %u.",
331 safe_str_client(service->onion_address),
332 TO_CIRCUIT(circ)->n_circ_id);
333 /* On error, the circuit has been closed. */
334 goto done;
337 /* Record the attempt to use this circuit. */
338 pathbias_count_use_attempt(circ);
339 goto done;
341 err:
342 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
343 done:
344 memwipe(payload, 0, sizeof(payload));
347 /* Return a string constant describing the anonymity of service. */
348 static const char *
349 get_service_anonymity_string(const hs_service_t *service)
351 if (service->config.is_single_onion) {
352 return "single onion";
353 } else {
354 return "hidden";
358 /* For a given service, the ntor onion key and a rendezvous cookie, launch a
359 * circuit to the rendezvous point specified by the link specifiers. On
360 * success, a circuit identifier is attached to the circuit with the needed
361 * data. This function will try to open a circuit for a maximum value of
362 * MAX_REND_FAILURES then it will give up. */
363 static void
364 launch_rendezvous_point_circuit(const hs_service_t *service,
365 const hs_service_intro_point_t *ip,
366 const hs_cell_introduce2_data_t *data)
368 int circ_needs_uptime;
369 time_t now = time(NULL);
370 extend_info_t *info = NULL;
371 origin_circuit_t *circ;
373 tor_assert(service);
374 tor_assert(ip);
375 tor_assert(data);
377 circ_needs_uptime = hs_service_requires_uptime_circ(service->config.ports);
379 /* Get the extend info data structure for the chosen rendezvous point
380 * specified by the given link specifiers. */
381 info = hs_get_extend_info_from_lspecs(data->link_specifiers,
382 &data->onion_pk,
383 service->config.is_single_onion);
384 if (info == NULL) {
385 /* We are done here, we can't extend to the rendezvous point.
386 * If you're running an IPv6-only v3 single onion service on 0.3.2 or with
387 * 0.3.2 clients, and somehow disable the option check, it will fail here.
389 log_fn(LOG_PROTOCOL_WARN, LD_REND,
390 "Not enough info to open a circuit to a rendezvous point for "
391 "%s service %s.",
392 get_service_anonymity_string(service),
393 safe_str_client(service->onion_address));
394 goto end;
397 for (int i = 0; i < MAX_REND_FAILURES; i++) {
398 int circ_flags = CIRCLAUNCH_NEED_CAPACITY | CIRCLAUNCH_IS_INTERNAL;
399 if (circ_needs_uptime) {
400 circ_flags |= CIRCLAUNCH_NEED_UPTIME;
402 /* Firewall and policies are checked when getting the extend info. */
403 if (service->config.is_single_onion) {
404 circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
407 circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND, info,
408 circ_flags);
409 if (circ != NULL) {
410 /* Stop retrying, we have a circuit! */
411 break;
414 if (circ == NULL) {
415 log_warn(LD_REND, "Giving up on launching a rendezvous circuit to %s "
416 "for %s service %s",
417 safe_str_client(extend_info_describe(info)),
418 get_service_anonymity_string(service),
419 safe_str_client(service->onion_address));
420 goto end;
422 log_info(LD_REND, "Rendezvous circuit launched to %s with cookie %s "
423 "for %s service %s",
424 safe_str_client(extend_info_describe(info)),
425 safe_str_client(hex_str((const char *) data->rendezvous_cookie,
426 REND_COOKIE_LEN)),
427 get_service_anonymity_string(service),
428 safe_str_client(service->onion_address));
429 tor_assert(circ->build_state);
430 /* Rendezvous circuit have a specific timeout for the time spent on trying
431 * to connect to the rendezvous point. */
432 circ->build_state->expiry_time = now + MAX_REND_TIMEOUT;
434 /* Create circuit identifier and key material. */
436 hs_ntor_rend_cell_keys_t keys;
437 curve25519_keypair_t ephemeral_kp;
438 /* No need for extra strong, this is only for this circuit life time. This
439 * key will be used for the RENDEZVOUS1 cell that will be sent on the
440 * circuit once opened. */
441 curve25519_keypair_generate(&ephemeral_kp, 0);
442 if (hs_ntor_service_get_rendezvous1_keys(&ip->auth_key_kp.pubkey,
443 &ip->enc_key_kp,
444 &ephemeral_kp, &data->client_pk,
445 &keys) < 0) {
446 /* This should not really happened but just in case, don't make tor
447 * freak out, close the circuit and move on. */
448 log_info(LD_REND, "Unable to get RENDEZVOUS1 key material for "
449 "service %s",
450 safe_str_client(service->onion_address));
451 circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
452 goto end;
454 circ->hs_ident = create_rp_circuit_identifier(service,
455 data->rendezvous_cookie,
456 &ephemeral_kp.pubkey, &keys);
457 memwipe(&ephemeral_kp, 0, sizeof(ephemeral_kp));
458 memwipe(&keys, 0, sizeof(keys));
459 tor_assert(circ->hs_ident);
462 end:
463 extend_info_free(info);
466 /* Return true iff the given service rendezvous circuit circ is allowed for a
467 * relaunch to the rendezvous point. */
468 static int
469 can_relaunch_service_rendezvous_point(const origin_circuit_t *circ)
471 tor_assert(circ);
472 /* This is initialized when allocating an origin circuit. */
473 tor_assert(circ->build_state);
474 tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
476 /* XXX: Retrying under certain condition. This is related to #22455. */
478 /* Avoid to relaunch twice a circuit to the same rendezvous point at the
479 * same time. */
480 if (circ->hs_service_side_rend_circ_has_been_relaunched) {
481 log_info(LD_REND, "Rendezvous circuit to %s has already been retried. "
482 "Skipping retry.",
483 safe_str_client(
484 extend_info_describe(circ->build_state->chosen_exit)));
485 goto disallow;
488 /* We check failure_count >= hs_get_service_max_rend_failures()-1 below, and
489 * the -1 is because we increment the failure count for our current failure
490 * *after* this clause. */
491 int max_rend_failures = hs_get_service_max_rend_failures() - 1;
493 /* A failure count that has reached maximum allowed or circuit that expired,
494 * we skip relaunching. */
495 if (circ->build_state->failure_count > max_rend_failures ||
496 circ->build_state->expiry_time <= time(NULL)) {
497 log_info(LD_REND, "Attempt to build a rendezvous circuit to %s has "
498 "failed with %d attempts and expiry time %ld. "
499 "Giving up building.",
500 safe_str_client(
501 extend_info_describe(circ->build_state->chosen_exit)),
502 circ->build_state->failure_count,
503 (long int) circ->build_state->expiry_time);
504 goto disallow;
507 /* Allowed to relaunch. */
508 return 1;
509 disallow:
510 return 0;
513 /* Retry the rendezvous point of circ by launching a new circuit to it. */
514 static void
515 retry_service_rendezvous_point(const origin_circuit_t *circ)
517 int flags = 0;
518 origin_circuit_t *new_circ;
519 cpath_build_state_t *bstate;
521 tor_assert(circ);
522 /* This is initialized when allocating an origin circuit. */
523 tor_assert(circ->build_state);
524 tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
526 /* Ease our life. */
527 bstate = circ->build_state;
529 log_info(LD_REND, "Retrying rendezvous point circuit to %s",
530 safe_str_client(extend_info_describe(bstate->chosen_exit)));
532 /* Get the current build state flags for the next circuit. */
533 flags |= (bstate->need_uptime) ? CIRCLAUNCH_NEED_UPTIME : 0;
534 flags |= (bstate->need_capacity) ? CIRCLAUNCH_NEED_CAPACITY : 0;
535 flags |= (bstate->is_internal) ? CIRCLAUNCH_IS_INTERNAL : 0;
537 /* We do NOT add the onehop tunnel flag even though it might be a single
538 * onion service. The reason is that if we failed once to connect to the RP
539 * with a direct connection, we consider that chances are that we will fail
540 * again so try a 3-hop circuit and hope for the best. Because the service
541 * has no anonymity (single onion), this change of behavior won't affect
542 * security directly. */
544 new_circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_CONNECT_REND,
545 bstate->chosen_exit, flags);
546 if (new_circ == NULL) {
547 log_warn(LD_REND, "Failed to launch rendezvous circuit to %s",
548 safe_str_client(extend_info_describe(bstate->chosen_exit)));
549 goto done;
552 /* Transfer build state information to the new circuit state in part to
553 * catch any other failures. */
554 new_circ->build_state->failure_count = bstate->failure_count+1;
555 new_circ->build_state->expiry_time = bstate->expiry_time;
556 new_circ->hs_ident = hs_ident_circuit_dup(circ->hs_ident);
558 done:
559 return;
562 /* Add all possible link specifiers in node to lspecs.
563 * legacy ID is mandatory thus MUST be present in node. If the primary address
564 * is not IPv4, log a BUG() warning, and return an empty smartlist.
565 * Includes ed25519 id and IPv6 link specifiers if present in the node. */
566 static void
567 get_lspecs_from_node(const node_t *node, smartlist_t *lspecs)
569 link_specifier_t *ls;
570 tor_addr_port_t ap;
572 tor_assert(node);
573 tor_assert(lspecs);
575 /* Get the relay's IPv4 address. */
576 node_get_prim_orport(node, &ap);
578 /* We expect the node's primary address to be a valid IPv4 address.
579 * This conforms to the protocol, which requires either an IPv4 or IPv6
580 * address (or both). */
581 if (BUG(!tor_addr_is_v4(&ap.addr)) ||
582 BUG(!tor_addr_port_is_valid_ap(&ap, 0))) {
583 return;
586 ls = link_specifier_new();
587 link_specifier_set_ls_type(ls, LS_IPV4);
588 link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr));
589 link_specifier_set_un_ipv4_port(ls, ap.port);
590 /* Four bytes IPv4 and two bytes port. */
591 link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) +
592 sizeof(ap.port));
593 smartlist_add(lspecs, ls);
595 /* Legacy ID is mandatory and will always be present in node. */
596 ls = link_specifier_new();
597 link_specifier_set_ls_type(ls, LS_LEGACY_ID);
598 memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity,
599 link_specifier_getlen_un_legacy_id(ls));
600 link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls));
601 smartlist_add(lspecs, ls);
603 /* ed25519 ID is only included if the node has it. */
604 if (!ed25519_public_key_is_zero(&node->ed25519_id)) {
605 ls = link_specifier_new();
606 link_specifier_set_ls_type(ls, LS_ED25519_ID);
607 memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id,
608 link_specifier_getlen_un_ed25519_id(ls));
609 link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls));
610 smartlist_add(lspecs, ls);
613 /* Check for IPv6. If so, include it as well. */
614 if (node_has_ipv6_orport(node)) {
615 ls = link_specifier_new();
616 node_get_pref_ipv6_orport(node, &ap);
617 link_specifier_set_ls_type(ls, LS_IPV6);
618 size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls);
619 const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr);
620 uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls);
621 memcpy(ipv6_array, in6_addr, addr_len);
622 link_specifier_set_un_ipv6_port(ls, ap.port);
623 /* Sixteen bytes IPv6 and two bytes port. */
624 link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port));
625 smartlist_add(lspecs, ls);
629 /* Using the given descriptor intro point ip, the node of the
630 * rendezvous point rp_node and the service's subcredential, populate the
631 * already allocated intro1_data object with the needed key material and link
632 * specifiers.
634 * If rp_node has an invalid primary address, intro1_data->link_specifiers
635 * will be an empty list. Otherwise, this function can't fail. The ip
636 * MUST be a valid object containing the needed keys and authentication
637 * method. */
638 static void
639 setup_introduce1_data(const hs_desc_intro_point_t *ip,
640 const node_t *rp_node,
641 const uint8_t *subcredential,
642 hs_cell_introduce1_data_t *intro1_data)
644 smartlist_t *rp_lspecs;
646 tor_assert(ip);
647 tor_assert(rp_node);
648 tor_assert(subcredential);
649 tor_assert(intro1_data);
651 /* Build the link specifiers from the extend information of the rendezvous
652 * circuit that we've picked previously. */
653 rp_lspecs = smartlist_new();
654 get_lspecs_from_node(rp_node, rp_lspecs);
656 /* Populate the introduce1 data object. */
657 memset(intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
658 if (ip->legacy.key != NULL) {
659 intro1_data->is_legacy = 1;
660 intro1_data->legacy_key = ip->legacy.key;
662 intro1_data->auth_pk = &ip->auth_key_cert->signed_key;
663 intro1_data->enc_pk = &ip->enc_key;
664 intro1_data->subcredential = subcredential;
665 intro1_data->onion_pk = node_get_curve25519_onion_key(rp_node);
666 intro1_data->link_specifiers = rp_lspecs;
669 /* ========== */
670 /* Public API */
671 /* ========== */
673 /* Return an introduction point circuit matching the given intro point object.
674 * NULL is returned is no such circuit can be found. */
675 origin_circuit_t *
676 hs_circ_service_get_intro_circ(const hs_service_intro_point_t *ip)
678 origin_circuit_t *circ = NULL;
680 tor_assert(ip);
682 if (ip->base.is_only_legacy) {
683 uint8_t digest[DIGEST_LEN];
684 if (BUG(crypto_pk_get_digest(ip->legacy_key, (char *) digest) < 0)) {
685 goto end;
687 circ = hs_circuitmap_get_intro_circ_v2_service_side(digest);
688 } else {
689 circ = hs_circuitmap_get_intro_circ_v3_service_side(
690 &ip->auth_key_kp.pubkey);
692 end:
693 return circ;
696 /* Called when we fail building a rendezvous circuit at some point other than
697 * the last hop: launches a new circuit to the same rendezvous point. This
698 * supports legacy service.
700 * We currently relaunch connections to rendezvous points if:
701 * - A rendezvous circuit timed out before connecting to RP.
702 * - The redenzvous circuit failed to connect to the RP.
704 * We avoid relaunching a connection to this rendezvous point if:
705 * - We have already tried MAX_REND_FAILURES times to connect to this RP.
706 * - We've been trying to connect to this RP for more than MAX_REND_TIMEOUT
707 * seconds
708 * - We've already retried this specific rendezvous circuit.
710 void
711 hs_circ_retry_service_rendezvous_point(origin_circuit_t *circ)
713 tor_assert(circ);
714 tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_S_CONNECT_REND);
716 /* Check if we are allowed to relaunch to the rendezvous point of circ. */
717 if (!can_relaunch_service_rendezvous_point(circ)) {
718 goto done;
721 /* Flag the circuit that we are relaunching so to avoid to relaunch twice a
722 * circuit to the same rendezvous point at the same time. */
723 circ->hs_service_side_rend_circ_has_been_relaunched = 1;
725 /* Legacy service don't have an hidden service ident. */
726 if (circ->hs_ident) {
727 retry_service_rendezvous_point(circ);
728 } else {
729 rend_service_relaunch_rendezvous(circ);
732 done:
733 return;
736 /* For a given service and a service intro point, launch a circuit to the
737 * extend info ei. If the service is a single onion, a one-hop circuit will be
738 * requested. Return 0 if the circuit was successfully launched and tagged
739 * with the correct identifier. On error, a negative value is returned. */
741 hs_circ_launch_intro_point(hs_service_t *service,
742 const hs_service_intro_point_t *ip,
743 extend_info_t *ei)
745 /* Standard flags for introduction circuit. */
746 int ret = -1, circ_flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
747 origin_circuit_t *circ;
749 tor_assert(service);
750 tor_assert(ip);
751 tor_assert(ei);
753 /* Update circuit flags in case of a single onion service that requires a
754 * direct connection. */
755 if (service->config.is_single_onion) {
756 circ_flags |= CIRCLAUNCH_ONEHOP_TUNNEL;
759 log_info(LD_REND, "Launching a circuit to intro point %s for service %s.",
760 safe_str_client(extend_info_describe(ei)),
761 safe_str_client(service->onion_address));
763 /* Note down the launch for the retry period. Even if the circuit fails to
764 * be launched, we still want to respect the retry period to avoid stress on
765 * the circuit subsystem. */
766 service->state.num_intro_circ_launched++;
767 circ = circuit_launch_by_extend_info(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
768 ei, circ_flags);
769 if (circ == NULL) {
770 goto end;
773 /* Setup the circuit identifier and attach it to it. */
774 circ->hs_ident = create_intro_circuit_identifier(service, ip);
775 tor_assert(circ->hs_ident);
776 /* Register circuit in the global circuitmap. */
777 register_intro_circ(ip, circ);
779 /* Success. */
780 ret = 0;
781 end:
782 return ret;
785 /* Called when a service introduction point circuit is done building. Given
786 * the service and intro point object, this function will send the
787 * ESTABLISH_INTRO cell on the circuit. Return 0 on success. Return 1 if the
788 * circuit has been repurposed to General because we already have too many
789 * opened. */
791 hs_circ_service_intro_has_opened(hs_service_t *service,
792 hs_service_intro_point_t *ip,
793 const hs_service_descriptor_t *desc,
794 origin_circuit_t *circ)
796 int ret = 0;
797 unsigned int num_intro_circ, num_needed_circ;
799 tor_assert(service);
800 tor_assert(ip);
801 tor_assert(desc);
802 tor_assert(circ);
804 /* Cound opened circuits that have sent ESTABLISH_INTRO cells or are already
805 * established introduction circuits */
806 num_intro_circ = count_opened_desc_intro_point_circuits(service, desc);
807 num_needed_circ = service->config.num_intro_points;
808 if (num_intro_circ > num_needed_circ) {
809 /* There are too many opened valid intro circuit for what the service
810 * needs so repurpose this one. */
812 /* XXX: Legacy code checks options->ExcludeNodes and if not NULL it just
813 * closes the circuit. I have NO idea why it does that so it hasn't been
814 * added here. I can only assume in case our ExcludeNodes list changes but
815 * in that case, all circuit are flagged unusable (config.c). --dgoulet */
817 log_info(LD_CIRC | LD_REND, "Introduction circuit just opened but we "
818 "have enough for service %s. Repurposing "
819 "it to general and leaving internal.",
820 safe_str_client(service->onion_address));
821 tor_assert(circ->build_state->is_internal);
822 /* Remove it from the circuitmap. */
823 hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
824 /* Cleaning up the hidden service identifier and repurpose. */
825 hs_ident_circuit_free(circ->hs_ident);
826 circ->hs_ident = NULL;
827 if (circuit_should_use_vanguards(TO_CIRCUIT(circ)->purpose))
828 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_HS_VANGUARDS);
829 else
830 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_C_GENERAL);
832 /* Inform that this circuit just opened for this new purpose. */
833 circuit_has_opened(circ);
834 /* This return value indicate to the caller that the IP object should be
835 * removed from the service because it's corresponding circuit has just
836 * been repurposed. */
837 ret = 1;
838 goto done;
841 log_info(LD_REND, "Introduction circuit %u established for service %s.",
842 TO_CIRCUIT(circ)->n_circ_id,
843 safe_str_client(service->onion_address));
844 circuit_log_path(LOG_INFO, LD_REND, circ);
846 /* Time to send an ESTABLISH_INTRO cell on this circuit. On error, this call
847 * makes sure the circuit gets closed. */
848 send_establish_intro(service, ip, circ);
850 done:
851 return ret;
854 /* Called when a service rendezvous point circuit is done building. Given the
855 * service and the circuit, this function will send a RENDEZVOUS1 cell on the
856 * circuit using the information in the circuit identifier. If the cell can't
857 * be sent, the circuit is closed. */
858 void
859 hs_circ_service_rp_has_opened(const hs_service_t *service,
860 origin_circuit_t *circ)
862 size_t payload_len;
863 uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
865 tor_assert(service);
866 tor_assert(circ);
867 tor_assert(circ->hs_ident);
869 /* Some useful logging. */
870 log_info(LD_REND, "Rendezvous circuit %u has opened with cookie %s "
871 "for service %s",
872 TO_CIRCUIT(circ)->n_circ_id,
873 hex_str((const char *) circ->hs_ident->rendezvous_cookie,
874 REND_COOKIE_LEN),
875 safe_str_client(service->onion_address));
876 circuit_log_path(LOG_INFO, LD_REND, circ);
878 /* This can't fail. */
879 payload_len = hs_cell_build_rendezvous1(
880 circ->hs_ident->rendezvous_cookie,
881 sizeof(circ->hs_ident->rendezvous_cookie),
882 circ->hs_ident->rendezvous_handshake_info,
883 sizeof(circ->hs_ident->rendezvous_handshake_info),
884 payload);
886 /* Pad the payload with random bytes so it matches the size of a legacy cell
887 * which is normally always bigger. Also, the size of a legacy cell is
888 * always smaller than the RELAY_PAYLOAD_SIZE so this is safe. */
889 if (payload_len < HS_LEGACY_RENDEZVOUS_CELL_SIZE) {
890 crypto_rand((char *) payload + payload_len,
891 HS_LEGACY_RENDEZVOUS_CELL_SIZE - payload_len);
892 payload_len = HS_LEGACY_RENDEZVOUS_CELL_SIZE;
895 if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
896 RELAY_COMMAND_RENDEZVOUS1,
897 (const char *) payload, payload_len,
898 circ->cpath->prev) < 0) {
899 /* On error, circuit is closed. */
900 log_warn(LD_REND, "Unable to send RENDEZVOUS1 cell on circuit %u "
901 "for service %s",
902 TO_CIRCUIT(circ)->n_circ_id,
903 safe_str_client(service->onion_address));
904 goto done;
907 /* Setup end-to-end rendezvous circuit between the client and us. */
908 if (hs_circuit_setup_e2e_rend_circ(circ,
909 circ->hs_ident->rendezvous_ntor_key_seed,
910 sizeof(circ->hs_ident->rendezvous_ntor_key_seed),
911 1) < 0) {
912 log_warn(LD_GENERAL, "Failed to setup circ");
913 goto done;
916 done:
917 memwipe(payload, 0, sizeof(payload));
920 /* Circ has been expecting an INTRO_ESTABLISHED cell that just arrived. Handle
921 * the INTRO_ESTABLISHED cell payload of length payload_len arriving on the
922 * given introduction circuit circ. The service is only used for logging
923 * purposes. Return 0 on success else a negative value. */
925 hs_circ_handle_intro_established(const hs_service_t *service,
926 const hs_service_intro_point_t *ip,
927 origin_circuit_t *circ,
928 const uint8_t *payload, size_t payload_len)
930 int ret = -1;
932 tor_assert(service);
933 tor_assert(ip);
934 tor_assert(circ);
935 tor_assert(payload);
937 if (BUG(TO_CIRCUIT(circ)->purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)) {
938 goto done;
941 /* Try to parse the payload into a cell making sure we do actually have a
942 * valid cell. For a legacy node, it's an empty payload so as long as we
943 * have the cell, we are good. */
944 if (!ip->base.is_only_legacy &&
945 hs_cell_parse_intro_established(payload, payload_len) < 0) {
946 log_warn(LD_REND, "Unable to parse the INTRO_ESTABLISHED cell on "
947 "circuit %u for service %s",
948 TO_CIRCUIT(circ)->n_circ_id,
949 safe_str_client(service->onion_address));
950 goto done;
953 /* Switch the purpose to a fully working intro point. */
954 circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_S_INTRO);
955 /* Getting a valid INTRODUCE_ESTABLISHED means we've successfully used the
956 * circuit so update our pathbias subsystem. */
957 pathbias_mark_use_success(circ);
958 /* Success. */
959 ret = 0;
961 done:
962 return ret;
965 /* We just received an INTRODUCE2 cell on the established introduction circuit
966 * circ. Handle the INTRODUCE2 payload of size payload_len for the given
967 * circuit and service. This cell is associated with the intro point object ip
968 * and the subcredential. Return 0 on success else a negative value. */
970 hs_circ_handle_introduce2(const hs_service_t *service,
971 const origin_circuit_t *circ,
972 hs_service_intro_point_t *ip,
973 const uint8_t *subcredential,
974 const uint8_t *payload, size_t payload_len)
976 int ret = -1;
977 time_t elapsed;
978 hs_cell_introduce2_data_t data;
980 tor_assert(service);
981 tor_assert(circ);
982 tor_assert(ip);
983 tor_assert(subcredential);
984 tor_assert(payload);
986 /* Populate the data structure with everything we need for the cell to be
987 * parsed, decrypted and key material computed correctly. */
988 data.auth_pk = &ip->auth_key_kp.pubkey;
989 data.enc_kp = &ip->enc_key_kp;
990 data.subcredential = subcredential;
991 data.payload = payload;
992 data.payload_len = payload_len;
993 data.link_specifiers = smartlist_new();
994 data.replay_cache = ip->replay_cache;
996 if (hs_cell_parse_introduce2(&data, circ, service) < 0) {
997 goto done;
1000 /* Check whether we've seen this REND_COOKIE before to detect repeats. */
1001 if (replaycache_add_test_and_elapsed(
1002 service->state.replay_cache_rend_cookie,
1003 data.rendezvous_cookie, sizeof(data.rendezvous_cookie),
1004 &elapsed)) {
1005 /* A Tor client will send a new INTRODUCE1 cell with the same REND_COOKIE
1006 * as its previous one if its intro circ times out while in state
1007 * CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT. If we received the first
1008 * INTRODUCE1 cell (the intro-point relay converts it into an INTRODUCE2
1009 * cell), we are already trying to connect to that rend point (and may
1010 * have already succeeded); drop this cell. */
1011 log_info(LD_REND, "We received an INTRODUCE2 cell with same REND_COOKIE "
1012 "field %ld seconds ago. Dropping cell.",
1013 (long int) elapsed);
1014 goto done;
1017 /* At this point, we just confirmed that the full INTRODUCE2 cell is valid
1018 * so increment our counter that we've seen one on this intro point. */
1019 ip->introduce2_count++;
1021 /* Launch rendezvous circuit with the onion key and rend cookie. */
1022 launch_rendezvous_point_circuit(service, ip, &data);
1023 /* Success. */
1024 ret = 0;
1026 done:
1027 SMARTLIST_FOREACH(data.link_specifiers, link_specifier_t *, lspec,
1028 link_specifier_free(lspec));
1029 smartlist_free(data.link_specifiers);
1030 memwipe(&data, 0, sizeof(data));
1031 return ret;
1034 /* Circuit <b>circ</b> just finished the rend ntor key exchange. Use the key
1035 * exchange output material at <b>ntor_key_seed</b> and setup <b>circ</b> to
1036 * serve as a rendezvous end-to-end circuit between the client and the
1037 * service. If <b>is_service_side</b> is set, then we are the hidden service
1038 * and the other side is the client.
1040 * Return 0 if the operation went well; in case of error return -1. */
1042 hs_circuit_setup_e2e_rend_circ(origin_circuit_t *circ,
1043 const uint8_t *ntor_key_seed, size_t seed_len,
1044 int is_service_side)
1046 if (BUG(!circuit_purpose_is_correct_for_rend(TO_CIRCUIT(circ)->purpose,
1047 is_service_side))) {
1048 return -1;
1051 crypt_path_t *hop = create_rend_cpath(ntor_key_seed, seed_len,
1052 is_service_side);
1053 if (!hop) {
1054 log_warn(LD_REND, "Couldn't get v3 %s cpath!",
1055 is_service_side ? "service-side" : "client-side");
1056 return -1;
1059 finalize_rend_circuit(circ, hop, is_service_side);
1061 return 0;
1064 /* We are a v2 legacy HS client and we just received a RENDEZVOUS1 cell
1065 * <b>rend_cell_body</b> on <b>circ</b>. Finish up the DH key exchange and then
1066 * extend the crypt path of <b>circ</b> so that the hidden service is on the
1067 * other side. */
1069 hs_circuit_setup_e2e_rend_circ_legacy_client(origin_circuit_t *circ,
1070 const uint8_t *rend_cell_body)
1073 if (BUG(!circuit_purpose_is_correct_for_rend(
1074 TO_CIRCUIT(circ)->purpose, 0))) {
1075 return -1;
1078 crypt_path_t *hop = create_rend_cpath_legacy(circ, rend_cell_body);
1079 if (!hop) {
1080 log_warn(LD_GENERAL, "Couldn't get v2 cpath.");
1081 return -1;
1084 finalize_rend_circuit(circ, hop, 0);
1086 return 0;
1089 /* Given the introduction circuit intro_circ, the rendezvous circuit
1090 * rend_circ, a descriptor intro point object ip and the service's
1091 * subcredential, send an INTRODUCE1 cell on intro_circ.
1093 * This will also setup the circuit identifier on rend_circ containing the key
1094 * material for the handshake and e2e encryption. Return 0 on success else
1095 * negative value. Because relay_send_command_from_edge() closes the circuit
1096 * on error, it is possible that intro_circ is closed on error. */
1098 hs_circ_send_introduce1(origin_circuit_t *intro_circ,
1099 origin_circuit_t *rend_circ,
1100 const hs_desc_intro_point_t *ip,
1101 const uint8_t *subcredential)
1103 int ret = -1;
1104 ssize_t payload_len;
1105 uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
1106 hs_cell_introduce1_data_t intro1_data;
1108 tor_assert(intro_circ);
1109 tor_assert(rend_circ);
1110 tor_assert(ip);
1111 tor_assert(subcredential);
1113 /* It is undefined behavior in hs_cell_introduce1_data_clear() if intro1_data
1114 * has been declared on the stack but not initialized. Here, we set it to 0.
1116 memset(&intro1_data, 0, sizeof(hs_cell_introduce1_data_t));
1118 /* This takes various objects in order to populate the introduce1 data
1119 * object which is used to build the content of the cell. */
1120 const node_t *exit_node = build_state_get_exit_node(rend_circ->build_state);
1121 if (exit_node == NULL) {
1122 log_info(LD_REND, "Unable to get rendezvous point for circuit %u. "
1123 "Failing.", TO_CIRCUIT(intro_circ)->n_circ_id);
1124 goto done;
1126 setup_introduce1_data(ip, exit_node, subcredential, &intro1_data);
1127 /* If we didn't get any link specifiers, it's because our node was
1128 * bad. */
1129 if (BUG(!intro1_data.link_specifiers) ||
1130 !smartlist_len(intro1_data.link_specifiers)) {
1131 log_warn(LD_REND, "Unable to get link specifiers for INTRODUCE1 cell on "
1132 "circuit %u.", TO_CIRCUIT(intro_circ)->n_circ_id);
1133 goto done;
1136 /* Final step before we encode a cell, we setup the circuit identifier which
1137 * will generate both the rendezvous cookie and client keypair for this
1138 * connection. Those are put in the ident. */
1139 intro1_data.rendezvous_cookie = rend_circ->hs_ident->rendezvous_cookie;
1140 intro1_data.client_kp = &rend_circ->hs_ident->rendezvous_client_kp;
1142 memcpy(intro_circ->hs_ident->rendezvous_cookie,
1143 rend_circ->hs_ident->rendezvous_cookie,
1144 sizeof(intro_circ->hs_ident->rendezvous_cookie));
1146 /* From the introduce1 data object, this will encode the INTRODUCE1 cell
1147 * into payload which is then ready to be sent as is. */
1148 payload_len = hs_cell_build_introduce1(&intro1_data, payload);
1149 if (BUG(payload_len < 0)) {
1150 goto done;
1153 if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(intro_circ),
1154 RELAY_COMMAND_INTRODUCE1,
1155 (const char *) payload, payload_len,
1156 intro_circ->cpath->prev) < 0) {
1157 /* On error, circuit is closed. */
1158 log_warn(LD_REND, "Unable to send INTRODUCE1 cell on circuit %u.",
1159 TO_CIRCUIT(intro_circ)->n_circ_id);
1160 goto done;
1163 /* Success. */
1164 ret = 0;
1165 goto done;
1167 done:
1168 hs_cell_introduce1_data_clear(&intro1_data);
1169 memwipe(payload, 0, sizeof(payload));
1170 return ret;
1173 /* Send an ESTABLISH_RENDEZVOUS cell along the rendezvous circuit circ. On
1174 * success, 0 is returned else -1 and the circuit is marked for close. */
1176 hs_circ_send_establish_rendezvous(origin_circuit_t *circ)
1178 ssize_t cell_len = 0;
1179 uint8_t cell[RELAY_PAYLOAD_SIZE] = {0};
1181 tor_assert(circ);
1182 tor_assert(TO_CIRCUIT(circ)->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
1184 log_info(LD_REND, "Send an ESTABLISH_RENDEZVOUS cell on circuit %u",
1185 TO_CIRCUIT(circ)->n_circ_id);
1187 /* Set timestamp_dirty, because circuit_expire_building expects it,
1188 * and the rend cookie also means we've used the circ. */
1189 TO_CIRCUIT(circ)->timestamp_dirty = time(NULL);
1191 /* We've attempted to use this circuit. Probe it if we fail */
1192 pathbias_count_use_attempt(circ);
1194 /* Generate the RENDEZVOUS_COOKIE and place it in the identifier so we can
1195 * complete the handshake when receiving the acknowledgement. */
1196 crypto_rand((char *) circ->hs_ident->rendezvous_cookie, HS_REND_COOKIE_LEN);
1197 /* Generate the client keypair. No need to be extra strong, not long term */
1198 curve25519_keypair_generate(&circ->hs_ident->rendezvous_client_kp, 0);
1200 cell_len =
1201 hs_cell_build_establish_rendezvous(circ->hs_ident->rendezvous_cookie,
1202 cell);
1203 if (BUG(cell_len < 0)) {
1204 goto err;
1207 if (relay_send_command_from_edge(CONTROL_CELL_ID, TO_CIRCUIT(circ),
1208 RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
1209 (const char *) cell, cell_len,
1210 circ->cpath->prev) < 0) {
1211 /* Circuit has been marked for close */
1212 log_warn(LD_REND, "Unable to send ESTABLISH_RENDEZVOUS cell on "
1213 "circuit %u", TO_CIRCUIT(circ)->n_circ_id);
1214 memwipe(cell, 0, cell_len);
1215 goto err;
1218 memwipe(cell, 0, cell_len);
1219 return 0;
1220 err:
1221 return -1;
1224 /* We are about to close or free this <b>circ</b>. Clean it up from any
1225 * related HS data structures. This function can be called multiple times
1226 * safely for the same circuit. */
1227 void
1228 hs_circ_cleanup(circuit_t *circ)
1230 tor_assert(circ);
1232 /* If it's a service-side intro circ, notify the HS subsystem for the intro
1233 * point circuit closing so it can be dealt with cleanly. */
1234 if (circ->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
1235 circ->purpose == CIRCUIT_PURPOSE_S_INTRO) {
1236 hs_service_intro_circ_has_closed(TO_ORIGIN_CIRCUIT(circ));
1239 /* Clear HS circuitmap token for this circ (if any). Very important to be
1240 * done after the HS subsystem has been notified of the close else the
1241 * circuit will not be found.
1243 * We do this at the close if possible because from that point on, the
1244 * circuit is good as dead. We can't rely on removing it in the circuit
1245 * free() function because we open a race window between the close and free
1246 * where we can't register a new circuit for the same intro point. */
1247 if (circ->hs_token) {
1248 hs_circuitmap_remove_circuit(circ);