eap_tnc.c 10 KB

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