ParseRule.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 ParseRule (int * argcp, char const * argvp [], struct rule * rule, struct cspec * cspec);
  44. *
  45. * rules.h
  46. *
  47. * This module takes an argument vector and an argument count
  48. * and fills in a classification rule structure that is suitable for
  49. * sending in a VS_CLASSIFICATION MME;
  50. *
  51. * This module is currently used by plcrule and pibruin;
  52. *
  53. *
  54. * Contributor(s):
  55. * Charles Maier
  56. * Nathaniel Houghton
  57. *
  58. *--------------------------------------------------------------------*/
  59. #include <memory.h>
  60. #include <errno.h>
  61. #include "../tools/memory.h"
  62. #include "../tools/number.h"
  63. #include "../tools/error.h"
  64. #include "../ether/ether.h"
  65. #include "../plc/rules.h"
  66. signed ParseRule (int * argcp, char const ** argvp [], struct MMERule * rule, struct cspec * cspec)
  67. {
  68. int argc = * argcp;
  69. char const ** argv = * argvp;
  70. union
  71. {
  72. uint32_t wide;
  73. uint16_t word;
  74. uint8_t byte [4];
  75. }
  76. temp;
  77. signed code;
  78. struct MMEClassifier * classifier = (struct MMEClassifier *) (& rule->CLASSIFIER);
  79. if ((code = lookup (* argv++, actions, SIZEOF (actions))) == -1)
  80. {
  81. assist (* -- argv, CLASSIFIER_ACTION_NAME, actions, SIZEOF (actions));
  82. }
  83. rule->MACTION = (uint8_t) (code);
  84. argc--;
  85. if ((code = lookup (* argv++, operands, SIZEOF (operands))) == -1)
  86. {
  87. assist (* -- argv, CLASSIFIER_OPERAND_NAME, operands, SIZEOF (operands));
  88. }
  89. rule->MOPERAND = (uint8_t) (code);
  90. argc--;
  91. while ((* argv) && (lookup (* argv, controls, SIZEOF (controls)) == -1))
  92. {
  93. if ((code = lookup (* argv++, fields, SIZEOF (fields))) == -1)
  94. {
  95. assist (* -- argv, CLASSIFIER_FIELD_NAME, fields, SIZEOF (fields));
  96. }
  97. classifier->CR_PID = (uint8_t) (code);
  98. argc--;
  99. if ((code = lookup (* argv++, operators, SIZEOF (operators))) == -1)
  100. {
  101. assist (* -- argv, CLASSIFIER_OPERATOR_NAME, operators, SIZEOF (operators));
  102. }
  103. classifier->CR_OPERAND = (uint8_t) (code);
  104. argc--;
  105. if (! argc || ! * argv)
  106. {
  107. error (1, ENOTSUP, "I have %s '%s' but no value", CLASSIFIER_OPERATOR_NAME, * -- argv);
  108. }
  109. switch (classifier->CR_PID)
  110. {
  111. case FIELD_ETH_SA:
  112. case FIELD_ETH_DA:
  113. bytespec (* argv++, classifier->CR_VALUE, ETHER_ADDR_LEN);
  114. break;
  115. case FIELD_IPV4_SA:
  116. case FIELD_IPV4_DA:
  117. ipv4spec (* argv++, classifier->CR_VALUE);
  118. break;
  119. case FIELD_IPV6_SA:
  120. case FIELD_IPV6_DA:
  121. ipv6spec (* argv++, classifier->CR_VALUE);
  122. break;
  123. case FIELD_VLAN_UP:
  124. case FIELD_IPV6_TC:
  125. case FIELD_IPV4_TOS:
  126. case FIELD_IPV4_PROT:
  127. classifier->CR_VALUE [0] = (uint8_t) (basespec (* argv++, 0, sizeof (classifier->CR_VALUE [0])));
  128. break;
  129. case FIELD_VLAN_ID:
  130. case FIELD_TCP_SP:
  131. case FIELD_TCP_DP:
  132. case FIELD_UDP_SP:
  133. case FIELD_UDP_DP:
  134. case FIELD_IP_SP:
  135. case FIELD_IP_DP:
  136. temp.word = (uint16_t) (basespec (* argv++, 0, sizeof (temp.word)));
  137. temp.word = htons (temp.word);
  138. memcpy (classifier->CR_VALUE, & temp, sizeof (temp.word));
  139. break;
  140. case FIELD_ETH_TYPE:
  141. temp.word = (uint16_t) (basespec (* argv++, 0, sizeof (temp.word)));
  142. temp.word = htons (temp.word);
  143. memcpy (classifier->CR_VALUE, & temp, sizeof (temp.word));
  144. break;
  145. case FIELD_IPV6_FL:
  146. temp.wide = (uint32_t) (basespec (* argv++, 0, sizeof (temp.wide))) & 0x000FFFFF;
  147. temp.wide = htonl (temp.wide);
  148. memcpy (classifier->CR_VALUE, & temp.byte [1], 3);
  149. break;
  150. case FIELD_HPAV_MME:
  151. bytespec (* argv++, classifier->CR_VALUE, sizeof (uint16_t) + sizeof (uint8_t));
  152. temp.byte [0] = classifier->CR_VALUE [1];
  153. classifier->CR_VALUE [1] = classifier->CR_VALUE [2];
  154. classifier->CR_VALUE [2] = temp.byte [0];
  155. break;
  156. case FIELD_TCP_ACK:
  157. if ((code = lookup (* argv++, states, SIZEOF (states))) == -1)
  158. {
  159. assist (* -- argv, CLASSIFIER_STATE_NAME, states, SIZEOF (states));
  160. }
  161. memset (classifier->CR_VALUE, 0, sizeof (classifier->CR_VALUE));
  162. break;
  163. case FIELD_VLAN_TAG:
  164. if ((code = lookup (* argv++, states, SIZEOF (states))) == -1)
  165. {
  166. assist (* -- argv, CLASSIFIER_STATE_NAME, states, SIZEOF (states));
  167. }
  168. memset (classifier->CR_VALUE, 0, sizeof (classifier->CR_VALUE));
  169. classifier->CR_OPERAND ^= code;
  170. break;
  171. default:
  172. error (1, ENOTSUP, "%s", argv [- 2]);
  173. break;
  174. }
  175. rule->NUM_CLASSIFIERS++;
  176. classifier++;
  177. argc--;
  178. }
  179. memcpy (classifier, cspec, sizeof (* cspec));
  180. if ((code = lookup (* argv++, controls, SIZEOF (controls))) == -1)
  181. {
  182. assist (* -- argv, CLASSIFIER_CONTROL_NAME, controls, SIZEOF (controls));
  183. }
  184. rule->MCONTROL = (uint8_t) (code);
  185. argc--;
  186. if ((code = lookup (* argv++, volatilities, SIZEOF (volatilities))) == -1)
  187. {
  188. assist (* -- argv, CLASSIFIER_VOLATILITY_NAME, volatilities, SIZEOF (volatilities));
  189. }
  190. rule->MVOLATILITY = (uint8_t) (code);
  191. argc--;
  192. * argcp = argc;
  193. * argvp = argv;
  194. return (0);
  195. }