hostap2_imc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Example IMC for TNC testing
  3. * Copyright (c) 2014, 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 "common/tnc.h"
  11. static int initialized = 0;
  12. static TNC_IMCID my_id = -1;
  13. static TNC_TNCC_SendMessagePointer send_message = NULL;
  14. static TNC_TNCC_ReportMessageTypesPointer report_message_types = NULL;
  15. static TNC_TNCC_RequestHandshakeRetryPointer request_retry = NULL;
  16. static TNC_MessageType message_types[] =
  17. {
  18. (TNC_VENDORID_ANY << 8) | TNC_SUBTYPE_ANY
  19. };
  20. TNC_Result TNC_IMC_Initialize(
  21. /*in*/ TNC_IMCID imcID,
  22. /*in*/ TNC_Version minVersion,
  23. /*in*/ TNC_Version maxVersion,
  24. /*out*/ TNC_Version *pOutActualVersion)
  25. {
  26. wpa_printf(MSG_INFO,
  27. "IMC(hostap2) %s(imcID=%u, minVersion=%u, maxVersion=%u)",
  28. __func__, (unsigned) imcID, (unsigned) minVersion,
  29. (unsigned) maxVersion);
  30. if (initialized)
  31. return TNC_RESULT_ALREADY_INITIALIZED;
  32. if (minVersion < TNC_IFIMC_VERSION_1 ||
  33. maxVersion > TNC_IFIMC_VERSION_1)
  34. return TNC_RESULT_NO_COMMON_VERSION;
  35. if (!pOutActualVersion)
  36. return TNC_RESULT_INVALID_PARAMETER;
  37. *pOutActualVersion = TNC_IFIMC_VERSION_1;
  38. my_id = imcID;
  39. initialized = 1;
  40. return TNC_RESULT_SUCCESS;
  41. }
  42. TNC_Result TNC_IMC_BeginHandshake(
  43. /*in*/ TNC_IMCID imcID,
  44. /*in*/ TNC_ConnectionID connectionID)
  45. {
  46. char *msg = "hello";
  47. TNC_Result res;
  48. wpa_printf(MSG_INFO, "IMC(hostap2) %s(imcID=%u, connectionID=%u)",
  49. __func__, (unsigned) imcID, (unsigned) connectionID);
  50. if (!initialized)
  51. return TNC_RESULT_NOT_INITIALIZED;
  52. if (imcID != my_id)
  53. return TNC_RESULT_INVALID_PARAMETER;
  54. if (!send_message)
  55. return TNC_RESULT_FATAL;
  56. res = send_message(imcID, connectionID, msg, os_strlen(msg), 1);
  57. if (res != TNC_RESULT_SUCCESS)
  58. return res;
  59. return TNC_RESULT_SUCCESS;
  60. }
  61. TNC_Result TNC_IMC_ProvideBindFunction(
  62. /*in*/ TNC_IMCID imcID,
  63. /*in*/ TNC_TNCC_BindFunctionPointer bindFunction)
  64. {
  65. TNC_Result res;
  66. wpa_printf(MSG_INFO, "IMC(hostap2) %s(imcID=%u)",
  67. __func__, (unsigned) imcID);
  68. if (!initialized)
  69. return TNC_RESULT_NOT_INITIALIZED;
  70. if (imcID != my_id || !bindFunction)
  71. return TNC_RESULT_INVALID_PARAMETER;
  72. if (bindFunction(imcID, "TNC_TNCC_SendMessage",
  73. (void **) &send_message) != TNC_RESULT_SUCCESS ||
  74. !send_message)
  75. return TNC_RESULT_FATAL;
  76. if (bindFunction(imcID, "TNC_TNCC_ReportMessageTypes",
  77. (void **) &report_message_types) !=
  78. TNC_RESULT_SUCCESS ||
  79. !report_message_types)
  80. return TNC_RESULT_FATAL;
  81. if (bindFunction(imcID, "TNC_TNCC_RequestHandshakeRetry",
  82. (void **) &request_retry) != TNC_RESULT_SUCCESS ||
  83. !request_retry)
  84. return TNC_RESULT_FATAL;
  85. res = report_message_types(imcID, message_types,
  86. ARRAY_SIZE(message_types));
  87. if (res != TNC_RESULT_SUCCESS)
  88. return res;
  89. return TNC_RESULT_SUCCESS;
  90. }
  91. TNC_Result TNC_IMC_NotifyConnectionChange(
  92. /*in*/ TNC_IMCID imcID,
  93. /*in*/ TNC_ConnectionID connectionID,
  94. /*in*/ TNC_ConnectionState newState)
  95. {
  96. wpa_printf(MSG_INFO,
  97. "IMC(hostap2) %s(imcID=%u, connectionID=%u, newState=%u)",
  98. __func__, (unsigned) imcID, (unsigned) connectionID,
  99. (unsigned) newState);
  100. return TNC_RESULT_SUCCESS;
  101. }
  102. TNC_Result TNC_IMC_ReceiveMessage(
  103. /*in*/ TNC_IMCID imcID,
  104. /*in*/ TNC_ConnectionID connectionID,
  105. /*in*/ TNC_BufferReference message,
  106. /*in*/ TNC_UInt32 messageLength,
  107. /*in*/ TNC_MessageType messageType)
  108. {
  109. TNC_Result res;
  110. wpa_printf(MSG_INFO,
  111. "IMC(hostap2) %s(imcID=%u, connectionID=%u, messageType=%u)",
  112. __func__, (unsigned) imcID, (unsigned) connectionID,
  113. (unsigned) messageType);
  114. wpa_hexdump_ascii(MSG_INFO, "IMC(hostap2) message",
  115. message, messageLength);
  116. if (messageType == 1 && messageLength == 5 &&
  117. os_memcmp(message, "hello", 5) == 0) {
  118. char *msg = "i'm fine";
  119. res = send_message(imcID, connectionID, msg, os_strlen(msg), 1);
  120. if (res != TNC_RESULT_SUCCESS)
  121. return res;
  122. }
  123. return TNC_RESULT_SUCCESS;
  124. }
  125. TNC_Result TNC_IMC_BatchEnding(
  126. /*in*/ TNC_IMCID imcID,
  127. /*in*/ TNC_ConnectionID connectionID)
  128. {
  129. wpa_printf(MSG_INFO, "IMC(hostap2) %s(imcID=%u, connectionID=%u)",
  130. __func__, (unsigned) imcID, (unsigned) connectionID);
  131. return TNC_RESULT_SUCCESS;
  132. }
  133. TNC_Result TNC_IMC_Terminate(
  134. /*in*/ TNC_IMCID imcID)
  135. {
  136. wpa_printf(MSG_INFO, "IMC(hostap2) %s(imcID=%u)",
  137. __func__, (unsigned) imcID);
  138. return TNC_RESULT_SUCCESS;
  139. }