2 * mem.c Memory allocation, deallocation stuff.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000,2001 The FreeRADIUS server project
21 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
26 static const char rcsid[] = "$Id$";
29 * Allocate a new EAP_PACKET
31 EAP_PACKET *eap_packet_alloc(void)
35 rp = rad_malloc(sizeof(EAP_PACKET));
36 memset(rp, 0, sizeof(EAP_PACKET));
43 void eap_packet_free(EAP_PACKET **eap_packet_ptr)
45 EAP_PACKET *eap_packet;
47 if (!eap_packet_ptr) return;
48 eap_packet = *eap_packet_ptr;
49 if (!eap_packet) return;
51 if (eap_packet->type.data) {
53 * This is just a pointer in the packet
54 * so we do not free it but we NULL it
55 free(eap_packet->type.data);
57 eap_packet->type.data = NULL;
60 if (eap_packet->packet) {
61 free(eap_packet->packet);
62 eap_packet->packet = NULL;
67 *eap_packet_ptr = NULL;
71 * Allocate a new EAP_PACKET
73 EAP_DS *eap_ds_alloc(void)
77 eap_ds = rad_malloc(sizeof(EAP_DS));
78 memset(eap_ds, 0, sizeof(EAP_DS));
79 if ((eap_ds->response = eap_packet_alloc()) == NULL) {
83 if ((eap_ds->request = eap_packet_alloc()) == NULL) {
91 void eap_ds_free(EAP_DS **eap_ds_p)
95 if (!eap_ds_p) return;
100 if (eap_ds->response) eap_packet_free(&(eap_ds->response));
101 if (eap_ds->request) eap_packet_free(&(eap_ds->request));
108 * Allocate a new EAP_HANDLER
110 EAP_HANDLER *eap_handler_alloc(void)
112 EAP_HANDLER *handler;
114 handler = rad_malloc(sizeof(EAP_HANDLER));
115 memset(handler, 0, sizeof(EAP_HANDLER));
119 void eap_handler_free(EAP_HANDLER *handler)
124 if (handler->identity) {
125 free(handler->identity);
126 handler->identity = NULL;
129 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
130 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
132 if ((handler->opaque) && (handler->free_opaque)) {
133 handler->free_opaque(handler->opaque);
134 handler->opaque = NULL;
136 else if ((handler->opaque) && (handler->free_opaque == NULL))
137 radlog(L_ERR, "Possible memory leak ...");
139 handler->opaque = NULL;
140 handler->free_opaque = NULL;
145 void eaptype_free(EAP_TYPES *i)
147 if (i->type->detach) (i->type->detach)(i->type_data);
149 if (i->handle) lt_dlclose(i->handle);
154 void eaplist_free(rlm_eap_t *inst)
156 EAP_HANDLER *node, *next;
158 for (node = inst->session_head; node != NULL; node = next) {
160 eap_handler_free(node);
163 inst->session_head = inst->session_tail = NULL;
167 * Add a handler to the set of active sessions.
169 * Since we're adding it to the list, we guess that this means
170 * the packet needs a State attribute. So add one.
172 int eaplist_add(rlm_eap_t *inst, EAP_HANDLER *handler)
177 rad_assert(handler != NULL);
178 rad_assert(handler->request != NULL);
181 * Generate State, since we've been asked to add it to
184 state = generate_state(handler->request->timestamp);
185 pairadd(&(handler->request->reply->vps), state);
188 * Create a unique 'key' for the handler, based
189 * on State, Client-IP-Address, and EAP ID.
191 rad_assert(state->length == EAP_STATE_LEN);
194 * The time at which this request was made was the time
195 * at which it was received by the RADIUS server.
197 handler->timestamp = handler->request->timestamp;
200 memcpy(handler->state, state->strvalue, sizeof(handler->state));
201 handler->src_ipaddr = handler->request->packet->src_ipaddr;
202 handler->eap_id = handler->eap_ds->request->id;
205 * We don't need this any more.
207 handler->request = NULL;
210 * Playing with a data structure shared among threads
211 * means that we need a lock, to avoid conflict.
213 pthread_mutex_lock(&(inst->session_mutex));
218 status = rbtree_insert(inst->session_tree, handler);
223 prev = inst->session_tail;
225 prev->next = handler;
226 handler->prev = prev;
227 handler->next = NULL;
228 inst->session_tail = handler;
230 inst->session_head = inst->session_tail = handler;
231 handler->next = handler->prev = NULL;
236 * Now that we've finished mucking with the list,
239 pthread_mutex_unlock(&(inst->session_mutex));
242 radlog(L_ERR, "rlm_eap: Failed to remember handler!");
243 eap_handler_free(handler);
251 * Find a a previous EAP-Request sent by us, which matches
252 * the current EAP-Response.
254 * Then, release the handle from the list, and return it to
257 * Also since we fill the eap_ds with the present EAP-Response we
258 * got to free the prev_eapds & move the eap_ds to prev_eapds
260 EAP_HANDLER *eaplist_find(rlm_eap_t *inst, REQUEST *request,
261 eap_packet_t *eap_packet)
266 EAP_HANDLER *handler, myHandler;
269 * We key the sessions off of the 'state' attribute, so it
272 state = pairfind(request->packet->vps, PW_STATE);
274 (state->length != EAP_STATE_LEN)) {
278 myHandler.src_ipaddr = request->packet->src_ipaddr;
279 myHandler.eap_id = eap_packet->id;
280 memcpy(myHandler.state, state->strvalue, sizeof(myHandler.state));
283 * Playing with a data structure shared among threads
284 * means that we need a lock, to avoid conflict.
286 pthread_mutex_lock(&(inst->session_mutex));
289 * Check the first few handlers in the list, and delete
290 * them if they're too old. We don't need to check them
291 * all, as incoming requests will quickly cause older
292 * handlers to be deleted.
295 for (i = 0; i < 2; i++) {
296 handler = inst->session_head;
298 ((request->timestamp - handler->timestamp) > inst->timer_limit)) {
299 node = rbtree_find(inst->session_tree, handler);
300 rad_assert(node != NULL);
301 rbtree_delete(inst->session_tree, node);
303 inst->session_head = handler->next;
304 if (handler->next) handler->next->prev = NULL;
305 eap_handler_free(handler);
310 node = rbtree_find(inst->session_tree, &myHandler);
312 handler = rbtree_node2data(inst->session_tree, node);
315 * Check against replays. The client can re-play
316 * a State attribute verbatim, so we wish to
317 * ensure that the attribute falls within the
318 * valid time window, which is the second at
319 * which it was sent out.
321 * Hmm... I'm not sure that this step is
322 * necessary, or even that it does anything.
324 if (verify_state(state, handler->timestamp) != 0) {
328 * It's OK, delete it from the tree.
330 rbtree_delete(inst->session_tree, node);
333 * And unsplice it from the linked list.
336 handler->prev->next = handler->next;
338 inst->session_head = handler->next;
341 handler->next->prev = handler->prev;
343 inst->session_tail = handler->prev;
345 handler->prev = handler->next = NULL;
349 pthread_mutex_unlock(&(inst->session_mutex));
355 DEBUG2(" rlm_eap: Request not found in the list");
360 * Found, but state verification failed.
363 radlog(L_ERR, "rlm_eap: State verification failed.");
367 DEBUG2(" rlm_eap: Request found, released from the list");
370 * Remember what the previous request was.
372 eap_ds_free(&(handler->prev_eapds));
373 handler->prev_eapds = handler->eap_ds;
374 handler->eap_ds = NULL;