ReadRules.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 ReadRules (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. * Read and Display the classifier rules read from a PLC device.
  48. *
  49. * Contributor(s):
  50. * Nathan Houghton
  51. * Charles Maier
  52. *
  53. *--------------------------------------------------------------------*/
  54. #ifndef READRULES_SOURCE
  55. #define READRULES_SOURCE
  56. #include <limits.h>
  57. #include <stdio.h>
  58. #include "../plc/plc.h"
  59. #include "../plc/rules.h"
  60. #include "../tools/error.h"
  61. signed ReadRules (struct plc * plc)
  62. {
  63. struct channel * channel = (struct channel *) (plc->channel);
  64. struct message * message = (struct message *) (plc->message);
  65. #ifndef __GNUC__
  66. #pragma pack (push,1)
  67. #endif
  68. struct __packed vs_classification_request
  69. {
  70. struct ethernet_hdr ethernet;
  71. struct qualcomm_hdr qualcomm;
  72. uint8_t MCONTROL;
  73. uint8_t RSVD;
  74. uint8_t OFFSET;
  75. uint8_t COUNT;
  76. }
  77. * request = (struct vs_classification_request *) (message);
  78. struct __packed vs_classification_confirm
  79. {
  80. struct ethernet_hdr ethernet;
  81. struct qualcomm_hdr qualcomm;
  82. uint8_t MSTATUS;
  83. uint8_t TOTAL_CLASSIFIERS;
  84. uint8_t OFFSET;
  85. uint8_t COUNT;
  86. struct __packed MMEReadRule
  87. {
  88. uint8_t MACTION;
  89. uint8_t MOPERAND;
  90. uint8_t NUM_CLASSIFIERS;
  91. struct MMEClassifier CLASSIFIER [3];
  92. struct cspec cspec;
  93. }
  94. RULESET [60];
  95. }
  96. * confirm = (struct vs_classification_confirm *) (message);
  97. #ifndef __GNUC__
  98. #pragma pack (pop)
  99. #endif
  100. unsigned index = 0;
  101. unsigned total = UINT_MAX;
  102. Request (plc, "Read Classifier Rules");
  103. while (index < total)
  104. {
  105. struct MMEReadRule * rule;
  106. memset (message, 0, sizeof (* message));
  107. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  108. QualcommHeader (& request->qualcomm, 0, (VS_CLASSIFICATION | MMTYPE_REQ));
  109. plc->packetsize = ETHER_MIN_LEN - ETHER_CRC_LEN;
  110. request->MCONTROL = CONTROL_READ;
  111. request->OFFSET = index;
  112. request->COUNT = SIZEOF (confirm->RULESET);
  113. if (SendMME (plc) <= 0)
  114. {
  115. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  116. return (-1);
  117. }
  118. if (ReadMME (plc, 0, (VS_CLASSIFICATION | MMTYPE_CNF)) <= 0)
  119. {
  120. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  121. return (-1);
  122. }
  123. if (confirm->MSTATUS)
  124. {
  125. Failure (plc, PLC_WONTDOIT);
  126. return (-1);
  127. }
  128. total = confirm->TOTAL_CLASSIFIERS;
  129. index += confirm->COUNT;
  130. rule = confirm->RULESET;
  131. while (confirm->COUNT--)
  132. {
  133. int count;
  134. int rule_len;
  135. const char * p1;
  136. const char * p2;
  137. struct cspec * cspec;
  138. if (rule->NUM_CLASSIFIERS > RULE_MAX_CLASSIFIERS)
  139. {
  140. error (1, 0, "too many classifiers in rule (%d, expecting <= %d)", rule->NUM_CLASSIFIERS, RULE_MAX_CLASSIFIERS);
  141. }
  142. rule_len = sizeof (* rule) - (RULE_MAX_CLASSIFIERS - rule->NUM_CLASSIFIERS) * sizeof (struct MMEClassifier) - sizeof (struct cspec);
  143. if (rule->MACTION == ACTION_AUTOCONNECT || rule->MACTION == ACTION_TAGTX || rule->MACTION == ACTION_TAGRX)
  144. {
  145. cspec = (struct cspec *) ((uint8_t *) rule + rule_len);
  146. rule_len += sizeof (struct cspec);
  147. }
  148. if (rule->MACTION == ACTION_TAGTX)
  149. {
  150. printf ("-T 0x%08X -V %d ", ntohl (cspec->VLAN_TAG), cspec->CSPEC_VERSION);
  151. }
  152. p1 = reword (rule->MACTION, actions, CLASSIFIER_ACTIONS);
  153. if (p1 == NULL)
  154. {
  155. error (1, 0, "invalid classifier action");
  156. }
  157. p2 = reword (rule->MOPERAND, operands, CLASSIFIER_OPERANDS);
  158. if (p2 == NULL)
  159. {
  160. error (1, 0, "invalid classifier operand");
  161. }
  162. printf ("%s", p1);
  163. printf (" %s ", p2);
  164. /* need to dump out the actual conditions here */
  165. for (count = 0; count < rule->NUM_CLASSIFIERS; ++ count)
  166. {
  167. struct MMEClassifier * classifier = & rule->CLASSIFIER [count];
  168. PrintRule (classifier->CR_PID, classifier->CR_OPERAND, classifier->CR_VALUE);
  169. putchar (' ');
  170. }
  171. printf ("add temp\n");
  172. rule = (struct MMEReadRule *) ((uint8_t *) (rule) + rule_len);
  173. }
  174. }
  175. return (0);
  176. }
  177. #endif