better split up of library files "types.h" & "definitions.h"
[strongswan.git] / src / charon / sa / ike_sa_manager.h
1 /**
2 * @file ike_sa_manager.h
3 *
4 * @brief Interface of ike_sa_manager_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #ifndef IKE_SA_MANAGER_H_
25 #define IKE_SA_MANAGER_H_
26
27 typedef struct ike_sa_manager_t ike_sa_manager_t;
28
29 #include <library.h>
30 #include <sa/ike_sa.h>
31
32 /**
33 * @brief The IKE_SA-Manager is responsible for managing all initiated and responded IKE_SA's.
34 *
35 * To avoid access from multiple threads, IKE_SAs must be checked out from
36 * the manager, and checked in after usage.
37 * The manager also handles deletion of SAs.
38 *
39 * @todo checking of double-checkouts from the same threads would be nice.
40 * This could be done by comparing thread-ids via pthread_self()...
41 *
42 * @todo Managing of ike_sa_t objects in a hash table instead of linked list.
43 *
44 * @b Constructors:
45 * - ike_sa_manager_create()
46 *
47 * @ingroup sa
48 */
49 struct ike_sa_manager_t {
50 /**
51 * @brief Checkout an IKE_SA, create it when necesarry.
52 *
53 * Checks out a SA by its ID. An SA will be created, when the responder
54 * SPI is not set (when received an IKE_SA_INIT from initiator).
55 * Management of SPIs is the managers job, he will set it.
56 * This function blocks until SA is available for checkout.
57 *
58 * @warning checking out two times without checking in will
59 * result in a deadlock!
60 *
61 * @param this the manager object
62 * @param[in/out] ike_sa_id the SA identifier, will be updated
63 * @returns
64 * - checked out IKE_SA if found
65 * - NULL, if no such IKE_SA available
66 */
67 ike_sa_t* (*checkout) (ike_sa_manager_t* this, ike_sa_id_t *sa_id);
68
69 /**
70 * @brief Checkout an existing IKE_SA by hosts and identifications.
71 *
72 * Allows the lookup of an IKE_SA by user IDs and hosts. It returns the
73 * first found occurence, if there are multiple candidates. Supplied IDs
74 * may contain wildcards, hosts may be %any.
75 * If no IKE_SA is found, a new one is created.
76 *
77 * @param this the manager object
78 * @param my_host address of our host
79 * @param other_id address of remote host
80 * @param my_id ID used by us
81 * @param other_id ID used by remote
82 * @return checked out/created IKE_SA
83 */
84 ike_sa_t* (*checkout_by_id) (ike_sa_manager_t* this,
85 host_t *my_host, host_t* other_host,
86 identification_t *my_id,
87 identification_t *other_id);
88
89 /**
90 * @brief Check out an IKE_SA by protocol and SPI of one of its CHILD_SA.
91 *
92 * The kernel sends us expire messages for IPsec SAs. To fullfill
93 * this request, we must check out the IKE SA which contains the
94 * CHILD_SA the kernel wants to modify.
95 *
96 * @param this the manager object
97 * @param reqid reqid of the CHILD_SA
98 * @return
99 * - checked out IKE_SA, if found
100 * - NULL, if not found
101 */
102 ike_sa_t* (*checkout_by_child) (ike_sa_manager_t* this, u_int32_t reqid);
103
104 /**
105 * @brief Create an iterator over all stored IKE_SAs.
106 *
107 * The avoid synchronization issues, the iterator locks access
108 * to the manager exclusively, until it gets destroyed.
109 * This iterator is for reading only! Writing will corrupt the manager.
110 *
111 * @param this the manager object
112 * @return iterator over all IKE_SAs.
113 */
114 iterator_t *(*create_iterator) (ike_sa_manager_t* this);
115
116 /**
117 * @brief Checkin the SA after usage.
118 *
119 * @warning the SA pointer MUST NOT be used after checkin!
120 * The SA must be checked out again!
121 *
122 * @param this the manager object
123 * @param[in/out] ike_sa_id the SA identifier, will be updated
124 * @param[out] ike_sa checked out SA
125 * @returns
126 * - SUCCESS if checked in
127 * - NOT_FOUND when not found (shouldn't happen!)
128 */
129 status_t (*checkin) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
130
131 /**
132 * @brief Delete a SA, which was not checked out.
133 *
134 * If the state allows it, the IKE SA is destroyed immediately. If it is
135 * in the state ESTABLSIHED, a delete message
136 * is sent to the remote peer, which has to be acknowledged.
137 *
138 * @warning do not use this when the SA is already checked out, this will
139 * deadlock!
140 *
141 * @param this the manager object
142 * @param[in/out] ike_sa_id the SA identifier
143 * @returns
144 * - SUCCESS if found
145 * - NOT_FOUND when no such SA is available
146 */
147 status_t (*delete) (ike_sa_manager_t* this, ike_sa_id_t *ike_sa_id);
148
149 /**
150 * @brief Delete a SA identified by its name, which was not checked out.
151 *
152 * Using delete_by_name allows the delete of IKE_SAs and CHILD_SAs.
153 * The supplied name may have one of the following format:
154 *
155 * name{x} => delete IKE_SA with "name" and unique id "x"
156 * name{} => delete all IKE_SAs with "name"
157 * name[x] => delete CHILD_SA with "name" and unique id "x"
158 * name[] => delete all CHILD_SAs with "name"
159 * name => delete all CHILD_SAs or IKE_SAs with "name"
160 *
161 * @warning do not use this when the SA is already checked out, this will
162 * deadlock!
163 *
164 * @param this the manager object
165 * @param name name in one of the format described above
166 * @returns
167 * - SUCCESS if found
168 * - NOT_FOUND when no such SA is available
169 */
170 status_t (*delete_by_name) (ike_sa_manager_t* this, char *name);
171
172 /**
173 * @brief Destroy a checked out SA.
174 *
175 * The IKE SA is destroyed without notification of the remote peer.
176 * Use this only if the other peer doesn't respond or behaves not
177 * as predicted.
178 * Checking in and destruction is an atomic operation (for the IKE_SA),
179 * so this can be called if the SA is in a "unclean" state, without the
180 * risk that another thread can get the SA.
181 *
182 * @param this the manager object
183 * @param ike_sa SA to delete
184 * @returns
185 * - SUCCESS if found
186 * - NOT_FOUND when no such SA is available
187 */
188 status_t (*checkin_and_destroy) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
189
190 /**
191 * @brief Destroys the manager with all associated SAs.
192 *
193 * Threads will be driven out, so all SAs can be deleted cleanly.
194 *
195 * @param this the manager object
196 */
197 void (*destroy) (ike_sa_manager_t *this);
198 };
199
200 /**
201 * @brief Create a manager.
202 *
203 * @returns ike_sa_manager_t object
204 *
205 * @ingroup sa
206 */
207 ike_sa_manager_t *ike_sa_manager_create(void);
208
209 #endif /*IKE_SA_MANAGER_H_*/