UnwantedMessage.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * signed UnwantedMessage (void const * memory, size_t extent, uint8_t MMV, uint16_t MMTYPE);
  44. *
  45. * mme.h
  46. *
  47. * return true if memory does not contains a Qualcomm Atheros message
  48. * of the specified version and type; the message version determines
  49. * the location of the OUI field; messages with MMV = 1 have an FMI
  50. * used to track multi-part confirmation counts; out of order counts
  51. * are treated as an error;
  52. *
  53. * constant __WHYNOT__ displays reason for message rejection;
  54. *
  55. * batch is the fragment group identifier taken from the fragment
  56. * frame;
  57. *
  58. * total is the number of fragements in the group taken from the
  59. * fragment frame;
  60. *
  61. * index is the fragment identifier from the fragment frame;
  62. *
  63. * count is the number of fragments received so far;
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier
  68. * Matthieu Poullet
  69. *
  70. *--------------------------------------------------------------------*/
  71. #ifndef UNWANTEDMESSAGE_SOURCE
  72. #define UNWANTEDMESSAGE_SOURCE
  73. #include <stdint.h>
  74. #include <memory.h>
  75. #include "../tools/endian.h"
  76. #include "../tools/error.h"
  77. #include "../mme/mme.h"
  78. signed UnwantedMessage (void const * memory, size_t extent, uint8_t MMV, uint16_t MMTYPE)
  79. {
  80. extern const byte localcast [ETHER_ADDR_LEN];
  81. // struct message * message = (struct message *)(memory);
  82. struct homeplug * homeplug = (struct homeplug *)(memory);
  83. if (!extent)
  84. {
  85. return (-1);
  86. }
  87. if (extent < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  88. {
  89. #if defined (__WHYNOT__)
  90. error (0, 0, "Wrong Ethernet Frame Size: Received " SIZE_T_SPEC " bytes but need at least %d", extent, ETHER_MIN_LEN - ETHER_CRC_LEN);
  91. #endif
  92. return (-1);
  93. }
  94. if (extent > (ETHER_MAX_LEN))
  95. {
  96. #if defined (__WHYNOT__)
  97. error (0, 0, "Wrong Ethernet Frame Size: Received " SIZE_T_SPEC " bytes but need at most %d", extent, ETHER_MAX_LEN);
  98. #endif
  99. return (-1);
  100. }
  101. if (ntohs (homeplug->ethernet.MTYPE) != ETH_P_HPAV)
  102. {
  103. #if defined (__WHYNOT__)
  104. error (0, 0, "Wrong Ethernet Frame Type: Received %04X while waiting for %04X", ntohs (homeplug->ethernet.MTYPE), ETH_P_HPAV);
  105. #endif
  106. return (-1);
  107. }
  108. if (homeplug->homeplug.MMV != MMV)
  109. {
  110. #if defined (__WHYNOT__)
  111. error (0, 0, "Wrong Message Version: Received %02X but expected %02X", homeplug->homeplug.MMV, MMV);
  112. #endif
  113. return (-1);
  114. }
  115. if (homeplug->homeplug.MMV == 0)
  116. {
  117. struct qualcomm_hdr * qualcomm = (struct qualcomm_hdr *)(&homeplug->homeplug);
  118. if (LE16TOH (qualcomm->MMTYPE) != MMTYPE)
  119. {
  120. #if defined (__WHYNOT__)
  121. error (0, 0, "Wrong Message Type: Received %04X while waiting for %04X", LE16TOH (qualcomm->MMTYPE), MMTYPE);
  122. #endif
  123. return (-1);
  124. }
  125. if ((MMTYPE < VS_MMTYPE_MIN) || (MMTYPE > VS_MMTYPE_MAX))
  126. {
  127. }
  128. else if (memcmp (localcast, qualcomm->OUI, sizeof (qualcomm->OUI)))
  129. {
  130. #if defined (__WHYNOT__)
  131. error (0, 0, "Wrong Message Vendor");
  132. #endif
  133. return (-1);
  134. }
  135. }
  136. if (homeplug->homeplug.MMV == 1)
  137. {
  138. struct qualcomm_fmi * qualcomm = (struct qualcomm_fmi *)(&homeplug->homeplug);
  139. #if FMI
  140. static unsigned total = 0;
  141. static unsigned index = 0;
  142. static unsigned count = 0;
  143. #endif
  144. if (LE16TOH (qualcomm->MMTYPE) != MMTYPE)
  145. {
  146. #if defined (__WHYNOT__)
  147. error (0, 0, "Wrong Message Type: Received %04X while waiting for %04X", LE16TOH (qualcomm->MMTYPE), MMTYPE);
  148. #endif
  149. return (-1);
  150. }
  151. #if FMI
  152. index = qualcomm->FMID >> 0 & 0x0F;
  153. if (!index)
  154. {
  155. total = qualcomm->FMID >> 4 & 0x0F;
  156. count = qualcomm->FMID >> 0 & 0x0F;
  157. if (memcmp (localcast, qualcomm->OUI, sizeof (qualcomm->OUI)))
  158. {
  159. #if defined (__WHYNOT__)
  160. error (0, 0, "Wrong Message Vendor");
  161. #endif
  162. return (-1);
  163. }
  164. }
  165. if (index != count)
  166. {
  167. #if defined (__WHYNOT__)
  168. error (0, 0, "Message Fragment Out-of-order");
  169. #endif
  170. return (-1);
  171. }
  172. if (count > total)
  173. {
  174. #if defined (__WHYNOT__)
  175. error (0, 0, "Fragment Count Exceeds Total");
  176. #endif
  177. return (-1);
  178. }
  179. count++;
  180. #endif
  181. }
  182. return (0);
  183. }
  184. #endif