hpavd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. * hpavd.c - Qualcomm Atheros Homeplug AV Packet Daemon;
  44. *
  45. * prints incoming Qualcomm Atheros HomePlug AV vendor-specific
  46. * management messages on stdout in hex dump format for analysis;
  47. *
  48. * the program can be run in foreground or background, as a daemon;
  49. *
  50. * this program receives raw ethernet frames and so requires root
  51. * privileges; if you install it using "chmod 555" and "chown
  52. * root:root" then you must login as root to run it; otherwise, you
  53. * can install it using "chmod 4555" and "chown root:root" so that
  54. * anyone can run it; the program will refuse to run until you get
  55. * thing right;
  56. *
  57. *
  58. * Contributor(s):
  59. * Charles Maier
  60. *
  61. *--------------------------------------------------------------------*/
  62. /*====================================================================*
  63. * system header files;
  64. *--------------------------------------------------------------------*/
  65. #include <fcntl.h>
  66. #include <stdio.h>
  67. #include <ctype.h>
  68. #include <unistd.h>
  69. #include <stdint.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <sys/signal.h>
  73. #include <sys/ioctl.h>
  74. #include <errno.h>
  75. #ifdef __linux__
  76. #include <net/if.h>
  77. #include <net/if_arp.h>
  78. #include <netpacket/packet.h>
  79. #include <signal.h>
  80. #endif
  81. /*====================================================================*
  82. * custom header files;
  83. *--------------------------------------------------------------------*/
  84. #include "../tools/getoptv.h"
  85. #include "../tools/putoptv.h"
  86. #include "../tools/memory.h"
  87. #include "../tools/number.h"
  88. #include "../tools/types.h"
  89. #include "../tools/flags.h"
  90. #include "../tools/error.h"
  91. #include "../plc/plc.h"
  92. #include "../mme/mme.h"
  93. /*====================================================================*
  94. * custom source files;
  95. *--------------------------------------------------------------------*/
  96. #ifndef MAKEFILE
  97. #include "../tools/getoptv.c"
  98. #include "../tools/putoptv.c"
  99. #include "../tools/version.c"
  100. #include "../tools/hexdump.c"
  101. #include "../tools/hexstring.c"
  102. #include "../tools/hexdecode.c"
  103. #include "../tools/error.c"
  104. #include "../tools/emalloc.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../mme/MMEPeek.c"
  108. #include "../mme/MMEName.c"
  109. #include "../mme/MMEMode.c"
  110. #endif
  111. /*====================================================================*
  112. * program constants;
  113. *--------------------------------------------------------------------*/
  114. #define HPAVD_DAEMON (1 << 0)
  115. #define HPAVD_VERBOSE (1 << 1)
  116. #define HPAVD_SILENCE (1 << 2)
  117. #define HPAVD_ERROR (1 << 3)
  118. #define PLCDEVICE "PLC"
  119. #define ETHDEVICE "eth1"
  120. #define ETHNUMBER 2
  121. /*====================================================================*
  122. * program variables;
  123. *--------------------------------------------------------------------*/
  124. static bool done = false;
  125. /*====================================================================*
  126. *
  127. * void terminate (signo_t signal);
  128. *
  129. * terminate the program; we want to ensure an orgaized program
  130. * exit so that the original Ethernet adapter state is restored;
  131. *
  132. *
  133. *--------------------------------------------------------------------*/
  134. static void terminate (signo_t signal)
  135. {
  136. done = true;
  137. return;
  138. }
  139. /*====================================================================*
  140. *
  141. * int main (int argc, char * argv[]);
  142. *
  143. *
  144. *--------------------------------------------------------------------*/
  145. int main (int argc, char const * argv [])
  146. {
  147. struct sigaction sa;
  148. struct ifreq ifreq;
  149. struct sockaddr_ll sockaddr =
  150. {
  151. PF_PACKET,
  152. htons (ETH_P_HPAV),
  153. 0x0000,
  154. ARPHRD_ETHER,
  155. PACKET_OTHERHOST,
  156. ETHER_ADDR_LEN,
  157. {
  158. 0x00,
  159. 0x00,
  160. 0x00,
  161. 0x00,
  162. 0x00,
  163. 0x00,
  164. 0x00,
  165. 0x00
  166. }
  167. };
  168. static char const * optv [] =
  169. {
  170. "di:qv",
  171. PUTOPTV_S_DIVINE,
  172. "Qualcomm Atheros HomePlug AV Packet Daemon",
  173. "d\trun in background as daemon",
  174. "i s\thost interface is (s) [" ETHDEVICE "]",
  175. "q\tsuppress normal output",
  176. "v\tverbose messages on stdout",
  177. (char const *) (0)
  178. };
  179. uint8_t packet [ETHER_MAX_LEN];
  180. flag_t state = (flag_t)(0);
  181. flag_t flags = (flag_t)(0);
  182. sock_t fd = -1;
  183. signed c;
  184. memset (&ifreq, 0, sizeof (ifreq));
  185. memcpy (ifreq.ifr_name, ETHDEVICE, sizeof (ETHDEVICE));
  186. if (getenv (PLCDEVICE))
  187. {
  188. memcpy (ifreq.ifr_name, getenv (PLCDEVICE), sizeof (ifreq.ifr_name));
  189. }
  190. optind = 1;
  191. while ((c = getoptv (argc, argv, optv)) != -1)
  192. {
  193. switch ((char) (c))
  194. {
  195. case 'd':
  196. _setbits (flags, HPAVD_DAEMON);
  197. break;
  198. case 'i':
  199. memcpy (ifreq.ifr_name, optarg, sizeof (ifreq.ifr_name));
  200. break;
  201. case 'q':
  202. _setbits (flags, HPAVD_SILENCE);
  203. break;
  204. case 'v':
  205. _setbits (flags, HPAVD_VERBOSE);
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. argc -= optind;
  212. argv += optind;
  213. if (geteuid ())
  214. {
  215. error (1, EPERM, ERROR_NOTROOT);
  216. }
  217. if (_anyset (flags, HPAVD_DAEMON))
  218. {
  219. pid_t pid = fork ();
  220. if (pid < 0)
  221. {
  222. error (1, errno, "razzlefrats!");
  223. }
  224. if (pid > 0)
  225. {
  226. exit (0);
  227. }
  228. }
  229. memset (&sa, 0, sizeof (struct sigaction));
  230. sa.sa_handler = terminate;
  231. sigaction (SIGTERM, &sa, (struct sigaction *)(0));
  232. sigaction (SIGQUIT, &sa, (struct sigaction *)(0));
  233. sigaction (SIGTSTP, &sa, (struct sigaction *)(0));
  234. sigaction (SIGINT, &sa, (struct sigaction *)(0));
  235. sigaction (SIGHUP, &sa, (struct sigaction *)(0));
  236. if ((fd = socket (sockaddr.sll_family, SOCK_RAW, sockaddr.sll_protocol)) == -1)
  237. {
  238. error (1, errno, "Can't create socket for %s", ifreq.ifr_name);
  239. }
  240. if (ioctl (fd, SIOCGIFFLAGS, &ifreq) < 0)
  241. {
  242. error (1, errno, "Can't read %s device state", ifreq.ifr_name);
  243. }
  244. state = ifreq.ifr_flags;
  245. _setbits (ifreq.ifr_flags, (IFF_UP | IFF_BROADCAST));
  246. _clrbits (ifreq.ifr_flags, (IFF_MULTICAST | IFF_ALLMULTI | IFF_PROMISC));
  247. if (ioctl (fd, SIOCSIFFLAGS, &ifreq) < 0)
  248. {
  249. error (1, errno, "Can't change %s device state", ifreq.ifr_name);
  250. }
  251. if (ioctl (fd, SIOCGIFINDEX, &ifreq) == -1)
  252. {
  253. error (1, errno, "Can't get %s interface index", ifreq.ifr_name);
  254. }
  255. sockaddr.sll_ifindex = ifreq.ifr_ifindex;
  256. if (ioctl (fd, SIOCGIFHWADDR, &ifreq) == -1)
  257. {
  258. error (1, errno, "Can't get %s hardware address", ifreq.ifr_name);
  259. }
  260. memcpy (sockaddr.sll_addr, ifreq.ifr_ifru.ifru_hwaddr.sa_data, sizeof (sockaddr.sll_addr));
  261. if (bind (fd, (struct sockaddr *) (&sockaddr), sizeof (struct sockaddr_ll)) == -1)
  262. {
  263. error (1, errno, "Can't bind socket to %s", ifreq.ifr_name);
  264. }
  265. while (!done)
  266. {
  267. signed length = recvfrom (fd, packet, sizeof (packet), 0, (struct sockaddr *) (0), (socklen_t *)(0));
  268. if (length > 0)
  269. {
  270. if (_allclr (flags, HPAVD_SILENCE))
  271. {
  272. MMEPeek (&packet, length, stdout);
  273. }
  274. if (_anyset (flags, HPAVD_VERBOSE))
  275. {
  276. hexdump (&packet, 0, length, stdout);
  277. }
  278. }
  279. }
  280. ifreq.ifr_flags = state;
  281. if (ioctl (fd, SIOCSIFFLAGS, &ifreq) < 0)
  282. {
  283. error (1, errno, "Can't restore %s device state", ifreq.ifr_name);
  284. }
  285. close (fd);
  286. return (0);
  287. }