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 rlm_eap_t *inst = handler->inst_holder;
79 if (handler->identity) {
80 talloc_free(handler->identity);
81 handler->identity = NULL;
84 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
85 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
87 if ((handler->opaque) && (handler->free_opaque)) {
88 handler->free_opaque(handler->opaque);
89 handler->opaque = NULL;
92 handler->opaque = NULL;
93 handler->free_opaque = NULL;
95 if (handler->certs) pairfree(&handler->certs);
97 PTHREAD_MUTEX_LOCK(&(inst->handler_mutex));
98 if (inst->handler_tree) {
99 rbtree_deletebydata(inst->handler_tree, handler);
102 * Free operations need to be synchronised too.
104 talloc_free(handler);
105 PTHREAD_MUTEX_UNLOCK(&(inst->handler_mutex));
111 * Allocate a new eap_handler_t
113 eap_handler_t *eap_handler_alloc(rlm_eap_t *inst)
115 eap_handler_t *handler;
117 PTHREAD_MUTEX_LOCK(&(inst->handler_mutex));
118 handler = talloc_zero(inst, eap_handler_t);
120 if (inst->handler_tree) {
121 rbtree_insert(inst->handler_tree, handler);
123 handler->inst_holder = inst;
125 PTHREAD_MUTEX_UNLOCK(&(inst->handler_mutex));
126 talloc_set_destructor(handler, _eap_handler_free);
131 typedef struct check_handler_t {
133 eap_handler_t *handler;
137 static int check_opaque_free(check_handler_t *check)
139 bool do_warning = false;
142 if (!check->inst || !check->handler) {
146 if (!check->inst->handler_tree) goto done;
148 PTHREAD_MUTEX_LOCK(&(check->inst->handler_mutex));
149 if (!rbtree_finddata(check->inst->handler_tree, check->handler)) {
154 * The session has continued *after* this packet.
155 * Don't do a warning.
157 if (check->handler->trips > check->trips) {
162 * No TLS means no warnings.
164 if (!check->handler->tls) goto done;
167 * If we're being deleted early, it's likely because we
168 * received a transmit from the client that re-uses the
169 * same RADIUS Id, which forces the current packet to be
170 * deleted. In that case, ignore the error.
172 if (time(NULL) < (check->handler->timestamp + 3)) goto done;
174 if (!check->handler->finished) {
176 memcpy(state, check->handler->state, sizeof(state));
180 PTHREAD_MUTEX_UNLOCK(&(check->inst->handler_mutex));
183 WARN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
184 WARN("!! EAP session with state 0x%02x%02x%02x%02x%02x%02x%02x%02x did not finish! !!",
190 WARN("!! Please read http://wiki.freeradius.org/guide/Certificate_Compatibility !!");
191 WARN("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
197 void eaplist_free(rlm_eap_t *inst)
199 eap_handler_t *node, *next;
201 for (node = inst->session_head; node != NULL; node = next) {
206 inst->session_head = inst->session_tail = NULL;
210 * Return a 32-bit random number.
212 static uint32_t eap_rand(fr_randctx *ctx)
216 num = ctx->randrsl[ctx->randcnt++];
217 if (ctx->randcnt >= 256) {
226 static eap_handler_t *eaplist_delete(rlm_eap_t *inst, REQUEST *request,
227 eap_handler_t *handler)
231 node = rbtree_find(inst->session_tree, handler);
232 if (!node) return NULL;
234 handler = rbtree_node2data(inst->session_tree, node);
236 RDEBUG("Finished EAP session with state "
237 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
238 handler->state[0], handler->state[1],
239 handler->state[2], handler->state[3],
240 handler->state[4], handler->state[5],
241 handler->state[6], handler->state[7]);
243 * Delete old handler from the tree.
245 rbtree_delete(inst->session_tree, node);
248 * And unsplice it from the linked list.
251 handler->prev->next = handler->next;
253 inst->session_head = handler->next;
256 handler->next->prev = handler->prev;
258 inst->session_tail = handler->prev;
260 handler->prev = handler->next = NULL;
266 static void eaplist_expire(rlm_eap_t *inst, REQUEST *request, time_t timestamp)
269 eap_handler_t *handler;
272 * Check the first few handlers in the list, and delete
273 * them if they're too old. We don't need to check them
274 * all, as incoming requests will quickly cause older
275 * handlers to be deleted.
278 for (i = 0; i < 3; i++) {
279 handler = inst->session_head;
282 RDEBUG("Expiring EAP session with state "
283 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
284 handler->state[0], handler->state[1],
285 handler->state[2], handler->state[3],
286 handler->state[4], handler->state[5],
287 handler->state[6], handler->state[7]);
290 * Expire entries from the start of the list.
291 * They should be the oldest ones.
293 if ((timestamp - handler->timestamp) > (int)inst->timer_limit) {
295 node = rbtree_find(inst->session_tree, handler);
296 rad_assert(node != NULL);
297 rbtree_delete(inst->session_tree, node);
300 * handler == inst->session_head
302 inst->session_head = handler->next;
304 handler->next->prev = NULL;
306 inst->session_head = NULL;
307 inst->session_tail = NULL;
309 talloc_free(handler);
317 * Add a handler to the set of active sessions.
319 * Since we're adding it to the list, we guess that this means
320 * the packet needs a State attribute. So add one.
322 int eaplist_add(rlm_eap_t *inst, eap_handler_t *handler)
326 REQUEST *request = handler->request;
329 * Generate State, since we've been asked to add it to
332 state = pairmake_reply("State", NULL, T_OP_EQ);
333 if (!state) return 0;
336 * The time at which this request was made was the time
337 * at which it was received by the RADIUS server.
339 handler->timestamp = request->timestamp;
342 handler->src_ipaddr = request->packet->src_ipaddr;
343 handler->eap_id = handler->eap_ds->request->id;
346 * Playing with a data structure shared among threads
347 * means that we need a lock, to avoid conflict.
349 PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
352 * If we have a DoS attack, discard new sessions.
354 if (rbtree_num_elements(inst->session_tree) >= inst->max_sessions) {
356 eaplist_expire(inst, request, handler->timestamp);
361 * Create a unique content for the State variable.
362 * It will be modified slightly per round trip, but less so
365 if (handler->trips == 0) {
368 for (i = 0; i < 4; i++) {
371 lvalue = eap_rand(&inst->rand_pool);
373 memcpy(handler->state + i * 4, &lvalue,
379 * Add some more data to distinguish the sessions.
381 handler->state[4] = handler->trips ^ handler->state[0];
382 handler->state[5] = handler->eap_id ^ handler->state[1];
383 handler->state[6] = handler->type ^ handler->state[2];
385 pairmemcpy(state, handler->state, sizeof(handler->state));
390 status = rbtree_insert(inst->session_tree, handler);
393 * Catch Access-Challenge without response.
395 if (inst->handler_tree) {
396 check_handler_t *check = talloc(handler, check_handler_t);
399 check->handler = handler;
400 check->trips = handler->trips;
402 talloc_set_destructor(check, check_opaque_free);
403 request_data_add(request, inst, 0, check, true);
409 prev = inst->session_tail;
411 prev->next = handler;
412 handler->prev = prev;
413 handler->next = NULL;
414 inst->session_tail = handler;
416 inst->session_head = inst->session_tail = handler;
417 handler->next = handler->prev = NULL;
422 * Now that we've finished mucking with the list,
428 * We don't need this any more.
430 if (status > 0) handler->request = NULL;
432 PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
438 static time_t last_logged = 0;
440 if (last_logged < handler->timestamp) {
441 last_logged = handler->timestamp;
442 ERROR("rlm_eap (%s): Too many open sessions. Try increasing \"max_sessions\" "
443 "in the EAP module configuration", inst->xlat_name);
446 ERROR("rlm_eap (%s): Failed to store handler", inst->xlat_name);
451 RDEBUG("New EAP session, adding 'State' attribute to reply 0x%02x%02x%02x%02x%02x%02x%02x%02x",
452 state->vp_octets[0], state->vp_octets[1], state->vp_octets[2], state->vp_octets[3],
453 state->vp_octets[4], state->vp_octets[5], state->vp_octets[6], state->vp_octets[7]);
459 * Find a a previous EAP-Request sent by us, which matches
460 * the current EAP-Response.
462 * Then, release the handle from the list, and return it to
465 * Also since we fill the eap_ds with the present EAP-Response we
466 * got to free the prev_eapds & move the eap_ds to prev_eapds
468 eap_handler_t *eaplist_find(rlm_eap_t *inst, REQUEST *request,
469 eap_packet_raw_t *eap_packet)
472 eap_handler_t *handler, myHandler;
475 * We key the sessions off of the 'state' attribute, so it
478 state = pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
480 (state->length != EAP_STATE_LEN)) {
484 myHandler.src_ipaddr = request->packet->src_ipaddr;
485 myHandler.eap_id = eap_packet->id;
486 memcpy(myHandler.state, state->vp_strvalue, sizeof(myHandler.state));
489 * Playing with a data structure shared among threads
490 * means that we need a lock, to avoid conflict.
492 PTHREAD_MUTEX_LOCK(&(inst->session_mutex));
494 eaplist_expire(inst, request, request->timestamp);
496 handler = eaplist_delete(inst, request, &myHandler);
497 PTHREAD_MUTEX_UNLOCK(&(inst->session_mutex));
500 * Might not have been there.
503 ERROR("rlm_eap (%s): No EAP session matching state "
504 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
506 state->vp_octets[0], state->vp_octets[1],
507 state->vp_octets[2], state->vp_octets[3],
508 state->vp_octets[4], state->vp_octets[5],
509 state->vp_octets[6], state->vp_octets[7]);
513 if (handler->trips >= 50) {
514 ERROR("rlm_eap (%s): Aborting! More than 50 roundtrips "
515 "made in session with state "
516 "0x%02x%02x%02x%02x%02x%02x%02x%02x",
518 state->vp_octets[0], state->vp_octets[1],
519 state->vp_octets[2], state->vp_octets[3],
520 state->vp_octets[4], state->vp_octets[5],
521 state->vp_octets[6], state->vp_octets[7]);
524 talloc_free(handler);
529 RDEBUG("Previous EAP request found for state "
530 "0x%02x%02x%02x%02x%02x%02x%02x%02x, released from the list",
531 state->vp_octets[0], state->vp_octets[1],
532 state->vp_octets[2], state->vp_octets[3],
533 state->vp_octets[4], state->vp_octets[5],
534 state->vp_octets[6], state->vp_octets[7]);
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;