dhcp_server.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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_IPADDR0
  53. #define DHCPD_SERVER_IPADDR0 192UL
  54. #define DHCPD_SERVER_IPADDR1 168UL
  55. #define DHCPD_SERVER_IPADDR2 169UL
  56. #define DHCPD_SERVER_IPADDR3 1UL
  57. #endif
  58. //#define DHCP_DEBUG_PRINTF
  59. #ifdef DHCP_DEBUG_PRINTF
  60. #define DEBUG_PRINTF rt_kprintf("[DHCP] "); rt_kprintf
  61. #else
  62. #define DEBUG_PRINTF(...)
  63. #endif /* DHCP_DEBUG_PRINTF */
  64. /* we need some routines in the DHCP of lwIP */
  65. #undef LWIP_DHCP
  66. #define LWIP_DHCP 1
  67. #include <lwip/dhcp.h>
  68. /* buffer size for receive DHCP packet */
  69. #define BUFSZ 1024
  70. #ifndef MAC_ADDR_LEN
  71. #define MAC_ADDR_LEN 6
  72. #endif
  73. #ifndef MAC_TABLE_LEN
  74. #define MAC_TABLE_LEN 4
  75. #endif
  76. struct mac_addr_t
  77. {
  78. uint8_t add[MAC_ADDR_LEN];
  79. };
  80. struct mac_ip_item_t
  81. {
  82. struct mac_addr_t mac_addr;
  83. uint8_t ip_addr_3;
  84. };
  85. static rt_err_t _low_level_dhcp_send(struct netif *netif,
  86. const void *buffer,
  87. rt_size_t size)
  88. {
  89. struct pbuf *p;
  90. struct eth_hdr *ethhdr;
  91. struct ip_hdr *iphdr;
  92. struct udp_hdr *udphdr;
  93. p = pbuf_alloc(PBUF_LINK,
  94. SIZEOF_ETH_HDR + sizeof(struct ip_hdr)
  95. + sizeof(struct udp_hdr) + size,
  96. PBUF_RAM);
  97. if (p == RT_NULL) return -RT_ENOMEM;
  98. ethhdr = (struct eth_hdr *)p->payload;
  99. iphdr = (struct ip_hdr *)((char *)ethhdr + SIZEOF_ETH_HDR);
  100. udphdr = (struct udp_hdr *)((char *)iphdr + sizeof(struct ip_hdr));
  101. ETHADDR32_COPY(&ethhdr->dest, (struct eth_addr *)&ethbroadcast);
  102. ETHADDR16_COPY(&ethhdr->src, netif->hwaddr);
  103. ethhdr->type = PP_HTONS(ETHTYPE_IP);
  104. iphdr->src.addr = 0x00000000; /* src: 0.0.0.0 */
  105. iphdr->dest.addr = 0xFFFFFFFF; /* src: 255.255.255.255 */
  106. IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
  107. IPH_TOS_SET(iphdr, 0x00);
  108. IPH_LEN_SET(iphdr, htons(IP_HLEN + sizeof(struct udp_hdr) + size));
  109. IPH_ID_SET(iphdr, htons(2));
  110. IPH_OFFSET_SET(iphdr, 0);
  111. IPH_TTL_SET(iphdr, 255);
  112. IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
  113. IPH_CHKSUM_SET(iphdr, 0);
  114. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  115. udphdr->src = htons(DHCP_SERVER_PORT);
  116. udphdr->dest = htons(DHCP_CLIENT_PORT);
  117. udphdr->len = htons(sizeof(struct udp_hdr) + size);
  118. udphdr->chksum = 0;
  119. memcpy((char *)udphdr + sizeof(struct udp_hdr),
  120. buffer, size);
  121. netif->linkoutput(netif, p);
  122. pbuf_free(p);
  123. return RT_EOK;
  124. }
  125. static uint8_t get_ip(struct mac_addr_t *p_mac_addr)
  126. {
  127. static uint8_t next_client_ip = DHCPD_CLIENT_IP_MIN;
  128. static struct mac_ip_item_t mac_table[MAC_TABLE_LEN];
  129. static int offset = 0;
  130. struct mac_addr_t bad_mac;
  131. int i;
  132. uint8_t ip_addr_3;
  133. rt_memset(&bad_mac, 0, sizeof(bad_mac));
  134. if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac)))
  135. {
  136. DEBUG_PRINTF("mac address all zero");
  137. ip_addr_3 = DHCPD_CLIENT_IP_MAX;
  138. goto _return;
  139. }
  140. rt_memset(&bad_mac, 0xFF, sizeof(bad_mac));
  141. if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac)))
  142. {
  143. DEBUG_PRINTF("mac address all one");
  144. ip_addr_3 = DHCPD_CLIENT_IP_MAX;
  145. goto _return;
  146. }
  147. for (i = 0; i < MAC_TABLE_LEN; i++)
  148. {
  149. if (!rt_memcmp(&mac_table[i].mac_addr, p_mac_addr, sizeof(bad_mac)))
  150. {
  151. //use old ip
  152. ip_addr_3 = mac_table[i].ip_addr_3;
  153. DEBUG_PRINTF("return old ip: %d\n", (int)ip_addr_3);
  154. goto _return;
  155. }
  156. }
  157. /* add new ip */
  158. mac_table[offset].mac_addr = *p_mac_addr;
  159. mac_table[offset].ip_addr_3 = next_client_ip;
  160. ip_addr_3 = mac_table[offset].ip_addr_3 ;
  161. offset++;
  162. if (offset >= MAC_TABLE_LEN)
  163. offset = 0;
  164. next_client_ip++;
  165. if (next_client_ip > DHCPD_CLIENT_IP_MAX)
  166. next_client_ip = DHCPD_CLIENT_IP_MIN;
  167. DEBUG_PRINTF("create new ip: %d\n", (int)ip_addr_3);
  168. DEBUG_PRINTF("next_client_ip %d\n", next_client_ip);
  169. _return:
  170. return ip_addr_3;
  171. }
  172. static void dhcpd_thread_entry(void *parameter)
  173. {
  174. struct netif *netif = RT_NULL;
  175. int sock;
  176. int bytes_read;
  177. char *recv_data;
  178. rt_uint32_t addr_len;
  179. struct sockaddr_in server_addr, client_addr;
  180. struct dhcp_msg *msg;
  181. int optval = 1;
  182. struct mac_addr_t mac_addr;
  183. /* get ethernet interface. */
  184. netif = (struct netif *) parameter;
  185. RT_ASSERT(netif != RT_NULL);
  186. /* our DHCP server information */
  187. DEBUG_PRINTF("DHCP server IP: %d.%d.%d.%d client IP: %d.%d.%d.%d-%d\n",
  188. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  189. DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3,
  190. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  191. DHCPD_SERVER_IPADDR2, DHCPD_CLIENT_IP_MIN, DHCPD_CLIENT_IP_MAX);
  192. /* allocate buffer for receive */
  193. recv_data = rt_malloc(BUFSZ);
  194. if (recv_data == RT_NULL)
  195. {
  196. /* No memory */
  197. DEBUG_PRINTF("Out of memory\n");
  198. return;
  199. }
  200. /* create a socket with UDP */
  201. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  202. {
  203. DEBUG_PRINTF("create socket failed, errno = %d\n", errno);
  204. rt_free(recv_data);
  205. return;
  206. }
  207. /* set to receive broadcast packet */
  208. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
  209. /* initialize server address */
  210. server_addr.sin_family = AF_INET;
  211. server_addr.sin_port = htons(DHCP_SERVER_PORT);
  212. server_addr.sin_addr.s_addr = INADDR_ANY;
  213. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  214. /* bind socket to the server address */
  215. if (bind(sock, (struct sockaddr *)&server_addr,
  216. sizeof(struct sockaddr)) == -1)
  217. {
  218. /* bind failed. */
  219. DEBUG_PRINTF("bind server address failed, errno=%d\n", errno);
  220. rt_free(recv_data);
  221. return;
  222. }
  223. addr_len = sizeof(struct sockaddr);
  224. DEBUG_PRINTF("DHCP server listen on port %d...\n", DHCP_SERVER_PORT);
  225. while (1)
  226. {
  227. bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
  228. (struct sockaddr *)&client_addr, &addr_len);
  229. if (bytes_read < DHCP_MSG_LEN)
  230. {
  231. DEBUG_PRINTF("packet too short, wait for next!\n");
  232. continue;
  233. }
  234. msg = (struct dhcp_msg *)recv_data;
  235. /* check message type to make sure we can handle it */
  236. if ((msg->op != DHCP_BOOTREQUEST) || (msg->cookie != PP_HTONL(DHCP_MAGIC_COOKIE)))
  237. {
  238. continue;
  239. }
  240. memcpy(mac_addr.add, msg->chaddr, MAC_ADDR_LEN);
  241. /* handler. */
  242. {
  243. uint8_t *dhcp_opt;
  244. uint8_t option;
  245. uint8_t length;
  246. uint8_t message_type = 0;
  247. uint8_t finished = 0;
  248. uint32_t request_ip = 0;
  249. uint8_t client_ip_3;
  250. client_ip_3 = get_ip(&mac_addr);
  251. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  252. while (finished == 0)
  253. {
  254. option = *dhcp_opt;
  255. length = *(dhcp_opt + 1);
  256. switch (option)
  257. {
  258. case DHCP_OPTION_REQUESTED_IP:
  259. request_ip = *(dhcp_opt + 2) << 24 | *(dhcp_opt + 3) << 16
  260. | *(dhcp_opt + 4) << 8 | *(dhcp_opt + 5);
  261. break;
  262. case DHCP_OPTION_END:
  263. finished = 1;
  264. break;
  265. case DHCP_OPTION_MESSAGE_TYPE:
  266. message_type = *(dhcp_opt + 2);
  267. break;
  268. default:
  269. break;
  270. } /* switch(option) */
  271. dhcp_opt += (2 + length);
  272. }
  273. /* reply. */
  274. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  275. /* check. */
  276. if (request_ip)
  277. {
  278. uint32_t client_ip = DHCPD_SERVER_IPADDR0 << 24 | DHCPD_SERVER_IPADDR1 << 16
  279. | DHCPD_SERVER_IPADDR2 << 8 | client_ip_3;
  280. DEBUG_PRINTF("message_type: %d, request_ip: %08X, client_ip: %08X.\n", message_type, request_ip, client_ip);
  281. if (request_ip != client_ip)
  282. {
  283. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  284. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  285. *dhcp_opt++ = DHCP_NAK;
  286. *dhcp_opt++ = DHCP_OPTION_END;
  287. DEBUG_PRINTF("requested IP invalid, reply DHCP_NAK\n");
  288. if (netif != RT_NULL)
  289. {
  290. int send_byte = (dhcp_opt - (uint8_t *)msg);
  291. _low_level_dhcp_send(netif, msg, send_byte);
  292. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  293. }
  294. continue;
  295. }
  296. }
  297. if (message_type == DHCP_DISCOVER)
  298. {
  299. DEBUG_PRINTF("request DHCP_DISCOVER\n");
  300. DEBUG_PRINTF("reply DHCP_OFFER\n");
  301. // DHCP_OPTION_MESSAGE_TYPE
  302. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  303. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  304. *dhcp_opt++ = DHCP_OFFER;
  305. // DHCP_OPTION_SERVER_ID
  306. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  307. *dhcp_opt++ = 4;
  308. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  309. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  310. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  311. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  312. // DHCP_OPTION_LEASE_TIME
  313. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  314. *dhcp_opt++ = 4;
  315. *dhcp_opt++ = 0x00;
  316. *dhcp_opt++ = 0x01;
  317. *dhcp_opt++ = 0x51;
  318. *dhcp_opt++ = 0x80;
  319. }
  320. else if (message_type == DHCP_REQUEST)
  321. {
  322. DEBUG_PRINTF("request DHCP_REQUEST\n");
  323. DEBUG_PRINTF("reply DHCP_ACK\n");
  324. // DHCP_OPTION_MESSAGE_TYPE
  325. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  326. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  327. *dhcp_opt++ = DHCP_ACK;
  328. // DHCP_OPTION_SERVER_ID
  329. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  330. *dhcp_opt++ = 4;
  331. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  332. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  333. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  334. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  335. // DHCP_OPTION_SUBNET_MASK
  336. *dhcp_opt++ = DHCP_OPTION_SUBNET_MASK;
  337. *dhcp_opt++ = 4;
  338. *dhcp_opt++ = 0xFF;
  339. *dhcp_opt++ = 0xFF;
  340. *dhcp_opt++ = 0xFF;
  341. *dhcp_opt++ = 0x00;
  342. #ifdef DHCPD_USING_ROUTER
  343. // DHCP_OPTION_ROUTER
  344. *dhcp_opt++ = DHCP_OPTION_ROUTER;
  345. *dhcp_opt++ = 4;
  346. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  347. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  348. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  349. *dhcp_opt++ = 1;
  350. #endif
  351. // DHCP_OPTION_DNS_SERVER, use the default DNS server address in lwIP
  352. *dhcp_opt++ = DHCP_OPTION_DNS_SERVER;
  353. *dhcp_opt++ = 4;
  354. *dhcp_opt++ = 208;
  355. *dhcp_opt++ = 67;
  356. *dhcp_opt++ = 222;
  357. *dhcp_opt++ = 222;
  358. // DHCP_OPTION_LEASE_TIME
  359. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  360. *dhcp_opt++ = 4;
  361. *dhcp_opt++ = 0x00;
  362. *dhcp_opt++ = 0x01;
  363. *dhcp_opt++ = 0x51;
  364. *dhcp_opt++ = 0x80;
  365. }
  366. else
  367. {
  368. DEBUG_PRINTF("un handle message:%d\n", message_type);
  369. }
  370. // append DHCP_OPTION_END
  371. *dhcp_opt++ = DHCP_OPTION_END;
  372. /* send reply. */
  373. if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST))
  374. {
  375. msg->op = DHCP_BOOTREPLY;
  376. IP4_ADDR(&msg->yiaddr,
  377. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  378. DHCPD_SERVER_IPADDR2, client_ip_3);
  379. client_addr.sin_addr.s_addr = INADDR_BROADCAST;
  380. if (netif != RT_NULL)
  381. {
  382. int send_byte = (dhcp_opt - (uint8_t *)msg);
  383. _low_level_dhcp_send(netif, msg, send_byte);
  384. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  385. }
  386. }
  387. } /* handler. */
  388. }
  389. }
  390. void dhcpd_start(const char *netif_name)
  391. {
  392. rt_thread_t thread;
  393. struct netif *netif = netif_list;
  394. if (strlen(netif_name) > sizeof(netif->name))
  395. {
  396. rt_kprintf("network interface name too long!\r\n");
  397. return;
  398. }
  399. while (netif != RT_NULL)
  400. {
  401. if (strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  402. break;
  403. netif = netif->next;
  404. if (netif == RT_NULL)
  405. {
  406. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  407. return;
  408. }
  409. }
  410. if (1)
  411. {
  412. extern void set_if(const char *netif_name, const char *ip_addr, const char *gw_addr, const char *nm_addr);
  413. char ip_str[4 * 4 + 1];
  414. dhcp_stop(netif);
  415. sprintf(ip_str, "%d.%d.%d.%d", DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1, DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3);
  416. set_if(netif_name, ip_str, "0.0.0.0", "255.255.255.0");
  417. netif_set_up(netif);
  418. }
  419. thread = rt_thread_create("dhcpd",
  420. dhcpd_thread_entry, netif,
  421. 1024,
  422. RT_THREAD_PRIORITY_MAX - 3,
  423. 2);
  424. if (thread != RT_NULL)
  425. {
  426. rt_thread_startup(thread);
  427. }
  428. }