eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

eap.c (24809B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include <string.h>
     31 #include <ell/ell.h>
     32 
     33 #include "src/missing.h"
     34 #include "src/module.h"
     35 #include "src/util.h"
     36 #include "src/eap.h"
     37 #include "src/eap-private.h"
     38 #include "src/iwd.h"
     39 
     40 static uint32_t default_mtu;
     41 static struct l_queue *eap_methods;
     42 
     43 static void dump_eap(const char *str, void *user_data)
     44 {
     45 	const char *prefix = user_data;
     46 
     47 	l_info("%s%s\n", prefix, str);
     48 }
     49 
     50 struct eap_state {
     51 	eap_tx_packet_func_t tx_packet;
     52 	eap_key_material_func_t set_key_material;
     53 	eap_complete_func_t complete;
     54 	eap_event_func_t event_func;
     55 	void *user_data;
     56 	size_t mtu;
     57 
     58 	struct eap_method *method;
     59 	char *identity;
     60 	char *identity_setting;
     61 	bool authenticator;
     62 	char *peer_id;
     63 
     64 	int last_id;
     65 	void *method_state;
     66 	bool method_success;
     67 	struct l_timeout *complete_timeout;
     68 	unsigned int identity_req_count;
     69 	unsigned int method_req_count;
     70 
     71 	bool discard_success_and_failure:1;
     72 };
     73 
     74 struct eap_state *eap_new(eap_tx_packet_func_t tx_packet,
     75 			eap_complete_func_t complete, void *user_data)
     76 {
     77 	struct eap_state *eap;
     78 
     79 	eap = l_new(struct eap_state, 1);
     80 
     81 	eap->last_id = -1;
     82 	eap->mtu = default_mtu;
     83 
     84 	eap->tx_packet = tx_packet;
     85 	eap->complete = complete;
     86 	eap->user_data = user_data;
     87 
     88 	return eap;
     89 }
     90 
     91 /*
     92  * Setting a non-NULL set_key_material callback for this EAP instance will
     93  * disable the legacy methods that don't generate key material, such
     94  * as EAP-MD5.
     95  */
     96 void eap_set_key_material_func(struct eap_state *eap,
     97 				eap_key_material_func_t func)
     98 {
     99 	eap->set_key_material = func;
    100 }
    101 
    102 void eap_set_event_func(struct eap_state *eap, eap_event_func_t func)
    103 {
    104 	eap->event_func = func;
    105 }
    106 
    107 bool eap_reset(struct eap_state *eap)
    108 {
    109 	if (eap->method_state && eap->method->reset_state) {
    110 		if (!eap->method->reset_state(eap))
    111 			return false;
    112 	}
    113 
    114 	eap->method_success = false;
    115 	l_timeout_remove(eap->complete_timeout);
    116 	eap->complete_timeout = NULL;
    117 
    118 	return true;
    119 }
    120 
    121 static void eap_free_common(struct eap_state *eap)
    122 {
    123 	if (eap->method_state && eap->method->free)
    124 		eap->method->free(eap);
    125 
    126 	eap->method = NULL;
    127 
    128 	if (eap->identity) {
    129 		l_free(eap->identity);
    130 		eap->identity = NULL;
    131 		l_free(eap->identity_setting);
    132 		eap->identity_setting = NULL;
    133 	}
    134 }
    135 
    136 void eap_free(struct eap_state *eap)
    137 {
    138 	/*
    139 	 * In supplicant mode we get here whether the authenticator has
    140 	 * sent an EAP-Failure or interrupted the connection at a higher
    141 	 * layer, or even when the negotiation stalled and we interrupted
    142 	 * it, so use this code path to check if it is the Identity value
    143 	 * that the authenticator may have a problem with.  We don't know
    144 	 * whether we're the phase 1 or phase 2 EAP state machine
    145 	 * (eap->set_key_material is a hint but doesn't work with EAP-PEAP
    146 	 * phase2) so print the setting name.
    147 	 */
    148 	if (!eap->authenticator && !eap->method_success) {
    149 		if (eap->identity_req_count && !eap->method_req_count)
    150 			l_info("EAP negotiation stopped after the Identity "
    151 				"exchange, this can happen when the %s value "
    152 				"is not what the authenticator expects",
    153 				eap->identity_setting ?: "identity");
    154 	}
    155 
    156 	eap_free_common(eap);
    157 	l_timeout_remove(eap->complete_timeout);
    158 	eap_set_peer_id(eap, NULL);
    159 
    160 	l_free(eap);
    161 }
    162 
    163 /* Note: callers must check for a minimum value */
    164 void eap_set_mtu(struct eap_state *eap, size_t mtu)
    165 {
    166 	eap->mtu = mtu;
    167 }
    168 
    169 size_t eap_get_mtu(struct eap_state *eap)
    170 {
    171 	return eap->mtu;
    172 }
    173 
    174 const char *eap_get_identity(struct eap_state *eap)
    175 {
    176 	return eap->identity;
    177 }
    178 
    179 static void eap_send_packet(struct eap_state *eap, enum eap_code code,
    180 				uint8_t id, uint8_t *buf, size_t len)
    181 {
    182 	buf[0] = code;
    183 	buf[1] = id;
    184 	l_put_be16(len, &buf[2]);
    185 
    186 	eap->tx_packet(buf, len, eap->user_data);
    187 }
    188 
    189 static void eap_send_response(struct eap_state *eap, enum eap_type type,
    190 				uint8_t *buf, size_t len)
    191 {
    192 	buf[4] = type;
    193 
    194 	if (type == EAP_TYPE_EXPANDED) {
    195 		memcpy(buf + 5, eap->method->vendor_id, 3);
    196 		l_put_be32(eap->method->vendor_type, buf + 8);
    197 	}
    198 
    199 	eap_send_packet(eap, EAP_CODE_RESPONSE, eap->last_id, buf, len);
    200 }
    201 
    202 /**
    203  * eap_method_respond:
    204  * @eap: EAP state
    205  * @buf: Buffer to send
    206  * @len: Size of the buffer
    207  *
    208  * Sends out a response to a received request.  This method first fills
    209  * the EAP header in the buffer based on the method's EAP type being
    210  * sent.
    211  *
    212  * If the method uses an expanded type , then the Vendor-Id and
    213  * Vendor-Type fields are filled in automatically.
    214  *
    215  * The buffer passed in MUST be at least 12 bytes long if method uses
    216  * an expanded type and at least 5 bytes for other cases.
    217  **/
    218 void eap_method_respond(struct eap_state *eap, uint8_t *buf, size_t len)
    219 {
    220 	eap_send_response(eap, eap->method->request_type, buf, len);
    221 }
    222 
    223 void eap_method_new_request(struct eap_state *eap, uint8_t *buf, size_t len)
    224 {
    225 	buf[4] = eap->method->request_type;
    226 
    227 	if (eap->method->request_type == EAP_TYPE_EXPANDED) {
    228 		memcpy(buf + 5, eap->method->vendor_id, 3);
    229 		l_put_be32(eap->method->vendor_type, buf + 8);
    230 	}
    231 
    232 	/* TODO: Save copy for retransmissions, set timer, increment counter */
    233 	eap_send_packet(eap, EAP_CODE_REQUEST, ++eap->last_id, buf, len);
    234 }
    235 
    236 static void eap_complete_timeout(struct l_timeout *timeout, void *user_data)
    237 {
    238 	struct eap_state *eap = user_data;
    239 
    240 	eap->complete_timeout = NULL;
    241 
    242 	eap->complete(eap->method_success ? EAP_RESULT_SUCCESS :
    243 			EAP_RESULT_TIMEOUT, eap->user_data);
    244 }
    245 
    246 void eap_start_complete_timeout(struct eap_state *eap)
    247 {
    248 	if (eap->complete_timeout)
    249 		l_timeout_remove(eap->complete_timeout);
    250 
    251 	eap->complete_timeout = l_timeout_create(5, eap_complete_timeout,
    252 							eap, NULL);
    253 }
    254 
    255 static void eap_send_identity_response(struct eap_state *eap, char *identity)
    256 {
    257 	int len = identity ? strlen(identity) : 0;
    258 	uint8_t buf[5 + len];
    259 
    260 	if (!identity)
    261 		identity = "";
    262 
    263 	memcpy(buf + 5, identity, len);
    264 
    265 	eap_send_response(eap, EAP_TYPE_IDENTITY, buf, len + 5);
    266 }
    267 
    268 /* To be used in authenticator mode to trigger our first request */
    269 void eap_start(struct eap_state *eap)
    270 {
    271 	uint8_t buf[5];
    272 
    273 	L_WARN_ON(!eap->method || !eap->authenticator);
    274 
    275 	/*
    276 	 * Until we've received the Identity response we can resend the
    277 	 * Identity request with a constant ID on EAPoL-Start.
    278 	 */
    279 	if (eap->identity)
    280 		return;
    281 
    282 	eap->last_id = 1;
    283 	buf[4] = EAP_TYPE_IDENTITY;
    284 	eap_send_packet(eap, EAP_CODE_REQUEST, eap->last_id, buf, 5);
    285 }
    286 
    287 void __eap_handle_request(struct eap_state *eap, uint16_t id,
    288 				const uint8_t *pkt, size_t len)
    289 {
    290 	enum eap_type type;
    291 	uint8_t buf[10];
    292 	int buf_len;
    293 	bool retransmit;
    294 
    295 	if (len < 1)
    296 		/* Invalid packets to be ignored */
    297 		return;
    298 
    299 	type = pkt[0];
    300 	if (type >= __EAP_TYPE_MIN_METHOD && !eap->method) {
    301 		l_warn("EAP server tried method %i while client had no method "
    302 			"configured", type);
    303 
    304 		goto unsupported_method;
    305 	}
    306 
    307 	retransmit = id == eap->last_id ? true : false;
    308 	eap->last_id = id;
    309 
    310 	if (type >= __EAP_TYPE_MIN_METHOD) {
    311 		void (*op)(struct eap_state *eap,
    312 					const uint8_t *pkt, size_t len);
    313 
    314 		eap->method_req_count++;
    315 
    316 		if (type != eap->method->request_type) {
    317 			l_warn("EAP server tried method %i while client was "
    318 					"configured for method %i",
    319 					type, eap->method->request_type);
    320 
    321 			goto unsupported_method;
    322 		}
    323 
    324 		op = retransmit && eap->method->handle_retransmit ?
    325 						eap->method->handle_retransmit :
    326 						eap->method->handle_request;
    327 
    328 		if (type != EAP_TYPE_EXPANDED) {
    329 			op(eap, pkt + 1, len - 1);
    330 			return;
    331 		}
    332 
    333 		/*
    334 		 * TODO: Handle Expanded Nak if our vendor-id / vendor-types
    335 		 * don't match
    336 		 */
    337 		if (len < 8)
    338 			return;
    339 
    340 		op(eap, pkt + 8, len - 8);
    341 		return;
    342 	}
    343 
    344 	switch (type) {
    345 	case EAP_TYPE_IDENTITY:
    346 		eap->identity_req_count++;
    347 
    348 		if (len >= 2)
    349 			l_debug("Optional EAP server identity prompt: \"%.*s\"",
    350 					(int) len - 1, pkt + 1);
    351 
    352 		eap_send_identity_response(eap, eap->identity);
    353 
    354 		return;
    355 
    356 	case EAP_TYPE_NOTIFICATION:
    357 		if (len < 2)
    358 			/* Invalid packets to be ignored */
    359 			return;
    360 
    361 		l_warn("EAP notification: \"%.*s\"", (int) len - 1, pkt + 1);
    362 
    363 		eap_send_response(eap, EAP_TYPE_NOTIFICATION, buf, 5);
    364 
    365 		return;
    366 
    367 	default:
    368 	unsupported_method:
    369 		if (!eap->method) {
    370 			l_info("Received an unhandled EAP packet:");
    371 			l_util_hexdump(true, pkt, len, dump_eap, "[EAP] ");
    372 		}
    373 
    374 		/* Send a legacy NAK response */
    375 		buf_len = 5;
    376 
    377 		/*
    378 		 * RFC3748, Section 5.3.1: "A peer supporting Expanded Types
    379 		 * that receives a Request for an unacceptable authentication
    380 		 * Type (4-253,255) MAY include the value 254 in the Nak
    381 		 * Response (Type 3) to indicate the desire for an Expanded
    382 		 * authentication Type."
    383 		 */
    384 		buf[buf_len++] = eap->method ? eap->method->request_type : 0;
    385 
    386 		eap_send_response(eap, EAP_TYPE_NAK, buf, buf_len);
    387 		return;
    388 	}
    389 }
    390 
    391 static const char *eap_type_to_str(enum eap_type type, uint32_t vendor_id,
    392 					uint32_t vendor_type)
    393 {
    394 	static char buf[100];
    395 
    396 	/*
    397 	 * Show vendor ID if type is expanded unless vendor_id and
    398 	 * vendor_type are 0 which means the peer is requesting an
    399 	 * expanded type through a Legacy Nak.  A Legacy Nak won't let
    400 	 * the peer specify the which expanded type it wants.
    401 	 *
    402 	 * TODO: Could also use names for known methods and/or vendors.
    403 	 */
    404 	if (type == EAP_TYPE_EXPANDED && vendor_id == 0 && vendor_type != 0)
    405 		type = vendor_type;
    406 
    407 	if (type != EAP_TYPE_EXPANDED || (vendor_id == 0 && vendor_type == 0)) {
    408 		snprintf(buf, sizeof(buf), "type(%i)", type);
    409 		return buf;
    410 	}
    411 
    412 	snprintf(buf, sizeof(buf), "vendor(%02x, %02x, %02x), type(%i)",
    413 			(vendor_id >> 16) & 255, (vendor_id >> 8) & 255,
    414 			vendor_id & 255, vendor_type);
    415 	return buf;
    416 }
    417 
    418 #define IS_EXPANDED_RESPONSE(id, t) \
    419 	(type == EAP_TYPE_EXPANDED && vendor_id == (id) && vendor_type == (t))
    420 
    421 #define RESPONSE_IS(t) \
    422 	(type == (t) || IS_EXPANDED_RESPONSE(0, (t)))
    423 
    424 static void eap_handle_response(struct eap_state *eap, const uint8_t *pkt,
    425 				size_t len)
    426 {
    427 	enum eap_type type;
    428 	uint32_t vendor_id = 0;
    429 	uint32_t vendor_type = 0;
    430 	enum eap_type our_type = eap->method->request_type;
    431 	uint32_t our_vendor_id = (eap->method->vendor_id[0] << 16) |
    432 				(eap->method->vendor_id[1] << 8) |
    433 				eap->method->vendor_id[2];
    434 	uint32_t our_vendor_type = eap->method->vendor_type;
    435 
    436 	if (len < 1)
    437 		/* Invalid packets to be ignored */
    438 		return;
    439 
    440 	type = *pkt++;
    441 	len--;
    442 
    443 	if (type == EAP_TYPE_EXPANDED) {
    444 		if (len < 7)
    445 			return;
    446 
    447 		vendor_id = (pkt[0] << 16) | (pkt[1] << 8) | pkt[2];
    448 		vendor_type = l_get_be32(pkt + 3);
    449 		pkt += 7;
    450 		len -= 7;
    451 
    452 		if (vendor_id == 0 && vendor_type == EAP_TYPE_NAK &&
    453 				our_type != EAP_TYPE_EXPANDED)
    454 			/*
    455 			 * RFC3748 5.3.2: "[The Expanded Nak Type] MUST
    456 			 * be sent only in reply to a Request of Type 254.
    457 			 */
    458 			return;
    459 	}
    460 
    461 	if (RESPONSE_IS(EAP_TYPE_NAK)) {
    462 		l_debug("EAP peer not configured for method: %s",
    463 			eap_type_to_str(our_type, our_vendor_id,
    464 							our_vendor_type));
    465 
    466 		if ((type == EAP_TYPE_NAK && len == 1 && pkt[0] == 0) ||
    467 				(type != EAP_TYPE_NAK && len == 8 &&
    468 				 l_memeqzero(pkt, 8)))
    469 			l_debug("EAP peer proposed no alternative methods");
    470 		else if (type == EAP_TYPE_NAK)
    471 			while (len) {
    472 				l_debug("EAP peer proposed method: %s",
    473 					eap_type_to_str(*pkt++, 0, 0));
    474 				len--;
    475 			}
    476 		else
    477 			while (len >= 8) {
    478 				uint32_t v_id = (pkt[1] << 16) |
    479 						(pkt[2] << 8) |
    480 						pkt[3];
    481 
    482 				l_debug("EAP peer proposed method: %s",
    483 					eap_type_to_str(pkt[0], v_id,
    484 							l_get_be32(pkt + 4)));
    485 				pkt += 8;
    486 				len -= 8;
    487 			}
    488 
    489 		goto unsupported_method;
    490 	}
    491 
    492 	/*
    493 	 * If we don't have peer's identity yet it means we've only sent the
    494 	 * Identity Request so far so we expect an Identity Response and
    495 	 * nothing else.  Otherwise we only accept Response types matching
    496 	 * the method type and we forward responses directly to the method.
    497 	 */
    498 
    499 	if (!eap->identity) {
    500 		if (!RESPONSE_IS(EAP_TYPE_IDENTITY))
    501 			goto unsupported_method;
    502 
    503 		/*
    504 		 * RFC3748 Section 5.1: "The Identity Response field MUST NOT be
    505 		 * null terminated."
    506 		 * Only the Request can have data after a NUL char.
    507 		 */
    508 		if (len > 253 && memchr(pkt, '\0', len))
    509 			goto error;
    510 
    511 		eap->identity = l_strndup((char *) pkt, len);
    512 		if (eap->method->validate_identity &&
    513 				!eap->method->validate_identity(eap,
    514 								eap->identity))
    515 			goto error;
    516 
    517 		return;
    518 	}
    519 
    520 	/*
    521 	 * RFC3748 5.7: "An implementation that supports the Expanded
    522 	 * attribute MUST treat EAP Types that are less than 256 equivalently,
    523 	 * whether they appear as a single octet or as the 32-bit Vendor-Type
    524 	 * within an Expanded Type where Vendor-Id is 0."
    525 	 * (with the exception of the Nak)
    526 	 */
    527 	if (our_type != EAP_TYPE_EXPANDED) {
    528 		if (RESPONSE_IS(our_type))
    529 			goto handle_response;
    530 _Pragma("GCC diagnostic push")
    531 _Pragma("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
    532 	} else if (IS_EXPANDED_RESPONSE(our_vendor_id, our_vendor_type))
    533 _Pragma("GCC diagnostic pop")
    534 		goto handle_response;
    535 
    536 error:
    537 unsupported_method:
    538 	eap_method_error(eap);
    539 	return;
    540 
    541 handle_response:
    542 	eap->method->handle_response(eap, pkt, len);
    543 }
    544 
    545 void eap_rx_packet(struct eap_state *eap, const uint8_t *pkt, size_t len)
    546 {
    547 	uint8_t code, id;
    548 	uint16_t eap_len;
    549 
    550 	if (len < 4 || l_get_be16(&pkt[2]) < 4 || len < l_get_be16(&pkt[2]))
    551 		/* Invalid packets to be silently discarded */
    552 		return;
    553 
    554 	code = pkt[0];
    555 	id = pkt[1];
    556 	eap_len = l_get_be16(&pkt[2]);
    557 
    558 	switch ((enum eap_code) code) {
    559 	case EAP_CODE_REQUEST:
    560 		if (eap->authenticator)
    561 			goto invalid;
    562 
    563 		__eap_handle_request(eap, id, pkt + 4, eap_len - 4);
    564 		return;
    565 
    566 	case EAP_CODE_RESPONSE:
    567 		if (!eap->authenticator || id != eap->last_id || !eap->method)
    568 			goto invalid;
    569 
    570 		eap_handle_response(eap, pkt + 4, eap_len - 4);
    571 		return;
    572 
    573 	case EAP_CODE_FAILURE:
    574 	case EAP_CODE_SUCCESS:
    575 		if (eap->authenticator)
    576 			goto invalid;
    577 
    578 		if (eap->discard_success_and_failure)
    579 			return;
    580 
    581 		l_timeout_remove(eap->complete_timeout);
    582 		eap->complete_timeout = NULL;
    583 
    584 		/* RFC3748, Section 4.2
    585 		 *
    586 		 * The Identifier field of the Success and Failure packets
    587 		 * MUST match the Identifier field of the Response packet that
    588 		 * it is sent in response to. However, many currently deployed
    589 		 * implementations ignore this rule and increment Identity for
    590 		 * the Success and Failure packets. In order to support
    591 		 * interoperability with these products we validate id against
    592 		 * eap->last_id and its incremented value.
    593 		 *
    594 		 * Note: Since last_id is stored as an int and id value is a
    595 		 * byte, we need to support overflow properly.
    596 		 */
    597 		if (id != eap->last_id && id != (eap->last_id + 1) % 256)
    598 			return;
    599 
    600 		if (eap_len != 4)
    601 			goto invalid;
    602 
    603 		if (code == EAP_CODE_SUCCESS && !eap->method_success)
    604 			/* "Canned" success packets to be discarded */
    605 			return;
    606 
    607 		if (code == EAP_CODE_FAILURE && eap->method_success)
    608 			/*
    609 			 * "On the peer, after success result indications have
    610 			 * been exchanged by both sides, a Failure packet MUST
    611 			 * be silently discarded."
    612 			 *
    613 			 * "Where the peer authenticates successfully to the
    614 			 * authenticator, but the authenticator does not send
    615 			 * a result indication, the authenticator MAY deny
    616 			 * access by sending a Failure packet where the peer
    617 			 * is not currently authorized for network access."
    618 			 * -- eap->method_success implies we've received
    619 			 * a full result indication.
    620 			 */
    621 			return;
    622 
    623 		eap->complete(code == EAP_CODE_SUCCESS ? EAP_RESULT_SUCCESS :
    624 				EAP_RESULT_FAIL, eap->user_data);
    625 		return;
    626 
    627 	invalid:
    628 	default:
    629 		/* Invalid packets to be silently discarded */
    630 		return;
    631 	}
    632 }
    633 
    634 bool eap_secret_info_match(const void *a, const void *b)
    635 {
    636 	const struct eap_secret_info *s = a;
    637 
    638 	return !strcmp(s->id, b);
    639 }
    640 
    641 void eap_append_secret(struct l_queue **out_missing, enum eap_secret_type type,
    642 			const char *id, const char *id2, const char *parameter,
    643 			enum eap_secret_cache_policy cache_policy)
    644 {
    645 	struct eap_secret_info *info;
    646 
    647 	if (!*out_missing)
    648 		*out_missing = l_queue_new();
    649 
    650 	info = l_new(struct eap_secret_info, 1);
    651 	info->id = l_strdup(id);
    652 	info->id2 = l_strdup(id2);
    653 	info->type = type;
    654 	info->parameter = l_strdup(parameter);
    655 	info->cache_policy = cache_policy;
    656 	l_queue_push_tail(*out_missing, info);
    657 }
    658 
    659 void eap_secret_info_free(void *data)
    660 {
    661 	struct eap_secret_info *info = data;
    662 
    663 	if (!info)
    664 		return;
    665 
    666 	if (info->value) {
    667 		size_t value_len = strlen(info->value) + 1;
    668 
    669 		if (info->type == EAP_SECRET_REMOTE_USER_PASSWORD)
    670 			value_len += strlen(info->value + value_len);
    671 
    672 		explicit_bzero(info->value, value_len);
    673 		l_free(info->value);
    674 	}
    675 
    676 	if (info->parameter) {
    677 		explicit_bzero(info->parameter, strlen(info->parameter));
    678 		l_free(info->parameter);
    679 	}
    680 
    681 	l_free(info->id);
    682 	l_free(info->id2);
    683 	l_free(info);
    684 }
    685 
    686 int __eap_check_settings(struct l_settings *settings, struct l_queue *secrets,
    687 				const char *prefix, bool set_key_material,
    688 				struct l_queue **missing)
    689 {
    690 	char setting[64];
    691 	const char *method_name;
    692 	const struct l_queue_entry *entry;
    693 	struct eap_method *method;
    694 	int ret = 0;
    695 
    696 	snprintf(setting, sizeof(setting), "%sMethod", prefix);
    697 	method_name = l_settings_get_value(settings, "Security", setting);
    698 
    699 	if (!method_name) {
    700 		l_error("Property %s missing", setting);
    701 		return -ENOENT;
    702 	}
    703 
    704 	for (entry = l_queue_get_entries(eap_methods); entry;
    705 					entry = entry->next) {
    706 		method = entry->data;
    707 
    708 		if (!strcasecmp(method_name, method->name))
    709 			break;
    710 	}
    711 
    712 	if (!entry) {
    713 		l_error("EAP method \"%s\" unsupported", method_name);
    714 		return -ENOTSUP;
    715 	}
    716 
    717 	/* Check if selected method is suitable for 802.1x */
    718 	if (set_key_material && !method->exports_msk) {
    719 		l_error("EAP method \"%s\" doesn't export key material",
    720 				method_name);
    721 		return -ENOTSUP;
    722 	}
    723 
    724 	if (method->check_settings) {
    725 		ret = method->check_settings(settings, secrets,
    726 						prefix, missing);
    727 
    728 		if (ret < 0)
    729 			return ret;
    730 	}
    731 
    732 	/*
    733 	 * Individual methods are responsible for ensuring, inside their
    734 	 * check_settings(), that they have enough data to return the
    735 	 * identity after load_settings() if it is required.
    736 	 */
    737 
    738 	return 0;
    739 }
    740 
    741 int eap_check_settings(struct l_settings *settings, struct l_queue *secrets,
    742 			const char *prefix, bool set_key_material,
    743 			struct l_queue **out_missing)
    744 {
    745 	struct l_queue *missing = NULL;
    746 	int ret = __eap_check_settings(settings, secrets, prefix,
    747 					set_key_material, &missing);
    748 
    749 	if (ret < 0) {
    750 		l_queue_destroy(missing, eap_secret_info_free);
    751 		return ret;
    752 	}
    753 
    754 	if (missing && l_queue_isempty(missing)) {
    755 		l_queue_destroy(missing, NULL);
    756 		missing = NULL;
    757 	}
    758 
    759 	*out_missing = missing;
    760 	return 0;
    761 }
    762 
    763 bool eap_load_settings(struct eap_state *eap, struct l_settings *settings,
    764 			const char *prefix)
    765 {
    766 	char setting[64];
    767 	const char *method_name;
    768 	const struct l_queue_entry *entry;
    769 	struct eap_method *method;
    770 
    771 	snprintf(setting, sizeof(setting), "%sMethod", prefix);
    772 	method_name = l_settings_get_value(settings, "Security", setting);
    773 
    774 	if (!method_name)
    775 		return false;
    776 
    777 	for (entry = l_queue_get_entries(eap_methods); entry;
    778 					entry = entry->next) {
    779 		method = entry->data;
    780 
    781 		if (!strcasecmp(method_name, method->name)) {
    782 			eap->method = method;
    783 
    784 			break;
    785 		}
    786 	}
    787 
    788 	if (!eap->method)
    789 		return false;
    790 
    791 	/* Check if selected method is suitable for 802.1x */
    792 	if (eap->set_key_material && !eap->method->exports_msk)
    793 		goto err;
    794 
    795 	if (eap->method->load_settings)
    796 		if (!eap->method->load_settings(eap, settings, prefix))
    797 			goto err;
    798 
    799 	eap->authenticator = eap->method->handle_request == NULL;
    800 
    801 	if (eap->authenticator)
    802 		return true;
    803 
    804 	/* get identity from settings or from EAP method */
    805 	if (!eap->method->get_identity) {
    806 		snprintf(setting, sizeof(setting), "%sIdentity", prefix);
    807 		eap->identity = l_settings_get_string(settings,
    808 							"Security", setting);
    809 		eap->identity_setting = l_strdup(setting);
    810 	} else {
    811 		eap->identity = l_strdup(eap->method->get_identity(eap));
    812 	}
    813 
    814 	/*
    815 	 * RFC 4282 Section 2.2 - NAI Length Considerations
    816 	 *
    817 	 * Devices handling NAIs MUST support an NAI length of at least 72
    818 	 * octets. Support for an NAI length of 253 octets is RECOMMENDED.
    819 	 * ...
    820 	 * RADIUS is unable to support NAI lengths beyond 253 octets
    821 	 *
    822 	 * We also need to fail if the identity is too large for the set MTU
    823 	 * size minus 5 (header).
    824 	 */
    825 	if (eap->identity) {
    826 		size_t id_len = strlen(eap->identity);
    827 
    828 		if (id_len > 253 || id_len > eap->mtu - 5) {
    829 			l_error("Identity is too long");
    830 			goto err;
    831 		}
    832 	}
    833 
    834 	return true;
    835 
    836 err:
    837 	eap_free_common(eap);
    838 
    839 	return false;
    840 }
    841 
    842 void eap_set_peer_id(struct eap_state *eap, const char *id)
    843 {
    844 	l_free(eap->peer_id);
    845 	eap->peer_id = l_strdup(id);
    846 }
    847 
    848 const char *eap_get_peer_id(struct eap_state *eap)
    849 {
    850 	return eap->peer_id;
    851 }
    852 
    853 void eap_set_data(struct eap_state *eap, void *data)
    854 {
    855 	eap->method_state = data;
    856 }
    857 
    858 void *eap_get_data(struct eap_state *eap)
    859 {
    860 	return eap->method_state;
    861 }
    862 
    863 enum eap_type eap_get_method_type(struct eap_state *eap)
    864 {
    865 	return eap->method->request_type;
    866 }
    867 
    868 const char *eap_get_method_name(struct eap_state *eap)
    869 {
    870 	return eap->method->name;
    871 }
    872 
    873 void eap_set_key_material(struct eap_state *eap,
    874 				const uint8_t *msk_data, size_t msk_len,
    875 				const uint8_t *emsk_data, size_t emsk_len,
    876 				const uint8_t *iv, size_t iv_len,
    877 				const uint8_t *session_id, size_t session_len)
    878 {
    879 	if (!eap->set_key_material)
    880 		return;
    881 
    882 	eap->set_key_material(msk_data, msk_len, emsk_data, emsk_len,
    883 				iv, iv_len, session_id, session_len,
    884 				eap->user_data);
    885 }
    886 
    887 void eap_method_event(struct eap_state *eap, unsigned int id, const void *data)
    888 {
    889 	if (!eap->event_func)
    890 		return;
    891 
    892 	eap->event_func(id, data, eap->user_data);
    893 }
    894 
    895 bool eap_method_is_success(struct eap_state *eap)
    896 {
    897 	return eap->method_success;
    898 }
    899 
    900 void eap_method_success(struct eap_state *eap)
    901 {
    902 	eap->method_success = true;
    903 
    904 	if (eap->authenticator) {
    905 		uint8_t buf[4];
    906 
    907 		/* ID not incremented for Success or Failure (RFC3748 4.2) */
    908 		eap_send_packet(eap, EAP_CODE_FAILURE, eap->last_id, buf, 4);
    909 		eap->complete(EAP_RESULT_SUCCESS, eap->user_data);
    910 	}
    911 }
    912 
    913 void eap_discard_success_and_failure(struct eap_state *eap, bool discard)
    914 {
    915 	eap->discard_success_and_failure = discard;
    916 }
    917 
    918 void eap_method_error(struct eap_state *eap)
    919 {
    920 	if (eap->authenticator) {
    921 		uint8_t buf[4];
    922 
    923 		/* ID not incremented for Success or Failure (RFC3748 4.2) */
    924 		eap_send_packet(eap, EAP_CODE_FAILURE, eap->last_id, buf, 4);
    925 	}
    926 
    927 	/*
    928 	 * It looks like neither EAP nor EAP-TLS specify the error handling
    929 	 * behavior.
    930 	 */
    931 	eap->complete(EAP_RESULT_FAIL, eap->user_data);
    932 }
    933 
    934 void eap_save_last_id(struct eap_state *eap, uint8_t *last_id)
    935 {
    936 	*last_id = eap->last_id;
    937 }
    938 
    939 void eap_restore_last_id(struct eap_state *eap, uint8_t last_id)
    940 {
    941 	eap->last_id = last_id;
    942 }
    943 
    944 int eap_register_method(struct eap_method *method)
    945 {
    946 	if (!method->handle_request && !method->handle_response)
    947 		return -EPERM;
    948 
    949 	l_queue_push_head(eap_methods, method);
    950 	return 0;
    951 }
    952 
    953 int eap_unregister_method(struct eap_method *method)
    954 {
    955 	bool r;
    956 
    957 	r = l_queue_remove(eap_methods, method);
    958 	if (r)
    959 		return 0;
    960 
    961 	return -ENOENT;
    962 }
    963 
    964 void __eap_set_config(struct l_settings *config)
    965 {
    966 	if (!l_settings_get_uint(config, "EAP", "MTU", &default_mtu))
    967 		default_mtu = 1400; /* on WiFi the real MTU is around 2304 */
    968 }
    969 
    970 static void __eap_method_enable(struct eap_method_desc *start,
    971 					struct eap_method_desc *stop)
    972 {
    973 	struct eap_method_desc *desc;
    974 
    975 	l_debug("");
    976 
    977 	if (!start || !stop)
    978 		return;
    979 
    980 	for (desc = start; desc < stop; desc++) {
    981 		if (!desc->init)
    982 			continue;
    983 
    984 		desc->init();
    985 	}
    986 }
    987 
    988 static void __eap_method_disable(struct eap_method_desc *start,
    989 					struct eap_method_desc *stop)
    990 {
    991 	struct eap_method_desc *desc;
    992 
    993 	l_debug("");
    994 
    995 	if (!start || !stop)
    996 		return;
    997 
    998 	for (desc = start; desc < stop; desc++) {
    999 		if (!desc->exit)
   1000 			continue;
   1001 
   1002 		desc->exit();
   1003 	}
   1004 }
   1005 
   1006 extern struct eap_method_desc __start___eap[];
   1007 extern struct eap_method_desc __stop___eap[];
   1008 
   1009 int eap_init(void)
   1010 {
   1011 	eap_methods = l_queue_new();
   1012 	__eap_method_enable(__start___eap, __stop___eap);
   1013 
   1014 	/*
   1015 	 * RFC 3748, Section 3.1, [4], "Minimum MTU":
   1016 	 * EAP is capable of functioning on lower layers that
   1017 	 *        provide an EAP MTU size of 1020 octets or greater.
   1018 	 */
   1019 	if (default_mtu == 0)
   1020 		default_mtu = 1020;
   1021 
   1022 	return 0;
   1023 }
   1024 
   1025 void eap_exit(void)
   1026 {
   1027 	__eap_method_disable(__start___eap, __stop___eap);
   1028 	l_queue_destroy(eap_methods, NULL);
   1029 }
   1030 
   1031 IWD_MODULE(eap, eap_init, eap_exit);