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