pibruin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. * pibruin.c - Atheros Classification Rule Insert Utility;
  44. *
  45. *
  46. * Contributor(s):
  47. * Nathaniel Houghton
  48. * Charles Maier
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*"
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <ctype.h>
  55. #include <stdio.h>
  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/number.h"
  68. #include "../tools/symbol.h"
  69. #include "../tools/chars.h"
  70. #include "../tools/types.h"
  71. #include "../tools/flags.h"
  72. #include "../tools/files.h"
  73. #include "../tools/error.h"
  74. #include "../plc/rules.h"
  75. #include "../nvm/nvm.h"
  76. #include "../pib/pib.h"
  77. /*====================================================================*
  78. * custom source files;
  79. *--------------------------------------------------------------------*/
  80. #ifndef MAKEFILE
  81. #include "../tools/getoptv.c"
  82. #include "../tools/putoptv.c"
  83. #include "../tools/version.c"
  84. #include "../tools/lookup.c"
  85. #include "../tools/reword.c"
  86. #include "../tools/hexstring.c"
  87. #include "../tools/hexdecode.c"
  88. #include "../tools/fdchecksum32.c"
  89. #include "../tools/checksum32.c"
  90. #include "../tools/memout.c"
  91. #include "../tools/assist.c"
  92. #include "../tools/bytespec.c"
  93. #include "../tools/basespec.c"
  94. #include "../tools/ipv4spec.c"
  95. #include "../tools/ipv6spec.c"
  96. #include "../tools/emalloc.c"
  97. #include "../tools/todigit.c"
  98. #include "../tools/codelist.c"
  99. #include "../tools/getargv.c"
  100. #include "../tools/error.c"
  101. #endif
  102. #ifndef MAKEFILE
  103. #include "../pib/pibfile1.c"
  104. #include "../pib/piblock.c"
  105. #if defined (PRINT_RULES) || defined (PRINT_CLASSIFIERS)
  106. #include "../pib/ruledump.c"
  107. #endif
  108. #endif
  109. #ifndef MAKEFILE
  110. #include "../nvm/nvmseek2.c"
  111. #endif
  112. #ifndef MAKEFILE
  113. #include "../plc/ParseRule.c"
  114. #include "../plc/rules.c"
  115. #endif
  116. /*====================================================================*
  117. * program constants;
  118. *--------------------------------------------------------------------*/
  119. #define PIB_OFFSET1 0x0228
  120. #define PIB_OFFSET2 0x0760
  121. /*====================================================================*
  122. *
  123. * signed pibimage1 (signed fd, char const * filename, unsigned offset, struct PIBClassifiers * classifiers);
  124. *
  125. * read thunderbolt/lightning parameter block into memory, edit it
  126. * and save it;
  127. *
  128. *
  129. * Contributor(s):
  130. * Charles Maier
  131. *
  132. *--------------------------------------------------------------------*/
  133. static signed pibimage1 (signed fd, char const * filename, unsigned offset, struct PIBClassifiers * classifiers)
  134. {
  135. struct pib_header * pib_header;
  136. off_t extent;
  137. void * memory;
  138. if ((extent = lseek (fd, 0, SEEK_END)) == -1)
  139. {
  140. error (1, errno, FILE_CANTSIZE, filename);
  141. }
  142. if (!(memory = malloc (extent)))
  143. {
  144. error (1, errno, FILE_CANTLOAD, filename);
  145. }
  146. if (lseek (fd, 0, SEEK_SET))
  147. {
  148. error (1, errno, FILE_CANTHOME, filename);
  149. }
  150. if (read (fd, memory, extent) != extent)
  151. {
  152. error (1, errno, FILE_CANTREAD, filename);
  153. }
  154. if (lseek (fd, 0, SEEK_SET))
  155. {
  156. error (1, errno, FILE_CANTHOME, filename);
  157. }
  158. memcpy ((byte *)(memory) + offset, classifiers, sizeof (*classifiers));
  159. pib_header = (struct pib_header *)(memory);
  160. pib_header->CHECKSUM = checksum32 (memory, extent, pib_header->CHECKSUM);
  161. if (write (fd, memory, extent) != extent)
  162. {
  163. error (1, errno, FILE_CANTSAVE, filename);
  164. }
  165. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  166. {
  167. error (1, errno, FILE_CANTHOME, filename);
  168. }
  169. free (memory);
  170. close (fd);
  171. return (0);
  172. }
  173. /*====================================================================*
  174. *
  175. * signed pibimage2 (signed fd, char const * filename, struct nvm_header2 * nvm_header, unsigned offset, struct PIBClassifiers * classifiers);
  176. *
  177. * read panther/lynx parameter block into memory, edit it and save
  178. * it;
  179. *
  180. *
  181. * Contributor(s):
  182. * Charles Maier
  183. *
  184. *--------------------------------------------------------------------*/
  185. static signed pibimage2 (signed fd, char const * filename, struct nvm_header2 * nvm_header, unsigned offset, struct PIBClassifiers * classifiers)
  186. {
  187. void * memory;
  188. off_t extent = LE32TOH (nvm_header->ImageLength);
  189. if (!(memory = malloc (extent)))
  190. {
  191. error (1, errno, FILE_CANTLOAD, filename);
  192. }
  193. if (read (fd, memory, extent) != extent)
  194. {
  195. error (1, errno, FILE_CANTREAD, filename);
  196. }
  197. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  198. {
  199. error (1, errno, FILE_CANTHOME, filename);
  200. }
  201. memcpy ((byte *)(memory) + offset, classifiers, sizeof (*classifiers));
  202. nvm_header->ImageChecksum = checksum32 (memory, extent, 0);
  203. if (write (fd, memory, extent) != extent)
  204. {
  205. error (1, errno, FILE_CANTSAVE, filename);
  206. }
  207. if (lseek (fd, (off_t)(0) - extent, SEEK_CUR) == -1)
  208. {
  209. error (1, errno, FILE_CANTHOME, filename);
  210. }
  211. nvm_header->HeaderChecksum = checksum32 (nvm_header, sizeof (* nvm_header), nvm_header->HeaderChecksum);
  212. if (lseek (fd, (off_t)(0) - sizeof (* nvm_header), SEEK_CUR) == -1)
  213. {
  214. error (1, errno, FILE_CANTHOME, filename);
  215. }
  216. if (write (fd, nvm_header, sizeof (* nvm_header)) != sizeof (* nvm_header))
  217. {
  218. error (1, errno, FILE_CANTSAVE, filename);
  219. }
  220. if (lseek (fd, (off_t)(0) - sizeof (* nvm_header), SEEK_CUR) == -1)
  221. {
  222. error (1, errno, FILE_CANTHOME, filename);
  223. }
  224. free (memory);
  225. return (0);
  226. }
  227. /*====================================================================*
  228. *
  229. * void make_rule (int argc, char const * argv [], struct PIBClassifiers * classifiers);
  230. *
  231. * read argv [] and populate struct MMEClassifiers with QCA PLC
  232. * classifier rules; function ParseRule converts each rule to an
  233. * MMERule; we then convert that into one of two formats used to
  234. * store classifier rules in the PIB;
  235. *
  236. *
  237. *--------------------------------------------------------------------*/
  238. static void make_rule (int argc, char const * argv [], struct PIBClassifiers * classifiers)
  239. {
  240. static char const * optv [] =
  241. {
  242. "T:V:",
  243. "",
  244. "Atheros Classification Rule Insert Utility",
  245. "T\ttag",
  246. "V\tversion",
  247. (char const *) (0)
  248. };
  249. struct MMERule rule;
  250. struct cspec cspec;
  251. signed c;
  252. optind = 1;
  253. memset (&rule, 0, sizeof (rule));
  254. memset (&cspec, 0, sizeof (cspec));
  255. while ((c = getoptv (argc, argv, optv)) != -1)
  256. {
  257. switch (c)
  258. {
  259. case 'T':
  260. cspec.VLAN_TAG = (uint32_t)(basespec (optarg, 16, sizeof (cspec.VLAN_TAG)));
  261. cspec.VLAN_TAG = htonl (cspec.VLAN_TAG);
  262. break;
  263. case 'V':
  264. cspec.CSPEC_VERSION = (uint16_t)(basespec (optarg, 10, sizeof (cspec.CSPEC_VERSION)));
  265. cspec.CSPEC_VERSION = HTOLE16 (cspec.CSPEC_VERSION);
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. argc -= optind;
  272. argv += optind;
  273. #if defined (DUMP_RULE_ARGS)
  274. /*
  275. * print arguments on stdout;
  276. */
  277. for (c = 0; c < argc; c++)
  278. {
  279. printf ("argv [%d] = [%s]\n", c, argv [c]);
  280. }
  281. #endif
  282. if ((argc) && (*argv))
  283. {
  284. ParseRule (&argc, &argv, &rule, &cspec);
  285. #if defined (PRINT_RULES)
  286. /*
  287. * print MMERule set on stdout in human readable format; these rules are populated by ParseRule but members
  288. * are not PIB ready; they are not properly grouped, byte-aligned or endian-ized;
  289. */
  290. MMERuleDump (&rule);
  291. #endif
  292. if ((rule.NUM_CLASSIFIERS > 1) || (rule.MACTION == ACTION_STRIPTX) || (rule.MACTION == ACTION_STRIPRX) || (rule.MACTION == ACTION_TAGTX) || (classifiers->priority_count >= RULE_MAX_PRIORITY_MAPS))
  293. {
  294. struct auto_connection * auto_connection;
  295. if (classifiers->autoconn_count >= RULE_MAX_AUTOCONN)
  296. {
  297. error (1, ENOTSUP, "Too many auto connection rules");
  298. }
  299. auto_connection = &classifiers->auto_connection [classifiers->autoconn_count];
  300. auto_connection->MACTION = rule.MACTION;
  301. auto_connection->MOPERAND = rule.MOPERAND;
  302. auto_connection->NUM_CLASSIFIERS = HTOLE16 ((uint16_t)(rule.NUM_CLASSIFIERS));
  303. for (c = 0; c < RULE_MAX_CLASSIFIERS; c++)
  304. {
  305. auto_connection->CLASSIFIER [c].CR_PID = HTOLE32 ((uint32_t)(0xFF));
  306. auto_connection->CLASSIFIER [c].CR_OPERAND = HTOLE32 ((uint32_t)(0xFF));
  307. }
  308. for (c = 0; c < rule.NUM_CLASSIFIERS; c++)
  309. {
  310. auto_connection->CLASSIFIER [c].CR_PID = HTOLE32 ((uint32_t)(rule.CLASSIFIER [c].CR_PID));
  311. auto_connection->CLASSIFIER [c].CR_OPERAND = HTOLE32 ((uint32_t)(rule.CLASSIFIER [c].CR_OPERAND));
  312. memcpy (auto_connection->CLASSIFIER [c].CR_VALUE, rule.CLASSIFIER [c].CR_VALUE, sizeof (auto_connection->CLASSIFIER [c].CR_VALUE));
  313. }
  314. memcpy (&auto_connection->cspec, &rule.cspec, sizeof (auto_connection->cspec));
  315. classifiers->autoconn_count++;
  316. }
  317. else
  318. {
  319. struct classifier_priority_map * classifier_priority_map;
  320. if (classifiers->priority_count >= RULE_MAX_PRIORITY_MAPS)
  321. {
  322. error (1, ENOTSUP, "Too many priority map rules");
  323. }
  324. classifier_priority_map = &classifiers->classifier_priority_map [classifiers->priority_count];
  325. classifier_priority_map->Priority = HTOLE32 ((uint32_t)(rule.MACTION));
  326. classifier_priority_map->CLASSIFIER.CR_PID = HTOLE32 ((uint32_t)(rule.CLASSIFIER [0].CR_PID));
  327. classifier_priority_map->CLASSIFIER.CR_OPERAND = HTOLE32 ((uint32_t)(rule.CLASSIFIER [0].CR_OPERAND));
  328. memcpy (classifier_priority_map->CLASSIFIER.CR_VALUE, rule.CLASSIFIER [0].CR_VALUE, sizeof (classifier_priority_map->CLASSIFIER.CR_VALUE));
  329. classifiers->priority_count++;
  330. }
  331. }
  332. return;
  333. }
  334. /*====================================================================*
  335. *
  336. * int main (int argc, char const * argv[]);
  337. *
  338. *
  339. *--------------------------------------------------------------------*/
  340. int main (int argc, char const * argv [])
  341. {
  342. static char const * optv [] =
  343. {
  344. "eo:qv",
  345. "pibfile < rules",
  346. "Atheros Classification Rule Insert Utility",
  347. "e\tredirect stderr messages to stdout",
  348. "o x\talternate offset is (x)",
  349. "q\tquiet mode",
  350. "v\tverbose mode",
  351. (char const *) (0)
  352. };
  353. struct PIBClassifiers classifiers;
  354. char const * vector [24];
  355. uint32_t offset = 0;
  356. uint32_t version;
  357. signed fd;
  358. signed count;
  359. flag_t flags = (flag_t)(0);
  360. signed c;
  361. optind = 1;
  362. while ((c = getoptv (argc, argv, optv)) != -1)
  363. {
  364. switch (c)
  365. {
  366. case 'e':
  367. dup2 (STDOUT_FILENO, STDERR_FILENO);
  368. break;
  369. case 'o':
  370. offset = (unsigned) (basespec (optarg, 16, sizeof (offset)));
  371. break;
  372. case 'q':
  373. _setbits (flags, PIB_SILENCE);
  374. break;
  375. case 'v':
  376. _setbits (flags, PIB_VERBOSE);
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. argc -= optind;
  383. argv += optind;
  384. memset (&classifiers, 0, sizeof (classifiers));
  385. while ((count = getargv (SIZEOF (vector), vector)))
  386. {
  387. make_rule (count, vector, &classifiers);
  388. }
  389. classifiers.priority_count = HTOLE32 (classifiers.priority_count);
  390. classifiers.autoconn_count = HTOLE32 (classifiers.autoconn_count);
  391. #if defined (PRINT_CLASSIFIERS)
  392. /*
  393. * print PIB-ready classification rules on stdout in human readable format; these rules are written
  394. * directly into the appropriate location in the PIB as a single block;
  395. */
  396. PIBClassifiersDump (&classifiers);
  397. #endif
  398. #if defined (WRITE_CLASSIFIERS)
  399. write (STDOUT_FILENO, &classifiers, sizeof (classifiers));
  400. #endif
  401. while ((argc) && (*argv))
  402. {
  403. if ((fd = open (*argv, O_BINARY|O_RDWR)) == -1)
  404. {
  405. error (0, errno, "%s", *argv);
  406. }
  407. else if (read (fd, &version, sizeof (version)) != sizeof (version))
  408. {
  409. error (0, errno, FILE_CANTREAD, *argv);
  410. }
  411. else if (lseek (fd, 0, SEEK_SET))
  412. {
  413. error (0, errno, FILE_CANTHOME, *argv);
  414. }
  415. else if (LE32TOH (version) == 0x00010001)
  416. {
  417. struct nvm_header2 nvm_header;
  418. if (!nvmseek2 (fd, *argv, &nvm_header, NVM_IMAGE_PIB))
  419. {
  420. pibimage2 (fd, *argv, &nvm_header, offset? offset: PIB_OFFSET2, &classifiers);
  421. }
  422. }
  423. else
  424. {
  425. pibimage1 (fd, *argv, offset? offset: PIB_OFFSET1, &classifiers);
  426. }
  427. close (fd);
  428. argc--;
  429. argv++;
  430. }
  431. exit (0);
  432. }