dhcp_server.c 13 KB

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