set_ike_spi.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2010 Martin Willi
  3. * Copyright (C) 2010 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "hook.h"
  16. #include <encoding/payloads/unknown_payload.h>
  17. typedef struct private_set_ike_spi_t private_set_ike_spi_t;
  18. /**
  19. * Private data of an set_ike_spi_t object.
  20. */
  21. struct private_set_ike_spi_t {
  22. /**
  23. * Implements the hook_t interface.
  24. */
  25. hook_t hook;
  26. /**
  27. * Alter requests or responses?
  28. */
  29. bool req;
  30. /**
  31. * ID of message to alter.
  32. */
  33. int id;
  34. /**
  35. * Initiator SPI
  36. */
  37. uint64_t spii;
  38. /**
  39. * Responder SPI
  40. */
  41. uint64_t spir;
  42. };
  43. METHOD(listener_t, message, bool,
  44. private_set_ike_spi_t *this, ike_sa_t *ike_sa, message_t *message,
  45. bool incoming, bool plain)
  46. {
  47. if (!incoming && plain &&
  48. message->get_request(message) == this->req &&
  49. message->get_message_id(message) == this->id)
  50. {
  51. ike_sa_id_t *id;
  52. DBG1(DBG_CFG, "setting IKE SPIs to: 0x%llx/0x%llx",
  53. this->spii, this->spir);
  54. id = message->get_ike_sa_id(message);
  55. id->set_initiator_spi(id, this->spii);
  56. id->set_responder_spi(id, this->spir);
  57. }
  58. return TRUE;
  59. }
  60. METHOD(hook_t, destroy, void,
  61. private_set_ike_spi_t *this)
  62. {
  63. free(this);
  64. }
  65. /**
  66. * Create the IKE_AUTH fill hook
  67. */
  68. hook_t *set_ike_spi_hook_create(char *name)
  69. {
  70. private_set_ike_spi_t *this;
  71. INIT(this,
  72. .hook = {
  73. .listener = {
  74. .message = _message,
  75. },
  76. .destroy = _destroy,
  77. },
  78. .req = conftest->test->get_bool(conftest->test,
  79. "hooks.%s.request", TRUE, name),
  80. .id = conftest->test->get_int(conftest->test,
  81. "hooks.%s.id", 0, name),
  82. .spii = strtoull(conftest->test->get_str(conftest->test,
  83. "hooks.%s.spii", "0", name), NULL, 16),
  84. .spir = strtoull(conftest->test->get_str(conftest->test,
  85. "hooks.%s.spir", "0", name), NULL, 16),
  86. );
  87. return &this->hook;
  88. }