PrintRule.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 PrintRule (uint32_t CR_PID, uint32_t CR_OPERAND, uint8_t CR_VALUE []);
  44. *
  45. * rules.h
  46. *
  47. * print one classifier rule on stdout;
  48. *
  49. * some classifier rule structures have an 8-bit PID and OPERAND
  50. * while others have a 32-bit PID and OPERAND; this function is
  51. * the common demominator;
  52. *
  53. * Contributor(s):
  54. * Nathaniel Houghton
  55. * Charles Maier
  56. *
  57. *--------------------------------------------------------------------*/
  58. #ifndef PRINTRULE_SOURCE
  59. #define PRINTRULE_SOURCE
  60. #include <string.h>
  61. #include "../plc/rules.h"
  62. #include "../tools/error.h"
  63. signed PrintRule (uint32_t CR_PID, uint32_t CR_OPERAND, uint8_t CR_VALUE [])
  64. {
  65. char buffer [CLASSIFIER_STRING];
  66. uint32_t val32;
  67. uint16_t val16;
  68. uint8_t val8;
  69. const char * p1;
  70. const char * p2;
  71. p1 = reword (CR_PID, fields, SIZEOF (fields));
  72. if (p1 == NULL)
  73. {
  74. error (1, 0, "invalid classifier PID");
  75. }
  76. p2 = reword (CR_OPERAND, operators, SIZEOF (operators));
  77. if (p2 == NULL)
  78. {
  79. error (1, 0, "invalid classifier operand");
  80. }
  81. printf ("%s %s", p1, p2);
  82. switch (CR_PID)
  83. {
  84. case FIELD_ETH_SA:
  85. case FIELD_ETH_DA:
  86. printf (" %s", hexstring (buffer, sizeof (buffer), CR_VALUE, ETHER_ADDR_LEN));
  87. break;
  88. case FIELD_IPV4_SA:
  89. case FIELD_IPV4_DA:
  90. putchar (' ');
  91. memout (CR_VALUE, IPv4_LEN, "%d", 1, '.', 0, stdout);
  92. break;
  93. case FIELD_IPV6_SA:
  94. case FIELD_IPV6_DA:
  95. putchar (' ');
  96. memout (CR_VALUE, IPv6_LEN, "%02x", 2, ':', 0, stdout);
  97. break;
  98. case FIELD_VLAN_UP:
  99. case FIELD_IPV6_TC:
  100. case FIELD_IPV4_TOS:
  101. case FIELD_IPV4_PROT:
  102. memcpy (& val8, CR_VALUE, sizeof (val8));
  103. printf (" 0x%02X", val8);
  104. break;
  105. case FIELD_VLAN_ID:
  106. case FIELD_TCP_SP:
  107. case FIELD_TCP_DP:
  108. case FIELD_UDP_SP:
  109. case FIELD_UDP_DP:
  110. case FIELD_IP_SP:
  111. case FIELD_IP_DP:
  112. memcpy (& val16, CR_VALUE, sizeof (val16));
  113. val16 = ntohs (val16);
  114. printf (" %d", val16);
  115. break;
  116. case FIELD_ETH_TYPE:
  117. memcpy (& val16, CR_VALUE, sizeof (val16));
  118. val16 = ntohs (val16);
  119. printf (" 0x%04X", val16);
  120. break;
  121. case FIELD_IPV6_FL:
  122. memcpy (& val32, & CR_VALUE [0], sizeof (val32));
  123. val32 = ntohl (val32);
  124. printf (" 0x%08X", val32);
  125. break;
  126. case FIELD_HPAV_MME:
  127. memcpy (& val8, & CR_VALUE [0], sizeof (val8));
  128. memcpy (& val16, & CR_VALUE [1], sizeof (val16));
  129. printf (" %02x:%04x", val8, val16);
  130. break;
  131. case FIELD_TCP_ACK:
  132. {
  133. code_t val;
  134. const char * p;
  135. memcpy (& val, CR_VALUE, sizeof (val));
  136. p = reword (val, states, SIZEOF (states));
  137. if (p == NULL)
  138. {
  139. error (1, 0, "invalid TCP ACK flag");
  140. }
  141. printf (" %s", p);
  142. }
  143. break;
  144. case FIELD_VLAN_TAG:
  145. {
  146. code_t val;
  147. const char * p;
  148. memcpy (& val, CR_VALUE, sizeof (val));
  149. p = reword (val, states, SIZEOF (states));
  150. if (p == NULL)
  151. {
  152. error (1, 0, "invalid VLAN tag flag");
  153. }
  154. printf (" %s", p);
  155. }
  156. break;
  157. default:
  158. printf (" *****UNSUPPORTED CODE*****");
  159. break;
  160. }
  161. return (0);
  162. }
  163. #endif