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>
30 #define PTHREAD_MUTEX_LOCK pthread_mutex_lock
31 #define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
33 #define PTHREAD_MUTEX_LOCK(_x)
34 #define PTHREAD_MUTEX_UNLOCK(_x)
38 * Allocate a new eap_packet_t
40 EAP_DS *eap_ds_alloc(eap_handler_t *handler)
44 eap_ds = talloc_zero(handler, EAP_DS);
45 eap_ds->response = talloc_zero(eap_ds, eap_packet_t);
46 if (!eap_ds->response) {
50 eap_ds->request = talloc_zero(eap_ds, eap_packet_t);
51 if (!eap_ds->response) {
59 void eap_ds_free(EAP_DS **eap_ds_p)
63 if (!eap_ds_p) return;
68 if (eap_ds->response) talloc_free(eap_ds->response);
69 if (eap_ds->request) talloc_free(eap_ds->request);
75 static int _eap_handler_free(eap_handler_t *handler)
77 if (handler->identity) {
78 talloc_free(handler->identity);
79 handler->identity = NULL;
82 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
83 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
85 if ((handler->opaque) && (handler->free_opaque)) {
86 handler->free_opaque(handler->opaque);
87 handler->opaque = NULL;
90 handler->opaque = NULL;
91 handler->free_opaque = NULL;
93 if (handler->certs) fr_pair_list_free(&handler->certs);
96 * Give helpful debug messages if:
98 * we're debugging TLS sessions, which don't finish,
99 * and which aren't deleted early due to a likely RADIUS
100 * retransmit which nukes our ID, and therefore our stare.
102 if (fr_debug_lvl && handler->tls && !handler->finished &&
103 (time(NULL) > (handler->timestamp + 3))) {
104 WARN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
105 WARN("!! EAP session with state 0x%02x%02x%02x%02x%02x%02x%02x%02x did not finish! !!",
106 handler->state[0], handler->state[1],
107 handler->state[2], handler->state[3],
108 handler->state[4], handler->state[5],
109 handler->state[6], handler->state[7]);
111 WARN("!! Please read http://wiki.freeradius.org/guide/Certificate_Compatibility !!");
112 WARN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
119 * Allocate a new eap_handler_t
121 eap_handler_t *eap_handler_alloc(rlm_eap_t *inst)
123 eap_handler_t *handler;
125 handler = talloc_zero(NULL, eap_handler_t);
127 ERROR("Failed allocating handler");
130 handler->inst_holder = inst;
132 /* Doesn't need to be inside the critical region */
133 talloc_set_destructor(handler, _eap_handler_free);
139 void eaplist_free(rlm_eap_t *inst)
141 eap_handler_t *node, *next;
143 for (node = inst->session_head; node != NULL; node = next) {
148 inst->session_head = inst->session_tail = NULL;
152 * Return a 32-bit random number.
154 static uint32_t eap_rand(fr_randctx *ctx)
158 num = ctx->randrsl[ctx->randcnt++];
159 if (ctx->randcnt >= 256) {
168 static eap_handler_t *eaplist_delete(rlm_eap_t *inst, REQUEST *request,
169 eap_handler_t *handler)
173 node = rbtree_find(inst->session_tree, handler);
174 if (!node) return NULL;
176 handler = rbtree_node2data(inst->session_tree, node);
178 RDEBUG("Finished EAP session with state "
179 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
180 handler->state[0], handler->state[1],
181 handler->state[2], handler->state[3],
182 handler->state[4], handler->state[5],
183 handler->state[6], handler->state[7]);
185 * Delete old handler from the tree.
187 rbtree_delete(inst->session_tree, node);
190 * And unsplice it from the linked list.
193 handler->prev->next = handler->next;
195 inst->session_head = handler->next;
198 handler->next->prev = handler->prev;
200 inst->session_tail = handler->prev;
202 handler->prev = handler->next = NULL;
208 static void eaplist_expire(rlm_eap_t *inst, REQUEST *request, time_t timestamp)
211 eap_handler_t *handler;
214 * Check the first few handlers in the list, and delete
215 * them if they're too old. We don't need to check them
216 * all, as incoming requests will quickly cause older
217 * handlers to be deleted.
220 for (i = 0; i < 3; i++) {
221 handler = inst->session_head;
224 RDEBUG("Expiring EAP session with state "
225 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
226 handler->state[0], handler->state[1],
227 handler->state[2], handler->state[3],
228 handler->state[4], handler->state[5],
229 handler->state[6], handler->state[7]);
232 * Expire entries from the start of the list.
233 * They should be the oldest ones.
235 if ((timestamp - handler->timestamp) > (int)inst->timer_limit) {
237 node = rbtree_find(inst->session_tree, handler);
238 rad_assert(node != NULL);
239 rbtree_delete(inst->session_tree, node);
242 * handler == inst->session_head
244 inst->session_head = handler->next;
246 handler->next->prev = NULL;
248 inst->session_head = NULL;
249 inst->session_tail = NULL;
251 talloc_free(handler);
259 * Add a handler to the set of active sessions.
261 * Since we're adding it to the list, we guess that this means
262 * the packet needs a State attribute. So add one.
264 int eaplist_add(rlm_eap_t *inst, eap_handler_t *handler)
268 REQUEST *request = handler->request;
271 * Generate State, since we've been asked to add it to
274 state = pair_make_reply("State", NULL, T_OP_EQ);
275 if (!state) return 0;
278 * The time at which this request was made was the time
279 * at which it was received by the RADIUS server.
281 handler->timestamp = request->timestamp;
284 handler->src_ipaddr = request->packet->src_ipaddr;
285 handler->eap_id = handler->eap_ds->request->id;
288 * Playing with a data structure shared among threads
289 * means that we need a lock, to avoid conflict.
291 PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
294 * If we have a DoS attack, discard new sessions.
296 if (rbtree_num_elements(inst->session_tree) >= inst->max_sessions) {
298 eaplist_expire(inst, request, handler->timestamp);
303 * Create a unique content for the State variable.
304 * It will be modified slightly per round trip, but less so
307 if (handler->trips == 0) {
310 for (i = 0; i < 4; i++) {
313 lvalue = eap_rand(&inst->rand_pool);
315 memcpy(handler->state + i * 4, &lvalue,
321 * Add some more data to distinguish the sessions.
323 handler->state[4] = handler->trips ^ handler->state[0];
324 handler->state[5] = handler->eap_id ^ handler->state[1];
325 handler->state[6] = handler->type ^ handler->state[2];
327 fr_pair_value_memcpy(state, handler->state, sizeof(handler->state));
332 status = rbtree_insert(inst->session_tree, handler);
337 prev = inst->session_tail;
339 prev->next = handler;
340 handler->prev = prev;
341 handler->next = NULL;
342 inst->session_tail = handler;
344 inst->session_head = inst->session_tail = handler;
345 handler->next = handler->prev = NULL;
350 * Now that we've finished mucking with the list,
356 * We don't need this any more.
358 if (status > 0) handler->request = NULL;
360 PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
363 fr_pair_delete_by_num(&request->reply->vps, PW_STATE, 0, TAG_ANY);
366 static time_t last_logged = 0;
368 if (last_logged < handler->timestamp) {
369 last_logged = handler->timestamp;
370 ERROR("rlm_eap (%s): Too many open sessions. Try increasing \"max_sessions\" "
371 "in the EAP module configuration", inst->xlat_name);
374 ERROR("rlm_eap (%s): Failed to store handler", inst->xlat_name);
379 RDEBUG("EAP session adding &reply:State = 0x%02x%02x%02x%02x%02x%02x%02x%02x",
380 state->vp_octets[0], state->vp_octets[1], state->vp_octets[2], state->vp_octets[3],
381 state->vp_octets[4], state->vp_octets[5], state->vp_octets[6], state->vp_octets[7]);
387 * Find a a previous EAP-Request sent by us, which matches
388 * the current EAP-Response.
390 * Then, release the handle from the list, and return it to
393 * Also since we fill the eap_ds with the present EAP-Response we
394 * got to free the prev_eapds & move the eap_ds to prev_eapds
396 eap_handler_t *eaplist_find(rlm_eap_t *inst, REQUEST *request,
397 eap_packet_raw_t *eap_packet)
400 eap_handler_t *handler, myHandler;
403 * We key the sessions off of the 'state' attribute, so it
406 state = fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY);
408 REDEBUG("EAP requires the State attribute to work, but no State exists in the Access-Request packet.");
409 REDEBUG("The RADIUS client is broken. No amount of changing FreeRADIUS will fix the RADIUS client.");
413 if (state->vp_length != EAP_STATE_LEN) {
414 REDEBUG("The RADIUS client has mangled the State attribute, OR you are forcing EAP in the wrong situation");
418 myHandler.src_ipaddr = request->packet->src_ipaddr;
419 myHandler.eap_id = eap_packet->id;
420 memcpy(myHandler.state, state->vp_strvalue, sizeof(myHandler.state));
423 * Playing with a data structure shared among threads
424 * means that we need a lock, to avoid conflict.
426 PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
428 eaplist_expire(inst, request, request->timestamp);
430 handler = eaplist_delete(inst, request, &myHandler);
431 PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
434 * Might not have been there.
437 RERROR("rlm_eap (%s): No EAP session matching state "
438 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
440 state->vp_octets[0], state->vp_octets[1],
441 state->vp_octets[2], state->vp_octets[3],
442 state->vp_octets[4], state->vp_octets[5],
443 state->vp_octets[6], state->vp_octets[7]);
447 if (handler->trips >= 50) {
448 RERROR("rlm_eap (%s): Aborting! More than 50 roundtrips "
449 "made in session with state "
450 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
452 state->vp_octets[0], state->vp_octets[1],
453 state->vp_octets[2], state->vp_octets[3],
454 state->vp_octets[4], state->vp_octets[5],
455 state->vp_octets[6], state->vp_octets[7]);
458 talloc_free(handler);
463 RDEBUG("Previous EAP request found for state "
464 "0x%02x%02x%02x%02x%02x%02x%02x%02x, released from the list",
465 state->vp_octets[0], state->vp_octets[1],
466 state->vp_octets[2], state->vp_octets[3],
467 state->vp_octets[4], state->vp_octets[5],
468 state->vp_octets[6], state->vp_octets[7]);
471 * Remember what the previous request was.
473 eap_ds_free(&(handler->prev_eapds));
474 handler->prev_eapds = handler->eap_ds;
475 handler->eap_ds = NULL;