safe_connection.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. #include "core/or/safe_connection.h"
  2. #include "app/config/config.h"
  3. #include "lib/net/buffers_net.h"
  4. #include "lib/tls/tortls.h"
  5. #include "lib/tls/buffers_tls.h"
  6. #include "lib/malloc/malloc.h"
  7. #include "core/proto/proto_cell.h"
  8. #include "core/or/connection_or.h"
  9. #include "core/or/var_cell_st.h"
  10. #include "core/or/cell_st.h"
  11. event_label_t safe_or_conn_tcp_connecting_ev = EVENT_LABEL_UNSET;
  12. event_label_t safe_or_conn_tls_handshaking_ev = EVENT_LABEL_UNSET;
  13. event_label_t safe_or_conn_link_handshaking_ev = EVENT_LABEL_UNSET;
  14. event_label_t safe_or_conn_open_ev = EVENT_LABEL_UNSET;
  15. event_label_t safe_or_conn_closed_ev = EVENT_LABEL_UNSET;
  16. event_label_t safe_or_conn_fixed_cell_ev = EVENT_LABEL_UNSET;
  17. event_label_t safe_or_conn_var_cell_ev = EVENT_LABEL_UNSET;
  18. static void
  19. safe_connection_refresh_events(safe_connection_t *safe_conn);
  20. static void
  21. safe_or_connection_refresh_bucket_rw_states(safe_or_connection_t *safe_or_conn);
  22. static tor_error_t
  23. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  24. or_conn_state_t new_state);
  25. static bool
  26. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn);
  27. static bool
  28. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn);
  29. static void
  30. safe_or_connection_read_cb(safe_connection_t *safe_conn);
  31. static void
  32. safe_or_connection_write_cb(safe_connection_t *safe_conn);
  33. static void
  34. safe_or_connection_socket_added_cb(safe_connection_t *safe_conn);
  35. static void
  36. safe_or_connection_outbuf_modified_cb(safe_connection_t *safe_conn);
  37. static void
  38. process_cells_from_inbuf(safe_or_connection_t *safe_or_conn);
  39. /********************************************************/
  40. safe_or_connection_t *
  41. TO_SAFE_OR_CONN(safe_connection_t *safe_conn)
  42. {
  43. tor_assert(safe_conn != NULL);
  44. tor_assert(safe_conn->magic == SAFE_OR_CONN_MAGIC);
  45. return DOWNCAST(safe_or_connection_t, safe_conn);
  46. }
  47. void
  48. safe_or_conn_register_events(event_registry_t *registry)
  49. {
  50. tor_assert(safe_or_conn_tcp_connecting_ev == EVENT_LABEL_UNSET);
  51. tor_assert(safe_or_conn_tls_handshaking_ev == EVENT_LABEL_UNSET);
  52. tor_assert(safe_or_conn_link_handshaking_ev == EVENT_LABEL_UNSET);
  53. tor_assert(safe_or_conn_open_ev == EVENT_LABEL_UNSET);
  54. tor_assert(safe_or_conn_closed_ev == EVENT_LABEL_UNSET);
  55. tor_assert(safe_or_conn_fixed_cell_ev == EVENT_LABEL_UNSET);
  56. tor_assert(safe_or_conn_var_cell_ev == EVENT_LABEL_UNSET);
  57. safe_or_conn_tcp_connecting_ev = \
  58. event_registry_register_event(registry, "OR Connection Connecting");
  59. safe_or_conn_tls_handshaking_ev = \
  60. event_registry_register_event(registry, "Starting OR TLS Handshake");
  61. safe_or_conn_link_handshaking_ev = \
  62. event_registry_register_event(registry, "Starting OR Link Handshake");
  63. safe_or_conn_open_ev = \
  64. event_registry_register_event(registry, "OR Connection Open");
  65. safe_or_conn_closed_ev = \
  66. event_registry_register_event(registry, "OR Connection Closed");
  67. safe_or_conn_fixed_cell_ev = \
  68. event_registry_register_event(registry, "OR Connection New Fixed-Size Cell");
  69. safe_or_conn_var_cell_ev = \
  70. event_registry_register_event(registry, "OR Connection New Variable-Size Cell");
  71. }
  72. /********************************************************/
  73. void
  74. link_handshaking_ev_free(void *ptr)
  75. {
  76. // we don't need to free the certs since we passed the ownership
  77. tor_free(ptr);
  78. }
  79. /********************************************************/
  80. static void
  81. socket_rw_state_init(socket_rw_state_t *rw_state,
  82. bool initial_state)
  83. {
  84. tor_assert(rw_state != NULL);
  85. rw_state->state = initial_state;
  86. }
  87. static bool
  88. socket_rw_state_get(socket_rw_state_t *rw_state)
  89. {
  90. tor_assert(rw_state != NULL);
  91. return rw_state->state;
  92. }
  93. static void
  94. socket_rw_state_set(socket_rw_state_t *rw_state,
  95. bool new_state,
  96. safe_connection_t *safe_conn)
  97. {
  98. tor_assert(rw_state != NULL);
  99. tor_assert(safe_conn != NULL);
  100. if (new_state != rw_state->state) {
  101. rw_state->state = new_state;
  102. safe_connection_refresh_events(safe_conn);
  103. }
  104. }
  105. /********************************************************/
  106. void
  107. safe_connection_init(safe_connection_t *safe_conn, uint32_t type_magic,
  108. bool (*is_read_wanted)(safe_connection_t *),
  109. bool (*is_write_wanted)(safe_connection_t *),
  110. void (*read_cb)(safe_connection_t *),
  111. void (*write_cb)(safe_connection_t *),
  112. void (*socket_added_cb)(safe_connection_t *),
  113. void (*inbuf_modified_cb)(safe_connection_t *),
  114. void (*outbuf_modified_cb)(safe_connection_t *),
  115. bool requires_buffers, bool linked)
  116. {
  117. tor_assert(safe_conn != NULL);
  118. tor_assert(is_read_wanted != NULL);
  119. tor_assert(is_write_wanted != NULL);
  120. tor_assert(read_cb != NULL);
  121. tor_assert(write_cb != NULL);
  122. memset(safe_conn, 0, sizeof(*safe_conn));
  123. safe_conn->magic = type_magic;
  124. safe_conn->socket = TOR_INVALID_SOCKET;
  125. safe_conn->linked = linked;
  126. safe_conn->event_source = event_source_new();
  127. socket_rw_state_init(&safe_conn->read_allowed, true);
  128. socket_rw_state_init(&safe_conn->write_allowed, true);
  129. tor_mutex_init(&safe_conn->lock);
  130. safe_conn->is_read_wanted = is_read_wanted;
  131. safe_conn->is_write_wanted = is_write_wanted;
  132. safe_conn->read_cb = read_cb;
  133. safe_conn->write_cb = write_cb;
  134. safe_conn->socket_added_cb = socket_added_cb;
  135. safe_conn->inbuf_modified_cb = inbuf_modified_cb;
  136. safe_conn->outbuf_modified_cb = outbuf_modified_cb;
  137. if (requires_buffers) {
  138. safe_conn->inbuf = buf_new();
  139. safe_conn->outbuf = buf_new();
  140. }
  141. }
  142. void
  143. safe_connection_set_socket(safe_connection_t *safe_conn, tor_socket_t socket)
  144. {
  145. tor_assert(safe_conn != NULL);
  146. tor_mutex_acquire(&safe_conn->lock);
  147. tor_assert(!safe_conn->linked);
  148. tor_assert(SOCKET_OK(socket));
  149. if (safe_conn->socket != TOR_INVALID_SOCKET) {
  150. log_warn(LD_BUG, "We're overwriting a previous socket");
  151. }
  152. safe_conn->socket = socket;
  153. if (safe_conn->socket_added_cb != NULL) {
  154. safe_conn->socket_added_cb(safe_conn);
  155. }
  156. tor_mutex_release(&safe_conn->lock);
  157. }
  158. static void
  159. safe_connection_read_cb(evutil_socket_t ev_sock, short fd, void *void_safe_conn)
  160. {
  161. (void)ev_sock;
  162. (void)fd;
  163. safe_connection_t *safe_conn = void_safe_conn;
  164. tor_assert(safe_conn != NULL);
  165. tor_mutex_acquire(&safe_conn->lock);
  166. tor_assert(safe_conn->read_cb != NULL);
  167. //tor_assert(safe_conn->read_event != NULL);
  168. // NOTE: the below requires obtaining a lock on the event base, which adds
  169. // unnecessary slowness
  170. // XX: Is the above true?
  171. //if (!event_pending(safe_conn->read_event, EV_READ, NULL)) {
  172. // // another thread may have disabled this event between when the
  173. // // callback started, and when we acquired the lock above
  174. // return;
  175. //}
  176. //if (!safe_conn->read_allowed || !safe_conn->read_wanted) {
  177. // // we shouldn't be reading
  178. // return;
  179. //}
  180. safe_conn->read_cb(safe_conn);
  181. tor_mutex_release(&safe_conn->lock);
  182. }
  183. static void
  184. safe_connection_write_cb(evutil_socket_t ev_sock, short fd, void *void_safe_conn)
  185. {
  186. (void)ev_sock;
  187. (void)fd;
  188. safe_connection_t *safe_conn = void_safe_conn;
  189. tor_assert(safe_conn != NULL);
  190. tor_mutex_acquire(&safe_conn->lock);
  191. tor_assert(safe_conn->write_cb != NULL);
  192. //tor_assert(safe_conn->write_event != NULL);
  193. // NOTE: the below requires obtaining a lock on the event base, which adds
  194. // unnecessary slowness
  195. // XX: Is the above true?
  196. //if (!event_pending(safe_conn->write_event, EV_WRITE, NULL)) {
  197. // // another thread may have disabled this event between when the
  198. // // callback started, and when we acquired the lock above
  199. // return;
  200. //}
  201. //if (!safe_conn->write_allowed || !safe_conn->write_wanted) {
  202. // // we shouldn't be writing
  203. // return;
  204. //}
  205. safe_conn->write_cb(safe_conn);
  206. tor_mutex_release(&safe_conn->lock);
  207. }
  208. void
  209. safe_connection_subscribe(safe_connection_t *safe_conn,
  210. event_listener_t *listener, event_label_t label)
  211. {
  212. tor_assert(safe_conn != NULL);
  213. tor_mutex_acquire(&safe_conn->lock);
  214. event_source_subscribe(safe_conn->event_source, listener, label);
  215. tor_mutex_release(&safe_conn->lock);
  216. }
  217. void
  218. safe_connection_unsubscribe_all(safe_connection_t *safe_conn,
  219. event_listener_t *listener)
  220. {
  221. tor_assert(safe_conn != NULL);
  222. tor_mutex_acquire(&safe_conn->lock);
  223. event_source_unsubscribe_all(safe_conn->event_source, listener);
  224. tor_mutex_release(&safe_conn->lock);
  225. }
  226. void
  227. safe_connection_unregister_events(safe_connection_t *safe_conn)
  228. {
  229. tor_assert(safe_conn != NULL);
  230. tor_mutex_acquire(&safe_conn->lock);
  231. if (safe_conn->read_event != NULL) {
  232. tor_event_free(safe_conn->read_event);
  233. }
  234. if (safe_conn->write_event != NULL) {
  235. tor_event_free(safe_conn->write_event);
  236. }
  237. tor_mutex_release(&safe_conn->lock);
  238. }
  239. tor_error_t
  240. safe_connection_register_events(safe_connection_t *safe_conn,
  241. struct event_base *event_base)
  242. {
  243. tor_assert(safe_conn != NULL);
  244. tor_mutex_acquire(&safe_conn->lock);
  245. tor_assert(safe_conn->read_cb != NULL);
  246. tor_assert(safe_conn->write_cb != NULL);
  247. tor_assert(safe_conn->linked != SOCKET_OK(safe_conn->socket));
  248. // is either linked or has a socket, but not both (or neither)
  249. safe_connection_unregister_events(safe_conn);
  250. safe_conn->read_event = tor_event_new(event_base, safe_conn->socket,
  251. EV_READ|EV_PERSIST,
  252. safe_connection_read_cb, safe_conn);
  253. safe_conn->write_event = tor_event_new(event_base, safe_conn->socket,
  254. EV_WRITE|EV_PERSIST,
  255. safe_connection_write_cb, safe_conn);
  256. if (safe_conn->read_event == NULL || safe_conn->write_event == NULL) {
  257. log_warn(LD_BUG, "Could not set events for %d", (int)safe_conn->socket);
  258. safe_connection_unregister_events(safe_conn);
  259. tor_mutex_release(&safe_conn->lock);
  260. return E_ERROR;
  261. }
  262. safe_connection_refresh_events(safe_conn);
  263. tor_mutex_release(&safe_conn->lock);
  264. return E_SUCCESS;
  265. }
  266. static void
  267. safe_connection_refresh_events(safe_connection_t *safe_conn)
  268. {
  269. tor_assert(safe_conn != NULL);
  270. tor_mutex_acquire(&safe_conn->lock);
  271. tor_assert(safe_conn->is_read_wanted != NULL);
  272. tor_assert(safe_conn->is_write_wanted != NULL);
  273. if (safe_conn->read_event != NULL) {
  274. if (socket_rw_state_get(&safe_conn->read_allowed) &&
  275. safe_conn->is_read_wanted(safe_conn)) {
  276. event_add(safe_conn->read_event, NULL);
  277. } else {
  278. event_del(safe_conn->read_event);
  279. }
  280. }
  281. if (safe_conn->write_event != NULL) {
  282. if (socket_rw_state_get(&safe_conn->write_allowed) &&
  283. safe_conn->is_write_wanted(safe_conn)) {
  284. event_add(safe_conn->write_event, NULL);
  285. } else {
  286. event_del(safe_conn->write_event);
  287. }
  288. }
  289. tor_mutex_release(&safe_conn->lock);
  290. }
  291. void
  292. safe_connection_set_read_permission(safe_connection_t *safe_conn,
  293. bool read_allowed)
  294. {
  295. tor_assert(safe_conn != NULL);
  296. tor_mutex_acquire(&safe_conn->lock);
  297. socket_rw_state_set(&safe_conn->read_allowed, read_allowed, safe_conn);
  298. tor_mutex_release(&safe_conn->lock);
  299. }
  300. void
  301. safe_connection_set_write_permission(safe_connection_t *safe_conn,
  302. bool write_allowed)
  303. {
  304. tor_assert(safe_conn != NULL);
  305. tor_mutex_acquire(&safe_conn->lock);
  306. socket_rw_state_set(&safe_conn->write_allowed, write_allowed, safe_conn);
  307. tor_mutex_release(&safe_conn->lock);
  308. }
  309. void
  310. safe_connection_inbuf_modified(safe_connection_t *safe_conn)
  311. {
  312. tor_assert(safe_conn != NULL);
  313. tor_mutex_acquire(&safe_conn->lock);
  314. if (safe_conn->inbuf_modified_cb != NULL) {
  315. safe_conn->inbuf_modified_cb(safe_conn);
  316. }
  317. tor_mutex_release(&safe_conn->lock);
  318. }
  319. void
  320. safe_connection_outbuf_modified(safe_connection_t *safe_conn)
  321. {
  322. tor_assert(safe_conn != NULL);
  323. tor_mutex_acquire(&safe_conn->lock);
  324. if (safe_conn->outbuf_modified_cb != NULL) {
  325. safe_conn->outbuf_modified_cb(safe_conn);
  326. }
  327. tor_mutex_release(&safe_conn->lock);
  328. }
  329. //void
  330. //safe_connection_use_inbuf(safe_connection_t *safe_conn,
  331. // int (*f)(struct buf_t *, void *, void **),
  332. // void *data,
  333. // void **ret_val)
  334. //{
  335. // tor_assert(safe_conn != NULL);
  336. // tor_assert(f != NULL);
  337. // tor_mutex_acquire(&safe_conn->lock);
  338. //
  339. // int rv = f(safe_conn->inbuf, data, ret_val);
  340. //
  341. // tor_mutex_release(&safe_conn->lock);
  342. //
  343. // return rv;
  344. //}
  345. /********************************************************/
  346. safe_or_connection_t *
  347. safe_or_connection_new(bool requires_buffers, bool is_outgoing,
  348. const char *remote_address_str)
  349. {
  350. safe_or_connection_t *safe_or_conn = \
  351. tor_malloc_zero(sizeof(safe_or_connection_t));
  352. safe_connection_init(TO_SAFE_CONN(safe_or_conn),
  353. SAFE_OR_CONN_MAGIC,
  354. safe_or_connection_is_read_wanted,
  355. safe_or_connection_is_write_wanted,
  356. safe_or_connection_read_cb,
  357. safe_or_connection_write_cb,
  358. safe_or_connection_socket_added_cb,
  359. NULL,
  360. safe_or_connection_outbuf_modified_cb,
  361. requires_buffers, false);
  362. token_bucket_rw_init(&safe_or_conn->bucket, 1, 1, time(NULL));
  363. safe_or_conn->is_outgoing = is_outgoing;
  364. if (remote_address_str != NULL) {
  365. safe_or_conn->remote_address_str = \
  366. tor_strdup(escaped_safe_str(remote_address_str));
  367. // the function 'escaped_safe_str' must be run in the main thread
  368. } else {
  369. safe_or_conn->remote_address_str = NULL;
  370. log_warn(LD_OR, "No remote address string was provided");
  371. }
  372. safe_or_conn->link_protocol = 0; // unknown protocol
  373. safe_or_conn->wide_circ_ids = false;
  374. safe_or_conn->allowed_to_process_cells = true;
  375. // these states should be set by 'safe_or_connection_update_state()'
  376. socket_rw_state_init(&safe_or_conn->tor_read_wanted, false);
  377. socket_rw_state_init(&safe_or_conn->tor_write_wanted, false);
  378. socket_rw_state_init(&safe_or_conn->tls_read_wanted, false);
  379. socket_rw_state_init(&safe_or_conn->tls_write_wanted, false);
  380. socket_rw_state_init(&safe_or_conn->bucket_read_allowed, false);
  381. socket_rw_state_init(&safe_or_conn->bucket_write_allowed, false);
  382. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  383. tor_assert(safe_or_connection_update_state(safe_or_conn,
  384. SAFE_OR_CONN_STATE_NO_SOCKET) == E_SUCCESS);
  385. return safe_or_conn;
  386. }
  387. static void
  388. safe_or_connection_socket_added_cb(safe_connection_t *safe_conn)
  389. {
  390. tor_assert(safe_conn != NULL);
  391. tor_assert(safe_or_connection_update_state(TO_SAFE_OR_CONN(safe_conn),
  392. SAFE_OR_CONN_STATE_TCP_CONNECTING) == E_SUCCESS);
  393. // it might already be connected, but it should be fine to transition
  394. // through this state first
  395. }
  396. static void
  397. safe_or_connection_outbuf_modified_cb(safe_connection_t *safe_conn)
  398. {
  399. tor_assert(safe_conn != NULL);
  400. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  401. if (safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING ||
  402. safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN) {
  403. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) > 0) {
  404. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  405. TO_SAFE_CONN(safe_or_conn));
  406. }
  407. } else {
  408. log_warn(LD_OR, "The outbuf was modified when in a state where it "
  409. "shouldn't be modified (state %d)", safe_or_conn->state);
  410. }
  411. }
  412. static void
  413. safe_or_connection_refresh_bucket_rw_states(safe_or_connection_t *safe_or_conn)
  414. {
  415. if (token_bucket_rw_get_read(&safe_or_conn->bucket) > 0) {
  416. // token bucket is not empty, so we can read now
  417. socket_rw_state_set(&safe_or_conn->bucket_read_allowed, true,
  418. TO_SAFE_CONN(safe_or_conn));
  419. log_debug(LD_OR, "Token bucket for %p read is non-empty", safe_or_conn);
  420. } else {
  421. // token bucket is empty, so can't read now
  422. socket_rw_state_set(&safe_or_conn->bucket_read_allowed, false,
  423. TO_SAFE_CONN(safe_or_conn));
  424. log_debug(LD_OR, "Token bucket for %p read is empty", safe_or_conn);
  425. }
  426. if (token_bucket_rw_get_write(&safe_or_conn->bucket) > 0) {
  427. // token bucket is not empty, so we can write now
  428. socket_rw_state_set(&safe_or_conn->bucket_write_allowed, true,
  429. TO_SAFE_CONN(safe_or_conn));
  430. log_debug(LD_OR, "Token bucket for %p write is non-empty", safe_or_conn);
  431. } else {
  432. // token bucket is empty, so can't write now
  433. socket_rw_state_set(&safe_or_conn->bucket_write_allowed, false,
  434. TO_SAFE_CONN(safe_or_conn));
  435. log_debug(LD_OR, "Token bucket for %p write is empty", safe_or_conn);
  436. }
  437. }
  438. void
  439. safe_or_connection_set_link_protocol(safe_or_connection_t *safe_or_conn,
  440. uint16_t link_protocol)
  441. {
  442. tor_assert(safe_or_conn != NULL);
  443. tor_assert(link_protocol >= 3);
  444. tor_assert(safe_or_conn->allowed_to_process_cells == false);
  445. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  446. safe_or_conn->link_protocol = link_protocol;
  447. safe_or_conn->wide_circ_ids = (link_protocol >= 3);
  448. safe_or_conn->allowed_to_process_cells = true;
  449. event_active(TO_SAFE_CONN(safe_or_conn)->read_event, 0, 0);
  450. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  451. }
  452. // TODO: we should get rid of this at some point
  453. void
  454. safe_or_connection_get_tls_desc(safe_or_connection_t *safe_or_conn,
  455. char *buf, size_t buf_size)
  456. {
  457. tor_assert(safe_or_conn != NULL);
  458. tor_assert(buf != NULL);
  459. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  460. if (safe_or_conn->tls != NULL) {
  461. tor_tls_get_state_description(safe_or_conn->tls, buf, buf_size);
  462. } else {
  463. tor_snprintf(buf, buf_size, "<no tls object>");
  464. }
  465. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  466. }
  467. int
  468. safe_or_connection_tls_secrets(safe_or_connection_t *safe_or_conn,
  469. uint8_t *secrets_out)
  470. {
  471. tor_assert(safe_or_conn != NULL);
  472. tor_assert(secrets_out != NULL);
  473. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  474. tor_assert(safe_or_conn->tls != NULL);
  475. int rv = tor_tls_get_tlssecrets(safe_or_conn->tls, secrets_out);
  476. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  477. return rv;
  478. }
  479. int
  480. safe_or_connection_key_material(safe_or_connection_t *safe_or_conn,
  481. uint8_t *secrets_out,
  482. const uint8_t *context,
  483. size_t context_len, const char *label)
  484. {
  485. tor_assert(safe_or_conn != NULL);
  486. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  487. tor_assert(safe_or_conn->tls != NULL);
  488. int rv = tor_tls_export_key_material(safe_or_conn->tls, secrets_out,
  489. context, context_len, label);
  490. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  491. return rv;
  492. }
  493. void
  494. safe_or_connection_refill_buckets(safe_or_connection_t *safe_or_conn,
  495. uint32_t now_ts)
  496. {
  497. tor_assert(safe_or_conn != NULL);
  498. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  499. tor_assert(&safe_or_conn->bucket != NULL);
  500. token_bucket_rw_refill(&safe_or_conn->bucket, now_ts);
  501. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  502. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  503. }
  504. // TODO: this might be better implemented as a message so that we don't need
  505. // to wait for the lock (but would require us to add a listener to the
  506. // safe conn
  507. void
  508. safe_or_connection_adjust_buckets(safe_or_connection_t *safe_or_conn,
  509. uint32_t rate, uint32_t burst,
  510. bool reset, uint32_t now_ts)
  511. {
  512. tor_assert(safe_or_conn != NULL);
  513. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  514. tor_assert(&safe_or_conn->bucket != NULL);
  515. token_bucket_rw_adjust(&safe_or_conn->bucket, rate, burst);
  516. if (reset) {
  517. token_bucket_rw_reset(&safe_or_conn->bucket, now_ts);
  518. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  519. }
  520. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  521. }
  522. static void
  523. safe_or_connection_decrement_buckets(safe_or_connection_t *safe_or_conn,
  524. size_t num_read, size_t num_written,
  525. bool use_conn_buckets)
  526. {
  527. if (use_conn_buckets) {
  528. token_bucket_rw_dec(&safe_or_conn->bucket, num_read, num_written);
  529. }
  530. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  531. }
  532. static size_t
  533. safe_or_connection_max_bytes_can_read(safe_or_connection_t *safe_or_conn,
  534. bool use_conn_buckets)
  535. {
  536. // this function may become more complicated if we add support for global
  537. // buckets in the future
  538. // note: that would be a bad way to do it, since instead we should borrow
  539. // some space from the global bucket, and then commit it once the read
  540. // is actually finished
  541. if (use_conn_buckets) {
  542. return token_bucket_rw_get_read(&safe_or_conn->bucket);
  543. } else {
  544. return SIZE_MAX;
  545. }
  546. }
  547. static size_t
  548. safe_or_connection_max_bytes_can_write(safe_or_connection_t *safe_or_conn,
  549. bool use_conn_buckets)
  550. {
  551. // this function may become more complicated if we add support for global
  552. // buckets in the future
  553. // note: that would be a bad way to do it, since instead we should borrow
  554. // some space from the global bucket, and then commit it once the write
  555. // is actually finished
  556. if (use_conn_buckets) {
  557. return token_bucket_rw_get_write(&safe_or_conn->bucket);
  558. } else {
  559. return SIZE_MAX;
  560. }
  561. }
  562. static bool
  563. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn)
  564. {
  565. tor_assert(safe_conn != NULL);
  566. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  567. return socket_rw_state_get(&safe_or_conn->tls_read_wanted) ||
  568. (socket_rw_state_get(&safe_or_conn->tor_read_wanted) &&
  569. socket_rw_state_get(&safe_or_conn->bucket_read_allowed));
  570. }
  571. static bool
  572. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn)
  573. {
  574. tor_assert(safe_conn != NULL);
  575. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  576. return socket_rw_state_get(&safe_or_conn->tls_write_wanted) ||
  577. (socket_rw_state_get(&safe_or_conn->tor_write_wanted) &&
  578. socket_rw_state_get(&safe_or_conn->bucket_write_allowed));
  579. }
  580. static tor_error_t
  581. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  582. or_conn_state_t new_state)
  583. {
  584. if (new_state == safe_or_conn->state) {
  585. log_warn(LD_OR, "Trying to change to the current state (or_conn_state_t) "
  586. "of %d", new_state);
  587. }
  588. event_data_t null_data = { .ptr = NULL };
  589. // this is used by several cases below
  590. switch (new_state) {
  591. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  592. tor_assert_unreached();
  593. break;
  594. case SAFE_OR_CONN_STATE_NO_SOCKET:
  595. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  596. TO_SAFE_CONN(safe_or_conn));
  597. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  598. TO_SAFE_CONN(safe_or_conn));
  599. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  600. TO_SAFE_CONN(safe_or_conn));
  601. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  602. TO_SAFE_CONN(safe_or_conn));
  603. break;
  604. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  605. // the socket was EINPROGRESS, so wait for the socket to become
  606. // writable
  607. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  608. TO_SAFE_CONN(safe_or_conn));
  609. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  610. TO_SAFE_CONN(safe_or_conn));
  611. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  612. TO_SAFE_CONN(safe_or_conn));
  613. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  614. TO_SAFE_CONN(safe_or_conn));
  615. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  616. safe_or_conn_tcp_connecting_ev,
  617. null_data, NULL);
  618. break;
  619. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  620. log_warn(LD_OR, "Relay connection proxy handshake state has not yet "
  621. "been implemented");
  622. tor_assert(0);
  623. break;
  624. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  625. {
  626. // begin the handshake when either the socket is readable or
  627. // writable
  628. if (safe_or_conn->tls != NULL) {
  629. log_warn(LD_OR, "safe_or_conn->tls should not be set");
  630. return E_ERROR;
  631. }
  632. bool is_receiving = !safe_or_conn->is_outgoing;
  633. if (TO_SAFE_CONN(safe_or_conn)->socket == TOR_INVALID_SOCKET) {
  634. log_warn(LD_OR, "No socket was set yet");
  635. return E_ERROR;
  636. }
  637. safe_or_conn->tls = tor_tls_new(TO_SAFE_CONN(safe_or_conn)->socket,
  638. is_receiving);
  639. if (safe_or_conn->tls == NULL) {
  640. log_warn(LD_OR, "Could not create a new tor TLS object");
  641. return E_ERROR;
  642. }
  643. if (safe_or_conn->remote_address_str != NULL) {
  644. tor_tls_set_logged_address(safe_or_conn->tls,
  645. safe_or_conn->remote_address_str);
  646. }
  647. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  648. TO_SAFE_CONN(safe_or_conn));
  649. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  650. TO_SAFE_CONN(safe_or_conn));
  651. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  652. TO_SAFE_CONN(safe_or_conn));
  653. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  654. TO_SAFE_CONN(safe_or_conn));
  655. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  656. safe_or_conn_tls_handshaking_ev,
  657. null_data, NULL);
  658. break;
  659. }
  660. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  661. {
  662. if (safe_or_conn->tls == NULL) {
  663. log_warn(LD_OR, "safe_or_conn->tls was not set");
  664. return E_ERROR;
  665. }
  666. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  667. TO_SAFE_CONN(safe_or_conn));
  668. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  669. TO_SAFE_CONN(safe_or_conn));
  670. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  671. TO_SAFE_CONN(safe_or_conn));
  672. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  673. TO_SAFE_CONN(safe_or_conn));
  674. link_handshaking_ev_data *handshake_data = \
  675. tor_malloc_zero(sizeof(link_handshaking_ev_data));
  676. handshake_data->tls_own_cert = tor_tls_get_own_cert(safe_or_conn->tls);
  677. handshake_data->tls_peer_cert = tor_tls_get_peer_cert(safe_or_conn->tls);
  678. event_data_t ev_data = { .ptr = handshake_data };
  679. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  680. safe_or_conn_link_handshaking_ev,
  681. ev_data, link_handshaking_ev_free);
  682. break;
  683. }
  684. case SAFE_OR_CONN_STATE_OPEN:
  685. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  686. TO_SAFE_CONN(safe_or_conn));
  687. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  688. TO_SAFE_CONN(safe_or_conn));
  689. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  690. TO_SAFE_CONN(safe_or_conn));
  691. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  692. TO_SAFE_CONN(safe_or_conn));
  693. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  694. safe_or_conn_open_ev, null_data, NULL);
  695. break;
  696. case SAFE_OR_CONN_STATE_CLOSED:
  697. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  698. TO_SAFE_CONN(safe_or_conn));
  699. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  700. TO_SAFE_CONN(safe_or_conn));
  701. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  702. TO_SAFE_CONN(safe_or_conn));
  703. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  704. TO_SAFE_CONN(safe_or_conn));
  705. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  706. safe_or_conn_closed_ev, null_data, NULL);
  707. break;
  708. default:
  709. log_warn(LD_OR, "Unexpected state");
  710. tor_assert(0);
  711. break;
  712. }
  713. log_debug(LD_OR, "Safe OR conn changed from state %d to state %d",
  714. safe_or_conn->state, new_state);
  715. safe_or_conn->state = new_state;
  716. return E_SUCCESS;
  717. }
  718. static tor_error_t
  719. safe_or_connection_check_tcp_connection(safe_or_connection_t *safe_or_conn)
  720. {
  721. tor_assert(safe_or_conn != NULL);
  722. int e;
  723. socklen_t len = (socklen_t)sizeof(e);
  724. if (getsockopt(TO_SAFE_CONN(safe_or_conn)->socket, SOL_SOCKET, SO_ERROR,
  725. (void *)&e, &len) < 0) {
  726. log_warn(LD_BUG, "getsockopt() syscall failed");
  727. return E_ERROR;
  728. }
  729. if (e != 0) {
  730. // some sort of error, but maybe just inprogress still
  731. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  732. log_info(LD_NET, "In-progress connect failed. Removing. (%s)",
  733. tor_socket_strerror(e));
  734. return E_ERROR;
  735. } else {
  736. // no change, see if next time is better
  737. return E_SUCCESS;
  738. }
  739. }
  740. // there was no error
  741. return safe_or_connection_update_state(safe_or_conn,
  742. SAFE_OR_CONN_STATE_TLS_HANDSHAKING);
  743. }
  744. static int
  745. safe_or_connection_read_tls(safe_or_connection_t *safe_or_conn,
  746. size_t suggested_bytes_to_read,
  747. size_t *total_bytes_read)
  748. {
  749. tor_assert(safe_or_conn != NULL);
  750. tor_assert(suggested_bytes_to_read > 0);
  751. *total_bytes_read = 0;
  752. {
  753. size_t bytes_read = 0;
  754. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  755. safe_or_conn->tls,
  756. suggested_bytes_to_read,
  757. &bytes_read);
  758. *total_bytes_read += bytes_read;
  759. if (tls_rv != TOR_TLS_DONE) {
  760. return tls_rv;
  761. }
  762. }
  763. int pending_bytes_to_read = tor_tls_get_pending_bytes(safe_or_conn->tls);
  764. if (pending_bytes_to_read > 0) {
  765. size_t bytes_read = 0;
  766. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  767. safe_or_conn->tls,
  768. pending_bytes_to_read,
  769. &bytes_read);
  770. if (PREDICT_LIKELY(SIZE_MAX-(*total_bytes_read) > bytes_read)) {
  771. *total_bytes_read += bytes_read;
  772. } else {
  773. *total_bytes_read = SIZE_MAX;
  774. }
  775. tor_assert(tls_rv != TOR_TLS_WANTREAD && tls_rv != TOR_TLS_WANTWRITE);
  776. // we don't expect either of these when reading pending bytes
  777. if (tls_rv != TOR_TLS_DONE) {
  778. return tls_rv;
  779. }
  780. }
  781. return TOR_TLS_DONE;
  782. }
  783. static int
  784. safe_or_connection_write_tls(safe_or_connection_t *safe_or_conn,
  785. size_t max_bytes_to_write,
  786. size_t *total_bytes_written)
  787. {
  788. tor_assert(safe_or_conn != NULL);
  789. tor_assert(max_bytes_to_write > 0);
  790. *total_bytes_written = 0;
  791. size_t bytes_written = 0;
  792. int tls_rv = buf_flush_to_tls(TO_SAFE_CONN(safe_or_conn)->outbuf,
  793. safe_or_conn->tls,
  794. max_bytes_to_write,
  795. &bytes_written);
  796. *total_bytes_written += bytes_written;
  797. return tls_rv;
  798. }
  799. // this function will be needed when proxies are supported
  800. /*
  801. static tor_error_t
  802. safe_or_connection_read_plaintext(safe_or_connection_t *safe_or_conn)
  803. {
  804. tor_assert(safe_or_conn != NULL);
  805. uint32_t coarse_time = monotime_coarse_get_stamp();
  806. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  807. size_t bytes_to_read = safe_or_connection_max_bytes_can_read(safe_or_conn);
  808. if (bytes_to_read == 0) {
  809. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  810. return E_SUCCESS;
  811. }
  812. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  813. size_t bytes_read = 0;
  814. int reached_eof = 0;
  815. int socket_error = 0;
  816. // STEVE: if reusing this with control connections, then need to wrap
  817. // with 'CONN_LOG_PROTECT' (see connection.c,
  818. // !connection_speaks_cells, !conn->linked_conn. )
  819. int rv = buf_read_from_socket(TO_SAFE_CONN(safe_or_conn)->inbuf,
  820. TO_SAFE_CONN(safe_or_conn)->socket,
  821. bytes_to_read, &reached_eof,
  822. &socket_error);
  823. if (rv < 0) {
  824. log_debug(LD_NET, "OR plaintext connection closed on read error.");
  825. // TODO: need to send the socket_error back to the main thread
  826. return E_ERROR;
  827. } else if(rv == 0 && reached_eof != 0) {
  828. // close the connection normally
  829. log_debug(LD_NET, "OR plaintext connection closed on read eof.");
  830. // return an error so that the calling function will close it
  831. return E_ERROR;
  832. } else {
  833. bytes_read = rv;
  834. }
  835. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  836. tor_assert(bytes_read == \
  837. buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size);
  838. } else {
  839. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  840. ">= SIZE_MAX",
  841. bytes_read);
  842. }
  843. log_debug(LD_NET, "OR plaintext read of %zu", bytes_read);
  844. safe_or_connection_decrement_buckets(safe_or_conn, bytes_read, 0);
  845. return E_SUCCESS;
  846. }
  847. */
  848. static tor_error_t
  849. safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
  850. bool use_conn_buckets)
  851. {
  852. tor_assert(safe_or_conn != NULL);
  853. uint32_t coarse_time = monotime_coarse_get_stamp();
  854. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  855. size_t suggested_bytes_to_read = \
  856. safe_or_connection_max_bytes_can_read(safe_or_conn, use_conn_buckets);
  857. // we may read slightly more than this due to pending TLS bytes
  858. if (suggested_bytes_to_read == 0) {
  859. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  860. return E_SUCCESS;
  861. }
  862. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  863. size_t bytes_read = 0;
  864. int tls_rv = safe_or_connection_read_tls(safe_or_conn,
  865. suggested_bytes_to_read,
  866. &bytes_read);
  867. switch (tls_rv) {
  868. case TOR_TLS_CLOSE:
  869. case TOR_TLS_ERROR_IO:
  870. log_debug(LD_NET, "TLS connection closed %son read. Closing.",
  871. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  872. return E_ERROR;
  873. CASE_TOR_TLS_ERROR_ANY_NONIO:
  874. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  875. tor_tls_err_to_string(tls_rv));
  876. return E_ERROR;
  877. case TOR_TLS_WANTWRITE:
  878. // we need to wait for the socket to become writable
  879. // before we can do another read
  880. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  881. TO_SAFE_CONN(safe_or_conn));
  882. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  883. TO_SAFE_CONN(safe_or_conn));
  884. break;
  885. case TOR_TLS_WANTREAD:
  886. // we need to wait for the socket to become readable
  887. // again, then do another read
  888. break;
  889. default:
  890. break;
  891. }
  892. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  893. size_t buf_len_diff = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size;
  894. if (bytes_read != buf_len_diff) {
  895. log_warn(LD_OR, "Doesn't match! bytes_read: %zu, buf_len_diff: %zu",
  896. bytes_read, buf_len_diff);
  897. tor_assert_nonfatal_unreached_once();
  898. }
  899. } else {
  900. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  901. ">= SIZE_MAX",
  902. bytes_read);
  903. }
  904. size_t tls_bytes_read = 0;
  905. size_t tls_bytes_written = 0;
  906. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  907. &tls_bytes_written);
  908. log_debug(LD_NET, "After TLS read of %zu: %zu read, %zu written",
  909. bytes_read, tls_bytes_read, tls_bytes_written);
  910. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  911. tls_bytes_written, use_conn_buckets);
  912. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  913. return E_SUCCESS;
  914. }
  915. static tor_error_t
  916. safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
  917. bool use_conn_buckets)
  918. {
  919. tor_assert(safe_or_conn != NULL);
  920. uint32_t coarse_time = monotime_coarse_get_stamp();
  921. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  922. size_t max_bytes_to_write = \
  923. safe_or_connection_max_bytes_can_write(safe_or_conn, use_conn_buckets);
  924. if (max_bytes_to_write == 0) {
  925. log_debug(LD_NET, "Write callback running, but not supposed to write bytes.");
  926. return E_SUCCESS;
  927. }
  928. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  929. size_t bytes_written = 0;
  930. max_bytes_to_write = MIN(max_bytes_to_write, buf_initial_size);
  931. int tls_rv = safe_or_connection_write_tls(safe_or_conn,
  932. max_bytes_to_write,
  933. &bytes_written);
  934. switch (tls_rv) {
  935. case TOR_TLS_CLOSE:
  936. case TOR_TLS_ERROR_IO:
  937. log_debug(LD_NET, "TLS connection closed %son write. Closing.",
  938. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  939. return E_ERROR;
  940. CASE_TOR_TLS_ERROR_ANY_NONIO:
  941. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  942. tor_tls_err_to_string(tls_rv));
  943. return E_ERROR;
  944. case TOR_TLS_WANTWRITE:
  945. // we need to wait for the socket to become writable
  946. // again, then do another write
  947. break;
  948. case TOR_TLS_WANTREAD:
  949. // we need to wait for the socket to become readable
  950. // before we can do another write
  951. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  952. TO_SAFE_CONN(safe_or_conn));
  953. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  954. TO_SAFE_CONN(safe_or_conn));
  955. break;
  956. default:
  957. break;
  958. }
  959. if (PREDICT_LIKELY(bytes_written < SIZE_MAX)) {
  960. size_t buf_len_diff = buf_initial_size-buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  961. if (bytes_written != buf_len_diff) {
  962. log_warn(LD_OR, "Doesn't match! bytes_written: %zu, buf_len_diff: %zu",
  963. bytes_written, buf_len_diff);
  964. tor_assert_nonfatal_unreached_once();
  965. }
  966. } else {
  967. log_warn(LD_NET, "We wrote an unexpectedly large number of bytes: %zu "
  968. ">= SIZE_MAX",
  969. bytes_written);
  970. }
  971. // fixes a throughput problem in old versions of Windows
  972. // TODO: we should still include this, but needs to be moved here since it's
  973. // currently static
  974. //update_send_buffer_size(TO_SAFE_CONN(safe_or_conn)->socket);
  975. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) == 0) {
  976. // we have no more data to write
  977. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  978. TO_SAFE_CONN(safe_or_conn));
  979. }
  980. size_t tls_bytes_read = 0;
  981. size_t tls_bytes_written = 0;
  982. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  983. &tls_bytes_written);
  984. log_debug(LD_NET, "After TLS write of %zu: %zu read, %zu written",
  985. bytes_written, tls_bytes_read, tls_bytes_written);
  986. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  987. tls_bytes_written, use_conn_buckets);
  988. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  989. return E_SUCCESS;
  990. }
  991. static tor_error_t
  992. safe_or_connection_tls_handshake(safe_or_connection_t *safe_or_conn)
  993. {
  994. tor_assert(safe_or_conn != NULL);
  995. check_no_tls_errors();
  996. int result = tor_tls_handshake(safe_or_conn->tls);
  997. switch (result) {
  998. CASE_TOR_TLS_ERROR_ANY:
  999. log_info(LD_OR, "TLS error [%s]",
  1000. tor_tls_err_to_string(result));
  1001. return E_ERROR;
  1002. case TOR_TLS_CLOSE:
  1003. log_info(LD_OR, "TLS closed");
  1004. return E_ERROR;
  1005. case TOR_TLS_WANTWRITE:
  1006. // we need to wait for the socket to become writable
  1007. // before we can continue the handshake
  1008. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  1009. TO_SAFE_CONN(safe_or_conn));
  1010. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1011. TO_SAFE_CONN(safe_or_conn));
  1012. return E_SUCCESS;
  1013. case TOR_TLS_WANTREAD:
  1014. // we need to wait for the socket to become readable
  1015. // before we can continue the handshake
  1016. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1017. TO_SAFE_CONN(safe_or_conn));
  1018. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1019. TO_SAFE_CONN(safe_or_conn));
  1020. return E_SUCCESS;
  1021. case TOR_TLS_DONE:
  1022. // the TLS handshake has finished, but not the entire link handshake
  1023. if (tor_tls_is_server(safe_or_conn->tls)) {
  1024. // we didn't start the handshake, so prepare for a v3 handshake
  1025. log_debug(LD_OR, "Done with initial SSL handshake (receiver-side)");
  1026. } else {
  1027. // we need to start the v3 handshake
  1028. log_debug(LD_OR, "Done with initial SSL handshake (initiator-side)");
  1029. //if (connection_or_launch_v3_or_handshake(conn) < 0) {
  1030. // return E_ERROR;
  1031. //}
  1032. }
  1033. return safe_or_connection_update_state(safe_or_conn,
  1034. SAFE_OR_CONN_STATE_LINK_HANDSHAKING);
  1035. default:
  1036. log_warn(LD_OR, "Unexpected return value from handshake");
  1037. return E_ERROR;
  1038. }
  1039. }
  1040. /*
  1041. static int
  1042. safe_or_connection_tls_finish_v1_handshake(safe_or_connection_t *safe_or_conn)
  1043. {
  1044. tor_assert(safe_or_conn != NULL);
  1045. tor_assert(tor_tls_used_v1_handshake(safe_or_conn->tls));
  1046. tor_assert(tor_tls_is_server(safe_or_conn->tls));
  1047. tor_assert(!safe_or_conn->is_outgoing);
  1048. // we should not be making v1 handshakes, but we may receive v1 handshakes
  1049. log_debug(LD_HANDSHAKE, "%s tls v1 handshake on %p with %s done, using "
  1050. "ciphersuite %s. verifying.",
  1051. safe_or_conn->is_outgoing?"Outgoing":"Incoming",
  1052. safe_or_conn,
  1053. safe_or_conn->remote_address_str,
  1054. tor_tls_get_ciphersuite_name(safe_or_conn->tls));
  1055. //tor_tls_block_renegotiation(safe_or_conn->tls);
  1056. char digest_rcvd[DIGEST_LEN] = {0};
  1057. // TODO fix below
  1058. if (connection_or_check_valid_tls_handshake(conn, started_here,
  1059. digest_rcvd) < 0) {
  1060. return -1;
  1061. }
  1062. // TODO in main thread
  1063. //circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1064. //conn->link_proto = 1;
  1065. //connection_or_init_conn_from_address(conn, &conn->base_.addr,
  1066. // conn->base_.port, digest_rcvd,
  1067. // NULL, 0);
  1068. //rep_hist_note_negotiated_link_proto(1, started_here);
  1069. //return connection_or_set_state_open(conn);
  1070. return 0;
  1071. }
  1072. */
  1073. static void
  1074. safe_or_connection_read_cb(safe_connection_t *safe_conn)
  1075. {
  1076. tor_assert(safe_conn != NULL);
  1077. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1078. log_debug(LD_OR, "OR connection read cb (state=%d, obj=%p, %s)",
  1079. safe_or_conn->state, safe_or_conn,
  1080. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1081. //if (safe_or_conn->tls_write_waiting_on_socket_readable) {
  1082. // // since the socket is now readable, we can re-enable TLS write again
  1083. // safe_or_conn->tls_write_waiting_on_socket_readable = false;
  1084. // safe_connection_set_write_state(TO_SAFE_CONN(safe_or_conn), true);
  1085. //}
  1086. switch (safe_or_conn->state) {
  1087. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1088. tor_assert_unreached();
  1089. break;
  1090. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1091. // we shouldn't get here, so make sure we're not wanting to read
  1092. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1093. TO_SAFE_CONN(safe_or_conn));
  1094. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1095. TO_SAFE_CONN(safe_or_conn));
  1096. log_warn(LD_OR, "Connecting OR conection wants to read");
  1097. break;
  1098. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1099. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1100. "been implemented");
  1101. tor_assert(0);
  1102. // we are performing the proxy handshake
  1103. //tor_error_t rv = safe_or_connection_plaintext(safe_or_conn);
  1104. //if (rv != E_SUCCESS) {
  1105. // tor_assert(safe_or_connection_update_state(safe_or_conn,
  1106. // SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1107. //}
  1108. break;
  1109. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1110. {
  1111. // we are performing the initial TLS handshake
  1112. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1113. if (rv != E_SUCCESS) {
  1114. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1115. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1116. }
  1117. break;
  1118. }
  1119. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1120. case SAFE_OR_CONN_STATE_OPEN:
  1121. {
  1122. // performing the link handshake, or the handshake has already
  1123. // completed and we're sending/receiving cells
  1124. if (socket_rw_state_get(&safe_or_conn->tls_read_wanted)) {
  1125. // since the socket is now readable, we can re-enable writing again
  1126. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1127. TO_SAFE_CONN(safe_or_conn));
  1128. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1129. TO_SAFE_CONN(safe_or_conn));
  1130. }
  1131. // TODO: we may not actually want to read here now that the states are
  1132. // updated, should we re-check?
  1133. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1134. tor_error_t rv = safe_or_connection_read_encrypted(safe_or_conn,
  1135. use_conn_buckets);
  1136. if (rv != E_SUCCESS) {
  1137. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1138. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1139. }
  1140. if (safe_or_conn->allowed_to_process_cells) {
  1141. process_cells_from_inbuf(safe_or_conn);
  1142. }
  1143. break;
  1144. }
  1145. case SAFE_OR_CONN_STATE_CLOSED:
  1146. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1147. // we shouldn't get here, so make sure we're not wanting to read
  1148. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1149. TO_SAFE_CONN(safe_or_conn));
  1150. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1151. TO_SAFE_CONN(safe_or_conn));
  1152. log_warn(LD_OR, "Closed OR conection wants to read");
  1153. break;
  1154. default:
  1155. log_warn(LD_OR, "Unexpected safe OR connection state");
  1156. tor_assert(0);
  1157. break;
  1158. }
  1159. }
  1160. static void
  1161. safe_or_connection_write_cb(safe_connection_t *safe_conn)
  1162. {
  1163. tor_assert(safe_conn != NULL);
  1164. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1165. log_debug(LD_OR, "OR connection write cb (state=%d, obj=%p, %s)",
  1166. safe_or_conn->state, safe_or_conn,
  1167. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1168. switch (safe_or_conn->state) {
  1169. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1170. tor_assert_unreached();
  1171. break;
  1172. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1173. {
  1174. // the socket was connecting and is now ready to write, so we
  1175. // should check for errors before using the socket
  1176. tor_error_t rv = safe_or_connection_check_tcp_connection(safe_or_conn);
  1177. if (rv != E_SUCCESS) {
  1178. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1179. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1180. }
  1181. break;
  1182. }
  1183. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1184. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1185. "been implemented");
  1186. tor_assert(0);
  1187. // we are performing the proxy handshake
  1188. break;
  1189. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1190. {
  1191. // we are performing the initial TLS handshake
  1192. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1193. if (rv != E_SUCCESS) {
  1194. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1195. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1196. }
  1197. break;
  1198. }
  1199. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1200. case SAFE_OR_CONN_STATE_OPEN:
  1201. {
  1202. // performing the link handshake, or the handshake has already
  1203. // completed and we're sending/receiving cells
  1204. if (socket_rw_state_get(&safe_or_conn->tls_write_wanted)) {
  1205. // since the socket is now writable, we can re-enable reading again
  1206. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1207. TO_SAFE_CONN(safe_or_conn));
  1208. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  1209. TO_SAFE_CONN(safe_or_conn));
  1210. }
  1211. // TODO: we may not actually want to write here now that the states are
  1212. // updated, should we re-check?
  1213. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1214. tor_error_t rv = safe_or_connection_write_encrypted(safe_or_conn,
  1215. use_conn_buckets);
  1216. if (rv != E_SUCCESS) {
  1217. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1218. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1219. }
  1220. break;
  1221. }
  1222. case SAFE_OR_CONN_STATE_CLOSED:
  1223. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1224. // we shouldn't get here, so make sure we're not wanting to write
  1225. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1226. TO_SAFE_CONN(safe_or_conn));
  1227. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1228. TO_SAFE_CONN(safe_or_conn));
  1229. log_warn(LD_OR, "Closed OR conection wants to write");
  1230. break;
  1231. default:
  1232. log_warn(LD_OR, "Unexpected safe OR connection state");
  1233. tor_assert(0);
  1234. break;
  1235. }
  1236. }
  1237. /********************************************************/
  1238. static bool
  1239. fetch_cell(safe_or_connection_t *safe_or_conn, char *cell_buf)
  1240. {
  1241. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1242. size_t cell_network_size = \
  1243. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1244. if (buf_datalen(safe_conn->inbuf) < cell_network_size) {
  1245. // don't have a full cell
  1246. return false;
  1247. }
  1248. buf_get_bytes(safe_conn->inbuf, cell_buf, cell_network_size);
  1249. safe_connection_inbuf_modified(safe_conn);
  1250. return true;
  1251. }
  1252. static bool
  1253. fetch_var_cell(safe_or_connection_t *safe_or_conn, var_cell_t **var_cell_ptr)
  1254. {
  1255. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1256. int link_protocol = safe_or_conn->link_protocol;
  1257. *var_cell_ptr = NULL;
  1258. int found_var_cell = fetch_var_cell_from_buf(safe_conn->inbuf, var_cell_ptr,
  1259. link_protocol);
  1260. if (*var_cell_ptr != NULL) {
  1261. // there was not a *full* cell
  1262. safe_connection_inbuf_modified(safe_conn);
  1263. }
  1264. return (found_var_cell != 0);
  1265. }
  1266. static void
  1267. void_var_cell_free(void *void_var_cell)
  1268. {
  1269. var_cell_free_((var_cell_t *)void_var_cell);
  1270. }
  1271. static void
  1272. process_cells_from_inbuf(safe_or_connection_t *safe_or_conn)
  1273. {
  1274. tor_assert(safe_or_conn != NULL);
  1275. while (true) {
  1276. var_cell_t *var_cell = NULL;
  1277. bool found_var_cell = fetch_var_cell(safe_or_conn, &var_cell);
  1278. if (found_var_cell) {
  1279. if (var_cell == NULL) {
  1280. // the next cell is a var cell, but it is not yet complete
  1281. return;
  1282. }
  1283. event_data_t event_data = { .ptr = var_cell };
  1284. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1285. safe_or_conn_var_cell_ev, event_data,
  1286. void_var_cell_free);
  1287. if (safe_or_conn->link_protocol == 0 &&
  1288. var_cell->command == CELL_VERSIONS) {
  1289. // this is the first VERSIONS cell we've received;
  1290. // in order to process future cells, we need to be told our
  1291. // protocol version
  1292. safe_or_conn->allowed_to_process_cells = false;
  1293. return;
  1294. }
  1295. } else {
  1296. char buf[CELL_MAX_NETWORK_SIZE];
  1297. bool found_cell = fetch_cell(safe_or_conn, buf);
  1298. if (found_cell) {
  1299. // retrieve cell info from buf (create the host-order struct from the
  1300. // network-order string)
  1301. cell_t *cell = tor_malloc(sizeof(cell_t));
  1302. cell_unpack(cell, buf, safe_or_conn->wide_circ_ids?1:0);
  1303. event_data_t event_data = { .ptr = cell };
  1304. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1305. safe_or_conn_fixed_cell_ev, event_data,
  1306. tor_free_);
  1307. } else {
  1308. // there is not yet a complete cell
  1309. return;
  1310. }
  1311. }
  1312. }
  1313. }