⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eapol_sm.c

📁 WPA在Linux下实现的原代码 WPA在Linux下实现的原代码
💻 C
📖 第 1 页 / 共 4 页
字号:
		/* restart EAPOL state machine step from timeout call in order		 * to allow other events to be processed. */		eloop_cancel_timeout(eapol_sm_step_timeout, NULL, sm);		eloop_register_timeout(0, 0, eapol_sm_step_timeout, NULL, sm);	}	if (sm->ctx->cb && sm->cb_status != EAPOL_CB_IN_PROGRESS) {		int success = sm->cb_status == EAPOL_CB_SUCCESS ? 1 : 0;		sm->cb_status = EAPOL_CB_IN_PROGRESS;		sm->ctx->cb(sm, success, sm->ctx->cb_ctx);	}}static const char *eapol_supp_pae_state(int state){	switch (state) {	case SUPP_PAE_LOGOFF:		return "LOGOFF";	case SUPP_PAE_DISCONNECTED:		return "DISCONNECTED";	case SUPP_PAE_CONNECTING:		return "CONNECTING";	case SUPP_PAE_AUTHENTICATING:		return "AUTHENTICATING";	case SUPP_PAE_HELD:		return "HELD";	case SUPP_PAE_AUTHENTICATED:		return "AUTHENTICATED";	case SUPP_PAE_RESTART:		return "RESTART";	default:		return "UNKNOWN";	}}static const char *eapol_supp_be_state(int state){	switch (state) {	case SUPP_BE_REQUEST:		return "REQUEST";	case SUPP_BE_RESPONSE:		return "RESPONSE";	case SUPP_BE_SUCCESS:		return "SUCCESS";	case SUPP_BE_FAIL:		return "FAIL";	case SUPP_BE_TIMEOUT:		return "TIMEOUT";	case SUPP_BE_IDLE:		return "IDLE";	case SUPP_BE_INITIALIZE:		return "INITIALIZE";	case SUPP_BE_RECEIVE:		return "RECEIVE";	default:		return "UNKNOWN";	}}static const char * eapol_port_status(PortStatus status){	if (status == Authorized)		return "Authorized";	else		return "Unauthorized";}static const char * eapol_port_control(PortControl ctrl){	switch (ctrl) {	case Auto:		return "Auto";	case ForceUnauthorized:		return "ForceUnauthorized";	case ForceAuthorized:		return "ForceAuthorized";	default:		return "Unknown";	}}/** * eapol_sm_configure - Set EAPOL variables * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @heldPeriod: dot1xSuppHeldPeriod * @authPeriod: dot1xSuppAuthPeriod * @startPeriod: dot1xSuppStartPeriod * @maxStart: dot1xSuppMaxStart * * Set configurable EAPOL state machine variables. Each variable can be set to * the given value or ignored if set to -1 (to set only some of the variables). */void eapol_sm_configure(struct eapol_sm *sm, int heldPeriod, int authPeriod,			int startPeriod, int maxStart){	if (sm == NULL)		return;	if (heldPeriod >= 0)		sm->heldPeriod = heldPeriod;	if (authPeriod >= 0)		sm->authPeriod = authPeriod;	if (startPeriod >= 0)		sm->startPeriod = startPeriod;	if (maxStart >= 0)		sm->maxStart = maxStart;}/** * eapol_sm_get_status - Get EAPOL state machine status * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @buf: Buffer for status information * @buflen: Maximum buffer length * @verbose: Whether to include verbose status information * Returns: Number of bytes written to buf. * * Query EAPOL state machine for status information. This function fills in a * text area with current status information from the EAPOL state machine. If * the buffer (buf) is not large enough, status information will be truncated * to fit the buffer. */int eapol_sm_get_status(struct eapol_sm *sm, char *buf, size_t buflen,			int verbose){	int len;	if (sm == NULL)		return 0;	len = snprintf(buf, buflen,		       "Supplicant PAE state=%s\n"		       "suppPortStatus=%s\n",		       eapol_supp_pae_state(sm->SUPP_PAE_state),		       eapol_port_status(sm->suppPortStatus));	if (verbose) {		len += snprintf(buf + len, buflen - len,				"heldPeriod=%u\n"				"authPeriod=%u\n"				"startPeriod=%u\n"				"maxStart=%u\n"				"portControl=%s\n"				"Supplicant Backend state=%s\n",				sm->heldPeriod,				sm->authPeriod,				sm->startPeriod,				sm->maxStart,				eapol_port_control(sm->portControl),				eapol_supp_be_state(sm->SUPP_BE_state));	}	len += eap_sm_get_status(sm->eap, buf + len, buflen - len, verbose);	return len;}/** * eapol_sm_get_mib - Get EAPOL state machine MIBs * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @buf: Buffer for MIB information * @buflen: Maximum buffer length * Returns: Number of bytes written to buf. * * Query EAPOL state machine for MIB information. This function fills in a * text area with current MIB information from the EAPOL state machine. If * the buffer (buf) is not large enough, MIB information will be truncated to * fit the buffer. */int eapol_sm_get_mib(struct eapol_sm *sm, char *buf, size_t buflen){	int len;	if (sm == NULL)		return 0;	len = snprintf(buf, buflen,		       "dot1xSuppPaeState=%d\n"		       "dot1xSuppHeldPeriod=%u\n"		       "dot1xSuppAuthPeriod=%u\n"		       "dot1xSuppStartPeriod=%u\n"		       "dot1xSuppMaxStart=%u\n"		       "dot1xSuppSuppControlledPortStatus=%s\n"		       "dot1xSuppBackendPaeState=%d\n"		       "dot1xSuppEapolFramesRx=%u\n"		       "dot1xSuppEapolFramesTx=%u\n"		       "dot1xSuppEapolStartFramesTx=%u\n"		       "dot1xSuppEapolLogoffFramesTx=%u\n"		       "dot1xSuppEapolRespFramesTx=%u\n"		       "dot1xSuppEapolReqIdFramesRx=%u\n"		       "dot1xSuppEapolReqFramesRx=%u\n"		       "dot1xSuppInvalidEapolFramesRx=%u\n"		       "dot1xSuppEapLengthErrorFramesRx=%u\n"		       "dot1xSuppLastEapolFrameVersion=%u\n"		       "dot1xSuppLastEapolFrameSource=" MACSTR "\n",		       sm->SUPP_PAE_state,		       sm->heldPeriod,		       sm->authPeriod,		       sm->startPeriod,		       sm->maxStart,		       sm->suppPortStatus == Authorized ?		       "Authorized" : "Unauthorized",		       sm->SUPP_BE_state,		       sm->dot1xSuppEapolFramesRx,		       sm->dot1xSuppEapolFramesTx,		       sm->dot1xSuppEapolStartFramesTx,		       sm->dot1xSuppEapolLogoffFramesTx,		       sm->dot1xSuppEapolRespFramesTx,		       sm->dot1xSuppEapolReqIdFramesRx,		       sm->dot1xSuppEapolReqFramesRx,		       sm->dot1xSuppInvalidEapolFramesRx,		       sm->dot1xSuppEapLengthErrorFramesRx,		       sm->dot1xSuppLastEapolFrameVersion,		       MAC2STR(sm->dot1xSuppLastEapolFrameSource));	return len;}/** * eapol_sm_rx_eapol - Process received EAPOL frames * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @src: Source MAC address of the EAPOL packet * @buf: Pointer to the beginning of the EAPOL data (EAPOL header) * @len: Length of the EAPOL frame * Returns: 1 = EAPOL frame processed, 0 = not for EAPOL state machine, * -1 failure */int eapol_sm_rx_eapol(struct eapol_sm *sm, const u8 *src, const u8 *buf,		      size_t len){	const struct ieee802_1x_hdr *hdr;	const struct ieee802_1x_eapol_key *key;	int plen, data_len;	int res = 1;	if (sm == NULL)		return 0;	sm->dot1xSuppEapolFramesRx++;	if (len < sizeof(*hdr)) {		sm->dot1xSuppInvalidEapolFramesRx++;		return 0;	}	hdr = (const struct ieee802_1x_hdr *) buf;	sm->dot1xSuppLastEapolFrameVersion = hdr->version;	memcpy(sm->dot1xSuppLastEapolFrameSource, src, ETH_ALEN);	if (hdr->version < EAPOL_VERSION) {		/* TODO: backwards compatibility */	}	plen = be_to_host16(hdr->length);	if (plen > len - sizeof(*hdr)) {		sm->dot1xSuppEapLengthErrorFramesRx++;		return 0;	}	data_len = plen + sizeof(*hdr);	switch (hdr->type) {	case IEEE802_1X_TYPE_EAP_PACKET:		if (sm->cached_pmk) {			/* Trying to use PMKSA caching, but Authenticator did			 * not seem to have a matching entry. Need to restart			 * EAPOL state machines.			 */			eapol_sm_abort_cached(sm);		}		free(sm->eapReqData);		sm->eapReqDataLen = plen;		sm->eapReqData = malloc(sm->eapReqDataLen);		if (sm->eapReqData) {			wpa_printf(MSG_DEBUG, "EAPOL: Received EAP-Packet "				   "frame");			memcpy(sm->eapReqData, (u8 *) (hdr + 1),			       sm->eapReqDataLen);			sm->eapolEap = TRUE;			eapol_sm_step(sm);		}		break;	case IEEE802_1X_TYPE_EAPOL_KEY:		if (plen < sizeof(*key)) {			wpa_printf(MSG_DEBUG, "EAPOL: Too short EAPOL-Key "				   "frame received");			break;		}		key = (const struct ieee802_1x_eapol_key *) (hdr + 1);		if (key->type == EAPOL_KEY_TYPE_WPA ||		    key->type == EAPOL_KEY_TYPE_RSN) {			/* WPA Supplicant takes care of this frame. */			wpa_printf(MSG_DEBUG, "EAPOL: Ignoring WPA EAPOL-Key "				   "frame in EAPOL state machines");			res = 0;			break;		}		if (key->type != EAPOL_KEY_TYPE_RC4) {			wpa_printf(MSG_DEBUG, "EAPOL: Ignored unknown "				   "EAPOL-Key type %d", key->type);			break;		}		free(sm->last_rx_key);		sm->last_rx_key = malloc(data_len);		if (sm->last_rx_key) {			wpa_printf(MSG_DEBUG, "EAPOL: Received EAPOL-Key "				   "frame");			memcpy(sm->last_rx_key, buf, data_len);			sm->last_rx_key_len = data_len;			sm->rxKey = TRUE;			eapol_sm_step(sm);		}		break;	default:		wpa_printf(MSG_DEBUG, "EAPOL: Received unknown EAPOL type %d",			   hdr->type);		sm->dot1xSuppInvalidEapolFramesRx++;		break;	}	return res;}/** * eapol_sm_notify_tx_eapol_key - Notification about transmitted EAPOL packet * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * * Notify EAPOL station machine about transmitted EAPOL packet from an external * component, e.g., WPA. This will update the statistics. */void eapol_sm_notify_tx_eapol_key(struct eapol_sm *sm){	if (sm)		sm->dot1xSuppEapolFramesTx++;}/** * eapol_sm_notify_portEnabled - Notification about portEnabled change * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @enabled: New portEnabled value * * Notify EAPOL station machine about new portEnabled value. */void eapol_sm_notify_portEnabled(struct eapol_sm *sm, Boolean enabled){	if (sm == NULL)		return;	wpa_printf(MSG_DEBUG, "EAPOL: External notification - "		   "portEnabled=%d", enabled);	sm->portEnabled = enabled;	eapol_sm_step(sm);}/** * eapol_sm_notify_portValid - Notification about portValid change * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @valid: New portValid value * * Notify EAPOL station machine about new portValid value. */void eapol_sm_notify_portValid(struct eapol_sm *sm, Boolean valid){	if (sm == NULL)		return;	wpa_printf(MSG_DEBUG, "EAPOL: External notification - "		   "portValid=%d", valid);	sm->portValid = valid;	eapol_sm_step(sm);}/** * eapol_sm_notify_eap_success - Notification of external EAP success trigger * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @success: %TRUE = set success, %FALSE = clear success * * Notify EAPOL station machine that external event has forced EAP state to * success (success = %TRUE). This can be cleared by setting success = %FALSE. * * This function is called to update EAP state when WPA-PSK key handshake has * been completed successfully since WPA-PSK does not use EAP state machine. */void eapol_sm_notify_eap_success(struct eapol_sm *sm, Boolean success){	if (sm == NULL)		return;	wpa_printf(MSG_DEBUG, "EAPOL: External notification - "		   "EAP success=%d", success);	sm->eapSuccess = success;	sm->altAccept = success;	if (success)		eap_notify_success(sm->eap);	eapol_sm_step(sm);}/** * eapol_sm_notify_eap_fail - Notification of external EAP failure trigger * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @fail: %TRUE = set failure, %FALSE = clear failure * * Notify EAPOL station machine that external event has forced EAP state to * failure (fail = %TRUE). This can be cleared by setting fail = %FALSE. */void eapol_sm_notify_eap_fail(struct eapol_sm *sm, Boolean fail){	if (sm == NULL)		return;	wpa_printf(MSG_DEBUG, "EAPOL: External notification - "		   "EAP fail=%d", fail);	sm->eapFail = fail;	sm->altReject = fail;	eapol_sm_step(sm);}/** * eapol_sm_notify_config - Notification of EAPOL configuration change * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init() * @config: Pointer to current network configuration * @conf: Pointer to EAPOL configuration data * * Notify EAPOL station machine that configuration has changed. config will be * stored as a backpointer to network configuration. This can be %NULL to clear * the stored pointed. conf will be copied to local EAPOL/EAP configuration * data. If conf is %NULL, this part of the configuration change will be * skipped. */void eapol_sm_notify_config(struct eapol_sm *sm, struct wpa_ssid *config,			    const struct eapol_config *conf){	if (sm == NULL)		return;	sm->config = config;	if (conf == NULL)		return;	sm->conf.accept_802_1x_keys = conf->accept_802_1x_keys;	sm->conf.required_keys = conf->required_keys;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -