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 <leon.woestenberg@gmx.net>
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 <leon.woestenberg@gmx.net>
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;
412 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
413 /* iterate through all network interfaces */
414 NETIF_FOREACH(netif) {
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 }
437 }
438
439 /**
440 * DHCP transaction timeout handling (this function must be called every 500ms,
441 * see @ref DHCP_FINE_TIMER_MSECS).
442 *
443 * A DHCP server is expected to respond within a short period of time.
444 * This timer checks whether an outstanding DHCP request is timed out.
445 */
446 void
dhcp_fine_tmr(void)447 dhcp_fine_tmr(void)
448 {
449 struct netif *netif;
450 /* loop through netif's */
451 NETIF_FOREACH(netif) {
452 struct dhcp *dhcp = netif_dhcp_data(netif);
453 /* only act on DHCP configured interfaces */
454 if (dhcp != NULL) {
455 /* timer is active (non zero), and is about to trigger now */
456 if (dhcp->request_timeout > 1) {
457 dhcp->request_timeout--;
458 }
459 else if (dhcp->request_timeout == 1) {
460 dhcp->request_timeout--;
461 /* { netif->dhcp->request_timeout == 0 } */
462 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
463 /* this client's request timeout triggered */
464 dhcp_timeout(netif);
465 }
466 }
467 }
468 }
469
470 /**
471 * A DHCP negotiation transaction, or ARP request, has timed out.
472 *
473 * The timer that was started with the DHCP or ARP request has
474 * timed out, indicating no response was received in time.
475 *
476 * @param netif the netif under DHCP control
477 */
478 static void
dhcp_timeout(struct netif * netif)479 dhcp_timeout(struct netif *netif)
480 {
481 struct dhcp *dhcp = netif_dhcp_data(netif);
482
483 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n"));
484 /* back-off period has passed, or server selection timed out */
485 if ((dhcp->state == DHCP_STATE_BACKING_OFF) || (dhcp->state == DHCP_STATE_SELECTING)) {
486 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
487 dhcp_discover(netif);
488 /* receiving the requested lease timed out */
489 } else if (dhcp->state == DHCP_STATE_REQUESTING) {
490 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
491 if (dhcp->tries <= 5) {
492 dhcp_select(netif);
493 } else {
494 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
495 dhcp_release(netif);
496 dhcp_discover(netif);
497 }
498 #if DHCP_DOES_ARP_CHECK
499 /* received no ARP reply for the offered address (which is good) */
500 } else if (dhcp->state == DHCP_STATE_CHECKING) {
501 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
502 if (dhcp->tries <= 1) {
503 dhcp_check(netif);
504 /* no ARP replies on the offered address,
505 looks like the IP address is indeed free */
506 } else {
507 /* bind the interface to the offered address */
508 dhcp_bind(netif);
509 }
510 #endif /* DHCP_DOES_ARP_CHECK */
511 } else if (dhcp->state == DHCP_STATE_REBOOTING) {
512 if (dhcp->tries < REBOOT_TRIES) {
513 dhcp_reboot(netif);
514 } else {
515 dhcp_discover(netif);
516 }
517 }
518 }
519
520 /**
521 * The renewal period has timed out.
522 *
523 * @param netif the netif under DHCP control
524 */
525 static void
dhcp_t1_timeout(struct netif * netif)526 dhcp_t1_timeout(struct netif *netif)
527 {
528 struct dhcp *dhcp = netif_dhcp_data(netif);
529
530 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
531 if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
532 (dhcp->state == DHCP_STATE_RENEWING)) {
533 /* just retry to renew - note that the rebind timer (t2) will
534 * eventually time-out if renew tries fail. */
535 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
536 ("dhcp_t1_timeout(): must renew\n"));
537 /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
538 DHCP_STATE_RENEWING, not DHCP_STATE_BOUND */
539 dhcp_renew(netif);
540 /* Calculate next timeout */
541 if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
542 {
543 dhcp->t1_renew_time = ((dhcp->t2_timeout - dhcp->lease_used) / 2);
544 }
545 }
546 }
547
548 /**
549 * The rebind period has timed out.
550 *
551 * @param netif the netif under DHCP control
552 */
553 static void
dhcp_t2_timeout(struct netif * netif)554 dhcp_t2_timeout(struct netif *netif)
555 {
556 struct dhcp *dhcp = netif_dhcp_data(netif);
557
558 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
559 if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
560 (dhcp->state == DHCP_STATE_RENEWING) || (dhcp->state == DHCP_STATE_REBINDING)) {
561 /* just retry to rebind */
562 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
563 ("dhcp_t2_timeout(): must rebind\n"));
564 /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
565 DHCP_STATE_REBINDING, not DHCP_STATE_BOUND */
566 dhcp_rebind(netif);
567 /* Calculate next timeout */
568 if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
569 {
570 dhcp->t2_rebind_time = ((dhcp->t0_timeout - dhcp->lease_used) / 2);
571 }
572 }
573 }
574
575 /**
576 * Handle a DHCP ACK packet
577 *
578 * @param netif the netif under DHCP control
579 */
580 static void
dhcp_handle_ack(struct netif * netif)581 dhcp_handle_ack(struct netif *netif)
582 {
583 struct dhcp *dhcp = netif_dhcp_data(netif);
584
585 #if LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV
586 u8_t n;
587 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV */
588 #if LWIP_DHCP_GET_NTP_SRV
589 ip4_addr_t ntp_server_addrs[LWIP_DHCP_MAX_NTP_SERVERS];
590 #endif
591
592 /* clear options we might not get from the ACK */
593 ip4_addr_set_zero(&dhcp->offered_sn_mask);
594 ip4_addr_set_zero(&dhcp->offered_gw_addr);
595 #if LWIP_DHCP_BOOTP_FILE
596 ip4_addr_set_zero(&dhcp->offered_si_addr);
597 #endif /* LWIP_DHCP_BOOTP_FILE */
598
599 /* lease time given? */
600 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
601 /* remember offered lease time */
602 dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
603 }
604 /* renewal period given? */
605 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
606 /* remember given renewal period */
607 dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
608 } else {
609 /* calculate safe periods for renewal */
610 dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
611 }
612
613 /* renewal period given? */
614 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
615 /* remember given rebind period */
616 dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
617 } else {
618 /* calculate safe periods for rebinding (offered_t0_lease * 0.875 -> 87.5%)*/
619 dhcp->offered_t2_rebind = (dhcp->offered_t0_lease * 7U) / 8U;
620 }
621
622 /* (y)our internet address */
623 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
624
625 #if LWIP_DHCP_BOOTP_FILE
626 /* copy boot server address,
627 boot file name copied in dhcp_parse_reply if not overloaded */
628 ip4_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
629 #endif /* LWIP_DHCP_BOOTP_FILE */
630
631 /* subnet mask given? */
632 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
633 /* remember given subnet mask */
634 ip4_addr_set_u32(&dhcp->offered_sn_mask, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
635 dhcp->subnet_mask_given = 1;
636 } else {
637 dhcp->subnet_mask_given = 0;
638 }
639
640 /* gateway router */
641 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
642 ip4_addr_set_u32(&dhcp->offered_gw_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
643 }
644
645 #if LWIP_DHCP_GET_NTP_SRV
646 /* NTP servers */
647 for (n = 0; (n < LWIP_DHCP_MAX_NTP_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n); n++) {
648 ip4_addr_set_u32(&ntp_server_addrs[n], lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n)));
649 }
650 dhcp_set_ntp_servers(n, ntp_server_addrs);
651 #endif /* LWIP_DHCP_GET_NTP_SRV */
652
653 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
654 /* DNS servers */
655 for (n = 0; (n < LWIP_DHCP_PROVIDE_DNS_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n); n++) {
656 ip_addr_t dns_addr;
657 ip_addr_set_ip4_u32(&dns_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
658 dns_setserver(n, &dns_addr);
659 }
660 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
661 }
662
663 /**
664 * @ingroup dhcp4
665 * Set a statically allocated struct dhcp to work with.
666 * Using this prevents dhcp_start to allocate it using mem_malloc.
667 *
668 * @param netif the netif for which to set the struct dhcp
669 * @param dhcp (uninitialised) dhcp struct allocated by the application
670 */
671 void
dhcp_set_struct(struct netif * netif,struct dhcp * dhcp)672 dhcp_set_struct(struct netif *netif, struct dhcp *dhcp)
673 {
674 LWIP_ASSERT("netif != NULL", netif != NULL);
675 LWIP_ASSERT("dhcp != NULL", dhcp != NULL);
676 LWIP_ASSERT("netif already has a struct dhcp set", netif_dhcp_data(netif) == NULL);
677
678 /* clear data structure */
679 memset(dhcp, 0, sizeof(struct dhcp));
680 /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */
681 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
682 }
683
684 /**
685 * @ingroup dhcp4
686 * Removes a struct dhcp from a netif.
687 *
688 * ATTENTION: Only use this when not using dhcp_set_struct() to allocate the
689 * struct dhcp since the memory is passed back to the heap.
690 *
691 * @param netif the netif from which to remove the struct dhcp
692 */
dhcp_cleanup(struct netif * netif)693 void dhcp_cleanup(struct netif *netif)
694 {
695 LWIP_ASSERT("netif != NULL", netif != NULL);
696
697 if (netif_dhcp_data(netif) != NULL) {
698 mem_free(netif_dhcp_data(netif));
699 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, NULL);
700 }
701 }
702
703 /**
704 * @ingroup dhcp4
705 * Start DHCP negotiation for a network interface.
706 *
707 * If no DHCP client instance was attached to this interface,
708 * a new client is created first. If a DHCP client instance
709 * was already present, it restarts negotiation.
710 *
711 * @param netif The lwIP network interface
712 * @return lwIP error code
713 * - ERR_OK - No error
714 * - ERR_MEM - Out of memory
715 */
716 err_t
dhcp_start(struct netif * netif)717 dhcp_start(struct netif *netif)
718 {
719 struct dhcp *dhcp;
720 err_t result;
721
722 LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
723 LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
724 dhcp = netif_dhcp_data(netif);
725 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));
726
727 /* check MTU of the netif */
728 if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
729 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
730 return ERR_MEM;
731 }
732
733 /* no DHCP client attached yet? */
734 if (dhcp == NULL) {
735 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
736 dhcp = (struct dhcp *)mem_malloc(sizeof(struct dhcp));
737 if (dhcp == NULL) {
738 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
739 return ERR_MEM;
740 }
741
742 /* store this dhcp client in the netif */
743 netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
744 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp"));
745 /* already has DHCP client attached */
746 } else {
747 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(): restarting DHCP configuration\n"));
748 LWIP_ASSERT("pbuf p_out wasn't freed", dhcp->p_out == NULL);
749 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL );
750
751 if (dhcp->pcb_allocated != 0) {
752 dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
753 }
754 /* dhcp is cleared below, no need to reset flag*/
755 }
756
757 /* clear data structure */
758 memset(dhcp, 0, sizeof(struct dhcp));
759 /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */
760
761 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
762
763 if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */
764 return ERR_MEM;
765 }
766 dhcp->pcb_allocated = 1;
767
768 #if LWIP_DHCP_CHECK_LINK_UP
769 if (!netif_is_link_up(netif)) {
770 /* set state INIT and wait for dhcp_network_changed() to call dhcp_discover() */
771 dhcp_set_state(dhcp, DHCP_STATE_INIT);
772 return ERR_OK;
773 }
774 #endif /* LWIP_DHCP_CHECK_LINK_UP */
775
776
777 /* (re)start the DHCP negotiation */
778 result = dhcp_discover(netif);
779 if (result != ERR_OK) {
780 /* free resources allocated above */
781 dhcp_stop(netif);
782 return ERR_MEM;
783 }
784 return result;
785 }
786
787 /**
788 * @ingroup dhcp4
789 * Inform a DHCP server of our manual configuration.
790 *
791 * This informs DHCP servers of our fixed IP address configuration
792 * by sending an INFORM message. It does not involve DHCP address
793 * configuration, it is just here to be nice to the network.
794 *
795 * @param netif The lwIP network interface
796 */
797 void
dhcp_inform(struct netif * netif)798 dhcp_inform(struct netif *netif)
799 {
800 struct dhcp dhcp;
801 err_t result = ERR_OK;
802
803 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
804
805 if (dhcp_inc_pcb_refcount() != ERR_OK) { /* ensure DHCP PCB is allocated */
806 return;
807 }
808
809 memset(&dhcp, 0, sizeof(struct dhcp));
810 dhcp_set_state(&dhcp, DHCP_STATE_INFORMING);
811
812 /* create and initialize the DHCP message header */
813 result = dhcp_create_msg(netif, &dhcp, DHCP_INFORM);
814 if (result == ERR_OK) {
815 dhcp_option(&dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
816 dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(netif));
817
818 dhcp_option_trailer(&dhcp);
819
820 pbuf_realloc(dhcp.p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp.options_out_len);
821
822 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
823
824 udp_sendto_if(dhcp_pcb, dhcp.p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
825
826 dhcp_delete_msg(&dhcp);
827 } else {
828 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n"));
829 }
830
831 dhcp_dec_pcb_refcount(); /* delete DHCP PCB if not needed any more */
832 }
833
834 /** Handle a possible change in the network configuration.
835 *
836 * This enters the REBOOTING state to verify that the currently bound
837 * address is still valid.
838 */
839 void
dhcp_network_changed(struct netif * netif)840 dhcp_network_changed(struct netif *netif)
841 {
842 struct dhcp *dhcp = netif_dhcp_data(netif);
843
844 if (!dhcp) {
845 return;
846 }
847 switch (dhcp->state) {
848 case DHCP_STATE_REBINDING:
849 case DHCP_STATE_RENEWING:
850 case DHCP_STATE_BOUND:
851 case DHCP_STATE_REBOOTING:
852 dhcp->tries = 0;
853 dhcp_reboot(netif);
854 break;
855 case DHCP_STATE_OFF:
856 /* stay off */
857 break;
858 default:
859 LWIP_ASSERT("invalid dhcp->state", dhcp->state <= DHCP_STATE_BACKING_OFF);
860 /* INIT/REQUESTING/CHECKING/BACKING_OFF restart with new 'rid' because the
861 state changes, SELECTING: continue with current 'rid' as we stay in the
862 same state */
863 #if LWIP_DHCP_AUTOIP_COOP
864 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
865 autoip_stop(netif);
866 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
867 }
868 #endif /* LWIP_DHCP_AUTOIP_COOP */
869 /* ensure we start with short timeouts, even if already discovering */
870 dhcp->tries = 0;
871 dhcp_discover(netif);
872 break;
873 }
874 }
875
876 #if DHCP_DOES_ARP_CHECK
877 /**
878 * Match an ARP reply with the offered IP address:
879 * check whether the offered IP address is not in use using ARP
880 *
881 * @param netif the network interface on which the reply was received
882 * @param addr The IP address we received a reply from
883 */
884 void
dhcp_arp_reply(struct netif * netif,const ip4_addr_t * addr)885 dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr)
886 {
887 struct dhcp *dhcp;
888
889 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
890 dhcp = netif_dhcp_data(netif);
891 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n"));
892 /* is a DHCP client doing an ARP check? */
893 if ((dhcp != NULL) && (dhcp->state == DHCP_STATE_CHECKING)) {
894 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n",
895 ip4_addr_get_u32(addr)));
896 /* did a host respond with the address we
897 were offered by the DHCP server? */
898 if (ip4_addr_cmp(addr, &dhcp->offered_ip_addr)) {
899 /* we will not accept the offered address */
900 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,
901 ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
902 dhcp_decline(netif);
903 }
904 }
905 }
906
907 /**
908 * Decline an offered lease.
909 *
910 * Tell the DHCP server we do not accept the offered address.
911 * One reason to decline the lease is when we find out the address
912 * is already in use by another host (through ARP).
913 *
914 * @param netif the netif under DHCP control
915 */
916 static err_t
dhcp_decline(struct netif * netif)917 dhcp_decline(struct netif *netif)
918 {
919 struct dhcp *dhcp = netif_dhcp_data(netif);
920 err_t result = ERR_OK;
921 u16_t msecs;
922 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));
923 dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
924 /* create and initialize the DHCP message header */
925 result = dhcp_create_msg(netif, dhcp, DHCP_DECLINE);
926 if (result == ERR_OK) {
927 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
928 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
929
930 dhcp_option_trailer(dhcp);
931 /* resize pbuf to reflect true size of options */
932 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
933
934 /* per section 4.4.4, broadcast DECLINE messages */
935 udp_sendto_if_src(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP4_ADDR_ANY);
936 dhcp_delete_msg(dhcp);
937 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
938 } else {
939 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
940 ("dhcp_decline: could not allocate DHCP request\n"));
941 }
942 if (dhcp->tries < 255) {
943 dhcp->tries++;
944 }
945 msecs = 10*1000;
946 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
947 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
948 return result;
949 }
950 #endif /* DHCP_DOES_ARP_CHECK */
951
952
953 /**
954 * Start the DHCP process, discover a DHCP server.
955 *
956 * @param netif the netif under DHCP control
957 */
958 static err_t
dhcp_discover(struct netif * netif)959 dhcp_discover(struct netif *netif)
960 {
961 struct dhcp *dhcp = netif_dhcp_data(netif);
962 err_t result = ERR_OK;
963 u16_t msecs;
964 u8_t i;
965 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover()\n"));
966 ip4_addr_set_any(&dhcp->offered_ip_addr);
967 dhcp_set_state(dhcp, DHCP_STATE_SELECTING);
968 /* create and initialize the DHCP message header */
969 result = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER);
970 if (result == ERR_OK) {
971 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
972
973 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
974 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
975
976 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
977 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
978 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
979 }
980 dhcp_option_trailer(dhcp);
981
982 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
983 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
984
985 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
986 udp_sendto_if_src(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif, IP4_ADDR_ANY);
987 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
988 dhcp_delete_msg(dhcp);
989 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
990 } else {
991 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n"));
992 }
993 if (dhcp->tries < 255) {
994 dhcp->tries++;
995 }
996 #if LWIP_DHCP_AUTOIP_COOP
997 if (dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {
998 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
999 autoip_start(netif);
1000 }
1001 #endif /* LWIP_DHCP_AUTOIP_COOP */
1002 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
1003 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1004 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
1005 return result;
1006 }
1007
1008
1009 /**
1010 * Bind the interface to the offered IP address.
1011 *
1012 * @param netif network interface to bind to the offered address
1013 */
1014 static void
dhcp_bind(struct netif * netif)1015 dhcp_bind(struct netif *netif)
1016 {
1017 u32_t timeout;
1018 struct dhcp *dhcp;
1019 ip4_addr_t sn_mask, gw_addr;
1020 LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
1021 dhcp = netif_dhcp_data(netif);
1022 LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
1023 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));
1024
1025 /* reset time used of lease */
1026 dhcp->lease_used = 0;
1027
1028 if (dhcp->offered_t0_lease != 0xffffffffUL) {
1029 /* set renewal period timer */
1030 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease));
1031 timeout = (dhcp->offered_t0_lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1032 if (timeout > 0xffff) {
1033 timeout = 0xffff;
1034 }
1035 dhcp->t0_timeout = (u16_t)timeout;
1036 if (dhcp->t0_timeout == 0) {
1037 dhcp->t0_timeout = 1;
1038 }
1039 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t0_lease*1000));
1040 }
1041
1042 /* temporary DHCP lease? */
1043 if (dhcp->offered_t1_renew != 0xffffffffUL) {
1044 /* set renewal period timer */
1045 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
1046 timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1047 if (timeout > 0xffff) {
1048 timeout = 0xffff;
1049 }
1050 dhcp->t1_timeout = (u16_t)timeout;
1051 if (dhcp->t1_timeout == 0) {
1052 dhcp->t1_timeout = 1;
1053 }
1054 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
1055 dhcp->t1_renew_time = dhcp->t1_timeout;
1056 }
1057 /* set renewal period timer */
1058 if (dhcp->offered_t2_rebind != 0xffffffffUL) {
1059 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
1060 timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1061 if (timeout > 0xffff) {
1062 timeout = 0xffff;
1063 }
1064 dhcp->t2_timeout = (u16_t)timeout;
1065 if (dhcp->t2_timeout == 0) {
1066 dhcp->t2_timeout = 1;
1067 }
1068 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
1069 dhcp->t2_rebind_time = dhcp->t2_timeout;
1070 }
1071
1072 /* If we have sub 1 minute lease, t2 and t1 will kick in at the same time. */
1073 if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1074 dhcp->t1_timeout = 0;
1075 }
1076
1077 if (dhcp->subnet_mask_given) {
1078 /* copy offered network mask */
1079 ip4_addr_copy(sn_mask, dhcp->offered_sn_mask);
1080 } else {
1081 /* subnet mask not given, choose a safe subnet mask given the network class */
1082 u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
1083 if (first_octet <= 127) {
1084 ip4_addr_set_u32(&sn_mask, PP_HTONL(0xff000000UL));
1085 } else if (first_octet >= 192) {
1086 ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffffff00UL));
1087 } else {
1088 ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffff0000UL));
1089 }
1090 }
1091
1092 ip4_addr_copy(gw_addr, dhcp->offered_gw_addr);
1093 /* gateway address not given? */
1094 if (ip4_addr_isany_val(gw_addr)) {
1095 /* copy network address */
1096 ip4_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
1097 /* use first host address on network as gateway */
1098 ip4_addr_set_u32(&gw_addr, ip4_addr_get_u32(&gw_addr) | PP_HTONL(0x00000001UL));
1099 }
1100
1101 #if LWIP_DHCP_AUTOIP_COOP
1102 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1103 autoip_stop(netif);
1104 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1105 }
1106 #endif /* LWIP_DHCP_AUTOIP_COOP */
1107
1108 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",
1109 ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr)));
1110 /* netif is now bound to DHCP leased address - set this before assigning the address
1111 to ensure the callback can use dhcp_supplied_address() */
1112 dhcp_set_state(dhcp, DHCP_STATE_BOUND);
1113
1114 netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
1115 /* interface is used by routing now that an address is set */
1116 }
1117
1118 /**
1119 * @ingroup dhcp4
1120 * Renew an existing DHCP lease at the involved DHCP server.
1121 *
1122 * @param netif network interface which must renew its lease
1123 */
1124 err_t
dhcp_renew(struct netif * netif)1125 dhcp_renew(struct netif *netif)
1126 {
1127 struct dhcp *dhcp = netif_dhcp_data(netif);
1128 err_t result;
1129 u16_t msecs;
1130 u8_t i;
1131 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_renew()\n"));
1132 dhcp_set_state(dhcp, DHCP_STATE_RENEWING);
1133
1134 /* create and initialize the DHCP message header */
1135 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1136 if (result == ERR_OK) {
1137 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1138 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1139
1140 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1141 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1142 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1143 }
1144
1145 #if LWIP_NETIF_HOSTNAME
1146 dhcp_option_hostname(dhcp, netif);
1147 #endif /* LWIP_NETIF_HOSTNAME */
1148
1149 /* append DHCP message trailer */
1150 dhcp_option_trailer(dhcp);
1151
1152 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1153
1154 udp_sendto_if(dhcp_pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1155 dhcp_delete_msg(dhcp);
1156
1157 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
1158 } else {
1159 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n"));
1160 }
1161 if (dhcp->tries < 255) {
1162 dhcp->tries++;
1163 }
1164 /* back-off on retries, but to a maximum of 20 seconds */
1165 msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
1166 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1167 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
1168 return result;
1169 }
1170
1171 /**
1172 * Rebind with a DHCP server for an existing DHCP lease.
1173 *
1174 * @param netif network interface which must rebind with a DHCP server
1175 */
1176 static err_t
dhcp_rebind(struct netif * netif)1177 dhcp_rebind(struct netif *netif)
1178 {
1179 struct dhcp *dhcp = netif_dhcp_data(netif);
1180 err_t result;
1181 u16_t msecs;
1182 u8_t i;
1183 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
1184 dhcp_set_state(dhcp, DHCP_STATE_REBINDING);
1185
1186 /* create and initialize the DHCP message header */
1187 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1188 if (result == ERR_OK) {
1189 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1190 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1191
1192 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1193 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1194 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1195 }
1196
1197 #if LWIP_NETIF_HOSTNAME
1198 dhcp_option_hostname(dhcp, netif);
1199 #endif /* LWIP_NETIF_HOSTNAME */
1200
1201 dhcp_option_trailer(dhcp);
1202
1203 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1204
1205 /* broadcast to server */
1206 udp_sendto_if(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1207 dhcp_delete_msg(dhcp);
1208 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
1209 } else {
1210 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n"));
1211 }
1212 if (dhcp->tries < 255) {
1213 dhcp->tries++;
1214 }
1215 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1216 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1217 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
1218 return result;
1219 }
1220
1221 /**
1222 * Enter REBOOTING state to verify an existing lease
1223 *
1224 * @param netif network interface which must reboot
1225 */
1226 static err_t
dhcp_reboot(struct netif * netif)1227 dhcp_reboot(struct netif *netif)
1228 {
1229 struct dhcp *dhcp = netif_dhcp_data(netif);
1230 err_t result;
1231 u16_t msecs;
1232 u8_t i;
1233 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot()\n"));
1234 dhcp_set_state(dhcp, DHCP_STATE_REBOOTING);
1235
1236 /* create and initialize the DHCP message header */
1237 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1238 if (result == ERR_OK) {
1239 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1240 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN_MIN_REQUIRED);
1241
1242 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1243 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1244
1245 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
1246 for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1247 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1248 }
1249
1250 dhcp_option_trailer(dhcp);
1251
1252 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1253
1254 /* broadcast to server */
1255 udp_sendto_if(dhcp_pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1256 dhcp_delete_msg(dhcp);
1257 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n"));
1258 } else {
1259 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n"));
1260 }
1261 if (dhcp->tries < 255) {
1262 dhcp->tries++;
1263 }
1264 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1265 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1266 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
1267 return result;
1268 }
1269
1270 /**
1271 * @ingroup dhcp4
1272 * Release a DHCP lease and stop DHCP statemachine (and AUTOIP if LWIP_DHCP_AUTOIP_COOP).
1273 *
1274 * @param netif network interface
1275 */
1276 void
dhcp_release_and_stop(struct netif * netif)1277 dhcp_release_and_stop(struct netif *netif)
1278 {
1279 struct dhcp *dhcp = netif_dhcp_data(netif);
1280 ip_addr_t server_ip_addr;
1281
1282 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release_and_stop()\n"));
1283 if (dhcp == NULL) {
1284 return;
1285 }
1286
1287 /* already off? -> nothing to do */
1288 if (dhcp->state == DHCP_STATE_OFF) {
1289 return;
1290 }
1291
1292 ip_addr_copy(server_ip_addr, dhcp->server_ip_addr);
1293
1294 /* clean old DHCP offer */
1295 ip_addr_set_zero_ip4(&dhcp->server_ip_addr);
1296 ip4_addr_set_zero(&dhcp->offered_ip_addr);
1297 ip4_addr_set_zero(&dhcp->offered_sn_mask);
1298 ip4_addr_set_zero(&dhcp->offered_gw_addr);
1299 #if LWIP_DHCP_BOOTP_FILE
1300 ip4_addr_set_zero(&dhcp->offered_si_addr);
1301 #endif /* LWIP_DHCP_BOOTP_FILE */
1302 dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1303 dhcp->t1_renew_time = dhcp->t2_rebind_time = dhcp->lease_used = dhcp->t0_timeout = 0;
1304
1305 /* send release message when current IP was assigned via DHCP */
1306 if (dhcp_supplied_address(netif)) {
1307 /* create and initialize the DHCP message header */
1308 err_t result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);
1309 if (result == ERR_OK) {
1310 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
1311 dhcp_option_long(dhcp, lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr))));
1312
1313 dhcp_option_trailer(dhcp);
1314
1315 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1316
1317 udp_sendto_if(dhcp_pcb, dhcp->p_out, &server_ip_addr, DHCP_SERVER_PORT, netif);
1318 dhcp_delete_msg(dhcp);
1319 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_STATE_OFF\n"));
1320 } else {
1321 /* sending release failed, but that's not a problem since the correct behaviour of dhcp does not rely on release */
1322 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n"));
1323 }
1324 }
1325
1326 /* remove IP address from interface (prevents routing from selecting this interface) */
1327 netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
1328
1329 #if LWIP_DHCP_AUTOIP_COOP
1330 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1331 autoip_stop(netif);
1332 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1333 }
1334 #endif /* LWIP_DHCP_AUTOIP_COOP */
1335
1336 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
1337 dhcp_set_state(dhcp, DHCP_STATE_OFF);
1338
1339 if (dhcp->pcb_allocated != 0) {
1340 dhcp_dec_pcb_refcount(); /* free DHCP PCB if not needed any more */
1341 dhcp->pcb_allocated = 0;
1342 }
1343 }
1344
1345 /**
1346 * @ingroup dhcp4
1347 * @deprecated Use dhcp_release_and_stop() instead.
1348 * This function calls dhcp_release_and_stop() internally.
1349 */
1350 err_t
dhcp_release(struct netif * netif)1351 dhcp_release(struct netif *netif)
1352 {
1353 dhcp_release_and_stop(netif);
1354 return ERR_OK;
1355 }
1356
1357 /**
1358 * @ingroup dhcp4
1359 * @deprecated Use dhcp_release_and_stop() instead.
1360 * This function calls dhcp_release_and_stop() internally.
1361 */
1362 void
dhcp_stop(struct netif * netif)1363 dhcp_stop(struct netif *netif)
1364 {
1365 dhcp_release_and_stop(netif);
1366 }
1367
1368 /*
1369 * Set the DHCP state of a DHCP client.
1370 *
1371 * If the state changed, reset the number of tries.
1372 */
1373 static void
dhcp_set_state(struct dhcp * dhcp,u8_t new_state)1374 dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
1375 {
1376 if (new_state != dhcp->state) {
1377 dhcp->state = new_state;
1378 dhcp->tries = 0;
1379 dhcp->request_timeout = 0;
1380 }
1381 }
1382
1383 /*
1384 * Concatenate an option type and length field to the outgoing
1385 * DHCP message.
1386 *
1387 */
1388 static void
dhcp_option(struct dhcp * dhcp,u8_t option_type,u8_t option_len)1389 dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
1390 {
1391 LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
1392 dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
1393 dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
1394 }
1395 /*
1396 * Concatenate a single byte to the outgoing DHCP message.
1397 *
1398 */
1399 static void
dhcp_option_byte(struct dhcp * dhcp,u8_t value)1400 dhcp_option_byte(struct dhcp *dhcp, u8_t value)
1401 {
1402 LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1403 dhcp->msg_out->options[dhcp->options_out_len++] = value;
1404 }
1405
1406 static void
dhcp_option_short(struct dhcp * dhcp,u16_t value)1407 dhcp_option_short(struct dhcp *dhcp, u16_t value)
1408 {
1409 LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
1410 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
1411 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);
1412 }
1413
1414 static void
dhcp_option_long(struct dhcp * dhcp,u32_t value)1415 dhcp_option_long(struct dhcp *dhcp, u32_t value)
1416 {
1417 LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
1418 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
1419 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
1420 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
1421 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));
1422 }
1423
1424 #if LWIP_NETIF_HOSTNAME
1425 static void
dhcp_option_hostname(struct dhcp * dhcp,struct netif * netif)1426 dhcp_option_hostname(struct dhcp *dhcp, struct netif *netif)
1427 {
1428 if (netif->hostname != NULL) {
1429 size_t namelen = strlen(netif->hostname);
1430 if (namelen > 0) {
1431 size_t len;
1432 const char *p = netif->hostname;
1433 /* Shrink len to available bytes (need 2 bytes for OPTION_HOSTNAME
1434 and 1 byte for trailer) */
1435 size_t available = DHCP_OPTIONS_LEN - dhcp->options_out_len - 3;
1436 LWIP_ASSERT("DHCP: hostname is too long!", namelen <= available);
1437 len = LWIP_MIN(namelen, available);
1438 LWIP_ASSERT("DHCP: hostname is too long!", len <= 0xFF);
1439 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, (u8_t)len);
1440 while (len--) {
1441 dhcp_option_byte(dhcp, *p++);
1442 }
1443 }
1444 }
1445 }
1446 #endif /* LWIP_NETIF_HOSTNAME */
1447
1448 /**
1449 * Extract the DHCP message and the DHCP options.
1450 *
1451 * Extract the DHCP message and the DHCP options, each into a contiguous
1452 * piece of memory. As a DHCP message is variable sized by its options,
1453 * and also allows overriding some fields for options, the easy approach
1454 * is to first unfold the options into a contiguous piece of memory, and
1455 * use that further on.
1456 *
1457 */
1458 static err_t
dhcp_parse_reply(struct dhcp * dhcp,struct pbuf * p)1459 dhcp_parse_reply(struct dhcp *dhcp, struct pbuf *p)
1460 {
1461 u8_t *options;
1462 u16_t offset;
1463 u16_t offset_max;
1464 u16_t options_idx;
1465 u16_t options_idx_max;
1466 struct pbuf *q;
1467 int parse_file_as_options = 0;
1468 int parse_sname_as_options = 0;
1469
1470 /* clear received options */
1471 dhcp_clear_all_options(dhcp);
1472 /* check that beginning of dhcp_msg (up to and including chaddr) is in first pbuf */
1473 if (p->len < DHCP_SNAME_OFS) {
1474 return ERR_BUF;
1475 }
1476 dhcp->msg_in = (struct dhcp_msg *)p->payload;
1477 #if LWIP_DHCP_BOOTP_FILE
1478 /* clear boot file name */
1479 dhcp->boot_file_name[0] = 0;
1480 #endif /* LWIP_DHCP_BOOTP_FILE */
1481
1482 /* parse options */
1483
1484 /* start with options field */
1485 options_idx = DHCP_OPTIONS_OFS;
1486 /* parse options to the end of the received packet */
1487 options_idx_max = p->tot_len;
1488 again:
1489 q = p;
1490 while ((q != NULL) && (options_idx >= q->len)) {
1491 options_idx -= q->len;
1492 options_idx_max -= q->len;
1493 q = q->next;
1494 }
1495 if (q == NULL) {
1496 return ERR_BUF;
1497 }
1498 offset = options_idx;
1499 offset_max = options_idx_max;
1500 options = (u8_t*)q->payload;
1501 /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
1502 while ((q != NULL) && (options[offset] != DHCP_OPTION_END) && (offset < offset_max)) {
1503 u8_t op = options[offset];
1504 u8_t len;
1505 u8_t decode_len = 0;
1506 int decode_idx = -1;
1507 u16_t val_offset = offset + 2;
1508 /* len byte might be in the next pbuf */
1509 if ((offset + 1) < q->len) {
1510 len = options[offset + 1];
1511 } else {
1512 len = (q->next != NULL ? ((u8_t*)q->next->payload)[0] : 0);
1513 }
1514 /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
1515 decode_len = len;
1516 switch(op) {
1517 /* case(DHCP_OPTION_END): handled above */
1518 case(DHCP_OPTION_PAD):
1519 /* special option: no len encoded */
1520 decode_len = len = 0;
1521 /* will be increased below */
1522 offset--;
1523 break;
1524 case(DHCP_OPTION_SUBNET_MASK):
1525 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1526 decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1527 break;
1528 case(DHCP_OPTION_ROUTER):
1529 decode_len = 4; /* only copy the first given router */
1530 LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1531 decode_idx = DHCP_OPTION_IDX_ROUTER;
1532 break;
1533 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
1534 case(DHCP_OPTION_DNS_SERVER):
1535 /* special case: there might be more than one server */
1536 LWIP_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
1537 /* limit number of DNS servers */
1538 decode_len = LWIP_MIN(len, 4 * DNS_MAX_SERVERS);
1539 LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1540 decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1541 break;
1542 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
1543 case(DHCP_OPTION_LEASE_TIME):
1544 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1545 decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1546 break;
1547 #if LWIP_DHCP_GET_NTP_SRV
1548 case(DHCP_OPTION_NTP):
1549 /* special case: there might be more than one server */
1550 LWIP_ERROR("len %% 4 == 0", len % 4 == 0, return ERR_VAL;);
1551 /* limit number of NTP servers */
1552 decode_len = LWIP_MIN(len, 4 * LWIP_DHCP_MAX_NTP_SERVERS);
1553 LWIP_ERROR("len >= decode_len", len >= decode_len, return ERR_VAL;);
1554 decode_idx = DHCP_OPTION_IDX_NTP_SERVER;
1555 break;
1556 #endif /* LWIP_DHCP_GET_NTP_SRV*/
1557 case(DHCP_OPTION_OVERLOAD):
1558 LWIP_ERROR("len == 1", len == 1, return ERR_VAL;);
1559 /* decode overload only in options, not in file/sname: invalid packet */
1560 LWIP_ERROR("overload in file/sname", options_idx == DHCP_OPTIONS_OFS, return ERR_VAL;);
1561 decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1562 break;
1563 case(DHCP_OPTION_MESSAGE_TYPE):
1564 LWIP_ERROR("len == 1", len == 1, return ERR_VAL;);
1565 decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1566 break;
1567 case(DHCP_OPTION_SERVER_ID):
1568 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1569 decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1570 break;
1571 case(DHCP_OPTION_T1):
1572 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1573 decode_idx = DHCP_OPTION_IDX_T1;
1574 break;
1575 case(DHCP_OPTION_T2):
1576 LWIP_ERROR("len == 4", len == 4, return ERR_VAL;);
1577 decode_idx = DHCP_OPTION_IDX_T2;
1578 break;
1579 default:
1580 decode_len = 0;
1581 LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", (u16_t)op));
1582 break;
1583 }
1584 offset += len + 2;
1585 if (decode_len > 0) {
1586 u32_t value = 0;
1587 u16_t copy_len;
1588 decode_next:
1589 LWIP_ASSERT("check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1590 if (!dhcp_option_given(dhcp, decode_idx)) {
1591 copy_len = LWIP_MIN(decode_len, 4);
1592 if (pbuf_copy_partial(q, &value, copy_len, val_offset) != copy_len) {
1593 return ERR_BUF;
1594 }
1595 if (decode_len > 4) {
1596 /* decode more than one u32_t */
1597 LWIP_ERROR("decode_len %% 4 == 0", decode_len % 4 == 0, return ERR_VAL;);
1598 dhcp_got_option(dhcp, decode_idx);
1599 dhcp_set_option_value(dhcp, decode_idx, lwip_htonl(value));
1600 decode_len -= 4;
1601 val_offset += 4;
1602 decode_idx++;
1603 goto decode_next;
1604 } else if (decode_len == 4) {
1605 value = lwip_ntohl(value);
1606 } else {
1607 LWIP_ERROR("invalid decode_len", decode_len == 1, return ERR_VAL;);
1608 value = ((u8_t*)&value)[0];
1609 }
1610 dhcp_got_option(dhcp, decode_idx);
1611 dhcp_set_option_value(dhcp, decode_idx, value);
1612 }
1613 }
1614 if (offset >= q->len) {
1615 offset -= q->len;
1616 offset_max -= q->len;
1617 if ((offset < offset_max) && offset_max) {
1618 q = q->next;
1619 LWIP_ASSERT("next pbuf was null", q);
1620 options = (u8_t*)q->payload;
1621 } else {
1622 /* We've run out of bytes, probably no end marker. Don't proceed. */
1623 break;
1624 }
1625 }
1626 }
1627 /* is this an overloaded message? */
1628 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1629 u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1630 dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1631 if (overload == DHCP_OVERLOAD_FILE) {
1632 parse_file_as_options = 1;
1633 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded file field\n"));
1634 } else if (overload == DHCP_OVERLOAD_SNAME) {
1635 parse_sname_as_options = 1;
1636 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname field\n"));
1637 } else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
1638 parse_sname_as_options = 1;
1639 parse_file_as_options = 1;
1640 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname and file field\n"));
1641 } else {
1642 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("invalid overload option: %d\n", (int)overload));
1643 }
1644 #if LWIP_DHCP_BOOTP_FILE
1645 if (!parse_file_as_options) {
1646 /* only do this for ACK messages */
1647 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1648 (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
1649 /* copy bootp file name, don't care for sname (server hostname) */
1650 if (pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN-1, DHCP_FILE_OFS) != (DHCP_FILE_LEN-1)) {
1651 return ERR_BUF;
1652 }
1653 /* make sure the string is really NULL-terminated */
1654 dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
1655 }
1656 #endif /* LWIP_DHCP_BOOTP_FILE */
1657 }
1658 if (parse_file_as_options) {
1659 /* if both are overloaded, parse file first and then sname (RFC 2131 ch. 4.1) */
1660 parse_file_as_options = 0;
1661 options_idx = DHCP_FILE_OFS;
1662 options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
1663 goto again;
1664 } else if (parse_sname_as_options) {
1665 parse_sname_as_options = 0;
1666 options_idx = DHCP_SNAME_OFS;
1667 options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
1668 goto again;
1669 }
1670 return ERR_OK;
1671 }
1672
1673 /**
1674 * If an incoming DHCP message is in response to us, then trigger the state machine
1675 */
1676 static void
dhcp_recv(void * arg,struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * addr,u16_t port)1677 dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
1678 {
1679 struct netif *netif = ip_current_input_netif();
1680 struct dhcp *dhcp = netif_dhcp_data(netif);
1681 struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
1682 u8_t msg_type;
1683 u8_t i;
1684
1685 LWIP_UNUSED_ARG(arg);
1686
1687 /* Caught DHCP message from netif that does not have DHCP enabled? -> not interested */
1688 if ((dhcp == NULL) || (dhcp->pcb_allocated == 0)) {
1689 goto free_pbuf_and_return;
1690 }
1691
1692 LWIP_ASSERT("invalid server address type", IP_IS_V4(addr));
1693
1694 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,
1695 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));
1696 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
1697 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
1698 /* prevent warnings about unused arguments */
1699 LWIP_UNUSED_ARG(pcb);
1700 LWIP_UNUSED_ARG(addr);
1701 LWIP_UNUSED_ARG(port);
1702
1703 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
1704
1705 if (p->len < DHCP_MIN_REPLY_LEN) {
1706 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP reply message or pbuf too short\n"));
1707 goto free_pbuf_and_return;
1708 }
1709
1710 if (reply_msg->op != DHCP_BOOTREPLY) {
1711 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));
1712 goto free_pbuf_and_return;
1713 }
1714 /* iterate through hardware address and match against DHCP message */
1715 for (i = 0; i < netif->hwaddr_len && i < NETIF_MAX_HWADDR_LEN && i < DHCP_CHADDR_LEN; i++) {
1716 if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
1717 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
1718 ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
1719 (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
1720 goto free_pbuf_and_return;
1721 }
1722 }
1723 /* match transaction ID against what we expected */
1724 if (lwip_ntohl(reply_msg->xid) != dhcp->xid) {
1725 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
1726 ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",lwip_ntohl(reply_msg->xid),dhcp->xid));
1727 goto free_pbuf_and_return;
1728 }
1729 /* option fields could be unfold? */
1730 if (dhcp_parse_reply(dhcp, p) != ERR_OK) {
1731 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1732 ("problem unfolding DHCP message - too short on memory?\n"));
1733 goto free_pbuf_and_return;
1734 }
1735
1736 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
1737 /* obtain pointer to DHCP message type */
1738 if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1739 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
1740 goto free_pbuf_and_return;
1741 }
1742
1743 /* read DHCP message type */
1744 msg_type = (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1745 /* message type is DHCP ACK? */
1746 if (msg_type == DHCP_ACK) {
1747 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_ACK received\n"));
1748 /* in requesting state? */
1749 if (dhcp->state == DHCP_STATE_REQUESTING) {
1750 dhcp_handle_ack(netif);
1751 #if DHCP_DOES_ARP_CHECK
1752 if ((netif->flags & NETIF_FLAG_ETHARP) != 0) {
1753 /* check if the acknowledged lease address is already in use */
1754 dhcp_check(netif);
1755 } else {
1756 /* bind interface to the acknowledged lease address */
1757 dhcp_bind(netif);
1758 }
1759 #else
1760 /* bind interface to the acknowledged lease address */
1761 dhcp_bind(netif);
1762 #endif
1763 }
1764 /* already bound to the given lease address? */
1765 else if ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REBINDING) ||
1766 (dhcp->state == DHCP_STATE_RENEWING)) {
1767 dhcp_handle_ack(netif);
1768 dhcp_bind(netif);
1769 }
1770 }
1771 /* received a DHCP_NAK in appropriate state? */
1772 else if ((msg_type == DHCP_NAK) &&
1773 ((dhcp->state == DHCP_STATE_REBOOTING) || (dhcp->state == DHCP_STATE_REQUESTING) ||
1774 (dhcp->state == DHCP_STATE_REBINDING) || (dhcp->state == DHCP_STATE_RENEWING ))) {
1775 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_NAK received\n"));
1776 dhcp_handle_nak(netif);
1777 }
1778 /* received a DHCP_OFFER in DHCP_STATE_SELECTING state? */
1779 else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_STATE_SELECTING)) {
1780 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_OFFER received in DHCP_STATE_SELECTING state\n"));
1781 dhcp->request_timeout = 0;
1782 /* remember offered lease */
1783 dhcp_handle_offer(netif);
1784 }
1785
1786 free_pbuf_and_return:
1787 if (dhcp != NULL) {
1788 dhcp->msg_in = NULL;
1789 }
1790 pbuf_free(p);
1791 }
1792
1793 /**
1794 * Create a DHCP request, fill in common headers
1795 *
1796 * @param netif the netif under DHCP control
1797 * @param dhcp dhcp control struct
1798 * @param message_type message type of the request
1799 */
1800 static err_t
dhcp_create_msg(struct netif * netif,struct dhcp * dhcp,u8_t message_type)1801 dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type)
1802 {
1803 u16_t i;
1804 #ifndef DHCP_GLOBAL_XID
1805 /** default global transaction identifier starting value (easy to match
1806 * with a packet analyser). We simply increment for each new request.
1807 * Predefine DHCP_GLOBAL_XID to a better value or a function call to generate one
1808 * at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */
1809 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1810 static u32_t xid;
1811 #else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1812 static u32_t xid = 0xABCD0000;
1813 #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1814 #else
1815 if (!xid_initialised) {
1816 xid = DHCP_GLOBAL_XID;
1817 xid_initialised = !xid_initialised;
1818 }
1819 #endif
1820 LWIP_ERROR("dhcp_create_msg: netif != NULL", (netif != NULL), return ERR_ARG;);
1821 LWIP_ERROR("dhcp_create_msg: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
1822 LWIP_ASSERT("dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out == NULL);
1823 LWIP_ASSERT("dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
1824 dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
1825 if (dhcp->p_out == NULL) {
1826 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1827 ("dhcp_create_msg(): could not allocate pbuf\n"));
1828 return ERR_MEM;
1829 }
1830 LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1831 (dhcp->p_out->len >= sizeof(struct dhcp_msg)));
1832
1833 /* DHCP_REQUEST should reuse 'xid' from DHCPOFFER */
1834 if (message_type != DHCP_REQUEST) {
1835 /* reuse transaction identifier in retransmissions */
1836 if (dhcp->tries == 0) {
1837 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1838 xid = LWIP_RAND();
1839 #else /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1840 xid++;
1841 #endif /* DHCP_CREATE_RAND_XID && defined(LWIP_RAND) */
1842 }
1843 dhcp->xid = xid;
1844 }
1845 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE,
1846 ("transaction id xid(%"X32_F")\n", xid));
1847
1848 dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
1849
1850 dhcp->msg_out->op = DHCP_BOOTREQUEST;
1851 /* @todo: make link layer independent */
1852 dhcp->msg_out->htype = DHCP_HTYPE_ETH;
1853 dhcp->msg_out->hlen = netif->hwaddr_len;
1854 dhcp->msg_out->hops = 0;
1855 dhcp->msg_out->xid = lwip_htonl(dhcp->xid);
1856 dhcp->msg_out->secs = 0;
1857 /* we don't need the broadcast flag since we can receive unicast traffic
1858 before being fully configured! */
1859 dhcp->msg_out->flags = 0;
1860 ip4_addr_set_zero(&dhcp->msg_out->ciaddr);
1861 /* set ciaddr to netif->ip_addr based on message_type and state */
1862 if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || (message_type == DHCP_RELEASE) ||
1863 ((message_type == DHCP_REQUEST) && /* DHCP_STATE_BOUND not used for sending! */
1864 ((dhcp->state== DHCP_STATE_RENEWING) || dhcp->state== DHCP_STATE_REBINDING))) {
1865 ip4_addr_copy(dhcp->msg_out->ciaddr, *netif_ip4_addr(netif));
1866 }
1867 ip4_addr_set_zero(&dhcp->msg_out->yiaddr);
1868 ip4_addr_set_zero(&dhcp->msg_out->siaddr);
1869 ip4_addr_set_zero(&dhcp->msg_out->giaddr);
1870 for (i = 0; i < DHCP_CHADDR_LEN; i++) {
1871 /* copy netif hardware address, pad with zeroes */
1872 dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len && i < NETIF_MAX_HWADDR_LEN) ? netif->hwaddr[i] : 0/* pad byte*/;
1873 }
1874 for (i = 0; i < DHCP_SNAME_LEN; i++) {
1875 dhcp->msg_out->sname[i] = 0;
1876 }
1877 for (i = 0; i < DHCP_FILE_LEN; i++) {
1878 dhcp->msg_out->file[i] = 0;
1879 }
1880 dhcp->msg_out->cookie = PP_HTONL(DHCP_MAGIC_COOKIE);
1881 dhcp->options_out_len = 0;
1882 /* fill options field with an incrementing array (for debugging purposes) */
1883 for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
1884 dhcp->msg_out->options[i] = (u8_t)i; /* for debugging only, no matter if truncated */
1885 }
1886 /* Add option MESSAGE_TYPE */
1887 dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
1888 dhcp_option_byte(dhcp, message_type);
1889 return ERR_OK;
1890 }
1891
1892 /**
1893 * Free previously allocated memory used to send a DHCP request.
1894 *
1895 * @param dhcp the dhcp struct to free the request from
1896 */
1897 static void
dhcp_delete_msg(struct dhcp * dhcp)1898 dhcp_delete_msg(struct dhcp *dhcp)
1899 {
1900 LWIP_ERROR("dhcp_delete_msg: dhcp != NULL", (dhcp != NULL), return;);
1901 LWIP_ASSERT("dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out != NULL);
1902 LWIP_ASSERT("dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
1903 if (dhcp->p_out != NULL) {
1904 pbuf_free(dhcp->p_out);
1905 }
1906 dhcp->p_out = NULL;
1907 dhcp->msg_out = NULL;
1908 }
1909
1910 /**
1911 * Add a DHCP message trailer
1912 *
1913 * Adds the END option to the DHCP message, and if
1914 * necessary, up to three padding bytes.
1915 *
1916 * @param dhcp DHCP state structure
1917 */
1918 static void
dhcp_option_trailer(struct dhcp * dhcp)1919 dhcp_option_trailer(struct dhcp *dhcp)
1920 {
1921 LWIP_ERROR("dhcp_option_trailer: dhcp != NULL", (dhcp != NULL), return;);
1922 LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
1923 LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1924 dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
1925 /* packet is too small, or not 4 byte aligned? */
1926 while (((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) &&
1927 (dhcp->options_out_len < DHCP_OPTIONS_LEN)) {
1928 /* add a fill/padding byte */
1929 dhcp->msg_out->options[dhcp->options_out_len++] = 0;
1930 }
1931 }
1932
1933 /** check if DHCP supplied netif->ip_addr
1934 *
1935 * @param netif the netif to check
1936 * @return 1 if DHCP supplied netif->ip_addr (states BOUND or RENEWING),
1937 * 0 otherwise
1938 */
1939 u8_t
dhcp_supplied_address(const struct netif * netif)1940 dhcp_supplied_address(const struct netif *netif)
1941 {
1942 if ((netif != NULL) && (netif_dhcp_data(netif) != NULL)) {
1943 struct dhcp* dhcp = netif_dhcp_data(netif);
1944 return (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING);
1945 }
1946 return 0;
1947 }
1948
1949 #endif /* LWIP_IPV4 && LWIP_DHCP */
1950