ipsec_sa_mgr.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2012 Tobias Brunner
  3. * Copyright (C) 2012 Giuliano Grassi
  4. * Copyright (C) 2012 Ralf Sager
  5. * HSR Hochschule fuer Technik Rapperswil
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. */
  17. /**
  18. * @defgroup ipsec_sa_mgr ipsec_sa_mgr
  19. * @{ @ingroup libipsec
  20. */
  21. #ifndef IPSEC_SA_MGR_H_
  22. #define IPSEC_SA_MGR_H_
  23. #include "ipsec_sa.h"
  24. #include <library.h>
  25. #include <ipsec/ipsec_types.h>
  26. #include <selectors/traffic_selector.h>
  27. #include <networking/host.h>
  28. typedef struct ipsec_sa_mgr_t ipsec_sa_mgr_t;
  29. /**
  30. * IPsec SA manager
  31. *
  32. * The first methods are modeled after those in kernel_ipsec_t.
  33. */
  34. struct ipsec_sa_mgr_t {
  35. /**
  36. * Allocate an SPI for an inbound IPsec SA
  37. *
  38. * @param src source address of the SA
  39. * @param dst destination address of the SA
  40. * @param protocol protocol of the SA (only ESP supported)
  41. * @param spi the allocated SPI
  42. * @return SUCCESS of operation successful
  43. */
  44. status_t (*get_spi)(ipsec_sa_mgr_t *this, host_t *src, host_t *dst,
  45. uint8_t protocol, uint32_t *spi);
  46. /**
  47. * Add a new SA
  48. *
  49. * @param src source address for this SA (gets cloned)
  50. * @param dst destination address for this SA (gets cloned)
  51. * @param spi SPI for this SA
  52. * @param protocol protocol for this SA (only ESP is supported)
  53. * @param reqid reqid for this SA
  54. * @param mark mark for this SA (ignored)
  55. * @param tfc Traffic Flow Confidentiality (not yet supported)
  56. * @param lifetime lifetime for this SA
  57. * @param enc_alg encryption algorithm for this SA
  58. * @param enc_key encryption key for this SA
  59. * @param int_alg integrity protection algorithm
  60. * @param int_key integrity protection key
  61. * @param mode mode for this SA (only tunnel mode is supported)
  62. * @param ipcomp IPcomp transform (not supported, use IPCOMP_NONE)
  63. * @param cpi CPI for IPcomp (ignored)
  64. * @param initiator TRUE if initiator of the exchange creating this SA
  65. * @param encap enable UDP encapsulation (must be TRUE)
  66. * @param esn Extended Sequence Numbers (currently not supported)
  67. * @param inbound TRUE if this is an inbound SA, FALSE otherwise
  68. * @param update TRUE if an SPI has already been allocated for SA
  69. * @return SUCCESS if operation completed
  70. */
  71. status_t (*add_sa)(ipsec_sa_mgr_t *this, host_t *src, host_t *dst,
  72. uint32_t spi, uint8_t protocol, uint32_t reqid,
  73. mark_t mark, uint32_t tfc, lifetime_cfg_t *lifetime,
  74. uint16_t enc_alg, chunk_t enc_key, uint16_t int_alg,
  75. chunk_t int_key, ipsec_mode_t mode, uint16_t ipcomp,
  76. uint16_t cpi, bool initiator, bool encap, bool esn,
  77. bool inbound, bool update);
  78. /**
  79. * Update the hosts on an installed SA.
  80. *
  81. * @param spi SPI of the SA
  82. * @param protocol protocol for this SA (ESP/AH)
  83. * @param cpi CPI for IPComp, 0 if no IPComp is used
  84. * @param src current source address
  85. * @param dst current destination address
  86. * @param new_src new source address
  87. * @param new_dst new destination address
  88. * @param encap current use of UDP encapsulation
  89. * @param new_encap new use of UDP encapsulation
  90. * @param mark optional mark for this SA
  91. * @return SUCCESS if operation completed
  92. */
  93. status_t (*update_sa)(ipsec_sa_mgr_t *this,
  94. uint32_t spi, uint8_t protocol, uint16_t cpi,
  95. host_t *src, host_t *dst,
  96. host_t *new_src, host_t *new_dst,
  97. bool encap, bool new_encap, mark_t mark);
  98. /**
  99. * Query the number of bytes processed by an SA from the SAD.
  100. *
  101. * @param src source address for this SA
  102. * @param dst destination address for this SA
  103. * @param spi SPI allocated by us or remote peer
  104. * @param protocol protocol for this SA (ESP/AH)
  105. * @param mark optional mark for this SA
  106. * @param[out] bytes the number of bytes processed by SA
  107. * @param[out] packets number of packets processed by SA
  108. * @param[out] time last (monotonic) time of SA use
  109. * @return SUCCESS if operation completed
  110. */
  111. status_t (*query_sa)(ipsec_sa_mgr_t *this, host_t *src, host_t *dst,
  112. uint32_t spi, uint8_t protocol, mark_t mark,
  113. uint64_t *bytes, uint64_t *packets, time_t *time);
  114. /**
  115. * Delete a previously added SA
  116. *
  117. * @param spi SPI of the SA
  118. * @param src source address of the SA
  119. * @param dst destination address of the SA
  120. * @param protocol protocol of the SA
  121. * @param cpi CPI for IPcomp
  122. * @param mark optional mark
  123. * @return SUCCESS if operation completed
  124. */
  125. status_t (*del_sa)(ipsec_sa_mgr_t *this, host_t *src, host_t *dst,
  126. uint32_t spi, uint8_t protocol, uint16_t cpi,
  127. mark_t mark);
  128. /**
  129. * Flush all SAs
  130. *
  131. * @return SUCCESS if operation completed
  132. */
  133. status_t (*flush_sas)(ipsec_sa_mgr_t *this);
  134. /**
  135. * Checkout an installed IPsec SA by SPI and destination address
  136. * Can be used to find the correct SA for an inbound packet.
  137. *
  138. * The matching SA is locked until it is checked in using checkin().
  139. * If the matching SA is already checked out, this call blocks until the
  140. * SA is checked in.
  141. *
  142. * Since other threads may be waiting for the checked out SA, it should be
  143. * checked in as soon as possible after use.
  144. *
  145. * @param spi SPI (e.g. of an inbound packet)
  146. * @param dst destination address (e.g. of an inbound packet)
  147. * @return the matching IPsec SA, or NULL if none is found
  148. */
  149. ipsec_sa_t *(*checkout_by_spi)(ipsec_sa_mgr_t *this, uint32_t spi,
  150. host_t *dst);
  151. /**
  152. * Checkout an installed IPsec SA by its reqid and inbound/outbound flag.
  153. * Can be used to find the correct SA for an outbound packet.
  154. *
  155. * The matching SA is locked until it is checked in using checkin().
  156. * If the matching SA is already checked out, this call blocks until the
  157. * SA is checked in.
  158. *
  159. * Since other threads may be waiting for a checked out SA, it should be
  160. * checked in as soon as possible after use.
  161. *
  162. * @param reqid reqid of the SA
  163. * @param inbound TRUE for an inbound SA, FALSE for an outbound SA
  164. * @return the matching IPsec SA, or NULL if none is found
  165. */
  166. ipsec_sa_t *(*checkout_by_reqid)(ipsec_sa_mgr_t *this, uint32_t reqid,
  167. bool inbound);
  168. /**
  169. * Checkin an SA after use.
  170. *
  171. * @param sa checked out SA
  172. */
  173. void (*checkin)(ipsec_sa_mgr_t *this, ipsec_sa_t *sa);
  174. /**
  175. * Destroy an ipsec_sa_mgr_t
  176. */
  177. void (*destroy)(ipsec_sa_mgr_t *this);
  178. };
  179. /**
  180. * Create an ipsec_sa_mgr instance
  181. *
  182. * @return IPsec SA manager instance
  183. */
  184. ipsec_sa_mgr_t *ipsec_sa_mgr_create();
  185. #endif /** IPSEC_SA_MGR_H_ @}*/