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