merged the modularization branch (credentials) back to trunk
[strongswan.git] / src / charon / sa / ike_sa_manager.h
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 /**
20 * @defgroup ike_sa_manager ike_sa_manager
21 * @{ @ingroup sa
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 #include <config/peer_cfg.h>
33
34 /**
35 * The IKE_SA-Manager is responsible for managing all initiated and responded IKE_SA's.
36 *
37 * To avoid access from multiple threads, IKE_SAs must be checked out from
38 * the manager, and checked in after usage.
39 * The manager also handles deletion of SAs.
40 *
41 * @todo checking of double-checkouts from the same threads would be nice.
42 * This could be done by comparing thread-ids via pthread_self()...
43 *
44 * @todo Managing of ike_sa_t objects in a hash table instead of linked list.
45 */
46 struct ike_sa_manager_t {
47
48 /**
49 * Checkout an existing IKE_SA.
50 *
51 * @param ike_sa_id the SA identifier, will be updated
52 * @returns
53 * - checked out IKE_SA if found
54 * - NULL, if specified IKE_SA is not found.
55 */
56 ike_sa_t* (*checkout) (ike_sa_manager_t* this, ike_sa_id_t *sa_id);
57
58 /**
59 * Create and check out a new IKE_SA.
60 *
61 * @param initiator TRUE for initiator, FALSE otherwise
62 * @returns created andchecked out IKE_SA
63 */
64 ike_sa_t* (*checkout_new) (ike_sa_manager_t* this, bool initiator);
65
66 /**
67 * Checkout an IKE_SA by a message.
68 *
69 * In some situations, it is necessary that the manager knows the
70 * message to use for the checkout. This has the folloing reasons:
71 *
72 * 1. If the targeted IKE_SA is already processing a message, we do not
73 * check it out if the message ID is the same.
74 * 2. If it is an IKE_SA_INIT request, we have to check if it is a
75 * retransmission. If so, we have to drop the message, we would
76 * create another unneded IKE_SA for each retransmitted packet.
77 *
78 * A call to checkout_by_message() returns a (maybe new created) IKE_SA.
79 * If processing the message does not make sense (for the reasons above),
80 * NULL is returned.
81 *
82 * @param ike_sa_id the SA identifier, will be updated
83 * @returns
84 * - checked out/created IKE_SA
85 * - NULL to not process message further
86 */
87 ike_sa_t* (*checkout_by_message) (ike_sa_manager_t* this, message_t *message);
88
89 /**
90 * Checkout an IKE_SA for initiation by a peer_config.
91 *
92 * To initiate, a CHILD_SA may be established within an existing IKE_SA.
93 * This call checks for an existing IKE_SA by comparing the configuration.
94 * If the CHILD_SA can be created in an existing IKE_SA, the matching SA
95 * is returned.
96 * If no IKE_SA is found, a new one is created. This is also the case when
97 * the found IKE_SA is in the DELETING state.
98 *
99 * @param peer_cfg configuration used to find an existing IKE_SA
100 * @return checked out/created IKE_SA
101 */
102 ike_sa_t* (*checkout_by_config) (ike_sa_manager_t* this,
103 peer_cfg_t *peer_cfg);
104
105 /**
106 * Check out an IKE_SA a unique ID.
107 *
108 * Every IKE_SA and every CHILD_SA is uniquely identified by an ID.
109 * These checkout function uses, depending
110 * on the child parameter, the unique ID of the IKE_SA or the reqid
111 * of one of a IKE_SAs CHILD_SA.
112 *
113 * @param id unique ID of the object
114 * @param child TRUE to use CHILD, FALSE to use IKE_SA
115 * @return
116 * - checked out IKE_SA, if found
117 * - NULL, if not found
118 */
119 ike_sa_t* (*checkout_by_id) (ike_sa_manager_t* this, u_int32_t id,
120 bool child);
121
122 /**
123 * Check out an IKE_SA by the policy/connection name.
124 *
125 * Check out the IKE_SA by the connections name or by a CHILD_SAs policy
126 * name.
127 *
128 * @param name name of the connection/policy
129 * @param child TRUE to use policy name, FALSE to use conn name
130 * @return
131 * - checked out IKE_SA, if found
132 * - NULL, if not found
133 */
134 ike_sa_t* (*checkout_by_name) (ike_sa_manager_t* this, char *name,
135 bool child);
136
137 /**
138 * Create an iterator over all stored IKE_SAs.
139 *
140 * The avoid synchronization issues, the iterator locks access
141 * to the manager exclusively, until it gets destroyed.
142 * This iterator is for reading only! Writing will corrupt the manager.
143 *
144 * @return iterator over all IKE_SAs.
145 */
146 iterator_t *(*create_iterator) (ike_sa_manager_t* this);
147
148 /**
149 * Checkin the SA after usage.
150 *
151 * @warning the SA pointer MUST NOT be used after checkin!
152 * The SA must be checked out again!
153 *
154 * @param ike_sa_id the SA identifier, will be updated
155 * @param ike_sa checked out SA
156 * @returns
157 * - SUCCESS if checked in
158 * - NOT_FOUND when not found (shouldn't happen!)
159 */
160 status_t (*checkin) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
161
162 /**
163 * Destroy a checked out SA.
164 *
165 * The IKE SA is destroyed without notification of the remote peer.
166 * Use this only if the other peer doesn't respond or behaves not
167 * as predicted.
168 * Checking in and destruction is an atomic operation (for the IKE_SA),
169 * so this can be called if the SA is in a "unclean" state, without the
170 * risk that another thread can get the SA.
171 *
172 * @param ike_sa SA to delete
173 * @returns
174 * - SUCCESS if found
175 * - NOT_FOUND when no such SA is available
176 */
177 status_t (*checkin_and_destroy) (ike_sa_manager_t* this, ike_sa_t *ike_sa);
178
179 /**
180 * Get the number of IKE_SAs which are in the connecting state.
181 *
182 * To prevent the server from resource exhaustion, cookies and other
183 * mechanisms are used. The number of half open IKE_SAs is a good
184 * indicator to see if a peer is flooding the server.
185 * If a host is supplied, only the number of half open IKE_SAs initiated
186 * from this IP are counted.
187 * Only SAs for which we are the responder are counted.
188 *
189 * @param ip NULL for all, IP for half open IKE_SAs with IP
190 * @return number of half open IKE_SAs
191 */
192 int (*get_half_open_count) (ike_sa_manager_t *this, host_t *ip);
193
194 /**
195 * Destroys the manager with all associated SAs.
196 *
197 * Threads will be driven out, so all SAs can be deleted cleanly.
198 */
199 void (*destroy) (ike_sa_manager_t *this);
200 };
201
202 /**
203 * Create a manager.
204 *
205 * @returns ike_sa_manager_t object, NULL if initialization fails
206 */
207 ike_sa_manager_t *ike_sa_manager_create(void);
208
209 #endif /*IKE_SA_MANAGER_H_ @} */