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