ExceptionsTests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * Changes from Qualcomm Innovation Center are provided under the following license:
  30. *
  31. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted (subject to the limitations in the
  35. * disclaimer below) provided that the following conditions are met:
  36. *
  37. * * Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. *
  40. * * Redistributions in binary form must reproduce the above
  41. * copyright notice, this list of conditions and the following
  42. * disclaimer in the documentation and/or other materials provided
  43. * with the distribution.
  44. *
  45. * * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  46. * contributors may be used to endorse or promote products derived
  47. * from this software without specific prior written permission.
  48. *
  49. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  50. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  51. * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  52. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  53. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  54. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  55. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  57. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  58. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  59. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  60. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  61. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  62. */
  63. #include "RoutingDriverWrapper.h"
  64. #include "HeaderInsertion.h"
  65. #include "Filtering.h"
  66. #include "IPAFilteringTable.h"
  67. #include "TestsUtils.h"
  68. #include "ExceptionsTestFixture.h"
  69. #include "IPv4Packet.h"
  70. #include <string.h>
  71. #define MAX_SENT_BUFFER_SIZE 1500
  72. #define MAX_RECEIVE_BUFFER_SIZE 1500
  73. #define VALIDATE_WITH_MSG_AND_RETVAL(bRetVal,msg) \
  74. if (false == bRetVal){ \
  75. LOG_MSG_ERROR(msg); \
  76. return false; \
  77. }
  78. using namespace IPA;
  79. ///////////////////////////////////////////////////////////////////////////////
  80. class ExceptionsTestNonIpPacket: public ExceptionsTestFixture {
  81. public:
  82. //The packet size to be sent
  83. size_t m_nPacketSize;
  84. //A buffer to hold the non-IP(V4/V6) packet
  85. Byte *m_pSendBuffer;
  86. ///////////////////////////////////////////////////////////////////////////
  87. //Set the tests name and description
  88. ExceptionsTestNonIpPacket() :
  89. m_nPacketSize(0), m_pSendBuffer(NULL) {
  90. m_name = "ExceptionsTestNonIpPacket";
  91. m_description =
  92. "Create a non-IP packet(version!=4 && version !=6) and \
  93. expect exception from Filter block";
  94. }
  95. ///////////////////////////////////////////////////////////////////////////
  96. virtual bool Run() {
  97. bool bRetVal = true;
  98. Byte *pReceiveBuffer = new Byte[MAX_RECEIVE_BUFFER_SIZE];
  99. //Send the non-IPV4/IPV6 packet to the IPA
  100. LOG_MSG_DEBUG("Send the non-IPV4/IPV6 packet to the IPA");
  101. size_t nBytesSent = m_USB1ToIpaPipe.Send(m_pSendBuffer, m_nPacketSize);
  102. if (nBytesSent != m_nPacketSize) {
  103. LOG_MSG_ERROR("Not all data was sent into the IPA");
  104. return false;
  105. }
  106. //Read from the exception pipe(from IPA to A5) - try to read as much as we can
  107. size_t nBytesRead = m_IpaToA5ExceptionPipe.Receive(pReceiveBuffer,
  108. MAX_RECEIVE_BUFFER_SIZE);
  109. if (nBytesRead != nBytesSent) {
  110. LOG_MSG_ERROR("Not all data was read:");
  111. print_buff(pReceiveBuffer, nBytesRead);
  112. return false;
  113. }
  114. //check the exception packet against the one that we sent
  115. bRetVal = !memcmp(m_pSendBuffer, pReceiveBuffer, nBytesSent);
  116. if (false == bRetVal) {
  117. LOG_MSG_ERROR("Received packet is not equal, Received:");
  118. print_buff(pReceiveBuffer, nBytesRead);
  119. LOG_MSG_ERROR("Received packet is not equal, Sent:");
  120. print_buff(m_pSendBuffer, m_nPacketSize);
  121. return false;
  122. }
  123. return true;
  124. }
  125. ///////////////////////////////////////////////////////////////////////////
  126. //build the non-IP packet
  127. virtual bool Setup() {
  128. bool bRetVal = true;
  129. m_pSendBuffer = new Byte[MAX_SENT_BUFFER_SIZE];
  130. //Load some default IPV4 packet and save its size
  131. m_nPacketSize = MAX_SENT_BUFFER_SIZE; //This parameter is In/Out
  132. bRetVal = LoadDefaultPacket(IPA_IP_v4, m_pSendBuffer, m_nPacketSize);
  133. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Load failed");
  134. //Set the version field to non-IPV4/IPV6(version = 5)
  135. m_pSendBuffer[0] &= 0x0F;
  136. m_pSendBuffer[0] |= 0x50;
  137. //initialize Pipes
  138. bRetVal = m_USB1ToIpaPipe.Init();
  139. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  140. bRetVal = m_IpaToA5ExceptionPipe.Init();
  141. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  142. return true;
  143. }
  144. ///////////////////////////////////////////////////////////////////////////
  145. virtual bool Teardown() {
  146. bool bRetVal = true;
  147. delete[] m_pSendBuffer;
  148. m_USB1ToIpaPipe.Destroy();
  149. m_IpaToA5ExceptionPipe.Destroy();
  150. return bRetVal;
  151. }
  152. ///////////////////////////////////////////////////////////////////////////
  153. };
  154. //ExceptionTestNoneIpPacket
  155. ///////////////////////////////////////////////////////////////////////////////
  156. ///////////////////////////////////////////////////////////////////////////////
  157. ///////////////////////////////////////////////////////////////////////////////
  158. ///////////////////////////////////////////////////////////////////////////////
  159. class ExceptionsTestFragmentedException: public ExceptionsTestFixture {
  160. public:
  161. //The packet size to be sent
  162. size_t m_nPacketSize;
  163. //A buffer to hold the non-IP(V4/V6) packet
  164. Byte *m_pSendBuffer;
  165. Byte *m_pReceiveBuffer;
  166. ///////////////////////////////////////////////////////////////////////////
  167. //Set the tests name and description
  168. ExceptionsTestFragmentedException():m_nPacketSize(0), m_pSendBuffer(NULL),
  169. m_pReceiveBuffer(NULL){
  170. m_name = "ExceptionsTestFragmentedException";
  171. m_description =
  172. "Send IP packet with MF set, create global Filter rule \
  173. that will hit it as Exception";
  174. }
  175. ///////////////////////////////////////////////////////////////////////////
  176. virtual bool Run() {
  177. bool bRetVal = true;
  178. //configuring the Filter block to catch the fragmented packet:
  179. ConfigureFilterGlobalRuleForMF();
  180. //Send the non-IPV4/IPV6 packet to the IPA
  181. LOG_MSG_DEBUG("Send the IP packet with the MF bit set(size = %d)", m_nPacketSize);
  182. size_t nBytesSent = m_USB1ToIpaPipe.Send(m_pSendBuffer, m_nPacketSize);
  183. if (nBytesSent != m_nPacketSize) {
  184. LOG_MSG_ERROR("Not all data was sent into the IPA(only %d)", nBytesSent);
  185. return false;
  186. }
  187. //Read from the exception pipe(from IPA to A5) - try to read as much as we can
  188. size_t nBytesRead = m_IpaToA5ExceptionPipe.Receive(m_pReceiveBuffer,
  189. MAX_RECEIVE_BUFFER_SIZE);
  190. if (nBytesRead != nBytesSent) {
  191. LOG_MSG_ERROR("Not all data was read:");
  192. print_buff(m_pReceiveBuffer, nBytesRead);
  193. return false;
  194. }
  195. //check the exception packet against the one that we sent
  196. bRetVal = !memcmp(m_pSendBuffer, m_pReceiveBuffer, nBytesSent);
  197. if (false == bRetVal) {
  198. LOG_MSG_ERROR("Received packet is not equal, Received:");
  199. print_buff(m_pReceiveBuffer, nBytesRead);
  200. LOG_MSG_ERROR("Received packet is not equal, Sent:");
  201. print_buff(m_pSendBuffer, m_nPacketSize);
  202. return false;
  203. }
  204. return true;
  205. }
  206. ///////////////////////////////////////////////////////////////////////////
  207. //build the non-IP packet
  208. virtual bool Setup() {
  209. bool bRetVal = true;
  210. m_pReceiveBuffer = new Byte[MAX_RECEIVE_BUFFER_SIZE];
  211. m_pSendBuffer = new Byte[MAX_RECEIVE_BUFFER_SIZE];
  212. //Load some default TCP packet
  213. TCPPacket tcpPacket;
  214. //Set the MF bit
  215. tcpPacket.SetMF(true);
  216. //copy the packet to the send buffer
  217. m_nPacketSize = tcpPacket.GetSize();
  218. tcpPacket.ToNetworkByteStream(m_pSendBuffer);
  219. //initialize Pipes
  220. bRetVal = m_USB1ToIpaPipe.Init();
  221. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  222. bRetVal = m_IpaToA5ExceptionPipe.Init();
  223. VALIDATE_WITH_MSG_AND_RETVAL(bRetVal, "Pipe Initialization failed");
  224. return true;
  225. }
  226. ///////////////////////////////////////////////////////////////////////////
  227. virtual bool Teardown() {
  228. bool bRetVal = true;
  229. delete[] m_pSendBuffer;
  230. m_USB1ToIpaPipe.Destroy();
  231. m_IpaToA5ExceptionPipe.Destroy();
  232. return bRetVal;
  233. }
  234. ///////////////////////////////////////////////////////////////////////////
  235. void ConfigureFilterGlobalRuleForMF(){
  236. //struct ipa_ioc_add_flt_rule *pRuleTable;
  237. //Allocate memory for a table with one rule.
  238. //Instruct the Driver to write this table(with its one rule) to the HW
  239. //Continue from here - build the rule to catch the fragmented packet
  240. }
  241. ///////////////////////////////////////////////////////////////////////////
  242. };
  243. //ExceptionsTestFragmentedException
  244. ///////////////////////////////////////////////////////////////////////////////
  245. ///////////////////////////////////////////////////////////////////////////////
  246. ///////////////////////////////////////////////////////////////////////////////
  247. class ExceptionsTestNonTCPUDP: public ExceptionsTestFixture {
  248. };
  249. ///////////////////////////////////////////////////////////////////////////////
  250. //Classes instances:
  251. static ExceptionsTestNonIpPacket exceptionsTestNonIpPacket;
  252. static ExceptionsTestFragmentedException exceptionsTestFragmentedException;
  253. ///////////////////////////////////////////////////////////////////////////////
  254. ////////////// EOF ////////
  255. ///////////////////////////////////////////////////////////////////////////////