respond.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <string.h>
  23. #include <limits.h>
  24. #include <errno.h>
  25. #include <sys/time.h>
  26. /*====================================================================*
  27. * custom header files;
  28. *--------------------------------------------------------------------*/
  29. #include "../tools/getoptv.h"
  30. #include "../tools/putoptv.h"
  31. #include "../tools/memory.h"
  32. #include "../tools/number.h"
  33. #include "../tools/symbol.h"
  34. #include "../tools/types.h"
  35. #include "../tools/flags.h"
  36. #include "../tools/files.h"
  37. #include "../tools/error.h"
  38. #include "../tools/timer.h"
  39. #include "../tools/config.h"
  40. #include "../ether/channel.h"
  41. #include "../lldp/lldp.h"
  42. #include "../mme/mme.h"
  43. /*====================================================================*
  44. * custom source files;
  45. *--------------------------------------------------------------------*/
  46. #ifndef MAKEFILE
  47. #include "../tools/getoptv.c"
  48. #include "../tools/putoptv.c"
  49. #include "../tools/version.c"
  50. #include "../tools/hexcopy.c"
  51. #include "../tools/hexdump.c"
  52. #include "../tools/hexdecode.c"
  53. #include "../tools/hexencode.c"
  54. #include "../tools/hexstring.c"
  55. #include "../tools/decdecode.c"
  56. #include "../tools/decstring.c"
  57. #include "../tools/uintspec.c"
  58. #include "../tools/todigit.c"
  59. #include "../tools/error.c"
  60. #include "../tools/strfbits.c"
  61. #include "../tools/synonym.c"
  62. #include "../tools/config.c"
  63. #endif
  64. #ifndef MAKEFILE
  65. #include "../ether/channel.c"
  66. #include "../ether/openchannel.c"
  67. #include "../ether/closechannel.c"
  68. #include "../ether/readpacket.c"
  69. #include "../ether/sendpacket.c"
  70. #endif
  71. #ifndef MAKEFILE
  72. #include "../mme/EthernetHeader.c"
  73. #endif
  74. #ifndef MAKEFILE
  75. #include "../lldp/TLVPack.c"
  76. #include "../lldp/TLVPackOS.c"
  77. #include "../lldp/TLVPick.c"
  78. #include "../lldp/TLVPeek.c"
  79. #include "../lldp/lldp.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, byte 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, byte buffer [], size_t length)
  102. {
  103. char SYSTEM_NAME [1024] =
  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, byte 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, byte 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. byte 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 ethernet_frame * frame, char const * profile, char const * section);
  270. *
  271. * announce network presence and collect any acknowledgements;
  272. *
  273. *--------------------------------------------------------------------*/
  274. static void announce (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section)
  275. {
  276. ssize_t length;
  277. memset (frame, 0, sizeof (* frame));
  278. EthernetHeader (frame, channel->peer, channel->host, ETH_P_LLDP);
  279. length = populate (frame->frame_data, sizeof (frame->frame_data), profile, section) - (byte *) (frame);
  280. if (length < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  281. {
  282. length = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  283. }
  284. if (sendpacket (channel, frame, length) > 0)
  285. {
  286. while (readpacket (channel, frame, sizeof (* frame)) > 0)
  287. {
  288. if (! memcmp (frame->frame_dhost, channel->host, sizeof (frame->frame_dhost)))
  289. {
  290. TLVPeek (frame->frame_data, sizeof (frame->frame_data));
  291. }
  292. }
  293. }
  294. return;
  295. }
  296. /*====================================================================*
  297. *
  298. * void monitor (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section);
  299. *
  300. * listen for discovery requests and respond;
  301. *
  302. *--------------------------------------------------------------------*/
  303. static void monitor (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section)
  304. {
  305. struct timeval ts;
  306. struct timeval tc;
  307. ssize_t length;
  308. if (gettimeofday (& ts, NULL) == - 1)
  309. {
  310. error (1, errno, CANT_START_TIMER);
  311. }
  312. while ((length = readpacket (channel, frame, sizeof (* frame))) >= 0)
  313. {
  314. if (! memcmp (frame->frame_dhost, channel->peer, sizeof (frame->frame_dhost)))
  315. {
  316. EthernetHeader (frame, frame->frame_shost, channel->host, ETH_P_LLDP);
  317. memset (frame->frame_data, 0, sizeof (frame->frame_data));
  318. length = populate (frame->frame_data, sizeof (frame->frame_data), profile, section) - (byte *) (frame);
  319. if (length < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  320. {
  321. length = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  322. }
  323. if (sendpacket (channel, frame, length) <= 0)
  324. {
  325. error (0, errno, CHANNEL_CANTSEND);
  326. }
  327. continue;
  328. }
  329. if (gettimeofday (& tc, NULL) == - 1)
  330. {
  331. error (1, errno, CANT_RESET_TIMER);
  332. }
  333. if (channel->timeout < 0)
  334. {
  335. continue;
  336. }
  337. if (channel->timeout > MILLISECONDS (ts, tc))
  338. {
  339. continue;
  340. }
  341. break;
  342. }
  343. return;
  344. }
  345. /*====================================================================*
  346. *
  347. * int main (int argc, char * argv[]);
  348. *
  349. *
  350. *--------------------------------------------------------------------*/
  351. int main (int argc, char const * argv [])
  352. {
  353. extern struct channel channel;
  354. extern struct _term_ const addresses [LLDP_ADDRESSES];
  355. extern byte const broadcast [ETHER_ADDR_LEN];
  356. static char const * optv [] =
  357. {
  358. "b:i:p:qt:vw:",
  359. PUTOPTV_S_DIVINE,
  360. "Qualcomm Atheros Network Responder",
  361. "b x\tbroadcast address is (x) [" LITERAL (BROADCAST) "]",
  362. #if defined (WINPCAP) || defined (LIBPCAP)
  363. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  364. #else
  365. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  366. #endif
  367. "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
  368. "q\tsuppress normal output",
  369. "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
  370. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  371. "v\tverbose frames on stdout",
  372. "w n\twakeup every (n) milliseconds [" LITERAL (LLDP_TIMER) "]",
  373. (char const *) (0)
  374. };
  375. struct ethernet_frame frame;
  376. char const * profile = PROFILE;
  377. char const * section = SECTION;
  378. signed c;
  379. memcpy (channel.peer, broadcast, sizeof (channel.peer));
  380. channel.type = ETH_P_LLDP;
  381. channel.timeout = LLDP_TIMER;
  382. if (getenv (PLCDEVICE))
  383. {
  384. #if defined (WINPCAP) || defined (LIBPCAP)
  385. channel.ifindex = atoi (getenv (PLCDEVICE));
  386. #else
  387. channel.ifname = strdup (getenv (PLCDEVICE));
  388. #endif
  389. }
  390. optind = 1;
  391. while (~ (c = getoptv (argc, argv, optv)))
  392. {
  393. switch (c)
  394. {
  395. case 'b':
  396. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (optarg, addresses, SIZEOF (addresses))))
  397. {
  398. error (1, errno, LLDP_BAD_MAC, * argv);
  399. }
  400. break;
  401. case 'i':
  402. #if defined (WINPCAP) || defined (LIBPCAP)
  403. channel.ifindex = atoi (optarg);
  404. #else
  405. channel.ifname = optarg;
  406. #endif
  407. break;
  408. case 'p':
  409. profile = optarg;
  410. break;
  411. case 'q':
  412. _clrbits (channel.flags, CHANNEL_SILENCE);
  413. break;
  414. case 's':
  415. section = optarg;
  416. break;
  417. case 't':
  418. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  419. break;
  420. case 'v':
  421. _setbits (channel.flags, CHANNEL_VERBOSE);
  422. break;
  423. case 'w':
  424. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. argc -= optind;
  431. argv += optind;
  432. if (argc)
  433. {
  434. error (1, 0, ERROR_TOOMANY);
  435. }
  436. if (access (profile, R_OK))
  437. {
  438. error (1, errno, FILE_CANTSTAT, profile);
  439. }
  440. openchannel (& channel);
  441. while (true)
  442. {
  443. announce (& channel, & frame, profile, section);
  444. monitor (& channel, & frame, profile, section);
  445. }
  446. closechannel (& channel);
  447. return (0);
  448. }