aboutsummaryrefslogtreecommitdiffstats
path: root/src/eap_peer/eap_tnc.c
diff options
context:
space:
mode:
authorJouni Malinen <j@w1.fi>2008-05-26 09:00:18 (GMT)
committerJouni Malinen <j@w1.fi>2008-05-26 09:00:18 (GMT)
commit1b52ea47e434d197b259dbf48b9178b5dd6e74af (patch)
tree9faa0556d15a6ffb85a88056795c12b62a8027a8 /src/eap_peer/eap_tnc.c
parentd9521c74385a08b0d4c21619dc816b5c362363d9 (diff)
downloadhostap-1b52ea47e434d197b259dbf48b9178b5dd6e74af.zip
hostap-1b52ea47e434d197b259dbf48b9178b5dd6e74af.tar.gz
hostap-1b52ea47e434d197b259dbf48b9178b5dd6e74af.tar.bz2
Added fragmentation support for EAP-TNC
Diffstat (limited to 'src/eap_peer/eap_tnc.c')
-rw-r--r--src/eap_peer/eap_tnc.c238
1 files changed, 224 insertions, 14 deletions
diff --git a/src/eap_peer/eap_tnc.c b/src/eap_peer/eap_tnc.c
index e808be0..c8d822f 100644
--- a/src/eap_peer/eap_tnc.c
+++ b/src/eap_peer/eap_tnc.c
@@ -21,8 +21,12 @@
struct eap_tnc_data {
- EapMethodState state;
+ enum { WAIT_START, MSG, WAIT_FRAG_ACK, DONE, FAIL } state;
struct tncc_data *tncc;
+ struct wpabuf *in_buf;
+ struct wpabuf *out_buf;
+ size_t out_used;
+ size_t fragment_size;
};
@@ -42,7 +46,8 @@ static void * eap_tnc_init(struct eap_sm *sm)
data = os_zalloc(sizeof(*data));
if (data == NULL)
return NULL;
- data->state = METHOD_INIT;
+ data->state = WAIT_START;
+ data->fragment_size = 1300;
data->tncc = tncc_init();
if (data->tncc == NULL) {
os_free(data);
@@ -57,44 +62,236 @@ static void eap_tnc_deinit(struct eap_sm *sm, void *priv)
{
struct eap_tnc_data *data = priv;
+ wpabuf_free(data->in_buf);
+ wpabuf_free(data->out_buf);
tncc_deinit(data->tncc);
os_free(data);
}
+static struct wpabuf * eap_tnc_build_frag_ack(u8 id, u8 code)
+{
+ struct wpabuf *msg;
+
+ msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 0, code, id);
+ if (msg == NULL) {
+ wpa_printf(MSG_ERROR, "EAP-TNC: Failed to allocate memory "
+ "for fragment ack");
+ return NULL;
+ }
+
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Send fragment ack");
+
+ return msg;
+}
+
+
+static struct wpabuf * eap_tnc_build_msg(struct eap_tnc_data *data,
+ struct eap_method_ret *ret, u8 id)
+{
+ struct wpabuf *resp;
+ u8 flags;
+ size_t send_len, plen;
+
+ ret->ignore = FALSE;
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Generating Response");
+ ret->allowNotifications = TRUE;
+
+ flags = EAP_TNC_VERSION;
+ send_len = wpabuf_len(data->out_buf) - data->out_used;
+ if (1 + send_len > data->fragment_size) {
+ send_len = data->fragment_size - 1;
+ flags |= EAP_TNC_FLAGS_MORE_FRAGMENTS;
+ if (data->out_used == 0) {
+ flags |= EAP_TNC_FLAGS_LENGTH_INCLUDED;
+ send_len -= 4;
+ }
+ }
+
+ plen = 1 + send_len;
+ if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
+ plen += 4;
+ resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, plen,
+ EAP_CODE_RESPONSE, id);
+ if (resp == NULL)
+ return NULL;
+
+ wpabuf_put_u8(resp, flags); /* Flags */
+ if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
+ wpabuf_put_be32(resp, wpabuf_len(data->out_buf));
+
+ wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
+ send_len);
+ data->out_used += send_len;
+
+ ret->methodState = METHOD_MAY_CONT;
+ ret->decision = DECISION_FAIL;
+
+ if (data->out_used == wpabuf_len(data->out_buf)) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
+ "(message sent completely)",
+ (unsigned long) send_len);
+ wpabuf_free(data->out_buf);
+ data->out_buf = NULL;
+ data->out_used = 0;
+ } else {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
+ "(%lu more to send)", (unsigned long) send_len,
+ (unsigned long) wpabuf_len(data->out_buf) -
+ data->out_used);
+ data->state = WAIT_FRAG_ACK;
+ }
+
+ return resp;
+}
+
+
+static int eap_tnc_process_cont(struct eap_tnc_data *data,
+ const u8 *buf, size_t len)
+{
+ /* Process continuation of a pending message */
+ if (len > wpabuf_tailroom(data->in_buf)) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment overflow");
+ data->state = FAIL;
+ return -1;
+ }
+
+ wpabuf_put_data(data->in_buf, buf, len);
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes, waiting for "
+ "%lu bytes more", (unsigned long) len,
+ (unsigned long) wpabuf_tailroom(data->in_buf));
+
+ return 0;
+}
+
+
+static struct wpabuf * eap_tnc_process_fragment(struct eap_tnc_data *data,
+ struct eap_method_ret *ret,
+ u8 id, u8 flags,
+ u32 message_length,
+ const u8 *buf, size_t len)
+{
+ /* Process a fragment that is not the last one of the message */
+ if (data->in_buf == NULL && !(flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: No Message Length field in a "
+ "fragmented packet");
+ ret->ignore = TRUE;
+ return NULL;
+ }
+
+ if (data->in_buf == NULL) {
+ /* First fragment of the message */
+ data->in_buf = wpabuf_alloc(message_length);
+ if (data->in_buf == NULL) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: No memory for "
+ "message");
+ ret->ignore = TRUE;
+ return NULL;
+ }
+ wpabuf_put_data(data->in_buf, buf, len);
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes in first "
+ "fragment, waiting for %lu bytes more",
+ (unsigned long) len,
+ (unsigned long) wpabuf_tailroom(data->in_buf));
+ }
+
+ return eap_tnc_build_frag_ack(id, EAP_CODE_RESPONSE);
+}
+
+
static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
struct eap_method_ret *ret,
const struct wpabuf *reqData)
{
struct eap_tnc_data *data = priv;
struct wpabuf *resp;
- const u8 *pos;
+ const u8 *pos, *end;
u8 *rpos, *rpos1, *start;
size_t len, rlen;
size_t imc_len;
char *start_buf, *end_buf;
size_t start_len, end_len;
int tncs_done = 0;
+ u8 flags, id;
+ u32 message_length = 0;
+ struct wpabuf tmpbuf;
pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TNC, reqData, &len);
- if (pos == NULL || len == 0) {
+ if (pos == NULL) {
wpa_printf(MSG_INFO, "EAP-TNC: Invalid frame (pos=%p len=%lu)",
pos, (unsigned long) len);
ret->ignore = TRUE;
return NULL;
}
- wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TNC: Received payload", pos, len);
+ id = eap_get_id(reqData);
+
+ end = pos + len;
+
+ if (len == 0)
+ flags = 0; /* fragment ack */
+ else
+ flags = *pos++;
- if ((*pos & EAP_TNC_VERSION_MASK) != EAP_TNC_VERSION) {
+ if (len > 0 && (flags & EAP_TNC_VERSION_MASK) != EAP_TNC_VERSION) {
wpa_printf(MSG_DEBUG, "EAP-TNC: Unsupported version %d",
- *pos & EAP_TNC_VERSION_MASK);
+ flags & EAP_TNC_VERSION_MASK);
ret->ignore = TRUE;
return NULL;
}
- if (data->state == METHOD_INIT) {
- if (!(*pos & EAP_TNC_FLAGS_START)) {
+ if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED) {
+ if (end - pos < 4) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Message underflow");
+ ret->ignore = TRUE;
+ return NULL;
+ }
+ message_length = WPA_GET_BE32(pos);
+ pos += 4;
+
+ if (message_length < (u32) (end - pos)) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Invalid Message "
+ "Length (%d; %ld remaining in this msg)",
+ message_length, (long) (end - pos));
+ ret->ignore = TRUE;
+ return NULL;
+ }
+ }
+
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Received packet: Flags 0x%x "
+ "Message Length %u", flags, message_length);
+
+ if (data->state == WAIT_FRAG_ACK) {
+ if (len != 0) {
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Unexpected payload in "
+ "WAIT_FRAG_ACK state");
+ ret->ignore = TRUE;
+ return NULL;
+ }
+ wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment acknowledged");
+ data->state = MSG;
+ return eap_tnc_build_msg(data, ret, id);
+ }
+
+ if (data->in_buf && eap_tnc_process_cont(data, pos, end - pos) < 0) {
+ ret->ignore = TRUE;
+ return NULL;
+ }
+
+ if (flags & EAP_TNC_FLAGS_MORE_FRAGMENTS) {
+ return eap_tnc_process_fragment(data, ret, id, flags,
+ message_length, pos,
+ end - pos);
+ }
+
+ if (data->in_buf == NULL) {
+ /* Wrap unfragmented messages as wpabuf without extra copy */
+ wpabuf_set(&tmpbuf, pos, end - pos);
+ data->in_buf = &tmpbuf;
+ }
+
+ if (data->state == WAIT_START) {
+ if (!(flags & EAP_TNC_FLAGS_START)) {
wpa_printf(MSG_DEBUG, "EAP-TNC: Server did not use "
"start flag in the first message");
ret->ignore = TRUE;
@@ -103,18 +300,20 @@ static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
tncc_init_connection(data->tncc);
- data->state = METHOD_MAY_CONT;
+ data->state = MSG;
} else {
enum tncc_process_res res;
- if (*pos & EAP_TNC_FLAGS_START) {
+ if (flags & EAP_TNC_FLAGS_START) {
wpa_printf(MSG_DEBUG, "EAP-TNC: Server used start "
"flag again");
ret->ignore = TRUE;
return NULL;
}
- res = tncc_process_if_tnccs(data->tncc, pos + 1, len - 1);
+ res = tncc_process_if_tnccs(data->tncc,
+ wpabuf_head(data->in_buf),
+ wpabuf_len(data->in_buf));
switch (res) {
case TNCCS_PROCESS_ERROR:
ret->ignore = TRUE;
@@ -142,11 +341,20 @@ static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
}
}
+ if (data->in_buf != &tmpbuf)
+ wpabuf_free(data->in_buf);
+ data->in_buf = NULL;
+
ret->ignore = FALSE;
- ret->methodState = data->state;
+ ret->methodState = METHOD_MAY_CONT;
ret->decision = DECISION_UNCOND_SUCC;
ret->allowNotifications = TRUE;
+ if (data->out_buf) {
+ data->state = MSG;
+ return eap_tnc_build_msg(data, ret, id);
+ }
+
if (tncs_done) {
resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1,
EAP_CODE_RESPONSE, eap_get_id(reqData));
@@ -195,7 +403,9 @@ static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TNC: Response", start, rlen);
- return resp;
+ data->out_buf = resp;
+ data->state = MSG;
+ return eap_tnc_build_msg(data, ret, id);
}