dhcp.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /**
  2. * @file
  3. * Dynamic Host Configuration Protocol client
  4. *
  5. */
  6. /*
  7. *
  8. * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
  9. * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is a contribution to the lwIP TCP/IP stack.
  35. * The Swedish Institute of Computer Science and Adam Dunkels
  36. * are specifically granted permission to redistribute this
  37. * source code.
  38. *
  39. * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
  40. *
  41. * This is a DHCP client for the lwIP TCP/IP stack. It aims to conform
  42. * with RFC 2131 and RFC 2132.
  43. *
  44. * TODO:
  45. * - Proper parsing of DHCP messages exploiting file/sname field overloading.
  46. * - Add JavaDoc style documentation (API, internals).
  47. * - Support for interfaces other than Ethernet (SLIP, PPP, ...)
  48. *
  49. * Please coordinate changes and requests with Leon Woestenberg
  50. * <leon.woestenberg@gmx.net>
  51. *
  52. * Integration with your code:
  53. *
  54. * In lwip/dhcp.h
  55. * #define DHCP_COARSE_TIMER_SECS (recommended 60 which is a minute)
  56. * #define DHCP_FINE_TIMER_MSECS (recommended 500 which equals TCP coarse timer)
  57. *
  58. * Then have your application call dhcp_coarse_tmr() and
  59. * dhcp_fine_tmr() on the defined intervals.
  60. *
  61. * dhcp_start(struct netif *netif);
  62. * starts a DHCP client instance which configures the interface by
  63. * obtaining an IP address lease and maintaining it.
  64. *
  65. * Use dhcp_release(netif) to end the lease and use dhcp_stop(netif)
  66. * to remove the DHCP client.
  67. *
  68. */
  69. #include "lwip/opt.h"
  70. #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
  71. #include "lwip/stats.h"
  72. #include "lwip/mem.h"
  73. #include "lwip/udp.h"
  74. #include "lwip/ip_addr.h"
  75. #include "lwip/netif.h"
  76. #include "lwip/inet.h"
  77. #include "lwip/sys.h"
  78. #include "lwip/dhcp.h"
  79. #include "lwip/autoip.h"
  80. #include "lwip/dns.h"
  81. #include "netif/etharp.h"
  82. #include <string.h>
  83. /** Default for DHCP_GLOBAL_XID is 0xABCD0000
  84. * This can be changed by defining DHCP_GLOBAL_XID and DHCP_GLOBAL_XID_HEADER, e.g.
  85. * #define DHCP_GLOBAL_XID_HEADER "stdlib.h"
  86. * #define DHCP_GLOBAL_XID rand()
  87. */
  88. #ifdef DHCP_GLOBAL_XID_HEADER
  89. #include DHCP_GLOBAL_XID_HEADER /* include optional starting XID generation prototypes */
  90. #endif
  91. /** DHCP_OPTION_MAX_MSG_SIZE is set to the MTU
  92. * MTU is checked to be big enough in dhcp_start */
  93. #define DHCP_MAX_MSG_LEN(netif) (netif->mtu)
  94. #define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576
  95. /* DHCP client state machine functions */
  96. static void dhcp_handle_ack(struct netif *netif);
  97. static void dhcp_handle_nak(struct netif *netif);
  98. static void dhcp_handle_offer(struct netif *netif);
  99. static err_t dhcp_discover(struct netif *netif);
  100. static err_t dhcp_select(struct netif *netif);
  101. static void dhcp_check(struct netif *netif);
  102. static void dhcp_bind(struct netif *netif);
  103. #if DHCP_DOES_ARP_CHECK
  104. static err_t dhcp_decline(struct netif *netif);
  105. #endif /* DHCP_DOES_ARP_CHECK */
  106. static err_t dhcp_rebind(struct netif *netif);
  107. static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
  108. /* receive, unfold, parse and free incoming messages */
  109. static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port);
  110. static err_t dhcp_unfold_reply(struct dhcp *dhcp);
  111. static u8_t *dhcp_get_option_ptr(struct dhcp *dhcp, u8_t option_type);
  112. static u8_t dhcp_get_option_byte(u8_t *ptr);
  113. #if 0
  114. static u16_t dhcp_get_option_short(u8_t *ptr);
  115. #endif
  116. static u32_t dhcp_get_option_long(u8_t *ptr);
  117. static void dhcp_free_reply(struct dhcp *dhcp);
  118. /* set the DHCP timers */
  119. static void dhcp_timeout(struct netif *netif);
  120. static void dhcp_t1_timeout(struct netif *netif);
  121. static void dhcp_t2_timeout(struct netif *netif);
  122. /* build outgoing messages */
  123. /* create a DHCP request, fill in common headers */
  124. static err_t dhcp_create_request(struct netif *netif);
  125. /* free a DHCP request */
  126. static void dhcp_delete_request(struct netif *netif);
  127. /* add a DHCP option (type, then length in bytes) */
  128. static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len);
  129. /* add option values */
  130. static void dhcp_option_byte(struct dhcp *dhcp, u8_t value);
  131. static void dhcp_option_short(struct dhcp *dhcp, u16_t value);
  132. static void dhcp_option_long(struct dhcp *dhcp, u32_t value);
  133. /* always add the DHCP options trailer to end and pad */
  134. static void dhcp_option_trailer(struct dhcp *dhcp);
  135. /**
  136. * Back-off the DHCP client (because of a received NAK response).
  137. *
  138. * Back-off the DHCP client because of a received NAK. Receiving a
  139. * NAK means the client asked for something non-sensible, for
  140. * example when it tries to renew a lease obtained on another network.
  141. *
  142. * We clear any existing set IP address and restart DHCP negotiation
  143. * afresh (as per RFC2131 3.2.3).
  144. *
  145. * @param netif the netif under DHCP control
  146. */
  147. static void
  148. dhcp_handle_nak(struct netif *netif)
  149. {
  150. struct dhcp *dhcp = netif->dhcp;
  151. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
  152. (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
  153. /* Set the interface down since the address must no longer be used, as per RFC2131 */
  154. netif_set_down(netif);
  155. /* remove IP address from interface */
  156. netif_set_ipaddr(netif, IP_ADDR_ANY);
  157. netif_set_gw(netif, IP_ADDR_ANY);
  158. netif_set_netmask(netif, IP_ADDR_ANY);
  159. /* Change to a defined state */
  160. dhcp_set_state(dhcp, DHCP_BACKING_OFF);
  161. /* We can immediately restart discovery */
  162. dhcp_discover(netif);
  163. }
  164. /**
  165. * Checks if the offered IP address is already in use.
  166. *
  167. * It does so by sending an ARP request for the offered address and
  168. * entering CHECKING state. If no ARP reply is received within a small
  169. * interval, the address is assumed to be free for use by us.
  170. *
  171. * @param netif the netif under DHCP control
  172. */
  173. static void
  174. dhcp_check(struct netif *netif)
  175. {
  176. struct dhcp *dhcp = netif->dhcp;
  177. err_t result;
  178. u16_t msecs;
  179. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
  180. (s16_t)netif->name[1]));
  181. dhcp_set_state(dhcp, DHCP_CHECKING);
  182. /* create an ARP query for the offered IP address, expecting that no host
  183. responds, as the IP address should not be in use. */
  184. result = etharp_query(netif, &dhcp->offered_ip_addr, NULL);
  185. if (result != ERR_OK) {
  186. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_check: could not perform ARP query\n"));
  187. }
  188. dhcp->tries++;
  189. msecs = 500;
  190. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  191. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
  192. }
  193. /**
  194. * Remember the configuration offered by a DHCP server.
  195. *
  196. * @param netif the netif under DHCP control
  197. */
  198. static void
  199. dhcp_handle_offer(struct netif *netif)
  200. {
  201. struct dhcp *dhcp = netif->dhcp;
  202. /* obtain the server address */
  203. u8_t *option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_SERVER_ID);
  204. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n",
  205. (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
  206. if (option_ptr != NULL) {
  207. dhcp->server_ip_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
  208. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n", dhcp->server_ip_addr.addr));
  209. /* remember offered address */
  210. ip_addr_set(&dhcp->offered_ip_addr, (struct ip_addr *)&dhcp->msg_in->yiaddr);
  211. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));
  212. dhcp_select(netif);
  213. }
  214. }
  215. /**
  216. * Select a DHCP server offer out of all offers.
  217. *
  218. * Simply select the first offer received.
  219. *
  220. * @param netif the netif under DHCP control
  221. * @return lwIP specific error (see error.h)
  222. */
  223. static err_t
  224. dhcp_select(struct netif *netif)
  225. {
  226. struct dhcp *dhcp = netif->dhcp;
  227. err_t result;
  228. u16_t msecs;
  229. #if LWIP_NETIF_HOSTNAME
  230. const char *p;
  231. #endif /* LWIP_NETIF_HOSTNAME */
  232. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
  233. dhcp_set_state(dhcp, DHCP_REQUESTING);
  234. /* create and initialize the DHCP message header */
  235. result = dhcp_create_request(netif);
  236. if (result == ERR_OK) {
  237. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  238. dhcp_option_byte(dhcp, DHCP_REQUEST);
  239. dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
  240. dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
  241. /* MUST request the offered IP address */
  242. dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
  243. dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
  244. dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
  245. dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
  246. dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
  247. dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
  248. dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
  249. dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
  250. dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
  251. #if LWIP_NETIF_HOSTNAME
  252. p = (const char*)netif->hostname;
  253. if (p != NULL) {
  254. dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p));
  255. while (*p) {
  256. dhcp_option_byte(dhcp, *p++);
  257. }
  258. }
  259. #endif /* LWIP_NETIF_HOSTNAME */
  260. dhcp_option_trailer(dhcp);
  261. /* shrink the pbuf to the actual content length */
  262. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  263. /* TODO: we really should bind to a specific local interface here
  264. but we cannot specify an unconfigured netif as it is addressless */
  265. /* send broadcast to any DHCP server */
  266. udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
  267. /* reconnect to any (or to server here?!) */
  268. udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
  269. dhcp_delete_request(netif);
  270. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n"));
  271. } else {
  272. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_select: could not allocate DHCP request\n"));
  273. }
  274. dhcp->tries++;
  275. msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
  276. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  277. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
  278. return result;
  279. }
  280. /**
  281. * The DHCP timer that checks for lease renewal/rebind timeouts.
  282. *
  283. */
  284. void
  285. dhcp_coarse_tmr()
  286. {
  287. struct netif *netif = netif_list;
  288. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
  289. /* iterate through all network interfaces */
  290. while (netif != NULL) {
  291. /* only act on DHCP configured interfaces */
  292. if (netif->dhcp != NULL) {
  293. /* timer is active (non zero), and triggers (zeroes) now? */
  294. if (netif->dhcp->t2_timeout-- == 1) {
  295. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
  296. /* this clients' rebind timeout triggered */
  297. dhcp_t2_timeout(netif);
  298. /* timer is active (non zero), and triggers (zeroes) now */
  299. } else if (netif->dhcp->t1_timeout-- == 1) {
  300. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
  301. /* this clients' renewal timeout triggered */
  302. dhcp_t1_timeout(netif);
  303. }
  304. }
  305. /* proceed to next netif */
  306. netif = netif->next;
  307. }
  308. }
  309. /**
  310. * DHCP transaction timeout handling
  311. *
  312. * A DHCP server is expected to respond within a short period of time.
  313. * This timer checks whether an outstanding DHCP request is timed out.
  314. *
  315. */
  316. void
  317. dhcp_fine_tmr()
  318. {
  319. struct netif *netif = netif_list;
  320. /* loop through netif's */
  321. while (netif != NULL) {
  322. /* only act on DHCP configured interfaces */
  323. if (netif->dhcp != NULL) {
  324. /* timer is active (non zero), and is about to trigger now */
  325. if (netif->dhcp->request_timeout > 1) {
  326. netif->dhcp->request_timeout--;
  327. }
  328. else if (netif->dhcp->request_timeout == 1) {
  329. netif->dhcp->request_timeout--;
  330. /* { netif->dhcp->request_timeout == 0 } */
  331. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
  332. /* this clients' request timeout triggered */
  333. dhcp_timeout(netif);
  334. }
  335. }
  336. /* proceed to next network interface */
  337. netif = netif->next;
  338. }
  339. }
  340. /**
  341. * A DHCP negotiation transaction, or ARP request, has timed out.
  342. *
  343. * The timer that was started with the DHCP or ARP request has
  344. * timed out, indicating no response was received in time.
  345. *
  346. * @param netif the netif under DHCP control
  347. */
  348. static void
  349. dhcp_timeout(struct netif *netif)
  350. {
  351. struct dhcp *dhcp = netif->dhcp;
  352. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_timeout()\n"));
  353. /* back-off period has passed, or server selection timed out */
  354. if ((dhcp->state == DHCP_BACKING_OFF) || (dhcp->state == DHCP_SELECTING)) {
  355. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
  356. dhcp_discover(netif);
  357. /* receiving the requested lease timed out */
  358. } else if (dhcp->state == DHCP_REQUESTING) {
  359. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
  360. if (dhcp->tries <= 5) {
  361. dhcp_select(netif);
  362. } else {
  363. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
  364. dhcp_release(netif);
  365. dhcp_discover(netif);
  366. }
  367. /* received no ARP reply for the offered address (which is good) */
  368. } else if (dhcp->state == DHCP_CHECKING) {
  369. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
  370. if (dhcp->tries <= 1) {
  371. dhcp_check(netif);
  372. /* no ARP replies on the offered address,
  373. looks like the IP address is indeed free */
  374. } else {
  375. /* bind the interface to the offered address */
  376. dhcp_bind(netif);
  377. }
  378. }
  379. /* did not get response to renew request? */
  380. else if (dhcp->state == DHCP_RENEWING) {
  381. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RENEWING, DHCP request timed out\n"));
  382. /* just retry renewal */
  383. /* note that the rebind timer will eventually time-out if renew does not work */
  384. dhcp_renew(netif);
  385. /* did not get response to rebind request? */
  386. } else if (dhcp->state == DHCP_REBINDING) {
  387. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REBINDING, DHCP request timed out\n"));
  388. if (dhcp->tries <= 8) {
  389. dhcp_rebind(netif);
  390. } else {
  391. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RELEASING, DISCOVERING\n"));
  392. dhcp_release(netif);
  393. dhcp_discover(netif);
  394. }
  395. }
  396. }
  397. /**
  398. * The renewal period has timed out.
  399. *
  400. * @param netif the netif under DHCP control
  401. */
  402. static void
  403. dhcp_t1_timeout(struct netif *netif)
  404. {
  405. struct dhcp *dhcp = netif->dhcp;
  406. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
  407. if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING)) {
  408. /* just retry to renew - note that the rebind timer (t2) will
  409. * eventually time-out if renew tries fail. */
  410. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t1_timeout(): must renew\n"));
  411. dhcp_renew(netif);
  412. }
  413. }
  414. /**
  415. * The rebind period has timed out.
  416. *
  417. * @param netif the netif under DHCP control
  418. */
  419. static void
  420. dhcp_t2_timeout(struct netif *netif)
  421. {
  422. struct dhcp *dhcp = netif->dhcp;
  423. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
  424. if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING)) {
  425. /* just retry to rebind */
  426. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout(): must rebind\n"));
  427. dhcp_rebind(netif);
  428. }
  429. }
  430. /**
  431. * Handle a DHCP ACK packet
  432. *
  433. * @param netif the netif under DHCP control
  434. */
  435. static void
  436. dhcp_handle_ack(struct netif *netif)
  437. {
  438. struct dhcp *dhcp = netif->dhcp;
  439. u8_t *option_ptr;
  440. /* clear options we might not get from the ACK */
  441. dhcp->offered_sn_mask.addr = 0;
  442. dhcp->offered_gw_addr.addr = 0;
  443. dhcp->offered_bc_addr.addr = 0;
  444. /* lease time given? */
  445. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_LEASE_TIME);
  446. if (option_ptr != NULL) {
  447. /* remember offered lease time */
  448. dhcp->offered_t0_lease = dhcp_get_option_long(option_ptr + 2);
  449. }
  450. /* renewal period given? */
  451. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_T1);
  452. if (option_ptr != NULL) {
  453. /* remember given renewal period */
  454. dhcp->offered_t1_renew = dhcp_get_option_long(option_ptr + 2);
  455. } else {
  456. /* calculate safe periods for renewal */
  457. dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
  458. }
  459. /* renewal period given? */
  460. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_T2);
  461. if (option_ptr != NULL) {
  462. /* remember given rebind period */
  463. dhcp->offered_t2_rebind = dhcp_get_option_long(option_ptr + 2);
  464. } else {
  465. /* calculate safe periods for rebinding */
  466. dhcp->offered_t2_rebind = dhcp->offered_t0_lease;
  467. }
  468. /* (y)our internet address */
  469. ip_addr_set(&dhcp->offered_ip_addr, &dhcp->msg_in->yiaddr);
  470. /**
  471. * Patch #1308
  472. * TODO: we must check if the file field is not overloaded by DHCP options!
  473. */
  474. #if 0
  475. /* boot server address */
  476. ip_addr_set(&dhcp->offered_si_addr, &dhcp->msg_in->siaddr);
  477. /* boot file name */
  478. if (dhcp->msg_in->file[0]) {
  479. dhcp->boot_file_name = mem_malloc(strlen(dhcp->msg_in->file) + 1);
  480. strcpy(dhcp->boot_file_name, dhcp->msg_in->file);
  481. }
  482. #endif
  483. /* subnet mask */
  484. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_SUBNET_MASK);
  485. /* subnet mask given? */
  486. if (option_ptr != NULL) {
  487. dhcp->offered_sn_mask.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
  488. }
  489. /* gateway router */
  490. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_ROUTER);
  491. if (option_ptr != NULL) {
  492. dhcp->offered_gw_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
  493. }
  494. /* broadcast address */
  495. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_BROADCAST);
  496. if (option_ptr != NULL) {
  497. dhcp->offered_bc_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
  498. }
  499. /* DNS servers */
  500. option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_DNS_SERVER);
  501. if (option_ptr != NULL) {
  502. u8_t n;
  503. dhcp->dns_count = dhcp_get_option_byte(&option_ptr[1]) / (u32_t)sizeof(struct ip_addr);
  504. /* limit to at most DHCP_MAX_DNS DNS servers */
  505. if (dhcp->dns_count > DHCP_MAX_DNS)
  506. dhcp->dns_count = DHCP_MAX_DNS;
  507. for (n = 0; n < dhcp->dns_count; n++) {
  508. dhcp->offered_dns_addr[n].addr = htonl(dhcp_get_option_long(&option_ptr[2 + n * 4]));
  509. #if LWIP_DNS
  510. dns_setserver( n, (struct ip_addr *)(&(dhcp->offered_dns_addr[n].addr)));
  511. #endif /* LWIP_DNS */
  512. }
  513. #if LWIP_DNS
  514. dns_setserver( n, (struct ip_addr *)(&ip_addr_any));
  515. #endif /* LWIP_DNS */
  516. }
  517. }
  518. /**
  519. * Start DHCP negotiation for a network interface.
  520. *
  521. * If no DHCP client instance was attached to this interface,
  522. * a new client is created first. If a DHCP client instance
  523. * was already present, it restarts negotiation.
  524. *
  525. * @param netif The lwIP network interface
  526. * @return lwIP error code
  527. * - ERR_OK - No error
  528. * - ERR_MEM - Out of memory
  529. */
  530. err_t
  531. dhcp_start(struct netif *netif)
  532. {
  533. struct dhcp *dhcp;
  534. err_t result = ERR_OK;
  535. LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
  536. dhcp = netif->dhcp;
  537. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
  538. /* Remove the flag that says this netif is handled by DHCP,
  539. it is set when we succeeded starting. */
  540. netif->flags &= ~NETIF_FLAG_DHCP;
  541. /* check MTU of the netif */
  542. if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
  543. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
  544. return ERR_MEM;
  545. }
  546. /* no DHCP client attached yet? */
  547. if (dhcp == NULL) {
  548. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
  549. dhcp = mem_malloc(sizeof(struct dhcp));
  550. if (dhcp == NULL) {
  551. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
  552. return ERR_MEM;
  553. }
  554. /* store this dhcp client in the netif */
  555. netif->dhcp = dhcp;
  556. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp"));
  557. /* already has DHCP client attached */
  558. } else {
  559. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 3, ("dhcp_start(): restarting DHCP configuration\n"));
  560. if (dhcp->pcb != NULL) {
  561. udp_remove(dhcp->pcb);
  562. }
  563. if (dhcp->p != NULL) {
  564. pbuf_free(dhcp->p);
  565. }
  566. }
  567. /* clear data structure */
  568. memset(dhcp, 0, sizeof(struct dhcp));
  569. /* allocate UDP PCB */
  570. dhcp->pcb = udp_new();
  571. if (dhcp->pcb == NULL) {
  572. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not obtain pcb\n"));
  573. mem_free((void *)dhcp);
  574. netif->dhcp = dhcp = NULL;
  575. return ERR_MEM;
  576. }
  577. #if IP_SOF_BROADCAST
  578. dhcp->pcb->so_options|=SOF_BROADCAST;
  579. #endif /* IP_SOF_BROADCAST */
  580. /* set up local and remote port for the pcb */
  581. udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
  582. udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
  583. /* set up the recv callback and argument */
  584. udp_recv(dhcp->pcb, dhcp_recv, netif);
  585. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
  586. /* (re)start the DHCP negotiation */
  587. result = dhcp_discover(netif);
  588. if (result != ERR_OK) {
  589. /* free resources allocated above */
  590. dhcp_stop(netif);
  591. return ERR_MEM;
  592. }
  593. /* Set the flag that says this netif is handled by DHCP. */
  594. netif->flags |= NETIF_FLAG_DHCP;
  595. return result;
  596. }
  597. /**
  598. * Inform a DHCP server of our manual configuration.
  599. *
  600. * This informs DHCP servers of our fixed IP address configuration
  601. * by sending an INFORM message. It does not involve DHCP address
  602. * configuration, it is just here to be nice to the network.
  603. *
  604. * @param netif The lwIP network interface
  605. */
  606. void
  607. dhcp_inform(struct netif *netif)
  608. {
  609. struct dhcp *dhcp, *old_dhcp = netif->dhcp;
  610. err_t result = ERR_OK;
  611. dhcp = mem_malloc(sizeof(struct dhcp));
  612. if (dhcp == NULL) {
  613. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_inform(): could not allocate dhcp\n"));
  614. return;
  615. }
  616. netif->dhcp = dhcp;
  617. memset(dhcp, 0, sizeof(struct dhcp));
  618. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): allocated dhcp\n"));
  619. dhcp->pcb = udp_new();
  620. if (dhcp->pcb == NULL) {
  621. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_inform(): could not obtain pcb"));
  622. mem_free((void *)dhcp);
  623. return;
  624. }
  625. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): created new udp pcb\n"));
  626. /* create and initialize the DHCP message header */
  627. result = dhcp_create_request(netif);
  628. if (result == ERR_OK) {
  629. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  630. dhcp_option_byte(dhcp, DHCP_INFORM);
  631. dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
  632. dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
  633. dhcp_option_trailer(dhcp);
  634. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  635. #if IP_SOF_BROADCAST
  636. dhcp->pcb->so_options|=SOF_BROADCAST;
  637. #endif /* IP_SOF_BROADCAST */
  638. udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
  639. udp_connect(dhcp->pcb, IP_ADDR_BROADCAST, DHCP_SERVER_PORT);
  640. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
  641. udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
  642. udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
  643. dhcp_delete_request(netif);
  644. } else {
  645. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_inform: could not allocate DHCP request\n"));
  646. }
  647. if (dhcp->pcb != NULL) {
  648. udp_remove(dhcp->pcb);
  649. }
  650. dhcp->pcb = NULL;
  651. mem_free((void *)dhcp);
  652. netif->dhcp = old_dhcp;
  653. }
  654. #if DHCP_DOES_ARP_CHECK
  655. /**
  656. * Match an ARP reply with the offered IP address.
  657. *
  658. * @param netif the network interface on which the reply was received
  659. * @param addr The IP address we received a reply from
  660. */
  661. void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr)
  662. {
  663. LWIP_ERROR("netif != NULL", (netif != NULL), return;);
  664. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_arp_reply()\n"));
  665. /* is a DHCP client doing an ARP check? */
  666. if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_CHECKING)) {
  667. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n", addr->addr));
  668. /* did a host respond with the address we
  669. were offered by the DHCP server? */
  670. if (ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
  671. /* we will not accept the offered address */
  672. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 1, ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
  673. dhcp_decline(netif);
  674. }
  675. }
  676. }
  677. /**
  678. * Decline an offered lease.
  679. *
  680. * Tell the DHCP server we do not accept the offered address.
  681. * One reason to decline the lease is when we find out the address
  682. * is already in use by another host (through ARP).
  683. *
  684. * @param netif the netif under DHCP control
  685. */
  686. static err_t
  687. dhcp_decline(struct netif *netif)
  688. {
  689. struct dhcp *dhcp = netif->dhcp;
  690. err_t result = ERR_OK;
  691. u16_t msecs;
  692. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_decline()\n"));
  693. dhcp_set_state(dhcp, DHCP_BACKING_OFF);
  694. /* create and initialize the DHCP message header */
  695. result = dhcp_create_request(netif);
  696. if (result == ERR_OK) {
  697. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  698. dhcp_option_byte(dhcp, DHCP_DECLINE);
  699. dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
  700. dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
  701. dhcp_option_trailer(dhcp);
  702. /* resize pbuf to reflect true size of options */
  703. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  704. /* @todo: should we really connect here? we are performing sendto() */
  705. udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
  706. /* per section 4.4.4, broadcast DECLINE messages */
  707. udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
  708. dhcp_delete_request(netif);
  709. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
  710. } else {
  711. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_decline: could not allocate DHCP request\n"));
  712. }
  713. dhcp->tries++;
  714. msecs = 10*1000;
  715. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  716. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
  717. return result;
  718. }
  719. #endif
  720. /**
  721. * Start the DHCP process, discover a DHCP server.
  722. *
  723. * @param netif the netif under DHCP control
  724. */
  725. static err_t
  726. dhcp_discover(struct netif *netif)
  727. {
  728. struct dhcp *dhcp = netif->dhcp;
  729. err_t result = ERR_OK;
  730. u16_t msecs;
  731. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_discover()\n"));
  732. ip_addr_set(&dhcp->offered_ip_addr, IP_ADDR_ANY);
  733. dhcp_set_state(dhcp, DHCP_SELECTING);
  734. /* create and initialize the DHCP message header */
  735. result = dhcp_create_request(netif);
  736. if (result == ERR_OK) {
  737. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
  738. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  739. dhcp_option_byte(dhcp, DHCP_DISCOVER);
  740. dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
  741. dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
  742. dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
  743. dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
  744. dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
  745. dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
  746. dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
  747. dhcp_option_trailer(dhcp);
  748. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
  749. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  750. udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
  751. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
  752. udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
  753. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
  754. dhcp_delete_request(netif);
  755. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
  756. } else {
  757. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_discover: could not allocate DHCP request\n"));
  758. }
  759. dhcp->tries++;
  760. #if LWIP_DHCP_AUTOIP_COOP
  761. if(dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {
  762. dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
  763. autoip_start(netif);
  764. }
  765. #endif /* LWIP_DHCP_AUTOIP_COOP */
  766. msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
  767. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  768. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
  769. return result;
  770. }
  771. /**
  772. * Bind the interface to the offered IP address.
  773. *
  774. * @param netif network interface to bind to the offered address
  775. */
  776. static void
  777. dhcp_bind(struct netif *netif)
  778. {
  779. u32_t timeout;
  780. struct dhcp *dhcp;
  781. struct ip_addr sn_mask, gw_addr;
  782. LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
  783. dhcp = netif->dhcp;
  784. LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
  785. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
  786. /* temporary DHCP lease? */
  787. if (dhcp->offered_t1_renew != 0xffffffffUL) {
  788. /* set renewal period timer */
  789. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
  790. timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
  791. if(timeout > 0xffff) {
  792. timeout = 0xffff;
  793. }
  794. dhcp->t1_timeout = (u16_t)timeout;
  795. if (dhcp->t1_timeout == 0) {
  796. dhcp->t1_timeout = 1;
  797. }
  798. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
  799. }
  800. /* set renewal period timer */
  801. if (dhcp->offered_t2_rebind != 0xffffffffUL) {
  802. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
  803. timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
  804. if(timeout > 0xffff) {
  805. timeout = 0xffff;
  806. }
  807. dhcp->t2_timeout = (u16_t)timeout;
  808. if (dhcp->t2_timeout == 0) {
  809. dhcp->t2_timeout = 1;
  810. }
  811. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
  812. }
  813. /* copy offered network mask */
  814. ip_addr_set(&sn_mask, &dhcp->offered_sn_mask);
  815. /* subnet mask not given? */
  816. /* TODO: this is not a valid check. what if the network mask is 0? */
  817. if (sn_mask.addr == 0) {
  818. /* choose a safe subnet mask given the network class */
  819. u8_t first_octet = ip4_addr1(&sn_mask);
  820. if (first_octet <= 127) {
  821. sn_mask.addr = htonl(0xff000000);
  822. } else if (first_octet >= 192) {
  823. sn_mask.addr = htonl(0xffffff00);
  824. } else {
  825. sn_mask.addr = htonl(0xffff0000);
  826. }
  827. }
  828. ip_addr_set(&gw_addr, &dhcp->offered_gw_addr);
  829. /* gateway address not given? */
  830. if (gw_addr.addr == 0) {
  831. /* copy network address */
  832. gw_addr.addr = (dhcp->offered_ip_addr.addr & sn_mask.addr);
  833. /* use first host address on network as gateway */
  834. gw_addr.addr |= htonl(0x00000001);
  835. }
  836. #if LWIP_DHCP_AUTOIP_COOP
  837. if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
  838. autoip_stop(netif);
  839. dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
  840. }
  841. #endif /* LWIP_DHCP_AUTOIP_COOP */
  842. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));
  843. netif_set_ipaddr(netif, &dhcp->offered_ip_addr);
  844. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): SN: 0x%08"X32_F"\n", sn_mask.addr));
  845. netif_set_netmask(netif, &sn_mask);
  846. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): GW: 0x%08"X32_F"\n", gw_addr.addr));
  847. netif_set_gw(netif, &gw_addr);
  848. /* bring the interface up */
  849. netif_set_up(netif);
  850. /* netif is now bound to DHCP leased address */
  851. dhcp_set_state(dhcp, DHCP_BOUND);
  852. }
  853. /**
  854. * Renew an existing DHCP lease at the involved DHCP server.
  855. *
  856. * @param netif network interface which must renew its lease
  857. */
  858. err_t
  859. dhcp_renew(struct netif *netif)
  860. {
  861. struct dhcp *dhcp = netif->dhcp;
  862. err_t result;
  863. u16_t msecs;
  864. #if LWIP_NETIF_HOSTNAME
  865. const char *p;
  866. #endif /* LWIP_NETIF_HOSTNAME */
  867. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_renew()\n"));
  868. dhcp_set_state(dhcp, DHCP_RENEWING);
  869. /* create and initialize the DHCP message header */
  870. result = dhcp_create_request(netif);
  871. if (result == ERR_OK) {
  872. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  873. dhcp_option_byte(dhcp, DHCP_REQUEST);
  874. dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
  875. dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
  876. #if LWIP_NETIF_HOSTNAME
  877. p = (const char*)netif->hostname;
  878. if (p != NULL) {
  879. dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p));
  880. while (*p) {
  881. dhcp_option_byte(dhcp, *p++);
  882. }
  883. }
  884. #endif /* LWIP_NETIF_HOSTNAME */
  885. #if 0
  886. dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
  887. dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
  888. #endif
  889. #if 0
  890. dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
  891. dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
  892. #endif
  893. /* append DHCP message trailer */
  894. dhcp_option_trailer(dhcp);
  895. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  896. udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
  897. udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
  898. dhcp_delete_request(netif);
  899. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
  900. } else {
  901. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_renew: could not allocate DHCP request\n"));
  902. }
  903. dhcp->tries++;
  904. /* back-off on retries, but to a maximum of 20 seconds */
  905. msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
  906. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  907. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
  908. return result;
  909. }
  910. /**
  911. * Rebind with a DHCP server for an existing DHCP lease.
  912. *
  913. * @param netif network interface which must rebind with a DHCP server
  914. */
  915. static err_t
  916. dhcp_rebind(struct netif *netif)
  917. {
  918. struct dhcp *dhcp = netif->dhcp;
  919. err_t result;
  920. u16_t msecs;
  921. #if LWIP_NETIF_HOSTNAME
  922. const char *p;
  923. #endif /* LWIP_NETIF_HOSTNAME */
  924. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
  925. dhcp_set_state(dhcp, DHCP_REBINDING);
  926. /* create and initialize the DHCP message header */
  927. result = dhcp_create_request(netif);
  928. if (result == ERR_OK) {
  929. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  930. dhcp_option_byte(dhcp, DHCP_REQUEST);
  931. dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
  932. dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
  933. #if LWIP_NETIF_HOSTNAME
  934. p = (const char*)netif->hostname;
  935. if (p != NULL) {
  936. dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p));
  937. while (*p) {
  938. dhcp_option_byte(dhcp, *p++);
  939. }
  940. }
  941. #endif /* LWIP_NETIF_HOSTNAME */
  942. #if 0
  943. dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
  944. dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
  945. dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
  946. dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
  947. #endif
  948. dhcp_option_trailer(dhcp);
  949. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  950. /* broadcast to server */
  951. udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
  952. udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
  953. dhcp_delete_request(netif);
  954. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
  955. } else {
  956. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_rebind: could not allocate DHCP request\n"));
  957. }
  958. dhcp->tries++;
  959. msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
  960. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  961. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
  962. return result;
  963. }
  964. /**
  965. * Release a DHCP lease.
  966. *
  967. * @param netif network interface which must release its lease
  968. */
  969. err_t
  970. dhcp_release(struct netif *netif)
  971. {
  972. struct dhcp *dhcp = netif->dhcp;
  973. err_t result;
  974. u16_t msecs;
  975. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_release()\n"));
  976. /* idle DHCP client */
  977. dhcp_set_state(dhcp, DHCP_OFF);
  978. /* clean old DHCP offer */
  979. dhcp->server_ip_addr.addr = 0;
  980. dhcp->offered_ip_addr.addr = dhcp->offered_sn_mask.addr = 0;
  981. dhcp->offered_gw_addr.addr = dhcp->offered_bc_addr.addr = 0;
  982. dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
  983. dhcp->dns_count = 0;
  984. /* create and initialize the DHCP message header */
  985. result = dhcp_create_request(netif);
  986. if (result == ERR_OK) {
  987. dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
  988. dhcp_option_byte(dhcp, DHCP_RELEASE);
  989. dhcp_option_trailer(dhcp);
  990. pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
  991. udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
  992. udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
  993. dhcp_delete_request(netif);
  994. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n"));
  995. } else {
  996. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_release: could not allocate DHCP request\n"));
  997. }
  998. dhcp->tries++;
  999. msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
  1000. dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
  1001. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release(): set request timeout %"U16_F" msecs\n", msecs));
  1002. /* bring the interface down */
  1003. netif_set_down(netif);
  1004. /* remove IP address from interface */
  1005. netif_set_ipaddr(netif, IP_ADDR_ANY);
  1006. netif_set_gw(netif, IP_ADDR_ANY);
  1007. netif_set_netmask(netif, IP_ADDR_ANY);
  1008. /* TODO: netif_down(netif); */
  1009. return result;
  1010. }
  1011. /**
  1012. * Remove the DHCP client from the interface.
  1013. *
  1014. * @param netif The network interface to stop DHCP on
  1015. */
  1016. void
  1017. dhcp_stop(struct netif *netif)
  1018. {
  1019. struct dhcp *dhcp = netif->dhcp;
  1020. LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);
  1021. /* Remove the flag that says this netif is handled by DHCP. */
  1022. netif->flags &= ~NETIF_FLAG_DHCP;
  1023. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_stop()\n"));
  1024. /* netif is DHCP configured? */
  1025. if (dhcp != NULL) {
  1026. #if LWIP_DHCP_AUTOIP_COOP
  1027. if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
  1028. autoip_stop(netif);
  1029. dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
  1030. }
  1031. #endif /* LWIP_DHCP_AUTOIP_COOP */
  1032. if (dhcp->pcb != NULL) {
  1033. udp_remove(dhcp->pcb);
  1034. dhcp->pcb = NULL;
  1035. }
  1036. if (dhcp->p != NULL) {
  1037. pbuf_free(dhcp->p);
  1038. dhcp->p = NULL;
  1039. }
  1040. /* free unfolded reply */
  1041. dhcp_free_reply(dhcp);
  1042. mem_free((void *)dhcp);
  1043. netif->dhcp = NULL;
  1044. }
  1045. }
  1046. /*
  1047. * Set the DHCP state of a DHCP client.
  1048. *
  1049. * If the state changed, reset the number of tries.
  1050. *
  1051. * TODO: we might also want to reset the timeout here?
  1052. */
  1053. static void
  1054. dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
  1055. {
  1056. if (new_state != dhcp->state) {
  1057. dhcp->state = new_state;
  1058. dhcp->tries = 0;
  1059. }
  1060. }
  1061. /*
  1062. * Concatenate an option type and length field to the outgoing
  1063. * DHCP message.
  1064. *
  1065. */
  1066. static void
  1067. dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
  1068. {
  1069. LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
  1070. dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
  1071. dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
  1072. }
  1073. /*
  1074. * Concatenate a single byte to the outgoing DHCP message.
  1075. *
  1076. */
  1077. static void
  1078. dhcp_option_byte(struct dhcp *dhcp, u8_t value)
  1079. {
  1080. LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
  1081. dhcp->msg_out->options[dhcp->options_out_len++] = value;
  1082. }
  1083. static void
  1084. dhcp_option_short(struct dhcp *dhcp, u16_t value)
  1085. {
  1086. LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
  1087. dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
  1088. dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);
  1089. }
  1090. static void
  1091. dhcp_option_long(struct dhcp *dhcp, u32_t value)
  1092. {
  1093. LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
  1094. dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
  1095. dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
  1096. dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
  1097. dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));
  1098. }
  1099. /**
  1100. * Extract the DHCP message and the DHCP options.
  1101. *
  1102. * Extract the DHCP message and the DHCP options, each into a contiguous
  1103. * piece of memory. As a DHCP message is variable sized by its options,
  1104. * and also allows overriding some fields for options, the easy approach
  1105. * is to first unfold the options into a conitguous piece of memory, and
  1106. * use that further on.
  1107. *
  1108. */
  1109. static err_t
  1110. dhcp_unfold_reply(struct dhcp *dhcp)
  1111. {
  1112. u16_t ret;
  1113. LWIP_ERROR("dhcp != NULL", (dhcp != NULL), return ERR_ARG;);
  1114. LWIP_ERROR("dhcp->p != NULL", (dhcp->p != NULL), return ERR_VAL;);
  1115. /* free any left-overs from previous unfolds */
  1116. dhcp_free_reply(dhcp);
  1117. /* options present? */
  1118. if (dhcp->p->tot_len > (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN)) {
  1119. dhcp->options_in_len = dhcp->p->tot_len - (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
  1120. dhcp->options_in = mem_malloc(dhcp->options_in_len);
  1121. if (dhcp->options_in == NULL) {
  1122. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->options\n"));
  1123. return ERR_MEM;
  1124. }
  1125. }
  1126. dhcp->msg_in = mem_malloc(sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
  1127. if (dhcp->msg_in == NULL) {
  1128. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->msg_in\n"));
  1129. mem_free((void *)dhcp->options_in);
  1130. dhcp->options_in = NULL;
  1131. return ERR_MEM;
  1132. }
  1133. /** copy the DHCP message without options */
  1134. ret = pbuf_copy_partial(dhcp->p, dhcp->msg_in, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN, 0);
  1135. LWIP_ASSERT("ret == sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN", ret == sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
  1136. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes into dhcp->msg_in[]\n",
  1137. sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN));
  1138. if (dhcp->options_in != NULL) {
  1139. /** copy the DHCP options */
  1140. ret = pbuf_copy_partial(dhcp->p, dhcp->options_in, dhcp->options_in_len, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
  1141. LWIP_ASSERT("ret == dhcp->options_in_len", ret == dhcp->options_in_len);
  1142. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes to dhcp->options_in[]\n",
  1143. dhcp->options_in_len));
  1144. }
  1145. LWIP_UNUSED_ARG(ret);
  1146. return ERR_OK;
  1147. }
  1148. /**
  1149. * Free the incoming DHCP message including contiguous copy of
  1150. * its DHCP options.
  1151. *
  1152. */
  1153. static void dhcp_free_reply(struct dhcp *dhcp)
  1154. {
  1155. if (dhcp->msg_in != NULL) {
  1156. mem_free((void *)dhcp->msg_in);
  1157. dhcp->msg_in = NULL;
  1158. }
  1159. if (dhcp->options_in) {
  1160. mem_free((void *)dhcp->options_in);
  1161. dhcp->options_in = NULL;
  1162. dhcp->options_in_len = 0;
  1163. }
  1164. LWIP_DEBUGF(DHCP_DEBUG, ("dhcp_free_reply(): free'd\n"));
  1165. }
  1166. /**
  1167. * If an incoming DHCP message is in response to us, then trigger the state machine
  1168. */
  1169. static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
  1170. {
  1171. struct netif *netif = (struct netif *)arg;
  1172. struct dhcp *dhcp = netif->dhcp;
  1173. struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
  1174. u8_t *options_ptr;
  1175. u8_t msg_type;
  1176. u8_t i;
  1177. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void*)p,
  1178. (u16_t)(ntohl(addr->addr) >> 24 & 0xff), (u16_t)(ntohl(addr->addr) >> 16 & 0xff),
  1179. (u16_t)(ntohl(addr->addr) >> 8 & 0xff), (u16_t)(ntohl(addr->addr) & 0xff), port));
  1180. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
  1181. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
  1182. /* prevent warnings about unused arguments */
  1183. LWIP_UNUSED_ARG(pcb);
  1184. LWIP_UNUSED_ARG(addr);
  1185. LWIP_UNUSED_ARG(port);
  1186. dhcp->p = p;
  1187. /* TODO: check packet length before reading them */
  1188. if (reply_msg->op != DHCP_BOOTREPLY) {
  1189. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
  1190. goto free_pbuf_and_return;
  1191. }
  1192. /* iterate through hardware address and match against DHCP message */
  1193. for (i = 0; i < netif->hwaddr_len; i++) {
  1194. if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
  1195. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
  1196. (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
  1197. goto free_pbuf_and_return;
  1198. }
  1199. }
  1200. /* match transaction ID against what we expected */
  1201. if (ntohl(reply_msg->xid) != dhcp->xid) {
  1202. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",ntohl(reply_msg->xid),dhcp->xid));
  1203. goto free_pbuf_and_return;
  1204. }
  1205. /* option fields could be unfold? */
  1206. if (dhcp_unfold_reply(dhcp) != ERR_OK) {
  1207. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("problem unfolding DHCP message - too short on memory?\n"));
  1208. goto free_pbuf_and_return;
  1209. }
  1210. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
  1211. /* obtain pointer to DHCP message type */
  1212. options_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_MESSAGE_TYPE);
  1213. if (options_ptr == NULL) {
  1214. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
  1215. goto free_pbuf_and_return;
  1216. }
  1217. /* read DHCP message type */
  1218. msg_type = dhcp_get_option_byte(options_ptr + 2);
  1219. /* message type is DHCP ACK? */
  1220. if (msg_type == DHCP_ACK) {
  1221. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_ACK received\n"));
  1222. /* in requesting state? */
  1223. if (dhcp->state == DHCP_REQUESTING) {
  1224. dhcp_handle_ack(netif);
  1225. dhcp->request_timeout = 0;
  1226. #if DHCP_DOES_ARP_CHECK
  1227. /* check if the acknowledged lease address is already in use */
  1228. dhcp_check(netif);
  1229. #else
  1230. /* bind interface to the acknowledged lease address */
  1231. dhcp_bind(netif);
  1232. #endif
  1233. }
  1234. /* already bound to the given lease address? */
  1235. else if ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING)) {
  1236. dhcp->request_timeout = 0;
  1237. dhcp_bind(netif);
  1238. }
  1239. }
  1240. /* received a DHCP_NAK in appropriate state? */
  1241. else if ((msg_type == DHCP_NAK) &&
  1242. ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REQUESTING) ||
  1243. (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING ))) {
  1244. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_NAK received\n"));
  1245. dhcp->request_timeout = 0;
  1246. dhcp_handle_nak(netif);
  1247. }
  1248. /* received a DHCP_OFFER in DHCP_SELECTING state? */
  1249. else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_SELECTING)) {
  1250. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_OFFER received in DHCP_SELECTING state\n"));
  1251. dhcp->request_timeout = 0;
  1252. /* remember offered lease */
  1253. dhcp_handle_offer(netif);
  1254. }
  1255. free_pbuf_and_return:
  1256. dhcp_free_reply(dhcp);
  1257. pbuf_free(p);
  1258. dhcp->p = NULL;
  1259. }
  1260. /**
  1261. * Create a DHCP request, fill in common headers
  1262. *
  1263. * @param netif the netif under DHCP control
  1264. */
  1265. static err_t
  1266. dhcp_create_request(struct netif *netif)
  1267. {
  1268. struct dhcp *dhcp;
  1269. u16_t i;
  1270. #ifndef DHCP_GLOBAL_XID
  1271. /** default global transaction identifier starting value (easy to match
  1272. * with a packet analyser). We simply increment for each new request.
  1273. * Predefine DHCP_GLOBAL_XID to a better value or a function call to generate one
  1274. * at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */
  1275. static u32_t xid = 0xABCD0000;
  1276. #else
  1277. static u32_t xid;
  1278. static u8_t xid_initialised = 0;
  1279. if (!xid_initialised) {
  1280. xid = DHCP_GLOBAL_XID;
  1281. xid_initialised = !xid_initialised;
  1282. }
  1283. #endif
  1284. LWIP_ERROR("dhcp_create_request: netif != NULL", (netif != NULL), return ERR_ARG;);
  1285. dhcp = netif->dhcp;
  1286. LWIP_ERROR("dhcp_create_request: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
  1287. LWIP_ASSERT("dhcp_create_request: dhcp->p_out == NULL", dhcp->p_out == NULL);
  1288. LWIP_ASSERT("dhcp_create_request: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
  1289. dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
  1290. if (dhcp->p_out == NULL) {
  1291. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_create_request(): could not allocate pbuf\n"));
  1292. return ERR_MEM;
  1293. }
  1294. LWIP_ASSERT("dhcp_create_request: check that first pbuf can hold struct dhcp_msg",
  1295. (dhcp->p_out->len >= sizeof(struct dhcp_msg)));
  1296. /* reuse transaction identifier in retransmissions */
  1297. if (dhcp->tries==0)
  1298. xid++;
  1299. dhcp->xid = xid;
  1300. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2,
  1301. ("transaction id xid(%"X32_F")\n", xid));
  1302. dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
  1303. dhcp->msg_out->op = DHCP_BOOTREQUEST;
  1304. /* TODO: make link layer independent */
  1305. dhcp->msg_out->htype = DHCP_HTYPE_ETH;
  1306. /* TODO: make link layer independent */
  1307. dhcp->msg_out->hlen = DHCP_HLEN_ETH;
  1308. dhcp->msg_out->hops = 0;
  1309. dhcp->msg_out->xid = htonl(dhcp->xid);
  1310. dhcp->msg_out->secs = 0;
  1311. dhcp->msg_out->flags = 0;
  1312. dhcp->msg_out->ciaddr.addr = 0;
  1313. if (dhcp->state==DHCP_BOUND || dhcp->state==DHCP_RENEWING || dhcp->state==DHCP_REBINDING) {
  1314. dhcp->msg_out->ciaddr.addr = netif->ip_addr.addr;
  1315. }
  1316. dhcp->msg_out->yiaddr.addr = 0;
  1317. dhcp->msg_out->siaddr.addr = 0;
  1318. dhcp->msg_out->giaddr.addr = 0;
  1319. for (i = 0; i < DHCP_CHADDR_LEN; i++) {
  1320. /* copy netif hardware address, pad with zeroes */
  1321. dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len) ? netif->hwaddr[i] : 0/* pad byte*/;
  1322. }
  1323. for (i = 0; i < DHCP_SNAME_LEN; i++) {
  1324. dhcp->msg_out->sname[i] = 0;
  1325. }
  1326. for (i = 0; i < DHCP_FILE_LEN; i++) {
  1327. dhcp->msg_out->file[i] = 0;
  1328. }
  1329. dhcp->msg_out->cookie = htonl(0x63825363UL);
  1330. dhcp->options_out_len = 0;
  1331. /* fill options field with an incrementing array (for debugging purposes) */
  1332. for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
  1333. dhcp->msg_out->options[i] = (u8_t)i; /* for debugging only, no matter if truncated */
  1334. }
  1335. return ERR_OK;
  1336. }
  1337. /**
  1338. * Free previously allocated memory used to send a DHCP request.
  1339. *
  1340. * @param netif the netif under DHCP control
  1341. */
  1342. static void
  1343. dhcp_delete_request(struct netif *netif)
  1344. {
  1345. struct dhcp *dhcp;
  1346. LWIP_ERROR("dhcp_delete_request: netif != NULL", (netif != NULL), return;);
  1347. dhcp = netif->dhcp;
  1348. LWIP_ERROR("dhcp_delete_request: dhcp != NULL", (dhcp != NULL), return;);
  1349. LWIP_ASSERT("dhcp_delete_request: dhcp->p_out != NULL", dhcp->p_out != NULL);
  1350. LWIP_ASSERT("dhcp_delete_request: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
  1351. if (dhcp->p_out != NULL) {
  1352. pbuf_free(dhcp->p_out);
  1353. }
  1354. dhcp->p_out = NULL;
  1355. dhcp->msg_out = NULL;
  1356. }
  1357. /**
  1358. * Add a DHCP message trailer
  1359. *
  1360. * Adds the END option to the DHCP message, and if
  1361. * necessary, up to three padding bytes.
  1362. *
  1363. * @param dhcp DHCP state structure
  1364. */
  1365. static void
  1366. dhcp_option_trailer(struct dhcp *dhcp)
  1367. {
  1368. LWIP_ERROR("dhcp_option_trailer: dhcp != NULL", (dhcp != NULL), return;);
  1369. LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
  1370. LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
  1371. dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
  1372. /* packet is too small, or not 4 byte aligned? */
  1373. while ((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) {
  1374. /* LWIP_DEBUGF(DHCP_DEBUG,("dhcp_option_trailer:dhcp->options_out_len=%"U16_F", DHCP_OPTIONS_LEN=%"U16_F, dhcp->options_out_len, DHCP_OPTIONS_LEN)); */
  1375. LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
  1376. /* add a fill/padding byte */
  1377. dhcp->msg_out->options[dhcp->options_out_len++] = 0;
  1378. }
  1379. }
  1380. /**
  1381. * Find the offset of a DHCP option inside the DHCP message.
  1382. *
  1383. * @param dhcp DHCP client
  1384. * @param option_type
  1385. *
  1386. * @return a byte offset into the UDP message where the option was found, or
  1387. * zero if the given option was not found.
  1388. */
  1389. static u8_t *dhcp_get_option_ptr(struct dhcp *dhcp, u8_t option_type)
  1390. {
  1391. u8_t overload = DHCP_OVERLOAD_NONE;
  1392. /* options available? */
  1393. if ((dhcp->options_in != NULL) && (dhcp->options_in_len > 0)) {
  1394. /* start with options field */
  1395. u8_t *options = (u8_t *)dhcp->options_in;
  1396. u16_t offset = 0;
  1397. /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
  1398. while ((offset < dhcp->options_in_len) && (options[offset] != DHCP_OPTION_END)) {
  1399. /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
  1400. /* are the sname and/or file field overloaded with options? */
  1401. if (options[offset] == DHCP_OPTION_OVERLOAD) {
  1402. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("overloaded message detected\n"));
  1403. /* skip option type and length */
  1404. offset += 2;
  1405. overload = options[offset++];
  1406. }
  1407. /* requested option found */
  1408. else if (options[offset] == option_type) {
  1409. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("option found at offset %"U16_F" in options\n", offset));
  1410. return &options[offset];
  1411. /* skip option */
  1412. } else {
  1413. LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", options[offset]));
  1414. /* skip option type */
  1415. offset++;
  1416. /* skip option length, and then length bytes */
  1417. offset += 1 + options[offset];
  1418. }
  1419. }
  1420. /* is this an overloaded message? */
  1421. if (overload != DHCP_OVERLOAD_NONE) {
  1422. u16_t field_len;
  1423. if (overload == DHCP_OVERLOAD_FILE) {
  1424. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("overloaded file field\n"));
  1425. options = (u8_t *)&dhcp->msg_in->file;
  1426. field_len = DHCP_FILE_LEN;
  1427. } else if (overload == DHCP_OVERLOAD_SNAME) {
  1428. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("overloaded sname field\n"));
  1429. options = (u8_t *)&dhcp->msg_in->sname;
  1430. field_len = DHCP_SNAME_LEN;
  1431. /* TODO: check if else if () is necessary */
  1432. } else {
  1433. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("overloaded sname and file field\n"));
  1434. options = (u8_t *)&dhcp->msg_in->sname;
  1435. field_len = DHCP_FILE_LEN + DHCP_SNAME_LEN;
  1436. }
  1437. offset = 0;
  1438. /* at least 1 byte to read and no end marker */
  1439. while ((offset < field_len) && (options[offset] != DHCP_OPTION_END)) {
  1440. if (options[offset] == option_type) {
  1441. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("option found at offset=%"U16_F"\n", offset));
  1442. return &options[offset];
  1443. /* skip option */
  1444. } else {
  1445. LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("skipping option %"U16_F"\n", options[offset]));
  1446. /* skip option type */
  1447. offset++;
  1448. offset += 1 + options[offset];
  1449. }
  1450. }
  1451. }
  1452. }
  1453. return NULL;
  1454. }
  1455. /**
  1456. * Return the byte of DHCP option data.
  1457. *
  1458. * @param client DHCP client.
  1459. * @param ptr pointer obtained by dhcp_get_option_ptr().
  1460. *
  1461. * @return byte value at the given address.
  1462. */
  1463. static u8_t
  1464. dhcp_get_option_byte(u8_t *ptr)
  1465. {
  1466. LWIP_DEBUGF(DHCP_DEBUG, ("option byte value=%"U16_F"\n", (u16_t)(*ptr)));
  1467. return *ptr;
  1468. }
  1469. #if 0 /* currently unused */
  1470. /**
  1471. * Return the 16-bit value of DHCP option data.
  1472. *
  1473. * @param client DHCP client.
  1474. * @param ptr pointer obtained by dhcp_get_option_ptr().
  1475. *
  1476. * @return byte value at the given address.
  1477. */
  1478. static u16_t
  1479. dhcp_get_option_short(u8_t *ptr)
  1480. {
  1481. u16_t value;
  1482. value = *ptr++ << 8;
  1483. value |= *ptr;
  1484. LWIP_DEBUGF(DHCP_DEBUG, ("option short value=%"U16_F"\n", value));
  1485. return value;
  1486. }
  1487. #endif
  1488. /**
  1489. * Return the 32-bit value of DHCP option data.
  1490. *
  1491. * @param client DHCP client.
  1492. * @param ptr pointer obtained by dhcp_get_option_ptr().
  1493. *
  1494. * @return byte value at the given address.
  1495. */
  1496. static u32_t dhcp_get_option_long(u8_t *ptr)
  1497. {
  1498. u32_t value;
  1499. value = (u32_t)(*ptr++) << 24;
  1500. value |= (u32_t)(*ptr++) << 16;
  1501. value |= (u32_t)(*ptr++) << 8;
  1502. value |= (u32_t)(*ptr++);
  1503. LWIP_DEBUGF(DHCP_DEBUG, ("option long value=%"U32_F"\n", value));
  1504. return value;
  1505. }
  1506. #endif /* LWIP_DHCP */