BArpProbe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * @file BArpProbe.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <sys/socket.h>
  33. #include <net/if.h>
  34. #include <net/if_arp.h>
  35. #include <sys/ioctl.h>
  36. #include <linux/filter.h>
  37. #include <misc/debug.h>
  38. #include <misc/byteorder.h>
  39. #include <misc/ethernet_proto.h>
  40. #include <misc/ipv4_proto.h>
  41. #include <misc/udp_proto.h>
  42. #include <base/BLog.h>
  43. #include "BArpProbe.h"
  44. #include <generated/blog_channel_BArpProbe.h>
  45. #define STATE_INITIAL 1
  46. #define STATE_NOEXIST 2
  47. #define STATE_EXIST 3
  48. #define STATE_EXIST_PANIC 4
  49. static int get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex)
  50. {
  51. struct ifreq ifr;
  52. int s = socket(AF_INET, SOCK_DGRAM, 0);
  53. if (!s) {
  54. BLog(BLOG_ERROR, "socket failed");
  55. goto fail0;
  56. }
  57. // get MAC
  58. memset(&ifr, 0, sizeof(ifr));
  59. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  60. if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
  61. BLog(BLOG_ERROR, "ioctl(SIOCGIFHWADDR) failed");
  62. goto fail1;
  63. }
  64. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  65. BLog(BLOG_ERROR, "hardware address not ethernet");
  66. goto fail1;
  67. }
  68. memcpy(out_mac, ifr.ifr_hwaddr.sa_data, 6);
  69. // get MTU
  70. memset(&ifr, 0, sizeof(ifr));
  71. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  72. if (ioctl(s, SIOCGIFMTU, &ifr)) {
  73. BLog(BLOG_ERROR, "ioctl(SIOCGIFMTU) failed");
  74. goto fail1;
  75. }
  76. *out_mtu = ifr.ifr_mtu;
  77. // get interface index
  78. memset(&ifr, 0, sizeof(ifr));
  79. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  80. if (ioctl(s, SIOCGIFINDEX, &ifr)) {
  81. BLog(BLOG_ERROR, "ioctl(SIOCGIFINDEX) failed");
  82. goto fail1;
  83. }
  84. *out_ifindex = ifr.ifr_ifindex;
  85. close(s);
  86. return 1;
  87. fail1:
  88. close(s);
  89. fail0:
  90. return 0;
  91. }
  92. static void dgram_handler (BArpProbe *o, int event)
  93. {
  94. DebugObject_Access(&o->d_obj);
  95. BLog(BLOG_ERROR, "packet socket error");
  96. // report error
  97. DEBUGERROR(&o->d_err, o->handler(o->user, BARPPROBE_EVENT_ERROR));
  98. return;
  99. }
  100. static void send_request (BArpProbe *o)
  101. {
  102. if (o->send_sending) {
  103. BLog(BLOG_ERROR, "cannot send packet while another packet is being sent!");
  104. return;
  105. }
  106. // build packet
  107. struct arp_packet *arp = &o->send_packet;
  108. arp->hardware_type = hton16(ARP_HARDWARE_TYPE_ETHERNET);
  109. arp->protocol_type = hton16(ETHERTYPE_IPV4);
  110. arp->hardware_size = hton8(6);
  111. arp->protocol_size = hton8(4);
  112. arp->opcode = hton16(ARP_OPCODE_REQUEST);
  113. memcpy(arp->sender_mac, o->if_mac, 6);
  114. arp->sender_ip = hton32(0);
  115. memset(arp->target_mac, 0, sizeof(arp->target_mac));
  116. arp->target_ip = o->addr;
  117. // send packet
  118. PacketPassInterface_Sender_Send(o->send_if, (uint8_t *)&o->send_packet, sizeof(o->send_packet));
  119. // set sending
  120. o->send_sending = 1;
  121. }
  122. static void send_if_handler_done (BArpProbe *o)
  123. {
  124. DebugObject_Access(&o->d_obj);
  125. ASSERT(o->send_sending)
  126. // set not sending
  127. o->send_sending = 0;
  128. }
  129. static void recv_if_handler_done (BArpProbe *o, int data_len)
  130. {
  131. DebugObject_Access(&o->d_obj);
  132. ASSERT(data_len >= 0)
  133. ASSERT(data_len <= sizeof(struct arp_packet))
  134. // receive next packet
  135. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)&o->recv_packet);
  136. if (data_len != sizeof(struct arp_packet)) {
  137. BLog(BLOG_WARNING, "receive: wrong size");
  138. return;
  139. }
  140. struct arp_packet *arp = &o->recv_packet;
  141. if (ntoh16(arp->hardware_type) != ARP_HARDWARE_TYPE_ETHERNET) {
  142. BLog(BLOG_WARNING, "receive: wrong hardware type");
  143. return;
  144. }
  145. if (ntoh16(arp->protocol_type) != ETHERTYPE_IPV4) {
  146. BLog(BLOG_WARNING, "receive: wrong protocol type");
  147. return;
  148. }
  149. if (ntoh8(arp->hardware_size) != 6) {
  150. BLog(BLOG_WARNING, "receive: wrong hardware size");
  151. return;
  152. }
  153. if (ntoh8(arp->protocol_size) != 4) {
  154. BLog(BLOG_WARNING, "receive: wrong protocol size");
  155. return;
  156. }
  157. if (ntoh16(arp->opcode) != ARP_OPCODE_REPLY) {
  158. return;
  159. }
  160. if (arp->sender_ip != o->addr) {
  161. return;
  162. }
  163. int old_state = o->state;
  164. // set minus one missed
  165. o->num_missed = -1;
  166. // set timer
  167. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_WAITSEND);
  168. // set state exist
  169. o->state = STATE_EXIST;
  170. // report exist if needed
  171. if (old_state == STATE_INITIAL || old_state == STATE_NOEXIST) {
  172. o->handler(o->user, BARPPROBE_EVENT_EXIST);
  173. return;
  174. }
  175. }
  176. static void timer_handler (BArpProbe *o)
  177. {
  178. DebugObject_Access(&o->d_obj);
  179. // send request
  180. send_request(o);
  181. switch (o->state) {
  182. case STATE_INITIAL: {
  183. ASSERT(o->num_missed >= 0)
  184. ASSERT(o->num_missed < BARPPROBE_INITIAL_NUM_ATTEMPTS)
  185. // increment missed
  186. o->num_missed++;
  187. // all attempts failed?
  188. if (o->num_missed == BARPPROBE_INITIAL_NUM_ATTEMPTS) {
  189. // set timer
  190. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  191. // set state noexist
  192. o->state = STATE_NOEXIST;
  193. // report noexist
  194. o->handler(o->user, BARPPROBE_EVENT_NOEXIST);
  195. return;
  196. }
  197. // set timer
  198. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_INITIAL_WAITRECV);
  199. } break;
  200. case STATE_NOEXIST: {
  201. // set timer
  202. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  203. } break;
  204. case STATE_EXIST: {
  205. ASSERT(o->num_missed >= -1)
  206. ASSERT(o->num_missed < BARPPROBE_EXIST_NUM_NOREPLY)
  207. // increment missed
  208. o->num_missed++;
  209. // all missed?
  210. if (o->num_missed == BARPPROBE_EXIST_NUM_NOREPLY) {
  211. // set timer
  212. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_PANIC_WAITRECV);
  213. // set zero missed
  214. o->num_missed = 0;
  215. // set state panic
  216. o->state = STATE_EXIST_PANIC;
  217. return;
  218. }
  219. // set timer
  220. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_WAITRECV);
  221. } break;
  222. case STATE_EXIST_PANIC: {
  223. ASSERT(o->num_missed >= 0)
  224. ASSERT(o->num_missed < BARPPROBE_EXIST_PANIC_NUM_NOREPLY)
  225. // increment missed
  226. o->num_missed++;
  227. // all missed?
  228. if (o->num_missed == BARPPROBE_EXIST_PANIC_NUM_NOREPLY) {
  229. // set timer
  230. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  231. // set state panic
  232. o->state = STATE_NOEXIST;
  233. // report noexist
  234. o->handler(o->user, BARPPROBE_EVENT_NOEXIST);
  235. return;
  236. }
  237. // set timer
  238. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_PANIC_WAITRECV);
  239. } break;
  240. }
  241. }
  242. int BArpProbe_Init (BArpProbe *o, const char *ifname, uint32_t addr, BReactor *reactor, void *user, BArpProbe_handler handler)
  243. {
  244. ASSERT(ifname)
  245. ASSERT(handler)
  246. // init arguments
  247. o->addr = addr;
  248. o->reactor = reactor;
  249. o->user = user;
  250. o->handler = handler;
  251. // get interface information
  252. int if_mtu;
  253. int if_index;
  254. if (!get_iface_info(ifname, o->if_mac, &if_mtu, &if_index)) {
  255. BLog(BLOG_ERROR, "failed to get interface information");
  256. goto fail0;
  257. }
  258. uint8_t *if_mac = o->if_mac;
  259. BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d",
  260. if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index);
  261. // check MTU
  262. if (if_mtu < sizeof(struct arp_packet)) {
  263. BLog(BLOG_ERROR, "MTU is too small for ARP !?!");
  264. goto fail0;
  265. }
  266. // init dgram
  267. if (!BDatagram_Init(&o->dgram, BADDR_TYPE_PACKET, o->reactor, o, (BDatagram_handler)dgram_handler)) {
  268. BLog(BLOG_ERROR, "BDatagram_Init failed");
  269. goto fail0;
  270. }
  271. // bind dgram
  272. BAddr bind_addr;
  273. BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_ARP), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac);
  274. if (!BDatagram_Bind(&o->dgram, bind_addr)) {
  275. BLog(BLOG_ERROR, "BDatagram_Bind failed");
  276. goto fail1;
  277. }
  278. // set dgram send addresses
  279. BAddr dest_addr;
  280. uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  281. BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_ARP), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac);
  282. BIPAddr local_addr;
  283. BIPAddr_InitInvalid(&local_addr);
  284. BDatagram_SetSendAddrs(&o->dgram, dest_addr, local_addr);
  285. // init send interface
  286. BDatagram_SendAsync_Init(&o->dgram, sizeof(struct arp_packet));
  287. o->send_if = BDatagram_SendAsync_GetIf(&o->dgram);
  288. PacketPassInterface_Sender_Init(o->send_if, (PacketPassInterface_handler_done)send_if_handler_done, o);
  289. // set not sending
  290. o->send_sending = 0;
  291. // init recv interface
  292. BDatagram_RecvAsync_Init(&o->dgram, sizeof(struct arp_packet));
  293. o->recv_if = BDatagram_RecvAsync_GetIf(&o->dgram);
  294. PacketRecvInterface_Receiver_Init(o->recv_if, (PacketRecvInterface_handler_done)recv_if_handler_done, o);
  295. // init timer
  296. BTimer_Init(&o->timer, 0, (BTimer_handler)timer_handler, o);
  297. // receive first packet
  298. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)&o->recv_packet);
  299. // send request
  300. send_request(o);
  301. // set timer
  302. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_INITIAL_WAITRECV);
  303. // set zero missed
  304. o->num_missed = 0;
  305. // set state initial
  306. o->state = STATE_INITIAL;
  307. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  308. DebugObject_Init(&o->d_obj);
  309. return 1;
  310. fail1:
  311. BDatagram_Free(&o->dgram);
  312. fail0:
  313. return 0;
  314. }
  315. void BArpProbe_Free (BArpProbe *o)
  316. {
  317. DebugObject_Free(&o->d_obj);
  318. DebugError_Free(&o->d_err);
  319. // free timer
  320. BReactor_RemoveTimer(o->reactor, &o->timer);
  321. // free recv interface
  322. BDatagram_RecvAsync_Free(&o->dgram);
  323. // free send interface
  324. BDatagram_SendAsync_Free(&o->dgram);
  325. // free dgram
  326. BDatagram_Free(&o->dgram);
  327. }