onion.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "or.h"
  2. /********* START VARIABLES **********/
  3. tracked_onion_t *tracked_onions = NULL; /* linked list of tracked onions */
  4. tracked_onion_t *last_tracked_onion = NULL;
  5. /********* END VARIABLES ************/
  6. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  7. uint32_t remote_addr, uint16_t remote_port) {
  8. if(local_addr > remote_addr)
  9. return ACI_TYPE_HIGHER;
  10. if(local_addr < remote_addr)
  11. return ACI_TYPE_LOWER;
  12. if(local_port > remote_port)
  13. return ACI_TYPE_HIGHER;
  14. /* else */
  15. return ACI_TYPE_LOWER;
  16. }
  17. int process_onion(circuit_t *circ, connection_t *conn) {
  18. aci_t aci_type;
  19. if(!decrypt_onion((onion_layer_t *)circ->onion,circ->onionlen,conn->prkey)) {
  20. log(LOG_DEBUG,"command_process_create_cell(): decrypt_onion() failed, closing circuit.");
  21. return -1;
  22. }
  23. log(LOG_DEBUG,"command_process_create_cell(): Onion decrypted.");
  24. /* check freshness */
  25. if (((onion_layer_t *)circ->onion)->expire < time(NULL)) /* expired onion */
  26. {
  27. log(LOG_NOTICE,"I have just received an expired onion. This could be a replay attack.");
  28. return -1;
  29. }
  30. aci_type = decide_aci_type(conn->local.sin_addr.s_addr, conn->local.sin_port,
  31. ((onion_layer_t *)circ->onion)->addr,((onion_layer_t *)circ->onion)->port);
  32. if(circuit_init(circ, aci_type) < 0) {
  33. log(LOG_ERR,"process_onion(): init_circuit() failed.");
  34. return -1;
  35. }
  36. /* check for replay */
  37. if(id_tracked_onion(circ->onion, circ->onionlen, tracked_onions)) {
  38. log(LOG_NOTICE,"process_onion(): I have just received a replayed onion. This could be a replay attack.");
  39. return -1;
  40. }
  41. /* track the new onion */
  42. if(!new_tracked_onion(circ->onion,circ->onionlen, &tracked_onions, &last_tracked_onion)) {
  43. log(LOG_DEBUG,"process_onion(): Onion tracking failed. Will ignore.");
  44. }
  45. return 0;
  46. }