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