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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2001,2006 The FreeRADIUS server project
21 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
24 #include <freeradius-devel/ident.h>
31 #define PTHREAD_MUTEX_LOCK pthread_mutex_lock
32 #define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
34 #define PTHREAD_MUTEX_LOCK(_x)
35 #define PTHREAD_MUTEX_UNLOCK(_x)
39 * Allocate a new EAP_PACKET
41 EAP_PACKET *eap_packet_alloc(void)
45 rp = rad_malloc(sizeof(EAP_PACKET));
46 memset(rp, 0, sizeof(EAP_PACKET));
53 void eap_packet_free(EAP_PACKET **eap_packet_ptr)
55 EAP_PACKET *eap_packet;
57 if (!eap_packet_ptr) return;
58 eap_packet = *eap_packet_ptr;
59 if (!eap_packet) return;
61 if (eap_packet->type.data) {
63 * There's no packet, OR the type data isn't
64 * pointing inside of the packet: free it.
66 if ((eap_packet->packet == NULL) ||
67 (eap_packet->type.data != (eap_packet->packet + 5))) {
68 free(eap_packet->type.data);
70 eap_packet->type.data = NULL;
73 if (eap_packet->packet) {
74 free(eap_packet->packet);
75 eap_packet->packet = NULL;
80 *eap_packet_ptr = NULL;
84 * Allocate a new EAP_PACKET
86 EAP_DS *eap_ds_alloc(void)
90 eap_ds = rad_malloc(sizeof(EAP_DS));
91 memset(eap_ds, 0, sizeof(EAP_DS));
92 if ((eap_ds->response = eap_packet_alloc()) == NULL) {
96 if ((eap_ds->request = eap_packet_alloc()) == NULL) {
104 void eap_ds_free(EAP_DS **eap_ds_p)
108 if (!eap_ds_p) return;
113 if (eap_ds->response) eap_packet_free(&(eap_ds->response));
114 if (eap_ds->request) eap_packet_free(&(eap_ds->request));
121 * Allocate a new EAP_HANDLER
123 EAP_HANDLER *eap_handler_alloc(rlm_eap_t *inst)
125 EAP_HANDLER *handler;
127 handler = rad_malloc(sizeof(EAP_HANDLER));
128 memset(handler, 0, sizeof(EAP_HANDLER));
130 if (fr_debug_flag && inst->handler_tree) {
131 PTHREAD_MUTEX_LOCK(&(inst->handler_mutex));
132 rbtree_insert(inst->handler_tree, handler);
133 PTHREAD_MUTEX_UNLOCK(&(inst->handler_mutex));
139 void eap_handler_free(rlm_eap_t *inst, EAP_HANDLER *handler)
144 if (inst->handler_tree) {
145 PTHREAD_MUTEX_LOCK(&(inst->handler_mutex));
146 rbtree_deletebydata(inst->handler_tree, handler);
147 PTHREAD_MUTEX_UNLOCK(&(inst->handler_mutex));
150 if (handler->identity) {
151 free(handler->identity);
152 handler->identity = NULL;
155 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
156 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
158 if ((handler->opaque) && (handler->free_opaque)) {
159 handler->free_opaque(handler->opaque);
160 handler->opaque = NULL;
162 else if ((handler->opaque) && (handler->free_opaque == NULL))
163 radlog(L_ERR, "Possible memory leak ...");
165 handler->opaque = NULL;
166 handler->free_opaque = NULL;
168 if (handler->certs) pairfree(&handler->certs);
174 typedef struct check_handler_t {
176 EAP_HANDLER *handler;
180 static void check_handler(void *data)
182 int do_warning = FALSE;
184 check_handler_t *check = data;
188 if (!check->inst || !check->handler) {
193 PTHREAD_MUTEX_LOCK(&(check->inst->handler_mutex));
194 if (!rbtree_finddata(check->inst->handler_tree, check->handler)) {
199 * The session has continued *after* this packet.
200 * Don't do a warning.
202 if (check->handler->trips > check->trips) {
206 if (check->handler->tls && !check->handler->finished) {
208 memcpy(state, check->handler->state, sizeof(state));
212 PTHREAD_MUTEX_UNLOCK(&(check->inst->handler_mutex));
216 DEBUG("WARNING: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
217 DEBUG("WARNING: !! EAP session for state 0x%02x%02x%02x%02x%02x%02x%02x%02x did not finish!",
223 DEBUG("WARNING: !! Please read http://wiki.freeradius.org/Certificate_Compatibility");
224 DEBUG("WARNING: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
228 void eaptype_free(EAP_TYPES *i)
230 if (i->type->detach) (i->type->detach)(i->type_data);
232 if (i->handle) lt_dlclose(i->handle);
237 void eaplist_free(rlm_eap_t *inst)
239 EAP_HANDLER *node, *next;
241 for (node = inst->session_head; node != NULL; node = next) {
243 eap_handler_free(inst, node);
246 inst->session_head = inst->session_tail = NULL;
250 * Return a 32-bit random number.
252 static uint32_t eap_rand(fr_randctx *ctx)
256 num = ctx->randrsl[ctx->randcnt++];
257 if (ctx->randcnt >= 256) {
266 static EAP_HANDLER *eaplist_delete(rlm_eap_t *inst, EAP_HANDLER *handler)
270 node = rbtree_find(inst->session_tree, handler);
271 if (!node) return NULL;
273 handler = rbtree_node2data(inst->session_tree, node);
276 * Delete old handler from the tree.
278 rbtree_delete(inst->session_tree, node);
281 * And unsplice it from the linked list.
284 handler->prev->next = handler->next;
286 inst->session_head = handler->next;
289 handler->next->prev = handler->prev;
291 inst->session_tail = handler->prev;
293 handler->prev = handler->next = NULL;
299 static void eaplist_expire(rlm_eap_t *inst, time_t timestamp)
302 EAP_HANDLER *handler;
305 * Check the first few handlers in the list, and delete
306 * them if they're too old. We don't need to check them
307 * all, as incoming requests will quickly cause older
308 * handlers to be deleted.
311 for (i = 0; i < 3; i++) {
312 handler = inst->session_head;
316 * Expire entries from the start of the list.
317 * They should be the oldest ones.
319 if ((timestamp - handler->timestamp) > inst->timer_limit) {
321 node = rbtree_find(inst->session_tree, handler);
322 rad_assert(node != NULL);
323 rbtree_delete(inst->session_tree, node);
326 * handler == inst->session_head
328 inst->session_head = handler->next;
330 handler->next->prev = NULL;
332 inst->session_head = NULL;
333 inst->session_tail = NULL;
335 eap_handler_free(inst, handler);
341 * Add a handler to the set of active sessions.
343 * Since we're adding it to the list, we guess that this means
344 * the packet needs a State attribute. So add one.
346 int eaplist_add(rlm_eap_t *inst, EAP_HANDLER *handler)
350 REQUEST *request = handler->request;
352 rad_assert(handler != NULL);
353 rad_assert(request != NULL);
356 * Generate State, since we've been asked to add it to
359 state = pairmake("State", "0x00", T_OP_EQ);
360 if (!state) return 0;
363 * The time at which this request was made was the time
364 * at which it was received by the RADIUS server.
366 handler->timestamp = request->timestamp;
369 handler->src_ipaddr = request->packet->src_ipaddr;
370 handler->eap_id = handler->eap_ds->request->id;
373 * Playing with a data structure shared among threads
374 * means that we need a lock, to avoid conflict.
376 PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
379 * If we have a DoS attack, discard new sessions.
381 if (rbtree_num_elements(inst->session_tree) >= inst->max_sessions) {
383 eaplist_expire(inst, handler->timestamp);
388 * Create a unique content for the State variable.
389 * It will be modified slightly per round trip, but less so
392 if (handler->trips == 0) {
395 for (i = 0; i < 4; i++) {
398 lvalue = eap_rand(&inst->rand_pool);
400 memcpy(handler->state + i * 4, &lvalue,
405 memcpy(state->vp_octets, handler->state, sizeof(handler->state));
406 state->length = EAP_STATE_LEN;
409 * Add some more data to distinguish the sessions.
411 state->vp_octets[4] = handler->trips ^ handler->state[0];
412 state->vp_octets[5] = handler->eap_id ^ handler->state[1];
413 state->vp_octets[6] = handler->eap_type ^ handler->state[2];
416 * and copy the state back again.
418 memcpy(handler->state, state->vp_octets, sizeof(handler->state));
423 status = rbtree_insert(inst->session_tree, handler);
426 * Catch Access-Challenge without response.
429 check_handler_t *check = rad_malloc(sizeof(*check));
432 check->handler = handler;
433 check->trips = handler->trips;
434 request_data_add(request, inst, 0, check, check_handler);
440 prev = inst->session_tail;
442 prev->next = handler;
443 handler->prev = prev;
444 handler->next = NULL;
445 inst->session_tail = handler;
447 inst->session_head = inst->session_tail = handler;
448 handler->next = handler->prev = NULL;
453 * Now that we've finished mucking with the list,
459 * We don't need this any more.
461 if (status > 0) handler->request = NULL;
463 PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
469 static time_t last_logged = 0;
471 if (last_logged < handler->timestamp) {
472 last_logged = handler->timestamp;
473 radlog(L_ERR, "rlm_eap: Too many open sessions. Try increasing \"max_sessions\" in the EAP module configuration");
476 radlog(L_ERR, "rlm_eap: Internal error: failed to store handler");
481 pairadd(&(request->reply->vps), state);
487 * Find a a previous EAP-Request sent by us, which matches
488 * the current EAP-Response.
490 * Then, release the handle from the list, and return it to
493 * Also since we fill the eap_ds with the present EAP-Response we
494 * got to free the prev_eapds & move the eap_ds to prev_eapds
496 EAP_HANDLER *eaplist_find(rlm_eap_t *inst, REQUEST *request,
497 eap_packet_t *eap_packet)
500 EAP_HANDLER *handler, myHandler;
503 * We key the sessions off of the 'state' attribute, so it
506 state = pairfind(request->packet->vps, PW_STATE, 0);
508 (state->length != EAP_STATE_LEN)) {
512 myHandler.src_ipaddr = request->packet->src_ipaddr;
513 myHandler.eap_id = eap_packet->id;
514 memcpy(myHandler.state, state->vp_strvalue, sizeof(myHandler.state));
517 * Playing with a data structure shared among threads
518 * means that we need a lock, to avoid conflict.
520 PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
522 eaplist_expire(inst, request->timestamp);
524 handler = eaplist_delete(inst, &myHandler);
525 PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
528 * Might not have been there.
531 radlog(L_ERR, "rlm_eap: No EAP session matching the State variable.");
535 if (handler->trips >= 50) {
536 RDEBUG2("More than 50 authentication packets for this EAP session. Aborted.");
537 eap_handler_free(inst, handler);
542 RDEBUG2("Request found, released from the list");
545 * Remember what the previous request was.
547 eap_ds_free(&(handler->prev_eapds));
548 handler->prev_eapds = handler->eap_ds;
549 handler->eap_ds = NULL;