pibruin1.c.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. pibruin1.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='pibpeek2.c.html' title=' pibpeek2.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='pibruin.c.html' title=' pibruin.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. * pibruin1.c - Atheros Classification Rule Insert Utility;
  64. *
  65. * This inserts classification rules into pib files from a rule
  66. * description file;
  67. *
  68. *
  69. * Contributor(s):
  70. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  71. *
  72. *--------------------------------------------------------------------*/
  73. /*====================================================================*&quot;
  74. * system header files;
  75. *--------------------------------------------------------------------*/
  76. #include &lt;ctype.h&gt;
  77. #include &lt;unistd.h&gt;
  78. #include &lt;stdlib.h&gt;
  79. #include &lt;stdint.h&gt;
  80. #include &lt;string.h&gt;
  81. #include &lt;netinet/in.h&gt;
  82. #include &lt;arpa/inet.h&gt;
  83. /*====================================================================*
  84. * custom header files;
  85. *--------------------------------------------------------------------*/
  86. #include &quot;../tools/getoptv.h&quot;
  87. #include &quot;../tools/memory.h&quot;
  88. #include &quot;../tools/number.h&quot;
  89. #include &quot;../tools/symbol.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;../pib/pib.h&quot;
  96. /*====================================================================*
  97. * custom source files;
  98. *--------------------------------------------------------------------*/
  99. #ifndef MAKEFILE
  100. #include &quot;../tools/getoptv.c&quot;
  101. #include &quot;../tools/putoptv.c&quot;
  102. #include &quot;../tools/version.c&quot;
  103. #include &quot;../tools/lookup.c&quot;
  104. #include &quot;../tools/reword.c&quot;
  105. #include &quot;../tools/hexstring.c&quot;
  106. #include &quot;../tools/hexdecode.c&quot;
  107. #include &quot;../tools/fdchecksum32.c&quot;
  108. #include &quot;../tools/memout.c&quot;
  109. #include &quot;../tools/assist.c&quot;
  110. #include &quot;../tools/bytespec.c&quot;
  111. #include &quot;../tools/basespec.c&quot;
  112. #include &quot;../tools/ipv4spec.c&quot;
  113. #include &quot;../tools/ipv6spec.c&quot;
  114. #include &quot;../tools/endian.c&quot;
  115. #include &quot;../tools/emalloc.c&quot;
  116. #include &quot;../tools/todigit.c&quot;
  117. #include &quot;../tools/codelist.c&quot;
  118. #include &quot;../tools/error.c&quot;
  119. #endif
  120. #ifndef MAKEFILE
  121. #include &quot;../pib/pibfile1.c&quot;
  122. #include &quot;../pib/piblock.c&quot;
  123. #endif
  124. #ifndef MAKEFILE
  125. #include &quot;../plc/rules.c&quot;
  126. #include &quot;../plc/ParseRule.c&quot;
  127. #endif
  128. /*====================================================================*
  129. * program variables;
  130. *--------------------------------------------------------------------*/
  131. #ifndef __GNUC__
  132. #pragma pack (push,1)
  133. #endif
  134. typedef struct __packed classifier_pib
  135. {
  136. uint32_t CR_PID;
  137. uint32_t CR_OPERAND;
  138. uint8_t CR_VALUE [16];
  139. }
  140. classifier_pib;
  141. struct __packed auto_connection
  142. {
  143. uint8_t MACTION;
  144. uint8_t MOPERAND;
  145. uint16_t NUM_CLASSIFIERS;
  146. struct classifier_pib CLASSIFIER [3];
  147. struct cspec cspec;
  148. uint8_t RSVD [14];
  149. }
  150. auto_connection;
  151. struct __packed classifier_priority_map
  152. {
  153. uint32_t Priority;
  154. struct classifier_pib CLASSIFIER;
  155. }
  156. classifier_priority_map;
  157. #ifndef __GNUC__
  158. #pragma pack (pop)
  159. #endif
  160. /*====================================================================*
  161. * program constants;
  162. *--------------------------------------------------------------------*/
  163. #define PIB_MAX_AUTOCONN 16
  164. #define PIB_MAX_PRIORITY_MAPS 8
  165. #define PIB_AUTOCONN_COUNT_OFFSET 0x22C
  166. #define PIB_PRIORITY_COUNT_OFFSET 0x228
  167. #define PIB_AUTOCONN_OFFSET 0x310
  168. #define PIB_PRIORITY_MAPS_OFFSET 0x230
  169. /*====================================================================*
  170. *
  171. * signed handle_rule (char * str, int argc, struct auto_connection * autoconn, struct classifier_priority_map * priority_map, int * autoconn_count, int * priority_count);
  172. *
  173. *
  174. *--------------------------------------------------------------------*/
  175. static signed handle_rule (char * str, int argc, struct auto_connection * autoconn, struct classifier_priority_map * priority_map, int * autoconn_count, int * priority_count)
  176. {
  177. char const ** argv;
  178. char **mem;
  179. unsigned i;
  180. signed c;
  181. size_t len;
  182. struct rule rule;
  183. struct cspec cspec;
  184. static char const * optv [] =
  185. {
  186. &quot;T:V:&quot;,
  187. &quot;&quot;,
  188. &quot;Atheros Classification Rule Insert Utility&quot;,
  189. &quot;T\ttag&quot;,
  190. &quot;V\tversion&quot;,
  191. (char const *) (0)
  192. };
  193. extern char const * program_name;
  194. mem = emalloc ((argc + 2) * sizeof (char *));
  195. argv = (char const **)(mem);
  196. * argv = program_name;
  197. ++argv;
  198. * argv = str;
  199. ++argv;
  200. len = strlen (str);
  201. for (i = 1; i &lt; len; ++i)
  202. {
  203. if (str [i] == ' ')
  204. {
  205. str [i] = '\0';
  206. * argv = &amp;str [i + 1];
  207. ++argv;
  208. }
  209. }
  210. * argv = NULL;
  211. optind = 1;
  212. argv = (char const **)(mem);
  213. memset (&amp;rule, 0, sizeof (rule));
  214. memset (&amp;cspec, 0, sizeof (cspec));
  215. while ((c = getoptv (argc, (char const **)(argv), optv)) != -1)
  216. {
  217. switch ((char) (c))
  218. {
  219. case 'T':
  220. cspec.VLAN_TAG = (uint32_t)(basespec (optarg, 16, sizeof (cspec.VLAN_TAG)));
  221. cspec.VLAN_TAG = htonl (cspec.VLAN_TAG);
  222. break;
  223. case 'V':
  224. cspec.CSPEC_VERSION = (uint16_t)(basespec (optarg, 10, sizeof (cspec.CSPEC_VERSION)));
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. argc -= optind;
  231. argv += optind;
  232. ParseRule (&amp;argc, (char const ***) &amp;argv, &amp;rule, &amp;cspec);
  233. /* Now migrate the rule into the correct PIB structure. */
  234. if (rule.NUM_CLASSIFIERS &gt; 1 || rule.MACTION == ACTION_STRIPTX || rule.MACTION == ACTION_STRIPRX || rule.MACTION == ACTION_TAGTX || *priority_count &gt;= PIB_MAX_PRIORITY_MAPS)
  235. {
  236. if (*autoconn_count &gt;= PIB_MAX_AUTOCONN)
  237. {
  238. error (1, 0, &quot;too many rules&quot;);
  239. }
  240. autoconn = &amp;autoconn [*autoconn_count];
  241. autoconn-&gt;CLASSIFIER [0].CR_PID = 0xFF;
  242. autoconn-&gt;CLASSIFIER [1].CR_PID = 0xFF;
  243. autoconn-&gt;CLASSIFIER [2].CR_PID = 0xFF;
  244. autoconn-&gt;MACTION = rule.MACTION;
  245. autoconn-&gt;MOPERAND = rule.MOPERAND;
  246. autoconn-&gt;NUM_CLASSIFIERS = rule.NUM_CLASSIFIERS;
  247. for (i = 0; i &lt; rule.NUM_CLASSIFIERS; ++i)
  248. {
  249. autoconn-&gt;CLASSIFIER [i].CR_PID = rule.CLASSIFIER [i].CR_PID;
  250. autoconn-&gt;CLASSIFIER [i].CR_OPERAND = rule.CLASSIFIER [i].CR_OPERAND;
  251. memcpy (&amp;autoconn-&gt;CLASSIFIER [i].CR_VALUE, &amp;rule.CLASSIFIER [i].CR_VALUE, sizeof (autoconn-&gt;CLASSIFIER [i].CR_VALUE));
  252. }
  253. memcpy (&amp;autoconn-&gt;cspec, &amp;rule.cspec, sizeof (autoconn-&gt;cspec));
  254. ++(*autoconn_count);
  255. }
  256. else
  257. {
  258. if (*priority_count &gt;= PIB_MAX_PRIORITY_MAPS)
  259. {
  260. error (1, 0, &quot;too many rules&quot;);
  261. }
  262. priority_map = &amp;priority_map [*priority_count];
  263. priority_map-&gt;Priority = rule.MACTION;
  264. priority_map-&gt;CLASSIFIER.CR_PID = rule.CLASSIFIER [0].CR_PID;
  265. priority_map-&gt;CLASSIFIER.CR_OPERAND = rule.CLASSIFIER [0].CR_OPERAND;
  266. memcpy (&amp;priority_map-&gt;CLASSIFIER.CR_VALUE, &amp;rule.CLASSIFIER [0].CR_VALUE, sizeof (priority_map-&gt;CLASSIFIER.CR_VALUE));
  267. ++(*priority_count);
  268. }
  269. free (mem);
  270. return (0);
  271. }
  272. static void read_rules (struct auto_connection auto_connection [], unsigned * autoconn_count, struct classifier_priority_map classifier_priority_map [], unsigned * priority_count)
  273. {
  274. int len = 0;
  275. int wc = 0;
  276. while ((c = getc (stdin)) != EOF)
  277. {
  278. if (isspace (c))
  279. {
  280. continue;
  281. }
  282. if (c == '#')
  283. {
  284. do
  285. {
  286. c = getc (stdin);
  287. }
  288. while (nobreak (c));
  289. continue;
  290. }
  291. len = 0;
  292. wc = 0;
  293. do
  294. {
  295. if (isspace (c))
  296. {
  297. while (c != '\n' &amp;&amp; (c = getc (stdin)) != EOF &amp;&amp; isspace (c))
  298. {
  299. continue;
  300. }
  301. if (len &gt; 0)
  302. {
  303. ++wc;
  304. }
  305. if (c != '\n' &amp;&amp; c != '#')
  306. {
  307. line [len++] = ' ';
  308. if (len == sizeof (line) - 1)
  309. {
  310. error (1, 0, &quot;rule too long&quot;);
  311. }
  312. }
  313. }
  314. if (c == '\n' || c == '#')
  315. {
  316. line [len] = '\0';
  317. handle_rule (line, wc, auto_connection, classifier_priority_map, autoconn_count, priority_count);
  318. len = 0;
  319. wc = 0;
  320. if (c == '#')
  321. {
  322. ungetc (c, stdin);
  323. }
  324. break;
  325. }
  326. line [len++] = c;
  327. if (len == sizeof (line) - 1)
  328. {
  329. error (1, 0, &quot;rule too long&quot;);
  330. }
  331. }
  332. while ((c = getc (stdin)) != EOF);
  333. }
  334. if (len &gt; 0)
  335. {
  336. line [len] = '\0';
  337. handle_rule (line, wc, auto_connection, classifier_priority_map, autoconn_count, priority_count);
  338. }
  339. }
  340. /*====================================================================*
  341. *
  342. * int main (int argc, char const * argv[]);
  343. *
  344. *
  345. *--------------------------------------------------------------------*/
  346. int main (int argc, char const * argv [])
  347. {
  348. static char const * optv [] =
  349. {
  350. &quot;eqv&quot;,
  351. &quot;pibfile &lt; rules&quot;,
  352. &quot;Atheros Classification Rule Insert Utility&quot;,
  353. &quot;e\tredirect stderr messages to stdout&quot;,
  354. &quot;q\tquiet mode&quot;,
  355. &quot;v\tverbose mode&quot;,
  356. (char const *) (0)
  357. };
  358. struct auto_connection auto_connection [PIB_MAX_AUTOCONN];
  359. struct classifier_priority_map classifier_priority_map [PIB_MAX_PRIORITY_MAPS];
  360. unsigned autoconn_count = 0;
  361. unsigned priority_count = 0;
  362. flag_t flags = (flag_t)(0);
  363. struct _file_ pib;
  364. char line [1024];
  365. signed c;
  366. optind = 1;
  367. while ((c = getoptv (argc, argv, optv)) != -1)
  368. {
  369. switch ((char) (c))
  370. {
  371. case 'e':
  372. dup2 (STDOUT_FILENO, STDERR_FILENO);
  373. break;
  374. case 'q':
  375. _setbits (flags, PIB_SILENCE);
  376. break;
  377. case 'v':
  378. _setbits (flags, PIB_VERBOSE);
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. argc -= optind;
  385. argv += optind;
  386. if (!argc)
  387. {
  388. error (1, 0, &quot;must specify PIB file&quot;);
  389. }
  390. memset (&amp;auto_connection, 0, sizeof (auto_connection));
  391. memset (&amp;classifier_priority_map, 0, sizeof (classifier_priority_map));
  392. read_rules (auto_connection, &amp;autoconn_count, classifier_priority_map, &amp;priority_count);
  393. pib.name = * argv;
  394. if ((pib.file = open (pib.name, O_BINARY|O_RDWR, FILE_FILEMODE)) == -1)
  395. {
  396. error (1, errno, &quot;%s&quot;, pib.name);
  397. }
  398. if (pibfile1 (&amp;pib))
  399. {
  400. error (1, errno, &quot;Bad PIB file: %s&quot;, pib.name);
  401. }
  402. if (lseek (pib.file, PIB_AUTOCONN_OFFSET, SEEK_SET) != PIB_AUTOCONN_OFFSET)
  403. {
  404. error (1, errno, &quot;could not seek to AutoConnections&quot;);
  405. }
  406. if (write (pib.file, &amp;auto_connection, sizeof (auto_connection)) != sizeof (auto_connection))
  407. {
  408. error (1, errno, &quot;could not write AutoConnections&quot;);
  409. }
  410. if (lseek (pib.file, PIB_AUTOCONN_COUNT_OFFSET, SEEK_SET) != PIB_AUTOCONN_COUNT_OFFSET)
  411. {
  412. error (1, errno, &quot;could not seek to AutoConnection count&quot;);
  413. }
  414. if (write (pib.file, &amp;autoconn_count, sizeof (autoconn_count)) != sizeof (autoconn_count))
  415. {
  416. error (1, errno, &quot;could not write AutoConnection count&quot;);
  417. }
  418. if (lseek (pib.file, PIB_PRIORITY_MAPS_OFFSET, SEEK_SET) != PIB_PRIORITY_MAPS_OFFSET)
  419. {
  420. error (1, errno, &quot;could not seek to Priority Map&quot;);
  421. }
  422. if (write (pib.file, &amp;classifier_priority_map, sizeof (classifier_priority_map)) != sizeof (classifier_priority_map))
  423. {
  424. error (1, errno, &quot;could not write Priority Map&quot;);
  425. }
  426. if (lseek (pib.file, PIB_PRIORITY_COUNT_OFFSET, SEEK_SET) != PIB_PRIORITY_COUNT_OFFSET)
  427. {
  428. error (1, errno, &quot;could not seek to PriorityMaps count&quot;);
  429. }
  430. if (write (pib.file, &amp;priority_count, sizeof (priority_count)) != sizeof (priority_count))
  431. {
  432. error (1, errno, &quot;could not write PriorityMaps count&quot;);
  433. }
  434. piblock (&amp;pib);
  435. close (pib.file);
  436. exit (0);
  437. }
  438. </pre>
  439. <div class='footerlink'>
  440. [<a href='pibpeek2.c.html' title=' pibpeek2.c '>PREV</a>]
  441. [<a href='toolkit.html' title=' Index '>HOME</a>]
  442. [<a href='pibruin.c.html' title=' pibruin.c '>NEXT</a>]
  443. </div>
  444. </body>
  445. </html>