dhcp_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. */
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <rtthread.h>
  32. #include <lwip/opt.h>
  33. #include <lwip/sockets.h>
  34. #include <lwip/inet_chksum.h>
  35. #include <netif/etharp.h>
  36. #include <netif/ethernetif.h>
  37. #include <lwip/ip.h>
  38. /* DHCP server option */
  39. /* allocated client ip range */
  40. #ifndef DHCPD_CLIENT_IP_MIN
  41. #define DHCPD_CLIENT_IP_MIN 2
  42. #endif
  43. #ifndef DHCPD_CLIENT_IP_MAX
  44. #define DHCPD_CLIENT_IP_MAX 254
  45. #endif
  46. /* the DHCP server address */
  47. #ifndef DHCPD_SERVER_IPADDR0
  48. #define DHCPD_SERVER_IPADDR0 192UL
  49. #define DHCPD_SERVER_IPADDR1 168UL
  50. #define DHCPD_SERVER_IPADDR2 169UL
  51. #define DHCPD_SERVER_IPADDR3 1UL
  52. #endif
  53. //#define DHCP_DEBUG_PRINTF
  54. #ifdef DHCP_DEBUG_PRINTF
  55. #define DEBUG_PRINTF rt_kprintf("[DHCP] "); rt_kprintf
  56. #else
  57. #define DEBUG_PRINTF(...)
  58. #endif /* DHCP_DEBUG_PRINTF */
  59. /* we need some routines in the DHCP of lwIP */
  60. #undef LWIP_DHCP
  61. #define LWIP_DHCP 1
  62. #include <lwip/dhcp.h>
  63. /* buffer size for receive DHCP packet */
  64. #define BUFSZ 1024
  65. static uint8_t next_client_ip = DHCPD_CLIENT_IP_MIN;
  66. static rt_err_t _low_level_dhcp_send(struct netif *netif,
  67. const void *buffer,
  68. rt_size_t size)
  69. {
  70. struct pbuf *p;
  71. struct eth_hdr *ethhdr;
  72. struct ip_hdr *iphdr;
  73. struct udp_hdr *udphdr;
  74. p = pbuf_alloc(PBUF_LINK,
  75. SIZEOF_ETH_HDR + sizeof(struct ip_hdr)
  76. + sizeof(struct udp_hdr) + size,
  77. PBUF_RAM);
  78. if (p == RT_NULL) return -RT_ENOMEM;
  79. ethhdr = (struct eth_hdr *)p->payload;
  80. iphdr = (struct ip_hdr *)((char *)ethhdr + SIZEOF_ETH_HDR);
  81. udphdr = (struct udp_hdr *)((char *)iphdr + sizeof(struct ip_hdr));
  82. ETHADDR32_COPY(&ethhdr->dest, (struct eth_addr *)&ethbroadcast);
  83. ETHADDR16_COPY(&ethhdr->src, netif->hwaddr);
  84. ethhdr->type = PP_HTONS(ETHTYPE_IP);
  85. iphdr->src.addr = 0x00000000; /* src: 0.0.0.0 */
  86. iphdr->dest.addr = 0xFFFFFFFF; /* src: 255.255.255.255 */
  87. IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
  88. IPH_TOS_SET(iphdr, 0x00);
  89. IPH_LEN_SET(iphdr, htons(IP_HLEN + sizeof(struct udp_hdr) + size));
  90. IPH_ID_SET(iphdr, htons(2));
  91. IPH_OFFSET_SET(iphdr, 0);
  92. IPH_TTL_SET(iphdr, 255);
  93. IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
  94. IPH_CHKSUM_SET(iphdr, 0);
  95. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
  96. udphdr->src = htons(DHCP_SERVER_PORT);
  97. udphdr->dest = htons(DHCP_CLIENT_PORT);
  98. udphdr->len = htons(sizeof(struct udp_hdr) + size);
  99. udphdr->chksum = 0;
  100. memcpy((char *)udphdr + sizeof(struct udp_hdr),
  101. buffer, size);
  102. return netif->linkoutput(netif, p);
  103. }
  104. static void dhcpd_thread_entry(void *parameter)
  105. {
  106. struct netif *netif = RT_NULL;
  107. int sock;
  108. int bytes_read;
  109. char *recv_data;
  110. rt_uint32_t addr_len;
  111. struct sockaddr_in server_addr, client_addr;
  112. struct dhcp_msg *msg;
  113. int optval = 1;
  114. /* get ethernet interface. */
  115. netif = (struct netif*) parameter;
  116. RT_ASSERT(netif != RT_NULL);
  117. /* our DHCP server information */
  118. DEBUG_PRINTF("DHCP server IP: %d.%d.%d.%d client IP: %d.%d.%d.%d-%d\n",
  119. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  120. DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3,
  121. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  122. DHCPD_SERVER_IPADDR2, DHCPD_CLIENT_IP_MIN, DHCPD_CLIENT_IP_MAX);
  123. /* allocate buffer for receive */
  124. recv_data = rt_malloc(BUFSZ);
  125. if (recv_data == RT_NULL)
  126. {
  127. /* No memory */
  128. DEBUG_PRINTF("Out of memory\n");
  129. return;
  130. }
  131. /* create a socket with UDP */
  132. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  133. {
  134. DEBUG_PRINTF("create socket failed, errno = %d\n", errno);
  135. rt_free(recv_data);
  136. return;
  137. }
  138. /* set to receive broadcast packet */
  139. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
  140. /* initialize server address */
  141. server_addr.sin_family = AF_INET;
  142. server_addr.sin_port = htons(DHCP_SERVER_PORT);
  143. server_addr.sin_addr.s_addr = INADDR_ANY;
  144. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  145. /* bind socket to the server address */
  146. if (bind(sock, (struct sockaddr *)&server_addr,
  147. sizeof(struct sockaddr)) == -1)
  148. {
  149. /* bind failed. */
  150. DEBUG_PRINTF("bind server address failed, errno=%d\n", errno);
  151. rt_free(recv_data);
  152. return;
  153. }
  154. addr_len = sizeof(struct sockaddr);
  155. DEBUG_PRINTF("DHCP server listen on port %d...\n", DHCP_SERVER_PORT);
  156. while (1)
  157. {
  158. bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
  159. (struct sockaddr *)&client_addr, &addr_len);
  160. if (bytes_read < DHCP_MSG_LEN)
  161. {
  162. DEBUG_PRINTF("packet too short, wait for next!\n");
  163. continue;
  164. }
  165. msg = (struct dhcp_msg *)recv_data;
  166. /* check message type to make sure we can handle it */
  167. if ((msg->op != DHCP_BOOTREQUEST) || (msg->cookie != PP_HTONL(DHCP_MAGIC_COOKIE)))
  168. {
  169. continue;
  170. }
  171. /* handler. */
  172. {
  173. uint8_t *dhcp_opt;
  174. uint8_t option;
  175. uint8_t length;
  176. uint8_t message_type = 0;
  177. uint8_t finished = 0;
  178. uint32_t request_ip = 0;
  179. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  180. while (finished == 0)
  181. {
  182. option = *dhcp_opt;
  183. length = *(dhcp_opt + 1);
  184. switch (option)
  185. {
  186. case DHCP_OPTION_REQUESTED_IP:
  187. request_ip = *(dhcp_opt + 2) << 24 | *(dhcp_opt + 3) << 16
  188. | *(dhcp_opt + 4) << 8 | *(dhcp_opt + 5);
  189. break;
  190. case DHCP_OPTION_END:
  191. finished = 1;
  192. break;
  193. case DHCP_OPTION_MESSAGE_TYPE:
  194. message_type = *(dhcp_opt + 2);
  195. break;
  196. default:
  197. break;
  198. } /* switch(option) */
  199. dhcp_opt += (2 + length);
  200. }
  201. /* reply. */
  202. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  203. /* check. */
  204. if (request_ip)
  205. {
  206. uint32_t client_ip = DHCPD_SERVER_IPADDR0 << 24 | DHCPD_SERVER_IPADDR1 << 16
  207. | DHCPD_SERVER_IPADDR2 << 8 | (next_client_ip);
  208. if (request_ip != client_ip)
  209. {
  210. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  211. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  212. *dhcp_opt++ = DHCP_NAK;
  213. *dhcp_opt++ = DHCP_OPTION_END;
  214. DEBUG_PRINTF("requested IP invalid, reply DHCP_NAK\n");
  215. if (netif != RT_NULL)
  216. {
  217. int send_byte = (dhcp_opt - (uint8_t *)msg);
  218. _low_level_dhcp_send(netif, msg, send_byte);
  219. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  220. }
  221. next_client_ip++;
  222. if (next_client_ip > DHCPD_CLIENT_IP_MAX)
  223. next_client_ip = DHCPD_CLIENT_IP_MIN;
  224. continue;
  225. }
  226. }
  227. if (message_type == DHCP_DISCOVER)
  228. {
  229. DEBUG_PRINTF("request DHCP_DISCOVER\n");
  230. DEBUG_PRINTF("reply DHCP_OFFER\n");
  231. // DHCP_OPTION_MESSAGE_TYPE
  232. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  233. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  234. *dhcp_opt++ = DHCP_OFFER;
  235. // DHCP_OPTION_SERVER_ID
  236. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  237. *dhcp_opt++ = 4;
  238. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  239. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  240. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  241. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  242. // DHCP_OPTION_LEASE_TIME
  243. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  244. *dhcp_opt++ = 4;
  245. *dhcp_opt++ = 0x00;
  246. *dhcp_opt++ = 0x01;
  247. *dhcp_opt++ = 0x51;
  248. *dhcp_opt++ = 0x80;
  249. }
  250. else if (message_type == DHCP_REQUEST)
  251. {
  252. DEBUG_PRINTF("request DHCP_REQUEST\n");
  253. DEBUG_PRINTF("reply DHCP_ACK\n");
  254. // DHCP_OPTION_MESSAGE_TYPE
  255. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  256. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  257. *dhcp_opt++ = DHCP_ACK;
  258. // DHCP_OPTION_SERVER_ID
  259. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  260. *dhcp_opt++ = 4;
  261. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  262. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  263. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  264. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  265. // DHCP_OPTION_SUBNET_MASK
  266. *dhcp_opt++ = DHCP_OPTION_SUBNET_MASK;
  267. *dhcp_opt++ = 4;
  268. *dhcp_opt++ = 0xFF;
  269. *dhcp_opt++ = 0xFF;
  270. *dhcp_opt++ = 0xFF;
  271. *dhcp_opt++ = 0x00;
  272. #ifdef DHCPD_USING_ROUTER
  273. // DHCP_OPTION_ROUTER
  274. *dhcp_opt++ = DHCP_OPTION_ROUTER;
  275. *dhcp_opt++ = 4;
  276. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  277. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  278. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  279. *dhcp_opt++ = 1;
  280. #endif
  281. // DHCP_OPTION_DNS_SERVER, use the default DNS server address in lwIP
  282. *dhcp_opt++ = DHCP_OPTION_DNS_SERVER;
  283. *dhcp_opt++ = 4;
  284. *dhcp_opt++ = 208;
  285. *dhcp_opt++ = 67;
  286. *dhcp_opt++ = 222;
  287. *dhcp_opt++ = 222;
  288. // DHCP_OPTION_LEASE_TIME
  289. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  290. *dhcp_opt++ = 4;
  291. *dhcp_opt++ = 0x00;
  292. *dhcp_opt++ = 0x01;
  293. *dhcp_opt++ = 0x51;
  294. *dhcp_opt++ = 0x80;
  295. }
  296. else
  297. {
  298. DEBUG_PRINTF("un handle message:%d\n", message_type);
  299. }
  300. // append DHCP_OPTION_END
  301. *dhcp_opt++ = DHCP_OPTION_END;
  302. /* send reply. */
  303. if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST))
  304. {
  305. msg->op = DHCP_BOOTREPLY;
  306. IP4_ADDR(&msg->yiaddr,
  307. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  308. DHCPD_SERVER_IPADDR2, next_client_ip);
  309. client_addr.sin_addr.s_addr = INADDR_BROADCAST;
  310. if (netif != RT_NULL)
  311. {
  312. int send_byte = (dhcp_opt - (uint8_t *)msg);
  313. _low_level_dhcp_send(netif, msg, send_byte);
  314. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  315. }
  316. }
  317. } /* handler. */
  318. }
  319. }
  320. void dhcpd_start(char* netif_name)
  321. {
  322. rt_thread_t thread;
  323. struct netif *netif = netif_list;
  324. if(strlen(netif_name) > sizeof(netif->name))
  325. {
  326. rt_kprintf("network interface name too long!\r\n");
  327. return;
  328. }
  329. while(netif != RT_NULL)
  330. {
  331. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  332. break;
  333. netif = netif->next;
  334. if( netif == RT_NULL )
  335. {
  336. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  337. return;
  338. }
  339. }
  340. thread = rt_thread_create("dhcpd",
  341. dhcpd_thread_entry, netif,
  342. 1024,
  343. RT_THREAD_PRIORITY_MAX - 3,
  344. 2);
  345. if (thread != RT_NULL)
  346. {
  347. rt_thread_startup(thread);
  348. }
  349. }