Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / eap_peer / eap_tnc.c
blob6c95f72c15071b55af51bbb7b6ce1ef994d26317
1 /*
2 * EAP peer method: EAP-TNC (Trusted Network Connect)
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "base64.h"
19 #include "eap_i.h"
20 #include "tncc.h"
23 struct eap_tnc_data {
24 enum { WAIT_START, PROC_MSG, WAIT_FRAG_ACK, DONE, FAIL } state;
25 struct tncc_data *tncc;
26 struct wpabuf *in_buf;
27 struct wpabuf *out_buf;
28 size_t out_used;
29 size_t fragment_size;
33 /* EAP-TNC Flags */
34 #define EAP_TNC_FLAGS_LENGTH_INCLUDED 0x80
35 #define EAP_TNC_FLAGS_MORE_FRAGMENTS 0x40
36 #define EAP_TNC_FLAGS_START 0x20
37 #define EAP_TNC_VERSION_MASK 0x07
39 #define EAP_TNC_VERSION 1
42 static void * eap_tnc_init(struct eap_sm *sm)
44 struct eap_tnc_data *data;
46 data = os_zalloc(sizeof(*data));
47 if (data == NULL)
48 return NULL;
49 data->state = WAIT_START;
50 data->fragment_size = 1300;
51 data->tncc = tncc_init();
52 if (data->tncc == NULL) {
53 os_free(data);
54 return NULL;
57 return data;
61 static void eap_tnc_deinit(struct eap_sm *sm, void *priv)
63 struct eap_tnc_data *data = priv;
65 wpabuf_free(data->in_buf);
66 wpabuf_free(data->out_buf);
67 tncc_deinit(data->tncc);
68 os_free(data);
72 static struct wpabuf * eap_tnc_build_frag_ack(u8 id, u8 code)
74 struct wpabuf *msg;
76 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1, code, id);
77 if (msg == NULL) {
78 wpa_printf(MSG_ERROR, "EAP-TNC: Failed to allocate memory "
79 "for fragment ack");
80 return NULL;
82 wpabuf_put_u8(msg, EAP_TNC_VERSION); /* Flags */
84 wpa_printf(MSG_DEBUG, "EAP-TNC: Send fragment ack");
86 return msg;
90 static struct wpabuf * eap_tnc_build_msg(struct eap_tnc_data *data,
91 struct eap_method_ret *ret, u8 id)
93 struct wpabuf *resp;
94 u8 flags;
95 size_t send_len, plen;
97 ret->ignore = FALSE;
98 wpa_printf(MSG_DEBUG, "EAP-TNC: Generating Response");
99 ret->allowNotifications = TRUE;
101 flags = EAP_TNC_VERSION;
102 send_len = wpabuf_len(data->out_buf) - data->out_used;
103 if (1 + send_len > data->fragment_size) {
104 send_len = data->fragment_size - 1;
105 flags |= EAP_TNC_FLAGS_MORE_FRAGMENTS;
106 if (data->out_used == 0) {
107 flags |= EAP_TNC_FLAGS_LENGTH_INCLUDED;
108 send_len -= 4;
112 plen = 1 + send_len;
113 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
114 plen += 4;
115 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, plen,
116 EAP_CODE_RESPONSE, id);
117 if (resp == NULL)
118 return NULL;
120 wpabuf_put_u8(resp, flags); /* Flags */
121 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
122 wpabuf_put_be32(resp, wpabuf_len(data->out_buf));
124 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
125 send_len);
126 data->out_used += send_len;
128 ret->methodState = METHOD_MAY_CONT;
129 ret->decision = DECISION_FAIL;
131 if (data->out_used == wpabuf_len(data->out_buf)) {
132 wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
133 "(message sent completely)",
134 (unsigned long) send_len);
135 wpabuf_free(data->out_buf);
136 data->out_buf = NULL;
137 data->out_used = 0;
138 } else {
139 wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
140 "(%lu more to send)", (unsigned long) send_len,
141 (unsigned long) wpabuf_len(data->out_buf) -
142 data->out_used);
143 data->state = WAIT_FRAG_ACK;
146 return resp;
150 static int eap_tnc_process_cont(struct eap_tnc_data *data,
151 const u8 *buf, size_t len)
153 /* Process continuation of a pending message */
154 if (len > wpabuf_tailroom(data->in_buf)) {
155 wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment overflow");
156 data->state = FAIL;
157 return -1;
160 wpabuf_put_data(data->in_buf, buf, len);
161 wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes, waiting for "
162 "%lu bytes more", (unsigned long) len,
163 (unsigned long) wpabuf_tailroom(data->in_buf));
165 return 0;
169 static struct wpabuf * eap_tnc_process_fragment(struct eap_tnc_data *data,
170 struct eap_method_ret *ret,
171 u8 id, u8 flags,
172 u32 message_length,
173 const u8 *buf, size_t len)
175 /* Process a fragment that is not the last one of the message */
176 if (data->in_buf == NULL && !(flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)) {
177 wpa_printf(MSG_DEBUG, "EAP-TNC: No Message Length field in a "
178 "fragmented packet");
179 ret->ignore = TRUE;
180 return NULL;
183 if (data->in_buf == NULL) {
184 /* First fragment of the message */
185 data->in_buf = wpabuf_alloc(message_length);
186 if (data->in_buf == NULL) {
187 wpa_printf(MSG_DEBUG, "EAP-TNC: No memory for "
188 "message");
189 ret->ignore = TRUE;
190 return NULL;
192 wpabuf_put_data(data->in_buf, buf, len);
193 wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes in first "
194 "fragment, waiting for %lu bytes more",
195 (unsigned long) len,
196 (unsigned long) wpabuf_tailroom(data->in_buf));
199 return eap_tnc_build_frag_ack(id, EAP_CODE_RESPONSE);
203 static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
204 struct eap_method_ret *ret,
205 const struct wpabuf *reqData)
207 struct eap_tnc_data *data = priv;
208 struct wpabuf *resp;
209 const u8 *pos, *end;
210 u8 *rpos, *rpos1;
211 size_t len, rlen;
212 size_t imc_len;
213 char *start_buf, *end_buf;
214 size_t start_len, end_len;
215 int tncs_done = 0;
216 u8 flags, id;
217 u32 message_length = 0;
218 struct wpabuf tmpbuf;
220 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TNC, reqData, &len);
221 if (pos == NULL) {
222 wpa_printf(MSG_INFO, "EAP-TNC: Invalid frame (pos=%p len=%lu)",
223 pos, (unsigned long) len);
224 ret->ignore = TRUE;
225 return NULL;
228 id = eap_get_id(reqData);
230 end = pos + len;
232 if (len == 0)
233 flags = 0; /* fragment ack */
234 else
235 flags = *pos++;
237 if (len > 0 && (flags & EAP_TNC_VERSION_MASK) != EAP_TNC_VERSION) {
238 wpa_printf(MSG_DEBUG, "EAP-TNC: Unsupported version %d",
239 flags & EAP_TNC_VERSION_MASK);
240 ret->ignore = TRUE;
241 return NULL;
244 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED) {
245 if (end - pos < 4) {
246 wpa_printf(MSG_DEBUG, "EAP-TNC: Message underflow");
247 ret->ignore = TRUE;
248 return NULL;
250 message_length = WPA_GET_BE32(pos);
251 pos += 4;
253 if (message_length < (u32) (end - pos)) {
254 wpa_printf(MSG_DEBUG, "EAP-TNC: Invalid Message "
255 "Length (%d; %ld remaining in this msg)",
256 message_length, (long) (end - pos));
257 ret->ignore = TRUE;
258 return NULL;
262 wpa_printf(MSG_DEBUG, "EAP-TNC: Received packet: Flags 0x%x "
263 "Message Length %u", flags, message_length);
265 if (data->state == WAIT_FRAG_ACK) {
266 if (len > 1) {
267 wpa_printf(MSG_DEBUG, "EAP-TNC: Unexpected payload in "
268 "WAIT_FRAG_ACK state");
269 ret->ignore = TRUE;
270 return NULL;
272 wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment acknowledged");
273 data->state = PROC_MSG;
274 return eap_tnc_build_msg(data, ret, id);
277 if (data->in_buf && eap_tnc_process_cont(data, pos, end - pos) < 0) {
278 ret->ignore = TRUE;
279 return NULL;
282 if (flags & EAP_TNC_FLAGS_MORE_FRAGMENTS) {
283 return eap_tnc_process_fragment(data, ret, id, flags,
284 message_length, pos,
285 end - pos);
288 if (data->in_buf == NULL) {
289 /* Wrap unfragmented messages as wpabuf without extra copy */
290 wpabuf_set(&tmpbuf, pos, end - pos);
291 data->in_buf = &tmpbuf;
294 if (data->state == WAIT_START) {
295 if (!(flags & EAP_TNC_FLAGS_START)) {
296 wpa_printf(MSG_DEBUG, "EAP-TNC: Server did not use "
297 "start flag in the first message");
298 ret->ignore = TRUE;
299 goto fail;
302 tncc_init_connection(data->tncc);
304 data->state = PROC_MSG;
305 } else {
306 enum tncc_process_res res;
308 if (flags & EAP_TNC_FLAGS_START) {
309 wpa_printf(MSG_DEBUG, "EAP-TNC: Server used start "
310 "flag again");
311 ret->ignore = TRUE;
312 goto fail;
315 res = tncc_process_if_tnccs(data->tncc,
316 wpabuf_head(data->in_buf),
317 wpabuf_len(data->in_buf));
318 switch (res) {
319 case TNCCS_PROCESS_ERROR:
320 ret->ignore = TRUE;
321 goto fail;
322 case TNCCS_PROCESS_OK_NO_RECOMMENDATION:
323 case TNCCS_RECOMMENDATION_ERROR:
324 wpa_printf(MSG_DEBUG, "EAP-TNC: No "
325 "TNCCS-Recommendation received");
326 break;
327 case TNCCS_RECOMMENDATION_ALLOW:
328 wpa_msg(sm->msg_ctx, MSG_INFO,
329 "TNC: Recommendation = allow");
330 tncs_done = 1;
331 break;
332 case TNCCS_RECOMMENDATION_NONE:
333 wpa_msg(sm->msg_ctx, MSG_INFO,
334 "TNC: Recommendation = none");
335 tncs_done = 1;
336 break;
337 case TNCCS_RECOMMENDATION_ISOLATE:
338 wpa_msg(sm->msg_ctx, MSG_INFO,
339 "TNC: Recommendation = isolate");
340 tncs_done = 1;
341 break;
345 if (data->in_buf != &tmpbuf)
346 wpabuf_free(data->in_buf);
347 data->in_buf = NULL;
349 ret->ignore = FALSE;
350 ret->methodState = METHOD_MAY_CONT;
351 ret->decision = DECISION_UNCOND_SUCC;
352 ret->allowNotifications = TRUE;
354 if (data->out_buf) {
355 data->state = PROC_MSG;
356 return eap_tnc_build_msg(data, ret, id);
359 if (tncs_done) {
360 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1,
361 EAP_CODE_RESPONSE, eap_get_id(reqData));
362 if (resp == NULL)
363 return NULL;
365 wpabuf_put_u8(resp, EAP_TNC_VERSION);
366 wpa_printf(MSG_DEBUG, "EAP-TNC: TNCS done - reply with an "
367 "empty ACK message");
368 return resp;
371 imc_len = tncc_total_send_len(data->tncc);
373 start_buf = tncc_if_tnccs_start(data->tncc);
374 if (start_buf == NULL)
375 return NULL;
376 start_len = os_strlen(start_buf);
377 end_buf = tncc_if_tnccs_end();
378 if (end_buf == NULL) {
379 os_free(start_buf);
380 return NULL;
382 end_len = os_strlen(end_buf);
384 rlen = start_len + imc_len + end_len;
385 resp = wpabuf_alloc(rlen);
386 if (resp == NULL) {
387 os_free(start_buf);
388 os_free(end_buf);
389 return NULL;
392 wpabuf_put_data(resp, start_buf, start_len);
393 os_free(start_buf);
395 rpos1 = wpabuf_put(resp, 0);
396 rpos = tncc_copy_send_buf(data->tncc, rpos1);
397 wpabuf_put(resp, rpos - rpos1);
399 wpabuf_put_data(resp, end_buf, end_len);
400 os_free(end_buf);
402 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TNC: Response",
403 wpabuf_head(resp), wpabuf_len(resp));
405 data->out_buf = resp;
406 data->state = PROC_MSG;
407 return eap_tnc_build_msg(data, ret, id);
409 fail:
410 if (data->in_buf == &tmpbuf)
411 data->in_buf = NULL;
412 return NULL;
416 int eap_peer_tnc_register(void)
418 struct eap_method *eap;
419 int ret;
421 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
422 EAP_VENDOR_IETF, EAP_TYPE_TNC, "TNC");
423 if (eap == NULL)
424 return -1;
426 eap->init = eap_tnc_init;
427 eap->deinit = eap_tnc_deinit;
428 eap->process = eap_tnc_process;
430 ret = eap_peer_method_register(eap);
431 if (ret)
432 eap_peer_method_free(eap);
433 return ret;