safe_connection.c 52 KB

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