BArpProbe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <misc/get_iface_info.h>
  43. #include <base/BLog.h>
  44. #include "BArpProbe.h"
  45. #include <generated/blog_channel_BArpProbe.h>
  46. #define STATE_INITIAL 1
  47. #define STATE_NOEXIST 2
  48. #define STATE_EXIST 3
  49. #define STATE_EXIST_PANIC 4
  50. static void dgram_handler (BArpProbe *o, int event)
  51. {
  52. DebugObject_Access(&o->d_obj);
  53. BLog(BLOG_ERROR, "packet socket error");
  54. // report error
  55. DEBUGERROR(&o->d_err, o->handler(o->user, BARPPROBE_EVENT_ERROR));
  56. return;
  57. }
  58. static void send_request (BArpProbe *o)
  59. {
  60. if (o->send_sending) {
  61. BLog(BLOG_ERROR, "cannot send packet while another packet is being sent!");
  62. return;
  63. }
  64. // build packet
  65. struct arp_packet *arp = &o->send_packet;
  66. arp->hardware_type = hton16(ARP_HARDWARE_TYPE_ETHERNET);
  67. arp->protocol_type = hton16(ETHERTYPE_IPV4);
  68. arp->hardware_size = hton8(6);
  69. arp->protocol_size = hton8(4);
  70. arp->opcode = hton16(ARP_OPCODE_REQUEST);
  71. memcpy(arp->sender_mac, o->if_mac, 6);
  72. arp->sender_ip = hton32(0);
  73. memset(arp->target_mac, 0, sizeof(arp->target_mac));
  74. arp->target_ip = o->addr;
  75. // send packet
  76. PacketPassInterface_Sender_Send(o->send_if, (uint8_t *)&o->send_packet, sizeof(o->send_packet));
  77. // set sending
  78. o->send_sending = 1;
  79. }
  80. static void send_if_handler_done (BArpProbe *o)
  81. {
  82. DebugObject_Access(&o->d_obj);
  83. ASSERT(o->send_sending)
  84. // set not sending
  85. o->send_sending = 0;
  86. }
  87. static void recv_if_handler_done (BArpProbe *o, int data_len)
  88. {
  89. DebugObject_Access(&o->d_obj);
  90. ASSERT(data_len >= 0)
  91. ASSERT(data_len <= sizeof(struct arp_packet))
  92. // receive next packet
  93. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)&o->recv_packet);
  94. if (data_len != sizeof(struct arp_packet)) {
  95. BLog(BLOG_WARNING, "receive: wrong size");
  96. return;
  97. }
  98. struct arp_packet *arp = &o->recv_packet;
  99. if (ntoh16(arp->hardware_type) != ARP_HARDWARE_TYPE_ETHERNET) {
  100. BLog(BLOG_WARNING, "receive: wrong hardware type");
  101. return;
  102. }
  103. if (ntoh16(arp->protocol_type) != ETHERTYPE_IPV4) {
  104. BLog(BLOG_WARNING, "receive: wrong protocol type");
  105. return;
  106. }
  107. if (ntoh8(arp->hardware_size) != 6) {
  108. BLog(BLOG_WARNING, "receive: wrong hardware size");
  109. return;
  110. }
  111. if (ntoh8(arp->protocol_size) != 4) {
  112. BLog(BLOG_WARNING, "receive: wrong protocol size");
  113. return;
  114. }
  115. if (ntoh16(arp->opcode) != ARP_OPCODE_REPLY) {
  116. return;
  117. }
  118. if (arp->sender_ip != o->addr) {
  119. return;
  120. }
  121. int old_state = o->state;
  122. // set minus one missed
  123. o->num_missed = -1;
  124. // set timer
  125. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_WAITSEND);
  126. // set state exist
  127. o->state = STATE_EXIST;
  128. // report exist if needed
  129. if (old_state == STATE_INITIAL || old_state == STATE_NOEXIST) {
  130. o->handler(o->user, BARPPROBE_EVENT_EXIST);
  131. return;
  132. }
  133. }
  134. static void timer_handler (BArpProbe *o)
  135. {
  136. DebugObject_Access(&o->d_obj);
  137. // send request
  138. send_request(o);
  139. switch (o->state) {
  140. case STATE_INITIAL: {
  141. ASSERT(o->num_missed >= 0)
  142. ASSERT(o->num_missed < BARPPROBE_INITIAL_NUM_ATTEMPTS)
  143. // increment missed
  144. o->num_missed++;
  145. // all attempts failed?
  146. if (o->num_missed == BARPPROBE_INITIAL_NUM_ATTEMPTS) {
  147. // set timer
  148. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  149. // set state noexist
  150. o->state = STATE_NOEXIST;
  151. // report noexist
  152. o->handler(o->user, BARPPROBE_EVENT_NOEXIST);
  153. return;
  154. }
  155. // set timer
  156. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_INITIAL_WAITRECV);
  157. } break;
  158. case STATE_NOEXIST: {
  159. // set timer
  160. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  161. } break;
  162. case STATE_EXIST: {
  163. ASSERT(o->num_missed >= -1)
  164. ASSERT(o->num_missed < BARPPROBE_EXIST_NUM_NOREPLY)
  165. // increment missed
  166. o->num_missed++;
  167. // all missed?
  168. if (o->num_missed == BARPPROBE_EXIST_NUM_NOREPLY) {
  169. // set timer
  170. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_PANIC_WAITRECV);
  171. // set zero missed
  172. o->num_missed = 0;
  173. // set state panic
  174. o->state = STATE_EXIST_PANIC;
  175. return;
  176. }
  177. // set timer
  178. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_WAITRECV);
  179. } break;
  180. case STATE_EXIST_PANIC: {
  181. ASSERT(o->num_missed >= 0)
  182. ASSERT(o->num_missed < BARPPROBE_EXIST_PANIC_NUM_NOREPLY)
  183. // increment missed
  184. o->num_missed++;
  185. // all missed?
  186. if (o->num_missed == BARPPROBE_EXIST_PANIC_NUM_NOREPLY) {
  187. // set timer
  188. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  189. // set state panic
  190. o->state = STATE_NOEXIST;
  191. // report noexist
  192. o->handler(o->user, BARPPROBE_EVENT_NOEXIST);
  193. return;
  194. }
  195. // set timer
  196. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_PANIC_WAITRECV);
  197. } break;
  198. }
  199. }
  200. int BArpProbe_Init (BArpProbe *o, const char *ifname, uint32_t addr, BReactor *reactor, void *user, BArpProbe_handler handler)
  201. {
  202. ASSERT(ifname)
  203. ASSERT(handler)
  204. // init arguments
  205. o->addr = addr;
  206. o->reactor = reactor;
  207. o->user = user;
  208. o->handler = handler;
  209. // get interface information
  210. int if_mtu;
  211. int if_index;
  212. if (!badvpn_get_iface_info(ifname, o->if_mac, &if_mtu, &if_index)) {
  213. BLog(BLOG_ERROR, "failed to get interface information");
  214. goto fail0;
  215. }
  216. uint8_t *if_mac = o->if_mac;
  217. BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d",
  218. if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index);
  219. // check MTU
  220. if (if_mtu < sizeof(struct arp_packet)) {
  221. BLog(BLOG_ERROR, "MTU is too small for ARP !?!");
  222. goto fail0;
  223. }
  224. // init dgram
  225. if (!BDatagram_Init(&o->dgram, BADDR_TYPE_PACKET, o->reactor, o, (BDatagram_handler)dgram_handler)) {
  226. BLog(BLOG_ERROR, "BDatagram_Init failed");
  227. goto fail0;
  228. }
  229. // bind dgram
  230. BAddr bind_addr;
  231. BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_ARP), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac);
  232. if (!BDatagram_Bind(&o->dgram, bind_addr)) {
  233. BLog(BLOG_ERROR, "BDatagram_Bind failed");
  234. goto fail1;
  235. }
  236. // set dgram send addresses
  237. BAddr dest_addr;
  238. uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  239. BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_ARP), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac);
  240. BIPAddr local_addr;
  241. BIPAddr_InitInvalid(&local_addr);
  242. BDatagram_SetSendAddrs(&o->dgram, dest_addr, local_addr);
  243. // init send interface
  244. BDatagram_SendAsync_Init(&o->dgram, sizeof(struct arp_packet));
  245. o->send_if = BDatagram_SendAsync_GetIf(&o->dgram);
  246. PacketPassInterface_Sender_Init(o->send_if, (PacketPassInterface_handler_done)send_if_handler_done, o);
  247. // set not sending
  248. o->send_sending = 0;
  249. // init recv interface
  250. BDatagram_RecvAsync_Init(&o->dgram, sizeof(struct arp_packet));
  251. o->recv_if = BDatagram_RecvAsync_GetIf(&o->dgram);
  252. PacketRecvInterface_Receiver_Init(o->recv_if, (PacketRecvInterface_handler_done)recv_if_handler_done, o);
  253. // init timer
  254. BTimer_Init(&o->timer, 0, (BTimer_handler)timer_handler, o);
  255. // receive first packet
  256. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)&o->recv_packet);
  257. // send request
  258. send_request(o);
  259. // set timer
  260. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_INITIAL_WAITRECV);
  261. // set zero missed
  262. o->num_missed = 0;
  263. // set state initial
  264. o->state = STATE_INITIAL;
  265. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  266. DebugObject_Init(&o->d_obj);
  267. return 1;
  268. fail1:
  269. BDatagram_Free(&o->dgram);
  270. fail0:
  271. return 0;
  272. }
  273. void BArpProbe_Free (BArpProbe *o)
  274. {
  275. DebugObject_Free(&o->d_obj);
  276. DebugError_Free(&o->d_err);
  277. // free timer
  278. BReactor_RemoveTimer(o->reactor, &o->timer);
  279. // free recv interface
  280. BDatagram_RecvAsync_Free(&o->dgram);
  281. // free send interface
  282. BDatagram_SendAsync_Free(&o->dgram);
  283. // free dgram
  284. BDatagram_Free(&o->dgram);
  285. }