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.
17 enum { WAIT_START, PROC_MSG, WAIT_FRAG_ACK, DONE, FAIL } state;
18 struct tncc_data *tncc;
19 struct wpabuf *in_buf;
20 struct wpabuf *out_buf;
27 #define EAP_TNC_FLAGS_LENGTH_INCLUDED 0x80
28 #define EAP_TNC_FLAGS_MORE_FRAGMENTS 0x40
29 #define EAP_TNC_FLAGS_START 0x20
30 #define EAP_TNC_VERSION_MASK 0x07
32 #define EAP_TNC_VERSION 1
35 static void * eap_tnc_init(struct eap_sm *sm)
37 struct eap_tnc_data *data;
39 data = os_zalloc(sizeof(*data));
42 data->state = WAIT_START;
43 data->fragment_size = 1300;
44 data->tncc = tncc_init();
45 if (data->tncc == NULL) {
54 static void eap_tnc_deinit(struct eap_sm *sm, void *priv)
56 struct eap_tnc_data *data = priv;
58 wpabuf_free(data->in_buf);
59 wpabuf_free(data->out_buf);
60 tncc_deinit(data->tncc);
65 static struct wpabuf * eap_tnc_build_frag_ack(u8 id, u8 code)
69 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1, code, id);
71 wpa_printf(MSG_ERROR, "EAP-TNC: Failed to allocate memory "
75 wpabuf_put_u8(msg, EAP_TNC_VERSION); /* Flags */
77 wpa_printf(MSG_DEBUG, "EAP-TNC: Send fragment ack");
83 static struct wpabuf * eap_tnc_build_msg(struct eap_tnc_data *data,
84 struct eap_method_ret *ret, u8 id)
88 size_t send_len, plen;
91 wpa_printf(MSG_DEBUG, "EAP-TNC: Generating Response");
92 ret->allowNotifications = TRUE;
94 flags = EAP_TNC_VERSION;
95 send_len = wpabuf_len(data->out_buf) - data->out_used;
96 if (1 + send_len > data->fragment_size) {
97 send_len = data->fragment_size - 1;
98 flags |= EAP_TNC_FLAGS_MORE_FRAGMENTS;
99 if (data->out_used == 0) {
100 flags |= EAP_TNC_FLAGS_LENGTH_INCLUDED;
106 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
108 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, plen,
109 EAP_CODE_RESPONSE, id);
113 wpabuf_put_u8(resp, flags); /* Flags */
114 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
115 wpabuf_put_be32(resp, wpabuf_len(data->out_buf));
117 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
119 data->out_used += send_len;
121 ret->methodState = METHOD_MAY_CONT;
122 ret->decision = DECISION_FAIL;
124 if (data->out_used == wpabuf_len(data->out_buf)) {
125 wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
126 "(message sent completely)",
127 (unsigned long) send_len);
128 wpabuf_free(data->out_buf);
129 data->out_buf = NULL;
132 wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
133 "(%lu more to send)", (unsigned long) send_len,
134 (unsigned long) wpabuf_len(data->out_buf) -
136 data->state = WAIT_FRAG_ACK;
143 static int eap_tnc_process_cont(struct eap_tnc_data *data,
144 const u8 *buf, size_t len)
146 /* Process continuation of a pending message */
147 if (len > wpabuf_tailroom(data->in_buf)) {
148 wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment overflow");
153 wpabuf_put_data(data->in_buf, buf, len);
154 wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes, waiting for "
155 "%lu bytes more", (unsigned long) len,
156 (unsigned long) wpabuf_tailroom(data->in_buf));
162 static struct wpabuf * eap_tnc_process_fragment(struct eap_tnc_data *data,
163 struct eap_method_ret *ret,
166 const u8 *buf, size_t len)
168 /* Process a fragment that is not the last one of the message */
169 if (data->in_buf == NULL && !(flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)) {
170 wpa_printf(MSG_DEBUG, "EAP-TNC: No Message Length field in a "
171 "fragmented packet");
176 if (data->in_buf == NULL) {
177 /* First fragment of the message */
178 data->in_buf = wpabuf_alloc(message_length);
179 if (data->in_buf == NULL) {
180 wpa_printf(MSG_DEBUG, "EAP-TNC: No memory for "
185 wpabuf_put_data(data->in_buf, buf, len);
186 wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes in first "
187 "fragment, waiting for %lu bytes more",
189 (unsigned long) wpabuf_tailroom(data->in_buf));
192 return eap_tnc_build_frag_ack(id, EAP_CODE_RESPONSE);
196 static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
197 struct eap_method_ret *ret,
198 const struct wpabuf *reqData)
200 struct eap_tnc_data *data = priv;
206 char *start_buf, *end_buf;
207 size_t start_len, end_len;
210 u32 message_length = 0;
211 struct wpabuf tmpbuf;
213 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TNC, reqData, &len);
215 wpa_printf(MSG_INFO, "EAP-TNC: Invalid frame (pos=%p len=%lu)",
216 pos, (unsigned long) len);
221 id = eap_get_id(reqData);
226 flags = 0; /* fragment ack */
230 if (len > 0 && (flags & EAP_TNC_VERSION_MASK) != EAP_TNC_VERSION) {
231 wpa_printf(MSG_DEBUG, "EAP-TNC: Unsupported version %d",
232 flags & EAP_TNC_VERSION_MASK);
237 if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED) {
239 wpa_printf(MSG_DEBUG, "EAP-TNC: Message underflow");
243 message_length = WPA_GET_BE32(pos);
246 if (message_length < (u32) (end - pos) ||
247 message_length > 75000) {
248 wpa_printf(MSG_DEBUG, "EAP-TNC: Invalid Message "
249 "Length (%d; %ld remaining in this msg)",
250 message_length, (long) (end - pos));
256 wpa_printf(MSG_DEBUG, "EAP-TNC: Received packet: Flags 0x%x "
257 "Message Length %u", flags, message_length);
259 if (data->state == WAIT_FRAG_ACK) {
261 wpa_printf(MSG_DEBUG, "EAP-TNC: Unexpected payload in "
262 "WAIT_FRAG_ACK state");
266 wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment acknowledged");
267 data->state = PROC_MSG;
268 return eap_tnc_build_msg(data, ret, id);
271 if (data->in_buf && eap_tnc_process_cont(data, pos, end - pos) < 0) {
276 if (flags & EAP_TNC_FLAGS_MORE_FRAGMENTS) {
277 return eap_tnc_process_fragment(data, ret, id, flags,
282 if (data->in_buf == NULL) {
283 /* Wrap unfragmented messages as wpabuf without extra copy */
284 wpabuf_set(&tmpbuf, pos, end - pos);
285 data->in_buf = &tmpbuf;
288 if (data->state == WAIT_START) {
289 if (!(flags & EAP_TNC_FLAGS_START)) {
290 wpa_printf(MSG_DEBUG, "EAP-TNC: Server did not use "
291 "start flag in the first message");
296 tncc_init_connection(data->tncc);
298 data->state = PROC_MSG;
300 enum tncc_process_res res;
302 if (flags & EAP_TNC_FLAGS_START) {
303 wpa_printf(MSG_DEBUG, "EAP-TNC: Server used start "
309 res = tncc_process_if_tnccs(data->tncc,
310 wpabuf_head(data->in_buf),
311 wpabuf_len(data->in_buf));
313 case TNCCS_PROCESS_ERROR:
316 case TNCCS_PROCESS_OK_NO_RECOMMENDATION:
317 case TNCCS_RECOMMENDATION_ERROR:
318 wpa_printf(MSG_DEBUG, "EAP-TNC: No "
319 "TNCCS-Recommendation received");
321 case TNCCS_RECOMMENDATION_ALLOW:
322 wpa_msg(sm->msg_ctx, MSG_INFO,
323 "TNC: Recommendation = allow");
326 case TNCCS_RECOMMENDATION_NONE:
327 wpa_msg(sm->msg_ctx, MSG_INFO,
328 "TNC: Recommendation = none");
331 case TNCCS_RECOMMENDATION_ISOLATE:
332 wpa_msg(sm->msg_ctx, MSG_INFO,
333 "TNC: Recommendation = isolate");
339 if (data->in_buf != &tmpbuf)
340 wpabuf_free(data->in_buf);
344 ret->methodState = METHOD_MAY_CONT;
345 ret->decision = DECISION_UNCOND_SUCC;
346 ret->allowNotifications = TRUE;
349 data->state = PROC_MSG;
350 return eap_tnc_build_msg(data, ret, id);
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;
415 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
416 EAP_VENDOR_IETF, EAP_TYPE_TNC, "TNC");
420 eap->init = eap_tnc_init;
421 eap->deinit = eap_tnc_deinit;
422 eap->process = eap_tnc_process;
424 ret = eap_peer_method_register(eap);
426 eap_peer_method_free(eap);