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(void)
117 EAP_HANDLER *handler;
119 handler = rad_malloc(sizeof(EAP_HANDLER));
120 memset(handler, 0, sizeof(EAP_HANDLER));
124 void eap_handler_free(EAP_HANDLER *handler)
129 if (handler->identity) {
130 free(handler->identity);
131 handler->identity = NULL;
134 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
135 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
137 if ((handler->opaque) && (handler->free_opaque)) {
138 handler->free_opaque(handler->opaque);
139 handler->opaque = NULL;
141 else if ((handler->opaque) && (handler->free_opaque == NULL))
142 radlog(L_ERR, "Possible memory leak ...");
144 handler->opaque = NULL;
145 handler->free_opaque = NULL;
150 void eaptype_free(EAP_TYPES *i)
152 if (i->type->detach) (i->type->detach)(i->type_data);
154 if (i->handle) lt_dlclose(i->handle);
159 void eaplist_free(rlm_eap_t *inst)
161 EAP_HANDLER *node, *next;
163 for (node = inst->session_head; node != NULL; node = next) {
165 eap_handler_free(node);
168 inst->session_head = inst->session_tail = NULL;
172 * Return a 32-bit random number.
174 static uint32_t eap_rand(fr_randctx *ctx)
178 num = ctx->randrsl[ctx->randcnt++];
179 if (ctx->randcnt == 256) {
188 * Add a handler to the set of active sessions.
190 * Since we're adding it to the list, we guess that this means
191 * the packet needs a State attribute. So add one.
193 int eaplist_add(rlm_eap_t *inst, EAP_HANDLER *handler)
199 rad_assert(handler != NULL);
200 rad_assert(handler->request != NULL);
203 * Generate State, since we've been asked to add it to
206 state = pairmake("State", "0x00", T_OP_EQ);
207 if (!state) return 0;
208 pairadd(&(handler->request->reply->vps), state);
209 state->length = EAP_STATE_LEN;
212 * The time at which this request was made was the time
213 * at which it was received by the RADIUS server.
215 handler->timestamp = handler->request->timestamp;
218 handler->src_ipaddr = handler->request->packet->src_ipaddr;
219 handler->eap_id = handler->eap_ds->request->id;
222 * We don't need this any more.
224 handler->request = NULL;
227 * Playing with a data structure shared among threads
228 * means that we need a lock, to avoid conflict.
230 pthread_mutex_lock(&(inst->session_mutex));
233 * Create a completely random state.
235 for (i = 0; i < 4; i++) {
236 lvalue = eap_rand(&inst->rand_pool);
237 memcpy(state->vp_octets + i * 4, &lvalue, sizeof(lvalue));
239 state->vp_octets[15] = handler->eap_id;
240 memcpy(handler->state, state->vp_strvalue, sizeof(handler->state));
245 status = rbtree_insert(inst->session_tree, handler);
250 prev = inst->session_tail;
252 prev->next = handler;
253 handler->prev = prev;
254 handler->next = NULL;
255 inst->session_tail = handler;
257 inst->session_head = inst->session_tail = handler;
258 handler->next = handler->prev = NULL;
263 * Now that we've finished mucking with the list,
266 pthread_mutex_unlock(&(inst->session_mutex));
269 radlog(L_ERR, "rlm_eap: Failed to remember handler!");
270 eap_handler_free(handler);
278 * Find a a previous EAP-Request sent by us, which matches
279 * the current EAP-Response.
281 * Then, release the handle from the list, and return it to
284 * Also since we fill the eap_ds with the present EAP-Response we
285 * got to free the prev_eapds & move the eap_ds to prev_eapds
287 EAP_HANDLER *eaplist_find(rlm_eap_t *inst, REQUEST *request,
288 eap_packet_t *eap_packet)
293 EAP_HANDLER *handler, myHandler;
296 * We key the sessions off of the 'state' attribute, so it
299 state = pairfind(request->packet->vps, PW_STATE);
301 (state->length != EAP_STATE_LEN)) {
305 myHandler.src_ipaddr = request->packet->src_ipaddr;
306 myHandler.eap_id = eap_packet->id;
307 memcpy(myHandler.state, state->vp_strvalue, sizeof(myHandler.state));
310 * Playing with a data structure shared among threads
311 * means that we need a lock, to avoid conflict.
313 pthread_mutex_lock(&(inst->session_mutex));
316 * Check the first few handlers in the list, and delete
317 * them if they're too old. We don't need to check them
318 * all, as incoming requests will quickly cause older
319 * handlers to be deleted.
322 for (i = 0; i < 2; i++) {
323 handler = inst->session_head;
325 ((request->timestamp - handler->timestamp) > inst->timer_limit)) {
326 node = rbtree_find(inst->session_tree, handler);
327 rad_assert(node != NULL);
328 rbtree_delete(inst->session_tree, node);
331 * handler == inst->session_head
333 inst->session_head = handler->next;
335 handler->next->prev = NULL;
337 inst->session_head = NULL;
339 eap_handler_free(handler);
344 node = rbtree_find(inst->session_tree, &myHandler);
346 handler = rbtree_node2data(inst->session_tree, node);
349 * Delete old handler from the tree.
351 rbtree_delete(inst->session_tree, node);
354 * And unsplice it from the linked list.
357 handler->prev->next = handler->next;
359 inst->session_head = handler->next;
362 handler->next->prev = handler->prev;
364 inst->session_tail = handler->prev;
366 handler->prev = handler->next = NULL;
369 pthread_mutex_unlock(&(inst->session_mutex));
375 DEBUG2(" rlm_eap: Request not found in the list");
380 * Found, but state verification failed.
383 radlog(L_ERR, "rlm_eap: State verification failed.");
387 if (handler->trips >= 50) {
388 DEBUG2(" rlm_eap: More than 50 authentication packets for this EAP session. Aborted.");
389 eap_handler_free(handler);
394 DEBUG2(" rlm_eap: Request found, released from the list");
397 * Remember what the previous request was.
399 eap_ds_free(&(handler->prev_eapds));
400 handler->prev_eapds = handler->eap_ds;
401 handler->eap_ds = NULL;