pibruin.c.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. pibruin.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='pibruin1.c.html' title=' pibruin1.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='pibrump.c.html' title=' pibrump.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * pibruin.c - Atheros Classification Rule Insert Utility;
  64. *
  65. *
  66. * Contributor(s):
  67. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  68. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*&quot;
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;ctype.h&gt;
  75. #include &lt;stdio.h&gt;
  76. #include &lt;unistd.h&gt;
  77. #include &lt;stdlib.h&gt;
  78. #include &lt;stdint.h&gt;
  79. #include &lt;string.h&gt;
  80. #include &lt;netinet/in.h&gt;
  81. #include &lt;arpa/inet.h&gt;
  82. /*====================================================================*
  83. * custom header files;
  84. *--------------------------------------------------------------------*/
  85. #include &quot;../tools/getoptv.h&quot;
  86. #include &quot;../tools/memory.h&quot;
  87. #include &quot;../tools/number.h&quot;
  88. #include &quot;../tools/symbol.h&quot;
  89. #include &quot;../tools/chars.h&quot;
  90. #include &quot;../tools/types.h&quot;
  91. #include &quot;../tools/flags.h&quot;
  92. #include &quot;../tools/files.h&quot;
  93. #include &quot;../tools/error.h&quot;
  94. #include &quot;../plc/rules.h&quot;
  95. #include &quot;../nvm/nvm.h&quot;
  96. #include &quot;../pib/pib.h&quot;
  97. /*====================================================================*
  98. * custom source files;
  99. *--------------------------------------------------------------------*/
  100. #ifndef MAKEFILE
  101. #include &quot;../tools/getoptv.c&quot;
  102. #include &quot;../tools/putoptv.c&quot;
  103. #include &quot;../tools/version.c&quot;
  104. #include &quot;../tools/lookup.c&quot;
  105. #include &quot;../tools/reword.c&quot;
  106. #include &quot;../tools/hexstring.c&quot;
  107. #include &quot;../tools/hexdecode.c&quot;
  108. #include &quot;../tools/fdchecksum32.c&quot;
  109. #include &quot;../tools/checksum32.c&quot;
  110. #include &quot;../tools/memout.c&quot;
  111. #include &quot;../tools/assist.c&quot;
  112. #include &quot;../tools/bytespec.c&quot;
  113. #include &quot;../tools/basespec.c&quot;
  114. #include &quot;../tools/ipv4spec.c&quot;
  115. #include &quot;../tools/ipv6spec.c&quot;
  116. #include &quot;../tools/emalloc.c&quot;
  117. #include &quot;../tools/todigit.c&quot;
  118. #include &quot;../tools/codelist.c&quot;
  119. #include &quot;../tools/getargv.c&quot;
  120. #include &quot;../tools/error.c&quot;
  121. #endif
  122. #ifndef MAKEFILE
  123. #include &quot;../pib/pibfile1.c&quot;
  124. #include &quot;../pib/piblock.c&quot;
  125. #if defined (PRINT_RULES) || defined (PRINT_CLASSIFIERS)
  126. #include &quot;../pib/ruledump.c&quot;
  127. #endif
  128. #endif
  129. #ifndef MAKEFILE
  130. #include &quot;../nvm/nvmseek2.c&quot;
  131. #endif
  132. #ifndef MAKEFILE
  133. #include &quot;../plc/ParseRule.c&quot;
  134. #include &quot;../plc/rules.c&quot;
  135. #endif
  136. /*====================================================================*
  137. * program constants;
  138. *--------------------------------------------------------------------*/
  139. #define PIB_OFFSET1 0x0228
  140. #define PIB_OFFSET2 0x0760
  141. /*====================================================================*
  142. *
  143. * signed pibimage1 (signed fd, char const * filename, unsigned offset, struct PIBClassifiers * classifiers);
  144. *
  145. * read thunderbolt/lightning parameter block into memory, edit it
  146. * and save it;
  147. *
  148. *
  149. * Contributor(s):
  150. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  151. *
  152. *--------------------------------------------------------------------*/
  153. static signed pibimage1 (signed fd, char const * filename, unsigned offset, struct PIBClassifiers * classifiers)
  154. {
  155. struct pib_header * pib_header;
  156. off_t extent;
  157. void * memory;
  158. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  159. {
  160. error (1, errno, FILE_CANTSIZE, filename);
  161. }
  162. if (!(memory = malloc (extent)))
  163. {
  164. error (1, errno, FILE_CANTLOAD, filename);
  165. }
  166. if (lseek (fd, 0, SEEK_SET))
  167. {
  168. error (1, errno, FILE_CANTHOME, filename);
  169. }
  170. if (read (fd, memory, extent) != extent)
  171. {
  172. error (1, errno, FILE_CANTREAD, filename);
  173. }
  174. if (lseek (fd, 0, SEEK_SET))
  175. {
  176. error (1, errno, FILE_CANTHOME, filename);
  177. }
  178. memcpy ((byte *)(memory) + offset, classifiers, sizeof (*classifiers));
  179. pib_header = (struct pib_header *)(memory);
  180. pib_header-&gt;CHECKSUM = checksum32 (memory, extent, pib_header-&gt;CHECKSUM);
  181. if (write (fd, memory, extent) != extent)
  182. {
  183. error (1, errno, FILE_CANTSAVE, filename);
  184. }
  185. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  186. {
  187. error (1, errno, FILE_CANTHOME, filename);
  188. }
  189. free (memory);
  190. close (fd);
  191. return (0);
  192. }
  193. /*====================================================================*
  194. *
  195. * signed pibimage2 (signed fd, char const * filename, struct nvm_header2 * nvm_header, unsigned offset, struct PIBClassifiers * classifiers);
  196. *
  197. * read panther/lynx parameter block into memory, edit it and save
  198. * it;
  199. *
  200. *
  201. * Contributor(s):
  202. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  203. *
  204. *--------------------------------------------------------------------*/
  205. static signed pibimage2 (signed fd, char const * filename, struct nvm_header2 * nvm_header, unsigned offset, struct PIBClassifiers * classifiers)
  206. {
  207. void * memory;
  208. off_t extent = LE32TOH (nvm_header-&gt;ImageLength);
  209. if (!(memory = malloc (extent)))
  210. {
  211. error (1, errno, FILE_CANTLOAD, filename);
  212. }
  213. if (read (fd, memory, extent) != extent)
  214. {
  215. error (1, errno, FILE_CANTREAD, filename);
  216. }
  217. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  218. {
  219. error (1, errno, FILE_CANTHOME, filename);
  220. }
  221. memcpy ((byte *)(memory) + offset, classifiers, sizeof (*classifiers));
  222. nvm_header-&gt;ImageChecksum = checksum32 (memory, extent, 0);
  223. if (write (fd, memory, extent) != extent)
  224. {
  225. error (1, errno, FILE_CANTSAVE, filename);
  226. }
  227. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  228. {
  229. error (1, errno, FILE_CANTHOME, filename);
  230. }
  231. nvm_header-&gt;HeaderChecksum = checksum32 (nvm_header, sizeof (* nvm_header), nvm_header-&gt;HeaderChecksum);
  232. if (lseek (fd, (off_t)(0) - sizeof (* nvm_header), SEEK_CUR) == -1)
  233. {
  234. error (1, errno, FILE_CANTHOME, filename);
  235. }
  236. if (write (fd, nvm_header, sizeof (* nvm_header)) != sizeof (* nvm_header))
  237. {
  238. error (1, errno, FILE_CANTSAVE, filename);
  239. }
  240. if (lseek (fd, (off_t)(0) - sizeof (* nvm_header), SEEK_CUR) == -1)
  241. {
  242. error (1, errno, FILE_CANTHOME, filename);
  243. }
  244. free (memory);
  245. return (0);
  246. }
  247. /*====================================================================*
  248. *
  249. * void make_rule (int argc, char const * argv [], struct PIBClassifiers * classifiers);
  250. *
  251. * read argv [] and populate struct MMEClassifiers with QCA PLC
  252. * classifier rules; function ParseRule converts each rule to an
  253. * MMERule; we then convert that into one of two formats used to
  254. * store classifier rules in the PIB;
  255. *
  256. *
  257. *--------------------------------------------------------------------*/
  258. static void make_rule (int argc, char const * argv [], struct PIBClassifiers * classifiers)
  259. {
  260. static char const * optv [] =
  261. {
  262. &quot;T:V:&quot;,
  263. &quot;&quot;,
  264. &quot;Atheros Classification Rule Insert Utility&quot;,
  265. &quot;T\ttag&quot;,
  266. &quot;V\tversion&quot;,
  267. (char const *) (0)
  268. };
  269. struct MMERule rule;
  270. struct cspec cspec;
  271. signed c;
  272. optind = 1;
  273. memset (&amp;rule, 0, sizeof (rule));
  274. memset (&amp;cspec, 0, sizeof (cspec));
  275. while ((c = getoptv (argc, argv, optv)) != -1)
  276. {
  277. switch (c)
  278. {
  279. case 'T':
  280. cspec.VLAN_TAG = (uint32_t)(basespec (optarg, 16, sizeof (cspec.VLAN_TAG)));
  281. cspec.VLAN_TAG = htonl (cspec.VLAN_TAG);
  282. break;
  283. case 'V':
  284. cspec.CSPEC_VERSION = (uint16_t)(basespec (optarg, 10, sizeof (cspec.CSPEC_VERSION)));
  285. cspec.CSPEC_VERSION = HTOLE16 (cspec.CSPEC_VERSION);
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. argc -= optind;
  292. argv += optind;
  293. #if defined (DUMP_RULE_ARGS)
  294. /*
  295. * print arguments on stdout;
  296. */
  297. for (c = 0; c &lt; argc; c++)
  298. {
  299. printf (&quot;argv [%d] = [%s]\n&quot;, c, argv [c]);
  300. }
  301. #endif
  302. if ((argc) &amp;&amp; (*argv))
  303. {
  304. ParseRule (&amp;argc, &amp;argv, &amp;rule, &amp;cspec);
  305. #if defined (PRINT_RULES)
  306. /*
  307. * print MMERule set on stdout in human readable format; these rules are populated by ParseRule but members
  308. * are not PIB ready; they are not properly grouped, byte-aligned or endian-ized;
  309. */
  310. MMERuleDump (&amp;rule);
  311. #endif
  312. if ((rule.NUM_CLASSIFIERS &gt; 1) || (rule.MACTION == ACTION_STRIPTX) || (rule.MACTION == ACTION_STRIPRX) || (rule.MACTION == ACTION_TAGTX) || (classifiers-&gt;priority_count &gt;= RULE_MAX_PRIORITY_MAPS))
  313. {
  314. struct auto_connection * auto_connection;
  315. if (classifiers-&gt;autoconn_count &gt;= RULE_MAX_AUTOCONN)
  316. {
  317. error (1, ENOTSUP, &quot;Too many auto connection rules&quot;);
  318. }
  319. auto_connection = &amp;classifiers-&gt;auto_connection [classifiers-&gt;autoconn_count];
  320. auto_connection-&gt;MACTION = rule.MACTION;
  321. auto_connection-&gt;MOPERAND = rule.MOPERAND;
  322. auto_connection-&gt;NUM_CLASSIFIERS = HTOLE16 ((uint16_t)(rule.NUM_CLASSIFIERS));
  323. for (c = 0; c &lt; RULE_MAX_CLASSIFIERS; c++)
  324. {
  325. auto_connection-&gt;CLASSIFIER [c].CR_PID = HTOLE32 ((uint32_t)(0xFF));
  326. auto_connection-&gt;CLASSIFIER [c].CR_OPERAND = HTOLE32 ((uint32_t)(0xFF));
  327. }
  328. for (c = 0; c &lt; rule.NUM_CLASSIFIERS; c++)
  329. {
  330. auto_connection-&gt;CLASSIFIER [c].CR_PID = HTOLE32 ((uint32_t)(rule.CLASSIFIER [c].CR_PID));
  331. auto_connection-&gt;CLASSIFIER [c].CR_OPERAND = HTOLE32 ((uint32_t)(rule.CLASSIFIER [c].CR_OPERAND));
  332. memcpy (auto_connection-&gt;CLASSIFIER [c].CR_VALUE, rule.CLASSIFIER [c].CR_VALUE, sizeof (auto_connection-&gt;CLASSIFIER [c].CR_VALUE));
  333. }
  334. memcpy (&amp;auto_connection-&gt;cspec, &amp;rule.cspec, sizeof (auto_connection-&gt;cspec));
  335. classifiers-&gt;autoconn_count++;
  336. }
  337. else
  338. {
  339. struct classifier_priority_map * classifier_priority_map;
  340. if (classifiers-&gt;priority_count &gt;= RULE_MAX_PRIORITY_MAPS)
  341. {
  342. error (1, ENOTSUP, &quot;Too many priority map rules&quot;);
  343. }
  344. classifier_priority_map = &amp;classifiers-&gt;classifier_priority_map [classifiers-&gt;priority_count];
  345. classifier_priority_map-&gt;Priority = HTOLE32 ((uint32_t)(rule.MACTION));
  346. classifier_priority_map-&gt;CLASSIFIER.CR_PID = HTOLE32 ((uint32_t)(rule.CLASSIFIER [0].CR_PID));
  347. classifier_priority_map-&gt;CLASSIFIER.CR_OPERAND = HTOLE32 ((uint32_t)(rule.CLASSIFIER [0].CR_OPERAND));
  348. memcpy (classifier_priority_map-&gt;CLASSIFIER.CR_VALUE, rule.CLASSIFIER [0].CR_VALUE, sizeof (classifier_priority_map-&gt;CLASSIFIER.CR_VALUE));
  349. classifiers-&gt;priority_count++;
  350. }
  351. }
  352. return;
  353. }
  354. /*====================================================================*
  355. *
  356. * int main (int argc, char const * argv[]);
  357. *
  358. *
  359. *--------------------------------------------------------------------*/
  360. int main (int argc, char const * argv [])
  361. {
  362. static char const * optv [] =
  363. {
  364. &quot;eo:qv&quot;,
  365. &quot;pibfile &lt; rules&quot;,
  366. &quot;Atheros Classification Rule Insert Utility&quot;,
  367. &quot;e\tredirect stderr messages to stdout&quot;,
  368. &quot;o x\talternate offset is (x)&quot;,
  369. &quot;q\tquiet mode&quot;,
  370. &quot;v\tverbose mode&quot;,
  371. (char const *) (0)
  372. };
  373. struct PIBClassifiers classifiers;
  374. char const * vector [24];
  375. uint32_t offset = 0;
  376. uint32_t version;
  377. signed fd;
  378. signed count;
  379. flag_t flags = (flag_t)(0);
  380. signed c;
  381. optind = 1;
  382. while ((c = getoptv (argc, argv, optv)) != -1)
  383. {
  384. switch (c)
  385. {
  386. case 'e':
  387. dup2 (STDOUT_FILENO, STDERR_FILENO);
  388. break;
  389. case 'o':
  390. offset = (unsigned) (basespec (optarg, 16, sizeof (offset)));
  391. break;
  392. case 'q':
  393. _setbits (flags, PIB_SILENCE);
  394. break;
  395. case 'v':
  396. _setbits (flags, PIB_VERBOSE);
  397. break;
  398. default:
  399. break;
  400. }
  401. }
  402. argc -= optind;
  403. argv += optind;
  404. memset (&amp;classifiers, 0, sizeof (classifiers));
  405. while ((count = getargv (SIZEOF (vector), vector)))
  406. {
  407. make_rule (count, vector, &amp;classifiers);
  408. }
  409. classifiers.priority_count = HTOLE32 (classifiers.priority_count);
  410. classifiers.autoconn_count = HTOLE32 (classifiers.autoconn_count);
  411. #if defined (PRINT_CLASSIFIERS)
  412. /*
  413. * print PIB-ready classification rules on stdout in human readable format; these rules are written
  414. * directly into the appropriate location in the PIB as a single block;
  415. */
  416. PIBClassifiersDump (&amp;classifiers);
  417. #endif
  418. #if defined (WRITE_CLASSIFIERS)
  419. write (STDOUT_FILENO, &amp;classifiers, sizeof (classifiers));
  420. #endif
  421. while ((argc) &amp;&amp; (*argv))
  422. {
  423. if ((fd = open (*argv, O_BINARY|O_RDWR)) == -1)
  424. {
  425. error (0, errno, &quot;%s&quot;, *argv);
  426. }
  427. else if (read (fd, &amp;version, sizeof (version)) != sizeof (version))
  428. {
  429. error (0, errno, FILE_CANTREAD, *argv);
  430. }
  431. else if (lseek (fd, 0, SEEK_SET))
  432. {
  433. error (0, errno, FILE_CANTHOME, *argv);
  434. }
  435. else if (LE32TOH (version) == 0x00010001)
  436. {
  437. struct nvm_header2 nvm_header;
  438. if (!nvmseek2 (fd, *argv, &amp;nvm_header, NVM_IMAGE_PIB))
  439. {
  440. pibimage2 (fd, *argv, &amp;nvm_header, offset? offset: PIB_OFFSET2, &amp;classifiers);
  441. }
  442. }
  443. else
  444. {
  445. pibimage1 (fd, *argv, offset? offset: PIB_OFFSET1, &amp;classifiers);
  446. }
  447. close (fd);
  448. argc--;
  449. argv++;
  450. }
  451. exit (0);
  452. }
  453. </pre>
  454. <div class='footerlink'>
  455. [<a href='pibruin1.c.html' title=' pibruin1.c '>PREV</a>]
  456. [<a href='toolkit.html' title=' Index '>HOME</a>]
  457. [<a href='pibrump.c.html' title=' pibrump.c '>NEXT</a>]
  458. </div>
  459. </body>
  460. </html>