safe_connection.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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_warn(LD_OR, "Safe OR conn changed from state %d to state %d",
  714. safe_or_conn->state, new_state);
  715. // TODO: change back to debug ^^
  716. safe_or_conn->state = new_state;
  717. return E_SUCCESS;
  718. }
  719. static tor_error_t
  720. safe_or_connection_check_tcp_connection(safe_or_connection_t *safe_or_conn)
  721. {
  722. tor_assert(safe_or_conn != NULL);
  723. int e;
  724. socklen_t len = (socklen_t)sizeof(e);
  725. if (getsockopt(TO_SAFE_CONN(safe_or_conn)->socket, SOL_SOCKET, SO_ERROR,
  726. (void *)&e, &len) < 0) {
  727. log_warn(LD_BUG, "getsockopt() syscall failed");
  728. return E_ERROR;
  729. }
  730. if (e != 0) {
  731. // some sort of error, but maybe just inprogress still
  732. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  733. log_info(LD_NET, "In-progress connect failed. Removing. (%s)",
  734. tor_socket_strerror(e));
  735. return E_ERROR;
  736. } else {
  737. // no change, see if next time is better
  738. return E_SUCCESS;
  739. }
  740. }
  741. // there was no error
  742. return safe_or_connection_update_state(safe_or_conn,
  743. SAFE_OR_CONN_STATE_TLS_HANDSHAKING);
  744. }
  745. static int
  746. safe_or_connection_read_tls(safe_or_connection_t *safe_or_conn,
  747. size_t suggested_bytes_to_read,
  748. size_t *total_bytes_read)
  749. {
  750. tor_assert(safe_or_conn != NULL);
  751. tor_assert(suggested_bytes_to_read > 0);
  752. *total_bytes_read = 0;
  753. {
  754. size_t bytes_read = 0;
  755. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  756. safe_or_conn->tls,
  757. suggested_bytes_to_read,
  758. &bytes_read);
  759. *total_bytes_read += bytes_read;
  760. if (tls_rv != TOR_TLS_DONE) {
  761. return tls_rv;
  762. }
  763. }
  764. int pending_bytes_to_read = tor_tls_get_pending_bytes(safe_or_conn->tls);
  765. if (pending_bytes_to_read > 0) {
  766. size_t bytes_read = 0;
  767. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  768. safe_or_conn->tls,
  769. pending_bytes_to_read,
  770. &bytes_read);
  771. if (PREDICT_LIKELY(SIZE_MAX-(*total_bytes_read) > bytes_read)) {
  772. *total_bytes_read += bytes_read;
  773. } else {
  774. *total_bytes_read = SIZE_MAX;
  775. }
  776. tor_assert(tls_rv != TOR_TLS_WANTREAD && tls_rv != TOR_TLS_WANTWRITE);
  777. // we don't expect either of these when reading pending bytes
  778. if (tls_rv != TOR_TLS_DONE) {
  779. return tls_rv;
  780. }
  781. }
  782. return TOR_TLS_DONE;
  783. }
  784. static int
  785. safe_or_connection_write_tls(safe_or_connection_t *safe_or_conn,
  786. size_t max_bytes_to_write,
  787. size_t *total_bytes_written)
  788. {
  789. tor_assert(safe_or_conn != NULL);
  790. tor_assert(max_bytes_to_write > 0);
  791. *total_bytes_written = 0;
  792. size_t bytes_written = 0;
  793. int tls_rv = buf_flush_to_tls(TO_SAFE_CONN(safe_or_conn)->outbuf,
  794. safe_or_conn->tls,
  795. max_bytes_to_write,
  796. &bytes_written);
  797. *total_bytes_written += bytes_written;
  798. return tls_rv;
  799. }
  800. // this function will be needed when proxies are supported
  801. /*
  802. static tor_error_t
  803. safe_or_connection_read_plaintext(safe_or_connection_t *safe_or_conn)
  804. {
  805. tor_assert(safe_or_conn != NULL);
  806. uint32_t coarse_time = monotime_coarse_get_stamp();
  807. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  808. size_t bytes_to_read = safe_or_connection_max_bytes_can_read(safe_or_conn);
  809. if (bytes_to_read == 0) {
  810. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  811. return E_SUCCESS;
  812. }
  813. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  814. size_t bytes_read = 0;
  815. int reached_eof = 0;
  816. int socket_error = 0;
  817. // STEVE: if reusing this with control connections, then need to wrap
  818. // with 'CONN_LOG_PROTECT' (see connection.c,
  819. // !connection_speaks_cells, !conn->linked_conn. )
  820. int rv = buf_read_from_socket(TO_SAFE_CONN(safe_or_conn)->inbuf,
  821. TO_SAFE_CONN(safe_or_conn)->socket,
  822. bytes_to_read, &reached_eof,
  823. &socket_error);
  824. if (rv < 0) {
  825. log_debug(LD_NET, "OR plaintext connection closed on read error.");
  826. // TODO: need to send the socket_error back to the main thread
  827. return E_ERROR;
  828. } else if(rv == 0 && reached_eof != 0) {
  829. // close the connection normally
  830. log_debug(LD_NET, "OR plaintext connection closed on read eof.");
  831. // return an error so that the calling function will close it
  832. return E_ERROR;
  833. } else {
  834. bytes_read = rv;
  835. }
  836. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  837. tor_assert(bytes_read == \
  838. buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size);
  839. } else {
  840. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  841. ">= SIZE_MAX",
  842. bytes_read);
  843. }
  844. log_debug(LD_NET, "OR plaintext read of %zu", bytes_read);
  845. safe_or_connection_decrement_buckets(safe_or_conn, bytes_read, 0);
  846. return E_SUCCESS;
  847. }
  848. */
  849. static tor_error_t
  850. safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
  851. bool use_conn_buckets)
  852. {
  853. tor_assert(safe_or_conn != NULL);
  854. uint32_t coarse_time = monotime_coarse_get_stamp();
  855. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  856. size_t suggested_bytes_to_read = \
  857. safe_or_connection_max_bytes_can_read(safe_or_conn, use_conn_buckets);
  858. // we may read slightly more than this due to pending TLS bytes
  859. log_warn(LD_NET, "suggested_bytes_to_read: %zu", suggested_bytes_to_read);
  860. // TODO: remove this ^^
  861. if (suggested_bytes_to_read == 0) {
  862. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  863. return E_SUCCESS;
  864. }
  865. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  866. size_t bytes_read = 0;
  867. int tls_rv = safe_or_connection_read_tls(safe_or_conn,
  868. suggested_bytes_to_read,
  869. &bytes_read);
  870. switch (tls_rv) {
  871. case TOR_TLS_CLOSE:
  872. case TOR_TLS_ERROR_IO:
  873. log_debug(LD_NET, "TLS connection closed %son read. Closing.",
  874. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  875. return E_ERROR;
  876. CASE_TOR_TLS_ERROR_ANY_NONIO:
  877. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  878. tor_tls_err_to_string(tls_rv));
  879. return E_ERROR;
  880. case TOR_TLS_WANTWRITE:
  881. // we need to wait for the socket to become writable
  882. // before we can do another read
  883. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  884. TO_SAFE_CONN(safe_or_conn));
  885. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  886. TO_SAFE_CONN(safe_or_conn));
  887. break;
  888. case TOR_TLS_WANTREAD:
  889. // we need to wait for the socket to become readable
  890. // again, then do another read
  891. break;
  892. default:
  893. break;
  894. }
  895. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  896. size_t buf_len_diff = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size;
  897. if (bytes_read != buf_len_diff) {
  898. log_warn(LD_OR, "Doesn't match! bytes_read: %zu, buf_len_diff: %zu",
  899. bytes_read, buf_len_diff);
  900. tor_assert_nonfatal_unreached_once();
  901. }
  902. } else {
  903. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  904. ">= SIZE_MAX",
  905. bytes_read);
  906. }
  907. size_t tls_bytes_read = 0;
  908. size_t tls_bytes_written = 0;
  909. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  910. &tls_bytes_written);
  911. log_warn(LD_NET, "After TLS read of %zu: %zu read, %zu written",
  912. bytes_read, tls_bytes_read, tls_bytes_written);
  913. // TODO: change this back to debug ^^
  914. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  915. tls_bytes_written, use_conn_buckets);
  916. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  917. return E_SUCCESS;
  918. }
  919. static tor_error_t
  920. safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
  921. bool use_conn_buckets)
  922. {
  923. tor_assert(safe_or_conn != NULL);
  924. uint32_t coarse_time = monotime_coarse_get_stamp();
  925. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  926. size_t max_bytes_to_write = \
  927. safe_or_connection_max_bytes_can_write(safe_or_conn, use_conn_buckets);
  928. log_warn(LD_NET, "max_bytes_to_write: %zu", max_bytes_to_write);
  929. // TODO: remove this ^^
  930. if (max_bytes_to_write == 0) {
  931. log_debug(LD_NET, "Write callback running, but not supposed to write bytes.");
  932. return E_SUCCESS;
  933. }
  934. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  935. size_t bytes_written = 0;
  936. max_bytes_to_write = MIN(max_bytes_to_write, buf_initial_size);
  937. int tls_rv = safe_or_connection_write_tls(safe_or_conn,
  938. max_bytes_to_write,
  939. &bytes_written);
  940. switch (tls_rv) {
  941. case TOR_TLS_CLOSE:
  942. case TOR_TLS_ERROR_IO:
  943. log_debug(LD_NET, "TLS connection closed %son write. Closing.",
  944. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  945. return E_ERROR;
  946. CASE_TOR_TLS_ERROR_ANY_NONIO:
  947. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  948. tor_tls_err_to_string(tls_rv));
  949. return E_ERROR;
  950. case TOR_TLS_WANTWRITE:
  951. // we need to wait for the socket to become writable
  952. // again, then do another write
  953. log_warn(LD_NET, "Wants write");
  954. // TODO: remove this ^^
  955. break;
  956. case TOR_TLS_WANTREAD:
  957. // we need to wait for the socket to become readable
  958. // before we can do another write
  959. log_warn(LD_NET, "Wants read");
  960. // TODO: remove this ^^
  961. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  962. TO_SAFE_CONN(safe_or_conn));
  963. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  964. TO_SAFE_CONN(safe_or_conn));
  965. break;
  966. default:
  967. break;
  968. }
  969. if (PREDICT_LIKELY(bytes_written < SIZE_MAX)) {
  970. size_t buf_len_diff = buf_initial_size-buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  971. if (bytes_written != buf_len_diff) {
  972. log_warn(LD_OR, "Doesn't match! bytes_written: %zu, buf_len_diff: %zu",
  973. bytes_written, buf_len_diff);
  974. tor_assert_nonfatal_unreached_once();
  975. }
  976. } else {
  977. log_warn(LD_NET, "We wrote an unexpectedly large number of bytes: %zu "
  978. ">= SIZE_MAX",
  979. bytes_written);
  980. }
  981. // fixes a throughput problem in old versions of Windows
  982. // TODO: we should still include this, but needs to be moved here since it's
  983. // currently static
  984. //update_send_buffer_size(TO_SAFE_CONN(safe_or_conn)->socket);
  985. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) == 0) {
  986. // we have no more data to write
  987. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  988. TO_SAFE_CONN(safe_or_conn));
  989. }
  990. size_t tls_bytes_read = 0;
  991. size_t tls_bytes_written = 0;
  992. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  993. &tls_bytes_written);
  994. log_warn(LD_NET, "After TLS write of %zu: %zu read, %zu written",
  995. bytes_written, tls_bytes_read, tls_bytes_written);
  996. // TODO: change this back to debug ^^
  997. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  998. tls_bytes_written, use_conn_buckets);
  999. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  1000. return E_SUCCESS;
  1001. }
  1002. static tor_error_t
  1003. safe_or_connection_tls_handshake(safe_or_connection_t *safe_or_conn)
  1004. {
  1005. tor_assert(safe_or_conn != NULL);
  1006. check_no_tls_errors();
  1007. int result = tor_tls_handshake(safe_or_conn->tls);
  1008. switch (result) {
  1009. CASE_TOR_TLS_ERROR_ANY:
  1010. log_info(LD_OR, "TLS error [%s]",
  1011. tor_tls_err_to_string(result));
  1012. return E_ERROR;
  1013. case TOR_TLS_CLOSE:
  1014. log_info(LD_OR, "TLS closed");
  1015. return E_ERROR;
  1016. case TOR_TLS_WANTWRITE:
  1017. // we need to wait for the socket to become writable
  1018. // before we can continue the handshake
  1019. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  1020. TO_SAFE_CONN(safe_or_conn));
  1021. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1022. TO_SAFE_CONN(safe_or_conn));
  1023. return E_SUCCESS;
  1024. case TOR_TLS_WANTREAD:
  1025. // we need to wait for the socket to become readable
  1026. // before we can continue the handshake
  1027. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1028. TO_SAFE_CONN(safe_or_conn));
  1029. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1030. TO_SAFE_CONN(safe_or_conn));
  1031. return E_SUCCESS;
  1032. case TOR_TLS_DONE:
  1033. // the TLS handshake has finished, but not the entire link handshake
  1034. if (tor_tls_is_server(safe_or_conn->tls)) {
  1035. // we didn't start the handshake, so prepare for a v3 handshake
  1036. log_debug(LD_OR, "Done with initial SSL handshake (receiver-side)");
  1037. } else {
  1038. // we need to start the v3 handshake
  1039. log_debug(LD_OR, "Done with initial SSL handshake (initiator-side)");
  1040. //if (connection_or_launch_v3_or_handshake(conn) < 0) {
  1041. // return E_ERROR;
  1042. //}
  1043. }
  1044. return safe_or_connection_update_state(safe_or_conn,
  1045. SAFE_OR_CONN_STATE_LINK_HANDSHAKING);
  1046. default:
  1047. log_warn(LD_OR, "Unexpected return value from handshake");
  1048. return E_ERROR;
  1049. }
  1050. }
  1051. /*
  1052. static int
  1053. safe_or_connection_tls_finish_v1_handshake(safe_or_connection_t *safe_or_conn)
  1054. {
  1055. tor_assert(safe_or_conn != NULL);
  1056. tor_assert(tor_tls_used_v1_handshake(safe_or_conn->tls));
  1057. tor_assert(tor_tls_is_server(safe_or_conn->tls));
  1058. tor_assert(!safe_or_conn->is_outgoing);
  1059. // we should not be making v1 handshakes, but we may receive v1 handshakes
  1060. log_debug(LD_HANDSHAKE, "%s tls v1 handshake on %p with %s done, using "
  1061. "ciphersuite %s. verifying.",
  1062. safe_or_conn->is_outgoing?"Outgoing":"Incoming",
  1063. safe_or_conn,
  1064. safe_or_conn->remote_address_str,
  1065. tor_tls_get_ciphersuite_name(safe_or_conn->tls));
  1066. //tor_tls_block_renegotiation(safe_or_conn->tls);
  1067. char digest_rcvd[DIGEST_LEN] = {0};
  1068. // TODO fix below
  1069. if (connection_or_check_valid_tls_handshake(conn, started_here,
  1070. digest_rcvd) < 0) {
  1071. return -1;
  1072. }
  1073. // TODO in main thread
  1074. //circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1075. //conn->link_proto = 1;
  1076. //connection_or_init_conn_from_address(conn, &conn->base_.addr,
  1077. // conn->base_.port, digest_rcvd,
  1078. // NULL, 0);
  1079. //rep_hist_note_negotiated_link_proto(1, started_here);
  1080. //return connection_or_set_state_open(conn);
  1081. return 0;
  1082. }
  1083. */
  1084. static void
  1085. safe_or_connection_read_cb(safe_connection_t *safe_conn)
  1086. {
  1087. tor_assert(safe_conn != NULL);
  1088. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1089. log_warn(LD_OR, "OR Connection Read (state=%d, obj=%p, %s)!!!!",
  1090. safe_or_conn->state, safe_or_conn,
  1091. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1092. // TODO: remove this ^^
  1093. //if (safe_or_conn->tls_write_waiting_on_socket_readable) {
  1094. // // since the socket is now readable, we can re-enable TLS write again
  1095. // safe_or_conn->tls_write_waiting_on_socket_readable = false;
  1096. // safe_connection_set_write_state(TO_SAFE_CONN(safe_or_conn), true);
  1097. //}
  1098. switch (safe_or_conn->state) {
  1099. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1100. tor_assert_unreached();
  1101. break;
  1102. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1103. // we shouldn't get here, so make sure we're not wanting to read
  1104. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1105. TO_SAFE_CONN(safe_or_conn));
  1106. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1107. TO_SAFE_CONN(safe_or_conn));
  1108. log_warn(LD_OR, "Connecting OR conection wants to read");
  1109. break;
  1110. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1111. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1112. "been implemented");
  1113. tor_assert(0);
  1114. // we are performing the proxy handshake
  1115. //tor_error_t rv = safe_or_connection_plaintext(safe_or_conn);
  1116. //if (rv != E_SUCCESS) {
  1117. // tor_assert(safe_or_connection_update_state(safe_or_conn,
  1118. // SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1119. //}
  1120. break;
  1121. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1122. {
  1123. // we are performing the initial TLS handshake
  1124. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1125. if (rv != E_SUCCESS) {
  1126. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1127. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1128. }
  1129. break;
  1130. }
  1131. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1132. case SAFE_OR_CONN_STATE_OPEN:
  1133. {
  1134. // performing the link handshake, or the handshake has already
  1135. // completed and we're sending/receiving cells
  1136. if (socket_rw_state_get(&safe_or_conn->tls_read_wanted)) {
  1137. // since the socket is now readable, we can re-enable writing again
  1138. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1139. TO_SAFE_CONN(safe_or_conn));
  1140. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1141. TO_SAFE_CONN(safe_or_conn));
  1142. }
  1143. // TODO: we may not actually want to read here now that the states are
  1144. // updated, should we re-check?
  1145. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1146. tor_error_t rv = safe_or_connection_read_encrypted(safe_or_conn,
  1147. use_conn_buckets);
  1148. if (rv != E_SUCCESS) {
  1149. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1150. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1151. }
  1152. if (safe_or_conn->allowed_to_process_cells) {
  1153. process_cells_from_inbuf(safe_or_conn);
  1154. }
  1155. break;
  1156. }
  1157. case SAFE_OR_CONN_STATE_CLOSED:
  1158. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1159. // we shouldn't get here, so make sure we're not wanting to read
  1160. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1161. TO_SAFE_CONN(safe_or_conn));
  1162. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1163. TO_SAFE_CONN(safe_or_conn));
  1164. log_warn(LD_OR, "Closed OR conection wants to read");
  1165. break;
  1166. default:
  1167. log_warn(LD_OR, "Unexpected safe OR connection state");
  1168. tor_assert(0);
  1169. break;
  1170. }
  1171. }
  1172. static void
  1173. safe_or_connection_write_cb(safe_connection_t *safe_conn)
  1174. {
  1175. tor_assert(safe_conn != NULL);
  1176. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1177. log_warn(LD_OR, "OR Connection Write (state=%d, obj=%p, %s)!!!!",
  1178. safe_or_conn->state, safe_or_conn,
  1179. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1180. // TODO: remove this ^^
  1181. switch (safe_or_conn->state) {
  1182. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1183. tor_assert_unreached();
  1184. break;
  1185. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1186. {
  1187. // the socket was connecting and is now ready to write, so we
  1188. // should check for errors before using the socket
  1189. tor_error_t rv = safe_or_connection_check_tcp_connection(safe_or_conn);
  1190. if (rv != E_SUCCESS) {
  1191. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1192. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1193. }
  1194. break;
  1195. }
  1196. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1197. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1198. "been implemented");
  1199. tor_assert(0);
  1200. // we are performing the proxy handshake
  1201. break;
  1202. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1203. {
  1204. // we are performing the initial TLS handshake
  1205. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1206. if (rv != E_SUCCESS) {
  1207. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1208. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1209. }
  1210. break;
  1211. }
  1212. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1213. case SAFE_OR_CONN_STATE_OPEN:
  1214. {
  1215. // performing the link handshake, or the handshake has already
  1216. // completed and we're sending/receiving cells
  1217. if (socket_rw_state_get(&safe_or_conn->tls_write_wanted)) {
  1218. // since the socket is now writable, we can re-enable reading again
  1219. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1220. TO_SAFE_CONN(safe_or_conn));
  1221. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  1222. TO_SAFE_CONN(safe_or_conn));
  1223. }
  1224. // TODO: we may not actually want to write here now that the states are
  1225. // updated, should we re-check?
  1226. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1227. tor_error_t rv = safe_or_connection_write_encrypted(safe_or_conn,
  1228. use_conn_buckets);
  1229. if (rv != E_SUCCESS) {
  1230. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1231. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1232. }
  1233. break;
  1234. }
  1235. case SAFE_OR_CONN_STATE_CLOSED:
  1236. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1237. // we shouldn't get here, so make sure we're not wanting to write
  1238. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1239. TO_SAFE_CONN(safe_or_conn));
  1240. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1241. TO_SAFE_CONN(safe_or_conn));
  1242. log_warn(LD_OR, "Closed OR conection wants to write");
  1243. break;
  1244. default:
  1245. log_warn(LD_OR, "Unexpected safe OR connection state");
  1246. tor_assert(0);
  1247. break;
  1248. }
  1249. }
  1250. /********************************************************/
  1251. static bool
  1252. fetch_cell(safe_or_connection_t *safe_or_conn, char *cell_buf)
  1253. {
  1254. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1255. size_t cell_network_size = \
  1256. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1257. if (buf_datalen(safe_conn->inbuf) < cell_network_size) {
  1258. // don't have a full cell
  1259. return false;
  1260. }
  1261. buf_get_bytes(safe_conn->inbuf, cell_buf, cell_network_size);
  1262. safe_connection_inbuf_modified(safe_conn);
  1263. return true;
  1264. }
  1265. static bool
  1266. fetch_var_cell(safe_or_connection_t *safe_or_conn, var_cell_t **var_cell_ptr)
  1267. {
  1268. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1269. int link_protocol = safe_or_conn->link_protocol;
  1270. *var_cell_ptr = NULL;
  1271. int found_var_cell = fetch_var_cell_from_buf(safe_conn->inbuf, var_cell_ptr,
  1272. link_protocol);
  1273. if (*var_cell_ptr != NULL) {
  1274. // there was not a *full* cell
  1275. safe_connection_inbuf_modified(safe_conn);
  1276. }
  1277. return (found_var_cell != 0);
  1278. }
  1279. static void
  1280. void_var_cell_free(void *void_var_cell)
  1281. {
  1282. var_cell_free_((var_cell_t *)void_var_cell);
  1283. }
  1284. static void
  1285. process_cells_from_inbuf(safe_or_connection_t *safe_or_conn)
  1286. {
  1287. tor_assert(safe_or_conn != NULL);
  1288. while (true) {
  1289. var_cell_t *var_cell = NULL;
  1290. bool found_var_cell = fetch_var_cell(safe_or_conn, &var_cell);
  1291. if (found_var_cell) {
  1292. if (var_cell == NULL) {
  1293. // the next cell is a var cell, but it is not yet complete
  1294. return;
  1295. }
  1296. event_data_t event_data = { .ptr = var_cell };
  1297. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1298. safe_or_conn_var_cell_ev, event_data,
  1299. void_var_cell_free);
  1300. if (safe_or_conn->link_protocol == 0 &&
  1301. var_cell->command == CELL_VERSIONS) {
  1302. // this is the first VERSIONS cell we've received;
  1303. // in order to process future cells, we need to be told our
  1304. // protocol version
  1305. safe_or_conn->allowed_to_process_cells = false;
  1306. return;
  1307. }
  1308. } else {
  1309. char buf[CELL_MAX_NETWORK_SIZE];
  1310. bool found_cell = fetch_cell(safe_or_conn, buf);
  1311. if (found_cell) {
  1312. // retrieve cell info from buf (create the host-order struct from the
  1313. // network-order string)
  1314. cell_t *cell = tor_malloc(sizeof(cell_t));
  1315. cell_unpack(cell, buf, safe_or_conn->wide_circ_ids?1:0);
  1316. event_data_t event_data = { .ptr = cell };
  1317. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1318. safe_or_conn_fixed_cell_ev, event_data,
  1319. tor_free_);
  1320. } else {
  1321. // there is not yet a complete cell
  1322. return;
  1323. }
  1324. }
  1325. }
  1326. }