BDHCPClient.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * @file BDHCPClient.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/dhcp_proto.h>
  43. #include <misc/get_iface_info.h>
  44. #include <base/BLog.h>
  45. #include <dhcpclient/BDHCPClient.h>
  46. #include <generated/blog_channel_BDHCPClient.h>
  47. #define DHCP_SERVER_PORT 67
  48. #define DHCP_CLIENT_PORT 68
  49. #define IPUDP_OVERHEAD (sizeof(struct ipv4_header) + sizeof(struct udp_header))
  50. static const struct sock_filter dhcp_sock_filter[] = {
  51. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 9), // A <- IP protocol
  52. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPV4_PROTOCOL_UDP, 0, 3), // IP protocol = UDP ?
  53. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 22), // A <- UDP destination port
  54. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), // UDP destination port = DHCP client ?
  55. BPF_STMT(BPF_RET + BPF_K, 65535), // return all
  56. BPF_STMT(BPF_RET + BPF_K, 0) // ignore
  57. };
  58. static void dgram_handler (BDHCPClient *o, int event)
  59. {
  60. DebugObject_Access(&o->d_obj);
  61. BLog(BLOG_ERROR, "packet socket error");
  62. // report error
  63. DEBUGERROR(&o->d_err, o->handler(o->user, BDHCPCLIENT_EVENT_ERROR));
  64. return;
  65. }
  66. static void dhcp_handler (BDHCPClient *o, int event)
  67. {
  68. DebugObject_Access(&o->d_obj);
  69. switch (event) {
  70. case BDHCPCLIENTCORE_EVENT_UP:
  71. ASSERT(!o->up)
  72. o->up = 1;
  73. o->handler(o->user, BDHCPCLIENT_EVENT_UP);
  74. return;
  75. case BDHCPCLIENTCORE_EVENT_DOWN:
  76. ASSERT(o->up)
  77. o->up = 0;
  78. o->handler(o->user, BDHCPCLIENT_EVENT_DOWN);
  79. return;
  80. default:
  81. ASSERT(0);
  82. }
  83. }
  84. static void dhcp_func_getsendermac (BDHCPClient *o, uint8_t *out_mac)
  85. {
  86. DebugObject_Access(&o->d_obj);
  87. BAddr remote_addr;
  88. BIPAddr local_addr;
  89. if (!BDatagram_GetLastReceiveAddrs(&o->dgram, &remote_addr, &local_addr)) {
  90. BLog(BLOG_ERROR, "BDatagram_GetLastReceiveAddrs failed");
  91. goto fail;
  92. }
  93. if (remote_addr.type != BADDR_TYPE_PACKET) {
  94. BLog(BLOG_ERROR, "address type invalid");
  95. goto fail;
  96. }
  97. if (remote_addr.packet.header_type != BADDR_PACKET_HEADER_TYPE_ETHERNET) {
  98. BLog(BLOG_ERROR, "address header type invalid");
  99. goto fail;
  100. }
  101. memcpy(out_mac, remote_addr.packet.phys_addr, 6);
  102. return;
  103. fail:
  104. memset(out_mac, 0, 6);
  105. }
  106. int BDHCPClient_Init (BDHCPClient *o, const char *ifname, struct BDHCPClient_opts opts, BReactor *reactor, BRandom2 *random2, BDHCPClient_handler handler, void *user)
  107. {
  108. // init arguments
  109. o->reactor = reactor;
  110. o->handler = handler;
  111. o->user = user;
  112. // get interface information
  113. uint8_t if_mac[6];
  114. int if_mtu;
  115. int if_index;
  116. if (!badvpn_get_iface_info(ifname, if_mac, &if_mtu, &if_index)) {
  117. BLog(BLOG_ERROR, "failed to get interface information");
  118. goto fail0;
  119. }
  120. BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d",
  121. if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index);
  122. if (if_mtu < IPUDP_OVERHEAD) {
  123. BLog(BLOG_ERROR, "MTU is too small for UDP/IP !?!");
  124. goto fail0;
  125. }
  126. int dhcp_mtu = if_mtu - IPUDP_OVERHEAD;
  127. // init dgram
  128. if (!BDatagram_Init(&o->dgram, BADDR_TYPE_PACKET, o->reactor, o, (BDatagram_handler)dgram_handler)) {
  129. BLog(BLOG_ERROR, "BDatagram_Init failed");
  130. goto fail0;
  131. }
  132. // set socket filter
  133. {
  134. struct sock_filter filter[sizeof(dhcp_sock_filter) / sizeof(dhcp_sock_filter[0])];
  135. memcpy(filter, dhcp_sock_filter, sizeof(filter));
  136. struct sock_fprog fprog = {
  137. .len = sizeof(filter) / sizeof(filter[0]),
  138. .filter = filter
  139. };
  140. if (setsockopt(BDatagram_GetFd(&o->dgram), SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)) < 0) {
  141. BLog(BLOG_NOTICE, "not using socket filter");
  142. }
  143. }
  144. // bind dgram
  145. BAddr bind_addr;
  146. BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac);
  147. if (!BDatagram_Bind(&o->dgram, bind_addr)) {
  148. BLog(BLOG_ERROR, "BDatagram_Bind failed");
  149. goto fail1;
  150. }
  151. // set dgram send addresses
  152. BAddr dest_addr;
  153. uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  154. BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac);
  155. BIPAddr local_addr;
  156. BIPAddr_InitInvalid(&local_addr);
  157. BDatagram_SetSendAddrs(&o->dgram, dest_addr, local_addr);
  158. // init dgram interfaces
  159. BDatagram_SendAsync_Init(&o->dgram, if_mtu);
  160. BDatagram_RecvAsync_Init(&o->dgram, if_mtu);
  161. // init sending
  162. // init copier
  163. PacketCopier_Init(&o->send_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor));
  164. // init encoder
  165. DHCPIpUdpEncoder_Init(&o->send_encoder, PacketCopier_GetOutput(&o->send_copier), BReactor_PendingGroup(o->reactor));
  166. // init buffer
  167. if (!SinglePacketBuffer_Init(&o->send_buffer, DHCPIpUdpEncoder_GetOutput(&o->send_encoder), BDatagram_SendAsync_GetIf(&o->dgram), BReactor_PendingGroup(o->reactor))) {
  168. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  169. goto fail2;
  170. }
  171. // init receiving
  172. // init copier
  173. PacketCopier_Init(&o->recv_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor));
  174. // init decoder
  175. DHCPIpUdpDecoder_Init(&o->recv_decoder, PacketCopier_GetInput(&o->recv_copier), BReactor_PendingGroup(o->reactor));
  176. // init buffer
  177. if (!SinglePacketBuffer_Init(&o->recv_buffer, BDatagram_RecvAsync_GetIf(&o->dgram), DHCPIpUdpDecoder_GetInput(&o->recv_decoder), BReactor_PendingGroup(o->reactor))) {
  178. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  179. goto fail3;
  180. }
  181. // init options
  182. struct BDHCPClientCore_opts core_opts;
  183. core_opts.hostname = opts.hostname;
  184. core_opts.vendorclassid = opts.vendorclassid;
  185. core_opts.clientid = opts.clientid;
  186. core_opts.clientid_len = opts.clientid_len;
  187. // auto-generate clientid from MAC if requested
  188. uint8_t mac_cid[7];
  189. if (opts.auto_clientid) {
  190. mac_cid[0] = DHCP_HARDWARE_ADDRESS_TYPE_ETHERNET;
  191. memcpy(mac_cid + 1, if_mac, 6);
  192. core_opts.clientid = mac_cid;
  193. core_opts.clientid_len = sizeof(mac_cid);
  194. }
  195. // init dhcp
  196. if (!BDHCPClientCore_Init(&o->dhcp, PacketCopier_GetInput(&o->send_copier), PacketCopier_GetOutput(&o->recv_copier), if_mac, core_opts, o->reactor, random2, o,
  197. (BDHCPClientCore_func_getsendermac)dhcp_func_getsendermac,
  198. (BDHCPClientCore_handler)dhcp_handler
  199. )) {
  200. BLog(BLOG_ERROR, "BDHCPClientCore_Init failed");
  201. goto fail4;
  202. }
  203. // set not up
  204. o->up = 0;
  205. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  206. DebugObject_Init(&o->d_obj);
  207. return 1;
  208. fail4:
  209. SinglePacketBuffer_Free(&o->recv_buffer);
  210. fail3:
  211. DHCPIpUdpDecoder_Free(&o->recv_decoder);
  212. PacketCopier_Free(&o->recv_copier);
  213. SinglePacketBuffer_Free(&o->send_buffer);
  214. fail2:
  215. DHCPIpUdpEncoder_Free(&o->send_encoder);
  216. PacketCopier_Free(&o->send_copier);
  217. BDatagram_RecvAsync_Free(&o->dgram);
  218. BDatagram_SendAsync_Free(&o->dgram);
  219. fail1:
  220. BDatagram_Free(&o->dgram);
  221. fail0:
  222. return 0;
  223. }
  224. void BDHCPClient_Free (BDHCPClient *o)
  225. {
  226. DebugObject_Free(&o->d_obj);
  227. DebugError_Free(&o->d_err);
  228. // free dhcp
  229. BDHCPClientCore_Free(&o->dhcp);
  230. // free receiving
  231. SinglePacketBuffer_Free(&o->recv_buffer);
  232. DHCPIpUdpDecoder_Free(&o->recv_decoder);
  233. PacketCopier_Free(&o->recv_copier);
  234. // free sending
  235. SinglePacketBuffer_Free(&o->send_buffer);
  236. DHCPIpUdpEncoder_Free(&o->send_encoder);
  237. PacketCopier_Free(&o->send_copier);
  238. // free dgram interfaces
  239. BDatagram_RecvAsync_Free(&o->dgram);
  240. BDatagram_SendAsync_Free(&o->dgram);
  241. // free dgram
  242. BDatagram_Free(&o->dgram);
  243. }
  244. int BDHCPClient_IsUp (BDHCPClient *o)
  245. {
  246. DebugObject_Access(&o->d_obj);
  247. return o->up;
  248. }
  249. void BDHCPClient_GetClientIP (BDHCPClient *o, uint32_t *out_ip)
  250. {
  251. DebugObject_Access(&o->d_obj);
  252. ASSERT(o->up)
  253. BDHCPClientCore_GetClientIP(&o->dhcp, out_ip);
  254. }
  255. void BDHCPClient_GetClientMask (BDHCPClient *o, uint32_t *out_mask)
  256. {
  257. DebugObject_Access(&o->d_obj);
  258. ASSERT(o->up)
  259. BDHCPClientCore_GetClientMask(&o->dhcp, out_mask);
  260. }
  261. int BDHCPClient_GetRouter (BDHCPClient *o, uint32_t *out_router)
  262. {
  263. DebugObject_Access(&o->d_obj);
  264. ASSERT(o->up)
  265. return BDHCPClientCore_GetRouter(&o->dhcp, out_router);
  266. }
  267. int BDHCPClient_GetDNS (BDHCPClient *o, uint32_t *out_dns_servers, size_t max_dns_servers)
  268. {
  269. DebugObject_Access(&o->d_obj);
  270. ASSERT(o->up)
  271. return BDHCPClientCore_GetDNS(&o->dhcp, out_dns_servers, max_dns_servers);
  272. }
  273. void BDHCPClient_GetServerMAC (BDHCPClient *o, uint8_t *out_mac)
  274. {
  275. DebugObject_Access(&o->d_obj);
  276. ASSERT(o->up)
  277. BDHCPClientCore_GetServerMAC(&o->dhcp, out_mac);
  278. }