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 * Allocate a new EAP_PACKET
33 EAP_PACKET *eap_packet_alloc(void)
37 rp = rad_malloc(sizeof(EAP_PACKET));
38 memset(rp, 0, sizeof(EAP_PACKET));
45 void eap_packet_free(EAP_PACKET **eap_packet_ptr)
47 EAP_PACKET *eap_packet;
49 if (!eap_packet_ptr) return;
50 eap_packet = *eap_packet_ptr;
51 if (!eap_packet) return;
53 if (eap_packet->type.data) {
55 * There's no packet, OR the type data isn't
56 * pointing inside of the packet: free it.
58 if ((eap_packet->packet == NULL) ||
59 (eap_packet->type.data != (eap_packet->packet + 5))) {
60 free(eap_packet->type.data);
62 eap_packet->type.data = NULL;
65 if (eap_packet->packet) {
66 free(eap_packet->packet);
67 eap_packet->packet = NULL;
72 *eap_packet_ptr = NULL;
76 * Allocate a new EAP_PACKET
78 EAP_DS *eap_ds_alloc(void)
82 eap_ds = rad_malloc(sizeof(EAP_DS));
83 memset(eap_ds, 0, sizeof(EAP_DS));
84 if ((eap_ds->response = eap_packet_alloc()) == NULL) {
88 if ((eap_ds->request = eap_packet_alloc()) == NULL) {
96 void eap_ds_free(EAP_DS **eap_ds_p)
100 if (!eap_ds_p) return;
105 if (eap_ds->response) eap_packet_free(&(eap_ds->response));
106 if (eap_ds->request) eap_packet_free(&(eap_ds->request));
113 * Allocate a new EAP_HANDLER
115 EAP_HANDLER *eap_handler_alloc(rlm_eap_t *inst)
117 EAP_HANDLER *handler;
119 handler = rad_malloc(sizeof(EAP_HANDLER));
120 memset(handler, 0, sizeof(EAP_HANDLER));
122 if (fr_debug_flag && inst->handler_tree) {
123 pthread_mutex_lock(&(inst->handler_mutex));
124 rbtree_insert(inst->handler_tree, handler);
125 pthread_mutex_unlock(&(inst->handler_mutex));
131 void eap_handler_free(rlm_eap_t *inst, EAP_HANDLER *handler)
136 if (inst->handler_tree) {
137 pthread_mutex_lock(&(inst->handler_mutex));
138 rbtree_deletebydata(inst->handler_tree, handler);
139 pthread_mutex_unlock(&(inst->handler_mutex));
142 if (handler->identity) {
143 free(handler->identity);
144 handler->identity = NULL;
147 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
148 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
150 if ((handler->opaque) && (handler->free_opaque)) {
151 handler->free_opaque(handler->opaque);
152 handler->opaque = NULL;
154 else if ((handler->opaque) && (handler->free_opaque == NULL))
155 radlog(L_ERR, "Possible memory leak ...");
157 handler->opaque = NULL;
158 handler->free_opaque = NULL;
160 if (handler->certs) pairfree(&handler->certs);
166 typedef struct check_handler_t {
168 EAP_HANDLER *handler;
172 static void check_handler(void *data)
174 int do_warning = FALSE;
176 check_handler_t *check = data;
180 if (!check->inst || !check->handler) {
185 pthread_mutex_lock(&(check->inst->handler_mutex));
186 if (!rbtree_finddata(check->inst->handler_tree, check->handler)) {
191 * The session has continued *after* this packet.
192 * Don't do a warning.
194 if (check->handler->trips > check->trips) {
198 if (check->handler->tls && !check->handler->finished) {
200 memcpy(state, check->handler->state, sizeof(state));
204 pthread_mutex_unlock(&(check->inst->handler_mutex));
208 DEBUG("WARNING: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
209 DEBUG("WARNING: !! EAP session for state 0x%02x%02x%02x%02x%02x%02x%02x%02x did not finish!",
215 DEBUG("WARNING: !! Please read http://wiki.freeradius.org/Certificate_Compatibility");
216 DEBUG("WARNING: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
220 void eaptype_free(EAP_TYPES *i)
222 if (i->type->detach) (i->type->detach)(i->type_data);
224 if (i->handle) lt_dlclose(i->handle);
229 void eaplist_free(rlm_eap_t *inst)
231 EAP_HANDLER *node, *next;
233 for (node = inst->session_head; node != NULL; node = next) {
235 eap_handler_free(inst, node);
238 inst->session_head = inst->session_tail = NULL;
242 * Return a 32-bit random number.
244 static uint32_t eap_rand(fr_randctx *ctx)
248 num = ctx->randrsl[ctx->randcnt++];
249 if (ctx->randcnt >= 256) {
258 static EAP_HANDLER *eaplist_delete(rlm_eap_t *inst, EAP_HANDLER *handler)
262 node = rbtree_find(inst->session_tree, handler);
263 if (!node) return NULL;
265 handler = rbtree_node2data(inst->session_tree, node);
268 * Delete old handler from the tree.
270 rbtree_delete(inst->session_tree, node);
273 * And unsplice it from the linked list.
276 handler->prev->next = handler->next;
278 inst->session_head = handler->next;
281 handler->next->prev = handler->prev;
283 inst->session_tail = handler->prev;
285 handler->prev = handler->next = NULL;
291 static void eaplist_expire(rlm_eap_t *inst, time_t timestamp)
294 EAP_HANDLER *handler;
297 * Check the first few handlers in the list, and delete
298 * them if they're too old. We don't need to check them
299 * all, as incoming requests will quickly cause older
300 * handlers to be deleted.
303 for (i = 0; i < 3; i++) {
304 handler = inst->session_head;
308 * Expire entries from the start of the list.
309 * They should be the oldest ones.
311 if ((timestamp - handler->timestamp) > inst->timer_limit) {
313 node = rbtree_find(inst->session_tree, handler);
314 rad_assert(node != NULL);
315 rbtree_delete(inst->session_tree, node);
318 * handler == inst->session_head
320 inst->session_head = handler->next;
322 handler->next->prev = NULL;
324 inst->session_head = NULL;
325 inst->session_tail = NULL;
327 eap_handler_free(inst, handler);
333 * Add a handler to the set of active sessions.
335 * Since we're adding it to the list, we guess that this means
336 * the packet needs a State attribute. So add one.
338 int eaplist_add(rlm_eap_t *inst, EAP_HANDLER *handler)
342 REQUEST *request = handler->request;
344 rad_assert(handler != NULL);
345 rad_assert(request != NULL);
348 * Generate State, since we've been asked to add it to
351 state = pairmake("State", "0x00", T_OP_EQ);
352 if (!state) return 0;
355 * The time at which this request was made was the time
356 * at which it was received by the RADIUS server.
358 handler->timestamp = request->timestamp;
361 handler->src_ipaddr = request->packet->src_ipaddr;
362 handler->eap_id = handler->eap_ds->request->id;
365 * Playing with a data structure shared among threads
366 * means that we need a lock, to avoid conflict.
368 pthread_mutex_lock(&(inst->session_mutex));
371 * If we have a DoS attack, discard new sessions.
373 if (rbtree_num_elements(inst->session_tree) >= inst->max_sessions) {
375 eaplist_expire(inst, handler->timestamp);
380 * Create a unique content for the State variable.
381 * It will be modified slightly per round trip, but less so
384 if (handler->trips == 0) {
387 for (i = 0; i < 4; i++) {
390 lvalue = eap_rand(&inst->rand_pool);
392 memcpy(handler->state + i * 4, &lvalue,
397 memcpy(state->vp_octets, handler->state, sizeof(handler->state));
398 state->length = EAP_STATE_LEN;
401 * Add some more data to distinguish the sessions.
403 state->vp_octets[4] = handler->trips ^ handler->state[0];
404 state->vp_octets[5] = handler->eap_id ^ handler->state[1];
405 state->vp_octets[6] = handler->eap_type ^ handler->state[2];
408 * and copy the state back again.
410 memcpy(handler->state, state->vp_octets, sizeof(handler->state));
415 status = rbtree_insert(inst->session_tree, handler);
418 * Catch Access-Challenge without response.
421 check_handler_t *check = rad_malloc(sizeof(*check));
424 check->handler = handler;
425 check->trips = handler->trips;
426 request_data_add(request, inst, 0, check, check_handler);
432 prev = inst->session_tail;
434 prev->next = handler;
435 handler->prev = prev;
436 handler->next = NULL;
437 inst->session_tail = handler;
439 inst->session_head = inst->session_tail = handler;
440 handler->next = handler->prev = NULL;
445 * Now that we've finished mucking with the list,
451 * We don't need this any more.
453 if (status > 0) handler->request = NULL;
455 pthread_mutex_unlock(&(inst->session_mutex));
461 static time_t last_logged = 0;
463 if (last_logged < handler->timestamp) {
464 last_logged = handler->timestamp;
465 radlog(L_ERR, "rlm_eap: Too many open sessions. Try increasing \"max_sessions\" in the EAP module configuration");
468 radlog(L_ERR, "rlm_eap: Internal error: failed to store handler");
473 pairadd(&(request->reply->vps), state);
479 * Find a a previous EAP-Request sent by us, which matches
480 * the current EAP-Response.
482 * Then, release the handle from the list, and return it to
485 * Also since we fill the eap_ds with the present EAP-Response we
486 * got to free the prev_eapds & move the eap_ds to prev_eapds
488 EAP_HANDLER *eaplist_find(rlm_eap_t *inst, REQUEST *request,
489 eap_packet_t *eap_packet)
492 EAP_HANDLER *handler, myHandler;
495 * We key the sessions off of the 'state' attribute, so it
498 state = pairfind(request->packet->vps, PW_STATE, 0);
500 (state->length != EAP_STATE_LEN)) {
504 myHandler.src_ipaddr = request->packet->src_ipaddr;
505 myHandler.eap_id = eap_packet->id;
506 memcpy(myHandler.state, state->vp_strvalue, sizeof(myHandler.state));
509 * Playing with a data structure shared among threads
510 * means that we need a lock, to avoid conflict.
512 pthread_mutex_lock(&(inst->session_mutex));
514 eaplist_expire(inst, request->timestamp);
516 handler = eaplist_delete(inst, &myHandler);
517 pthread_mutex_unlock(&(inst->session_mutex));
520 * Might not have been there.
523 radlog(L_ERR, "rlm_eap: No EAP session matching the State variable.");
527 if (handler->trips >= 50) {
528 RDEBUG2("More than 50 authentication packets for this EAP session. Aborted.");
529 eap_handler_free(inst, handler);
534 RDEBUG2("Request found, released from the list");
537 * Remember what the previous request was.
539 eap_ds_free(&(handler->prev_eapds));
540 handler->prev_eapds = handler->eap_ds;
541 handler->eap_ds = NULL;