BDHCPClient.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * @file BDHCPClient.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 <system/BLog.h>
  36. #include <dhcpclient/BDHCPClient.h>
  37. #include <generated/blog_channel_BDHCPClient.h>
  38. #define COMPONENT_SOURCE 1
  39. #define COMPONENT_SINK 2
  40. #define DHCP_SERVER_PORT 67
  41. #define DHCP_CLIENT_PORT 68
  42. #define IPUDP_OVERHEAD (sizeof(struct ipv4_header) + sizeof(struct udp_header))
  43. static const struct sock_filter dhcp_sock_filter[] = {
  44. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 9), // A <- IP protocol
  45. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPV4_PROTOCOL_UDP, 0, 3), // IP protocol = UDP ?
  46. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 22), // A <- UDP destination port
  47. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), // UDP destination port = DHCP client ?
  48. BPF_STMT(BPF_RET + BPF_K, 65535), // return all
  49. BPF_STMT(BPF_RET + BPF_K, 0) // ignore
  50. };
  51. static void error_handler (BDHCPClient *o, int component, int code)
  52. {
  53. DebugObject_Access(&o->d_obj);
  54. switch (component) {
  55. case COMPONENT_SOURCE: {
  56. BLog(BLOG_ERROR, "source error");
  57. } break;
  58. case COMPONENT_SINK: {
  59. BLog(BLOG_ERROR, "sink error");
  60. } break;
  61. default:
  62. ASSERT(0);
  63. }
  64. }
  65. static void dhcp_handler (BDHCPClient *o, int event)
  66. {
  67. DebugObject_Access(&o->d_obj);
  68. switch (event) {
  69. case BDHCPCLIENTCORE_EVENT_UP:
  70. ASSERT(!o->up)
  71. o->up = 1;
  72. o->handler(o->user, BDHCPCLIENT_EVENT_UP);
  73. return;
  74. case BDHCPCLIENTCORE_EVENT_DOWN:
  75. ASSERT(o->up)
  76. o->up = 0;
  77. o->handler(o->user, BDHCPCLIENT_EVENT_DOWN);
  78. return;
  79. default:
  80. ASSERT(0);
  81. }
  82. }
  83. static int get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex)
  84. {
  85. struct ifreq ifr;
  86. int s = socket(AF_INET, SOCK_DGRAM, 0);
  87. if (!s) {
  88. BLog(BLOG_ERROR, "socket failed");
  89. goto fail0;
  90. }
  91. // get MAC
  92. memset(&ifr, 0, sizeof(ifr));
  93. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  94. if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
  95. BLog(BLOG_ERROR, "ioctl(SIOCGIFHWADDR) failed");
  96. goto fail1;
  97. }
  98. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  99. BLog(BLOG_ERROR, "hardware address not ethernet");
  100. goto fail1;
  101. }
  102. memcpy(out_mac, ifr.ifr_hwaddr.sa_data, 6);
  103. // get MTU
  104. memset(&ifr, 0, sizeof(ifr));
  105. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  106. if (ioctl(s, SIOCGIFMTU, &ifr)) {
  107. BLog(BLOG_ERROR, "ioctl(SIOCGIFMTU) failed");
  108. goto fail1;
  109. }
  110. *out_mtu = ifr.ifr_mtu;
  111. // get interface index
  112. memset(&ifr, 0, sizeof(ifr));
  113. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  114. if (ioctl(s, SIOCGIFINDEX, &ifr)) {
  115. BLog(BLOG_ERROR, "ioctl(SIOCGIFINDEX) failed");
  116. goto fail1;
  117. }
  118. *out_ifindex = ifr.ifr_ifindex;
  119. close(s);
  120. return 1;
  121. fail1:
  122. close(s);
  123. fail0:
  124. return 0;
  125. }
  126. int BDHCPClient_Init (BDHCPClient *o, const char *ifname, BReactor *reactor, BDHCPClient_handler handler, void *user)
  127. {
  128. // init arguments
  129. o->reactor = reactor;
  130. o->handler = handler;
  131. o->user = user;
  132. // get interface information
  133. uint8_t if_mac[6];
  134. int if_mtu;
  135. int if_index;
  136. if (!get_iface_info(ifname, if_mac, &if_mtu, &if_index)) {
  137. BLog(BLOG_ERROR, "failed to get interface information");
  138. goto fail0;
  139. }
  140. BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d",
  141. if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index);
  142. if (if_mtu < IPUDP_OVERHEAD) {
  143. BLog(BLOG_ERROR, "MTU is too small for UDP/IP !?!");
  144. goto fail0;
  145. }
  146. int dhcp_mtu = if_mtu - IPUDP_OVERHEAD;
  147. // init socket
  148. if (BSocket_Init(&o->sock, o->reactor, BADDR_TYPE_PACKET, BSOCKET_TYPE_DGRAM) < 0) {
  149. BLog(BLOG_ERROR, "BSocket_Init failed");
  150. goto fail0;
  151. }
  152. // set socket filter
  153. {
  154. size_t flen = sizeof(dhcp_sock_filter) / sizeof(dhcp_sock_filter[0]);
  155. struct sock_filter filter[flen];
  156. memcpy(filter, dhcp_sock_filter, sizeof(filter));
  157. struct sock_fprog fprog = {
  158. .len = flen,
  159. .filter = filter
  160. };
  161. if (setsockopt(BSocket_SockFd(&o->sock), SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
  162. BLog(BLOG_NOTICE, "not using socket filter");
  163. }
  164. }
  165. // bind socket
  166. BAddr bind_addr;
  167. BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac);
  168. if (BSocket_Bind(&o->sock, &bind_addr) < 0) {
  169. BLog(BLOG_ERROR, "BSocket_Bind failed");
  170. goto fail1;
  171. }
  172. // init error handler
  173. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  174. // init sending
  175. // init sink
  176. BAddr dest_addr;
  177. uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  178. BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac);
  179. BIPAddr local_addr;
  180. BIPAddr_InitInvalid(&local_addr);
  181. DatagramSocketSink_Init(&o->send_sink, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), &o->sock, if_mtu, dest_addr, local_addr, BReactor_PendingGroup(o->reactor));
  182. // init copier
  183. PacketCopier_Init(&o->send_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor));
  184. // init encoder
  185. DHCPIpUdpEncoder_Init(&o->send_encoder, PacketCopier_GetOutput(&o->send_copier), BReactor_PendingGroup(o->reactor));
  186. // init buffer
  187. if (!SinglePacketBuffer_Init(&o->send_buffer, DHCPIpUdpEncoder_GetOutput(&o->send_encoder), DatagramSocketSink_GetInput(&o->send_sink), BReactor_PendingGroup(o->reactor))) {
  188. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  189. goto fail2;
  190. }
  191. // init receiving
  192. // init source
  193. DatagramSocketSource_Init(&o->recv_source, FlowErrorReporter_Create(&o->domain, COMPONENT_SOURCE), &o->sock, if_mtu, BReactor_PendingGroup(o->reactor));
  194. // init copier
  195. PacketCopier_Init(&o->recv_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor));
  196. // init decoder
  197. DHCPIpUdpDecoder_Init(&o->recv_decoder, PacketCopier_GetInput(&o->recv_copier), BReactor_PendingGroup(o->reactor));
  198. // init buffer
  199. if (!SinglePacketBuffer_Init(&o->recv_buffer, DatagramSocketSource_GetOutput(&o->recv_source), DHCPIpUdpDecoder_GetInput(&o->recv_decoder), BReactor_PendingGroup(o->reactor))) {
  200. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  201. goto fail3;
  202. }
  203. // init dhcp
  204. if (!BDHCPClientCore_Init(&o->dhcp, PacketCopier_GetInput(&o->send_copier), PacketCopier_GetOutput(&o->recv_copier), if_mac, o->reactor, (BDHCPClientCore_handler)dhcp_handler, o)) {
  205. BLog(BLOG_ERROR, "BDHCPClientCore_Init failed");
  206. goto fail4;
  207. }
  208. // set not up
  209. o->up = 0;
  210. DebugObject_Init(&o->d_obj);
  211. return 1;
  212. fail4:
  213. SinglePacketBuffer_Free(&o->recv_buffer);
  214. fail3:
  215. DHCPIpUdpDecoder_Free(&o->recv_decoder);
  216. PacketCopier_Free(&o->recv_copier);
  217. DatagramSocketSource_Free(&o->recv_source);
  218. SinglePacketBuffer_Free(&o->send_buffer);
  219. fail2:
  220. DHCPIpUdpEncoder_Free(&o->send_encoder);
  221. PacketCopier_Free(&o->send_copier);
  222. DatagramSocketSink_Free(&o->send_sink);
  223. fail1:
  224. BSocket_Free(&o->sock);
  225. fail0:
  226. return 0;
  227. }
  228. void BDHCPClient_Free (BDHCPClient *o)
  229. {
  230. DebugObject_Free(&o->d_obj);
  231. // free dhcp
  232. BDHCPClientCore_Free(&o->dhcp);
  233. // free receiving
  234. SinglePacketBuffer_Free(&o->recv_buffer);
  235. DHCPIpUdpDecoder_Free(&o->recv_decoder);
  236. PacketCopier_Free(&o->recv_copier);
  237. DatagramSocketSource_Free(&o->recv_source);
  238. // free sending
  239. SinglePacketBuffer_Free(&o->send_buffer);
  240. DHCPIpUdpEncoder_Free(&o->send_encoder);
  241. PacketCopier_Free(&o->send_copier);
  242. DatagramSocketSink_Free(&o->send_sink);
  243. // free socket
  244. BSocket_Free(&o->sock);
  245. }
  246. int BDHCPClient_IsUp (BDHCPClient *o)
  247. {
  248. DebugObject_Access(&o->d_obj);
  249. return o->up;
  250. }
  251. void BDHCPClient_GetClientIP (BDHCPClient *o, uint32_t *out_ip)
  252. {
  253. DebugObject_Access(&o->d_obj);
  254. ASSERT(o->up)
  255. BDHCPClientCore_GetClientIP(&o->dhcp, out_ip);
  256. }
  257. void BDHCPClient_GetClientMask (BDHCPClient *o, uint32_t *out_mask)
  258. {
  259. DebugObject_Access(&o->d_obj);
  260. ASSERT(o->up)
  261. BDHCPClientCore_GetClientMask(&o->dhcp, out_mask);
  262. }
  263. int BDHCPClient_GetRouter (BDHCPClient *o, uint32_t *out_router)
  264. {
  265. DebugObject_Access(&o->d_obj);
  266. ASSERT(o->up)
  267. return BDHCPClientCore_GetRouter(&o->dhcp, out_router);
  268. }
  269. int BDHCPClient_GetDNS (BDHCPClient *o, uint32_t *out_dns_servers, size_t max_dns_servers)
  270. {
  271. DebugObject_Access(&o->d_obj);
  272. ASSERT(o->up)
  273. return BDHCPClientCore_GetDNS(&o->dhcp, out_dns_servers, max_dns_servers);
  274. }