• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Dynamic Host Configuration Protocol client
4  *
5  * @defgroup dhcp4 DHCPv4
6  * @ingroup ip4
7  * DHCP (IPv4) related functions
8  * This is a DHCP client for the lwIP TCP/IP stack. It aims to conform
9  * with RFC 2131 and RFC 2132.
10  *
11  * @todo:
12  * - Support for interfaces other than Ethernet (SLIP, PPP, ...)
13  *
14  * Options:
15  * @ref DHCP_COARSE_TIMER_SECS (recommended 60 which is a minute)
16  * @ref DHCP_FINE_TIMER_MSECS (recommended 500 which equals TCP coarse timer)
17  *
18  * dhcp_start() starts a DHCP client instance which
19  * configures the interface by obtaining an IP address lease and maintaining it.
20  *
21  * Use dhcp_release() to end the lease and use dhcp_stop()
22  * to remove the DHCP client.
23  *
24  * @see LWIP_HOOK_DHCP_APPEND_OPTIONS
25  * @see LWIP_HOOK_DHCP_PARSE_OPTION
26  *
27  * @see netifapi_dhcp4
28  */
29 
30 /*
31  * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
32  * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without modification,
36  * are permitted provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright notice,
39  *    this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright notice,
41  *    this list of conditions and the following disclaimer in the documentation
42  *    and/or other materials provided with the distribution.
43  * 3. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
47  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
49  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
51  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
54  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
55  * OF SUCH DAMAGE.
56  *
57  * This file is part of the lwIP TCP/IP stack.
58  * The Swedish Institute of Computer Science and Adam Dunkels
59  * are specifically granted permission to redistribute this
60  * source code.
61  *
62  * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
63  *
64  */
65 
66 #include "lwip/opt.h"
67 
68 #if LWIP_IPV4 && LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
69 
70 #include "lwip/stats.h"
71 #include "lwip/mem.h"
72 #include "lwip/udp.h"
73 #include "lwip/ip_addr.h"
74 #include "lwip/netif.h"
75 #include "lwip/def.h"
76 #include "lwip/dhcp.h"
77 #include "lwip/autoip.h"
78 #include "lwip/acd.h"
79 #include "lwip/dns.h"
80 #include "lwip/etharp.h"
81 #include "lwip/prot/dhcp.h"
82 #include "lwip/prot/iana.h"
83 
84 #include <string.h>
85 
86 #ifdef LWIP_HOOK_FILENAME
87 #include LWIP_HOOK_FILENAME
88 #endif
89 #ifndef LWIP_HOOK_DHCP_APPEND_OPTIONS
90 #define LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, state, msg, msg_type, options_len_ptr)
91 #endif
92 #ifndef LWIP_HOOK_DHCP_PARSE_OPTION
93 #define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, offset) do { LWIP_UNUSED_ARG(msg); } while(0)
94 #endif
95 
96 /** DHCP_ADD_EXTRA_REQUEST_OPTIONS: Additional options added to the list of options
97  * that the client requests from the servers (opt 55: DHCP_OPTION_PARAMETER_REQUEST_LIST)
98  * If additional options are requested, define this macro as a comma separated list, with leading comma.
99  * This macro is useful for example when requested vendor specific ids (VCI/VSI options), here is an example
100  * of requesting the VSI option (option 43) (yes, the notation is a bit strange, but it works :)
101  * (NOTE: the space between # and define is required because of doxygen...)
102  * # define DHCP_ADD_EXTRA_REQUEST_OPTIONS ,43
103  */
104 #ifndef DHCP_ADD_EXTRA_REQUEST_OPTIONS
105 #define DHCP_ADD_EXTRA_REQUEST_OPTIONS
106 #endif
107 
108 /** DHCP_DEFINE_CUSTOM_TIMEOUTS: if this is defined then you can customize various DHCP timeouts using these macros:
109       - DHCP_SET_TIMEOUT_FROM_OFFERED_T0_LEASE() to adjust the t0 lease timeout from the offered value
110       - DHCP_SET_TIMEOUT_FROM_OFFERED_T1_RENEW() same for t1 renew
111       - DHCP_SET_TIMEOUT_FROM_OFFERED_T2_REBIND() same for t2 rebind
112       - DHCP_NEXT_TIMEOUT_THRESHOLD to adjust the period of the next timeout
113       - DHCP_REQUEST_BACKOFF_SEQUENCE to adjust back-off times based on DHCP request attempts
114  */
115 #ifndef DHCP_DEFINE_CUSTOM_TIMEOUTS
116 #define SET_TIMEOUT_FROM_OFFERED(result, offered, min, max) do { \
117   u32_t timeout = (offered + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;  \
118   if (timeout > max) {    \
119     timeout = max;        \
120   }                       \
121   if (timeout == min) {   \
122     timeout = 1;          \
123   } \
124   result = (dhcp_timeout_t)timeout; \
125 } while(0)
126 
127 #define DHCP_SET_TIMEOUT_FROM_OFFERED_T0_LEASE(res, dhcp)  SET_TIMEOUT_FROM_OFFERED(res, (dhcp)->offered_t0_lease, 0, 0xffff)
128 #define DHCP_SET_TIMEOUT_FROM_OFFERED_T1_RENEW(res, dhcp)  SET_TIMEOUT_FROM_OFFERED(res, (dhcp)->offered_t1_renew, 0, 0xffff)
129 #define DHCP_SET_TIMEOUT_FROM_OFFERED_T2_REBIND(res, dhcp) SET_TIMEOUT_FROM_OFFERED(res, (dhcp)->offered_t2_rebind, 0, 0xffff)
130 
131 #define DHCP_NEXT_TIMEOUT_THRESHOLD ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)
132 #define DHCP_REQUEST_BACKOFF_SEQUENCE(tries)   (u16_t)(( (tries) < 6 ? 1 << (tries) : 60) * 1000)
133 
134 #endif /* DHCP_DEFINE_CUSTOM_TIMEOUTS */
135 
136 /** DHCP_CREATE_RAND_XID: if this is set to 1, the xid is created using
137  * LWIP_RAND() (this overrides DHCP_GLOBAL_XID)
138  */
139 #ifndef DHCP_CREATE_RAND_XID
140 #define DHCP_CREATE_RAND_XID        1
141 #endif
142 
143 /** Default for DHCP_GLOBAL_XID is 0xABCD0000
144  * This can be changed by defining DHCP_GLOBAL_XID and DHCP_GLOBAL_XID_HEADER, e.g.
145  *  \#define DHCP_GLOBAL_XID_HEADER "stdlib.h"
146  *  \#define DHCP_GLOBAL_XID rand()
147  */
148 #ifdef DHCP_GLOBAL_XID_HEADER
149 #include DHCP_GLOBAL_XID_HEADER /* include optional starting XID generation prototypes */
150 #endif
151 
152 /** DHCP_OPTION_MAX_MSG_SIZE is set to the MTU
153  * MTU is checked to be big enough in dhcp_start */
154 #define DHCP_MAX_MSG_LEN(netif)        (netif->mtu)
155 #define DHCP_MAX_MSG_LEN_MIN_REQUIRED  576
156 /** Minimum length for reply before packet is parsed */
157 #define DHCP_MIN_REPLY_LEN             44
158 
159 #define REBOOT_TRIES                2
160 #if 0 /* The following codes are moved to dhcp.h for fixing it's todo, kept here just for notice */
161 #if LWIP_DNS && LWIP_DHCP_MAX_DNS_SERVERS
162 #if DNS_MAX_SERVERS > LWIP_DHCP_MAX_DNS_SERVERS
163 #define LWIP_DHCP_PROVIDE_DNS_SERVERS LWIP_DHCP_MAX_DNS_SERVERS
164 #else
165 #define LWIP_DHCP_PROVIDE_DNS_SERVERS DNS_MAX_SERVERS
166 #endif
167 #else
168 #define LWIP_DHCP_PROVIDE_DNS_SERVERS 0
169 #endif
170 #endif /* 0 */
171 
172 #ifndef LWIP_DHCP_INPUT_ERROR
173 #define LWIP_DHCP_INPUT_ERROR(message, expression, handler) do { if (!(expression)) { \
174   handler;} } while(0)
175 #endif
176 
177 #if 0
178 /** Option handling: options are parsed in dhcp_parse_reply
179  * and saved in an array where other functions can load them from.
180  * This might be moved into the struct dhcp (not necessarily since
181  * lwIP is single-threaded and the array is only used while in recv
182  * callback). */
183 enum dhcp_option_idx {
184   DHCP_OPTION_IDX_OVERLOAD = 0,
185   DHCP_OPTION_IDX_MSG_TYPE,
186   DHCP_OPTION_IDX_SERVER_ID,
187   DHCP_OPTION_IDX_LEASE_TIME,
188   DHCP_OPTION_IDX_T1,
189   DHCP_OPTION_IDX_T2,
190   DHCP_OPTION_IDX_SUBNET_MASK,
191   DHCP_OPTION_IDX_ROUTER,
192 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
193   DHCP_OPTION_IDX_DNS_SERVER,
194   DHCP_OPTION_IDX_DNS_SERVER_LAST = DHCP_OPTION_IDX_DNS_SERVER + LWIP_DHCP_PROVIDE_DNS_SERVERS - 1,
195 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
196 #if LWIP_DHCP_GET_NTP_SRV
197   DHCP_OPTION_IDX_NTP_SERVER,
198   DHCP_OPTION_IDX_NTP_SERVER_LAST = DHCP_OPTION_IDX_NTP_SERVER + LWIP_DHCP_MAX_NTP_SERVERS - 1,
199 #endif /* LWIP_DHCP_GET_NTP_SRV */
200   DHCP_OPTION_IDX_MAX
201 };
202 
203 /** Holds the decoded option values, only valid while in dhcp_recv.
204     @todo: move this into struct dhcp? */
205 static u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
206 /** Holds a flag which option was received and is contained in dhcp_rx_options_val,
207     only valid while in dhcp_recv.
208     @todo: move this into struct dhcp? */
209 static u8_t  dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
210 #endif /* 0 */
211 static u8_t dhcp_discover_request_options[] = {
212   DHCP_OPTION_SUBNET_MASK,
213   DHCP_OPTION_ROUTER,
214   DHCP_OPTION_BROADCAST
215 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
216   , DHCP_OPTION_DNS_SERVER
217 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
218 #if LWIP_DHCP_GET_NTP_SRV
219   , DHCP_OPTION_NTP
220 #endif /* LWIP_DHCP_GET_NTP_SRV */
221   DHCP_ADD_EXTRA_REQUEST_OPTIONS
222 };
223 
224 #ifdef DHCP_GLOBAL_XID
225 static u32_t xid;
226 static u8_t xid_initialised;
227 #endif /* DHCP_GLOBAL_XID */
228 
229 #define dhcp_option_given(dhcp, idx)          ((dhcp)->rx_options_given[idx] != 0)
230 #define dhcp_got_option(dhcp, idx)            ((dhcp)->rx_options_given[idx] = 1)
231 #define dhcp_clear_option(dhcp, idx)          ((dhcp)->rx_options_given[idx] = 0)
232 #define dhcp_clear_all_options(dhcp)          (memset((dhcp)->rx_options_given, 0, sizeof((dhcp)->rx_options_given)))
233 #define dhcp_get_option_value(dhcp, idx)      ((dhcp)->rx_options_val[idx])
234 #define dhcp_set_option_value(dhcp, idx, val) ((dhcp)->rx_options_val[idx] = (val))
235 
236 static struct udp_pcb *dhcp_pcb;
237 static u8_t dhcp_pcb_refcount;
238 
239 /* DHCP client state machine functions */
240 static err_t dhcp_discover(struct netif *netif);
241 static err_t dhcp_select(struct netif *netif);
242 static void dhcp_bind(struct netif *netif);
243 #if LWIP_DHCP_DOES_ACD_CHECK
244 static err_t dhcp_decline(struct netif *netif);
245 #endif /* LWIP_DHCP_DOES_ACD_CHECK */
246 static err_t dhcp_rebind(struct netif *netif);
247 static err_t dhcp_reboot(struct netif *netif);
248 static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
249 
250 /* receive, unfold, parse and free incoming messages */
251 static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
252 
253 /* set the DHCP timers */
254 static void dhcp_timeout(struct netif *netif);
255 static void dhcp_t1_timeout(struct netif *netif);
256 static void dhcp_t2_timeout(struct netif *netif);
257 
258 /* build outgoing messages */
259 /* create a DHCP message, fill in common headers */
260 static struct pbuf *dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t *options_out_len);
261 /* add a DHCP option (type, then length in bytes) */
262 static u16_t dhcp_option(u16_t options_out_len, u8_t *options, u8_t option_type, u8_t option_len);
263 /* add option values */
264 static u16_t dhcp_option_byte(u16_t options_out_len, u8_t *options, u8_t value);
265 static u16_t dhcp_option_short(u16_t options_out_len, u8_t *options, u16_t value);
266 static u16_t dhcp_option_long(u16_t options_out_len, u8_t *options, u32_t value);
267 #if LWIP_NETIF_HOSTNAME
268 static u16_t dhcp_option_hostname(u16_t options_out_len, u8_t *options, struct netif *netif);
269 #endif /* LWIP_NETIF_HOSTNAME */
270 /* always add the DHCP options trailer to end and pad */
271 static void dhcp_option_trailer(u16_t options_out_len, u8_t *options, struct pbuf *p_out);
272 
273 /** Ensure DHCP PCB is allocated and bound */
274 static err_t
dhcp_inc_pcb_refcount(void)275 dhcp_inc_pcb_refcount(void)
276 {
277   if (dhcp_pcb_refcount == 0) {
278     LWIP_ASSERT("dhcp_inc_pcb_refcount(): memory leak", dhcp_pcb == NULL);
279 
280     /* allocate UDP PCB */
281     dhcp_pcb = udp_new();
282 
283     if (dhcp_pcb == NULL) {
284       return ERR_MEM;
285     }
286 
287     ip_set_option(dhcp_pcb, SOF_BROADCAST);
288 
289     /* set up local and remote port for the pcb -> listen on all interfaces on all src/dest IPs */
290     udp_bind(dhcp_pcb, IP4_ADDR_ANY, LWIP_IANA_PORT_DHCP_CLIENT);
291     udp_connect(dhcp_pcb, IP4_ADDR_ANY, LWIP_IANA_PORT_DHCP_SERVER);
292     udp_recv(dhcp_pcb, dhcp_recv, NULL);
293   }
294 
295   dhcp_pcb_refcount++;
296 
297   return ERR_OK;
298 }
299 
300 /** Free DHCP PCB if the last netif stops using it */
301 static void
dhcp_dec_pcb_refcount(void)302 dhcp_dec_pcb_refcount(void)
303 {
304   LWIP_ASSERT("dhcp_pcb_refcount(): refcount error", (dhcp_pcb_refcount > 0));
305   dhcp_pcb_refcount--;
306 
307   if (dhcp_pcb_refcount == 0) {
308     udp_remove(dhcp_pcb);
309     dhcp_pcb = NULL;
310   }
311 }
312 
313 /**
314  * Back-off the DHCP client (because of a received NAK response).
315  *
316  * Back-off the DHCP client because of a received NAK. Receiving a
317  * NAK means the client asked for something non-sensible, for
318  * example when it tries to renew a lease obtained on another network.
319  *
320  * We clear any existing set IP address and restart DHCP negotiation
321  * afresh (as per RFC2131 3.2.3).
322  *
323  * @param netif the netif under DHCP control
324  */
325 static void
dhcp_handle_nak(struct netif * netif)326 dhcp_handle_nak(struct netif *netif)
327 {
328   struct dhcp *dhcp = netif_dhcp_data(netif);
329 
330   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
331               (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
332   /* Change to a defined state - set this before assigning the address
333      to ensure the callback can use dhcp_supplied_address() */
334   dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
335   /* remove IP address from interface (must no longer be used, as per RFC2131) */
336   netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
337   /* We can immediately restart discovery */
338   dhcp_discover(netif);
339 }
340 
341 #if LWIP_DHCP_DOES_ACD_CHECK
342 /**
343 * Handle conflict information from ACD module
344 *
345 * @param netif   network interface to handle conflict information on
346 * @param state   acd_callback_enum_t
347  */
348 static void
dhcp_conflict_callback(struct netif * netif,acd_callback_enum_t state)349 dhcp_conflict_callback(struct netif *netif, acd_callback_enum_t state)
350 {
351   struct dhcp *dhcp = netif_dhcp_data(netif);
352   u16_t msecs;
353 
354   LWIP_ASSERT("DHCP should be enabled at this point, but it is not!",
355               (dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF));
356 
357   switch (state) {
358     case ACD_IP_OK:
359       dhcp_bind(netif);
360       break;
361     case ACD_RESTART_CLIENT:
362       /* wait 10s before restarting
363        * According to RFC2131 section 3.1 point 5:
364        * If the client detects that the address is already in use (e.g., through
365        * the use of ARP), the client MUST send a DHCPDECLINE message to the
366        * server and restarts the configuration process.  The client SHOULD wait
367        * a minimum of ten seconds before restarting the configuration process to
368        * avoid excessive network traffic in case of looping. */
369        dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
370        msecs = 10 * 1000;
371        dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
372        LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
373       break;
374     case ACD_DECLINE:
375       /* remove IP address from interface
376        * (prevents routing from selecting this interface) */
377       netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
378       /* Let the DHCP server know we will not use the address */
379       dhcp_decline(netif);
380       break;
381     default:
382       break;
383   }
384 }
385 
386 /**
387  * Checks if the offered IP address is already in use.
388  *
389  * It does this according to the address conflict detection method described in
390  * RFC5227.
391  *
392  * @param netif the netif under DHCP control
393  */
394 static void
dhcp_check(struct netif * netif)395 dhcp_check(struct netif *netif)
396 {
397   struct dhcp *dhcp = netif_dhcp_data(netif);
398 
399   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
400               (s16_t)netif->name[1]));
401   dhcp_set_state(dhcp, DHCP_STATE_CHECKING);
402 
403   /* start ACD module */
404   acd_start(netif, &dhcp->acd, dhcp->offered_ip_addr);
405 }
406 #endif /* LWIP_DHCP_DOES_ACD_CHECK */
407 
408 /**
409  * Remember the configuration offered by a DHCP server.
410  *
411  * @param netif the netif under DHCP control
412  */
413 static void
dhcp_handle_offer(struct netif * netif,struct dhcp_msg * msg_in)414 dhcp_handle_offer(struct netif *netif, struct dhcp_msg *msg_in)
415 {
416   struct dhcp *dhcp = netif_dhcp_data(netif);
417 
418   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n",
419               (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
420   /* obtain the server address */
421   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
422     dhcp->request_timeout = 0; /* stop timer */
423 
424     ip_addr_set_ip4_u32(&dhcp->server_ip_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
425     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n",
426                 ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
427     /* remember offered address */
428     ip4_addr_copy(dhcp->offered_ip_addr, msg_in->yiaddr);
429     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n",
430                 ip4_addr_get_u32(&dhcp->offered_ip_addr)));
431 
432     dhcp_select(netif);
433   } else {
434     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
435                 ("dhcp_handle_offer(netif=%p) did not get server ID!\n", (void *)netif));
436   }
437 }
438 
439 /**
440  * Select a DHCP server offer out of all offers.
441  *
442  * Simply select the first offer received.
443  *
444  * @param netif the netif under DHCP control
445  * @return lwIP specific error (see error.h)
446  */
447 static err_t
dhcp_select(struct netif * netif)448 dhcp_select(struct netif *netif)
449 {
450   struct dhcp *dhcp;
451   err_t result;
452   u16_t msecs;
453   u8_t i;
454   struct pbuf *p_out;
455   u16_t options_out_len;
456 
457   LWIP_ERROR("dhcp_select: netif != NULL", (netif != NULL), return ERR_ARG;);
458   dhcp = netif_dhcp_data(netif);
459   LWIP_ERROR("dhcp_select: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
460 
461   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
462   dhcp_set_state(dhcp, DHCP_STATE_REQUESTING);
463 
464   /* create and initialize the DHCP message header */
465   p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len);
466   if (p_out != NULL) {
467     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
468     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
469     options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif));
470 
471     /* MUST request the offered IP address */
472     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_REQUESTED_IP, 4);
473     options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
474 
475     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_SERVER_ID, 4);
476     options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
477 
478     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
479     for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
480       options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]);
481     }
482 
483 #if LWIP_NETIF_HOSTNAME
484     options_out_len = dhcp_option_hostname(options_out_len, msg_out->options, netif);
485 #endif /* LWIP_NETIF_HOSTNAME */
486 
487     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_REQUESTING, msg_out, DHCP_REQUEST, &options_out_len);
488     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
489 
490     /* send broadcast to any DHCP server */
491     result = udp_sendto_if_src(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif, IP4_ADDR_ANY);
492     pbuf_free(p_out);
493     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n"));
494   } else {
495     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_select: could not allocate DHCP request\n"));
496     result = ERR_MEM;
497   }
498   if (dhcp->tries < 255) {
499     dhcp->tries++;
500   }
501   msecs = (u16_t)((dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000);
502   dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
503   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
504   return result;
505 }
506 
507 /**
508  * The DHCP timer that checks for lease renewal/rebind timeouts.
509  * Must be called once a minute (see @ref DHCP_COARSE_TIMER_SECS).
510  */
511 void
dhcp_coarse_tmr(void)512 dhcp_coarse_tmr(void)
513 {
514   struct netif *netif;
515   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
516   /* iterate through all network interfaces */
517 #ifdef LOSCFG_NET_CONTAINER
518   NETIF_FOREACH(netif, get_root_net_group()) {
519 #else
520   NETIF_FOREACH(netif) {
521 #endif
522     /* only act on DHCP configured interfaces */
523     struct dhcp *dhcp = netif_dhcp_data(netif);
524     if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) {
525       /* compare lease time to expire timeout */
526       if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) {
527         LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t0 timeout\n"));
528         /* this clients' lease time has expired */
529         dhcp_release_and_stop(netif);
530         dhcp_start(netif);
531         /* timer is active (non zero), and triggers (zeroes) now? */
532       } else if (dhcp->t2_rebind_time && (dhcp->t2_rebind_time-- == 1)) {
533         LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
534         /* this clients' rebind timeout triggered */
535         dhcp_t2_timeout(netif);
536         /* timer is active (non zero), and triggers (zeroes) now */
537       } else if (dhcp->t1_renew_time && (dhcp->t1_renew_time-- == 1)) {
538         LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
539         /* this clients' renewal timeout triggered */
540         dhcp_t1_timeout(netif);
541       }
542     }
543   }
544 }
545 
546 #if LWIP_LOWPOWER
547 #include "lwip/lowpower.h"
548 
549 u32_t
550 dhcp_coarse_tmr_tick(void)
551 {
552   struct netif *netif;
553   u32_t tick = 0;
554   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
555   /* iterate through all network interfaces */
556 #ifdef LOSCFG_NET_CONTAINER
557   NETIF_FOREACH(netif, get_root_net_group())
558 #else
559   NETIF_FOREACH(netif)
560 #endif
561   {
562     struct dhcp *dhcp = netif_dhcp_data(netif);
563     if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) {
564       if (dhcp->t0_timeout > 0) {
565         if (dhcp->t0_timeout > dhcp->lease_used) {
566           SET_TMR_TICK(tick, dhcp->t0_timeout - dhcp->lease_used);
567         } else {
568           SET_TMR_TICK(tick, 1);
569         }
570       }
571       if (dhcp->t2_rebind_time > 0) {
572         SET_TMR_TICK(tick, dhcp->t2_rebind_time);
573       }
574       if (dhcp->t1_renew_time > 0) {
575         SET_TMR_TICK(tick, dhcp->t1_renew_time);
576       }
577     }
578   }
579   return tick;
580 }
581 
582 u32_t
583 dhcp_fine_tmr_tick(void)
584 {
585   struct netif *netif;
586   u32_t tick = 0;
587   /* loop through netif's */
588 #ifdef LOSCFG_NET_CONTAINER
589   NETIF_FOREACH(netif, get_root_net_group())
590 #else
591   NETIF_FOREACH(netif)
592 #endif
593   {
594     struct dhcp *dhcp = netif_dhcp_data(netif);
595     if (dhcp != NULL) {
596       if (dhcp->request_timeout > 0) {
597         SET_TMR_TICK(tick, dhcp->request_timeout);
598       }
599     }
600   }
601   return tick;
602 }
603 #endif /* LWIP_LOWPOWER */
604 
605 /**
606  * DHCP transaction timeout handling (this function must be called every 500ms,
607  * see @ref DHCP_FINE_TIMER_MSECS).
608  *
609  * A DHCP server is expected to respond within a short period of time.
610  * This timer checks whether an outstanding DHCP request is timed out.
611  */
612 void
613 dhcp_fine_tmr(void)
614 {
615   struct netif *netif;
616   /* loop through netif's */
617 #ifdef LOSCFG_NET_CONTAINER
618   NETIF_FOREACH(netif, get_root_net_group()) {
619 #else
620   NETIF_FOREACH(netif) {
621 #endif
622     struct dhcp *dhcp = netif_dhcp_data(netif);
623     /* only act on DHCP configured interfaces */
624     if (dhcp != NULL) {
625       /* timer is active (non zero), and is about to trigger now */
626       if (dhcp->request_timeout > 1) {
627         dhcp->request_timeout--;
628       } else if (dhcp->request_timeout == 1) {
629         dhcp->request_timeout--;
630         /* { dhcp->request_timeout == 0 } */
631         LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
632         /* this client's request timeout triggered */
633         dhcp_timeout(netif);
634       }
635     }
636   }
637 }
638 
639 /**
640  * A DHCP negotiation transaction, or ARP request, has timed out.
641  *
642  * The timer that was started with the DHCP or ARP request has
643  * timed out, indicating no response was received in time.
644  *
645  * @param netif the netif under DHCP control
646  */
647 static void
648 dhcp_timeout(struct netif *netif)
649 {
650   struct dhcp *dhcp = netif_dhcp_data(netif);
651 
652   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n"));
653   /* back-off period has passed, or server selection timed out */
654   if ((dhcp->state == DHCP_STATE_BACKING_OFF) || (dhcp->state == DHCP_STATE_SELECTING)) {
655     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
656     dhcp_discover(netif);
657     /* receiving the requested lease timed out */
658   } else if (dhcp->state == DHCP_STATE_REQUESTING) {
659     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
660     if (dhcp->tries <= 5) {
661       dhcp_select(netif);
662     } else {
663       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
664       dhcp_release_and_stop(netif);
665       dhcp_start(netif);
666     }
667   } else if (dhcp->state == DHCP_STATE_REBOOTING) {
668     if (dhcp->tries < REBOOT_TRIES) {
669       dhcp_reboot(netif);
670     } else {
671       dhcp_discover(netif);
672     }
673   }
674 }
675 
676 /**
677  * The renewal period has timed out.
678  *
679  * @param netif the netif under DHCP control
680  */
681 static void
682 dhcp_t1_timeout(struct netif *netif)
683 {
684   struct dhcp *dhcp = netif_dhcp_data(netif);
685 
686   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
687   if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
688       (dhcp->state == DHCP_STATE_RENEWING)) {
689     /* just retry to renew - note that the rebind timer (t2) will
690      * eventually time-out if renew tries fail. */
691     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
692                 ("dhcp_t1_timeout(): must renew\n"));
693     /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
694        DHCP_STATE_RENEWING, not DHCP_STATE_BOUND */
695     dhcp_renew(netif);
696     /* Calculate next timeout */
697     if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= DHCP_NEXT_TIMEOUT_THRESHOLD) {
698       dhcp->t1_renew_time = (dhcp_timeout_t)((dhcp->t2_timeout - dhcp->lease_used) / 2);
699     }
700   }
701 }
702 
703 /**
704  * The rebind period has timed out.
705  *
706  * @param netif the netif under DHCP control
707  */
708 static void
709 dhcp_t2_timeout(struct netif *netif)
710 {
711   struct dhcp *dhcp = netif_dhcp_data(netif);
712 
713   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
714   if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
715       (dhcp->state == DHCP_STATE_RENEWING) || (dhcp->state == DHCP_STATE_REBINDING)) {
716     /* just retry to rebind */
717     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
718                 ("dhcp_t2_timeout(): must rebind\n"));
719     /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
720        DHCP_STATE_REBINDING, not DHCP_STATE_BOUND */
721     dhcp_rebind(netif);
722     /* Calculate next timeout */
723     if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= DHCP_NEXT_TIMEOUT_THRESHOLD) {
724       dhcp->t2_rebind_time = (dhcp_timeout_t)((dhcp->t0_timeout - dhcp->lease_used) / 2);
725     }
726   }
727 }
728 
729 /**
730  * Handle a DHCP ACK packet
731  *
732  * @param netif the netif under DHCP control
733  */
734 static void
735 dhcp_handle_ack(struct netif *netif, struct dhcp_msg *msg_in)
736 {
737   struct dhcp *dhcp = netif_dhcp_data(netif);
738 
739 #if LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV
740   u8_t n;
741 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV */
742 #if LWIP_DHCP_GET_NTP_SRV
743   ip4_addr_t ntp_server_addrs[LWIP_DHCP_MAX_NTP_SERVERS];
744 #endif
745 
746   /* clear options we might not get from the ACK */
747   ip4_addr_set_zero(&dhcp->offered_sn_mask);
748   ip4_addr_set_zero(&dhcp->offered_gw_addr);
749 #if LWIP_DHCP_BOOTP_FILE
750   ip4_addr_set_zero(&dhcp->offered_si_addr);
751 #endif /* LWIP_DHCP_BOOTP_FILE */
752 
753   /* lease time given? */
754   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
755     /* remember offered lease time */
756     dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
757   }
758   /* renewal period given? */
759   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
760     /* remember given renewal period */
761     dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
762   } else {
763     /* calculate safe periods for renewal */
764     dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
765   }
766 
767   /* renewal period given? */
768   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
769     /* remember given rebind period */
770     dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
771   } else {
772     /* calculate safe periods for rebinding (offered_t0_lease * 0.875 -> 87.5%)*/
773     dhcp->offered_t2_rebind = (dhcp->offered_t0_lease * 7U) / 8U;
774   }
775 
776   /* (y)our internet address */
777   ip4_addr_copy(dhcp->offered_ip_addr, msg_in->yiaddr);
778 
779 #if LWIP_DHCP_BOOTP_FILE
780   /* copy boot server address,
781      boot file name copied in dhcp_parse_reply if not overloaded */
782   ip4_addr_copy(dhcp->offered_si_addr, msg_in->siaddr);
783 #endif /* LWIP_DHCP_BOOTP_FILE */
784 
785   /* subnet mask given? */
786   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
787     /* remember given subnet mask */
788     ip4_addr_set_u32(&dhcp->offered_sn_mask, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
789     dhcp->flags |= DHCP_FLAG_SUBNET_MASK_GIVEN;
790   } else {
791     dhcp->flags &= ~DHCP_FLAG_SUBNET_MASK_GIVEN;
792   }
793 
794   /* gateway router */
795   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
796     ip4_addr_set_u32(&dhcp->offered_gw_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
797   }
798 
799 #if LWIP_DHCP_GET_NTP_SRV
800   /* NTP servers */
801   for (n = 0; (n < LWIP_DHCP_MAX_NTP_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n); n++) {
802     ip4_addr_set_u32(&ntp_server_addrs[n], lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n)));
803   }
804   dhcp_set_ntp_servers(n, ntp_server_addrs);
805 #endif /* LWIP_DHCP_GET_NTP_SRV */
806 
807 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
808   /* DNS servers */
809   for (n = 0; (n < LWIP_DHCP_PROVIDE_DNS_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n); n++) {
810     ip_addr_t dns_addr;
811     ip_addr_set_ip4_u32_val(dns_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
812     dns_setserver(n, &dns_addr);
813   }
814 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
815 }
816 
817 /**
818  * @ingroup dhcp4
819  * Set a statically allocated struct dhcp to work with.
820  * Using this prevents dhcp_start to allocate it using mem_malloc.
821  *
822  * @param netif the netif for which to set the struct dhcp
823  * @param dhcp (uninitialised) dhcp struct allocated by the application
824  */
825 void
826 dhcp_set_struct(struct netif *netif, struct dhcp *dhcp)
827 {
828   LWIP_ASSERT_CORE_LOCKED();
829   LWIP_ASSERT("netif != NULL", netif != NULL);
830   LWIP_ASSERT("dhcp != NULL", dhcp != NULL);
831   LWIP_ASSERT("netif already has a struct dhcp set", netif_dhcp_data(netif) == NULL);
832 
833   /* clear data structure */
834   memset(dhcp, 0, sizeof(struct dhcp));
835   /* mark this as externally allocated */
836   dhcp->flags |= DHCP_FLAG_EXTERNAL_MEM;
837   /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */
838   netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
839 }
840 
841 /**
842  * @ingroup dhcp4
843  * Removes a struct dhcp from a netif.
844  *
845  * ATTENTION: Only use this when not using dhcp_set_struct() to allocate the
846  *            struct dhcp since the memory is passed back to the heap.
847  *
848  * @param netif the netif from which to remove the struct dhcp
849  */
850 void dhcp_cleanup(struct netif *netif)
851 {
852   struct dhcp *dhcp;
853   LWIP_ASSERT_CORE_LOCKED();
854   LWIP_ASSERT("netif != NULL", netif != NULL);
855 
856   dhcp = netif_dhcp_data(netif);
857   if (dhcp != NULL) {
858     if (!(dhcp->flags & DHCP_FLAG_EXTERNAL_MEM)) {
859       mem_free(dhcp);
860     }
861     netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, NULL);
862   }
863 }
864 
865 /**
866  * @ingroup dhcp4
867  * Start DHCP negotiation for a network interface.
868  *
869  * If no DHCP client instance was attached to this interface,
870  * a new client is created first. If a DHCP client instance
871  * was already present, it restarts negotiation.
872  *
873  * @param netif The lwIP network interface
874  * @return lwIP error code
875  * - ERR_OK - No error
876  * - ERR_MEM - Out of memory
877  */
878 err_t
879 dhcp_start(struct netif *netif)
880 {
881   struct dhcp *dhcp;
882   err_t result;
883 
884   LWIP_ASSERT_CORE_LOCKED();
885   LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
886   LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
887   dhcp = netif_dhcp_data(netif);
888   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));
889 
890   /* check MTU of the netif */
891   if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
892     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
893     return ERR_MEM;
894   }
895 
896   /* no DHCP client attached yet? */
897   if (dhcp == NULL) {
898     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): mallocing new DHCP client\n"));
899     dhcp = (struct dhcp *)mem_malloc(sizeof(struct dhcp));
900     if (dhcp == NULL) {
901       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
902       return ERR_MEM;
903     }
904 
905     /* store this dhcp client in the netif */
906     netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
907     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp\n"));
908     /* already has DHCP client attached */
909   } else {
910     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(): restarting DHCP configuration\n"));
911 
912     if (dhcp->pcb_allocated != 0) {
913       dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
914     }
915     /* dhcp is cleared below, no need to reset flag*/
916   }
917 
918   /* clear data structure */
919   memset(dhcp, 0, sizeof(struct dhcp));
920   /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */
921 
922 
923 #if LWIP_DHCP_DOES_ACD_CHECK
924   /* add acd struct to list*/
925   acd_add(netif, &dhcp->acd, dhcp_conflict_callback);
926 #endif /* LWIP_DHCP_DOES_ACD_CHECK */
927 
928 
929   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
930 
931   if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */
932     return ERR_MEM;
933   }
934   dhcp->pcb_allocated = 1;
935 
936   if (!netif_is_link_up(netif)) {
937     /* set state INIT and wait for dhcp_network_changed() to call dhcp_discover() */
938     dhcp_set_state(dhcp, DHCP_STATE_INIT);
939     return ERR_OK;
940   }
941 
942   /* (re)start the DHCP negotiation */
943   result = dhcp_discover(netif);
944   if (result != ERR_OK) {
945     /* free resources allocated above */
946     dhcp_release_and_stop(netif);
947     return ERR_MEM;
948   }
949   return result;
950 }
951 
952 /**
953  * @ingroup dhcp4
954  * Inform a DHCP server of our manual configuration.
955  *
956  * This informs DHCP servers of our fixed IP address configuration
957  * by sending an INFORM message. It does not involve DHCP address
958  * configuration, it is just here to be nice to the network.
959  *
960  * @param netif The lwIP network interface
961  */
962 void
963 dhcp_inform(struct netif *netif)
964 {
965   struct dhcp dhcp;
966   struct pbuf *p_out;
967   u16_t options_out_len;
968 
969   LWIP_ASSERT_CORE_LOCKED();
970   LWIP_ERROR("netif != NULL", (netif != NULL), return;);
971 
972   if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */
973     return;
974   }
975 
976   memset(&dhcp, 0, sizeof(struct dhcp));
977   dhcp_set_state(&dhcp, DHCP_STATE_INFORMING);
978 
979   /* create and initialize the DHCP message header */
980   p_out = dhcp_create_msg(netif, &dhcp, DHCP_INFORM, &options_out_len);
981   if (p_out != NULL) {
982     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
983     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
984     options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif));
985 
986     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, &dhcp, DHCP_STATE_INFORMING, msg_out, DHCP_INFORM, &options_out_len);
987     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
988 
989     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
990 
991     udp_sendto_if(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif);
992 
993     pbuf_free(p_out);
994   } else {
995     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n"));
996   }
997 
998   dhcp_dec_pcb_refcount(); /* delete DHCP PCB if not needed any more */
999 }
1000 
1001 /** Handle a possible change in the network configuration.
1002  *
1003  * This enters the REBOOTING state to verify that the currently bound
1004  * address is still valid.
1005  */
1006 void
1007 dhcp_network_changed_link_up(struct netif *netif)
1008 {
1009   struct dhcp *dhcp = netif_dhcp_data(netif);
1010 
1011   if (!dhcp) {
1012     return;
1013   }
1014   switch (dhcp->state) {
1015     case DHCP_STATE_REBINDING:
1016     case DHCP_STATE_RENEWING:
1017     case DHCP_STATE_BOUND:
1018     case DHCP_STATE_REBOOTING:
1019       dhcp->tries = 0;
1020       dhcp_reboot(netif);
1021       break;
1022     case DHCP_STATE_OFF:
1023       /* stay off */
1024       break;
1025     default:
1026       LWIP_ASSERT("invalid dhcp->state", dhcp->state <= DHCP_STATE_BACKING_OFF);
1027       /* INIT/REQUESTING/CHECKING/BACKING_OFF restart with new 'rid' because the
1028          state changes, SELECTING: continue with current 'rid' as we stay in the
1029          same state */
1030       /* ensure we start with short timeouts, even if already discovering */
1031       dhcp->tries = 0;
1032       dhcp_discover(netif);
1033       break;
1034   }
1035 }
1036 
1037 #if LWIP_DHCP_DOES_ACD_CHECK
1038 /**
1039  * Decline an offered lease.
1040  *
1041  * Tell the DHCP server we do not accept the offered address.
1042  * One reason to decline the lease is when we find out the address
1043  * is already in use by another host (through ARP).
1044  *
1045  * @param netif the netif under DHCP control
1046  */
1047 static err_t
1048 dhcp_decline(struct netif *netif)
1049 {
1050   struct dhcp *dhcp = netif_dhcp_data(netif);
1051   err_t result;
1052   struct pbuf *p_out;
1053   u16_t options_out_len;
1054 
1055   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));
1056   dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
1057 
1058   /* create and initialize the DHCP message header */
1059   p_out = dhcp_create_msg(netif, dhcp, DHCP_DECLINE, &options_out_len);
1060   if (p_out != NULL) {
1061     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
1062     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_REQUESTED_IP, 4);
1063     options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1064 
1065     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_BACKING_OFF, msg_out, DHCP_DECLINE, &options_out_len);
1066     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
1067 
1068     /* per section 4.4.4, broadcast DECLINE messages */
1069     result = udp_sendto_if_src(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif, IP4_ADDR_ANY);
1070     pbuf_free(p_out);
1071     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
1072   } else {
1073     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1074                 ("dhcp_decline: could not allocate DHCP request\n"));
1075     result = ERR_MEM;
1076   }
1077   return result;
1078 }
1079 #endif /* LWIP_DHCP_DOES_ACD_CHECK */
1080 
1081 
1082 /**
1083  * Start the DHCP process, discover a DHCP server.
1084  *
1085  * @param netif the netif under DHCP control
1086  */
1087 static err_t
1088 dhcp_discover(struct netif *netif)
1089 {
1090   struct dhcp *dhcp = netif_dhcp_data(netif);
1091   err_t result = ERR_OK;
1092   u16_t msecs;
1093   u8_t i;
1094   struct pbuf *p_out;
1095   u16_t options_out_len;
1096 
1097   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover()\n"));
1098 
1099 #if LWIP_DHCP_AUTOIP_COOP
1100   if (dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES) {
1101     autoip_start(netif);
1102   }
1103 #endif /* LWIP_DHCP_AUTOIP_COOP */
1104 
1105   ip4_addr_set_any(&dhcp->offered_ip_addr);
1106   dhcp_set_state(dhcp, DHCP_STATE_SELECTING);
1107   /* create and initialize the DHCP message header */
1108   p_out = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER, &options_out_len);
1109   if (p_out != NULL) {
1110     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
1111     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
1112 
1113     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1114     options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif));
1115 
1116 #if LWIP_NETIF_HOSTNAME && LWIP_DHCP_DISCOVER_ADD_HOSTNAME
1117     options_out_len = dhcp_option_hostname(options_out_len, msg_out->options, netif);
1118 #endif /* LWIP NETIF HOSTNAME && LWIP_DHCP_DISCOVER_ADD_HOSTNAME */
1119 
1120     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1121     for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1122       options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]);
1123     }
1124     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_SELECTING, msg_out, DHCP_DISCOVER, &options_out_len);
1125     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
1126 
1127     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER)\n"));
1128     udp_sendto_if_src(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif, IP4_ADDR_ANY);
1129     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()\n"));
1130     pbuf_free(p_out);
1131     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
1132   } else {
1133     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n"));
1134   }
1135 
1136   if (dhcp->tries < 255) {
1137     dhcp->tries++;
1138   }
1139   msecs = DHCP_REQUEST_BACKOFF_SEQUENCE(dhcp->tries);
1140   dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
1141   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
1142   return result;
1143 }
1144 
1145 
1146 /**
1147  * Bind the interface to the offered IP address.
1148  *
1149  * @param netif network interface to bind to the offered address
1150  */
1151 static void
1152 dhcp_bind(struct netif *netif)
1153 {
1154   struct dhcp *dhcp;
1155   ip4_addr_t sn_mask, gw_addr;
1156   LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
1157   dhcp = netif_dhcp_data(netif);
1158   LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
1159   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void *)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
1160 
1161   /* reset time used of lease */
1162   dhcp->lease_used = 0;
1163 
1164   if (dhcp->offered_t0_lease != 0xffffffffUL) {
1165     /* set renewal period timer */
1166     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease));
1167     DHCP_SET_TIMEOUT_FROM_OFFERED_T0_LEASE(dhcp->t0_timeout, dhcp);
1168     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t0_lease * 1000));
1169   }
1170 
1171   /* temporary DHCP lease? */
1172   if (dhcp->offered_t1_renew != 0xffffffffUL) {
1173     /* set renewal period timer */
1174     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
1175     DHCP_SET_TIMEOUT_FROM_OFFERED_T1_RENEW(dhcp->t1_timeout, dhcp);
1176     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew * 1000));
1177     dhcp->t1_renew_time = dhcp->t1_timeout;
1178   }
1179   /* set renewal period timer */
1180   if (dhcp->offered_t2_rebind != 0xffffffffUL) {
1181     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
1182     DHCP_SET_TIMEOUT_FROM_OFFERED_T2_REBIND(dhcp->t2_timeout, dhcp);
1183     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind * 1000));
1184     dhcp->t2_rebind_time = dhcp->t2_timeout;
1185   }
1186 
1187   /* If we have sub 1 minute lease, t2 and t1 will kick in at the same time. */
1188   if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1189     dhcp->t1_timeout = 0;
1190   }
1191 
1192   if (dhcp->flags & DHCP_FLAG_SUBNET_MASK_GIVEN) {
1193     /* copy offered network mask */
1194     ip4_addr_copy(sn_mask, dhcp->offered_sn_mask);
1195   } else {
1196     /* subnet mask not given, choose a safe subnet mask given the network class */
1197     u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
1198     if (first_octet <= 127) {
1199       ip4_addr_set_u32(&sn_mask, PP_HTONL(0xff000000UL));
1200     } else if (first_octet >= 192) {
1201       ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffffff00UL));
1202     } else {
1203       ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffff0000UL));
1204     }
1205   }
1206 
1207   ip4_addr_copy(gw_addr, dhcp->offered_gw_addr);
1208 
1209   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F" SN: 0x%08"X32_F" GW: 0x%08"X32_F"\n",
1210               ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr)));
1211   /* netif is now bound to DHCP leased address - set this before assigning the address
1212      to ensure the callback can use dhcp_supplied_address() */
1213   dhcp_set_state(dhcp, DHCP_STATE_BOUND);
1214 
1215   netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
1216   /* interface is used by routing now that an address is set */
1217 }
1218 
1219 /**
1220  * @ingroup dhcp4
1221  * Renew an existing DHCP lease at the involved DHCP server.
1222  *
1223  * @param netif network interface which must renew its lease
1224  */
1225 err_t
1226 dhcp_renew(struct netif *netif)
1227 {
1228   struct dhcp *dhcp = netif_dhcp_data(netif);
1229   err_t result;
1230   u16_t msecs;
1231   u8_t i;
1232   struct pbuf *p_out;
1233   u16_t options_out_len;
1234 
1235   LWIP_ASSERT_CORE_LOCKED();
1236   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_renew()\n"));
1237   dhcp_set_state(dhcp, DHCP_STATE_RENEWING);
1238 
1239   /* create and initialize the DHCP message header */
1240   p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len);
1241   if (p_out != NULL) {
1242     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
1243     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1244     options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif));
1245 
1246     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1247     for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1248       options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]);
1249     }
1250 
1251 #if LWIP_NETIF_HOSTNAME
1252     options_out_len = dhcp_option_hostname(options_out_len, msg_out->options, netif);
1253 #endif /* LWIP_NETIF_HOSTNAME */
1254 
1255     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_RENEWING, msg_out, DHCP_REQUEST, &options_out_len);
1256     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
1257 
1258     result = udp_sendto_if(dhcp_pcb, p_out, &dhcp->server_ip_addr, LWIP_IANA_PORT_DHCP_SERVER, netif);
1259     pbuf_free(p_out);
1260 
1261     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
1262   } else {
1263     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n"));
1264     result = ERR_MEM;
1265   }
1266   if (dhcp->tries < 255) {
1267     dhcp->tries++;
1268   }
1269   /* back-off on retries, but to a maximum of 20 seconds */
1270   msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000);
1271   dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
1272   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
1273   return result;
1274 }
1275 
1276 /**
1277  * Rebind with a DHCP server for an existing DHCP lease.
1278  *
1279  * @param netif network interface which must rebind with a DHCP server
1280  */
1281 static err_t
1282 dhcp_rebind(struct netif *netif)
1283 {
1284   struct dhcp *dhcp = netif_dhcp_data(netif);
1285   err_t result;
1286   u16_t msecs;
1287   u8_t i;
1288   struct pbuf *p_out;
1289   u16_t options_out_len;
1290 
1291   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
1292   dhcp_set_state(dhcp, DHCP_STATE_REBINDING);
1293 
1294   /* create and initialize the DHCP message header */
1295   p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len);
1296   if (p_out != NULL) {
1297     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
1298     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1299     options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN(netif));
1300 
1301     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1302     for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1303       options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]);
1304     }
1305 
1306 #if LWIP_NETIF_HOSTNAME
1307     options_out_len = dhcp_option_hostname(options_out_len, msg_out->options, netif);
1308 #endif /* LWIP_NETIF_HOSTNAME */
1309 
1310     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_REBINDING, msg_out, DHCP_DISCOVER, &options_out_len);
1311     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
1312 
1313     /* broadcast to server */
1314     result = udp_sendto_if(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif);
1315     pbuf_free(p_out);
1316     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
1317   } else {
1318     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n"));
1319     result = ERR_MEM;
1320   }
1321   if (dhcp->tries < 255) {
1322     dhcp->tries++;
1323   }
1324   msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000);
1325   dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
1326   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
1327   return result;
1328 }
1329 
1330 /**
1331  * Enter REBOOTING state to verify an existing lease
1332  *
1333  * @param netif network interface which must reboot
1334  */
1335 static err_t
1336 dhcp_reboot(struct netif *netif)
1337 {
1338   struct dhcp *dhcp = netif_dhcp_data(netif);
1339   err_t result;
1340   u16_t msecs;
1341   u8_t i;
1342   struct pbuf *p_out;
1343   u16_t options_out_len;
1344 
1345   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot()\n"));
1346   dhcp_set_state(dhcp, DHCP_STATE_REBOOTING);
1347 
1348   /* create and initialize the DHCP message header */
1349   p_out = dhcp_create_msg(netif, dhcp, DHCP_REQUEST, &options_out_len);
1350   if (p_out != NULL) {
1351     struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
1352     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1353     options_out_len = dhcp_option_short(options_out_len, msg_out->options, DHCP_MAX_MSG_LEN_MIN_REQUIRED);
1354 
1355     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_REQUESTED_IP, 4);
1356     options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1357 
1358     options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1359     for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1360       options_out_len = dhcp_option_byte(options_out_len, msg_out->options, dhcp_discover_request_options[i]);
1361     }
1362 
1363 #if LWIP_NETIF_HOSTNAME
1364     options_out_len = dhcp_option_hostname(options_out_len, msg_out->options, netif);
1365 #endif /* LWIP_NETIF_HOSTNAME */
1366 
1367     LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, DHCP_STATE_REBOOTING, msg_out, DHCP_REQUEST, &options_out_len);
1368     dhcp_option_trailer(options_out_len, msg_out->options, p_out);
1369 
1370     /* broadcast to server */
1371     result = udp_sendto_if(dhcp_pcb, p_out, IP_ADDR_BROADCAST, LWIP_IANA_PORT_DHCP_SERVER, netif);
1372     pbuf_free(p_out);
1373     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n"));
1374   } else {
1375     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n"));
1376     result = ERR_MEM;
1377   }
1378   if (dhcp->tries < 255) {
1379     dhcp->tries++;
1380   }
1381   msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000);
1382   dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
1383   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
1384   return result;
1385 }
1386 
1387 /**
1388  * @ingroup dhcp4
1389  * Release a DHCP lease and stop DHCP statemachine (and AUTOIP if LWIP_DHCP_AUTOIP_COOP).
1390  *
1391  * @param netif network interface
1392  */
1393 void
1394 dhcp_release_and_stop(struct netif *netif)
1395 {
1396   struct dhcp *dhcp = netif_dhcp_data(netif);
1397   ip_addr_t server_ip_addr;
1398 
1399   LWIP_ASSERT_CORE_LOCKED();
1400   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release_and_stop()\n"));
1401   if (dhcp == NULL) {
1402     return;
1403   }
1404 
1405   /* already off? -> nothing to do */
1406   if (dhcp->state == DHCP_STATE_OFF) {
1407     return;
1408   }
1409 
1410   ip_addr_copy(server_ip_addr, dhcp->server_ip_addr);
1411 
1412   /* clean old DHCP offer */
1413   ip_addr_set_zero_ip4(&dhcp->server_ip_addr);
1414   ip4_addr_set_zero(&dhcp->offered_ip_addr);
1415   ip4_addr_set_zero(&dhcp->offered_sn_mask);
1416   ip4_addr_set_zero(&dhcp->offered_gw_addr);
1417 #if LWIP_DHCP_BOOTP_FILE
1418   ip4_addr_set_zero(&dhcp->offered_si_addr);
1419 #endif /* LWIP_DHCP_BOOTP_FILE */
1420   dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1421   dhcp->t1_renew_time = dhcp->t2_rebind_time = dhcp->lease_used = dhcp->t0_timeout = 0;
1422 
1423   /* send release message when current IP was assigned via DHCP */
1424   if (dhcp_supplied_address(netif)) {
1425     /* create and initialize the DHCP message header */
1426     struct pbuf *p_out;
1427     u16_t options_out_len;
1428     dhcp_set_state(dhcp, DHCP_STATE_OFF);
1429     p_out = dhcp_create_msg(netif, dhcp, DHCP_RELEASE, &options_out_len);
1430     if (p_out != NULL) {
1431       struct dhcp_msg *msg_out = (struct dhcp_msg *)p_out->payload;
1432       options_out_len = dhcp_option(options_out_len, msg_out->options, DHCP_OPTION_SERVER_ID, 4);
1433       options_out_len = dhcp_option_long(options_out_len, msg_out->options, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr))));
1434 
1435       LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, dhcp->state, msg_out, DHCP_RELEASE, &options_out_len);
1436       dhcp_option_trailer(options_out_len, msg_out->options, p_out);
1437 
1438       udp_sendto_if(dhcp_pcb, p_out, &server_ip_addr, LWIP_IANA_PORT_DHCP_SERVER, netif);
1439       pbuf_free(p_out);
1440       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_STATE_OFF\n"));
1441     } else {
1442       /* sending release failed, but that's not a problem since the correct behaviour of dhcp does not rely on release */
1443       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n"));
1444     }
1445 
1446     /* remove IP address from interface (prevents routing from selecting this interface) */
1447     netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
1448   } else {
1449      dhcp_set_state(dhcp, DHCP_STATE_OFF);
1450   }
1451 
1452 #if LWIP_DHCP_DOES_ACD_CHECK
1453   /* stop acd because we may be in checking state and the callback would trigger a bind */
1454   acd_remove(netif, &dhcp->acd);
1455 #endif
1456 
1457   if (dhcp->pcb_allocated != 0) {
1458     dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
1459     dhcp->pcb_allocated = 0;
1460   }
1461 }
1462 
1463 /**
1464  * @ingroup dhcp4
1465  * This function calls dhcp_release_and_stop() internally.
1466  * @deprecated Use dhcp_release_and_stop() instead.
1467  */
1468 err_t
1469 dhcp_release(struct netif *netif)
1470 {
1471   dhcp_release_and_stop(netif);
1472   return ERR_OK;
1473 }
1474 
1475 /**
1476  * @ingroup dhcp4
1477  * This function calls dhcp_release_and_stop() internally.
1478  * @deprecated Use dhcp_release_and_stop() instead.
1479  */
1480 void
1481 dhcp_stop(struct netif *netif)
1482 {
1483   dhcp_release_and_stop(netif);
1484 }
1485 
1486 /*
1487  * Set the DHCP state of a DHCP client.
1488  *
1489  * If the state changed, reset the number of tries.
1490  */
1491 static void
1492 dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
1493 {
1494   if (new_state != dhcp->state) {
1495     dhcp->state = new_state;
1496     dhcp->tries = 0;
1497     dhcp->request_timeout = 0;
1498   }
1499 }
1500 
1501 /*
1502  * Concatenate an option type and length field to the outgoing
1503  * DHCP message.
1504  *
1505  */
1506 static u16_t
1507 dhcp_option(u16_t options_out_len, u8_t *options, u8_t option_type, u8_t option_len)
1508 {
1509   LWIP_ASSERT("dhcp_option: options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
1510   options[options_out_len++] = option_type;
1511   options[options_out_len++] = option_len;
1512   return options_out_len;
1513 }
1514 /*
1515  * Concatenate a single byte to the outgoing DHCP message.
1516  *
1517  */
1518 static u16_t
1519 dhcp_option_byte(u16_t options_out_len, u8_t *options, u8_t value)
1520 {
1521   LWIP_ASSERT("dhcp_option_byte: options_out_len < DHCP_OPTIONS_LEN", options_out_len < DHCP_OPTIONS_LEN);
1522   options[options_out_len++] = value;
1523   return options_out_len;
1524 }
1525 
1526 static u16_t
1527 dhcp_option_short(u16_t options_out_len, u8_t *options, u16_t value)
1528 {
1529   LWIP_ASSERT("dhcp_option_short: options_out_len + 2 <= DHCP_OPTIONS_LEN", options_out_len + 2U <= DHCP_OPTIONS_LEN);
1530   options[options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
1531   options[options_out_len++] = (u8_t) (value & 0x00ffU);
1532   return options_out_len;
1533 }
1534 
1535 static u16_t
1536 dhcp_option_long(u16_t options_out_len, u8_t *options, u32_t value)
1537 {
1538   LWIP_ASSERT("dhcp_option_long: options_out_len + 4 <= DHCP_OPTIONS_LEN", options_out_len + 4U <= DHCP_OPTIONS_LEN);
1539   options[options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
1540   options[options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
1541   options[options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
1542   options[options_out_len++] = (u8_t)((value & 0x000000ffUL));
1543   return options_out_len;
1544 }
1545 
1546 #if LWIP_NETIF_HOSTNAME
1547 static u16_t
1548 dhcp_option_hostname(u16_t options_out_len, u8_t *options, struct netif *netif)
1549 {
1550   if (netif->hostname != NULL) {
1551     size_t namelen = strlen(netif->hostname);
1552     if (namelen > 0) {
1553       size_t len;
1554       const char *p = netif->hostname;
1555       /* Shrink len to available bytes (need 2 bytes for OPTION_HOSTNAME
1556          and 1 byte for trailer) */
1557       size_t available = DHCP_OPTIONS_LEN - options_out_len - 3;
1558       LWIP_ASSERT("DHCP: hostname is too long!", namelen <= available);
1559       len = LWIP_MIN(namelen, available);
1560       LWIP_ASSERT("DHCP: hostname is too long!", len <= 0xFF);
1561       options_out_len = dhcp_option(options_out_len, options, DHCP_OPTION_HOSTNAME, (u8_t)len);
1562       while (len--) {
1563         options_out_len = dhcp_option_byte(options_out_len, options, *p++);
1564       }
1565     }
1566   }
1567   return options_out_len;
1568 }
1569 #endif /* LWIP_NETIF_HOSTNAME */
1570 
1571 /**
1572  * Extract the DHCP message and the DHCP options.
1573  *
1574  * Extract the DHCP message and the DHCP options, each into a contiguous
1575  * piece of memory. As a DHCP message is variable sized by its options,
1576  * and also allows overriding some fields for options, the easy approach
1577  * is to first unfold the options into a contiguous piece of memory, and
1578  * use that further on.
1579  *
1580  */
1581 static err_t
1582 dhcp_parse_reply(struct pbuf *p, struct dhcp *dhcp)
1583 {
1584   u8_t *options;
1585   u16_t offset;
1586   u16_t offset_max;
1587   u16_t options_offset;
1588   u16_t options_idx;
1589   u16_t options_idx_max;
1590   struct pbuf *q;
1591   int parse_file_as_options = 0;
1592   int parse_sname_as_options = 0;
1593   struct dhcp_msg *msg_in;
1594 #if LWIP_DHCP_BOOTP_FILE
1595   int file_overloaded = 0;
1596 #endif
1597 
1598   LWIP_UNUSED_ARG(dhcp);
1599 
1600   /* clear received options */
1601   dhcp_clear_all_options(dhcp);
1602   /* check that beginning of dhcp_msg (up to and including chaddr) is in first pbuf */
1603   if (p->len < DHCP_SNAME_OFS) {
1604     return ERR_BUF;
1605   }
1606   msg_in = (struct dhcp_msg *)p->payload;
1607 #if LWIP_DHCP_BOOTP_FILE
1608   /* clear boot file name */
1609   dhcp->boot_file_name[0] = 0;
1610 #endif /* LWIP_DHCP_BOOTP_FILE */
1611 
1612   /* parse options */
1613 
1614   /* start with options field */
1615   options_idx = DHCP_OPTIONS_OFS;
1616   /* parse options to the end of the received packet */
1617   options_idx_max = p->tot_len;
1618 again:
1619   q = p;
1620   options_offset = options_idx;
1621   while ((q != NULL) && (options_idx >= q->len)) {
1622     options_idx = (u16_t)(options_idx - q->len);
1623     options_idx_max = (u16_t)(options_idx_max - q->len);
1624     q = q->next;
1625   }
1626   if (q == NULL) {
1627     return ERR_BUF;
1628   }
1629   offset = options_idx;
1630   offset_max = options_idx_max;
1631   options = (u8_t *)q->payload;
1632   /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
1633   while ((q != NULL) && (offset < offset_max) && (options[offset] != DHCP_OPTION_END)) {
1634     u8_t op = options[offset];
1635     u8_t len;
1636     u8_t decode_len = 0;
1637     int decode_idx = -1;
1638     u16_t val_offset = (u16_t)(offset + 2);
1639     if (val_offset < offset) {
1640       /* overflow */
1641       return ERR_BUF;
1642     }
1643     /* len byte might be in the next pbuf */
1644     if ((offset + 1) < q->len) {
1645       len = options[offset + 1];
1646     } else {
1647       len = (q->next != NULL ? ((u8_t *)q->next->payload)[0] : 0);
1648     }
1649     /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F"\n", msg_offset, q->len)); */
1650     decode_len = len;
1651     switch (op) {
1652       /* case(DHCP_OPTION_END): handled above */
1653       case (DHCP_OPTION_PAD):
1654         /* special option: no len encoded */
1655         decode_len = len = 0;
1656         /* will be increased below */
1657         break;
1658       case (DHCP_OPTION_SUBNET_MASK):
1659         LWIP_DHCP_INPUT_ERROR("len == 4", len == 4, return ERR_VAL;);
1660         decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1661         break;
1662       case (DHCP_OPTION_ROUTER):
1663         decode_len = 4; /* only copy the first given router */
1664         LWIP_DHCP_INPUT_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1665         decode_idx = DHCP_OPTION_IDX_ROUTER;
1666         break;
1667 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
1668       case (DHCP_OPTION_DNS_SERVER):
1669         /* special case: there might be more than one server */
1670         LWIP_DHCP_INPUT_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
1671         /* limit number of DNS servers */
1672         decode_len = LWIP_MIN(len, 4 * LWIP_DHCP_PROVIDE_DNS_SERVERS);
1673         LWIP_DHCP_INPUT_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1674         decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1675         break;
1676 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
1677       case (DHCP_OPTION_LEASE_TIME):
1678         LWIP_DHCP_INPUT_ERROR("len == 4", len == 4, return ERR_VAL;);
1679         decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1680         break;
1681 #if LWIP_DHCP_GET_NTP_SRV
1682       case (DHCP_OPTION_NTP):
1683         /* special case: there might be more than one server */
1684         LWIP_DHCP_INPUT_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
1685         /* limit number of NTP servers */
1686         decode_len = LWIP_MIN(len, 4 * LWIP_DHCP_MAX_NTP_SERVERS);
1687         LWIP_DHCP_INPUT_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1688         decode_idx = DHCP_OPTION_IDX_NTP_SERVER;
1689         break;
1690 #endif /* LWIP_DHCP_GET_NTP_SRV*/
1691       case (DHCP_OPTION_OVERLOAD):
1692         LWIP_DHCP_INPUT_ERROR("len == 1", len == 1, return ERR_VAL;);
1693         /* decode overload only in options, not in file/sname: invalid packet */
1694         LWIP_DHCP_INPUT_ERROR("overload in file/sname", options_offset == DHCP_OPTIONS_OFS, return ERR_VAL;);
1695         decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1696         break;
1697       case (DHCP_OPTION_MESSAGE_TYPE):
1698         LWIP_DHCP_INPUT_ERROR("len == 1", len == 1, return ERR_VAL;);
1699         decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1700         break;
1701       case (DHCP_OPTION_SERVER_ID):
1702         LWIP_DHCP_INPUT_ERROR("len == 4", len == 4, return ERR_VAL;);
1703         decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1704         break;
1705       case (DHCP_OPTION_T1):
1706         LWIP_DHCP_INPUT_ERROR("len == 4", len == 4, return ERR_VAL;);
1707         decode_idx = DHCP_OPTION_IDX_T1;
1708         break;
1709       case (DHCP_OPTION_T2):
1710         LWIP_DHCP_INPUT_ERROR("len == 4", len == 4, return ERR_VAL;);
1711         decode_idx = DHCP_OPTION_IDX_T2;
1712         break;
1713       default:
1714         decode_len = 0;
1715         LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", (u16_t)op));
1716         LWIP_HOOK_DHCP_PARSE_OPTION(ip_current_netif(), dhcp, dhcp->state, msg_in,
1717                                     dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) ? (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) : 0,
1718                                     op, len, q, val_offset);
1719         break;
1720     }
1721     if (op == DHCP_OPTION_PAD) {
1722       offset++;
1723     } else {
1724       if (offset + len + 2 > 0xFFFF) {
1725         /* overflow */
1726         return ERR_BUF;
1727       }
1728       offset = (u16_t)(offset + len + 2);
1729       if (decode_len > 0) {
1730         u32_t value = 0;
1731         u16_t copy_len;
1732 decode_next:
1733         LWIP_ASSERT("check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1734         if (!dhcp_option_given(dhcp, decode_idx)) {
1735           copy_len = LWIP_MIN(decode_len, 4);
1736           if (pbuf_copy_partial(q, &value, copy_len, val_offset) != copy_len) {
1737             return ERR_BUF;
1738           }
1739           if (decode_len > 4) {
1740             /* decode more than one u32_t */
1741             u16_t next_val_offset;
1742             LWIP_DHCP_INPUT_ERROR("decode_len %% 4 == 0", decode_len % 4 == 0, return ERR_VAL;);
1743             dhcp_got_option(dhcp, decode_idx);
1744             dhcp_set_option_value(dhcp, decode_idx, lwip_htonl(value));
1745             decode_len = (u8_t)(decode_len - 4);
1746             next_val_offset = (u16_t)(val_offset + 4);
1747             if (next_val_offset < val_offset) {
1748               /* overflow */
1749               return ERR_BUF;
1750             }
1751             val_offset = next_val_offset;
1752             decode_idx++;
1753             goto decode_next;
1754           } else if (decode_len == 4) {
1755             value = lwip_ntohl(value);
1756           } else {
1757             LWIP_DHCP_INPUT_ERROR("invalid decode_len", decode_len == 1, return ERR_VAL;);
1758             value = ((u8_t *)&value)[0];
1759           }
1760           dhcp_got_option(dhcp, decode_idx);
1761           dhcp_set_option_value(dhcp, decode_idx, value);
1762         }
1763       }
1764     }
1765     if (offset >= q->len) {
1766       offset = (u16_t)(offset - q->len);
1767       offset_max = (u16_t)(offset_max - q->len);
1768       if (offset < offset_max) {
1769         q = q->next;
1770         LWIP_DHCP_INPUT_ERROR("next pbuf was null", q != NULL, return ERR_VAL;);
1771         options = (u8_t *)q->payload;
1772       } else {
1773         /* We've run out of bytes, probably no end marker. Don't proceed. */
1774         return ERR_BUF;
1775       }
1776     }
1777   }
1778   /* is this an overloaded message? */
1779   if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1780     u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1781     dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1782     if (overload == DHCP_OVERLOAD_FILE) {
1783       parse_file_as_options = 1;
1784       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded file field\n"));
1785     } else if (overload == DHCP_OVERLOAD_SNAME) {
1786       parse_sname_as_options = 1;
1787       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname field\n"));
1788     } else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
1789       parse_sname_as_options = 1;
1790       parse_file_as_options = 1;
1791       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname and file field\n"));
1792     } else {
1793       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("invalid overload option: %d\n", (int)overload));
1794     }
1795   }
1796   if (parse_file_as_options) {
1797     /* if both are overloaded, parse file first and then sname (RFC 2131 ch. 4.1) */
1798     parse_file_as_options = 0;
1799     options_idx = DHCP_FILE_OFS;
1800     options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
1801 #if LWIP_DHCP_BOOTP_FILE
1802     file_overloaded = 1;
1803 #endif
1804     goto again;
1805   } else if (parse_sname_as_options) {
1806     parse_sname_as_options = 0;
1807     options_idx = DHCP_SNAME_OFS;
1808     options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
1809     goto again;
1810   }
1811 #if LWIP_DHCP_BOOTP_FILE
1812   if (!file_overloaded) {
1813     /* only do this for ACK messages */
1814     if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1815       (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
1816     /* copy bootp file name, don't care for sname (server hostname) */
1817     if (pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN-1, DHCP_FILE_OFS) != (DHCP_FILE_LEN-1)) {
1818       return ERR_BUF;
1819     }
1820     /* make sure the string is really NULL-terminated */
1821     dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
1822   }
1823 #endif /* LWIP_DHCP_BOOTP_FILE */
1824   return ERR_OK;
1825 }
1826 
1827 /**
1828  * If an incoming DHCP message is in response to us, then trigger the state machine
1829  */
1830 static void
1831 dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
1832 {
1833   struct netif *netif = ip_current_input_netif();
1834   struct dhcp *dhcp = netif_dhcp_data(netif);
1835   struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
1836   u8_t msg_type;
1837   u8_t i;
1838   struct dhcp_msg *msg_in;
1839 
1840   LWIP_UNUSED_ARG(arg);
1841 
1842   /* Caught DHCP message from netif that does not have DHCP enabled? -> not interested */
1843   if ((dhcp == NULL) || (dhcp->pcb_allocated == 0)) {
1844     goto free_pbuf_and_return;
1845   }
1846 
1847   LWIP_ASSERT("invalid server address type", IP_IS_V4(addr));
1848 
1849   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void *)p,
1850               ip4_addr1_16(ip_2_ip4(addr)), ip4_addr2_16(ip_2_ip4(addr)), ip4_addr3_16(ip_2_ip4(addr)), ip4_addr4_16(ip_2_ip4(addr)), port));
1851   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
1852   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
1853   /* prevent warnings about unused arguments */
1854   LWIP_UNUSED_ARG(pcb);
1855   LWIP_UNUSED_ARG(addr);
1856   LWIP_UNUSED_ARG(port);
1857 
1858   if (p->len < DHCP_MIN_REPLY_LEN) {
1859     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP reply message or pbuf too short\n"));
1860     goto free_pbuf_and_return;
1861   }
1862 
1863   if (reply_msg->op != DHCP_BOOTREPLY) {
1864     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
1865     goto free_pbuf_and_return;
1866   }
1867   /* iterate through hardware address and match against DHCP message */
1868   for (i = 0; i < netif->hwaddr_len && i < LWIP_MIN(DHCP_CHADDR_LEN, NETIF_MAX_HWADDR_LEN); i++) {
1869     if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
1870       LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
1871                   ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
1872                    (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
1873       goto free_pbuf_and_return;
1874     }
1875   }
1876   /* match transaction ID against what we expected */
1877   if (lwip_ntohl(reply_msg->xid) != dhcp->xid) {
1878     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
1879                 ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n", lwip_ntohl(reply_msg->xid), dhcp->xid));
1880     goto free_pbuf_and_return;
1881   }
1882   /* option fields could be unfold? */
1883   if (dhcp_parse_reply(p, dhcp) != ERR_OK) {
1884     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1885                 ("problem unfolding DHCP message - too short on memory?\n"));
1886     goto free_pbuf_and_return;
1887   }
1888 
1889   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
1890   /* obtain pointer to DHCP message type */
1891   if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1892     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
1893     goto free_pbuf_and_return;
1894   }
1895 
1896   msg_in = (struct dhcp_msg *)p->payload;
1897   /* read DHCP message type */
1898   msg_type = (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1899   /* message type is DHCP ACK? */
1900   if (msg_type == DHCP_ACK) {
1901     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_ACK received\n"));
1902     /* in requesting state or just reconnected to the network? */
1903     if ((dhcp->state == DHCP_STATE_REQUESTING) ||
1904         (dhcp->state == DHCP_STATE_REBOOTING)) {
1905       dhcp_handle_ack(netif, msg_in);
1906 #if LWIP_DHCP_DOES_ACD_CHECK
1907       if ((netif->flags & NETIF_FLAG_ETHARP) != 0) {
1908         /* check if the acknowledged lease address is already in use */
1909         dhcp_check(netif);
1910       } else {
1911         /* bind interface to the acknowledged lease address */
1912         dhcp_bind(netif);
1913       }
1914 #else
1915       /* bind interface to the acknowledged lease address */
1916       dhcp_bind(netif);
1917 #endif
1918     }
1919     /* already bound to the given lease address and using it? */
1920     else if ((dhcp->state == DHCP_STATE_REBINDING) ||
1921              (dhcp->state == DHCP_STATE_RENEWING)) {
1922       dhcp_handle_ack(netif, msg_in);
1923       dhcp_bind(netif);
1924     }
1925   }
1926   /* received a DHCP_NAK in appropriate state? */
1927   else if ((msg_type == DHCP_NAK) &&
1928            ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REQUESTING) ||
1929             (dhcp->state == DHCP_STATE_REBINDING) || (dhcp->state == DHCP_STATE_RENEWING  ))) {
1930     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_NAK received\n"));
1931     dhcp_handle_nak(netif);
1932   }
1933   /* received a DHCP_OFFER in DHCP_STATE_SELECTING state? */
1934   else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_STATE_SELECTING)) {
1935     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_OFFER received in DHCP_STATE_SELECTING state\n"));
1936     /* remember offered lease */
1937     dhcp_handle_offer(netif, msg_in);
1938   }
1939 
1940 free_pbuf_and_return:
1941   pbuf_free(p);
1942 }
1943 
1944 /**
1945  * Create a DHCP request, fill in common headers
1946  *
1947  * @param netif the netif under DHCP control
1948  * @param dhcp dhcp control struct
1949  * @param message_type message type of the request
1950  */
1951 static struct pbuf *
1952 dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type, u16_t *options_out_len)
1953 {
1954   u16_t i;
1955   struct pbuf *p_out;
1956   struct dhcp_msg *msg_out;
1957   u16_t options_out_len_loc;
1958 
1959 #ifndef DHCP_GLOBAL_XID
1960   /** default global transaction identifier starting value (easy to match
1961    *  with a packet analyser). We simply increment for each new request.
1962    *  Predefine DHCP_GLOBAL_XID to a better value or a function call to generate one
1963    *  at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */
1964 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1965   static u32_t xid;
1966 #else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1967   static u32_t xid = 0xABCD0000;
1968 #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1969 #else
1970   if (!xid_initialised) {
1971     xid = DHCP_GLOBAL_XID;
1972     xid_initialised = !xid_initialised;
1973   }
1974 #endif
1975   LWIP_ERROR("dhcp_create_msg: netif != NULL", (netif != NULL), return NULL;);
1976   LWIP_ERROR("dhcp_create_msg: dhcp != NULL", (dhcp != NULL), return NULL;);
1977   p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
1978   if (p_out == NULL) {
1979     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1980                 ("dhcp_create_msg(): could not allocate pbuf\n"));
1981     return NULL;
1982   }
1983   LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1984               (p_out->len >= sizeof(struct dhcp_msg)));
1985 
1986   /* DHCP_REQUEST should reuse 'xid' from DHCPOFFER */
1987   if ((message_type != DHCP_REQUEST) || (dhcp->state == DHCP_STATE_REBOOTING)) {
1988     /* reuse transaction identifier in retransmissions */
1989     if (dhcp->tries == 0) {
1990 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1991       xid = LWIP_RAND();
1992 #else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1993       xid++;
1994 #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1995     }
1996     dhcp->xid = xid;
1997   }
1998   LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE,
1999               ("transaction id xid(%"X32_F")\n", xid));
2000 
2001   msg_out = (struct dhcp_msg *)p_out->payload;
2002   memset(msg_out, 0, sizeof(struct dhcp_msg));
2003 
2004   msg_out->op = DHCP_BOOTREQUEST;
2005   /* @todo: make link layer independent */
2006   msg_out->htype = LWIP_IANA_HWTYPE_ETHERNET;
2007   msg_out->hlen = netif->hwaddr_len;
2008   msg_out->xid = lwip_htonl(dhcp->xid);
2009   /* we don't need the broadcast flag since we can receive unicast traffic
2010      before being fully configured! */
2011   /* set ciaddr to netif->ip_addr based on message_type and state */
2012   if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || (message_type == DHCP_RELEASE) ||
2013       ((message_type == DHCP_REQUEST) && /* DHCP_STATE_BOUND not used for sending! */
2014        ((dhcp->state == DHCP_STATE_RENEWING) || dhcp->state == DHCP_STATE_REBINDING))) {
2015     ip4_addr_copy(msg_out->ciaddr, *netif_ip4_addr(netif));
2016   }
2017   for (i = 0; i < LWIP_MIN(DHCP_CHADDR_LEN, NETIF_MAX_HWADDR_LEN); i++) {
2018     /* copy netif hardware address (padded with zeroes through memset already) */
2019     msg_out->chaddr[i] = netif->hwaddr[i];
2020   }
2021   msg_out->cookie = PP_HTONL(DHCP_MAGIC_COOKIE);
2022   /* Add option MESSAGE_TYPE */
2023   options_out_len_loc = dhcp_option(0, msg_out->options, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
2024   options_out_len_loc = dhcp_option_byte(options_out_len_loc, msg_out->options, message_type);
2025   if (options_out_len) {
2026     *options_out_len = options_out_len_loc;
2027   }
2028   return p_out;
2029 }
2030 
2031 /**
2032  * Add a DHCP message trailer
2033  *
2034  * Adds the END option to the DHCP message, and if
2035  * necessary, up to three padding bytes.
2036  */
2037 static void
2038 dhcp_option_trailer(u16_t options_out_len, u8_t *options, struct pbuf *p_out)
2039 {
2040   options[options_out_len++] = DHCP_OPTION_END;
2041   /* packet is too small, or not 4 byte aligned? */
2042   while (((options_out_len < DHCP_MIN_OPTIONS_LEN) || (options_out_len & 3)) &&
2043          (options_out_len < DHCP_OPTIONS_LEN)) {
2044     /* add a fill/padding byte */
2045     options[options_out_len++] = 0;
2046   }
2047   /* shrink the pbuf to the actual content length */
2048   pbuf_realloc(p_out, (u16_t)(sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + options_out_len));
2049 }
2050 
2051 /** check if DHCP supplied netif->ip_addr
2052  *
2053  * @param netif the netif to check
2054  * @return 1 if DHCP supplied netif->ip_addr (states BOUND or RENEWING),
2055  *         0 otherwise
2056  */
2057 u8_t
2058 dhcp_supplied_address(const struct netif *netif)
2059 {
2060   if ((netif != NULL) && (netif_dhcp_data(netif) != NULL)) {
2061     struct dhcp *dhcp = netif_dhcp_data(netif);
2062     return (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING) ||
2063            (dhcp->state == DHCP_STATE_REBINDING);
2064   }
2065   return 0;
2066 }
2067 
2068 #endif /* LWIP_IPV4 && LWIP_DHCP */
2069