dhserver.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 by Sergey Fetisov <fsenok@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "dhserver.h"
  25. /* DHCP message type */
  26. #define DHCP_DISCOVER 1
  27. #define DHCP_OFFER 2
  28. #define DHCP_REQUEST 3
  29. #define DHCP_DECLINE 4
  30. #define DHCP_ACK 5
  31. #define DHCP_NAK 6
  32. #define DHCP_RELEASE 7
  33. #define DHCP_INFORM 8
  34. /* DHCP options */
  35. enum DHCP_OPTIONS {
  36. DHCP_PAD = 0,
  37. DHCP_SUBNETMASK = 1,
  38. DHCP_ROUTER = 3,
  39. DHCP_DNSSERVER = 6,
  40. DHCP_HOSTNAME = 12,
  41. DHCP_DNSDOMAIN = 15,
  42. DHCP_MTU = 26,
  43. DHCP_BROADCAST = 28,
  44. DHCP_PERFORMROUTERDISC = 31,
  45. DHCP_STATICROUTE = 33,
  46. DHCP_NISDOMAIN = 40,
  47. DHCP_NISSERVER = 41,
  48. DHCP_NTPSERVER = 42,
  49. DHCP_VENDOR = 43,
  50. DHCP_IPADDRESS = 50,
  51. DHCP_LEASETIME = 51,
  52. DHCP_OPTIONSOVERLOADED = 52,
  53. DHCP_MESSAGETYPE = 53,
  54. DHCP_SERVERID = 54,
  55. DHCP_PARAMETERREQUESTLIST = 55,
  56. DHCP_MESSAGE = 56,
  57. DHCP_MAXMESSAGESIZE = 57,
  58. DHCP_RENEWALTIME = 58,
  59. DHCP_REBINDTIME = 59,
  60. DHCP_CLASSID = 60,
  61. DHCP_CLIENTID = 61,
  62. DHCP_USERCLASS = 77, /* RFC 3004 */
  63. DHCP_FQDN = 81,
  64. DHCP_DNSSEARCH = 119, /* RFC 3397 */
  65. DHCP_CSR = 121, /* RFC 3442 */
  66. DHCP_MSCSR = 249, /* MS code for RFC 3442 */
  67. DHCP_END = 255
  68. };
  69. typedef struct {
  70. uint8_t dp_op; /* packet opcode type */
  71. uint8_t dp_htype; /* hardware addr type */
  72. uint8_t dp_hlen; /* hardware addr length */
  73. uint8_t dp_hops; /* gateway hops */
  74. uint32_t dp_xid; /* transaction ID */
  75. uint16_t dp_secs; /* seconds since boot began */
  76. uint16_t dp_flags;
  77. uint8_t dp_ciaddr[4]; /* client IP address */
  78. uint8_t dp_yiaddr[4]; /* 'your' IP address */
  79. uint8_t dp_siaddr[4]; /* server IP address */
  80. uint8_t dp_giaddr[4]; /* gateway IP address */
  81. uint8_t dp_chaddr[16]; /* client hardware address */
  82. uint8_t dp_legacy[192];
  83. uint8_t dp_magic[4];
  84. uint8_t dp_options[275]; /* options area */
  85. } DHCP_TYPE;
  86. DHCP_TYPE dhcp_data;
  87. static struct udp_pcb *pcb = NULL;
  88. static dhcp_config_t *config = NULL;
  89. char magic_cookie[] = { 0x63, 0x82, 0x53, 0x63 };
  90. static dhcp_entry_t *entry_by_ip(uint32_t ip)
  91. {
  92. int i;
  93. for (i = 0; i < config->num_entry; i++)
  94. if (*(uint32_t *)config->entries[i].addr == ip)
  95. return &config->entries[i];
  96. return NULL;
  97. }
  98. static dhcp_entry_t *entry_by_mac(uint8_t *mac)
  99. {
  100. int i;
  101. for (i = 0; i < config->num_entry; i++)
  102. if (memcmp(config->entries[i].mac, mac, 6) == 0)
  103. return &config->entries[i];
  104. return NULL;
  105. }
  106. static inline bool is_vacant(dhcp_entry_t *entry)
  107. {
  108. return memcmp("\0\0\0\0\0", entry->mac, 6) == 0;
  109. }
  110. static dhcp_entry_t *vacant_address(void)
  111. {
  112. int i;
  113. for (i = 0; i < config->num_entry; i++)
  114. if (is_vacant(config->entries + i))
  115. return config->entries + i;
  116. return NULL;
  117. }
  118. static inline void free_entry(dhcp_entry_t *entry)
  119. {
  120. memset(entry->mac, 0, 6);
  121. }
  122. uint8_t *find_dhcp_option(uint8_t *attrs, int size, uint8_t attr)
  123. {
  124. int i = 0;
  125. while ((i + 1) < size) {
  126. int next = i + attrs[i + 1] + 2;
  127. if (next > size)
  128. return NULL;
  129. if (attrs[i] == attr)
  130. return attrs + i;
  131. i = next;
  132. }
  133. return NULL;
  134. }
  135. int fill_options(void *dest,
  136. uint8_t msg_type,
  137. const char *domain,
  138. uint32_t dns,
  139. int lease_time,
  140. uint32_t serverid,
  141. uint32_t router,
  142. uint32_t subnet)
  143. {
  144. uint8_t *ptr = (uint8_t *)dest;
  145. /* ACK message type */
  146. *ptr++ = 53;
  147. *ptr++ = 1;
  148. *ptr++ = msg_type;
  149. /* dhcp server identifier */
  150. *ptr++ = DHCP_SERVERID;
  151. *ptr++ = 4;
  152. *(uint32_t *)ptr = serverid;
  153. ptr += 4;
  154. /* lease time */
  155. *ptr++ = DHCP_LEASETIME;
  156. *ptr++ = 4;
  157. *ptr++ = (lease_time >> 24) & 0xFF;
  158. *ptr++ = (lease_time >> 16) & 0xFF;
  159. *ptr++ = (lease_time >> 8) & 0xFF;
  160. *ptr++ = (lease_time >> 0) & 0xFF;
  161. /* subnet mask */
  162. *ptr++ = DHCP_SUBNETMASK;
  163. *ptr++ = 4;
  164. *(uint32_t *)ptr = subnet;
  165. ptr += 4;
  166. /* router */
  167. if (router != 0) {
  168. *ptr++ = DHCP_ROUTER;
  169. *ptr++ = 4;
  170. *(uint32_t *)ptr = router;
  171. ptr += 4;
  172. }
  173. /* domain name */
  174. if (domain != NULL) {
  175. int len = strlen(domain);
  176. *ptr++ = DHCP_DNSDOMAIN;
  177. *ptr++ = len;
  178. memcpy(ptr, domain, len);
  179. ptr += len;
  180. }
  181. /* domain name server (DNS) */
  182. if (dns != 0) {
  183. *ptr++ = DHCP_DNSSERVER;
  184. *ptr++ = 4;
  185. *(uint32_t *)ptr = dns;
  186. ptr += 4;
  187. }
  188. /* end */
  189. *ptr++ = DHCP_END;
  190. return ptr - (uint8_t *)dest;
  191. }
  192. static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  193. {
  194. (void)arg;
  195. (void)addr;
  196. uint8_t *ptr;
  197. dhcp_entry_t *entry;
  198. struct pbuf *pp;
  199. uint32_t n = p->len;
  200. if (n > sizeof(dhcp_data))
  201. n = sizeof(dhcp_data);
  202. memcpy(&dhcp_data, p->payload, n);
  203. switch (dhcp_data.dp_options[2]) {
  204. case DHCP_DISCOVER:
  205. entry = entry_by_mac(dhcp_data.dp_chaddr);
  206. if (entry == NULL)
  207. entry = vacant_address();
  208. if (entry == NULL)
  209. break;
  210. dhcp_data.dp_op = 2; /* reply */
  211. dhcp_data.dp_secs = 0;
  212. dhcp_data.dp_flags = 0;
  213. *(uint32_t *)dhcp_data.dp_yiaddr = *(uint32_t *)entry->addr;
  214. memcpy(dhcp_data.dp_magic, magic_cookie, 4);
  215. memset(dhcp_data.dp_options, 0, sizeof(dhcp_data.dp_options));
  216. fill_options(dhcp_data.dp_options,
  217. DHCP_OFFER,
  218. config->domain,
  219. *(uint32_t *)config->dns,
  220. entry->lease,
  221. *(uint32_t *)config->addr,
  222. *(uint32_t *)config->addr,
  223. *(uint32_t *)entry->subnet);
  224. pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
  225. if (pp == NULL)
  226. break;
  227. memcpy(pp->payload, &dhcp_data, sizeof(dhcp_data));
  228. udp_sendto(upcb, pp, IP_ADDR_BROADCAST, port);
  229. pbuf_free(pp);
  230. break;
  231. case DHCP_REQUEST:
  232. /* 1. find requested ipaddr in option list */
  233. ptr = find_dhcp_option(dhcp_data.dp_options, sizeof(dhcp_data.dp_options), DHCP_IPADDRESS);
  234. if (ptr == NULL)
  235. break;
  236. if (ptr[1] != 4)
  237. break;
  238. ptr += 2;
  239. /* 2. does hw-address registered? */
  240. entry = entry_by_mac(dhcp_data.dp_chaddr);
  241. if (entry != NULL)
  242. free_entry(entry);
  243. /* 3. find requested ipaddr */
  244. entry = entry_by_ip(*(uint32_t *)ptr);
  245. if (entry == NULL)
  246. break;
  247. if (!is_vacant(entry))
  248. break;
  249. /* 4. fill struct fields */
  250. memcpy(dhcp_data.dp_yiaddr, ptr, 4);
  251. dhcp_data.dp_op = 2; /* reply */
  252. dhcp_data.dp_secs = 0;
  253. dhcp_data.dp_flags = 0;
  254. memcpy(dhcp_data.dp_magic, magic_cookie, 4);
  255. /* 5. fill options */
  256. memset(dhcp_data.dp_options, 0, sizeof(dhcp_data.dp_options));
  257. fill_options(dhcp_data.dp_options,
  258. DHCP_ACK,
  259. config->domain,
  260. *(uint32_t *)config->dns,
  261. entry->lease,
  262. *(uint32_t *)config->addr,
  263. *(uint32_t *)config->addr,
  264. *(uint32_t *)entry->subnet);
  265. /* 6. send ACK */
  266. pp = pbuf_alloc(PBUF_TRANSPORT, sizeof(dhcp_data), PBUF_POOL);
  267. if (pp == NULL)
  268. break;
  269. memcpy(entry->mac, dhcp_data.dp_chaddr, 6);
  270. memcpy(pp->payload, &dhcp_data, sizeof(dhcp_data));
  271. udp_sendto(upcb, pp, IP_ADDR_BROADCAST, port);
  272. pbuf_free(pp);
  273. break;
  274. default:
  275. break;
  276. }
  277. pbuf_free(p);
  278. }
  279. err_t dhserv_init(dhcp_config_t *c)
  280. {
  281. err_t err;
  282. udp_init();
  283. dhserv_free();
  284. pcb = udp_new();
  285. if (pcb == NULL)
  286. return ERR_MEM;
  287. err = udp_bind(pcb, IP_ADDR_ANY, c->port);
  288. if (err != ERR_OK) {
  289. dhserv_free();
  290. return err;
  291. }
  292. udp_recv(pcb, udp_recv_proc, NULL);
  293. config = c;
  294. return ERR_OK;
  295. }
  296. void dhserv_free(void)
  297. {
  298. if (pcb == NULL)
  299. return;
  300. udp_remove(pcb);
  301. pcb = NULL;
  302. }