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