amprule.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. * amprule.c - Qualcomm Atheros Message MakeRule 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 AMPRULE_CSPEC_VERSION 0
  126. #define AMPRULE_VLAN_TAG 0x00000000
  127. #define COMMA ","
  128. #define QUOTE "''"
  129. /*====================================================================*
  130. *
  131. * int main (int argc, char const * argv[]);
  132. *
  133. *
  134. *--------------------------------------------------------------------*/
  135. int main (int argc, char const * argv [])
  136. {
  137. extern struct channel channel;
  138. static char const * optv [] =
  139. {
  140. "ei:qrst:T:vV:",
  141. "action operand condition [...] control volatility [device] [...]\n\n where a condition is: field operator value",
  142. "Qualcomm Atheros Stream MakeRule Utility",
  143. "e\tredirect stderr to stdout",
  144. #if defined (WINPCAP) || defined (LIBPCAP)
  145. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  146. #else
  147. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  148. #endif
  149. "q\tquiet mode",
  150. "r\tread rules from device",
  151. "s\tdisplay symbol tables",
  152. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  153. "T x\tinserted vlan tag is x [" LITERAL (AMPRULE_VLAN_TAG) "]",
  154. "v\tverbose mode",
  155. "V n\tcspec version is n [" LITERAL (AMPRULE_CSPEC_VERSION) "]",
  156. (char const *) (0)
  157. };
  158. #include "../plc/plc.c"
  159. struct cspec cspec;
  160. struct MMERule rule;
  161. signed c;
  162. memset (& rule, 0, sizeof (rule));
  163. memset (& cspec, 0, sizeof (cspec));
  164. cspec.VLAN_TAG = AMPRULE_VLAN_TAG;
  165. cspec.CSPEC_VERSION = AMPRULE_CSPEC_VERSION;
  166. if (getenv (PLCDEVICE))
  167. {
  168. #if defined (WINPCAP) || defined (LIBPCAP)
  169. channel.ifindex = atoi (getenv (PLCDEVICE));
  170. #else
  171. channel.ifname = strdup (getenv (PLCDEVICE));
  172. #endif
  173. }
  174. optind = 1;
  175. while ((c = getoptv (argc, argv, optv)) != - 1)
  176. {
  177. switch (c)
  178. {
  179. case 'e':
  180. dup2 (STDOUT_FILENO, STDERR_FILENO);
  181. break;
  182. case 'i':
  183. #if defined (WINPCAP) || defined (LIBPCAP)
  184. channel.ifindex = atoi (optarg);
  185. #else
  186. channel.ifname = optarg;
  187. #endif
  188. break;
  189. case 'q':
  190. _setbits (channel.flags, CHANNEL_SILENCE);
  191. _setbits (plc.flags, PLC_SILENCE);
  192. break;
  193. case 'r':
  194. _setbits (plc.flags, PLC_ANALYSE);
  195. break;
  196. case 's':
  197. printf ("\n");
  198. printf (" Controls are ");
  199. codelist (controls, SIZEOF (controls), COMMA, QUOTE, stdout);
  200. printf (".\n");
  201. printf (" Volatilities are ");
  202. codelist (volatilities, SIZEOF (volatilities), COMMA, QUOTE, stdout);
  203. printf (".\n");
  204. printf (" Actions are ");
  205. codelist (actions, SIZEOF (actions), COMMA, QUOTE, stdout);
  206. printf (".\n");
  207. printf (" Operands are ");
  208. codelist (operands, SIZEOF (operands), COMMA, QUOTE, stdout);
  209. printf (".\n");
  210. printf (" Fields are ");
  211. codelist (fields, SIZEOF (fields), COMMA, QUOTE, stdout);
  212. printf (".\n");
  213. printf (" Operators are ");
  214. codelist (operators, SIZEOF (operators), COMMA, QUOTE, stdout);
  215. printf (".\n");
  216. printf (" States are ");
  217. codelist (states, SIZEOF (states), COMMA, QUOTE, stdout);
  218. printf (".\n");
  219. printf ("\n");
  220. return (0);
  221. case 't':
  222. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  223. break;
  224. case 'T':
  225. cspec.VLAN_TAG = (uint32_t) (basespec (optarg, 16, sizeof (cspec.VLAN_TAG)));
  226. cspec.VLAN_TAG = htonl (cspec.VLAN_TAG);
  227. break;
  228. case 'v':
  229. _setbits (channel.flags, CHANNEL_VERBOSE);
  230. _setbits (plc.flags, PLC_VERBOSE);
  231. break;
  232. case 'V':
  233. cspec.CSPEC_VERSION = (uint16_t) (basespec (optarg, 10, sizeof (cspec.CSPEC_VERSION)));
  234. cspec.CSPEC_VERSION = HTOLE16 (cspec.CSPEC_VERSION);
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. argc -= optind;
  241. argv += optind;
  242. if (_allclr (plc.flags, PLC_ANALYSE))
  243. {
  244. if (ParseRule (& argc, & argv, & rule, & cspec) == - 1)
  245. {
  246. error (1, 0, "invalid rule");
  247. }
  248. }
  249. openchannel (& channel);
  250. if (!(plc.message = malloc (sizeof (* plc.message))))
  251. {
  252. error (1, errno, PLC_NOMEMORY);
  253. }
  254. if (!argc)
  255. {
  256. if (_anyset (plc.flags, PLC_ANALYSE))
  257. {
  258. ReadRules (& plc);
  259. }
  260. else
  261. {
  262. MakeRule (& plc, & rule);
  263. }
  264. }
  265. while ((argc) && (* argv))
  266. {
  267. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  268. {
  269. error (1, errno, PLC_BAD_MAC, * argv);
  270. }
  271. if (_anyset (plc.flags, PLC_ANALYSE))
  272. {
  273. ReadRules (& plc);
  274. }
  275. else
  276. {
  277. MakeRule (& plc, & rule);
  278. }
  279. argc--;
  280. argv++;
  281. }
  282. free (plc.message);
  283. closechannel (& channel);
  284. exit (0);
  285. }