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
12 * See README and COPYING for more details.
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;
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));
49 data->state = WAIT_START;
50 data->fragment_size = 1300;
51 data->tncc = tncc_init();
52 if (data->tncc == NULL) {
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);
72 static struct wpabuf * eap_tnc_build_frag_ack(u8 id, u8 code)
76 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1, code, id);
78 wpa_printf(MSG_ERROR, "EAP-TNC: Failed to allocate memory "
82 wpabuf_put_u8(msg, EAP_TNC_VERSION); /* Flags */
84 wpa_printf(MSG_DEBUG, "EAP-TNC: Send fragment ack");
90 static struct wpabuf * eap_tnc_build_msg(struct eap_tnc_data *data,
91 struct eap_method_ret *ret, u8 id)
95 size_t send_len, plen;
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;
113 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
115 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, plen,
116 EAP_CODE_RESPONSE, id);
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,
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;
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) -
143 data->state = WAIT_FRAG_ACK;
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");
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));
169 static struct wpabuf * eap_tnc_process_fragment(struct eap_tnc_data *data,
170 struct eap_method_ret *ret,
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");
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 "
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",
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;
213 char *start_buf, *end_buf;
214 size_t start_len, end_len;
217 u32 message_length = 0;
218 struct wpabuf tmpbuf;
220 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TNC, reqData, &len);
222 wpa_printf(MSG_INFO, "EAP-TNC: Invalid frame (pos=%p len=%lu)",
223 pos, (unsigned long) len);
228 id = eap_get_id(reqData);
233 flags = 0; /* fragment ack */
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);
244 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED) {
246 wpa_printf(MSG_DEBUG, "EAP-TNC: Message underflow");
250 message_length = WPA_GET_BE32(pos);
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));
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) {
267 wpa_printf(MSG_DEBUG, "EAP-TNC: Unexpected payload in "
268 "WAIT_FRAG_ACK state");
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) {
282 if (flags & EAP_TNC_FLAGS_MORE_FRAGMENTS) {
283 return eap_tnc_process_fragment(data, ret, id, flags,
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");
302 tncc_init_connection(data->tncc);
304 data->state = PROC_MSG;
306 enum tncc_process_res res;
308 if (flags & EAP_TNC_FLAGS_START) {
309 wpa_printf(MSG_DEBUG, "EAP-TNC: Server used start "
315 res = tncc_process_if_tnccs(data->tncc,
316 wpabuf_head(data->in_buf),
317 wpabuf_len(data->in_buf));
319 case TNCCS_PROCESS_ERROR:
322 case TNCCS_PROCESS_OK_NO_RECOMMENDATION:
323 case TNCCS_RECOMMENDATION_ERROR:
324 wpa_printf(MSG_DEBUG, "EAP-TNC: No "
325 "TNCCS-Recommendation received");
327 case TNCCS_RECOMMENDATION_ALLOW:
328 wpa_msg(sm->msg_ctx, MSG_INFO,
329 "TNC: Recommendation = allow");
332 case TNCCS_RECOMMENDATION_NONE:
333 wpa_msg(sm->msg_ctx, MSG_INFO,
334 "TNC: Recommendation = none");
337 case TNCCS_RECOMMENDATION_ISOLATE:
338 wpa_msg(sm->msg_ctx, MSG_INFO,
339 "TNC: Recommendation = isolate");
345 if (data->in_buf != &tmpbuf)
346 wpabuf_free(data->in_buf);
350 ret->methodState = METHOD_MAY_CONT;
351 ret->decision = DECISION_UNCOND_SUCC;
352 ret->allowNotifications = TRUE;
355 data->state = PROC_MSG;
356 return eap_tnc_build_msg(data, ret, id);
360 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1,
361 EAP_CODE_RESPONSE, eap_get_id(reqData));
365 wpabuf_put_u8(resp, EAP_TNC_VERSION);
366 wpa_printf(MSG_DEBUG, "EAP-TNC: TNCS done - reply with an "
367 "empty ACK message");
371 imc_len = tncc_total_send_len(data->tncc);
373 start_buf = tncc_if_tnccs_start(data->tncc);
374 if (start_buf == NULL)
376 start_len = os_strlen(start_buf);
377 end_buf = tncc_if_tnccs_end();
378 if (end_buf == NULL) {
382 end_len = os_strlen(end_buf);
384 rlen = start_len + imc_len + end_len;
385 resp = wpabuf_alloc(rlen);
392 wpabuf_put_data(resp, start_buf, start_len);
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);
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);
410 if (data->in_buf == &tmpbuf)
416 int eap_peer_tnc_register(void)
418 struct eap_method *eap;
421 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
422 EAP_VENDOR_IETF, EAP_TYPE_TNC, "TNC");
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);
432 eap_peer_method_free(eap);