2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
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>.
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
19 * @defgroup ike_sa_manager ike_sa_manager
23 #ifndef IKE_SA_MANAGER_H_
24 #define IKE_SA_MANAGER_H_
26 typedef struct ike_sa_manager_t ike_sa_manager_t
;
29 #include <sa/ike_sa.h>
30 #include <encoding/message.h>
31 #include <config/peer_cfg.h>
34 * Manages and synchronizes access to all IKE_SAs.
36 * To synchronize access to thread-unsave IKE_SAs, they are checked out for
37 * use and checked in afterwards. A checked out SA is exclusively accessible
38 * by the owning thread.
40 struct ike_sa_manager_t
{
43 * Checkout an existing IKE_SA.
45 * @param ike_sa_id the SA identifier, will be updated
47 * - checked out IKE_SA if found
48 * - NULL, if specified IKE_SA is not found.
50 ike_sa_t
* (*checkout
) (ike_sa_manager_t
* this, ike_sa_id_t
*sa_id
);
53 * Create and check out a new IKE_SA.
55 * @note If initiator equals FALSE, the returned IKE_SA is not registered
58 * @param initiator TRUE for initiator, FALSE otherwise
59 * @returns created and checked out IKE_SA
61 ike_sa_t
* (*checkout_new
) (ike_sa_manager_t
* this, bool initiator
);
64 * Checkout an IKE_SA by a message.
66 * In some situations, it is necessary that the manager knows the
67 * message to use for the checkout. This has the following reasons:
69 * 1. If the targeted IKE_SA is already processing a message, we do not
70 * check it out if the message ID is the same.
71 * 2. If it is an IKE_SA_INIT request, we have to check if it is a
72 * retransmission. If so, we have to drop the message, we would
73 * create another unneeded IKE_SA for each retransmitted packet.
75 * A call to checkout_by_message() returns a (maybe new created) IKE_SA.
76 * If processing the message does not make sense (for the reasons above),
79 * @param ike_sa_id the SA identifier, will be updated
81 * - checked out/created IKE_SA
82 * - NULL to not process message further
84 ike_sa_t
* (*checkout_by_message
) (ike_sa_manager_t
* this, message_t
*message
);
87 * Checkout an IKE_SA for initiation by a peer_config.
89 * To initiate, a CHILD_SA may be established within an existing IKE_SA.
90 * This call checks for an existing IKE_SA by comparing the configuration.
91 * If the CHILD_SA can be created in an existing IKE_SA, the matching SA
93 * If no IKE_SA is found, a new one is created. This is also the case when
94 * the found IKE_SA is in the DELETING state.
96 * @param peer_cfg configuration used to find an existing IKE_SA
97 * @return checked out/created IKE_SA
99 ike_sa_t
* (*checkout_by_config
) (ike_sa_manager_t
* this,
100 peer_cfg_t
*peer_cfg
);
103 * Check for duplicates of the given IKE_SA.
105 * Measures are taken according to the uniqueness policy of the IKE_SA.
106 * The return value indicates whether duplicates have been found and if
107 * further measures should be taken (e.g. cancelling an IKE_AUTH exchange).
108 * check_uniqueness() must be called before the IKE_SA is complete,
109 * deadlocks occur otherwise.
111 * @param ike_sa ike_sa to check
112 * @return TRUE, if the given IKE_SA has duplicates and
115 bool (*check_uniqueness
)(ike_sa_manager_t
*this, ike_sa_t
*ike_sa
);
118 * Check out an IKE_SA a unique ID.
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.
125 * @param id unique ID of the object
126 * @param child TRUE to use CHILD, FALSE to use IKE_SA
128 * - checked out IKE_SA, if found
129 * - NULL, if not found
131 ike_sa_t
* (*checkout_by_id
) (ike_sa_manager_t
* this, u_int32_t id
,
135 * Check out an IKE_SA by the policy/connection name.
137 * Check out the IKE_SA by the configuration name, either from the IKE- or
138 * one of its CHILD_SAs.
140 * @param name name of the connection/policy
141 * @param child TRUE to use policy name, FALSE to use conn name
143 * - checked out IKE_SA, if found
144 * - NULL, if not found
146 ike_sa_t
* (*checkout_by_name
) (ike_sa_manager_t
* this, char *name
,
150 * Create an enumerator over all stored IKE_SAs.
152 * While enumerating an IKE_SA, it is temporarily checked out and
153 * automatically checked in after the current enumeration step.
155 * @return enumerator over all IKE_SAs.
157 enumerator_t
*(*create_enumerator
) (ike_sa_manager_t
* this);
160 * Checkin the SA after usage.
162 * If the IKE_SA is not registered in the manager, a new entry is created.
164 * @param ike_sa_id the SA identifier, will be updated
165 * @param ike_sa checked out SA
167 void (*checkin
) (ike_sa_manager_t
* this, ike_sa_t
*ike_sa
);
170 * Destroy a checked out SA.
172 * The IKE SA is destroyed without notification of the remote peer.
173 * Use this only if the other peer doesn't respond or behaves not
175 * Checking in and destruction is an atomic operation (for the IKE_SA),
176 * so this can be called if the SA is in a "unclean" state, without the
177 * risk that another thread can get the SA.
179 * @param ike_sa SA to delete
181 void (*checkin_and_destroy
) (ike_sa_manager_t
* this, ike_sa_t
*ike_sa
);
184 * Get the number of IKE_SAs which are in the connecting state.
186 * To prevent the server from resource exhaustion, cookies and other
187 * mechanisms are used. The number of half open IKE_SAs is a good
188 * indicator to see if a peer is flooding the server.
189 * If a host is supplied, only the number of half open IKE_SAs initiated
190 * from this IP are counted.
191 * Only SAs for which we are the responder are counted.
193 * @param ip NULL for all, IP for half open IKE_SAs with IP
194 * @return number of half open IKE_SAs
196 int (*get_half_open_count
) (ike_sa_manager_t
*this, host_t
*ip
);
199 * Delete all existing IKE_SAs and destroy them immediately.
201 * Threads will be driven out, so all SAs can be deleted cleanly.
203 void (*flush
)(ike_sa_manager_t
*this);
206 * Destroys the manager with all associated SAs.
208 * A call to flush() is required before calling destroy.
210 void (*destroy
) (ike_sa_manager_t
*this);
214 * Create the IKE_SA manager.
216 * @returns ike_sa_manager_t object, NULL if initialization fails
218 ike_sa_manager_t
*ike_sa_manager_create(void);
220 #endif /** IKE_SA_MANAGER_H_ @}*/