respond.1.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * respond.c - Qualcomm Atheros Atheros Network Responder;
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. #define _GETOPT_H
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #include <errno.h>
  24. /*====================================================================*
  25. * custom header files;
  26. *--------------------------------------------------------------------*/
  27. #include "../tools/getoptv.h"
  28. #include "../tools/putoptv.h"
  29. #include "../tools/memory.h"
  30. #include "../tools/number.h"
  31. #include "../tools/types.h"
  32. #include "../tools/flags.h"
  33. #include "../tools/files.h"
  34. #include "../tools/error.h"
  35. #include "../tools/config.h"
  36. #include "../ether/channel.h"
  37. #include "../nda/lldp.h"
  38. #include "../plc/plc.h"
  39. /*====================================================================*
  40. * custom source files;
  41. *--------------------------------------------------------------------*/
  42. #ifndef MAKEFILE
  43. #include "../tools/getoptv.c"
  44. #include "../tools/putoptv.c"
  45. #include "../tools/version.c"
  46. #include "../tools/hexcopy.c"
  47. #include "../tools/hexdump.c"
  48. #include "../tools/hexdecode.c"
  49. #include "../tools/hexstring.c"
  50. #include "../tools/decdecode.c"
  51. #include "../tools/decstring.c"
  52. #include "../tools/uintspec.c"
  53. #include "../tools/todigit.c"
  54. #include "../tools/error.c"
  55. #include "../tools/strfbits.c"
  56. #include "../tools/config.c"
  57. #endif
  58. #ifndef MAKEFILE
  59. #include "../plc/Devices.c"
  60. #endif
  61. #ifndef MAKEFILE
  62. #include "../mme/EthernetHeader.c"
  63. #include "../mme/QualcommHeader.c"
  64. #include "../mme/UnwantedMessage.c"
  65. #include "../mme/readmessage.c"
  66. #include "../mme/sendmessage.c"
  67. #endif
  68. #ifndef MAKEFILE
  69. #include "../ether/channel.c"
  70. #include "../ether/openchannel.c"
  71. #include "../ether/closechannel.c"
  72. #include "../ether/sendpacket.c"
  73. #include "../ether/readpacket.c"
  74. #endif
  75. #ifndef MAKEFILE
  76. #include "../lldp/TLVPack.c"
  77. #include "../lldp/TLVPackOS.c"
  78. #include "../lldp/TLVPick.c"
  79. #include "../lldp/TLVPeek.c"
  80. #endif
  81. /*====================================================================*
  82. * program constants;
  83. *--------------------------------------------------------------------*/
  84. #define RESPOND_VERBOSE (1 << 0)
  85. #define RESPOND_SILENCE (1 << 1)
  86. #define PLCDEVICE "PLC"
  87. #define PROFILE "respond.ini"
  88. #define SECTION "default"
  89. #define STANDARD "Standard"
  90. #define SPECIFIC "Specific"
  91. /*====================================================================*
  92. *
  93. * byte * populate1 (byte * memory, size_t extent, char const * profile, char const * section, char buffer [], size_t length)
  94. *
  95. * populate memory region with IEEE standard TLVs of interest to
  96. * us; not all IEEE 802 TLVs are implemented here; the data source
  97. * is static data declarations in one case and a configuration
  98. * file in the other case;
  99. *
  100. *--------------------------------------------------------------------*/
  101. static byte * populate1 (byte * memory, size_t extent, char const * profile, char const * section, char buffer [], size_t length)
  102. {
  103. char SYSTEM_NAME [HOST_NAME_MAX] =
  104. {
  105. "System Name"
  106. };
  107. #ifndef PROFILE
  108. byte * length = memory + extent;
  109. static uint16_t TIME_TO_LIVE = 50000;
  110. static byte CHASSIS_ID [] =
  111. {
  112. 0x04,
  113. 0xAA,
  114. 0xBB,
  115. 0xCC,
  116. 0xDD,
  117. 0xEE,
  118. 0xFF
  119. };
  120. static byte PORT_ID [] =
  121. {
  122. 0x03,
  123. 0x11,
  124. 0x22,
  125. 0x33,
  126. 0x44,
  127. 0x55,
  128. 0x66
  129. };
  130. static byte SYSTEM_CAPABILITIES [] =
  131. {
  132. 0x00,
  133. 0xFF,
  134. 0xFF,
  135. 0xAB,
  136. 0xCD
  137. };
  138. static char PORT_DESCRIPTION [] = "Port Description";
  139. static char SYSTEM_DESCRIPTION [] = "System Description";
  140. gethostname (SYSTEM_NAME, sizeof (SYSTEM_NAME));
  141. memory = TLVPack (memory, length - memory, TLV_IEEE_CHASSIS_ID, sizeof (CHASSIS_ID), CHASSIS_ID);
  142. memory = TLVPack (memory, length - memory, TLV_IEEE_PORT_ID, sizeof (PORT_ID), PORT_ID);
  143. memory = TLVPack (memory, length - memory, TLV_IEEE_TIME_TO_LIVE, sizeof (TIME_TO_LIVE), & TIME_TO_LIVE);
  144. memory = TLVPack (memory, length - memory, TLV_IEEE_PORT_DESCRIPTION, sizeof (PORT_DESCRIPTION), PORT_DESCRIPTION);
  145. memory = TLVPack (memory, length - memory, TLV_IEEE_SYSTEM_NAME, strlen (SYSTEM_NAME) + 1, SYSTEM_NAME);
  146. memory = TLVPack (memory, length - memory, TLV_IEEE_SYSTEM_DESCRIPTION, sizeof (SYSTEM_DESCRIPTION), SYSTEM_DESCRIPTION);
  147. memory = TLVPack (memory, length - memory, TLV_IEEE_SYSTEM_CAPABILITIES, sizeof (SYSTEM_CAPABILITIES), SYSTEM_CAPABILITIES);
  148. #else
  149. uint16_t ttl;
  150. char const * string;
  151. byte * offset = memory;
  152. memory += extent;
  153. gethostname (SYSTEM_NAME, sizeof (SYSTEM_NAME));
  154. string = configstring (profile, section, "Chassis ID", "00:00:00:00:00:00:00");
  155. offset = TLVPack (offset, memory - offset, TLV_IEEE_CHASSIS_ID, hexcopy (buffer, length, string), buffer);
  156. string = configstring (profile, section, "Port ID", "00:00:00:00:00:00:00");
  157. offset = TLVPack (offset, memory - offset, TLV_IEEE_PORT_ID, hexcopy (buffer, length, string), buffer);
  158. string = configstring (profile, section, "Time To Live", "0");
  159. ttl = htons (atoi (string));
  160. offset = TLVPack (offset, memory - offset, TLV_IEEE_TIME_TO_LIVE, sizeof (ttl), & ttl);
  161. string = configstring (profile, section, "Port Description", "Unspecified port");
  162. offset = TLVPack (offset, memory - offset, TLV_IEEE_PORT_DESCRIPTION, 1 + strlen (string), string);
  163. string = configstring (profile, section, "System Name", SYSTEM_NAME);
  164. offset = TLVPack (offset, memory - offset, TLV_IEEE_SYSTEM_NAME, 1 + strlen (string), string);
  165. string = configstring (profile, section, "System Description", "Do nothing quickly and accurately");
  166. offset = TLVPack (offset, memory - offset, TLV_IEEE_SYSTEM_DESCRIPTION, 1 + strlen (string), string);
  167. string = configstring (profile, section, "System Capabilities", "00:00:00:00:00");
  168. offset = TLVPack (offset, memory - offset, TLV_IEEE_SYSTEM_CAPABILITIES, hexcopy (buffer, length, string), buffer);
  169. string = configstring (profile, section, "Management URL", "http://www.management.com");
  170. offset = TLVPack (offset, memory - offset, TLV_IEEE_MANAGEMENT_ADDRESS, 1 + strlen (string), string);
  171. #endif
  172. return (offset);
  173. }
  174. /*====================================================================*
  175. *
  176. * byte * populate2 (byte * memory, size_t extent, char const * profile, char const * section, char buffer [], size_t length)
  177. *
  178. * populate memory region with IEEE organizationally specific TLVs
  179. * of interest to us; Qualcomm Atheros has defined the ones used
  180. * here; the data source here is static data declarations in one
  181. * case and a configuration file in the other;
  182. *
  183. *--------------------------------------------------------------------*/
  184. static byte * populate2 (byte * memory, size_t extent, char const * profile, char const * section, char buffer [], size_t length)
  185. {
  186. #ifndef PROFILE
  187. extern struct channel channel;
  188. byte * length = memory + extent;
  189. static char MANUFACTURER [] = "Qualcomm Atheros";
  190. static char HARDWARE_MODEL [] = "QCA7000";
  191. static char SERIAL_NUMBER [] = "00:B0:52:00:00:55";
  192. static byte PLC_MAC [] =
  193. {
  194. 0x00,
  195. 0xB0,
  196. 0x52,
  197. 0x00,
  198. 0xBA,
  199. 0xBE
  200. };
  201. static byte WIFI_MAC [] =
  202. {
  203. 0xAA,
  204. 0xBB,
  205. 0xCC,
  206. 0xDD,
  207. 0xEE,
  208. 0xFF
  209. };
  210. static byte WIFI_IP [] =
  211. {
  212. 192,
  213. 168,
  214. 1,
  215. 88
  216. };
  217. memory = TLVPackOS (memory, length - memory, TLV_OS_MANUFACTURER, sizeof (MANUFACTURER), MANUFACTURER);
  218. memory = TLVPackOS (memory, length - memory, TLV_OS_HARDWARE_MODEL, sizeof (HARDWARE_MODEL), HARDWARE_MODEL);
  219. memory = TLVPackOS (memory, length - memory, TLV_OS_SERIAL_NUMBER, sizeof (SERIAL_NUMBER), SERIAL_NUMBER);
  220. memory = TLVPackOS (memory, length - memory, TLV_OS_HOST_MAC, sizeof (channel.host), channel.host);
  221. memory = TLVPackOS (memory, length - memory, TLV_OS_WIFI_MAC, sizeof (WIFI_MAC), WIFI_MAC);
  222. memory = TLVPackOS (memory, length - memory, TLV_OS_WIFI_IP, sizeof (WIFI_IP), WIFI_IP);
  223. memory = TLVPackOS (memory, length - memory, TLV_OS_PLC_MAC, sizeof (PLC_MAC), PLC_MAC);
  224. #else
  225. char const * string;
  226. byte * offset = memory;
  227. memory += extent;
  228. string = configstring (profile, section, "Manufacturer", "Unknown");
  229. offset = TLVPackOS (offset, memory - offset, TLV_OS_MANUFACTURER, 1 + strlen (string), string);
  230. string = configstring (profile, section, "Hardware Model", "Unknown");
  231. offset = TLVPackOS (offset, memory - offset, TLV_OS_HARDWARE_MODEL, 1 + strlen (string), string);
  232. string = configstring (profile, section, "Serial Number", "0000-0000-0000-0000");
  233. offset = TLVPackOS (offset, memory - offset, TLV_OS_SERIAL_NUMBER, 1 + strlen (string), string);
  234. string = configstring (profile, section, "Host MAC", "00:00:00:00:00:00");
  235. offset = TLVPackOS (offset, memory - offset, TLV_OS_HOST_MAC, hexcopy (buffer, length, string), buffer);
  236. string = configstring (profile, section, "WiFi MAC", "00:00:00:00:00:00");
  237. offset = TLVPackOS (offset, memory - offset, TLV_OS_WIFI_MAC, hexcopy (buffer, length, string), buffer);
  238. string = configstring (profile, section, "WiFi IP", "00:00:00:00");
  239. offset = TLVPackOS (offset, memory - offset, TLV_OS_WIFI_IP, hexcopy (buffer, length, string), buffer);
  240. string = configstring (profile, section, "PLC MAC", "00:00:00:00:00:00");
  241. offset = TLVPackOS (offset, memory - offset, TLV_OS_PLC_MAC, hexcopy (buffer, length, string), buffer);
  242. #endif
  243. return (offset);
  244. }
  245. /*====================================================================*
  246. *
  247. * byte * populate (byte * memory, size_t extent, char const * profile, char const * section);
  248. *
  249. * populate a memory region with IEEE 802 standard TLVs then IEEE
  250. * 802 organizationally specific TLVs; provide the buffer that is
  251. * used to format and encode information before copying it to the
  252. * memory region;
  253. *
  254. * customers may implement their own populate functions, possibly
  255. * reading from a MIB or other type of database;
  256. *
  257. *--------------------------------------------------------------------*/
  258. static byte * populate (byte * memory, size_t extent, char const * profile, char const * section)
  259. {
  260. char buffer [1024];
  261. byte * offset = memory;
  262. memory += extent;
  263. offset = populate1 (offset, memory - offset, profile, STANDARD, buffer, sizeof (buffer));
  264. offset = populate2 (offset, memory - offset, profile, SPECIFIC, buffer, sizeof (buffer));
  265. return (offset);
  266. }
  267. /*====================================================================*
  268. *
  269. * void announce (struct channel * channel, struct message * message, char const * profile, char const * section, unsigned timer);
  270. *
  271. * announce network presence and collect acknowledgements; do not
  272. * very long for an acknowledgement; timeout and start listening;
  273. *
  274. *--------------------------------------------------------------------*/
  275. static void announce (struct channel * channel, struct message * message, char const * profile, char const * section, unsigned timer)
  276. {
  277. #ifndef __GNUC__
  278. #pragma pack (push,1)
  279. #endif
  280. struct __packed ms_discover_indicate
  281. {
  282. struct ethernet_hdr ethernet;
  283. struct qualcomm_hdr qualcomm;
  284. uint8_t MDATA [1494];
  285. }
  286. * indicate = (struct ms_discover_indicate *) (message);
  287. struct __packed ms_discover_response
  288. {
  289. struct ethernet_hdr ethernet;
  290. struct qualcomm_hdr qualcomm;
  291. uint8_t MDATA [1494];
  292. }
  293. * response = (struct ms_discover_response *) (message);
  294. #ifndef __GNUC__
  295. #pragma pack (pop)
  296. #endif
  297. ssize_t length;
  298. memset (message, 0, sizeof (* message));
  299. EthernetHeader (& indicate->ethernet, channel->peer, channel->host, channel->type);
  300. QualcommHeader (& indicate->qualcomm, 0, (MS_DISCOVER | MMTYPE_IND));
  301. length = populate (indicate->MDATA, sizeof (indicate->MDATA), profile, section) - (byte *) (message);
  302. if (length < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  303. {
  304. length = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  305. }
  306. if (sendmessage (channel, message, length) > 0)
  307. {
  308. #if 0
  309. TLVPeek (indicate->MDATA, sizeof (indicate->MDATA));
  310. #endif
  311. while (readmessage (channel, message, 0, (MS_DISCOVER | MMTYPE_RSP)) > 0)
  312. {
  313. TLVPeek (response->MDATA, sizeof (response->MDATA));
  314. }
  315. }
  316. return;
  317. }
  318. /*====================================================================*
  319. *
  320. * void monitor (struct channel * channel, struct message * message, char const * profile, char const * section, unsigned timer);
  321. *
  322. * monitor for discovery requests and respond; lengthen the channel
  323. * timeout during this operations;
  324. *
  325. *--------------------------------------------------------------------*/
  326. static void monitor (struct channel * channel, struct message * message, char const * profile, char const * section, unsigned timer)
  327. {
  328. #ifndef __GNUC__
  329. #pragma pack (push,1)
  330. #endif
  331. struct __packed ms_discover_request
  332. {
  333. struct ethernet_hdr ethernet;
  334. struct qualcomm_hdr qualcomm;
  335. uint8_t MACTION;
  336. }
  337. * request = (struct ms_discover_request *) (message);
  338. struct __packed ms_discover_confirm
  339. {
  340. struct ethernet_hdr ethernet;
  341. struct qualcomm_hdr qualcomm;
  342. uint8_t MDATA [1494];
  343. }
  344. * confirm = (struct ms_discover_confirm *) (message);
  345. #ifndef __GNUC__
  346. #pragma pack (pop)
  347. #endif
  348. ssize_t length;
  349. unsigned timeout = channel->timeout;
  350. channel->timeout = timer;
  351. while (readmessage (channel, message, 0, (MS_DISCOVER | MMTYPE_REQ)) > 0)
  352. {
  353. if (! request->MACTION)
  354. {
  355. EthernetHeader (& confirm->ethernet, request->ethernet.OSA, channel->host, channel->type);
  356. QualcommHeader (& confirm->qualcomm, request->qualcomm.MMV, (MS_DISCOVER | MMTYPE_CNF));
  357. memset (confirm->MDATA, 0, sizeof (confirm->MDATA));
  358. length = populate (confirm->MDATA, sizeof (confirm->MDATA), profile, section) - (byte *) (message);
  359. if (length < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  360. {
  361. length = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  362. }
  363. #if 0
  364. TLVPeek (confirm->MDATA, sizeof (confirm->MDATA));
  365. #endif
  366. if (sendmessage (channel, message, length) <= 0)
  367. {
  368. error (0, errno, CHANNEL_CANTSEND);
  369. }
  370. }
  371. }
  372. channel->timeout = timeout;
  373. return;
  374. }
  375. /*====================================================================*
  376. *
  377. * int main (int argc, char * argv[]);
  378. *
  379. *
  380. *--------------------------------------------------------------------*/
  381. int main (int argc, char const * argv [])
  382. {
  383. extern struct channel channel;
  384. extern byte const broadcast [ETHER_ADDR_LEN];
  385. static char const * optv [] =
  386. {
  387. #ifdef PROFILE
  388. "i:p:qt:vw:",
  389. #else
  390. "i:qt:vw:",
  391. #endif
  392. PUTOPTV_S_DIVINE,
  393. "Qualcomm Atheros Network Responder",
  394. #if defined (WINPCAP) || defined (LIBPCAP)
  395. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  396. #else
  397. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  398. #endif
  399. #ifdef PROFILE
  400. "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
  401. #endif
  402. "q\tsuppress normal output",
  403. #ifdef PROFILE
  404. "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
  405. #endif
  406. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  407. "v\tverbose messages on stdout",
  408. "w n\twakeup every (n) milliseconds [" LITERAL (PLC_LONGTIME) "]",
  409. (char const *) (0)
  410. };
  411. struct message message;
  412. char const * profile = PROFILE;
  413. char const * section = SECTION;
  414. unsigned timer = PLC_LONGTIME;
  415. signed c;
  416. if (getenv (PLCDEVICE))
  417. {
  418. #if defined (WINPCAP) || defined (LIBPCAP)
  419. channel.ifindex = atoi (getenv (PLCDEVICE));
  420. #else
  421. channel.ifname = strdup (getenv (PLCDEVICE));
  422. #endif
  423. }
  424. optind = 1;
  425. memcpy (channel.peer, broadcast, sizeof (channel.peer));
  426. while (~ (c = getoptv (argc, argv, optv)))
  427. {
  428. switch (c)
  429. {
  430. case 'i':
  431. #if defined (WINPCAP) || defined (LIBPCAP)
  432. channel.ifindex = atoi (optarg);
  433. #else
  434. channel.ifname = optarg;
  435. #endif
  436. break;
  437. #ifdef PROFILE
  438. case 'p':
  439. profile = optarg;
  440. break;
  441. #endif
  442. case 'q':
  443. _clrbits (channel.flags, CHANNEL_SILENCE);
  444. break;
  445. #ifdef PROFILE
  446. case 's':
  447. section = optarg;
  448. break;
  449. #endif
  450. case 't':
  451. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  452. break;
  453. case 'v':
  454. _setbits (channel.flags, CHANNEL_VERBOSE);
  455. break;
  456. case 'w':
  457. timer = (unsigned) (uintspec (optarg, 1, UINT_MAX));
  458. break;
  459. default:
  460. break;
  461. }
  462. }
  463. argc -= optind;
  464. argv += optind;
  465. if (argc)
  466. {
  467. error (1, 0, ERROR_TOOMANY);
  468. }
  469. #ifdef PROFILE
  470. if (access (profile, R_OK))
  471. {
  472. error (1, errno, "Can't access '%s'", profile);
  473. }
  474. #endif
  475. openchannel (& channel);
  476. while (true)
  477. {
  478. announce (& channel, & message, profile, section, timer);
  479. monitor (& channel, & message, profile, section, timer);
  480. }
  481. closechannel (& channel);
  482. return (0);
  483. }