dhcp_server.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * File : dhcp_server.c
  3. * A simple DHCP server implementation
  4. *
  5. * This file is part of RT-Thread RTOS
  6. * COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
  7. * http://www.rt-thread.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. * 2013-01-30 aozima the first version
  26. * 2013-08-08 aozima support different network segments.
  27. * 2015-01-30 bernard release to RT-Thread RTOS.
  28. * 2017-12-27 aozima add [mac-ip] table support.
  29. */
  30. #include <stdio.h>
  31. #include <stdint.h>
  32. #include <rtthread.h>
  33. #include <lwip/opt.h>
  34. #include <lwip/sockets.h>
  35. #include <lwip/inet_chksum.h>
  36. #include <netif/etharp.h>
  37. #include <netif/ethernetif.h>
  38. #include <lwip/ip.h>
  39. #include <lwip/init.h>
  40. #if (LWIP_VERSION) >= 0x02000000U
  41. #include <lwip/prot/dhcp.h>
  42. #endif
  43. /* DHCP server option */
  44. /* allocated client ip range */
  45. #ifndef DHCPD_CLIENT_IP_MIN
  46. #define DHCPD_CLIENT_IP_MIN 2
  47. #endif
  48. #ifndef DHCPD_CLIENT_IP_MAX
  49. #define DHCPD_CLIENT_IP_MAX 254
  50. #endif
  51. /* the DHCP server address */
  52. #ifndef DHCPD_SERVER_IP
  53. #define DHCPD_SERVER_IP "192.168.169.1"
  54. #endif
  55. //#define DHCP_DEBUG_PRINTF
  56. #ifdef DHCP_DEBUG_PRINTF
  57. #define DEBUG_PRINTF rt_kprintf("[DHCP] "); rt_kprintf
  58. #else
  59. #define DEBUG_PRINTF(...)
  60. #endif /* DHCP_DEBUG_PRINTF */
  61. /* we need some routines in the DHCP of lwIP */
  62. #undef LWIP_DHCP
  63. #define LWIP_DHCP 1
  64. #include <lwip/dhcp.h>
  65. /* buffer size for receive DHCP packet */
  66. #define BUFSZ 1024
  67. #ifndef MAC_ADDR_LEN
  68. #define MAC_ADDR_LEN 6
  69. #endif
  70. #ifndef MAC_TABLE_LEN
  71. #define MAC_TABLE_LEN 4
  72. #endif
  73. struct mac_addr_t
  74. {
  75. uint8_t add[MAC_ADDR_LEN];
  76. };
  77. struct mac_ip_item_t
  78. {
  79. struct mac_addr_t mac_addr;
  80. uint8_t ip_addr_3;
  81. };
  82. static rt_err_t _low_level_dhcp_send(struct netif *netif,
  83. const void *buffer,
  84. rt_size_t size)
  85. {
  86. struct pbuf *p;
  87. struct eth_hdr *ethhdr;
  88. struct ip_hdr *iphdr;
  89. struct udp_hdr *udphdr;
  90. p = pbuf_alloc(PBUF_LINK,
  91. SIZEOF_ETH_HDR + sizeof(struct ip_hdr)
  92. + sizeof(struct udp_hdr) + size,
  93. PBUF_RAM);
  94. if (p == RT_NULL) return -RT_ENOMEM;
  95. ethhdr = (struct eth_hdr *)p->payload;
  96. iphdr = (struct ip_hdr *)((char *)ethhdr + SIZEOF_ETH_HDR);
  97. udphdr = (struct udp_hdr *)((char *)iphdr + sizeof(struct ip_hdr));
  98. ETHADDR32_COPY(&ethhdr->dest, (struct eth_addr *)&ethbroadcast);
  99. ETHADDR16_COPY(&ethhdr->src, netif->hwaddr);
  100. ethhdr->type = PP_HTONS(ETHTYPE_IP);
  101. iphdr->src.addr = 0x00000000; /* src: 0.0.0.0 */
  102. iphdr->dest.addr = 0xFFFFFFFF; /* src: 255.255.255.255 */
  103. IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
  104. IPH_TOS_SET(iphdr, 0x00);
  105. IPH_LEN_SET(iphdr, htons(IP_HLEN + sizeof(struct udp_hdr) + size));
  106. IPH_ID_SET(iphdr, htons(2));
  107. IPH_OFFSET_SET(iphdr, 0);
  108. IPH_TTL_SET(iphdr, 255);
  109. IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
  110. IPH_CHKSUM_SET(iphdr, 0);
  111. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  112. udphdr->src = htons(DHCP_SERVER_PORT);
  113. udphdr->dest = htons(DHCP_CLIENT_PORT);
  114. udphdr->len = htons(sizeof(struct udp_hdr) + size);
  115. udphdr->chksum = 0;
  116. memcpy((char *)udphdr + sizeof(struct udp_hdr),
  117. buffer, size);
  118. netif->linkoutput(netif, p);
  119. pbuf_free(p);
  120. return RT_EOK;
  121. }
  122. static uint8_t get_ip(struct mac_addr_t *p_mac_addr)
  123. {
  124. static uint8_t next_client_ip = DHCPD_CLIENT_IP_MIN;
  125. static struct mac_ip_item_t mac_table[MAC_TABLE_LEN];
  126. static int offset = 0;
  127. struct mac_addr_t bad_mac;
  128. int i;
  129. uint8_t ip_addr_3;
  130. rt_memset(&bad_mac, 0, sizeof(bad_mac));
  131. if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac)))
  132. {
  133. DEBUG_PRINTF("mac address all zero");
  134. ip_addr_3 = DHCPD_CLIENT_IP_MAX;
  135. goto _return;
  136. }
  137. rt_memset(&bad_mac, 0xFF, sizeof(bad_mac));
  138. if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac)))
  139. {
  140. DEBUG_PRINTF("mac address all one");
  141. ip_addr_3 = DHCPD_CLIENT_IP_MAX;
  142. goto _return;
  143. }
  144. for (i = 0; i < MAC_TABLE_LEN; i++)
  145. {
  146. if (!rt_memcmp(&mac_table[i].mac_addr, p_mac_addr, sizeof(bad_mac)))
  147. {
  148. //use old ip
  149. ip_addr_3 = mac_table[i].ip_addr_3;
  150. DEBUG_PRINTF("return old ip: %d\n", (int)ip_addr_3);
  151. goto _return;
  152. }
  153. }
  154. /* add new ip */
  155. mac_table[offset].mac_addr = *p_mac_addr;
  156. mac_table[offset].ip_addr_3 = next_client_ip;
  157. ip_addr_3 = mac_table[offset].ip_addr_3 ;
  158. offset++;
  159. if (offset >= MAC_TABLE_LEN)
  160. offset = 0;
  161. next_client_ip++;
  162. if (next_client_ip > DHCPD_CLIENT_IP_MAX)
  163. next_client_ip = DHCPD_CLIENT_IP_MIN;
  164. DEBUG_PRINTF("create new ip: %d\n", (int)ip_addr_3);
  165. DEBUG_PRINTF("next_client_ip %d\n", next_client_ip);
  166. _return:
  167. return ip_addr_3;
  168. }
  169. static void dhcpd_thread_entry(void *parameter)
  170. {
  171. struct netif *netif = RT_NULL;
  172. int sock;
  173. int bytes_read;
  174. char *recv_data;
  175. rt_uint32_t addr_len;
  176. struct sockaddr_in server_addr, client_addr;
  177. struct dhcp_msg *msg;
  178. int optval = 1;
  179. struct mac_addr_t mac_addr;
  180. uint8_t DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1, DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3;
  181. /* get ethernet interface. */
  182. netif = (struct netif *) parameter;
  183. RT_ASSERT(netif != RT_NULL);
  184. /* our DHCP server information */
  185. {
  186. #if (LWIP_VERSION) >= 0x02000000U
  187. ip4_addr_t addr;
  188. #else
  189. struct ip_addr addr;
  190. #endif /* LWIP_VERSION */
  191. ip4addr_aton(DHCPD_SERVER_IP, &addr);
  192. DHCPD_SERVER_IPADDR0 = (ntohl(addr.addr) >> 24) & 0xFF;
  193. DHCPD_SERVER_IPADDR1 = (ntohl(addr.addr) >> 16) & 0xFF;
  194. DHCPD_SERVER_IPADDR2 = (ntohl(addr.addr) >> 8) & 0xFF;
  195. DHCPD_SERVER_IPADDR3 = (ntohl(addr.addr) >> 0) & 0xFF;
  196. }
  197. DEBUG_PRINTF("DHCP server IP: %d.%d.%d.%d client IP: %d.%d.%d.%d-%d\n",
  198. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  199. DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3,
  200. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  201. DHCPD_SERVER_IPADDR2, DHCPD_CLIENT_IP_MIN, DHCPD_CLIENT_IP_MAX);
  202. /* allocate buffer for receive */
  203. recv_data = rt_malloc(BUFSZ);
  204. if (recv_data == RT_NULL)
  205. {
  206. /* No memory */
  207. DEBUG_PRINTF("Out of memory\n");
  208. return;
  209. }
  210. /* create a socket with UDP */
  211. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  212. {
  213. DEBUG_PRINTF("create socket failed, errno = %d\n", errno);
  214. rt_free(recv_data);
  215. return;
  216. }
  217. /* set to receive broadcast packet */
  218. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
  219. /* initialize server address */
  220. server_addr.sin_family = AF_INET;
  221. server_addr.sin_port = htons(DHCP_SERVER_PORT);
  222. server_addr.sin_addr.s_addr = INADDR_ANY;
  223. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  224. /* bind socket to the server address */
  225. if (bind(sock, (struct sockaddr *)&server_addr,
  226. sizeof(struct sockaddr)) == -1)
  227. {
  228. /* bind failed. */
  229. DEBUG_PRINTF("bind server address failed, errno=%d\n", errno);
  230. rt_free(recv_data);
  231. return;
  232. }
  233. addr_len = sizeof(struct sockaddr);
  234. DEBUG_PRINTF("DHCP server listen on port %d...\n", DHCP_SERVER_PORT);
  235. while (1)
  236. {
  237. bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
  238. (struct sockaddr *)&client_addr, &addr_len);
  239. if (bytes_read < DHCP_MSG_LEN)
  240. {
  241. DEBUG_PRINTF("packet too short, wait for next!\n");
  242. continue;
  243. }
  244. msg = (struct dhcp_msg *)recv_data;
  245. /* check message type to make sure we can handle it */
  246. if ((msg->op != DHCP_BOOTREQUEST) || (msg->cookie != PP_HTONL(DHCP_MAGIC_COOKIE)))
  247. {
  248. continue;
  249. }
  250. memcpy(mac_addr.add, msg->chaddr, MAC_ADDR_LEN);
  251. /* handler. */
  252. {
  253. uint8_t *dhcp_opt;
  254. uint8_t option;
  255. uint8_t length;
  256. uint8_t message_type = 0;
  257. uint8_t finished = 0;
  258. uint32_t request_ip = 0;
  259. uint8_t client_ip_3;
  260. client_ip_3 = get_ip(&mac_addr);
  261. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  262. while (finished == 0)
  263. {
  264. option = *dhcp_opt;
  265. length = *(dhcp_opt + 1);
  266. switch (option)
  267. {
  268. case DHCP_OPTION_REQUESTED_IP:
  269. request_ip = *(dhcp_opt + 2) << 24 | *(dhcp_opt + 3) << 16
  270. | *(dhcp_opt + 4) << 8 | *(dhcp_opt + 5);
  271. break;
  272. case DHCP_OPTION_END:
  273. finished = 1;
  274. break;
  275. case DHCP_OPTION_MESSAGE_TYPE:
  276. message_type = *(dhcp_opt + 2);
  277. break;
  278. default:
  279. break;
  280. } /* switch(option) */
  281. dhcp_opt += (2 + length);
  282. }
  283. /* reply. */
  284. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  285. /* check. */
  286. if (request_ip)
  287. {
  288. uint32_t client_ip = DHCPD_SERVER_IPADDR0 << 24 | DHCPD_SERVER_IPADDR1 << 16
  289. | DHCPD_SERVER_IPADDR2 << 8 | client_ip_3;
  290. DEBUG_PRINTF("message_type: %d, request_ip: %08X, client_ip: %08X.\n", message_type, request_ip, client_ip);
  291. if (request_ip != client_ip)
  292. {
  293. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  294. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  295. *dhcp_opt++ = DHCP_NAK;
  296. *dhcp_opt++ = DHCP_OPTION_END;
  297. DEBUG_PRINTF("requested IP invalid, reply DHCP_NAK\n");
  298. if (netif != RT_NULL)
  299. {
  300. int send_byte = (dhcp_opt - (uint8_t *)msg);
  301. _low_level_dhcp_send(netif, msg, send_byte);
  302. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  303. }
  304. continue;
  305. }
  306. }
  307. if (message_type == DHCP_DISCOVER)
  308. {
  309. DEBUG_PRINTF("request DHCP_DISCOVER\n");
  310. DEBUG_PRINTF("reply DHCP_OFFER\n");
  311. // DHCP_OPTION_MESSAGE_TYPE
  312. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  313. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  314. *dhcp_opt++ = DHCP_OFFER;
  315. // DHCP_OPTION_SERVER_ID
  316. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  317. *dhcp_opt++ = 4;
  318. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  319. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  320. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  321. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  322. // DHCP_OPTION_LEASE_TIME
  323. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  324. *dhcp_opt++ = 4;
  325. *dhcp_opt++ = 0x00;
  326. *dhcp_opt++ = 0x01;
  327. *dhcp_opt++ = 0x51;
  328. *dhcp_opt++ = 0x80;
  329. }
  330. else if (message_type == DHCP_REQUEST)
  331. {
  332. DEBUG_PRINTF("request DHCP_REQUEST\n");
  333. DEBUG_PRINTF("reply DHCP_ACK\n");
  334. // DHCP_OPTION_MESSAGE_TYPE
  335. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  336. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  337. *dhcp_opt++ = DHCP_ACK;
  338. // DHCP_OPTION_SERVER_ID
  339. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  340. *dhcp_opt++ = 4;
  341. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  342. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  343. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  344. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  345. // DHCP_OPTION_SUBNET_MASK
  346. *dhcp_opt++ = DHCP_OPTION_SUBNET_MASK;
  347. *dhcp_opt++ = 4;
  348. *dhcp_opt++ = 0xFF;
  349. *dhcp_opt++ = 0xFF;
  350. *dhcp_opt++ = 0xFF;
  351. *dhcp_opt++ = 0x00;
  352. #ifdef DHCPD_USING_ROUTER
  353. // DHCP_OPTION_ROUTER
  354. *dhcp_opt++ = DHCP_OPTION_ROUTER;
  355. *dhcp_opt++ = 4;
  356. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  357. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  358. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  359. *dhcp_opt++ = 1;
  360. #endif
  361. // DHCP_OPTION_DNS_SERVER, use the default DNS server address in lwIP
  362. *dhcp_opt++ = DHCP_OPTION_DNS_SERVER;
  363. *dhcp_opt++ = 4;
  364. *dhcp_opt++ = 208;
  365. *dhcp_opt++ = 67;
  366. *dhcp_opt++ = 222;
  367. *dhcp_opt++ = 222;
  368. // DHCP_OPTION_LEASE_TIME
  369. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  370. *dhcp_opt++ = 4;
  371. *dhcp_opt++ = 0x00;
  372. *dhcp_opt++ = 0x01;
  373. *dhcp_opt++ = 0x51;
  374. *dhcp_opt++ = 0x80;
  375. }
  376. else
  377. {
  378. DEBUG_PRINTF("un handle message:%d\n", message_type);
  379. }
  380. // append DHCP_OPTION_END
  381. *dhcp_opt++ = DHCP_OPTION_END;
  382. /* send reply. */
  383. if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST))
  384. {
  385. msg->op = DHCP_BOOTREPLY;
  386. IP4_ADDR(&msg->yiaddr,
  387. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  388. DHCPD_SERVER_IPADDR2, client_ip_3);
  389. client_addr.sin_addr.s_addr = INADDR_BROADCAST;
  390. if (netif != RT_NULL)
  391. {
  392. int send_byte = (dhcp_opt - (uint8_t *)msg);
  393. _low_level_dhcp_send(netif, msg, send_byte);
  394. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  395. }
  396. }
  397. } /* handler. */
  398. }
  399. }
  400. void dhcpd_start(const char *netif_name)
  401. {
  402. rt_thread_t thread;
  403. struct netif *netif = netif_list;
  404. if (strlen(netif_name) > sizeof(netif->name))
  405. {
  406. rt_kprintf("network interface name too long!\r\n");
  407. return;
  408. }
  409. while (netif != RT_NULL)
  410. {
  411. if (strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  412. break;
  413. netif = netif->next;
  414. if (netif == RT_NULL)
  415. {
  416. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  417. return;
  418. }
  419. }
  420. if (1)
  421. {
  422. extern void set_if(const char *netif_name, const char *ip_addr, const char *gw_addr, const char *nm_addr);
  423. dhcp_stop(netif);
  424. set_if(netif_name, DHCPD_SERVER_IP, "0.0.0.0", "255.255.255.0");
  425. netif_set_up(netif);
  426. }
  427. thread = rt_thread_create("dhcpd",
  428. dhcpd_thread_entry, netif,
  429. 1024,
  430. RT_THREAD_PRIORITY_MAX - 3,
  431. 2);
  432. if (thread != RT_NULL)
  433. {
  434. rt_thread_startup(thread);
  435. }
  436. }