Tor 0.4.9.13
Loading...
Searching...
No Matches
extendinfo.c
Go to the documentation of this file.
1/* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
5/* See LICENSE for licensing information */
6
7/**
8 * @file extendinfo.c
9 * @brief Functions for creating and using extend_info_t objects.
10 *
11 * An extend_info_t is the information we hold about a relay in order to
12 * extend a circuit to it.
13 **/
14
15#include "core/or/or.h"
16#include "core/or/extendinfo.h"
17
18#include "app/config/config.h"
19#include "core/or/policies.h"
26
31
32/** Allocate a new extend_info object based on the various arguments. */
34extend_info_new(const char *nickname,
35 const char *rsa_id_digest,
36 const ed25519_public_key_t *ed_id,
37 const curve25519_public_key_t *ntor_key,
38 const tor_addr_t *addr, uint16_t port,
40 bool for_exit_use)
41{
42 (void) for_exit_use;
43
44 extend_info_t *info = tor_malloc_zero(sizeof(extend_info_t));
45 if (rsa_id_digest)
46 memcpy(info->identity_digest, rsa_id_digest, DIGEST_LEN);
47 if (ed_id && !ed25519_public_key_is_zero(ed_id))
48 memcpy(&info->ed_identity, ed_id, sizeof(ed25519_public_key_t));
49 if (nickname)
50 strlcpy(info->nickname, nickname, sizeof(info->nickname));
51 if (ntor_key)
52 memcpy(&info->curve25519_onion_key, ntor_key,
54 for (int i = 0; i < EXTEND_INFO_MAX_ADDRS; ++i) {
55 tor_addr_make_unspec(&info->orports[i].addr);
56 }
57
58 if (addr) {
59 extend_info_add_orport(info, addr, port);
60 }
61
62 if (pv) {
65 }
66
67 if (pv) {
70 }
71
72 return info;
73}
74
75/**
76 * Add another address:port pair to a given extend_info_t, if there is
77 * room. Return 0 on success, -1 on failure.
78 **/
79int
81 const tor_addr_t *addr,
82 uint16_t port)
83{
84 for (int i = 0; i < EXTEND_INFO_MAX_ADDRS; ++i) {
85 if (tor_addr_is_unspec(&ei->orports[i].addr)) {
86 tor_addr_copy(&ei->orports[i].addr, addr);
87 ei->orports[i].port = port;
88 return 0;
89 }
90 }
91 return -1;
92}
93
94/** Allocate and return a new extend_info that can be used to build a
95 * circuit to or through the node <b>node</b>. Use the primary address
96 * of the node (i.e. its IPv4 address) unless
97 * <b>for_direct_connect</b> is true, in which case the preferred
98 * address is used instead. May return NULL if there is not enough
99 * info about <b>node</b> to extend to it--for example, if the preferred
100 * routerinfo_t or microdesc_t is missing, or if for_direct_connect is
101 * true and none of the node's addresses is allowed by tor's firewall
102 * and IP version config.
103 **/
105extend_info_from_node(const node_t *node, int for_direct_connect,
106 bool for_exit)
107{
108 extend_info_t *info = NULL;
110 int valid_addr = 0;
111
112 if (!node_has_preferred_descriptor(node, for_direct_connect)) {
113 return NULL;
114 }
115
116 /* Choose a preferred address first, but fall back to an allowed address. */
117 if (for_direct_connect)
118 reachable_addr_choose_from_node(node, FIREWALL_OR_CONNECTION, 0, &ap);
119 else {
120 node_get_prim_orport(node, &ap);
121 }
122 valid_addr = tor_addr_port_is_valid_ap(&ap, 0);
123
124 if (valid_addr)
125 log_debug(LD_CIRC, "using %s for %s",
126 fmt_addrport(&ap.addr, ap.port),
127 node->ri ? node->ri->nickname : node->rs->nickname);
128 else
129 log_warn(LD_CIRC, "Could not choose valid address for %s",
130 node->ri ? node->ri->nickname : node->rs->nickname);
131
132 /* Every node we connect or extend to must support ntor */
134 log_fn(LOG_PROTOCOL_WARN, LD_CIRC,
135 "Attempted to create extend_info for a node that does not support "
136 "ntor: %s", node_describe(node));
137 return NULL;
138 }
139
140 const ed25519_public_key_t *ed_pubkey = NULL;
141
142 /* Don't send the ed25519 pubkey unless the target node actually supports
143 * authenticating with it. */
145 log_info(LD_CIRC, "Including Ed25519 ID for %s", node_describe(node));
146 ed_pubkey = node_get_ed25519_id(node);
147 } else if (node_get_ed25519_id(node)) {
148 log_info(LD_CIRC, "Not including the ed25519 ID for %s, since it won't "
149 "be able to authenticate it.",
150 node_describe(node));
151 }
152
153 /* Retrieve the curve25519 pubkey. */
154 const curve25519_public_key_t *curve_pubkey =
156
157 if (valid_addr && node->ri) {
158 info = extend_info_new(node->ri->nickname,
159 node->identity,
160 ed_pubkey,
161 curve_pubkey,
162 &ap.addr,
163 ap.port,
164 &node->ri->pv,
165 for_exit);
166 } else if (valid_addr && node->rs && node->md) {
167 info = extend_info_new(node->rs->nickname,
168 node->identity,
169 ed_pubkey,
170 curve_pubkey,
171 &ap.addr,
172 ap.port,
173 &node->rs->pv,
174 for_exit);
175 }
176
177 return info;
178}
179
180/** Release storage held by an extend_info_t struct. */
181void
183{
184 if (!info)
185 return;
186 tor_free(info);
187}
188
189/** Allocate and return a new extend_info_t with the same contents as
190 * <b>info</b>. */
193{
194 extend_info_t *newinfo;
195 tor_assert(info);
196 newinfo = tor_malloc(sizeof(extend_info_t));
197 memcpy(newinfo, info, sizeof(extend_info_t));
198 return newinfo;
199}
200
201/* Does ei have a valid ntor key? */
202int
203extend_info_supports_ntor(const extend_info_t* ei)
204{
205 tor_assert(ei);
206 /* Valid ntor keys have at least one non-zero byte */
207 return !fast_mem_is_zero(
208 (const char*)ei->curve25519_onion_key.public_key,
210}
211
212/** Return true if we can use the Ntor v3 handshake with `ei` */
213int
215{
216 tor_assert(ei);
217 return ei->supports_ntor_v3;
218}
219
220/* Does ei have an onion key which it would prefer to use?
221 * Currently, we prefer ntor keys*/
222int
223extend_info_has_preferred_onion_key(const extend_info_t* ei)
224{
225 tor_assert(ei);
226 return extend_info_supports_ntor(ei);
227}
228
229/** Return true iff the given address can be used to extend to. */
230int
232{
233 tor_assert(addr);
234
235 /* Check if we have a private address and if we can extend to it. */
236 if ((tor_addr_is_internal(addr, 0) || tor_addr_is_multicast(addr)) &&
237 !get_options()->ExtendAllowPrivateAddresses) {
238 goto disallow;
239 }
240 /* Allowed! */
241 return 1;
242 disallow:
243 return 0;
244}
245
246/**
247 * Return true if @a addr : @a port is a listed ORPort in @a ei.
248 **/
249bool
251 const tor_addr_t *addr, uint16_t port)
252{
253 IF_BUG_ONCE(ei == NULL) {
254 return false;
255 }
256
257 for (int i = 0; i < EXTEND_INFO_MAX_ADDRS; ++i) {
258 const tor_addr_port_t *ei_ap = &ei->orports[i];
259 if (tor_addr_eq(&ei_ap->addr, addr) && ei_ap->port == port)
260 return true;
261 }
262 return false;
263}
264
265/**
266 * If the extend_info @a ei has an orport of the chosen family, then return
267 * that orport. Otherwise, return NULL.
268 **/
269const tor_addr_port_t *
271{
272 for (int i = 0; i < EXTEND_INFO_MAX_ADDRS; ++i) {
273 if (tor_addr_is_unspec(&ei->orports[i].addr))
274 continue;
275 if (tor_addr_family(&ei->orports[i].addr) == family)
276 return &ei->orports[i];
277 }
278 return NULL;
279}
280
281/**
282 * Chose an addr_port_t within @a ei to connect to.
283 **/
284const tor_addr_port_t *
286{
287 IF_BUG_ONCE(!ei) {
288 return NULL;
289 }
290 const or_options_t *options = get_options();
291 if (!server_mode(options)) {
292 // If we aren't a server, just pick the first address we built into
293 // this extendinfo.
294 return &ei->orports[0];
295 }
296
297 const bool ipv6_ok = router_can_extend_over_ipv6(options);
298
299 // Use 'usable' to collect the usable orports, then pick one.
301 int n_usable = 0;
302 for (int i = 0; i < EXTEND_INFO_MAX_ADDRS; ++i) {
303 const tor_addr_port_t *a = &ei->orports[i];
304 const int family = tor_addr_family(&a->addr);
305 if (family == AF_INET || (ipv6_ok && family == AF_INET6)) {
306 usable[n_usable++] = a;
307 }
308 }
309
310 if (n_usable == 0) {
311 // Need to bail out early, since nothing will work.
312 return NULL;
313 }
314
316 const int idx = crypto_fast_rng_get_uint(rng, n_usable);
317
318 return usable[idx];
319}
320
321/**
322 * Return true if any orport address in @a ei is an internal address.
323 **/
324bool
326{
327 IF_BUG_ONCE(ei == NULL) {
328 return false;
329 }
330
331 for (int i = 0; i < EXTEND_INFO_MAX_ADDRS; ++i) {
332 if (! tor_addr_is_unspec(&ei->orports[i].addr) &&
333 tor_addr_is_internal(&ei->orports[i].addr, 0))
334 return true;
335 }
336 return false;
337}
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition address.c:933
void tor_addr_make_unspec(tor_addr_t *a)
Definition address.c:225
int tor_addr_is_multicast(const tor_addr_t *a)
Definition address.c:1624
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition address.c:1199
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition address.h:189
#define tor_addr_eq(a, b)
Definition address.h:282
static bool tor_addr_is_unspec(const tor_addr_t *a)
Definition address.h:198
const or_options_t * get_options(void)
Definition config.c:949
Header file for config.c.
bool congestion_control_enabled(void)
Public APIs for congestion control.
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
Common functions for using (pseudo-)random number generators.
crypto_fast_rng_t * get_thread_fast_rng(void)
unsigned crypto_fast_rng_get_uint(crypto_fast_rng_t *rng, unsigned limit)
const char * node_describe(const node_t *node)
Definition describe.c:160
Header file for describe.c.
#define DIGEST_LEN
Extend-info structure.
#define EXTEND_INFO_MAX_ADDRS
void extend_info_free_(extend_info_t *info)
Definition extendinfo.c:182
extend_info_t * extend_info_dup(extend_info_t *info)
Definition extendinfo.c:192
extend_info_t * extend_info_from_node(const node_t *node, int for_direct_connect, bool for_exit)
Definition extendinfo.c:105
const tor_addr_port_t * extend_info_pick_orport(const extend_info_t *ei)
Definition extendinfo.c:285
int extend_info_add_orport(extend_info_t *ei, const tor_addr_t *addr, uint16_t port)
Definition extendinfo.c:80
extend_info_t * extend_info_new(const char *nickname, const char *rsa_id_digest, const ed25519_public_key_t *ed_id, const curve25519_public_key_t *ntor_key, const tor_addr_t *addr, uint16_t port, const protover_summary_flags_t *pv, bool for_exit_use)
Definition extendinfo.c:34
const tor_addr_port_t * extend_info_get_orport(const extend_info_t *ei, int family)
Definition extendinfo.c:270
int extend_info_supports_ntor_v3(const extend_info_t *ei)
Definition extendinfo.c:214
bool extend_info_any_orport_addr_is_internal(const extend_info_t *ei)
Definition extendinfo.c:325
bool extend_info_has_orport(const extend_info_t *ei, const tor_addr_t *addr, uint16_t port)
Definition extendinfo.c:250
int extend_info_addr_is_allowed(const tor_addr_t *addr)
Definition extendinfo.c:231
Header for core/or/extendinfo.c.
#define log_fn(severity, domain, args,...)
Definition log.h:283
#define LD_CIRC
Definition log.h:82
#define tor_free(p)
Definition malloc.h:56
Node information structure.
void node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition nodelist.c:1855
int node_has_preferred_descriptor(const node_t *node, int for_direct_connect)
Definition nodelist.c:1534
bool node_supports_ed25519_link_authentication(const node_t *node, bool compatible_with_us)
Definition nodelist.c:1260
const curve25519_public_key_t * node_get_curve25519_onion_key(const node_t *node)
Definition nodelist.c:2050
const ed25519_public_key_t * node_get_ed25519_id(const node_t *node)
Definition nodelist.c:1175
int node_has_curve25519_onion_key(const node_t *node)
Definition nodelist.c:2043
Header file for nodelist.c.
Master header file for Tor-specific functionality.
void reachable_addr_choose_from_node(const node_t *node, firewall_connection_t fw_connection, int pref_only, tor_addr_port_t *ap)
Definition policies.c:988
Header file for policies.c.
bool router_can_extend_over_ipv6(const or_options_t *options)
Definition router.c:1610
Header file for router.c.
Router descriptor structure.
Header file for routermode.c.
Routerstatus (consensus entry) structure.
tor_addr_port_t orports[EXTEND_INFO_MAX_ADDRS]
ed25519_public_key_t ed_identity
char identity_digest[DIGEST_LEN]
bool use_congestion_control
char nickname[MAX_HEX_NICKNAME_LEN+1]
curve25519_public_key_t curve25519_onion_key
char identity[DIGEST_LEN]
Definition node_st.h:46
unsigned int supports_cgo
Definition or.h:845
unsigned int supports_ntor_v3
Definition or.h:848
unsigned int supports_congestion_control
Definition or.h:839
protover_summary_flags_t pv
protover_summary_flags_t pv
char nickname[MAX_NICKNAME_LEN+1]
#define tor_assert(expr)
Definition util_bug.h:103
#define IF_BUG_ONCE(cond)
Definition util_bug.h:254
int fast_mem_is_zero(const char *mem, size_t len)
Definition util_string.c:76
#define CURVE25519_PUBKEY_LEN