2005-11-09 13:37:55 +00:00
|
|
|
/**
|
|
|
|
* @file parser.c
|
|
|
|
*
|
|
|
|
* @brief Generic parser class used to parse IKEv2-Header and Payload
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2005-11-10 19:19:56 +00:00
|
|
|
#include <arpa/inet.h>
|
2005-11-09 13:37:55 +00:00
|
|
|
|
2005-11-11 13:52:11 +00:00
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
#include "types.h"
|
2005-11-11 12:27:06 +00:00
|
|
|
#include "definitions.h"
|
2005-11-11 11:20:22 +00:00
|
|
|
#include "globals.h"
|
2005-11-11 13:52:11 +00:00
|
|
|
#include "utils/allocator.h"
|
|
|
|
#include "utils/logger.h"
|
2005-11-14 06:55:51 +00:00
|
|
|
#include "payloads/payload.h"
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/**
|
|
|
|
* @private data stored in a context
|
|
|
|
*
|
|
|
|
* contains pointers and counters to store current state
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
typedef struct private_parser_s private_parser_t;
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
struct private_parser_s {
|
2005-11-10 19:19:56 +00:00
|
|
|
/**
|
|
|
|
* Public members
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
parser_t public;
|
2005-11-10 19:19:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current bit for reading in input data
|
|
|
|
*/
|
|
|
|
u_int8_t bit_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current byte for reading in input data
|
|
|
|
*/
|
|
|
|
u_int8_t *byte_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* input data to parse
|
|
|
|
*/
|
|
|
|
u_int8_t *input;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* roof of input
|
|
|
|
*/
|
|
|
|
u_int8_t *input_roof;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* logger object
|
|
|
|
*/
|
|
|
|
logger_t *logger;
|
2005-11-11 08:04:51 +00:00
|
|
|
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
};
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/**
|
|
|
|
* implementation of parser_context_t.parse_payload
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
static status_t parse_payload(private_parser_t *this, payload_type_t payload_type, payload_t **payload)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
|
|
|
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "parsing %s payload", mapping_find(payload_type_t_mappings, payload_type));
|
2005-11-11 12:27:06 +00:00
|
|
|
|
2005-11-10 19:19:56 +00:00
|
|
|
/* find payload in null terminated list*/
|
2005-11-14 09:43:18 +00:00
|
|
|
|
|
|
|
payload_t *pld;
|
|
|
|
void *output;
|
|
|
|
int current;
|
|
|
|
encoding_rule_t *rule;
|
|
|
|
size_t rule_count;
|
|
|
|
|
|
|
|
/* ok, do the parsing */
|
|
|
|
pld = payload_create(payload_type);
|
|
|
|
if (pld == NULL)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " payload %s not supported", mapping_find(payload_type_t_mappings, payload_type));
|
2005-11-14 09:43:18 +00:00
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
/* base pointer for output, avoids casting in every rule */
|
|
|
|
output = pld;
|
|
|
|
|
|
|
|
pld->get_encoding_rules(pld, &rule, &rule_count);
|
|
|
|
|
|
|
|
for (current = 0; current < rule_count; current++)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, CONTROL_MORE, " parsing rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
switch (rule->type)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 09:43:18 +00:00
|
|
|
case U_INT_4:
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 09:43:18 +00:00
|
|
|
u_int8_t *output_pos = output + rule->offset;
|
2005-11-14 12:10:05 +00:00
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
switch (this->bit_pos)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
*output_pos = *(this->byte_pos) >> 4;
|
|
|
|
this->bit_pos = 4;
|
2005-11-10 19:19:56 +00:00
|
|
|
break;
|
2005-11-14 09:43:18 +00:00
|
|
|
case 4:
|
|
|
|
*output_pos = *(this->byte_pos) & 0x0F;
|
|
|
|
this->bit_pos = 0;
|
|
|
|
this->byte_pos++;
|
2005-11-10 19:19:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
2005-11-10 19:19:56 +00:00
|
|
|
return PARSE_ERROR;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_8:
|
|
|
|
{
|
|
|
|
u_int8_t *output_pos = output + rule->offset;
|
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*output_pos = *(this->byte_pos);
|
|
|
|
this->byte_pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_16:
|
|
|
|
{
|
|
|
|
u_int16_t *output_pos = output + rule->offset;
|
|
|
|
if (this->byte_pos + sizeof(u_int16_t) > this->input_roof)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if ((int)this->byte_pos % 2)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos odd bytepos",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
*output_pos = ntohs(*((u_int16_t*)this->byte_pos));
|
|
|
|
this->byte_pos += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_32:
|
|
|
|
{
|
|
|
|
u_int32_t *output_pos = output + rule->offset;
|
|
|
|
if (this->byte_pos + sizeof(u_int32_t) > this->input_roof)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if ((int)this->byte_pos % 4)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on unaligned bytepos",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
*output_pos = ntohl(*((u_int32_t*)this->byte_pos));
|
|
|
|
this->byte_pos += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_64:
|
|
|
|
{
|
|
|
|
u_int32_t *output_pos = output + rule->offset;
|
|
|
|
if (this->byte_pos + 2 * sizeof(u_int32_t) > this->input_roof)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if ((int)this->byte_pos % 8)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on unaligned bytepos",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
/* assuming little endian host order */
|
|
|
|
*(output_pos + 1) = ntohl(*((u_int32_t*)this->byte_pos));
|
|
|
|
this->byte_pos += 4;
|
|
|
|
*output_pos = ntohl(*((u_int32_t*)this->byte_pos));
|
|
|
|
this->byte_pos += 4;
|
|
|
|
|
|
|
|
break;
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
case RESERVED_BIT:
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
this->bit_pos = (this->bit_pos + 1) % 8;
|
|
|
|
if (this->bit_pos == 0)
|
|
|
|
{
|
|
|
|
this->byte_pos++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RESERVED_BYTE:
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
this->byte_pos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FLAG:
|
|
|
|
{
|
|
|
|
bool *output_pos = output + rule->offset;
|
|
|
|
u_int8_t mask;
|
2005-11-14 12:10:05 +00:00
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
mask = 0x01 << (7 - this->bit_pos);
|
|
|
|
*output_pos = *this->byte_pos & mask;
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
if (*output_pos)
|
|
|
|
{
|
|
|
|
/* set to a "clean", comparable true */
|
|
|
|
*output_pos = TRUE;
|
|
|
|
}
|
|
|
|
this->bit_pos = (this->bit_pos + 1) % 8;
|
|
|
|
if (this->bit_pos == 0)
|
|
|
|
{
|
|
|
|
this->byte_pos++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-11-14 12:10:05 +00:00
|
|
|
case HEADER_LENGTH:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
u_int32_t *output_pos = output + rule->offset;
|
|
|
|
if (this->byte_pos + sizeof(u_int32_t) > this->input_roof)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type), this->bit_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if ((int)this->byte_pos % 4)
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on unaligned bytepos",
|
|
|
|
current, mapping_find(encoding_type_t_mappings, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
*output_pos = ntohl(*((u_int32_t*)this->byte_pos));
|
|
|
|
this->byte_pos += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " no rule to parse rule %d %s (%d)", current, mapping_find(payload_type_t_mappings, payload_type), payload_type);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
/* process next rulue */
|
|
|
|
rule++;
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
*payload = pld;
|
|
|
|
return SUCCESS;
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/**
|
|
|
|
* implementation of parser_t.destroy
|
|
|
|
*/
|
2005-11-10 19:19:56 +00:00
|
|
|
static status_t destroy(private_parser_t *this)
|
|
|
|
{
|
2005-11-11 11:20:22 +00:00
|
|
|
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
|
2005-11-10 19:19:56 +00:00
|
|
|
allocator_free(this);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/*
|
|
|
|
* see header file
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
parser_t *parser_create(chunk_t data)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
|
|
|
private_parser_t *this = allocator_alloc_thing(private_parser_t);
|
|
|
|
|
|
|
|
if (this == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
this->logger = global_logger_manager->create_logger(global_logger_manager, PARSER, NULL);
|
2005-11-14 12:10:05 +00:00
|
|
|
this->logger->enable_level(this->logger, ALL);
|
|
|
|
|
2005-11-11 11:20:22 +00:00
|
|
|
|
2005-11-10 19:19:56 +00:00
|
|
|
if (this->logger == NULL)
|
|
|
|
{
|
|
|
|
allocator_free(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
|
|
|
|
this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,payload_t**)) parse_payload;
|
2005-11-10 19:19:56 +00:00
|
|
|
this->public.destroy = (status_t(*)(parser_t*)) destroy;
|
|
|
|
|
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
this->input = data.ptr;
|
|
|
|
this->byte_pos = data.ptr;
|
|
|
|
this->bit_pos = 0;
|
|
|
|
this->input_roof = data.ptr + data.len;
|
2005-11-10 19:19:56 +00:00
|
|
|
|
|
|
|
return (parser_t*)this;
|
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
|