2 * eap.c rfc2284 & rfc2869 implementation
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000-2003 The FreeRADIUS server project
21 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
22 * Copyright 2003 Alan DeKok <aland@freeradius.org>
28 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * | Code | Identifier | Length |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * EAP Request and Response Packet Format
37 * --- ------- --- -------- ------ ------
39 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Code | Identifier | Length |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Type | Type-Data ...
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
47 * EAP Success and Failure Packet Format
48 * --- ------- --- ------- ------ ------
50 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Code | Identifier | Length |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 static const char rcsid[] = "$Id$";
61 static const char *eap_codes[] = {
62 "", /* 0 is invalid */
70 * Load all the required eap authentication types.
71 * Get all the supported EAP-types from config file.
73 int eaptype_load(EAP_TYPES **type, int eap_type, CONF_SECTION *cs)
79 snprintf(buffer, sizeof(buffer), "rlm_eap_%s", eap_types[eap_type]);
81 /* Link the loaded EAP-Type */
82 handle = lt_dlopenext(buffer);
84 radlog(L_ERR, "rlm_eap: Failed to link EAP-Type/%s: %s",
85 eap_types[eap_type], lt_dlerror());
89 /* Make room for the EAP-Type */
90 node = (EAP_TYPES *)malloc(sizeof(EAP_TYPES));
92 radlog(L_ERR, "rlm_eap: out of memory");
95 memset(node, 0, sizeof(*node));
97 /* fill in the structure */
98 node->handle = handle;
100 node->typename = eap_types[eap_type];
101 node->type_data = NULL;
103 node->type = (EAP_TYPE *)lt_dlsym(node->handle, buffer);
105 radlog(L_ERR, "rlm_eap: Failed linking to %s structure in %s: %s",
106 buffer, eap_types[eap_type], lt_dlerror());
107 lt_dlclose(node->handle); /* ignore any errors */
111 if ((node->type->attach) &&
112 ((node->type->attach)(node->cs, &(node->type_data)) < 0)) {
114 radlog(L_ERR, "rlm_eap: Failed to initialize type %s",
115 eap_types[eap_type]);
116 lt_dlclose(node->handle);
121 DEBUG("rlm_eap: Loaded and initialized type %s", eap_types[eap_type]);
128 * Call the appropriate handle with the right eap_type.
130 static int eaptype_call(EAP_TYPES *atype, EAP_HANDLER *handler)
134 DEBUG2(" rlm_eap: processing type %s", atype->typename);
136 rad_assert(atype != NULL);
138 switch (handler->stage) {
140 if (!atype->type->initiate(atype->type_data, handler))
146 * The called function updates the EAP reply packet.
148 if (!atype->type->authorize ||
149 !atype->type->authorize(atype->type_data, handler))
155 * The called function updates the EAP reply packet.
157 if (!atype->type->authenticate ||
158 !atype->type->authenticate(atype->type_data, handler))
163 /* Should never enter here */
164 radlog(L_DBG, "rlm_eap: Invalid operation on eap_type");
173 * Based on TYPE, call the appropriate EAP-type handler
174 * Default to the configured EAP-Type
175 * for all Unsupported EAP-Types
177 int eaptype_select(rlm_eap_t *inst, EAP_HANDLER *handler)
180 int default_eap_type = inst->default_eap_type;
182 eaptype = &handler->eap_ds->response->type;
185 * Don't trust anyone.
187 if ((eaptype->type == 0) ||
188 (eaptype->type > PW_EAP_MAX_TYPES)) {
189 DEBUG2(" rlm_eap: Asked to select bad type");
194 * Figure out what to do.
196 switch(eaptype->type) {
197 case PW_EAP_IDENTITY:
201 DEBUG2(" rlm_eap: EAP Identity");
204 * Allow per-user configuration of EAP types.
206 vp = pairfind(handler->request->config_items,
208 if (vp) default_eap_type = vp->lvalue;
214 if ((default_eap_type < PW_EAP_MD5) ||
215 (default_eap_type > PW_EAP_MAX_TYPES) ||
216 (inst->types[default_eap_type] == NULL)) {
217 DEBUG2(" rlm_eap: No such EAP type %d",
222 handler->stage = INITIATE;
223 handler->eap_type = default_eap_type;
226 * Wild & crazy stuff! For TTLS & PEAP,
227 * we initiate a TLS session, and then pass
228 * that session data to TTLS or PEAP for
229 * the authenticate stage.
231 * Handler->eap_type holds the TRUE
234 if ((default_eap_type == PW_EAP_TTLS) ||
235 (default_eap_type == PW_EAP_PEAP)) {
236 default_eap_type = PW_EAP_TLS;
241 * We don't do TLS inside of TLS, as it's
244 if (((handler->request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) != 0) &&
245 (default_eap_type == PW_EAP_TLS)) {
246 DEBUG2(" rlm_eap: Unable to tunnel TLS inside of TLS");
250 if (eaptype_call(inst->types[default_eap_type],
252 DEBUG2(" rlm_eap: Default EAP type %s failed in initiate", eap_types[default_eap_type]);
260 * The one byte of NAK data is the preferred EAP type
263 DEBUG2(" rlm_eap: EAP NAK");
266 * Delete old data, if necessary.
268 if (handler->opaque && handler->free_opaque) {
269 handler->free_opaque(handler->opaque);
270 handler->free_opaque = NULL;
271 handler->opaque = NULL;
275 * It is invalid to request identity,
276 * notification & nak in nak
278 if (eaptype->data == NULL) {
279 DEBUG2(" rlm_eap: Empty NAK packet, cannot decide what EAP type the client wants.");
283 if ((eaptype->data[0] < PW_EAP_MD5) ||
284 (eaptype->data[0] > PW_EAP_MAX_TYPES)) {
285 DEBUG2(" rlm_eap: NAK asked for bad type %d",
290 default_eap_type = eaptype->data[0];
291 DEBUG2(" rlm_eap: EAP-NAK asked for EAP-Type/%s",
292 eap_types[default_eap_type]);
294 if (handler->eap_type == default_eap_type) {
295 DEBUG2(" rlm_eap: ERROR! Our request for %s was NAK'd with a request for %s, what is the client thinking?",
296 eap_types[default_eap_type],
297 eap_types[default_eap_type]);
304 * Key off of the configured sub-modules.
307 DEBUG2(" rlm_eap: EAP_TYPE - %s",
308 eap_types[eaptype->type]);
311 * We haven't configured it, it doesn't exit.
313 if (!inst->types[eaptype->type]) {
314 DEBUG2(" rlm_eap: EAP type %d is unsupported",
319 rad_assert(handler->stage == AUTHENTICATE);
320 handler->eap_type = eaptype->type;
321 if (eaptype_call(inst->types[eaptype->type],
323 DEBUG2(" rlm_eap: Handler failed in EAP type %d",
334 * Handles multiple EAP-Message attrs
335 * ie concatenates all to get the complete EAP packet
337 * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
338 * refer fragmentation in rfc2869.
340 eap_packet_t *eap_attribute(VALUE_PAIR *vps)
342 VALUE_PAIR *first, *vp;
343 eap_packet_t *eap_packet;
349 * Get only EAP-Message attribute list
351 first = pairfind(vps, PW_EAP_MESSAGE);
353 radlog(L_ERR, "rlm_eap: EAP-Message not found. You have misconfigured the server.");
354 DEBUG2(" You have 'Auth-Type := EAP' in another module or configuration file.");
355 DEBUG2(" DO NOT set 'Auth-Type' to 'EAP'. The EAP module will do it, if necessary.");
360 * Sanity check the length before doing anything.
362 if (first->length < 4) {
363 radlog(L_ERR, "rlm_eap: EAP packet is too short.");
368 * Get the Actual length from the EAP packet
369 * First EAP-Message contains the EAP packet header
371 memcpy(&len, first->strvalue + 2, sizeof(len));
375 * Take out even more weird things.
378 radlog(L_ERR, "rlm_eap: EAP packet has invalid length.");
383 * Sanity check the length, BEFORE malloc'ing memory.
386 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
387 total_len += vp->length;
389 if (total_len > len) {
390 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
396 * If the length is SMALLER, die, too.
398 if (total_len < len) {
399 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
404 * Now that we know the lengths are OK, allocate memory.
406 eap_packet = (eap_packet_t *) malloc(len);
407 if (eap_packet == NULL) {
408 radlog(L_ERR, "rlm_eap: out of memory");
413 * Copy the data from EAP-Message's over to out EAP packet.
415 ptr = (unsigned char *)eap_packet;
417 /* RADIUS ensures order of attrs, so just concatenate all */
418 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
419 memcpy(ptr, vp->strvalue, vp->length);
427 * EAP packet format to be sent over the wire
429 * i.e. code+id+length+data where data = null/type+typedata
432 static int eap_wireformat(EAP_PACKET *reply)
435 uint16_t total_length = 0;
437 if (reply == NULL) return EAP_INVALID;
439 total_length = EAP_HEADER_LEN;
440 if (reply->code < 3) {
441 total_length += 1/*EAPtype*/;
442 if (reply->type.data && reply->type.length > 0) {
443 total_length += reply->type.length;
447 reply->packet = (unsigned char *)malloc(total_length);
448 hdr = (eap_packet_t *)reply->packet;
450 radlog(L_ERR, "rlm_eap: out of memory");
454 hdr->code = (reply->code & 0xFF);
455 hdr->id = (reply->id & 0xFF);
456 total_length = htons(total_length);
457 memcpy(hdr->length, &total_length, sizeof(uint16_t));
460 * Request and Response packets are special.
462 if ((reply->code == PW_EAP_REQUEST) ||
463 (reply->code == PW_EAP_RESPONSE)) {
464 hdr->data[0] = (reply->type.type & 0xFF);
467 * Here since we cannot know the typedata format and length
469 * Type_data is expected to be wired by each EAP-Type
471 * Zero length/No typedata is supported as long as
474 if (reply->type.data && reply->type.length > 0) {
475 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
476 free(reply->type.data);
477 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
485 * compose EAP reply packet in EAP-Message attr of RADIUS. If
486 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
488 * Set the RADIUS reply codes based on EAP request codes. Append
489 * any additonal VPs to RADIUS reply
491 int eap_compose(EAP_HANDLER *handler)
493 uint16_t eap_len, len;
496 eap_packet_t *eap_packet;
498 REQUEST *request = handler->request;
499 EAP_DS *eap_ds = handler->eap_ds;
500 EAP_PACKET *reply = eap_ds->request;
504 * The Id for the EAP packet to the NAS wasn't set.
507 * LEAP requires the Id to be incremented on EAP-Success
508 * in Stage 4, so that we can carry on the conversation
509 * where the client asks us to authenticate ourselves
512 if (!eap_ds->set_request_id) {
514 * Id serves to suppport request/response
515 * retransmission in the EAP layer and as such
516 * must be different for 'adjacent' packets
517 * except in case of success/failure-replies.
519 * RFC2716 (EAP-TLS) requires this to be
520 * incremented, RFC2284 only makes the above-
521 * mentioned restriction.
523 reply->id = handler->eap_ds->response->id;
525 switch (reply->code) {
527 * The Id is a simple "ack" for success
535 * We've sent a response to their
536 * request, the Id is incremented.
542 DEBUG2(" rlm_eap: Underlying EAP-Type set EAP ID to %d",
547 * For Request & Response packets, set the EAP sub-type,
548 * if the EAP sub-module didn't already set it.
550 * This allows the TLS module to be "morphic", and means
551 * that the TTLS and PEAP modules can call it to do most
552 * of their dirty work.
554 if (((eap_ds->request->code == PW_EAP_REQUEST) ||
555 (eap_ds->request->code == PW_EAP_RESPONSE)) &&
556 (eap_ds->request->type.type == 0)) {
557 rad_assert(handler->eap_type >= PW_EAP_MD5);
558 rad_assert(handler->eap_type <= PW_EAP_MAX_TYPES);
560 eap_ds->request->type.type = handler->eap_type;
564 if (eap_wireformat(reply) == EAP_INVALID) {
565 return RLM_MODULE_INVALID;
567 eap_packet = (eap_packet_t *)reply->packet;
569 memcpy(&eap_len, &(eap_packet->length), sizeof(uint16_t));
570 len = eap_len = ntohs(eap_len);
571 ptr = (unsigned char *)eap_packet;
583 * create a value pair & append it to the request reply list
584 * This memory gets freed up when request is freed up
586 eap_msg = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
587 memcpy(eap_msg->strvalue, ptr, len);
588 eap_msg->length = len;
589 pairadd(&(request->reply->vps), eap_msg);
595 * EAP-Message is always associated with
596 * Message-Authenticator but not vice-versa.
598 * Don't add a Message-Authenticator if it's already
601 vp = pairfind(request->reply->vps, PW_MESSAGE_AUTHENTICATOR);
603 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
604 memset(vp->strvalue, 0, AUTH_VECTOR_LEN);
605 vp->length = AUTH_VECTOR_LEN;
606 pairadd(&(request->reply->vps), vp);
609 /* Set request reply code, but only if it's not already set. */
610 rcode = RLM_MODULE_OK;
611 if (!request->reply->code) switch(reply->code) {
612 case PW_EAP_RESPONSE:
613 request->reply->code = PW_AUTHENTICATION_ACK;
614 rcode = RLM_MODULE_HANDLED; /* leap weirdness */
617 request->reply->code = PW_AUTHENTICATION_ACK;
618 rcode = RLM_MODULE_OK;
621 request->reply->code = PW_AUTHENTICATION_REJECT;
622 rcode = RLM_MODULE_REJECT;
625 request->reply->code = PW_ACCESS_CHALLENGE;
626 rcode = RLM_MODULE_HANDLED;
629 /* Should never enter here */
630 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
631 request->reply->code = PW_AUTHENTICATION_REJECT;
632 rcode = RLM_MODULE_REJECT;
640 * Radius criteria, EAP-Message is invalid without Message-Authenticator
641 * For EAP_START, send Access-Challenge with EAP Identity request.
643 int eap_start(rlm_eap_t *inst, REQUEST *request)
650 eap_msg = pairfind(request->packet->vps, PW_EAP_MESSAGE);
651 if (eap_msg == NULL) {
652 DEBUG2(" rlm_eap: No EAP-Message, not doing EAP");
657 * Look for EAP-Type = None (FreeRADIUS specific attribute)
658 * this allows you to NOT do EAP for some users.
660 vp = pairfind(request->packet->vps, PW_EAP_TYPE);
661 if (vp && vp->lvalue == 0) {
662 DEBUG2(" rlm_eap: Found EAP-Message, but EAP-Type = None, so we're not doing EAP.");
667 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
669 * This is handled by rad_recv().
673 * We're allowed only a few codes. Request, Response,
674 * Success, or Failure.
676 if ((eap_msg->strvalue[0] == 0) ||
677 (eap_msg->strvalue[0] > PW_EAP_MAX_CODES)) {
678 DEBUG2(" rlm_eap: Unknown EAP packet");
680 DEBUG2(" rlm_eap: EAP packet type %s id %d length %d",
681 eap_codes[eap_msg->strvalue[0]],
682 eap_msg->strvalue[1],
687 * If we've been configured to proxy, do nothing.
689 * Note that we don't check if the realm is local.
690 * We figure that anyone bright enough to add
691 * Proxy-To-Realm is bright enough to NOT do so
692 * when it's a local realm.
694 if (pairfind(request->config_items, PW_PROXY_TO_REALM) != NULL) {
695 DEBUG2(" rlm_eap: Request is supposed to be proxied. Not doing EAP.");
700 * Not a start message. Don't start anything.
702 * Later EAP messages are longer than the 'start' message,
703 * so this function returns 'no start found', so that
704 * the rest of the EAP code can use the State attribute
705 * to match this EAP-Message to an ongoing conversation.
707 if (eap_msg->length != EAP_START) {
708 DEBUG2(" rlm_eap: No EAP Start, assuming it's an on-going EAP conversation");
711 * Add the 'EAP-Type' attribute to the request,
712 * if it's part of an EAP conversation, and the
713 * EAP sub-type is in the EAP packet.
715 * Store the EAP type in the request, so modules
716 * outside of EAP can check & use it.
718 if (((eap_msg->strvalue[0] == PW_EAP_REQUEST) ||
719 (eap_msg->strvalue[0] == PW_EAP_RESPONSE)) &&
720 (eap_msg->length >= (EAP_HEADER_LEN + 1))) {
722 * Create an EAP-Type of the type which
723 * was NAK'd, or of the type in the packet.
725 vp = paircreate(PW_EAP_TYPE, PW_TYPE_INTEGER);
727 vp->lvalue = eap_msg->strvalue[4];
728 pairadd(&(request->packet->vps), vp);
732 * We've been told to ignore unknown EAP
733 * types, AND it's an unknown type.
734 * Return "NOOP", which will cause the
735 * eap_authorize() to return NOOP.
737 * EAP-Identity, Notification, and NAK
738 * are all handled internally, so they
739 * never have handlers.
741 if ((eap_msg->strvalue[4] >= PW_EAP_MD5) &&
742 inst->ignore_unknown_eap_types &&
743 ((eap_msg->strvalue[4] == 0) ||
744 (eap_msg->strvalue[4] > PW_EAP_MAX_TYPES) ||
745 (inst->types[eap_msg->strvalue[4]] == NULL))) {
746 DEBUG2(" rlm_eap: Ignoring Unknown EAP type");
751 * They're NAKing the EAP type we wanted
752 * to use, and asking for one which we don't
755 * NAK is code + id + length1 + length + NAK
756 * + requested EAP type.
758 * We know at this point that we can't
759 * handle the request. We could either
760 * return an EAP-Fail here, but it's not
763 * By returning "noop", we can ensure
764 * that authorize() returns NOOP, and
765 * another module may choose to proxy
768 if ((eap_msg->strvalue[4] == PW_EAP_NAK) &&
769 (eap_msg->length >= (EAP_HEADER_LEN + 2)) &&
770 inst->ignore_unknown_eap_types &&
771 ((eap_msg->strvalue[5] == 0) ||
772 (eap_msg->strvalue[5] > PW_EAP_MAX_TYPES) ||
773 (inst->types[eap_msg->strvalue[5]] == NULL))) {
774 DEBUG2(" rlm_eap: Ignoring NAK with request for unknown EAP type");
777 } /* else it's not an EAP-Request or EAP-Response */
780 * No EAP-Start found.
785 DEBUG2(" rlm_eap: Got EAP_START message");
786 if ((eap_ds = eap_ds_alloc()) == NULL) {
787 DEBUG2(" rlm_eap: EAP Start failed in allocation");
792 * It's an EAP-Start packet. Tell them to stop wasting
793 * our time, and give us an EAP-Identity packet.
795 * Hmm... we should probably check the contents of the
796 * EAP-Start packet for something...
798 eap_ds->request->code = PW_EAP_REQUEST;
799 eap_ds->request->type.type = PW_EAP_IDENTITY;
802 * We don't have a handler, but eap_compose needs one,
803 * (for various reasons), so we fake it out here.
805 memset(&handler, 0, sizeof(handler));
806 handler.request = request;
807 handler.eap_ds = eap_ds;
809 eap_compose(&handler);
811 eap_ds_free(&eap_ds);
816 * compose EAP FAILURE packet in EAP-Message
818 void eap_fail(EAP_HANDLER *handler)
820 handler->eap_ds->request->code = PW_EAP_FAILURE;
821 eap_compose(handler);
825 * compose EAP SUCCESS packet in EAP-Message
827 void eap_success(EAP_HANDLER *handler)
829 handler->eap_ds->request->code = PW_EAP_SUCCESS;
830 eap_compose(handler);
834 * Basic EAP packet verfications & validations
836 static int eap_validation(eap_packet_t *eap_packet)
840 memcpy(&len, eap_packet->length, sizeof(uint16_t));
844 * High level EAP packet checks
846 if ((len <= EAP_HEADER_LEN) ||
847 ((eap_packet->code != PW_EAP_RESPONSE) &&
848 (eap_packet->code != PW_EAP_REQUEST)) ||
849 (eap_packet->data[0] <= 0) ||
850 (eap_packet->data[0] > PW_EAP_MAX_TYPES)) {
852 radlog(L_AUTH, "rlm_eap: Incorrect EAP Message, "
853 "Ignoring the packet");
857 /* we don't expect notification, but we send it */
858 if (eap_packet->data[0] == PW_EAP_NOTIFICATION) {
859 radlog(L_AUTH, "rlm_eap: Got NOTIFICATION, "
860 "Ignoring the packet");
869 * Get the user Identity only from EAP-Identity packets
871 static char *eap_identity(eap_packet_t *eap_packet)
877 if ((eap_packet == NULL) ||
878 (eap_packet->code != PW_EAP_RESPONSE) ||
879 (eap_packet->data[0] != PW_EAP_IDENTITY)) {
883 memcpy(&len, eap_packet->length, sizeof(uint16_t));
886 if ((len <= 5) || (eap_packet->data[1] == 0x00)) {
887 radlog(L_ERR, "rlm_eap: UserIdentity Unknown ");
892 identity = (char *)malloc(size + 1);
893 if (identity == NULL) {
894 radlog(L_ERR, "rlm_eap: out of memory");
897 memcpy(identity, &eap_packet->data[1], size);
898 identity[size] = '\0';
905 * Create our Request-Response data structure with the eap packet
907 static EAP_DS *eap_buildds(eap_packet_t **eap_packet_p)
909 EAP_DS *eap_ds = NULL;
910 eap_packet_t *eap_packet = *eap_packet_p;
914 if ((eap_ds = eap_ds_alloc()) == NULL) {
918 eap_ds->response->packet = (unsigned char *)eap_packet;
919 eap_ds->response->code = eap_packet->code;
920 eap_ds->response->id = eap_packet->id;
921 eap_ds->response->type.type = eap_packet->data[0];
923 memcpy(&len, eap_packet->length, sizeof(uint16_t));
925 eap_ds->response->length = len;
928 * We've eaten the eap packet into the eap_ds.
930 *eap_packet_p = NULL;
933 * First 5 bytes in eap, are code + id + length(2) + type.
935 * The rest is type-specific data. We skip type while
936 * getting typedata from data.
938 typelen = len - 5/*code + id + length + type */;
941 * Since the packet contains the complete
942 * eap_packet, typedata will be a ptr in packet
945 eap_ds->response->type.data = eap_ds->response->packet + 5/*code+id+length+type*/;
946 eap_ds->response->type.length = typelen;
948 eap_ds->response->type.length = 0;
949 eap_ds->response->type.data = NULL;
957 * If identity response then create a fresh handler & fill the identity
958 * else handler MUST be in our list, get that.
959 * This handler creation cannot fail
961 * username contains REQUEST->username which might have been stripped.
962 * identity contains the one sent in EAP-Identity response
964 EAP_HANDLER *eap_handler(rlm_eap_t *inst, eap_packet_t **eap_packet_p,
967 EAP_HANDLER *handler = NULL;
968 eap_packet_t *eap_packet = *eap_packet_p;
971 * Ensure it's a valid EAP-Request, or EAP-Response.
973 if (eap_validation(eap_packet) == EAP_INVALID) {
975 *eap_packet_p = NULL;
980 * EAP_HANDLER MUST be found in the list if it is not
981 * EAP-Identity response
983 if (eap_packet->data[0] != PW_EAP_IDENTITY) {
984 handler = eaplist_find(inst, request, eap_packet);
985 if (handler == NULL) {
986 /* Either send EAP_Identity or EAP-Fail */
987 radlog(L_ERR, "rlm_eap: Either EAP-request timed out OR"
988 " EAP-response to an unknown EAP-request");
990 *eap_packet_p = NULL;
995 * Even more paranoia. Without this, some weird
996 * clients could do crazy things.
998 * It's ok to send EAP sub-type NAK in response
999 * to a request for a particular type, but it's NOT
1000 * OK to blindly return data for another type.
1002 if ((eap_packet->data[0] != PW_EAP_NAK) &&
1003 (eap_packet->data[0] != handler->eap_type)) {
1004 radlog(L_ERR, "rlm_eap: Response appears to match, but EAP type is wrong.");
1005 free(*eap_packet_p);
1006 *eap_packet_p = NULL;
1010 handler = eap_handler_alloc();
1011 if (handler == NULL) {
1012 radlog(L_ERR, "rlm_eap: out of memory");
1013 free(*eap_packet_p);
1014 *eap_packet_p = NULL;
1019 * All fields in the handler are set to zero.
1022 handler->identity = eap_identity(eap_packet);
1023 if (handler->identity == NULL) {
1024 radlog(L_ERR, "rlm_eap: Identity Unknown, authentication failed");
1025 free(*eap_packet_p);
1026 *eap_packet_p = NULL;
1027 eap_handler_free(&handler);
1032 handler->eap_ds = eap_buildds(eap_packet_p);
1033 if (handler->eap_ds == NULL) {
1034 free(*eap_packet_p);
1035 *eap_packet_p = NULL;
1036 eap_handler_free(&handler);
1040 handler->timestamp = time(NULL);
1041 handler->request = request; /* LEAP needs this */