pibruin1.c 12 KB

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