plcrule.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. * plcrule.c - Qualcomm Atheros Message MakeRules Utility;
  44. *
  45. * Contributor(s):
  46. * Charles Maier
  47. *
  48. *--------------------------------------------------------------------*/
  49. /*====================================================================*
  50. * system header files;
  51. *--------------------------------------------------------------------*/
  52. #include <unistd.h>
  53. #include <stdlib.h>
  54. #include <stdint.h>
  55. #include <limits.h>
  56. /*====================================================================*
  57. * custom header files;
  58. *--------------------------------------------------------------------*/
  59. #include "../tools/getoptv.h"
  60. #include "../tools/putoptv.h"
  61. #include "../tools/memory.h"
  62. #include "../tools/number.h"
  63. #include "../tools/symbol.h"
  64. #include "../tools/types.h"
  65. #include "../tools/flags.h"
  66. #include "../tools/files.h"
  67. #include "../tools/error.h"
  68. #include "../plc/plc.h"
  69. #include "../plc/rules.h"
  70. /*====================================================================*
  71. * custom source files;
  72. *--------------------------------------------------------------------*/
  73. #ifndef MAKEFILE
  74. #include "../tools/getoptv.c"
  75. #include "../tools/putoptv.c"
  76. #include "../tools/version.c"
  77. #include "../tools/basespec.c"
  78. #include "../tools/hexdump.c"
  79. #include "../tools/hexencode.c"
  80. #include "../tools/bytespec.c"
  81. #include "../tools/ipv4spec.c"
  82. #include "../tools/ipv6spec.c"
  83. #include "../tools/uintspec.c"
  84. #include "../tools/hexdecode.c"
  85. #include "../tools/hexstring.c"
  86. #include "../tools/todigit.c"
  87. #include "../tools/error.c"
  88. #include "../tools/synonym.c"
  89. #include "../tools/assist.c"
  90. #include "../tools/lookup.c"
  91. #include "../tools/codelist.c"
  92. #include "../tools/memout.c"
  93. #include "../tools/reword.c"
  94. #endif
  95. #ifndef MAKEFILE
  96. #include "../plc/Confirm.c"
  97. #include "../plc/Display.c"
  98. #include "../plc/Failure.c"
  99. #include "../plc/ReadMME.c"
  100. #include "../plc/Request.c"
  101. #include "../plc/SendMME.c"
  102. #include "../plc/Devices.c"
  103. #include "../plc/rules.c"
  104. #include "../plc/ParseRule.c"
  105. #include "../plc/PrintRule.c"
  106. #include "../plc/MakeRule.c"
  107. #include "../plc/ReadRules.c"
  108. #endif
  109. #ifndef MAKEFILE
  110. #include "../ether/channel.c"
  111. #include "../ether/openchannel.c"
  112. #include "../ether/closechannel.c"
  113. #include "../ether/readpacket.c"
  114. #include "../ether/sendpacket.c"
  115. #endif
  116. #ifndef MAKEFILE
  117. #include "../mme/MMECode.c"
  118. #include "../mme/EthernetHeader.c"
  119. #include "../mme/QualcommHeader.c"
  120. #include "../mme/UnwantedMessage.c"
  121. #endif
  122. /*====================================================================*
  123. * program constants;
  124. *--------------------------------------------------------------------*/
  125. #define PLCRULE_CSPEC_VERSION 0
  126. #define PLCRULE_VLAN_TAG 0x00000000
  127. #define COMMA ","
  128. #define QUOTE "''"
  129. /*====================================================================*
  130. *
  131. * int main (int argc, char const * argv[]);
  132. *
  133. *--------------------------------------------------------------------*/
  134. int main (int argc, char const * argv [])
  135. {
  136. extern struct channel channel;
  137. static char const * optv [] =
  138. {
  139. "ei:qrst:T:vV:",
  140. "action operand condition [...] control volatility [device] [...]\n\n where a condition is: field operator value",
  141. "Qualcomm Atheros Stream MakeRules Utility",
  142. "e\tredirect stderr to stdout",
  143. #if defined (WINPCAP) || defined (LIBPCAP)
  144. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  145. #else
  146. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  147. #endif
  148. "q\tquiet mode",
  149. "r\tread rules from device",
  150. "s\tdisplay symbol tables",
  151. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  152. "T x\tinserted vlan tag is x [" LITERAL (PLCRULE_VLAN_TAG) "]",
  153. "v\tverbose mode",
  154. "V n\tcspec version is n [" LITERAL (PLCRULE_CSPEC_VERSION) "]",
  155. (char const *) (0)
  156. };
  157. #include "../plc/plc.c"
  158. struct cspec cspec;
  159. struct MMERule rule;
  160. signed c;
  161. memset (& rule, 0, sizeof (rule));
  162. memset (& cspec, 0, sizeof (cspec));
  163. cspec.VLAN_TAG = PLCRULE_VLAN_TAG;
  164. cspec.CSPEC_VERSION = PLCRULE_CSPEC_VERSION;
  165. if (getenv (PLCDEVICE))
  166. {
  167. #if defined (WINPCAP) || defined (LIBPCAP)
  168. channel.ifindex = atoi (getenv (PLCDEVICE));
  169. #else
  170. channel.ifname = strdup (getenv (PLCDEVICE));
  171. #endif
  172. }
  173. optind = 1;
  174. while (~ (c = getoptv (argc, argv, optv)))
  175. {
  176. switch (c)
  177. {
  178. case 'e':
  179. dup2 (STDOUT_FILENO, STDERR_FILENO);
  180. break;
  181. case 'i':
  182. #if defined (WINPCAP) || defined (LIBPCAP)
  183. channel.ifindex = atoi (optarg);
  184. #else
  185. channel.ifname = optarg;
  186. #endif
  187. break;
  188. case 'q':
  189. _setbits (channel.flags, CHANNEL_SILENCE);
  190. _setbits (plc.flags, PLC_SILENCE);
  191. break;
  192. case 'r':
  193. _setbits (plc.flags, PLC_ANALYSE);
  194. break;
  195. case 's':
  196. printf ("\n");
  197. printf (" Controls are ");
  198. codelist (controls, SIZEOF (controls), COMMA, QUOTE, stdout);
  199. printf (".\n");
  200. printf (" Volatilities are ");
  201. codelist (volatilities, SIZEOF (volatilities), COMMA, QUOTE, stdout);
  202. printf (".\n");
  203. printf (" Actions are ");
  204. codelist (actions, SIZEOF (actions), COMMA, QUOTE, stdout);
  205. printf (".\n");
  206. printf (" Operands are ");
  207. codelist (operands, SIZEOF (operands), COMMA, QUOTE, stdout);
  208. printf (".\n");
  209. printf (" Fields are ");
  210. codelist (fields, SIZEOF (fields), COMMA, QUOTE, stdout);
  211. printf (".\n");
  212. printf (" Operators are ");
  213. codelist (operators, SIZEOF (operators), COMMA, QUOTE, stdout);
  214. printf (".\n");
  215. printf (" States are ");
  216. codelist (states, SIZEOF (states), COMMA, QUOTE, stdout);
  217. printf (".\n");
  218. printf ("\n");
  219. return (0);
  220. case 't':
  221. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  222. break;
  223. case 'T':
  224. cspec.VLAN_TAG = (uint32_t) (basespec (optarg, 16, sizeof (cspec.VLAN_TAG)));
  225. cspec.VLAN_TAG = htonl (cspec.VLAN_TAG);
  226. break;
  227. case 'v':
  228. _setbits (channel.flags, CHANNEL_VERBOSE);
  229. _setbits (plc.flags, PLC_VERBOSE);
  230. break;
  231. case 'V':
  232. cspec.CSPEC_VERSION = (uint16_t) (basespec (optarg, 10, sizeof (cspec.CSPEC_VERSION)));
  233. cspec.CSPEC_VERSION = HTOLE16 (cspec.CSPEC_VERSION);
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. argc -= optind;
  240. argv += optind;
  241. if (_allclr (plc.flags, PLC_ANALYSE))
  242. {
  243. if (ParseRule (& argc, & argv, & rule, & cspec) == -1)
  244. {
  245. error (1, 0, "invalid rule");
  246. }
  247. }
  248. openchannel (& channel);
  249. if (! (plc.message = malloc (sizeof (* plc.message))))
  250. {
  251. error (1, errno, PLC_NOMEMORY);
  252. }
  253. if (! argc)
  254. {
  255. if (_anyset (plc.flags, PLC_ANALYSE))
  256. {
  257. ReadRules (& plc);
  258. }
  259. else
  260. {
  261. MakeRule (& plc, & rule);
  262. }
  263. }
  264. while ((argc) && (* argv))
  265. {
  266. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  267. {
  268. error (1, errno, PLC_BAD_MAC, * argv);
  269. }
  270. if (_anyset (plc.flags, PLC_ANALYSE))
  271. {
  272. ReadRules (& plc);
  273. }
  274. else
  275. {
  276. MakeRule (& plc, & rule);
  277. }
  278. argc--;
  279. argv++;
  280. }
  281. free (plc.message);
  282. closechannel (& channel);
  283. exit (0);
  284. }