BArpProbe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * @file BArpProbe.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/socket.h>
  26. #include <net/if.h>
  27. #include <net/if_arp.h>
  28. #include <sys/ioctl.h>
  29. #include <linux/filter.h>
  30. #include <misc/debug.h>
  31. #include <misc/byteorder.h>
  32. #include <misc/ethernet_proto.h>
  33. #include <misc/ipv4_proto.h>
  34. #include <misc/udp_proto.h>
  35. #include <base/BLog.h>
  36. #include "BArpProbe.h"
  37. #include <generated/blog_channel_BArpProbe.h>
  38. #define STATE_INITIAL 1
  39. #define STATE_NOEXIST 2
  40. #define STATE_EXIST 3
  41. #define STATE_EXIST_PANIC 4
  42. static int get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex)
  43. {
  44. struct ifreq ifr;
  45. int s = socket(AF_INET, SOCK_DGRAM, 0);
  46. if (!s) {
  47. BLog(BLOG_ERROR, "socket failed");
  48. goto fail0;
  49. }
  50. // get MAC
  51. memset(&ifr, 0, sizeof(ifr));
  52. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  53. if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
  54. BLog(BLOG_ERROR, "ioctl(SIOCGIFHWADDR) failed");
  55. goto fail1;
  56. }
  57. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  58. BLog(BLOG_ERROR, "hardware address not ethernet");
  59. goto fail1;
  60. }
  61. memcpy(out_mac, ifr.ifr_hwaddr.sa_data, 6);
  62. // get MTU
  63. memset(&ifr, 0, sizeof(ifr));
  64. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  65. if (ioctl(s, SIOCGIFMTU, &ifr)) {
  66. BLog(BLOG_ERROR, "ioctl(SIOCGIFMTU) failed");
  67. goto fail1;
  68. }
  69. *out_mtu = ifr.ifr_mtu;
  70. // get interface index
  71. memset(&ifr, 0, sizeof(ifr));
  72. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  73. if (ioctl(s, SIOCGIFINDEX, &ifr)) {
  74. BLog(BLOG_ERROR, "ioctl(SIOCGIFINDEX) failed");
  75. goto fail1;
  76. }
  77. *out_ifindex = ifr.ifr_ifindex;
  78. close(s);
  79. return 1;
  80. fail1:
  81. close(s);
  82. fail0:
  83. return 0;
  84. }
  85. static void dgram_handler (BArpProbe *o, int event)
  86. {
  87. DebugObject_Access(&o->d_obj);
  88. BLog(BLOG_ERROR, "packet socket error");
  89. // report error
  90. DEBUGERROR(&o->d_err, o->handler(o->user, BARPPROBE_EVENT_ERROR));
  91. return;
  92. }
  93. static void send_request (BArpProbe *o)
  94. {
  95. if (o->send_sending) {
  96. BLog(BLOG_ERROR, "cannot send packet while another packet is being sent!");
  97. return;
  98. }
  99. // build packet
  100. struct arp_packet *arp = &o->send_packet;
  101. arp->hardware_type = hton16(ARP_HARDWARE_TYPE_ETHERNET);
  102. arp->protocol_type = hton16(ETHERTYPE_IPV4);
  103. arp->hardware_size = hton8(6);
  104. arp->protocol_size = hton8(4);
  105. arp->opcode = hton16(ARP_OPCODE_REQUEST);
  106. memcpy(arp->sender_mac, o->if_mac, 6);
  107. arp->sender_ip = hton32(0);
  108. memset(arp->target_mac, 0, sizeof(arp->target_mac));
  109. arp->target_ip = o->addr;
  110. // send packet
  111. PacketPassInterface_Sender_Send(o->send_if, (uint8_t *)&o->send_packet, sizeof(o->send_packet));
  112. // set sending
  113. o->send_sending = 1;
  114. }
  115. static void send_if_handler_done (BArpProbe *o)
  116. {
  117. DebugObject_Access(&o->d_obj);
  118. ASSERT(o->send_sending)
  119. // set not sending
  120. o->send_sending = 0;
  121. }
  122. static void recv_if_handler_done (BArpProbe *o, int data_len)
  123. {
  124. DebugObject_Access(&o->d_obj);
  125. ASSERT(data_len >= 0)
  126. ASSERT(data_len <= sizeof(struct arp_packet))
  127. // receive next packet
  128. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)&o->recv_packet);
  129. if (data_len != sizeof(struct arp_packet)) {
  130. BLog(BLOG_WARNING, "receive: wrong size");
  131. return;
  132. }
  133. struct arp_packet *arp = &o->recv_packet;
  134. if (ntoh16(arp->hardware_type) != ARP_HARDWARE_TYPE_ETHERNET) {
  135. BLog(BLOG_WARNING, "receive: wrong hardware type");
  136. return;
  137. }
  138. if (ntoh16(arp->protocol_type) != ETHERTYPE_IPV4) {
  139. BLog(BLOG_WARNING, "receive: wrong protocol type");
  140. return;
  141. }
  142. if (ntoh8(arp->hardware_size) != 6) {
  143. BLog(BLOG_WARNING, "receive: wrong hardware size");
  144. return;
  145. }
  146. if (ntoh8(arp->protocol_size) != 4) {
  147. BLog(BLOG_WARNING, "receive: wrong protocol size");
  148. return;
  149. }
  150. if (ntoh16(arp->opcode) != ARP_OPCODE_REPLY) {
  151. return;
  152. }
  153. if (arp->sender_ip != o->addr) {
  154. return;
  155. }
  156. int old_state = o->state;
  157. // set minus one missed
  158. o->num_missed = -1;
  159. // set timer
  160. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_WAITSEND);
  161. // set state exist
  162. o->state = STATE_EXIST;
  163. // report exist if needed
  164. if (old_state == STATE_INITIAL || old_state == STATE_NOEXIST) {
  165. o->handler(o->user, BARPPROBE_EVENT_EXIST);
  166. return;
  167. }
  168. }
  169. static void timer_handler (BArpProbe *o)
  170. {
  171. DebugObject_Access(&o->d_obj);
  172. // send request
  173. send_request(o);
  174. switch (o->state) {
  175. case STATE_INITIAL: {
  176. ASSERT(o->num_missed >= 0)
  177. ASSERT(o->num_missed < BARPPROBE_INITIAL_NUM_ATTEMPTS)
  178. // increment missed
  179. o->num_missed++;
  180. // all attempts failed?
  181. if (o->num_missed == BARPPROBE_INITIAL_NUM_ATTEMPTS) {
  182. // set timer
  183. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  184. // set state noexist
  185. o->state = STATE_NOEXIST;
  186. // report noexist
  187. o->handler(o->user, BARPPROBE_EVENT_NOEXIST);
  188. return;
  189. }
  190. // set timer
  191. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_INITIAL_WAITRECV);
  192. } break;
  193. case STATE_NOEXIST: {
  194. // set timer
  195. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  196. } break;
  197. case STATE_EXIST: {
  198. ASSERT(o->num_missed >= -1)
  199. ASSERT(o->num_missed < BARPPROBE_EXIST_NUM_NOREPLY)
  200. // increment missed
  201. o->num_missed++;
  202. // all missed?
  203. if (o->num_missed == BARPPROBE_EXIST_NUM_NOREPLY) {
  204. // set timer
  205. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_PANIC_WAITRECV);
  206. // set zero missed
  207. o->num_missed = 0;
  208. // set state panic
  209. o->state = STATE_EXIST_PANIC;
  210. return;
  211. }
  212. // set timer
  213. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_WAITRECV);
  214. } break;
  215. case STATE_EXIST_PANIC: {
  216. ASSERT(o->num_missed >= 0)
  217. ASSERT(o->num_missed < BARPPROBE_EXIST_PANIC_NUM_NOREPLY)
  218. // increment missed
  219. o->num_missed++;
  220. // all missed?
  221. if (o->num_missed == BARPPROBE_EXIST_PANIC_NUM_NOREPLY) {
  222. // set timer
  223. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_NOEXIST_WAITRECV);
  224. // set state panic
  225. o->state = STATE_NOEXIST;
  226. // report noexist
  227. o->handler(o->user, BARPPROBE_EVENT_NOEXIST);
  228. return;
  229. }
  230. // set timer
  231. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_EXIST_PANIC_WAITRECV);
  232. } break;
  233. }
  234. }
  235. int BArpProbe_Init (BArpProbe *o, const char *ifname, uint32_t addr, BReactor *reactor, void *user, BArpProbe_handler handler)
  236. {
  237. ASSERT(ifname)
  238. ASSERT(handler)
  239. // init arguments
  240. o->addr = addr;
  241. o->reactor = reactor;
  242. o->user = user;
  243. o->handler = handler;
  244. // get interface information
  245. int if_mtu;
  246. int if_index;
  247. if (!get_iface_info(ifname, o->if_mac, &if_mtu, &if_index)) {
  248. BLog(BLOG_ERROR, "failed to get interface information");
  249. goto fail0;
  250. }
  251. uint8_t *if_mac = o->if_mac;
  252. BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d",
  253. if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index);
  254. // check MTU
  255. if (if_mtu < sizeof(struct arp_packet)) {
  256. BLog(BLOG_ERROR, "MTU is too small for ARP !?!");
  257. goto fail0;
  258. }
  259. // init dgram
  260. if (!BDatagram_Init(&o->dgram, BADDR_TYPE_PACKET, o->reactor, o, (BDatagram_handler)dgram_handler)) {
  261. BLog(BLOG_ERROR, "BDatagram_Init failed");
  262. goto fail0;
  263. }
  264. // bind dgram
  265. BAddr bind_addr;
  266. BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_ARP), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac);
  267. if (!BDatagram_Bind(&o->dgram, bind_addr)) {
  268. BLog(BLOG_ERROR, "BDatagram_Bind failed");
  269. goto fail1;
  270. }
  271. // set dgram send addresses
  272. BAddr dest_addr;
  273. uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  274. BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_ARP), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac);
  275. BIPAddr local_addr;
  276. BIPAddr_InitInvalid(&local_addr);
  277. BDatagram_SetSendAddrs(&o->dgram, dest_addr, local_addr);
  278. // init send interface
  279. BDatagram_SendAsync_Init(&o->dgram, sizeof(struct arp_packet));
  280. o->send_if = BDatagram_SendAsync_GetIf(&o->dgram);
  281. PacketPassInterface_Sender_Init(o->send_if, (PacketPassInterface_handler_done)send_if_handler_done, o);
  282. // set not sending
  283. o->send_sending = 0;
  284. // init recv interface
  285. BDatagram_RecvAsync_Init(&o->dgram, sizeof(struct arp_packet));
  286. o->recv_if = BDatagram_RecvAsync_GetIf(&o->dgram);
  287. PacketRecvInterface_Receiver_Init(o->recv_if, (PacketRecvInterface_handler_done)recv_if_handler_done, o);
  288. // init timer
  289. BTimer_Init(&o->timer, 0, (BTimer_handler)timer_handler, o);
  290. // receive first packet
  291. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)&o->recv_packet);
  292. // send request
  293. send_request(o);
  294. // set timer
  295. BReactor_SetTimerAfter(o->reactor, &o->timer, BARPPROBE_INITIAL_WAITRECV);
  296. // set zero missed
  297. o->num_missed = 0;
  298. // set state initial
  299. o->state = STATE_INITIAL;
  300. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  301. DebugObject_Init(&o->d_obj);
  302. return 1;
  303. fail1:
  304. BDatagram_Free(&o->dgram);
  305. fail0:
  306. return 0;
  307. }
  308. void BArpProbe_Free (BArpProbe *o)
  309. {
  310. DebugObject_Free(&o->d_obj);
  311. DebugError_Free(&o->d_err);
  312. // free timer
  313. BReactor_RemoveTimer(o->reactor, &o->timer);
  314. // free recv interface
  315. BDatagram_RecvAsync_Free(&o->dgram);
  316. // free send interface
  317. BDatagram_SendAsync_Free(&o->dgram);
  318. // free dgram
  319. BDatagram_Free(&o->dgram);
  320. }