hostap2_imv.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Example IMV 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_IMVID my_id = -1;
  13. static TNC_TNCS_ReportMessageTypesPointer report_message_types = NULL;
  14. static TNC_TNCS_SendMessagePointer send_message = NULL;
  15. static TNC_TNCS_RequestHandshakeRetryPointer request_retry = NULL;
  16. TNC_TNCS_ProvideRecommendationPointer provide_recomm = NULL;
  17. static TNC_MessageType message_types[] =
  18. {
  19. (TNC_VENDORID_ANY << 8) | TNC_SUBTYPE_ANY
  20. };
  21. TNC_Result TNC_IMV_Initialize(
  22. /*in*/ TNC_IMVID imvID,
  23. /*in*/ TNC_Version minVersion,
  24. /*in*/ TNC_Version maxVersion,
  25. /*out*/ TNC_Version *pOutActualVersion)
  26. {
  27. wpa_printf(MSG_INFO,
  28. "IMV(hostap2) %s(imvID=%u, minVersion=%u, maxVersion=%u)",
  29. __func__, (unsigned) imvID, (unsigned) minVersion,
  30. (unsigned) maxVersion);
  31. if (initialized)
  32. return TNC_RESULT_ALREADY_INITIALIZED;
  33. if (minVersion < TNC_IFIMV_VERSION_1 ||
  34. maxVersion > TNC_IFIMV_VERSION_1)
  35. return TNC_RESULT_NO_COMMON_VERSION;
  36. if (!pOutActualVersion)
  37. return TNC_RESULT_INVALID_PARAMETER;
  38. *pOutActualVersion = TNC_IFIMV_VERSION_1;
  39. initialized = 1;
  40. my_id = imvID;
  41. return TNC_RESULT_SUCCESS;
  42. }
  43. TNC_Result TNC_IMV_NotifyConnectionChange(
  44. /*in*/ TNC_IMVID imvID,
  45. /*in*/ TNC_ConnectionID connectionID,
  46. /*in*/ TNC_ConnectionState newState)
  47. {
  48. wpa_printf(MSG_INFO,
  49. "IMV(hostap2) %s(imvID=%u, connectionID=%u, newState=%u)",
  50. __func__, (unsigned) imvID, (unsigned) connectionID,
  51. (unsigned) newState);
  52. if (!initialized)
  53. return TNC_RESULT_NOT_INITIALIZED;
  54. if (imvID != my_id)
  55. return TNC_RESULT_INVALID_PARAMETER;
  56. /* TODO: call TNC_TNCS_ProvideRecommendation */
  57. return TNC_RESULT_SUCCESS;
  58. }
  59. TNC_Result TNC_IMV_ReceiveMessage(
  60. /*in*/ TNC_IMVID imvID,
  61. /*in*/ TNC_ConnectionID connectionID,
  62. /*in*/ TNC_BufferReference message,
  63. /*in*/ TNC_UInt32 messageLength,
  64. /*in*/ TNC_MessageType messageType)
  65. {
  66. TNC_Result res;
  67. wpa_printf(MSG_INFO,
  68. "IMV(hostap2) %s(imvID=%u, connectionID=%u, messageType=%u)",
  69. __func__, (unsigned) imvID, (unsigned) connectionID,
  70. (unsigned) messageType);
  71. wpa_hexdump_ascii(MSG_INFO, "IMV(hostap2) message",
  72. message, messageLength);
  73. if (!send_message)
  74. return TNC_RESULT_FATAL;
  75. if (messageType == 1 && messageLength == 5 &&
  76. os_memcmp(message, "hello", 5) == 0) {
  77. char *msg = "hello";
  78. res = send_message(imvID, connectionID, msg, os_strlen(msg), 1);
  79. if (res != TNC_RESULT_SUCCESS)
  80. return res;
  81. }
  82. if (messageType == 1 && messageLength == 8 &&
  83. os_memcmp(message, "i'm fine", 8) == 0) {
  84. if (!provide_recomm)
  85. return TNC_RESULT_FATAL;
  86. res = provide_recomm(imvID, connectionID,
  87. TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
  88. TNC_IMV_EVALUATION_RESULT_COMPLIANT);
  89. if (res != TNC_RESULT_SUCCESS)
  90. return res;
  91. }
  92. return TNC_RESULT_SUCCESS;
  93. }
  94. TNC_Result TNC_IMV_SolicitRecommendation(
  95. /*in*/ TNC_IMVID imvID,
  96. /*in*/ TNC_ConnectionID connectionID)
  97. {
  98. wpa_printf(MSG_INFO, "IMV(hostap2) %s(imvID=%u, connectionID=%u)",
  99. __func__, (unsigned) imvID, (unsigned) connectionID);
  100. if (!initialized)
  101. return TNC_RESULT_NOT_INITIALIZED;
  102. if (imvID != my_id)
  103. return TNC_RESULT_INVALID_PARAMETER;
  104. /* TODO: call TNC_TNCS_ProvideRecommendation */
  105. return TNC_RESULT_SUCCESS;
  106. }
  107. TNC_Result TNC_IMV_BatchEnding(
  108. /*in*/ TNC_IMVID imvID,
  109. /*in*/ TNC_ConnectionID connectionID)
  110. {
  111. wpa_printf(MSG_INFO, "IMV(hostap2) %s(imvID=%u, connectionID=%u)",
  112. __func__, (unsigned) imvID, (unsigned) connectionID);
  113. return TNC_RESULT_SUCCESS;
  114. }
  115. TNC_Result TNC_IMV_Terminate(
  116. /*in*/ TNC_IMVID imvID)
  117. {
  118. wpa_printf(MSG_INFO, "IMV(hostap2) %s(imvID=%u)",
  119. __func__, (unsigned) imvID);
  120. return TNC_RESULT_SUCCESS;
  121. }
  122. TNC_Result TNC_IMV_ProvideBindFunction(
  123. /*in*/ TNC_IMVID imvID,
  124. /*in*/ TNC_TNCS_BindFunctionPointer bindFunction)
  125. {
  126. TNC_Result res;
  127. wpa_printf(MSG_INFO, "IMV(hostap2) %s(imvID=%u)",
  128. __func__, (unsigned) imvID);
  129. if (!initialized)
  130. return TNC_RESULT_NOT_INITIALIZED;
  131. if (imvID != my_id || !bindFunction)
  132. return TNC_RESULT_INVALID_PARAMETER;
  133. if (bindFunction(imvID, "TNC_TNCS_ReportMessageTypes",
  134. (void **) &report_message_types) !=
  135. TNC_RESULT_SUCCESS ||
  136. !report_message_types)
  137. return TNC_RESULT_FATAL;
  138. if (bindFunction(imvID, "TNC_TNCS_SendMessage",
  139. (void **) &send_message) != TNC_RESULT_SUCCESS ||
  140. !send_message)
  141. return TNC_RESULT_FATAL;
  142. if (bindFunction(imvID, "TNC_TNCS_RequestHandshakeRetry",
  143. (void **) &request_retry) != TNC_RESULT_SUCCESS ||
  144. !request_retry)
  145. return TNC_RESULT_FATAL;
  146. if (bindFunction(imvID, "TNC_TNCS_ProvideRecommendation",
  147. (void **) &provide_recomm) != TNC_RESULT_SUCCESS ||
  148. !provide_recomm)
  149. return TNC_RESULT_FATAL;
  150. res = report_message_types(imvID, message_types,
  151. ARRAY_SIZE(message_types));
  152. if (res != TNC_RESULT_SUCCESS)
  153. return res;
  154. return TNC_RESULT_SUCCESS;
  155. }