Line data Source code
1 : /*
2 : * EAP peer method: EAP-TNC (Trusted Network Connect)
3 : * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "common.h"
12 : #include "eap_i.h"
13 : #include "tncc.h"
14 :
15 :
16 : struct eap_tnc_data {
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;
21 : size_t out_used;
22 : size_t fragment_size;
23 : };
24 :
25 :
26 : /* EAP-TNC Flags */
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
31 :
32 : #define EAP_TNC_VERSION 1
33 :
34 :
35 3 : static void * eap_tnc_init(struct eap_sm *sm)
36 : {
37 : struct eap_tnc_data *data;
38 :
39 3 : data = os_zalloc(sizeof(*data));
40 3 : if (data == NULL)
41 0 : return NULL;
42 3 : data->state = WAIT_START;
43 3 : data->fragment_size = 1300;
44 3 : data->tncc = tncc_init();
45 3 : if (data->tncc == NULL) {
46 0 : os_free(data);
47 0 : return NULL;
48 : }
49 :
50 3 : return data;
51 : }
52 :
53 :
54 3 : static void eap_tnc_deinit(struct eap_sm *sm, void *priv)
55 : {
56 3 : struct eap_tnc_data *data = priv;
57 :
58 3 : wpabuf_free(data->in_buf);
59 3 : wpabuf_free(data->out_buf);
60 3 : tncc_deinit(data->tncc);
61 3 : os_free(data);
62 3 : }
63 :
64 :
65 17 : static struct wpabuf * eap_tnc_build_frag_ack(u8 id, u8 code)
66 : {
67 : struct wpabuf *msg;
68 :
69 17 : msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1, code, id);
70 17 : if (msg == NULL) {
71 0 : wpa_printf(MSG_ERROR, "EAP-TNC: Failed to allocate memory "
72 : "for fragment ack");
73 0 : return NULL;
74 : }
75 17 : wpabuf_put_u8(msg, EAP_TNC_VERSION); /* Flags */
76 :
77 17 : wpa_printf(MSG_DEBUG, "EAP-TNC: Send fragment ack");
78 :
79 17 : return msg;
80 : }
81 :
82 :
83 6 : static struct wpabuf * eap_tnc_build_msg(struct eap_tnc_data *data,
84 : struct eap_method_ret *ret, u8 id)
85 : {
86 : struct wpabuf *resp;
87 : u8 flags;
88 : size_t send_len, plen;
89 :
90 6 : ret->ignore = FALSE;
91 6 : wpa_printf(MSG_DEBUG, "EAP-TNC: Generating Response");
92 6 : ret->allowNotifications = TRUE;
93 :
94 6 : flags = EAP_TNC_VERSION;
95 6 : send_len = wpabuf_len(data->out_buf) - data->out_used;
96 6 : if (1 + send_len > data->fragment_size) {
97 0 : send_len = data->fragment_size - 1;
98 0 : flags |= EAP_TNC_FLAGS_MORE_FRAGMENTS;
99 0 : if (data->out_used == 0) {
100 0 : flags |= EAP_TNC_FLAGS_LENGTH_INCLUDED;
101 0 : send_len -= 4;
102 : }
103 : }
104 :
105 6 : plen = 1 + send_len;
106 6 : if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
107 0 : plen += 4;
108 6 : resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, plen,
109 : EAP_CODE_RESPONSE, id);
110 6 : if (resp == NULL)
111 0 : return NULL;
112 :
113 6 : wpabuf_put_u8(resp, flags); /* Flags */
114 6 : if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)
115 0 : wpabuf_put_be32(resp, wpabuf_len(data->out_buf));
116 :
117 6 : wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
118 : send_len);
119 6 : data->out_used += send_len;
120 :
121 6 : ret->methodState = METHOD_MAY_CONT;
122 6 : ret->decision = DECISION_FAIL;
123 :
124 6 : if (data->out_used == wpabuf_len(data->out_buf)) {
125 6 : wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
126 : "(message sent completely)",
127 : (unsigned long) send_len);
128 6 : wpabuf_free(data->out_buf);
129 6 : data->out_buf = NULL;
130 6 : data->out_used = 0;
131 : } else {
132 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Sending out %lu bytes "
133 : "(%lu more to send)", (unsigned long) send_len,
134 0 : (unsigned long) wpabuf_len(data->out_buf) -
135 0 : data->out_used);
136 0 : data->state = WAIT_FRAG_ACK;
137 : }
138 :
139 6 : return resp;
140 : }
141 :
142 :
143 17 : static int eap_tnc_process_cont(struct eap_tnc_data *data,
144 : const u8 *buf, size_t len)
145 : {
146 : /* Process continuation of a pending message */
147 17 : if (len > wpabuf_tailroom(data->in_buf)) {
148 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment overflow");
149 0 : data->state = FAIL;
150 0 : return -1;
151 : }
152 :
153 17 : wpabuf_put_data(data->in_buf, buf, len);
154 17 : wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes, waiting for "
155 : "%lu bytes more", (unsigned long) len,
156 17 : (unsigned long) wpabuf_tailroom(data->in_buf));
157 :
158 17 : return 0;
159 : }
160 :
161 :
162 17 : static struct wpabuf * eap_tnc_process_fragment(struct eap_tnc_data *data,
163 : struct eap_method_ret *ret,
164 : u8 id, u8 flags,
165 : u32 message_length,
166 : const u8 *buf, size_t len)
167 : {
168 : /* Process a fragment that is not the last one of the message */
169 17 : if (data->in_buf == NULL && !(flags & EAP_TNC_FLAGS_LENGTH_INCLUDED)) {
170 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: No Message Length field in a "
171 : "fragmented packet");
172 0 : ret->ignore = TRUE;
173 0 : return NULL;
174 : }
175 :
176 17 : if (data->in_buf == NULL) {
177 : /* First fragment of the message */
178 2 : data->in_buf = wpabuf_alloc(message_length);
179 2 : if (data->in_buf == NULL) {
180 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: No memory for "
181 : "message");
182 0 : ret->ignore = TRUE;
183 0 : return NULL;
184 : }
185 2 : wpabuf_put_data(data->in_buf, buf, len);
186 2 : wpa_printf(MSG_DEBUG, "EAP-TNC: Received %lu bytes in first "
187 : "fragment, waiting for %lu bytes more",
188 : (unsigned long) len,
189 2 : (unsigned long) wpabuf_tailroom(data->in_buf));
190 : }
191 :
192 17 : return eap_tnc_build_frag_ack(id, EAP_CODE_RESPONSE);
193 : }
194 :
195 :
196 26 : static struct wpabuf * eap_tnc_process(struct eap_sm *sm, void *priv,
197 : struct eap_method_ret *ret,
198 : const struct wpabuf *reqData)
199 : {
200 26 : struct eap_tnc_data *data = priv;
201 : struct wpabuf *resp;
202 : const u8 *pos, *end;
203 : u8 *rpos, *rpos1;
204 : size_t len, rlen;
205 : size_t imc_len;
206 : char *start_buf, *end_buf;
207 : size_t start_len, end_len;
208 26 : int tncs_done = 0;
209 : u8 flags, id;
210 26 : u32 message_length = 0;
211 : struct wpabuf tmpbuf;
212 :
213 26 : pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TNC, reqData, &len);
214 26 : if (pos == NULL) {
215 0 : wpa_printf(MSG_INFO, "EAP-TNC: Invalid frame (pos=%p len=%lu)",
216 : pos, (unsigned long) len);
217 0 : ret->ignore = TRUE;
218 0 : return NULL;
219 : }
220 :
221 26 : id = eap_get_id(reqData);
222 :
223 26 : end = pos + len;
224 :
225 26 : if (len == 0)
226 0 : flags = 0; /* fragment ack */
227 : else
228 26 : flags = *pos++;
229 :
230 26 : if (len > 0 && (flags & EAP_TNC_VERSION_MASK) != EAP_TNC_VERSION) {
231 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Unsupported version %d",
232 : flags & EAP_TNC_VERSION_MASK);
233 0 : ret->ignore = TRUE;
234 0 : return NULL;
235 : }
236 :
237 26 : if (flags & EAP_TNC_FLAGS_LENGTH_INCLUDED) {
238 2 : if (end - pos < 4) {
239 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Message underflow");
240 0 : ret->ignore = TRUE;
241 0 : return NULL;
242 : }
243 2 : message_length = WPA_GET_BE32(pos);
244 2 : pos += 4;
245 :
246 2 : if (message_length < (u32) (end - pos) ||
247 : message_length > 75000) {
248 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Invalid Message "
249 : "Length (%d; %ld remaining in this msg)",
250 : message_length, (long) (end - pos));
251 0 : ret->ignore = TRUE;
252 0 : return NULL;
253 : }
254 : }
255 :
256 26 : wpa_printf(MSG_DEBUG, "EAP-TNC: Received packet: Flags 0x%x "
257 : "Message Length %u", flags, message_length);
258 :
259 26 : if (data->state == WAIT_FRAG_ACK) {
260 0 : if (len > 1) {
261 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Unexpected payload in "
262 : "WAIT_FRAG_ACK state");
263 0 : ret->ignore = TRUE;
264 0 : return NULL;
265 : }
266 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Fragment acknowledged");
267 0 : data->state = PROC_MSG;
268 0 : return eap_tnc_build_msg(data, ret, id);
269 : }
270 :
271 26 : if (data->in_buf && eap_tnc_process_cont(data, pos, end - pos) < 0) {
272 0 : ret->ignore = TRUE;
273 0 : return NULL;
274 : }
275 :
276 26 : if (flags & EAP_TNC_FLAGS_MORE_FRAGMENTS) {
277 17 : return eap_tnc_process_fragment(data, ret, id, flags,
278 : message_length, pos,
279 17 : end - pos);
280 : }
281 :
282 9 : if (data->in_buf == NULL) {
283 : /* Wrap unfragmented messages as wpabuf without extra copy */
284 7 : wpabuf_set(&tmpbuf, pos, end - pos);
285 7 : data->in_buf = &tmpbuf;
286 : }
287 :
288 9 : if (data->state == WAIT_START) {
289 3 : if (!(flags & EAP_TNC_FLAGS_START)) {
290 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Server did not use "
291 : "start flag in the first message");
292 0 : ret->ignore = TRUE;
293 0 : goto fail;
294 : }
295 :
296 3 : tncc_init_connection(data->tncc);
297 :
298 3 : data->state = PROC_MSG;
299 : } else {
300 : enum tncc_process_res res;
301 :
302 6 : if (flags & EAP_TNC_FLAGS_START) {
303 0 : wpa_printf(MSG_DEBUG, "EAP-TNC: Server used start "
304 : "flag again");
305 0 : ret->ignore = TRUE;
306 0 : goto fail;
307 : }
308 :
309 12 : res = tncc_process_if_tnccs(data->tncc,
310 6 : wpabuf_head(data->in_buf),
311 6 : wpabuf_len(data->in_buf));
312 6 : switch (res) {
313 : case TNCCS_PROCESS_ERROR:
314 0 : ret->ignore = TRUE;
315 0 : goto fail;
316 : case TNCCS_PROCESS_OK_NO_RECOMMENDATION:
317 : case TNCCS_RECOMMENDATION_ERROR:
318 3 : wpa_printf(MSG_DEBUG, "EAP-TNC: No "
319 : "TNCCS-Recommendation received");
320 3 : break;
321 : case TNCCS_RECOMMENDATION_ALLOW:
322 3 : wpa_msg(sm->msg_ctx, MSG_INFO,
323 : "TNC: Recommendation = allow");
324 3 : tncs_done = 1;
325 3 : break;
326 : case TNCCS_RECOMMENDATION_NONE:
327 0 : wpa_msg(sm->msg_ctx, MSG_INFO,
328 : "TNC: Recommendation = none");
329 0 : tncs_done = 1;
330 0 : break;
331 : case TNCCS_RECOMMENDATION_ISOLATE:
332 0 : wpa_msg(sm->msg_ctx, MSG_INFO,
333 : "TNC: Recommendation = isolate");
334 0 : tncs_done = 1;
335 0 : break;
336 : }
337 : }
338 :
339 9 : if (data->in_buf != &tmpbuf)
340 2 : wpabuf_free(data->in_buf);
341 9 : data->in_buf = NULL;
342 :
343 9 : ret->ignore = FALSE;
344 9 : ret->methodState = METHOD_MAY_CONT;
345 9 : ret->decision = DECISION_UNCOND_SUCC;
346 9 : ret->allowNotifications = TRUE;
347 :
348 9 : if (data->out_buf) {
349 0 : data->state = PROC_MSG;
350 0 : return eap_tnc_build_msg(data, ret, id);
351 : }
352 :
353 9 : if (tncs_done) {
354 3 : resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TNC, 1,
355 3 : EAP_CODE_RESPONSE, eap_get_id(reqData));
356 3 : if (resp == NULL)
357 0 : return NULL;
358 :
359 3 : wpabuf_put_u8(resp, EAP_TNC_VERSION);
360 3 : wpa_printf(MSG_DEBUG, "EAP-TNC: TNCS done - reply with an "
361 : "empty ACK message");
362 3 : return resp;
363 : }
364 :
365 6 : imc_len = tncc_total_send_len(data->tncc);
366 :
367 6 : start_buf = tncc_if_tnccs_start(data->tncc);
368 6 : if (start_buf == NULL)
369 0 : return NULL;
370 6 : start_len = os_strlen(start_buf);
371 6 : end_buf = tncc_if_tnccs_end();
372 6 : if (end_buf == NULL) {
373 0 : os_free(start_buf);
374 0 : return NULL;
375 : }
376 6 : end_len = os_strlen(end_buf);
377 :
378 6 : rlen = start_len + imc_len + end_len;
379 6 : resp = wpabuf_alloc(rlen);
380 6 : if (resp == NULL) {
381 0 : os_free(start_buf);
382 0 : os_free(end_buf);
383 0 : return NULL;
384 : }
385 :
386 6 : wpabuf_put_data(resp, start_buf, start_len);
387 6 : os_free(start_buf);
388 :
389 6 : rpos1 = wpabuf_put(resp, 0);
390 6 : rpos = tncc_copy_send_buf(data->tncc, rpos1);
391 6 : wpabuf_put(resp, rpos - rpos1);
392 :
393 6 : wpabuf_put_data(resp, end_buf, end_len);
394 6 : os_free(end_buf);
395 :
396 6 : wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TNC: Response",
397 : wpabuf_head(resp), wpabuf_len(resp));
398 :
399 6 : data->out_buf = resp;
400 6 : data->state = PROC_MSG;
401 6 : return eap_tnc_build_msg(data, ret, id);
402 :
403 : fail:
404 0 : if (data->in_buf == &tmpbuf)
405 0 : data->in_buf = NULL;
406 0 : return NULL;
407 : }
408 :
409 :
410 30 : int eap_peer_tnc_register(void)
411 : {
412 : struct eap_method *eap;
413 : int ret;
414 :
415 30 : eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
416 : EAP_VENDOR_IETF, EAP_TYPE_TNC, "TNC");
417 30 : if (eap == NULL)
418 0 : return -1;
419 :
420 30 : eap->init = eap_tnc_init;
421 30 : eap->deinit = eap_tnc_deinit;
422 30 : eap->process = eap_tnc_process;
423 :
424 30 : ret = eap_peer_method_register(eap);
425 30 : if (ret)
426 0 : eap_peer_method_free(eap);
427 30 : return ret;
428 : }
|