pibrump.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * pibrump.c - Qualcomm Atheros Classification Rule Dump Utility;
  11. *
  12. * This program read classification rules stored in a PIB file and
  13. * prints them on stdout in a format similar to int6krule commands;
  14. *
  15. * Contributor(s):
  16. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  17. *
  18. *--------------------------------------------------------------------*/
  19. /*====================================================================*
  20. * system header files;
  21. *--------------------------------------------------------------------*/
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. /*====================================================================*
  29. * custom header files;
  30. *--------------------------------------------------------------------*/
  31. #include "../tools/getoptv.h"
  32. #include "../tools/memory.h"
  33. #include "../tools/symbol.h"
  34. #include "../tools/types.h"
  35. #include "../tools/flags.h"
  36. #include "../tools/files.h"
  37. #include "../tools/error.h"
  38. #include "../pib/pib.h"
  39. #include "../plc/rules.h"
  40. /*====================================================================*
  41. * custom source files;
  42. *--------------------------------------------------------------------*/
  43. #ifndef MAKEFILE
  44. #include "../tools/getoptv.c"
  45. #include "../tools/putoptv.c"
  46. #include "../tools/version.c"
  47. #include "../tools/lookup.c"
  48. #include "../tools/reword.c"
  49. #include "../tools/hexstring.c"
  50. #include "../tools/hexdecode.c"
  51. #include "../tools/fdchecksum32.c"
  52. #include "../tools/memout.c"
  53. #include "../tools/error.c"
  54. #endif
  55. #ifndef MAKEFILE
  56. #include "../pib/lightning_pib_file.c"
  57. #endif
  58. #ifndef MAKEFILE
  59. #include "../plc/rules.c"
  60. #include "../plc/PrintRule.c"
  61. #endif
  62. /*====================================================================*
  63. * program constants;
  64. *--------------------------------------------------------------------*/
  65. #define PIB_MAX_AUTOCONN 16
  66. #define PIB_MAX_PRIORITY_MAPS 8
  67. #define PIB_AUTOCONN_COUNT_OFFSET 0x22C
  68. #define PIB_PRIORITY_COUNT_OFFSET 0x228
  69. #define PIB_AUTOCONN_OFFSET 0x310
  70. #define PIB_PRIORITY_MAPS_OFFSET 0x230
  71. /*====================================================================*
  72. *
  73. * signed AutoConnection (struct auto_connection * auto_connection, flag_t flags)
  74. *
  75. * plc.h
  76. *
  77. * This function for program pibrump displays a single AutoConnection
  78. * structure;
  79. *
  80. * Contributor(s):
  81. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  82. *
  83. *--------------------------------------------------------------------*/
  84. signed AutoConnection (struct auto_connection * auto_connection)
  85. {
  86. int i;
  87. const char * p1;
  88. const char * p2;
  89. if (auto_connection->MACTION == ACTION_TAGTX)
  90. {
  91. printf ("-T 0x%08X -V %d ", ntohl (auto_connection->cspec.VLAN_TAG), LE16TOH (auto_connection->cspec.CSPEC_VERSION));
  92. }
  93. p1 = reword (auto_connection->MACTION, actions, SIZEOF (actions));
  94. if (p1 == NULL)
  95. {
  96. error (1, 0, "invalid classifier action");
  97. }
  98. p2 = reword (auto_connection->MOPERAND, operands, SIZEOF (operands));
  99. if (p2 == NULL)
  100. {
  101. error (1, 0, "invalid classifier operand");
  102. }
  103. printf ("%s %s ", p1, p2);
  104. for (i = 0; i < LE16TOH (auto_connection->NUM_CLASSIFIERS); ++ i)
  105. {
  106. struct PIBClassifier * classifier = & auto_connection->CLASSIFIER [i];
  107. PrintRule (classifier->CR_PID, classifier->CR_OPERAND, classifier->CR_VALUE);
  108. putchar (' ');
  109. }
  110. printf ("add perm\n");
  111. return (0);
  112. }
  113. /*====================================================================*
  114. *
  115. * signed ClassifierPriorityMap (struct classifier_priority_map * map)
  116. *
  117. * plc.h
  118. *
  119. * This function for program pibrump displays a single
  120. * ClassifierPriorityMap structure;
  121. *
  122. * Contributor(s):
  123. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  124. *
  125. *--------------------------------------------------------------------*/
  126. signed ClassifierPriorityMap (struct classifier_priority_map * map)
  127. {
  128. struct PIBClassifier * classifier = & map->CLASSIFIER;
  129. const char * p = reword (LE32TOH (map->Priority), actions, SIZEOF (actions));
  130. if (p == NULL)
  131. {
  132. error (1, 0, "invalid classifier priority");
  133. }
  134. printf ("%s Any ", p);
  135. PrintRule (classifier->CR_PID, classifier->CR_OPERAND, classifier->CR_VALUE);
  136. printf (" add perm\n");
  137. return (0);
  138. }
  139. /*====================================================================*
  140. *
  141. * int main (int argc, char const * argv[]);
  142. *
  143. *
  144. *--------------------------------------------------------------------*/
  145. int main (int argc, char const * argv [])
  146. {
  147. static char const * optv [] =
  148. {
  149. "eqv",
  150. "pibfile",
  151. "Qualcomm Atheros Classification Rule Dump Utility",
  152. "e\tredirect stderr messages to stdout",
  153. "q\tquiet mode",
  154. "v\tverbose mode",
  155. (char const *) (0)
  156. };
  157. struct auto_connection auto_connection [PIB_MAX_AUTOCONN];
  158. struct classifier_priority_map classifier_priority_map [PIB_MAX_PRIORITY_MAPS];
  159. uint32_t AutoConnection_count;
  160. uint32_t PriorityMaps_count;
  161. unsigned i;
  162. flag_t flags = (flag_t) (0);
  163. struct _file_ pib;
  164. signed c;
  165. optind = 1;
  166. while (~ (c = getoptv (argc, argv, optv)))
  167. {
  168. switch ((char) (c))
  169. {
  170. case 'e':
  171. dup2 (STDOUT_FILENO, STDERR_FILENO);
  172. break;
  173. case 'q':
  174. _setbits (flags, PIB_SILENCE);
  175. break;
  176. case 'v':
  177. _setbits (flags, PIB_VERBOSE);
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. argc -= optind;
  184. argv += optind;
  185. while ((argc) && (* argv))
  186. {
  187. pib.name = * argv;
  188. if ((pib.file = open (pib.name, O_BINARY | O_RDONLY)) == - 1)
  189. {
  190. error (1, errno, "%s", pib.name);
  191. }
  192. if (lightning_pib_file (& pib))
  193. {
  194. error (1, errno, "Bad PIB file: %s", pib.name);
  195. }
  196. if (lseek (pib.file, PIB_AUTOCONN_COUNT_OFFSET, SEEK_SET) != PIB_AUTOCONN_COUNT_OFFSET)
  197. {
  198. error (1, errno, "could not seek to AutoConnection count");
  199. }
  200. if (read (pib.file, & AutoConnection_count, sizeof (AutoConnection_count)) != sizeof (AutoConnection_count))
  201. {
  202. error (1, errno, "could not read AutoConnection count");
  203. }
  204. if (lseek (pib.file, PIB_AUTOCONN_OFFSET, SEEK_SET) != PIB_AUTOCONN_OFFSET)
  205. {
  206. error (1, errno, "could not seek to AutoConnections");
  207. }
  208. if (read (pib.file, & auto_connection, sizeof (auto_connection)) != sizeof (auto_connection))
  209. {
  210. error (1, errno, "could not read AutoConnections");
  211. }
  212. if (lseek (pib.file, PIB_PRIORITY_COUNT_OFFSET, SEEK_SET) != PIB_PRIORITY_COUNT_OFFSET)
  213. {
  214. error (1, errno, "could not seek to PriorityMaps count");
  215. }
  216. if (read (pib.file, & PriorityMaps_count, sizeof (PriorityMaps_count)) != sizeof (PriorityMaps_count))
  217. {
  218. error (1, errno, "could not read PriorityMaps count");
  219. }
  220. if (lseek (pib.file, PIB_PRIORITY_MAPS_OFFSET, SEEK_SET) != PIB_PRIORITY_MAPS_OFFSET)
  221. {
  222. error (1, errno, "could not seek to Priority Map");
  223. }
  224. if (read (pib.file, & classifier_priority_map, sizeof (classifier_priority_map)) != sizeof (classifier_priority_map))
  225. {
  226. error (1, errno, "could not read Priority Map");
  227. }
  228. close (pib.file);
  229. if (_allclr (flags, PIB_SILENCE))
  230. {
  231. printf ("# auto connection rules:\n");
  232. }
  233. for (i = 0; i < AutoConnection_count; ++ i)
  234. {
  235. AutoConnection (& auto_connection [i]);
  236. }
  237. if (_allclr (flags, PIB_SILENCE))
  238. {
  239. printf ("# priority mapping rules:\n");
  240. }
  241. for (i = 0; i < PriorityMaps_count; ++ i)
  242. {
  243. ClassifierPriorityMap (& classifier_priority_map [i]);
  244. }
  245. argv++;
  246. argc--;
  247. }
  248. exit (0);
  249. }