BDHCPClient.c 12 KB

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