pibrump.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. * pibrump.c - Atheros Classification Rule Dump Utility;
  44. *
  45. * This program read classification rules stored in a PIB file and
  46. * prints them on stdout in a format similar to int6krule commands;
  47. *
  48. *
  49. * Contributor(s):
  50. * Nathaniel Houghton
  51. *
  52. *--------------------------------------------------------------------*/
  53. /*====================================================================*"
  54. * system header files;
  55. *--------------------------------------------------------------------*/
  56. #include <unistd.h>
  57. #include <stdlib.h>
  58. #include <stdint.h>
  59. #include <string.h>
  60. #include <netinet/in.h>
  61. #include <arpa/inet.h>
  62. /*====================================================================*
  63. * custom header files;
  64. *--------------------------------------------------------------------*/
  65. #include "../tools/getoptv.h"
  66. #include "../tools/memory.h"
  67. #include "../tools/symbol.h"
  68. #include "../tools/types.h"
  69. #include "../tools/flags.h"
  70. #include "../tools/files.h"
  71. #include "../tools/error.h"
  72. #include "../pib/pib.h"
  73. #include "../plc/rules.h"
  74. /*====================================================================*
  75. * custom source files;
  76. *--------------------------------------------------------------------*/
  77. #ifndef MAKEFILE
  78. #include "../tools/getoptv.c"
  79. #include "../tools/putoptv.c"
  80. #include "../tools/version.c"
  81. #include "../tools/lookup.c"
  82. #include "../tools/reword.c"
  83. #include "../tools/hexstring.c"
  84. #include "../tools/hexdecode.c"
  85. #include "../tools/fdchecksum32.c"
  86. #include "../tools/memout.c"
  87. #include "../tools/error.c"
  88. #endif
  89. #ifndef MAKEFILE
  90. #include "../pib/pibfile1.c"
  91. #endif
  92. #ifndef MAKEFILE
  93. #include "../plc/rules.c"
  94. #endif
  95. /*====================================================================*
  96. * program constants;
  97. *--------------------------------------------------------------------*/
  98. #define PIB_MAX_AUTOCONN 16
  99. #define PIB_MAX_PRIORITY_MAPS 8
  100. #define PIB_AUTOCONN_COUNT_OFFSET 0x22C
  101. #define PIB_PRIORITY_COUNT_OFFSET 0x228
  102. #define PIB_AUTOCONN_OFFSET 0x310
  103. #define PIB_PRIORITY_MAPS_OFFSET 0x230
  104. /*====================================================================*
  105. *
  106. * signed Classifier (struct PIBClassifier * classifier);
  107. *
  108. * plc.h
  109. *
  110. * This function for program pibrump displays a single classifier;
  111. *
  112. * Contributor(s):
  113. * Nathaniel Houghton
  114. *
  115. *--------------------------------------------------------------------*/
  116. signed Classifier (struct PIBClassifier * classifier)
  117. {
  118. char buffer [sizeof (classifier->CR_VALUE) * 3 + 1];
  119. uint32_t val32;
  120. uint16_t val16;
  121. uint8_t val8;
  122. printf ("%s %s", reword (classifier->CR_PID, fields, SIZEOF (fields)), reword (classifier->CR_OPERAND, operators, SIZEOF (operators)));
  123. switch (classifier->CR_PID)
  124. {
  125. case FIELD_ETH_SA:
  126. case FIELD_ETH_DA:
  127. printf (" %s", hexstring (buffer, sizeof (buffer), classifier->CR_VALUE, ETHER_ADDR_LEN));
  128. break;
  129. case FIELD_IPV4_SA:
  130. case FIELD_IPV4_DA:
  131. putchar (' ');
  132. memout (classifier->CR_VALUE, IPv4_LEN, "%d", 1, '.', 0, stdout);
  133. break;
  134. case FIELD_IPV6_SA:
  135. case FIELD_IPV6_DA:
  136. putchar (' ');
  137. memout (classifier->CR_VALUE, IPv6_LEN, "%02x", 2, ':', 0, stdout);
  138. break;
  139. case FIELD_VLAN_UP:
  140. case FIELD_IPV6_TC:
  141. case FIELD_IPV4_TOS:
  142. case FIELD_IPV4_PROT:
  143. memcpy (&val8, classifier->CR_VALUE, sizeof (val8));
  144. printf (" 0x%02X", val8);
  145. break;
  146. case FIELD_VLAN_ID:
  147. case FIELD_TCP_SP:
  148. case FIELD_TCP_DP:
  149. case FIELD_UDP_SP:
  150. case FIELD_UDP_DP:
  151. case FIELD_IP_SP:
  152. case FIELD_IP_DP:
  153. memcpy (&val16, classifier->CR_VALUE, sizeof (val16));
  154. val16 = ntohs (val16);
  155. printf (" %d", val16);
  156. break;
  157. case FIELD_ETH_TYPE:
  158. memcpy (&val16, classifier->CR_VALUE, sizeof (val16));
  159. printf (" 0x%04X", val16);
  160. break;
  161. case FIELD_IPV6_FL:
  162. memcpy (&val32, &classifier->CR_VALUE [0], sizeof (val32));
  163. val32 = ntohl (val32);
  164. printf (" 0x%08X", val32);
  165. break;
  166. case FIELD_HPAV_MME:
  167. memcpy (&val8, &classifier->CR_VALUE [0], sizeof (val8));
  168. memcpy (&val16, &classifier->CR_VALUE [1], sizeof (val16));
  169. printf (" %02x:%04x", val8, val16);
  170. break;
  171. case FIELD_TCP_ACK:
  172. {
  173. code_t val;
  174. memcpy (&val, classifier->CR_VALUE, sizeof (val));
  175. printf (" %s", reword (val, states, SIZEOF (states)));
  176. }
  177. break;
  178. case FIELD_VLAN_TAG:
  179. {
  180. code_t val;
  181. memcpy (&val, classifier->CR_VALUE, sizeof (val));
  182. printf (" %s", reword (val, states, SIZEOF (states)));
  183. }
  184. break;
  185. default:
  186. printf (" *****UNSUPPORTED CODE*****");
  187. break;
  188. }
  189. return (0);
  190. }
  191. /*====================================================================*
  192. *
  193. * signed AutoConnection (struct auto_connection * auto_connection, flag_t flags)
  194. *
  195. * plc.h
  196. *
  197. * This function for program pibrump displays a single AutoConnection
  198. * structure;
  199. *
  200. * Contributor(s):
  201. * Nathaniel Houghton
  202. *
  203. *--------------------------------------------------------------------*/
  204. signed AutoConnection (struct auto_connection * auto_connection)
  205. {
  206. int i;
  207. if (auto_connection->MACTION == ACTION_TAGTX)
  208. {
  209. printf ("-T 0x%08X -V %d ", ntohl (auto_connection->cspec.VLAN_TAG), LE16TOH (auto_connection->cspec.CSPEC_VERSION));
  210. }
  211. printf ("%s", reword (auto_connection->MACTION, actions, SIZEOF (actions)));
  212. printf (" %s ", reword (auto_connection->MOPERAND, operands, SIZEOF (operands)));
  213. for (i = 0; i < LE16TOH (auto_connection->NUM_CLASSIFIERS); ++i)
  214. {
  215. Classifier (&auto_connection->CLASSIFIER [i]);
  216. putchar (' ');
  217. }
  218. printf ("add perm\n");
  219. return (0);
  220. }
  221. /*====================================================================*
  222. *
  223. * signed ClassifierPriorityMap (struct classifier_priority_map * map)
  224. *
  225. * plc.h
  226. *
  227. * This function for program pibrump displays a single
  228. * ClassifierPriorityMap structure;
  229. *
  230. * Contributor(s):
  231. * Nathaniel Houghton
  232. *
  233. *--------------------------------------------------------------------*/
  234. signed ClassifierPriorityMap (struct classifier_priority_map * map)
  235. {
  236. printf ("%s Any ", reword (LE32TOH (map->Priority), actions, SIZEOF (actions)));
  237. Classifier (&map->CLASSIFIER);
  238. printf (" add perm\n");
  239. return (0);
  240. }
  241. /*====================================================================*
  242. *
  243. * int main (int argc, char const * argv[]);
  244. *
  245. *--------------------------------------------------------------------*/
  246. int main (int argc, char const * argv [])
  247. {
  248. static char const * optv [] =
  249. {
  250. "eqv",
  251. "pibfile",
  252. "Atheros Classification Rule Dump Utility",
  253. "e\tredirect stderr messages to stdout",
  254. "q\tquiet mode",
  255. "v\tverbose mode",
  256. (char const *) (0)
  257. };
  258. struct auto_connection auto_connection [PIB_MAX_AUTOCONN];
  259. struct classifier_priority_map classifier_priority_map [PIB_MAX_PRIORITY_MAPS];
  260. uint32_t AutoConnection_count;
  261. uint32_t PriorityMaps_count;
  262. unsigned i;
  263. flag_t flags = (flag_t)(0);
  264. struct _file_ pib;
  265. signed c;
  266. optind = 1;
  267. while ((c = getoptv (argc, argv, optv)) != -1)
  268. {
  269. switch ((char) (c))
  270. {
  271. case 'e':
  272. dup2 (STDOUT_FILENO, STDERR_FILENO);
  273. break;
  274. case 'q':
  275. _setbits (flags, PIB_SILENCE);
  276. break;
  277. case 'v':
  278. _setbits (flags, PIB_VERBOSE);
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. argc -= optind;
  285. argv += optind;
  286. while ((argc) && (* argv))
  287. {
  288. pib.name = * argv;
  289. if ((pib.file = open (pib.name, O_BINARY|O_RDONLY)) == -1)
  290. {
  291. error (1, errno, "%s", pib.name);
  292. }
  293. if (pibfile1 (&pib))
  294. {
  295. error (1, errno, "Bad PIB file: %s", pib.name);
  296. }
  297. if (lseek (pib.file, PIB_AUTOCONN_COUNT_OFFSET, SEEK_SET) != PIB_AUTOCONN_COUNT_OFFSET)
  298. {
  299. error (1, errno, "could not seek to AutoConnection count");
  300. }
  301. if (read (pib.file, &AutoConnection_count, sizeof (AutoConnection_count)) != sizeof (AutoConnection_count))
  302. {
  303. error (1, errno, "could not read AutoConnection count");
  304. }
  305. if (lseek (pib.file, PIB_AUTOCONN_OFFSET, SEEK_SET) != PIB_AUTOCONN_OFFSET)
  306. {
  307. error (1, errno, "could not seek to AutoConnections");
  308. }
  309. if (read (pib.file, &auto_connection, sizeof (auto_connection)) != sizeof (auto_connection))
  310. {
  311. error (1, errno, "could not read AutoConnections");
  312. }
  313. if (lseek (pib.file, PIB_PRIORITY_COUNT_OFFSET, SEEK_SET) != PIB_PRIORITY_COUNT_OFFSET)
  314. {
  315. error (1, errno, "could not seek to PriorityMaps count");
  316. }
  317. if (read (pib.file, &PriorityMaps_count, sizeof (PriorityMaps_count)) != sizeof (PriorityMaps_count))
  318. {
  319. error (1, errno, "could not read PriorityMaps count");
  320. }
  321. if (lseek (pib.file, PIB_PRIORITY_MAPS_OFFSET, SEEK_SET) != PIB_PRIORITY_MAPS_OFFSET)
  322. {
  323. error (1, errno, "could not seek to Priority Map");
  324. }
  325. if (read (pib.file, &classifier_priority_map, sizeof (classifier_priority_map)) != sizeof (classifier_priority_map))
  326. {
  327. error (1, errno, "could not read Priority Map");
  328. }
  329. close (pib.file);
  330. if (_allclr (flags, PIB_SILENCE))
  331. {
  332. printf ("# auto connection rules:\n");
  333. }
  334. for (i = 0; i < AutoConnection_count; ++i)
  335. {
  336. AutoConnection (&auto_connection [i]);
  337. }
  338. if (_allclr (flags, PIB_SILENCE))
  339. {
  340. printf ("# priority mapping rules:\n");
  341. }
  342. for (i = 0; i < PriorityMaps_count; ++i)
  343. {
  344. ClassifierPriorityMap (&classifier_priority_map [i]);
  345. }
  346. argv++;
  347. argc--;
  348. }
  349. exit (0);
  350. }