coqos_add.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. * coqos_add.c - Add MCMSA Stream
  44. *
  45. *
  46. * Contributor(s):
  47. * Bill Wike
  48. * Nathaniel Houghton
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. /*====================================================================*"
  53. * system header files;
  54. *--------------------------------------------------------------------*/
  55. #include <unistd.h>
  56. #include <stdlib.h>
  57. #include <stdint.h>
  58. #include <limits.h>
  59. /*====================================================================*
  60. * custom header files;
  61. *--------------------------------------------------------------------*/
  62. #include "../tools/getoptv.h"
  63. #include "../tools/putoptv.h"
  64. #include "../tools/memory.h"
  65. #include "../tools/number.h"
  66. #include "../tools/symbol.h"
  67. #include "../tools/types.h"
  68. #include "../tools/flags.h"
  69. #include "../tools/files.h"
  70. #include "../tools/error.h"
  71. #include "../plc/plc.h"
  72. #include "../plc/coqos.h"
  73. /*====================================================================*
  74. * custom source files;
  75. *--------------------------------------------------------------------*/
  76. #ifndef MAKEFILE
  77. #include "../tools/getoptv.c"
  78. #include "../tools/putoptv.c"
  79. #include "../tools/version.c"
  80. #include "../tools/basespec.c"
  81. #include "../tools/uintspec.c"
  82. #include "../tools/hexdump.c"
  83. #include "../tools/hexencode.c"
  84. #include "../tools/bytespec.c"
  85. #include "../tools/ipv4spec.c"
  86. #include "../tools/ipv6spec.c"
  87. #include "../tools/hexdecode.c"
  88. #include "../tools/todigit.c"
  89. #include "../tools/typename.c"
  90. #include "../tools/error.c"
  91. #include "../tools/synonym.c"
  92. #include "../tools/lookup.c"
  93. #include "../tools/assist.c"
  94. #include "../tools/codelist.c"
  95. #endif
  96. #ifndef MAKEFILE
  97. #include "../plc/Request.c"
  98. #include "../plc/Confirm.c"
  99. #include "../plc/Failure.c"
  100. #include "../plc/Display.c"
  101. #include "../plc/ReadMME.c"
  102. #include "../plc/SendMME.c"
  103. #include "../plc/Devices.c"
  104. #include "../plc/rules.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../ether/channel.c"
  108. #include "../ether/openchannel.c"
  109. #include "../ether/closechannel.c"
  110. #include "../ether/readpacket.c"
  111. #include "../ether/sendpacket.c"
  112. #endif
  113. #ifndef MAKEFILE
  114. #include "../mme/MMECode.c"
  115. #include "../mme/EthernetHeader.c"
  116. #include "../mme/QualcommHeader.c"
  117. #include "../mme/UnwantedMessage.c"
  118. #endif
  119. /*====================================================================*
  120. *
  121. * signed add_conn (struct plc * plc, struct connection * connection);
  122. *
  123. *
  124. *
  125. * Contributor(s):
  126. * Bill Wike
  127. *
  128. *--------------------------------------------------------------------*/
  129. static signed add_conn (struct plc * plc, struct connection * connection)
  130. {
  131. struct channel * channel = (struct channel *)(plc->channel);
  132. struct message * message = (struct message *)(plc->message);
  133. #ifndef __GNUC__
  134. #pragma pack (push,1)
  135. #endif
  136. struct __packed vs_add_conn_req
  137. {
  138. struct ethernet_hdr ethernet;
  139. struct qualcomm_hdr qualcomm;
  140. struct connection connection;
  141. }
  142. * request = (struct vs_add_conn_req *)(message);
  143. struct __packed vs_add_conn_cnf
  144. {
  145. struct ethernet_hdr ethernet;
  146. struct qualcomm_hdr qualcomm;
  147. uint32_t REQ_ID;
  148. uint8_t MSTATUS;
  149. uint16_t ERR_REC_CODE;
  150. uint32_t RSVD;
  151. uint16_t CID;
  152. uint8_t REJECT_MAC [ETHER_ADDR_LEN];
  153. uint16_t CSPEC_VERSION;
  154. uint8_t CONN_CAP;
  155. uint8_t CONN_COQOS_PRIO;
  156. uint16_t CONN_RATE;
  157. uint32_t CONN_TTL;
  158. }
  159. * confirm = (struct vs_add_conn_cnf *)(message);
  160. #ifndef __GNUC__
  161. #pragma pack (pop)
  162. #endif
  163. Request (plc, "Add COQOS connection");
  164. memset (message, 0, sizeof (* message));
  165. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  166. QualcommHeader (&request->qualcomm, 0, (VS_CONN_ADD | MMTYPE_REQ));
  167. memcpy (&request->connection, connection, sizeof (struct connection));
  168. memcpy (&request->connection.rule.CLASSIFIERS [request->connection.rule.NUM_CLASSIFIERS], &request->connection.cspec, sizeof (request->connection.cspec));
  169. plc->packetsize = sizeof (struct vs_add_conn_req);
  170. if (SendMME (plc) <= 0)
  171. {
  172. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  173. return (-1);
  174. }
  175. while (ReadMME (plc, 0, (VS_CONN_ADD | MMTYPE_CNF)) <= 0)
  176. {
  177. if (confirm->MSTATUS)
  178. {
  179. Failure (plc, "Device won't add connection");
  180. return (-1);
  181. }
  182. Confirm (plc, "Setting ...");
  183. }
  184. return (0);
  185. }
  186. /*====================================================================*
  187. *
  188. * int main (int argc, char const * argv[]);
  189. *
  190. *
  191. *--------------------------------------------------------------------*/
  192. int main (int argc, char const * argv [])
  193. {
  194. extern struct channel channel;
  195. static char const * optv [] =
  196. {
  197. "ei:qv",
  198. "action priority destination rate ttl operand condition [...] [device] [...]\n\n where condition is field operator value",
  199. "CoQos Stream Utility",
  200. "e\tredirect stderr to stdout",
  201. #if defined (WINPCAP) || defined (LIBPCAP)
  202. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  203. #else
  204. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  205. #endif
  206. "q\tquiet mode",
  207. "v\tverbose mode",
  208. (char const *) (0)
  209. };
  210. #include "../plc/plc.c"
  211. struct connection connection;
  212. struct MMEClassifier * rule = (struct MMEClassifier *)(&connection.rule.CLASSIFIERS);
  213. uint16_t * word;
  214. uint8_t * byte;
  215. signed code;
  216. signed c;
  217. if (getenv (PLCDEVICE))
  218. {
  219. #if defined (WINPCAP) || defined (LIBPCAP)
  220. channel.ifindex = atoi (getenv (PLCDEVICE));
  221. #else
  222. channel.ifname = strdup (getenv (PLCDEVICE));
  223. #endif
  224. }
  225. optind = 1;
  226. while ((c = getoptv (argc, argv, optv)) != -1)
  227. {
  228. switch (c)
  229. {
  230. case 'e':
  231. dup2 (STDOUT_FILENO, STDERR_FILENO);
  232. break;
  233. case 'i':
  234. #if defined (WINPCAP) || defined (LIBPCAP)
  235. channel.ifindex = atoi (optarg);
  236. #else
  237. channel.ifname = optarg;
  238. #endif
  239. break;
  240. case 'q':
  241. _setbits (channel.flags, CHANNEL_SILENCE);
  242. _setbits (plc.flags, PLC_SILENCE);
  243. break;
  244. case 'v':
  245. _setbits (channel.flags, CHANNEL_VERBOSE);
  246. _setbits (plc.flags, PLC_VERBOSE);
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. argc -= optind;
  253. argv += optind;
  254. memset (&connection, 0, sizeof (connection));
  255. if ((code = lookup (* argv++, actions, SIZEOF (actions))) == -1)
  256. {
  257. assist (*--argv, CLASSIFIER_ACTION_NAME, actions, SIZEOF (actions));
  258. }
  259. connection.cspec.CONN_CAP = (uint8_t)(code);
  260. argc--;
  261. if (!argc)
  262. {
  263. error (1, ECANCELED, "Expected Priority: 0-15");
  264. }
  265. connection.cspec.CONN_COQOS_PRIO = (uint8_t)(uintspec (* argv++, 0, 15));
  266. argc--;
  267. if (!argc)
  268. {
  269. error (1, ECANCELED, "Expected Destination MAC Address");
  270. }
  271. if (!hexencode (connection.APP_DA, sizeof (connection.APP_DA), synonym (* argv++, devices, SIZEOF (devices))))
  272. {
  273. error (1, errno, "Invalid MAC=[%s]", *--argv);
  274. }
  275. argc--;
  276. if (!argc)
  277. {
  278. error (1, ECANCELED, "Expected Data Rate: 10-9000 (kbps)");
  279. }
  280. connection.cspec.CONN_RATE = (uint16_t)(uintspec (* argv++, 1, 9000));
  281. argc--;
  282. if (!argc)
  283. {
  284. error (1, ECANCELED, "Expected TTL: 10000-2000000 (microseconds)");
  285. }
  286. connection.cspec.CONN_TTL = (uint32_t)(uintspec (* argv++, 10000, 2000000));
  287. argc--;
  288. if ((code = lookup (* argv++, operands, SIZEOF (operands))) == -1)
  289. {
  290. assist (*--argv, CLASSIFIER_OPERAND_NAME, operands, SIZEOF (operands));
  291. }
  292. connection.rule.MOPERAND = (uint8_t)(code);
  293. argc--;
  294. while ((* argv) && (lookup (* argv, controls, SIZEOF (controls)) == -1))
  295. {
  296. if ((code = lookup (* argv++, fields, SIZEOF (fields))) == -1)
  297. {
  298. assist (*--argv, CLASSIFIER_FIELD_NAME, fields, SIZEOF (fields));
  299. }
  300. rule->CR_PID = (uint8_t)(code);
  301. argc--;
  302. if ((code = lookup (* argv++, operators, SIZEOF (operators))) == -1)
  303. {
  304. assist (*--argv, CLASSIFIER_OPERATOR_NAME, operators, SIZEOF (operators));
  305. }
  306. rule->CR_OPERAND = (uint8_t)(code);
  307. argc--;
  308. if (!argc || !* argv)
  309. {
  310. error (1, ENOTSUP, "Have %s '%s' without any value", CLASSIFIER_OPERATOR_NAME, *--argv);
  311. }
  312. switch (rule->CR_PID)
  313. {
  314. case FIELD_ETH_SA:
  315. case FIELD_ETH_DA:
  316. bytespec (* argv++, rule->CR_VALUE, ETHER_ADDR_LEN);
  317. break;
  318. case FIELD_IPV4_SA:
  319. case FIELD_IPV4_DA:
  320. ipv4spec (* argv++, rule->CR_VALUE);
  321. break;
  322. case FIELD_IPV6_SA:
  323. case FIELD_IPV6_DA:
  324. ipv6spec (* argv++, rule->CR_VALUE);
  325. break;
  326. case FIELD_VLAN_UP:
  327. case FIELD_IPV4_TOS:
  328. case FIELD_IPV4_PROT:
  329. byte = (uint8_t *)(rule->CR_VALUE);
  330. *byte = (uint8_t)(basespec (* argv++, 0, sizeof (* byte)));
  331. break;
  332. case FIELD_VLAN_ID:
  333. case FIELD_TCP_SP:
  334. case FIELD_TCP_DP:
  335. case FIELD_UDP_SP:
  336. case FIELD_UDP_DP:
  337. case FIELD_IP_SP:
  338. case FIELD_IP_DP:
  339. word = (uint16_t *)(rule->CR_VALUE);
  340. *word = (uint16_t)(basespec (* argv++, 0, sizeof (* word)));
  341. *word = htons (*word);
  342. break;
  343. case FIELD_ETH_TYPE:
  344. word = (uint16_t *)(rule->CR_VALUE);
  345. *word = (uint16_t)(basespec (* argv++, 0, sizeof (* word)));
  346. *word = htons (*word);
  347. break;
  348. case FIELD_HPAV_MME:
  349. bytespec (* argv++, rule->CR_VALUE, sizeof (uint8_t) + sizeof (uint16_t));
  350. byte = (uint8_t *)(rule->CR_VALUE);
  351. HTOBE16 ((uint16_t)(* ++byte));
  352. break;
  353. case FIELD_IPV6_TC:
  354. case FIELD_IPV6_FL:
  355. case FIELD_TCP_ACK:
  356. default:
  357. error (1, ENOTSUP, "Field '%s' (0x%02X)", argv [-2], rule->CR_PID);
  358. break;
  359. }
  360. connection.rule.NUM_CLASSIFIERS++;
  361. if (connection.rule.NUM_CLASSIFIERS > RULE_MAX_CLASSIFIERS)
  362. {
  363. error (1, ENOTSUP, "More than %d classifiers in rule", RULE_MAX_CLASSIFIERS);
  364. }
  365. rule++;
  366. argc--;
  367. }
  368. connection.cspec.CSPEC_VERSION = 0x0001;
  369. openchannel (&channel);
  370. if (!(plc.message = malloc (sizeof (* plc.message))))
  371. {
  372. error (1, errno, PLC_NOMEMORY);
  373. }
  374. if (!argc)
  375. {
  376. add_conn (&plc, &connection);
  377. }
  378. while ((argc) && (* argv))
  379. {
  380. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  381. {
  382. error (1, errno, PLC_BAD_MAC, * argv);
  383. }
  384. add_conn (&plc, &connection);
  385. argc--;
  386. argv++;
  387. }
  388. free (plc.message);
  389. closechannel (&channel);
  390. exit (0);
  391. }