dhcp_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. netif->linkoutput(netif, p);
  107. pbuf_free(p);
  108. return RT_EOK;
  109. }
  110. static void dhcpd_thread_entry(void *parameter)
  111. {
  112. struct netif *netif = RT_NULL;
  113. int sock;
  114. int bytes_read;
  115. char *recv_data;
  116. rt_uint32_t addr_len;
  117. struct sockaddr_in server_addr, client_addr;
  118. struct dhcp_msg *msg;
  119. int optval = 1;
  120. /* get ethernet interface. */
  121. netif = (struct netif*) parameter;
  122. RT_ASSERT(netif != RT_NULL);
  123. /* our DHCP server information */
  124. DEBUG_PRINTF("DHCP server IP: %d.%d.%d.%d client IP: %d.%d.%d.%d-%d\n",
  125. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  126. DHCPD_SERVER_IPADDR2, DHCPD_SERVER_IPADDR3,
  127. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  128. DHCPD_SERVER_IPADDR2, DHCPD_CLIENT_IP_MIN, DHCPD_CLIENT_IP_MAX);
  129. /* allocate buffer for receive */
  130. recv_data = rt_malloc(BUFSZ);
  131. if (recv_data == RT_NULL)
  132. {
  133. /* No memory */
  134. DEBUG_PRINTF("Out of memory\n");
  135. return;
  136. }
  137. /* create a socket with UDP */
  138. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  139. {
  140. DEBUG_PRINTF("create socket failed, errno = %d\n", errno);
  141. rt_free(recv_data);
  142. return;
  143. }
  144. /* set to receive broadcast packet */
  145. setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
  146. /* initialize server address */
  147. server_addr.sin_family = AF_INET;
  148. server_addr.sin_port = htons(DHCP_SERVER_PORT);
  149. server_addr.sin_addr.s_addr = INADDR_ANY;
  150. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  151. /* bind socket to the server address */
  152. if (bind(sock, (struct sockaddr *)&server_addr,
  153. sizeof(struct sockaddr)) == -1)
  154. {
  155. /* bind failed. */
  156. DEBUG_PRINTF("bind server address failed, errno=%d\n", errno);
  157. rt_free(recv_data);
  158. return;
  159. }
  160. addr_len = sizeof(struct sockaddr);
  161. DEBUG_PRINTF("DHCP server listen on port %d...\n", DHCP_SERVER_PORT);
  162. while (1)
  163. {
  164. bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
  165. (struct sockaddr *)&client_addr, &addr_len);
  166. if (bytes_read < DHCP_MSG_LEN)
  167. {
  168. DEBUG_PRINTF("packet too short, wait for next!\n");
  169. continue;
  170. }
  171. msg = (struct dhcp_msg *)recv_data;
  172. /* check message type to make sure we can handle it */
  173. if ((msg->op != DHCP_BOOTREQUEST) || (msg->cookie != PP_HTONL(DHCP_MAGIC_COOKIE)))
  174. {
  175. continue;
  176. }
  177. /* handler. */
  178. {
  179. uint8_t *dhcp_opt;
  180. uint8_t option;
  181. uint8_t length;
  182. uint8_t message_type = 0;
  183. uint8_t finished = 0;
  184. uint32_t request_ip = 0;
  185. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  186. while (finished == 0)
  187. {
  188. option = *dhcp_opt;
  189. length = *(dhcp_opt + 1);
  190. switch (option)
  191. {
  192. case DHCP_OPTION_REQUESTED_IP:
  193. request_ip = *(dhcp_opt + 2) << 24 | *(dhcp_opt + 3) << 16
  194. | *(dhcp_opt + 4) << 8 | *(dhcp_opt + 5);
  195. break;
  196. case DHCP_OPTION_END:
  197. finished = 1;
  198. break;
  199. case DHCP_OPTION_MESSAGE_TYPE:
  200. message_type = *(dhcp_opt + 2);
  201. break;
  202. default:
  203. break;
  204. } /* switch(option) */
  205. dhcp_opt += (2 + length);
  206. }
  207. /* reply. */
  208. dhcp_opt = (uint8_t *)msg + DHCP_OPTIONS_OFS;
  209. /* check. */
  210. if (request_ip)
  211. {
  212. uint32_t client_ip = DHCPD_SERVER_IPADDR0 << 24 | DHCPD_SERVER_IPADDR1 << 16
  213. | DHCPD_SERVER_IPADDR2 << 8 | (next_client_ip);
  214. if (request_ip != client_ip)
  215. {
  216. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  217. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  218. *dhcp_opt++ = DHCP_NAK;
  219. *dhcp_opt++ = DHCP_OPTION_END;
  220. DEBUG_PRINTF("requested IP invalid, reply DHCP_NAK\n");
  221. if (netif != RT_NULL)
  222. {
  223. int send_byte = (dhcp_opt - (uint8_t *)msg);
  224. _low_level_dhcp_send(netif, msg, send_byte);
  225. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  226. }
  227. next_client_ip++;
  228. if (next_client_ip > DHCPD_CLIENT_IP_MAX)
  229. next_client_ip = DHCPD_CLIENT_IP_MIN;
  230. continue;
  231. }
  232. }
  233. if (message_type == DHCP_DISCOVER)
  234. {
  235. DEBUG_PRINTF("request DHCP_DISCOVER\n");
  236. DEBUG_PRINTF("reply DHCP_OFFER\n");
  237. // DHCP_OPTION_MESSAGE_TYPE
  238. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  239. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  240. *dhcp_opt++ = DHCP_OFFER;
  241. // DHCP_OPTION_SERVER_ID
  242. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  243. *dhcp_opt++ = 4;
  244. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  245. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  246. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  247. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  248. // DHCP_OPTION_LEASE_TIME
  249. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  250. *dhcp_opt++ = 4;
  251. *dhcp_opt++ = 0x00;
  252. *dhcp_opt++ = 0x01;
  253. *dhcp_opt++ = 0x51;
  254. *dhcp_opt++ = 0x80;
  255. }
  256. else if (message_type == DHCP_REQUEST)
  257. {
  258. DEBUG_PRINTF("request DHCP_REQUEST\n");
  259. DEBUG_PRINTF("reply DHCP_ACK\n");
  260. // DHCP_OPTION_MESSAGE_TYPE
  261. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE;
  262. *dhcp_opt++ = DHCP_OPTION_MESSAGE_TYPE_LEN;
  263. *dhcp_opt++ = DHCP_ACK;
  264. // DHCP_OPTION_SERVER_ID
  265. *dhcp_opt++ = DHCP_OPTION_SERVER_ID;
  266. *dhcp_opt++ = 4;
  267. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  268. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  269. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  270. *dhcp_opt++ = DHCPD_SERVER_IPADDR3;
  271. // DHCP_OPTION_SUBNET_MASK
  272. *dhcp_opt++ = DHCP_OPTION_SUBNET_MASK;
  273. *dhcp_opt++ = 4;
  274. *dhcp_opt++ = 0xFF;
  275. *dhcp_opt++ = 0xFF;
  276. *dhcp_opt++ = 0xFF;
  277. *dhcp_opt++ = 0x00;
  278. #ifdef DHCPD_USING_ROUTER
  279. // DHCP_OPTION_ROUTER
  280. *dhcp_opt++ = DHCP_OPTION_ROUTER;
  281. *dhcp_opt++ = 4;
  282. *dhcp_opt++ = DHCPD_SERVER_IPADDR0;
  283. *dhcp_opt++ = DHCPD_SERVER_IPADDR1;
  284. *dhcp_opt++ = DHCPD_SERVER_IPADDR2;
  285. *dhcp_opt++ = 1;
  286. #endif
  287. // DHCP_OPTION_DNS_SERVER, use the default DNS server address in lwIP
  288. *dhcp_opt++ = DHCP_OPTION_DNS_SERVER;
  289. *dhcp_opt++ = 4;
  290. *dhcp_opt++ = 208;
  291. *dhcp_opt++ = 67;
  292. *dhcp_opt++ = 222;
  293. *dhcp_opt++ = 222;
  294. // DHCP_OPTION_LEASE_TIME
  295. *dhcp_opt++ = DHCP_OPTION_LEASE_TIME;
  296. *dhcp_opt++ = 4;
  297. *dhcp_opt++ = 0x00;
  298. *dhcp_opt++ = 0x01;
  299. *dhcp_opt++ = 0x51;
  300. *dhcp_opt++ = 0x80;
  301. }
  302. else
  303. {
  304. DEBUG_PRINTF("un handle message:%d\n", message_type);
  305. }
  306. // append DHCP_OPTION_END
  307. *dhcp_opt++ = DHCP_OPTION_END;
  308. /* send reply. */
  309. if ((message_type == DHCP_DISCOVER) || (message_type == DHCP_REQUEST))
  310. {
  311. msg->op = DHCP_BOOTREPLY;
  312. IP4_ADDR(&msg->yiaddr,
  313. DHCPD_SERVER_IPADDR0, DHCPD_SERVER_IPADDR1,
  314. DHCPD_SERVER_IPADDR2, next_client_ip);
  315. client_addr.sin_addr.s_addr = INADDR_BROADCAST;
  316. if (netif != RT_NULL)
  317. {
  318. int send_byte = (dhcp_opt - (uint8_t *)msg);
  319. _low_level_dhcp_send(netif, msg, send_byte);
  320. DEBUG_PRINTF("DHCP server send %d byte\n", send_byte);
  321. }
  322. }
  323. } /* handler. */
  324. }
  325. }
  326. void dhcpd_start(char* netif_name)
  327. {
  328. rt_thread_t thread;
  329. struct netif *netif = netif_list;
  330. if(strlen(netif_name) > sizeof(netif->name))
  331. {
  332. rt_kprintf("network interface name too long!\r\n");
  333. return;
  334. }
  335. while(netif != RT_NULL)
  336. {
  337. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  338. break;
  339. netif = netif->next;
  340. if( netif == RT_NULL )
  341. {
  342. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  343. return;
  344. }
  345. }
  346. thread = rt_thread_create("dhcpd",
  347. dhcpd_thread_entry, netif,
  348. 1024,
  349. RT_THREAD_PRIORITY_MAX - 3,
  350. 2);
  351. if (thread != RT_NULL)
  352. {
  353. rt_thread_startup(thread);
  354. }
  355. }