added IKE_SA_INIT retransmission detection
[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 #include <encoding/message.h>
32
33 /**
34 * @brief The IKE_SA-Manager is responsible for managing all initiated and responded IKE_SA's.
35 *
36 * To avoid access from multiple threads, IKE_SAs must be checked out from
37 * the manager, and checked in after usage.
38 * The manager also handles deletion of SAs.
39 *
40 * @todo checking of double-checkouts from the same threads would be nice.
41 * This could be done by comparing thread-ids via pthread_self()...
42 *
43 * @todo Managing of ike_sa_t objects in a hash table instead of linked list.
44 *
45 * @b Constructors:
46 * - ike_sa_manager_create()
47 *
48 * @ingroup sa
49 */
50 struct ike_sa_manager_t {
51
52 /**
53 * @brief Checkout an existing IKE_SA.
54 *
55 * @param this the manager object
56 * @param ike_sa_id the SA identifier, will be updated
57 * @returns
58 * - checked out IKE_SA if found
59 * - NULL, if specified IKE_SA is not found.
60 */
61 ike_sa_t* (*checkout) (ike_sa_manager_t* this, ike_sa_id_t *sa_id);
62
63 /**
64 * @brief Create and check out a new IKE_SA.
65 *
66 * @param this the manager object
67 * @param initiator TRUE for initiator, FALSE otherwise
68 * @returns created andchecked out IKE_SA
69 */
70 ike_sa_t* (*checkout_new) (ike_sa_manager_t* this, bool initiator);
71
72 /**
73 * @brief Checkout an IKE_SA by a message.
74 *
75 * In some situations, it is necessary that the manager knows the
76 * message to use for the checkout. This has the folloing reasons:
77 *
78 * 1. If the targeted IKE_SA is already processing a message, we do not
79 * check it out if the message ID is the same.
80 * 2. If it is an IKE_SA_INIT request, we have to check if it is a
81 * retransmission. If so, we have to drop the message, we would
82 * create another unneded IKE_SA for each retransmitted packet.
83 *
84 * A call to checkout_by_message() returns a (maybe new created) IKE_SA.
85 * If processing the message does not make sense (for the reasons above),
86 * NULL is returned.
87 *
88 * @param this the manager object
89 * @param ike_sa_id the SA identifier, will be updated
90 * @returns
91 * - checked out/created IKE_SA
92 * - NULL to not process message further
93 */
94 ike_sa_t* (*checkout_by_message) (ike_sa_manager_t* this, message_t *message);
95
96 /**
97 * @brief Checkout an existing IKE_SA by hosts and identifications.
98 *
99 * Allows the lookup of an IKE_SA by user IDs and hosts. It returns the
100 * first found occurence, if there are multiple candidates. Supplied IDs
101 * may contain wildcards, hosts may be %any.
102 * If no IKE_SA is found, a new one is created. This is also the case when
103 * the found IKE_SA is in the DELETING state.
104 *
105 * @param this the manager object
106 * @param my_host address of our host
107 * @param other_id address of remote host
108 * @param my_id ID used by us
109 * @param other_id ID used by remote
110 * @return checked out/created IKE_SA
111 */
112 ike_sa_t* (*checkout_by_peer) (ike_sa_manager_t* this,
113 host_t *my_host, host_t* other_host,
114 identification_t *my_id,
115 identification_t *other_id);
116
117 /**
118 * @brief Check out an IKE_SA a unique ID.
119 *
120 * Every IKE_SA and every CHILD_SA is uniquely identified by an ID.
121 * These checkout function uses, depending
122 * on the child parameter, the unique ID of the IKE_SA or the reqid
123 * of one of a IKE_SAs CHILD_SA.
124 *
125 * @param this the manager object
126 * @param id unique ID of the object
127 * @param child TRUE to use CHILD, FALSE to use IKE_SA
128 * @return
129 * - checked out IKE_SA, if found
130 * - NULL, if not found
131 */
132 ike_sa_t* (*checkout_by_id) (ike_sa_manager_t* this, u_int32_t id,
133 bool child);
134
135 /**
136 * @brief Check out an IKE_SA by the policy/connection name.
137 *
138 * Check out the IKE_SA by the connections name or by a CHILD_SAs policy
139 * name.
140 *
141 * @param this the manager object
142 * @param name name of the connection/policy
143 * @param child TRUE to use policy name, FALSE to use conn name
144 * @return
145 * - checked out IKE_SA, if found
146 * - NULL, if not found
147 */
148 ike_sa_t* (*checkout_by_name) (ike_sa_manager_t* this, char *name,
149 bool child);
150
151 /**
152 * @brief Create an iterator over all stored IKE_SAs.
153 *
154 * The avoid synchronization issues, the iterator locks access
155 * to the manager exclusively, until it gets destroyed.
156 * This iterator is for reading only! Writing will corrupt the manager.
157 *
158 * @param this the manager object
159 * @return iterator over all IKE_SAs.
160 */
161 iterator_t *(*create_iterator) (ike_sa_manager_t* this);
162
163 /**
164 * @brief Checkin the SA after usage.
165 *
166 * @warning the SA pointer MUST NOT be used after checkin!
167 * The SA must be checked out again!
168 *
169 * @param this the manager object
170 * @param ike_sa_id the SA identifier, will be updated
171 * @param ike_sa checked out SA
172 * @returns
173 * - SUCCESS if checked in
174 * - NOT_FOUND when not found (shouldn't happen!)
175 */
176 status_t (*checkin) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
177
178 /**
179 * @brief Destroy a checked out SA.
180 *
181 * The IKE SA is destroyed without notification of the remote peer.
182 * Use this only if the other peer doesn't respond or behaves not
183 * as predicted.
184 * Checking in and destruction is an atomic operation (for the IKE_SA),
185 * so this can be called if the SA is in a "unclean" state, without the
186 * risk that another thread can get the SA.
187 *
188 * @param this the manager object
189 * @param ike_sa SA to delete
190 * @returns
191 * - SUCCESS if found
192 * - NOT_FOUND when no such SA is available
193 */
194 status_t (*checkin_and_destroy) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
195
196 /**
197 * @brief Get the number of IKE_SAs which are in the connecting state.
198 *
199 * To prevent the server from resource exhaustion, cookies and other
200 * mechanisms are used. The number of half open IKE_SAs is a good
201 * indicator to see if a peer is flooding the server.
202 * If a host is supplied, only the number of half open IKE_SAs initiated
203 * from this IP are counted.
204 * Only SAs for which we are the responder are counted.
205 *
206 * @param this the manager object
207 * @param ip NULL for all, IP for half open IKE_SAs with IP
208 * @return number of half open IKE_SAs
209 */
210 int (*get_half_open_count) (ike_sa_manager_t *this, host_t *ip);
211
212 /**
213 * @brief Destroys the manager with all associated SAs.
214 *
215 * Threads will be driven out, so all SAs can be deleted cleanly.
216 *
217 * @param this the manager object
218 */
219 void (*destroy) (ike_sa_manager_t *this);
220 };
221
222 /**
223 * @brief Create a manager.
224 *
225 * @returns ike_sa_manager_t object
226 *
227 * @ingroup sa
228 */
229 ike_sa_manager_t *ike_sa_manager_create(void);
230
231 #endif /*IKE_SA_MANAGER_H_*/