2 * EAP peer method: EAP-TNC (Trusted Network Connect)
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "eap_config.h"
18 enum { WAIT_START, PROC_MSG, WAIT_FRAG_ACK, DONE, FAIL } state;
19 struct tncc_data *tncc;
20 struct wpabuf *in_buf;
21 struct wpabuf *out_buf;
28 #define EAP_TNC_FLAGS_LENGTH_INCLUDED 0x80
29 #define EAP_TNC_FLAGS_MORE_FRAGMENTS 0x40
30 #define EAP_TNC_FLAGS_START 0x20
31 #define EAP_TNC_VERSION_MASK 0x07
33 #define EAP_TNC_VERSION 1
36 static void * eap_tnc_init(struct eap_sm *sm)
38 struct eap_tnc_data *data;
39 struct eap_peer_config *config = eap_get_config(sm);
41 data = os_zalloc(sizeof(*data));
44 data->state = WAIT_START;
45 if (config && config->fragment_size)
46 data->fragment_size = config->fragment_size;
48 data->fragment_size = 1300;
49 data->tncc = tncc_init();
50 if (data->tncc == NULL) {
59 static void eap_tnc_deinit(struct eap_sm *sm, void *priv)
61 struct eap_tnc_data *data = priv;
63 wpabuf_free(data->in_buf);
64 wpabuf_free(data->out_buf);
65 tncc_deinit(data->tncc);
70 static struct wpabuf * eap_tnc_build_frag_ack(u8 id, u8 code)
74 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1, code, id);
76 wpa_printf(MSG_ERROR, "EAP-TNC: Failed to allocate memory "
80 wpabuf_put_u8(msg, EAP_TNC_VERSION); /* Flags */
82 wpa_printf(MSG_DEBUG, "EAP-TNC: Send fragment ack");
88 static struct wpabuf * eap_tnc_build_msg(struct eap_tnc_data *data,
89 struct eap_method_ret *ret, u8 id)
93 size_t send_len, plen;
96 wpa_printf(MSG_DEBUG, "EAP-TNC: Generating Response");
97 ret->allowNotifications = TRUE;
99 flags = EAP_TNC_VERSION;
100 send_len = wpabuf_len(data->out_buf) - data->out_used;
101 if (1 + send_len > data->fragment_size) {
102 send_len = data->fragment_size - 1;
103 flags |= EAP_TNC_FLAGS_MORE_FRAGMENTS;
104 if (data->out_used == 0) {
105 flags |= EAP_TNC_FLAGS_LENGTH_INCLUDED;
111 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
113 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, plen,
114 EAP_CODE_RESPONSE, id);
118 wpabuf_put_u8(resp, flags); /* Flags */
119 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
120 wpabuf_put_be32(resp, wpabuf_len(data->out_buf));
122 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
124 data->out_used += send_len;
126 ret->methodState = METHOD_MAY_CONT;
127 ret->decision = DECISION_FAIL;
129 if (data->out_used == wpabuf_len(data->out_buf)) {
130 wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
131 "(message sent completely)",
132 (unsigned long) send_len);
133 wpabuf_free(data->out_buf);
134 data->out_buf = NULL;
137 wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
138 "(%lu more to send)", (unsigned long) send_len,
139 (unsigned long) wpabuf_len(data->out_buf) -
141 data->state = WAIT_FRAG_ACK;
148 static int eap_tnc_process_cont(struct eap_tnc_data *data,
149 const u8 *buf, size_t len)
151 /* Process continuation of a pending message */
152 if (len > wpabuf_tailroom(data->in_buf)) {
153 wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment overflow");
158 wpabuf_put_data(data->in_buf, buf, len);
159 wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes, waiting for "
160 "%lu bytes more", (unsigned long) len,
161 (unsigned long) wpabuf_tailroom(data->in_buf));
167 static struct wpabuf * eap_tnc_process_fragment(struct eap_tnc_data *data,
168 struct eap_method_ret *ret,
171 const u8 *buf, size_t len)
173 /* Process a fragment that is not the last one of the message */
174 if (data->in_buf == NULL && !(flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)) {
175 wpa_printf(MSG_DEBUG, "EAP-TNC: No Message Length field in a "
176 "fragmented packet");
181 if (data->in_buf == NULL) {
182 /* First fragment of the message */
183 data->in_buf = wpabuf_alloc(message_length);
184 if (data->in_buf == NULL) {
185 wpa_printf(MSG_DEBUG, "EAP-TNC: No memory for "
190 wpabuf_put_data(data->in_buf, buf, len);
191 wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes in first "
192 "fragment, waiting for %lu bytes more",
194 (unsigned long) wpabuf_tailroom(data->in_buf));
197 return eap_tnc_build_frag_ack(id, EAP_CODE_RESPONSE);
201 static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
202 struct eap_method_ret *ret,
203 const struct wpabuf *reqData)
205 struct eap_tnc_data *data = priv;
211 char *start_buf, *end_buf;
212 size_t start_len, end_len;
215 u32 message_length = 0;
216 struct wpabuf tmpbuf;
218 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TNC, reqData, &len);
220 wpa_printf(MSG_INFO, "EAP-TNC: Invalid frame (pos=%p len=%lu)",
221 pos, (unsigned long) len);
226 id = eap_get_id(reqData);
231 flags = 0; /* fragment ack */
235 if (len > 0 && (flags & EAP_TNC_VERSION_MASK) != EAP_TNC_VERSION) {
236 wpa_printf(MSG_DEBUG, "EAP-TNC: Unsupported version %d",
237 flags & EAP_TNC_VERSION_MASK);
242 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED) {
244 wpa_printf(MSG_DEBUG, "EAP-TNC: Message underflow");
248 message_length = WPA_GET_BE32(pos);
251 if (message_length < (u32) (end - pos) ||
252 message_length > 75000) {
253 wpa_printf(MSG_DEBUG, "EAP-TNC: Invalid Message "
254 "Length (%d; %ld remaining in this msg)",
255 message_length, (long) (end - pos));
261 wpa_printf(MSG_DEBUG, "EAP-TNC: Received packet: Flags 0x%x "
262 "Message Length %u", flags, message_length);
264 if (data->state == WAIT_FRAG_ACK) {
266 wpa_printf(MSG_DEBUG, "EAP-TNC: Unexpected payload in "
267 "WAIT_FRAG_ACK state");
271 wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment acknowledged");
272 data->state = PROC_MSG;
273 return eap_tnc_build_msg(data, ret, id);
276 if (data->in_buf && eap_tnc_process_cont(data, pos, end - pos) < 0) {
281 if (flags & EAP_TNC_FLAGS_MORE_FRAGMENTS) {
282 return eap_tnc_process_fragment(data, ret, id, flags,
287 if (data->in_buf == NULL) {
288 /* Wrap unfragmented messages as wpabuf without extra copy */
289 wpabuf_set(&tmpbuf, pos, end - pos);
290 data->in_buf = &tmpbuf;
293 if (data->state == WAIT_START) {
294 if (!(flags & EAP_TNC_FLAGS_START)) {
295 wpa_printf(MSG_DEBUG, "EAP-TNC: Server did not use "
296 "start flag in the first message");
301 tncc_init_connection(data->tncc);
303 data->state = PROC_MSG;
305 enum tncc_process_res res;
307 if (flags & EAP_TNC_FLAGS_START) {
308 wpa_printf(MSG_DEBUG, "EAP-TNC: Server used start "
314 res = tncc_process_if_tnccs(data->tncc,
315 wpabuf_head(data->in_buf),
316 wpabuf_len(data->in_buf));
318 case TNCCS_PROCESS_ERROR:
321 case TNCCS_PROCESS_OK_NO_RECOMMENDATION:
322 case TNCCS_RECOMMENDATION_ERROR:
323 wpa_printf(MSG_DEBUG, "EAP-TNC: No "
324 "TNCCS-Recommendation received");
326 case TNCCS_RECOMMENDATION_ALLOW:
327 wpa_msg(sm->msg_ctx, MSG_INFO,
328 "TNC: Recommendation = allow");
331 case TNCCS_RECOMMENDATION_NONE:
332 wpa_msg(sm->msg_ctx, MSG_INFO,
333 "TNC: Recommendation = none");
336 case TNCCS_RECOMMENDATION_ISOLATE:
337 wpa_msg(sm->msg_ctx, MSG_INFO,
338 "TNC: Recommendation = isolate");
344 if (data->in_buf != &tmpbuf)
345 wpabuf_free(data->in_buf);
349 ret->methodState = METHOD_MAY_CONT;
350 ret->decision = DECISION_UNCOND_SUCC;
351 ret->allowNotifications = TRUE;
354 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1,
355 EAP_CODE_RESPONSE, eap_get_id(reqData));
359 wpabuf_put_u8(resp, EAP_TNC_VERSION);
360 wpa_printf(MSG_DEBUG, "EAP-TNC: TNCS done - reply with an "
361 "empty ACK message");
365 imc_len = tncc_total_send_len(data->tncc);
367 start_buf = tncc_if_tnccs_start(data->tncc);
368 if (start_buf == NULL)
370 start_len = os_strlen(start_buf);
371 end_buf = tncc_if_tnccs_end();
372 if (end_buf == NULL) {
376 end_len = os_strlen(end_buf);
378 rlen = start_len + imc_len + end_len;
379 resp = wpabuf_alloc(rlen);
386 wpabuf_put_data(resp, start_buf, start_len);
389 rpos1 = wpabuf_put(resp, 0);
390 rpos = tncc_copy_send_buf(data->tncc, rpos1);
391 wpabuf_put(resp, rpos - rpos1);
393 wpabuf_put_data(resp, end_buf, end_len);
396 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TNC: Response",
397 wpabuf_head(resp), wpabuf_len(resp));
399 data->out_buf = resp;
400 data->state = PROC_MSG;
401 return eap_tnc_build_msg(data, ret, id);
404 if (data->in_buf == &tmpbuf)
410 int eap_peer_tnc_register(void)
412 struct eap_method *eap;
414 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
415 EAP_VENDOR_IETF, EAP_TYPE_TNC, "TNC");
419 eap->init = eap_tnc_init;
420 eap->deinit = eap_tnc_deinit;
421 eap->process = eap_tnc_process;
423 return eap_peer_method_register(eap);