2003-03-25 12:59:42 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Dynamic Host Configuration Protocol client
|
2007-08-09 22:21:44 +00:00
|
|
|
*
|
2003-03-25 12:59:42 +00:00
|
|
|
*/
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2002-11-08 15:57:23 +00:00
|
|
|
/*
|
2003-03-25 12:59:42 +00:00
|
|
|
*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
|
|
|
|
* Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
|
2002-11-08 15:57:23 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2003-06-11 22:34:51 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2002-11-08 15:57:23 +00:00
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2003-06-11 22:34:51 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
2003-06-11 22:34:51 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
2002-11-08 15:57:23 +00:00
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is a contribution to the lwIP TCP/IP stack.
|
|
|
|
* The Swedish Institute of Computer Science and Adam Dunkels
|
|
|
|
* are specifically granted permission to redistribute this
|
|
|
|
* source code.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* Author: Leon Woestenberg <leon.woestenberg@gmx.net>
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* This is a DHCP client for the lwIP TCP/IP stack. It aims to conform
|
|
|
|
* with RFC 2131 and RFC 2132.
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
2003-02-20 08:41:59 +00:00
|
|
|
* - Proper parsing of DHCP messages exploiting file/sname field overloading.
|
2002-11-08 15:57:23 +00:00
|
|
|
* - Add JavaDoc style documentation (API, internals).
|
|
|
|
* - Support for interfaces other than Ethernet (SLIP, PPP, ...)
|
|
|
|
*
|
|
|
|
* Please coordinate changes and requests with Leon Woestenberg
|
2003-02-20 08:41:59 +00:00
|
|
|
* <leon.woestenberg@gmx.net>
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
|
|
|
* Integration with your code:
|
|
|
|
*
|
|
|
|
* In lwip/dhcp.h
|
|
|
|
* #define DHCP_COARSE_TIMER_SECS (recommended 60 which is a minute)
|
|
|
|
* #define DHCP_FINE_TIMER_MSECS (recommended 500 which equals TCP coarse timer)
|
|
|
|
*
|
|
|
|
* Then have your application call dhcp_coarse_tmr() and
|
|
|
|
* dhcp_fine_tmr() on the defined intervals.
|
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* dhcp_start(struct netif *netif);
|
2002-12-01 19:49:45 +00:00
|
|
|
* starts a DHCP client instance which configures the interface by
|
2002-11-08 15:57:23 +00:00
|
|
|
* obtaining an IP address lease and maintaining it.
|
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* Use dhcp_release(netif) to end the lease and use dhcp_stop(netif)
|
2002-12-01 19:49:45 +00:00
|
|
|
* to remove the DHCP client.
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2007-09-07 23:01:59 +00:00
|
|
|
|
|
|
|
#include "lwip/opt.h"
|
|
|
|
|
|
|
|
#if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
|
|
|
|
|
2002-11-08 15:57:23 +00:00
|
|
|
#include "lwip/stats.h"
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/udp.h"
|
2004-03-12 00:10:07 +00:00
|
|
|
#include "lwip/ip_addr.h"
|
2003-02-20 08:41:59 +00:00
|
|
|
#include "lwip/netif.h"
|
2003-03-10 16:13:25 +00:00
|
|
|
#include "lwip/inet.h"
|
2002-11-08 15:57:23 +00:00
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/dhcp.h"
|
2007-06-18 21:45:34 +00:00
|
|
|
#include "lwip/autoip.h"
|
2007-11-14 23:27:13 +00:00
|
|
|
#include "lwip/dns.h"
|
2007-09-07 23:01:59 +00:00
|
|
|
#include "netif/etharp.h"
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#include <string.h>
|
2004-04-16 22:34:25 +00:00
|
|
|
|
2009-04-15 18:48:10 +00:00
|
|
|
/** Default for DHCP_GLOBAL_XID is 0xABCD0000
|
|
|
|
* This can be changed by defining DHCP_GLOBAL_XID and DHCP_GLOBAL_XID_HEADER, e.g.
|
|
|
|
* #define DHCP_GLOBAL_XID_HEADER "stdlib.h"
|
|
|
|
* #define DHCP_GLOBAL_XID rand()
|
|
|
|
*/
|
|
|
|
#ifdef DHCP_GLOBAL_XID_HEADER
|
|
|
|
#include DHCP_GLOBAL_XID_HEADER /* include optional starting XID generation prototypes */
|
|
|
|
#endif
|
|
|
|
|
2009-04-15 18:35:04 +00:00
|
|
|
/** DHCP_OPTION_MAX_MSG_SIZE is set to the MTU
|
|
|
|
* MTU is checked to be big enough in dhcp_start */
|
|
|
|
#define DHCP_MAX_MSG_LEN(netif) (netif->mtu)
|
|
|
|
#define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576
|
|
|
|
|
2007-08-09 22:21:44 +00:00
|
|
|
/* DHCP client state machine functions */
|
2003-02-20 13:13:51 +00:00
|
|
|
static void dhcp_handle_ack(struct netif *netif);
|
|
|
|
static void dhcp_handle_nak(struct netif *netif);
|
|
|
|
static void dhcp_handle_offer(struct netif *netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
|
|
|
|
static err_t dhcp_discover(struct netif *netif);
|
|
|
|
static err_t dhcp_select(struct netif *netif);
|
|
|
|
static void dhcp_check(struct netif *netif);
|
|
|
|
static void dhcp_bind(struct netif *netif);
|
2008-03-04 13:41:23 +00:00
|
|
|
#if DHCP_DOES_ARP_CHECK
|
2003-02-20 08:41:59 +00:00
|
|
|
static err_t dhcp_decline(struct netif *netif);
|
2008-03-04 14:25:58 +00:00
|
|
|
#endif /* DHCP_DOES_ARP_CHECK */
|
2003-02-20 08:41:59 +00:00
|
|
|
static err_t dhcp_rebind(struct netif *netif);
|
2005-11-25 12:03:38 +00:00
|
|
|
static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2007-08-09 22:21:44 +00:00
|
|
|
/* receive, unfold, parse and free incoming messages */
|
2002-11-08 15:57:23 +00:00
|
|
|
static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port);
|
2003-02-20 08:41:59 +00:00
|
|
|
static err_t dhcp_unfold_reply(struct dhcp *dhcp);
|
|
|
|
static u8_t *dhcp_get_option_ptr(struct dhcp *dhcp, u8_t option_type);
|
2002-11-08 15:57:23 +00:00
|
|
|
static u8_t dhcp_get_option_byte(u8_t *ptr);
|
2006-06-08 00:16:56 +00:00
|
|
|
#if 0
|
2002-11-08 15:57:23 +00:00
|
|
|
static u16_t dhcp_get_option_short(u8_t *ptr);
|
2006-06-08 00:16:56 +00:00
|
|
|
#endif
|
2002-11-08 15:57:23 +00:00
|
|
|
static u32_t dhcp_get_option_long(u8_t *ptr);
|
2003-02-20 08:41:59 +00:00
|
|
|
static void dhcp_free_reply(struct dhcp *dhcp);
|
2002-12-01 19:49:45 +00:00
|
|
|
|
2007-08-09 22:21:44 +00:00
|
|
|
/* set the DHCP timers */
|
2003-02-20 08:41:59 +00:00
|
|
|
static void dhcp_timeout(struct netif *netif);
|
2003-02-20 13:13:51 +00:00
|
|
|
static void dhcp_t1_timeout(struct netif *netif);
|
|
|
|
static void dhcp_t2_timeout(struct netif *netif);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2007-08-09 22:21:44 +00:00
|
|
|
/* build outgoing messages */
|
|
|
|
/* create a DHCP request, fill in common headers */
|
2003-02-20 13:13:51 +00:00
|
|
|
static err_t dhcp_create_request(struct netif *netif);
|
2007-08-09 22:21:44 +00:00
|
|
|
/* free a DHCP request */
|
2003-02-20 13:13:51 +00:00
|
|
|
static void dhcp_delete_request(struct netif *netif);
|
2007-08-09 22:21:44 +00:00
|
|
|
/* add a DHCP option (type, then length in bytes) */
|
2003-02-20 08:41:59 +00:00
|
|
|
static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len);
|
2007-08-09 22:21:44 +00:00
|
|
|
/* add option values */
|
2003-02-20 08:41:59 +00:00
|
|
|
static void dhcp_option_byte(struct dhcp *dhcp, u8_t value);
|
|
|
|
static void dhcp_option_short(struct dhcp *dhcp, u16_t value);
|
|
|
|
static void dhcp_option_long(struct dhcp *dhcp, u32_t value);
|
2007-08-09 22:21:44 +00:00
|
|
|
/* always add the DHCP options trailer to end and pad */
|
2003-02-20 08:41:59 +00:00
|
|
|
static void dhcp_option_trailer(struct dhcp *dhcp);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
2003-02-20 08:41:59 +00:00
|
|
|
* Back-off the DHCP client (because of a received NAK response).
|
|
|
|
*
|
2002-12-01 19:49:45 +00:00
|
|
|
* Back-off the DHCP client because of a received NAK. Receiving a
|
|
|
|
* NAK means the client asked for something non-sensible, for
|
|
|
|
* example when it tries to renew a lease obtained on another network.
|
|
|
|
*
|
2008-01-06 21:09:11 +00:00
|
|
|
* We clear any existing set IP address and restart DHCP negotiation
|
|
|
|
* afresh (as per RFC2131 3.2.3).
|
2003-02-20 08:41:59 +00:00
|
|
|
*
|
2007-11-25 14:23:59 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2003-06-11 22:34:51 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_handle_nak(struct netif *netif)
|
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
|
2005-11-25 12:03:38 +00:00
|
|
|
(void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
|
2008-01-06 21:09:11 +00:00
|
|
|
/* Set the interface down since the address must no longer be used, as per RFC2131 */
|
|
|
|
netif_set_down(netif);
|
|
|
|
/* remove IP address from interface */
|
|
|
|
netif_set_ipaddr(netif, IP_ADDR_ANY);
|
|
|
|
netif_set_gw(netif, IP_ADDR_ANY);
|
|
|
|
netif_set_netmask(netif, IP_ADDR_ANY);
|
|
|
|
/* Change to a defined state */
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_BACKING_OFF);
|
2008-01-06 21:09:11 +00:00
|
|
|
/* We can immediately restart discovery */
|
2008-01-06 22:07:03 +00:00
|
|
|
dhcp_discover(netif);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
2003-03-16 22:36:54 +00:00
|
|
|
* Checks if the offered IP address is already in use.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2002-12-01 19:49:45 +00:00
|
|
|
* It does so by sending an ARP request for the offered address and
|
|
|
|
* entering CHECKING state. If no ARP reply is received within a small
|
|
|
|
* interval, the address is assumed to be free for use by us.
|
2007-11-25 14:23:59 +00:00
|
|
|
*
|
|
|
|
* @param netif the netif under DHCP control
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_check(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2002-11-08 15:57:23 +00:00
|
|
|
err_t result;
|
2003-02-20 08:41:59 +00:00
|
|
|
u16_t msecs;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
|
2005-11-25 12:03:38 +00:00
|
|
|
(s16_t)netif->name[1]));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* create an ARP query for the offered IP address, expecting that no host
|
|
|
|
responds, as the IP address should not be in use. */
|
2003-04-01 14:02:50 +00:00
|
|
|
result = etharp_query(netif, &dhcp->offered_ip_addr, NULL);
|
|
|
|
if (result != ERR_OK) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_check: could not perform ARP query\n"));
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->tries++;
|
|
|
|
msecs = 500;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_CHECKING);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Remember the configuration offered by a DHCP server.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2007-11-25 14:23:59 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_handle_offer(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 13:13:51 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2003-02-20 08:41:59 +00:00
|
|
|
/* obtain the server address */
|
|
|
|
u8_t *option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_SERVER_ID);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n",
|
2005-11-25 12:03:38 +00:00
|
|
|
(void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
|
2007-11-25 14:23:59 +00:00
|
|
|
if (option_ptr != NULL) {
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->server_ip_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n", dhcp->server_ip_addr.addr));
|
2002-11-08 15:57:23 +00:00
|
|
|
/* remember offered address */
|
2003-02-20 08:41:59 +00:00
|
|
|
ip_addr_set(&dhcp->offered_ip_addr, (struct ip_addr *)&dhcp->msg_in->yiaddr);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));
|
2003-12-28 02:38:51 +00:00
|
|
|
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_select(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a DHCP server offer out of all offers.
|
|
|
|
*
|
2002-12-01 19:49:45 +00:00
|
|
|
* Simply select the first offer received.
|
2003-02-20 08:41:59 +00:00
|
|
|
*
|
2003-03-16 22:36:54 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2003-02-20 08:41:59 +00:00
|
|
|
* @return lwIP specific error (see error.h)
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static err_t
|
|
|
|
dhcp_select(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 13:13:51 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2003-02-20 08:41:59 +00:00
|
|
|
err_t result;
|
2007-03-26 18:52:52 +00:00
|
|
|
u16_t msecs;
|
2007-03-28 09:39:12 +00:00
|
|
|
#if LWIP_NETIF_HOSTNAME
|
|
|
|
const char *p;
|
|
|
|
#endif /* LWIP_NETIF_HOSTNAME */
|
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
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));
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (result == ERR_OK) {
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_REQUEST);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
|
2009-04-15 18:35:04 +00:00
|
|
|
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
|
2002-11-08 15:57:23 +00:00
|
|
|
|
|
|
|
/* MUST request the offered IP address */
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
|
|
|
|
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
|
|
|
|
|
2004-07-22 21:56:09 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
|
2004-07-22 21:56:09 +00:00
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
|
2003-02-20 08:41:59 +00:00
|
|
|
|
2007-03-28 09:39:12 +00:00
|
|
|
#if LWIP_NETIF_HOSTNAME
|
2007-11-25 14:23:59 +00:00
|
|
|
p = (const char*)netif->hostname;
|
2009-04-15 18:35:04 +00:00
|
|
|
if (p != NULL) {
|
2007-03-28 09:39:12 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p));
|
2007-11-25 14:23:59 +00:00
|
|
|
while (*p) {
|
2007-03-28 09:39:12 +00:00
|
|
|
dhcp_option_byte(dhcp, *p++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_NETIF_HOSTNAME */
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
/* shrink the pbuf to the actual content length */
|
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
|
|
|
|
|
|
|
/* TODO: we really should bind to a specific local interface here
|
|
|
|
but we cannot specify an unconfigured netif as it is addressless */
|
|
|
|
/* send broadcast to any DHCP server */
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* reconnect to any (or to server here?!) */
|
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_REQUESTING);
|
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_select: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
dhcp->tries++;
|
|
|
|
msecs = dhcp->tries < 4 ? dhcp->tries * 1000 : 4 * 1000;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-04-02 20:34:18 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
|
2003-02-20 08:41:59 +00:00
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* The DHCP timer that checks for lease renewal/rebind timeouts.
|
|
|
|
*
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
void
|
|
|
|
dhcp_coarse_tmr()
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct netif *netif = netif_list;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* iterate through all network interfaces */
|
|
|
|
while (netif != NULL) {
|
|
|
|
/* only act on DHCP configured interfaces */
|
|
|
|
if (netif->dhcp != NULL) {
|
|
|
|
/* timer is active (non zero), and triggers (zeroes) now? */
|
|
|
|
if (netif->dhcp->t2_timeout-- == 1) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* this clients' rebind timeout triggered */
|
|
|
|
dhcp_t2_timeout(netif);
|
|
|
|
/* timer is active (non zero), and triggers (zeroes) now */
|
|
|
|
} else if (netif->dhcp->t1_timeout-- == 1) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* this clients' renewal timeout triggered */
|
|
|
|
dhcp_t1_timeout(netif);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* proceed to next netif */
|
|
|
|
netif = netif->next;
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
2003-03-16 22:36:54 +00:00
|
|
|
* DHCP transaction timeout handling
|
2002-12-01 19:49:45 +00:00
|
|
|
*
|
2004-06-30 18:42:33 +00:00
|
|
|
* A DHCP server is expected to respond within a short period of time.
|
|
|
|
* This timer checks whether an outstanding DHCP request is timed out.
|
|
|
|
*
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
void
|
|
|
|
dhcp_fine_tmr()
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct netif *netif = netif_list;
|
2004-06-30 18:42:33 +00:00
|
|
|
/* loop through netif's */
|
2003-02-20 08:41:59 +00:00
|
|
|
while (netif != NULL) {
|
|
|
|
/* only act on DHCP configured interfaces */
|
|
|
|
if (netif->dhcp != NULL) {
|
2006-11-30 16:25:43 +00:00
|
|
|
/* timer is active (non zero), and is about to trigger now */
|
|
|
|
if (netif->dhcp->request_timeout > 1) {
|
|
|
|
netif->dhcp->request_timeout--;
|
|
|
|
}
|
|
|
|
else if (netif->dhcp->request_timeout == 1) {
|
|
|
|
netif->dhcp->request_timeout--;
|
2004-06-30 18:42:33 +00:00
|
|
|
/* { netif->dhcp->request_timeout == 0 } */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* this clients' request timeout triggered */
|
|
|
|
dhcp_timeout(netif);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* proceed to next network interface */
|
|
|
|
netif = netif->next;
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* A DHCP negotiation transaction, or ARP request, has timed out.
|
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* The timer that was started with the DHCP or ARP request has
|
2003-06-11 22:34:51 +00:00
|
|
|
* timed out, indicating no response was received in time.
|
2003-02-20 08:41:59 +00:00
|
|
|
*
|
2003-03-16 22:36:54 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_timeout(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 13:13:51 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_timeout()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* back-off period has passed, or server selection timed out */
|
|
|
|
if ((dhcp->state == DHCP_BACKING_OFF) || (dhcp->state == DHCP_SELECTING)) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_discover(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* receiving the requested lease timed out */
|
|
|
|
} else if (dhcp->state == DHCP_REQUESTING) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->tries <= 5) {
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_select(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_release(netif);
|
|
|
|
dhcp_discover(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
/* received no ARP reply for the offered address (which is good) */
|
|
|
|
} else if (dhcp->state == DHCP_CHECKING) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->tries <= 1) {
|
|
|
|
dhcp_check(netif);
|
2003-06-11 22:34:51 +00:00
|
|
|
/* no ARP replies on the offered address,
|
2003-02-20 08:41:59 +00:00
|
|
|
looks like the IP address is indeed free */
|
|
|
|
} else {
|
|
|
|
/* bind the interface to the offered address */
|
|
|
|
dhcp_bind(netif);
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
/* did not get response to renew request? */
|
|
|
|
else if (dhcp->state == DHCP_RENEWING) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RENEWING, DHCP request timed out\n"));
|
2003-06-11 22:34:51 +00:00
|
|
|
/* just retry renewal */
|
2003-02-20 08:41:59 +00:00
|
|
|
/* note that the rebind timer will eventually time-out if renew does not work */
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_renew(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* did not get response to rebind request? */
|
|
|
|
} else if (dhcp->state == DHCP_REBINDING) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REBINDING, DHCP request timed out\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->tries <= 8) {
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_rebind(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RELEASING, DISCOVERING\n"));
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_release(netif);
|
|
|
|
dhcp_discover(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* The renewal period has timed out.
|
|
|
|
*
|
2003-03-16 22:36:54 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_t1_timeout(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING)) {
|
2004-06-30 18:42:33 +00:00
|
|
|
/* just retry to renew - note that the rebind timer (t2) will
|
|
|
|
* eventually time-out if renew tries fail. */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t1_timeout(): must renew\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_renew(netif);
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2002-12-01 19:49:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The rebind period has timed out.
|
|
|
|
*
|
2007-11-25 14:23:59 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_t2_timeout(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING)) {
|
|
|
|
/* just retry to rebind */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout(): must rebind\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_rebind(netif);
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-25 14:23:59 +00:00
|
|
|
* Handle a DHCP ACK packet
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
2003-03-16 22:36:54 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_handle_ack(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 13:13:51 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2002-11-08 15:57:23 +00:00
|
|
|
u8_t *option_ptr;
|
2002-12-01 19:49:45 +00:00
|
|
|
/* clear options we might not get from the ACK */
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->offered_sn_mask.addr = 0;
|
|
|
|
dhcp->offered_gw_addr.addr = 0;
|
|
|
|
dhcp->offered_bc_addr.addr = 0;
|
|
|
|
|
|
|
|
/* lease time given? */
|
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_LEASE_TIME);
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
/* remember offered lease time */
|
|
|
|
dhcp->offered_t0_lease = dhcp_get_option_long(option_ptr + 2);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
/* renewal period given? */
|
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_T1);
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
/* remember given renewal period */
|
|
|
|
dhcp->offered_t1_renew = dhcp_get_option_long(option_ptr + 2);
|
|
|
|
} else {
|
|
|
|
/* calculate safe periods for renewal */
|
|
|
|
dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
|
|
|
|
/* renewal period given? */
|
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_T2);
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
/* remember given rebind period */
|
|
|
|
dhcp->offered_t2_rebind = dhcp_get_option_long(option_ptr + 2);
|
|
|
|
} else {
|
|
|
|
/* calculate safe periods for rebinding */
|
|
|
|
dhcp->offered_t2_rebind = dhcp->offered_t0_lease;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/* (y)our internet address */
|
2003-02-20 08:41:59 +00:00
|
|
|
ip_addr_set(&dhcp->offered_ip_addr, &dhcp->msg_in->yiaddr);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-05-01 08:00:41 +00:00
|
|
|
/**
|
|
|
|
* Patch #1308
|
|
|
|
* TODO: we must check if the file field is not overloaded by DHCP options!
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
/* boot server address */
|
|
|
|
ip_addr_set(&dhcp->offered_si_addr, &dhcp->msg_in->siaddr);
|
|
|
|
/* boot file name */
|
|
|
|
if (dhcp->msg_in->file[0]) {
|
|
|
|
dhcp->boot_file_name = mem_malloc(strlen(dhcp->msg_in->file) + 1);
|
2003-06-09 21:14:47 +00:00
|
|
|
strcpy(dhcp->boot_file_name, dhcp->msg_in->file);
|
2003-05-01 08:00:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/* subnet mask */
|
2003-02-20 08:41:59 +00:00
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_SUBNET_MASK);
|
|
|
|
/* subnet mask given? */
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
dhcp->offered_sn_mask.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/* gateway router */
|
2003-02-20 08:41:59 +00:00
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_ROUTER);
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
dhcp->offered_gw_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/* broadcast address */
|
2003-02-20 08:41:59 +00:00
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_BROADCAST);
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
dhcp->offered_bc_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
|
|
|
|
}
|
2004-07-22 21:56:09 +00:00
|
|
|
|
|
|
|
/* DNS servers */
|
|
|
|
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_DNS_SERVER);
|
|
|
|
if (option_ptr != NULL) {
|
|
|
|
u8_t n;
|
2006-06-08 00:16:56 +00:00
|
|
|
dhcp->dns_count = dhcp_get_option_byte(&option_ptr[1]) / (u32_t)sizeof(struct ip_addr);
|
2004-07-22 21:56:09 +00:00
|
|
|
/* limit to at most DHCP_MAX_DNS DNS servers */
|
2006-06-08 00:16:56 +00:00
|
|
|
if (dhcp->dns_count > DHCP_MAX_DNS)
|
2006-11-17 10:51:13 +00:00
|
|
|
dhcp->dns_count = DHCP_MAX_DNS;
|
2006-06-08 00:16:56 +00:00
|
|
|
for (n = 0; n < dhcp->dns_count; n++) {
|
|
|
|
dhcp->offered_dns_addr[n].addr = htonl(dhcp_get_option_long(&option_ptr[2 + n * 4]));
|
2007-11-14 23:27:13 +00:00
|
|
|
#if LWIP_DNS
|
|
|
|
dns_setserver( n, (struct ip_addr *)(&(dhcp->offered_dns_addr[n].addr)));
|
|
|
|
#endif /* LWIP_DNS */
|
2004-07-22 21:56:09 +00:00
|
|
|
}
|
2007-11-18 20:30:12 +00:00
|
|
|
#if LWIP_DNS
|
2007-11-18 20:54:44 +00:00
|
|
|
dns_setserver( n, (struct ip_addr *)(&ip_addr_any));
|
2007-11-18 20:30:12 +00:00
|
|
|
#endif /* LWIP_DNS */
|
2004-07-22 21:56:09 +00:00
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start DHCP negotiation for a network interface.
|
|
|
|
*
|
|
|
|
* If no DHCP client instance was attached to this interface,
|
2002-12-01 19:49:45 +00:00
|
|
|
* a new client is created first. If a DHCP client instance
|
|
|
|
* was already present, it restarts negotiation.
|
|
|
|
*
|
2003-06-11 22:34:51 +00:00
|
|
|
* @param netif The lwIP network interface
|
2003-03-16 22:36:54 +00:00
|
|
|
* @return lwIP error code
|
|
|
|
* - ERR_OK - No error
|
|
|
|
* - ERR_MEM - Out of memory
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
err_t
|
|
|
|
dhcp_start(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-06-17 14:39:50 +00:00
|
|
|
struct dhcp *dhcp;
|
2003-02-20 08:41:59 +00:00
|
|
|
err_t result = ERR_OK;
|
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
|
2007-06-17 14:39:50 +00:00
|
|
|
dhcp = netif->dhcp;
|
2007-03-30 08:47:04 +00:00
|
|
|
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));
|
2008-04-15 17:24:54 +00:00
|
|
|
/* Remove the flag that says this netif is handled by DHCP,
|
|
|
|
it is set when we succeeded starting. */
|
2004-06-30 18:42:33 +00:00
|
|
|
netif->flags &= ~NETIF_FLAG_DHCP;
|
2003-02-20 08:41:59 +00:00
|
|
|
|
2009-04-15 18:35:04 +00:00
|
|
|
/* check MTU of the netif */
|
|
|
|
if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
|
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
2004-06-30 18:42:33 +00:00
|
|
|
/* no DHCP client attached yet? */
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp = mem_malloc(sizeof(struct dhcp));
|
|
|
|
if (dhcp == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
return ERR_MEM;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
/* store this dhcp client in the netif */
|
|
|
|
netif->dhcp = dhcp;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp"));
|
2004-06-30 18:42:33 +00:00
|
|
|
/* already has DHCP client attached */
|
2003-02-20 08:41:59 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 3, ("dhcp_start(): restarting DHCP configuration\n"));
|
2009-02-11 20:27:00 +00:00
|
|
|
if (dhcp->pcb != NULL) {
|
|
|
|
udp_remove(dhcp->pcb);
|
|
|
|
}
|
|
|
|
if (dhcp->p != NULL) {
|
|
|
|
pbuf_free(dhcp->p);
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2006-11-17 10:51:13 +00:00
|
|
|
|
|
|
|
/* clear data structure */
|
|
|
|
memset(dhcp, 0, sizeof(struct dhcp));
|
2004-06-30 18:42:33 +00:00
|
|
|
/* allocate UDP PCB */
|
2006-11-17 10:51:13 +00:00
|
|
|
dhcp->pcb = udp_new();
|
|
|
|
if (dhcp->pcb == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not obtain pcb\n"));
|
2006-11-17 10:51:13 +00:00
|
|
|
mem_free((void *)dhcp);
|
|
|
|
netif->dhcp = dhcp = NULL;
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2007-11-27 20:13:16 +00:00
|
|
|
/* set up local and remote port for the pcb */
|
|
|
|
udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
|
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
|
|
|
|
/* set up the recv callback and argument */
|
|
|
|
udp_recv(dhcp->pcb, dhcp_recv, netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* (re)start the DHCP negotiation */
|
|
|
|
result = dhcp_discover(netif);
|
|
|
|
if (result != ERR_OK) {
|
|
|
|
/* free resources allocated above */
|
|
|
|
dhcp_stop(netif);
|
2004-06-30 18:42:33 +00:00
|
|
|
return ERR_MEM;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2008-04-15 17:24:54 +00:00
|
|
|
/* Set the flag that says this netif is handled by DHCP. */
|
2004-06-30 18:42:33 +00:00
|
|
|
netif->flags |= NETIF_FLAG_DHCP;
|
2003-02-20 08:41:59 +00:00
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Inform a DHCP server of our manual configuration.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2002-12-01 19:49:45 +00:00
|
|
|
* This informs DHCP servers of our fixed IP address configuration
|
2003-02-20 08:41:59 +00:00
|
|
|
* by sending an INFORM message. It does not involve DHCP address
|
|
|
|
* configuration, it is just here to be nice to the network.
|
|
|
|
*
|
2003-06-11 22:34:51 +00:00
|
|
|
* @param netif The lwIP network interface
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
void
|
|
|
|
dhcp_inform(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-11-27 20:13:16 +00:00
|
|
|
struct dhcp *dhcp, *old_dhcp = netif->dhcp;
|
2003-02-20 08:41:59 +00:00
|
|
|
err_t result = ERR_OK;
|
|
|
|
dhcp = mem_malloc(sizeof(struct dhcp));
|
|
|
|
if (dhcp == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_inform(): could not allocate dhcp\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
return;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2003-04-15 12:37:06 +00:00
|
|
|
netif->dhcp = dhcp;
|
2003-02-20 08:41:59 +00:00
|
|
|
memset(dhcp, 0, sizeof(struct dhcp));
|
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): allocated dhcp\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->pcb = udp_new();
|
|
|
|
if (dhcp->pcb == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_inform(): could not obtain pcb"));
|
2003-02-20 08:41:59 +00:00
|
|
|
mem_free((void *)dhcp);
|
|
|
|
return;
|
|
|
|
}
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): created new udp pcb\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
if (result == ERR_OK) {
|
|
|
|
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_INFORM);
|
|
|
|
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
|
2009-04-15 18:35:04 +00:00
|
|
|
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
|
2003-02-20 08:41:59 +00:00
|
|
|
|
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
|
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
|
|
|
|
|
|
|
udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
|
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_BROADCAST, DHCP_SERVER_PORT);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2003-03-31 08:02:52 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_inform: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-11 20:27:00 +00:00
|
|
|
if (dhcp->pcb != NULL) {
|
|
|
|
udp_remove(dhcp->pcb);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2009-02-11 20:27:00 +00:00
|
|
|
dhcp->pcb = NULL;
|
|
|
|
mem_free((void *)dhcp);
|
|
|
|
netif->dhcp = old_dhcp;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DHCP_DOES_ARP_CHECK
|
2003-02-20 08:41:59 +00:00
|
|
|
/**
|
|
|
|
* Match an ARP reply with the offered IP address.
|
|
|
|
*
|
2007-06-17 14:39:50 +00:00
|
|
|
* @param netif the network interface on which the reply was received
|
2003-02-20 08:41:59 +00:00
|
|
|
* @param addr The IP address we received a reply from
|
|
|
|
*/
|
|
|
|
void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netif != NULL", (netif != NULL), return;);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_arp_reply()\n"));
|
2005-01-24 23:02:29 +00:00
|
|
|
/* is a DHCP client doing an ARP check? */
|
2003-02-20 08:41:59 +00:00
|
|
|
if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_CHECKING)) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n", addr->addr));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* did a host respond with the address we
|
|
|
|
were offered by the DHCP server? */
|
2003-02-20 13:13:51 +00:00
|
|
|
if (ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
|
2003-02-20 08:41:59 +00:00
|
|
|
/* we will not accept the offered address */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | 1, ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_decline(netif);
|
|
|
|
}
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2003-06-11 22:34:51 +00:00
|
|
|
/**
|
2003-02-20 08:41:59 +00:00
|
|
|
* Decline an offered lease.
|
2002-12-01 19:49:45 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* Tell the DHCP server we do not accept the offered address.
|
|
|
|
* One reason to decline the lease is when we find out the address
|
|
|
|
* is already in use by another host (through ARP).
|
2007-11-25 14:23:59 +00:00
|
|
|
*
|
|
|
|
* @param netif the netif under DHCP control
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static err_t
|
|
|
|
dhcp_decline(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
|
|
|
err_t result = ERR_OK;
|
|
|
|
u16_t msecs;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_decline()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_BACKING_OFF);
|
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (result == ERR_OK) {
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_DECLINE);
|
|
|
|
|
2003-12-28 02:38:51 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
/* resize pbuf to reflect true size of options */
|
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
|
|
|
|
2004-11-25 11:10:53 +00:00
|
|
|
/* @todo: should we really connect here? we are performing sendto() */
|
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
|
|
|
|
/* per section 4.4.4, broadcast DECLINE messages */
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_decline: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
dhcp->tries++;
|
|
|
|
msecs = 10*1000;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-11-25 14:23:59 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
|
2003-02-20 08:41:59 +00:00
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2002-12-01 19:49:45 +00:00
|
|
|
* Start the DHCP process, discover a DHCP server.
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
2007-11-25 14:23:59 +00:00
|
|
|
* @param netif the netif under DHCP control
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static err_t
|
|
|
|
dhcp_discover(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
|
|
|
err_t result = ERR_OK;
|
|
|
|
u16_t msecs;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_discover()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
ip_addr_set(&dhcp->offered_ip_addr, IP_ADDR_ANY);
|
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (result == ERR_OK) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_DISCOVER);
|
|
|
|
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
|
2009-04-15 18:35:04 +00:00
|
|
|
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
|
2003-02-20 08:41:59 +00:00
|
|
|
|
2004-07-22 21:56:09 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
|
2004-07-22 21:56:09 +00:00
|
|
|
dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
|
2003-02-20 08:41:59 +00:00
|
|
|
|
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
|
|
|
|
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_SELECTING);
|
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_discover: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
dhcp->tries++;
|
2007-06-09 16:57:04 +00:00
|
|
|
#if LWIP_DHCP_AUTOIP_COOP
|
2009-04-28 19:29:46 +00:00
|
|
|
if(dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {
|
2007-06-09 16:57:04 +00:00
|
|
|
dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
|
|
|
|
autoip_start(netif);
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DHCP_AUTOIP_COOP */
|
2003-02-20 08:41:59 +00:00
|
|
|
msecs = dhcp->tries < 4 ? (dhcp->tries + 1) * 1000 : 10 * 1000;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
|
2003-02-20 08:41:59 +00:00
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind the interface to the offered IP address.
|
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* @param netif network interface to bind to the offered address
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_bind(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-03-26 18:52:52 +00:00
|
|
|
u32_t timeout;
|
2007-06-17 14:39:50 +00:00
|
|
|
struct dhcp *dhcp;
|
2002-11-08 15:57:23 +00:00
|
|
|
struct ip_addr sn_mask, gw_addr;
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
|
2007-06-17 14:39:50 +00:00
|
|
|
dhcp = netif->dhcp;
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
|
2007-03-30 08:47:04 +00:00
|
|
|
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));
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/* temporary DHCP lease? */
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->offered_t1_renew != 0xffffffffUL) {
|
2002-12-01 19:49:45 +00:00
|
|
|
/* set renewal period timer */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
|
2007-03-26 18:52:52 +00:00
|
|
|
timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
|
2007-11-25 14:23:59 +00:00
|
|
|
if(timeout > 0xffff) {
|
2007-03-26 18:52:52 +00:00
|
|
|
timeout = 0xffff;
|
2007-11-25 14:23:59 +00:00
|
|
|
}
|
2007-03-26 18:52:52 +00:00
|
|
|
dhcp->t1_timeout = (u16_t)timeout;
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->t1_timeout == 0) {
|
2007-03-26 18:52:52 +00:00
|
|
|
dhcp->t1_timeout = 1;
|
2007-11-25 14:23:59 +00:00
|
|
|
}
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
2002-12-01 19:49:45 +00:00
|
|
|
/* set renewal period timer */
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->offered_t2_rebind != 0xffffffffUL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
|
2007-03-26 18:52:52 +00:00
|
|
|
timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
|
2007-11-25 14:23:59 +00:00
|
|
|
if(timeout > 0xffff) {
|
2007-03-26 18:52:52 +00:00
|
|
|
timeout = 0xffff;
|
2007-11-25 14:23:59 +00:00
|
|
|
}
|
2007-03-26 18:52:52 +00:00
|
|
|
dhcp->t2_timeout = (u16_t)timeout;
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->t2_timeout == 0) {
|
2007-03-26 18:52:52 +00:00
|
|
|
dhcp->t2_timeout = 1;
|
2007-11-25 14:23:59 +00:00
|
|
|
}
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
/* copy offered network mask */
|
|
|
|
ip_addr_set(&sn_mask, &dhcp->offered_sn_mask);
|
|
|
|
|
|
|
|
/* subnet mask not given? */
|
2003-02-20 13:13:51 +00:00
|
|
|
/* TODO: this is not a valid check. what if the network mask is 0? */
|
2003-02-20 08:41:59 +00:00
|
|
|
if (sn_mask.addr == 0) {
|
|
|
|
/* choose a safe subnet mask given the network class */
|
|
|
|
u8_t first_octet = ip4_addr1(&sn_mask);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (first_octet <= 127) {
|
2007-03-26 18:52:52 +00:00
|
|
|
sn_mask.addr = htonl(0xff000000);
|
2007-11-25 14:23:59 +00:00
|
|
|
} else if (first_octet >= 192) {
|
2007-03-26 18:52:52 +00:00
|
|
|
sn_mask.addr = htonl(0xffffff00);
|
2007-11-25 14:23:59 +00:00
|
|
|
} else {
|
2007-03-26 18:52:52 +00:00
|
|
|
sn_mask.addr = htonl(0xffff0000);
|
2007-11-25 14:23:59 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ip_addr_set(&gw_addr, &dhcp->offered_gw_addr);
|
|
|
|
/* gateway address not given? */
|
|
|
|
if (gw_addr.addr == 0) {
|
|
|
|
/* copy network address */
|
2003-02-20 13:13:51 +00:00
|
|
|
gw_addr.addr = (dhcp->offered_ip_addr.addr & sn_mask.addr);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* use first host address on network as gateway */
|
|
|
|
gw_addr.addr |= htonl(0x00000001);
|
|
|
|
}
|
|
|
|
|
2009-04-28 19:29:46 +00:00
|
|
|
#if LWIP_DHCP_AUTOIP_COOP
|
|
|
|
if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
|
|
|
|
autoip_stop(netif);
|
|
|
|
dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DHCP_AUTOIP_COOP */
|
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));
|
2003-02-20 08:41:59 +00:00
|
|
|
netif_set_ipaddr(netif, &dhcp->offered_ip_addr);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): SN: 0x%08"X32_F"\n", sn_mask.addr));
|
2003-02-20 08:41:59 +00:00
|
|
|
netif_set_netmask(netif, &sn_mask);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): GW: 0x%08"X32_F"\n", gw_addr.addr));
|
2003-02-20 08:41:59 +00:00
|
|
|
netif_set_gw(netif, &gw_addr);
|
2004-10-14 11:57:53 +00:00
|
|
|
/* bring the interface up */
|
|
|
|
netif_set_up(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* netif is now bound to DHCP leased address */
|
|
|
|
dhcp_set_state(dhcp, DHCP_BOUND);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Renew an existing DHCP lease at the involved DHCP server.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* @param netif network interface which must renew its lease
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
err_t
|
|
|
|
dhcp_renew(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
|
|
|
err_t result;
|
2002-11-08 15:57:23 +00:00
|
|
|
u16_t msecs;
|
2009-04-15 18:35:04 +00:00
|
|
|
#if LWIP_NETIF_HOSTNAME
|
|
|
|
const char *p;
|
|
|
|
#endif /* LWIP_NETIF_HOSTNAME */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_renew()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_RENEWING);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
if (result == ERR_OK) {
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_REQUEST);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
|
2009-04-15 18:35:04 +00:00
|
|
|
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
|
|
|
|
|
|
|
|
#if LWIP_NETIF_HOSTNAME
|
|
|
|
p = (const char*)netif->hostname;
|
|
|
|
if (p != NULL) {
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p));
|
|
|
|
while (*p) {
|
|
|
|
dhcp_option_byte(dhcp, *p++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_NETIF_HOSTNAME */
|
2002-11-08 15:57:23 +00:00
|
|
|
|
|
|
|
#if 0
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
|
2002-11-08 15:57:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
|
2002-11-08 15:57:23 +00:00
|
|
|
#endif
|
2003-02-20 08:41:59 +00:00
|
|
|
/* append DHCP message trailer */
|
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
|
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2003-03-31 08:02:52 +00:00
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_renew: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
dhcp->tries++;
|
|
|
|
/* back-off on retries, but to a maximum of 20 seconds */
|
|
|
|
msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-11-25 14:23:59 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
|
2003-02-20 08:41:59 +00:00
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Rebind with a DHCP server for an existing DHCP lease.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* @param netif network interface which must rebind with a DHCP server
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static err_t
|
|
|
|
dhcp_rebind(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
|
|
|
err_t result;
|
|
|
|
u16_t msecs;
|
2009-04-15 18:35:04 +00:00
|
|
|
#if LWIP_NETIF_HOSTNAME
|
|
|
|
const char *p;
|
|
|
|
#endif /* LWIP_NETIF_HOSTNAME */
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_set_state(dhcp, DHCP_REBINDING);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (result == ERR_OK) {
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_REQUEST);
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
|
2009-04-15 18:35:04 +00:00
|
|
|
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
|
|
|
|
|
|
|
|
#if LWIP_NETIF_HOSTNAME
|
|
|
|
p = (const char*)netif->hostname;
|
|
|
|
if (p != NULL) {
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p));
|
|
|
|
while (*p) {
|
|
|
|
dhcp_option_byte(dhcp, *p++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_NETIF_HOSTNAME */
|
2002-11-08 15:57:23 +00:00
|
|
|
|
|
|
|
#if 0
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
|
|
|
|
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
|
2002-11-08 15:57:23 +00:00
|
|
|
#endif
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
|
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
|
|
|
|
2004-03-11 20:25:35 +00:00
|
|
|
/* broadcast to server */
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
|
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_rebind: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
dhcp->tries++;
|
|
|
|
msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-11-25 14:23:59 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
|
2003-02-20 08:41:59 +00:00
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
2003-02-20 08:41:59 +00:00
|
|
|
* Release a DHCP lease.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* @param netif network interface which must release its lease
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
err_t
|
|
|
|
dhcp_release(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
|
|
|
err_t result;
|
|
|
|
u16_t msecs;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_release()\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* idle DHCP client */
|
|
|
|
dhcp_set_state(dhcp, DHCP_OFF);
|
2004-06-30 18:42:33 +00:00
|
|
|
/* clean old DHCP offer */
|
2004-07-22 22:10:17 +00:00
|
|
|
dhcp->server_ip_addr.addr = 0;
|
|
|
|
dhcp->offered_ip_addr.addr = dhcp->offered_sn_mask.addr = 0;
|
|
|
|
dhcp->offered_gw_addr.addr = dhcp->offered_bc_addr.addr = 0;
|
2004-07-22 22:07:00 +00:00
|
|
|
dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
|
|
|
|
dhcp->dns_count = 0;
|
2004-06-30 18:42:33 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* create and initialize the DHCP message header */
|
2003-02-20 13:13:51 +00:00
|
|
|
result = dhcp_create_request(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
if (result == ERR_OK) {
|
|
|
|
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
|
|
|
|
dhcp_option_byte(dhcp, DHCP_RELEASE);
|
|
|
|
|
|
|
|
dhcp_option_trailer(dhcp);
|
|
|
|
|
|
|
|
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
|
|
|
|
|
|
|
|
udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
|
2007-11-27 20:13:16 +00:00
|
|
|
udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_delete_request(netif);
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n"));
|
2003-03-31 08:02:52 +00:00
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_release: could not allocate DHCP request\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
dhcp->tries++;
|
|
|
|
msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
|
|
|
|
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release(): set request timeout %"U16_F" msecs\n", msecs));
|
2004-10-14 11:57:53 +00:00
|
|
|
/* bring the interface down */
|
|
|
|
netif_set_down(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* remove IP address from interface */
|
|
|
|
netif_set_ipaddr(netif, IP_ADDR_ANY);
|
|
|
|
netif_set_gw(netif, IP_ADDR_ANY);
|
|
|
|
netif_set_netmask(netif, IP_ADDR_ANY);
|
2004-10-14 11:57:53 +00:00
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* TODO: netif_down(netif); */
|
|
|
|
return result;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2007-11-25 14:23:59 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Remove the DHCP client from the interface.
|
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* @param netif The network interface to stop DHCP on
|
2002-12-01 19:49:45 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
void
|
|
|
|
dhcp_stop(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);
|
2008-04-15 17:24:54 +00:00
|
|
|
/* Remove the flag that says this netif is handled by DHCP. */
|
|
|
|
netif->flags &= ~NETIF_FLAG_DHCP;
|
2003-05-20 09:04:10 +00:00
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 3, ("dhcp_stop()\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* netif is DHCP configured? */
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp != NULL) {
|
2009-05-02 15:40:39 +00:00
|
|
|
#if LWIP_DHCP_AUTOIP_COOP
|
|
|
|
if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
|
|
|
|
autoip_stop(netif);
|
|
|
|
dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DHCP_AUTOIP_COOP */
|
|
|
|
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->pcb != NULL) {
|
2003-02-20 08:41:59 +00:00
|
|
|
udp_remove(dhcp->pcb);
|
|
|
|
dhcp->pcb = NULL;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->p != NULL) {
|
2003-02-20 08:41:59 +00:00
|
|
|
pbuf_free(dhcp->p);
|
|
|
|
dhcp->p = NULL;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-03-16 22:36:54 +00:00
|
|
|
/* free unfolded reply */
|
|
|
|
dhcp_free_reply(dhcp);
|
2003-02-20 08:41:59 +00:00
|
|
|
mem_free((void *)dhcp);
|
|
|
|
netif->dhcp = NULL;
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/*
|
|
|
|
* Set the DHCP state of a DHCP client.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* If the state changed, reset the number of tries.
|
|
|
|
*
|
|
|
|
* TODO: we might also want to reset the timeout here?
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-11-25 14:23:59 +00:00
|
|
|
if (new_state != dhcp->state) {
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->state = new_state;
|
|
|
|
dhcp->tries = 0;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/*
|
|
|
|
* Concatenate an option type and length field to the outgoing
|
|
|
|
* DHCP message.
|
|
|
|
*
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-11-25 11:51:44 +00:00
|
|
|
LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
/*
|
|
|
|
* Concatenate a single byte to the outgoing DHCP message.
|
|
|
|
*
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_option_byte(struct dhcp *dhcp, u8_t value)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-11-25 11:51:44 +00:00
|
|
|
LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = value;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2007-11-25 14:23:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
dhcp_option_short(struct dhcp *dhcp, u16_t value)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-03-26 17:15:23 +00:00
|
|
|
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2007-11-25 14:23:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
dhcp_option_long(struct dhcp *dhcp, u32_t value)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-03-26 17:15:23 +00:00
|
|
|
LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-03-16 22:36:54 +00:00
|
|
|
* Extract the DHCP message and the DHCP options.
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
2003-03-16 22:36:54 +00:00
|
|
|
* Extract the DHCP message and the DHCP options, each into a contiguous
|
|
|
|
* piece of memory. As a DHCP message is variable sized by its options,
|
|
|
|
* and also allows overriding some fields for options, the easy approach
|
|
|
|
* is to first unfold the options into a conitguous piece of memory, and
|
|
|
|
* use that further on.
|
2003-06-11 22:34:51 +00:00
|
|
|
*
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static err_t
|
|
|
|
dhcp_unfold_reply(struct dhcp *dhcp)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-11-25 11:51:44 +00:00
|
|
|
u16_t ret;
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("dhcp != NULL", (dhcp != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("dhcp->p != NULL", (dhcp->p != NULL), return ERR_VAL;);
|
2003-03-16 22:36:54 +00:00
|
|
|
/* free any left-overs from previous unfolds */
|
|
|
|
dhcp_free_reply(dhcp);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* options present? */
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->p->tot_len > (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN)) {
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->options_in_len = dhcp->p->tot_len - (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
|
|
|
|
dhcp->options_in = mem_malloc(dhcp->options_in_len);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->options_in == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->options\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dhcp->msg_in = mem_malloc(sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
|
2007-11-25 14:23:59 +00:00
|
|
|
if (dhcp->msg_in == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->msg_in\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
mem_free((void *)dhcp->options_in);
|
|
|
|
dhcp->options_in = NULL;
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
2007-11-25 11:51:44 +00:00
|
|
|
/** copy the DHCP message without options */
|
|
|
|
ret = pbuf_copy_partial(dhcp->p, dhcp->msg_in, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN, 0);
|
|
|
|
LWIP_ASSERT("ret == sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN", ret == sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
|
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes into dhcp->msg_in[]\n",
|
|
|
|
sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN));
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->options_in != NULL) {
|
2007-11-25 11:51:44 +00:00
|
|
|
/** copy the DHCP options */
|
|
|
|
ret = pbuf_copy_partial(dhcp->p, dhcp->options_in, dhcp->options_in_len, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
|
|
|
|
LWIP_ASSERT("ret == dhcp->options_in_len", ret == dhcp->options_in_len);
|
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes to dhcp->options_in[]\n",
|
|
|
|
dhcp->options_in_len));
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
2008-01-21 16:55:26 +00:00
|
|
|
LWIP_UNUSED_ARG(ret);
|
2003-02-20 08:41:59 +00:00
|
|
|
return ERR_OK;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-06-11 22:34:51 +00:00
|
|
|
* Free the incoming DHCP message including contiguous copy of
|
2003-02-20 08:41:59 +00:00
|
|
|
* its DHCP options.
|
2002-11-08 15:57:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2003-02-20 08:41:59 +00:00
|
|
|
static void dhcp_free_reply(struct dhcp *dhcp)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
if (dhcp->msg_in != NULL) {
|
|
|
|
mem_free((void *)dhcp->msg_in);
|
|
|
|
dhcp->msg_in = NULL;
|
|
|
|
}
|
|
|
|
if (dhcp->options_in) {
|
|
|
|
mem_free((void *)dhcp->options_in);
|
|
|
|
dhcp->options_in = NULL;
|
|
|
|
dhcp->options_in_len = 0;
|
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG, ("dhcp_free_reply(): free'd\n"));
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-05-18 22:29:10 +00:00
|
|
|
* If an incoming DHCP message is in response to us, then trigger the state machine
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
|
|
|
static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
|
|
|
|
{
|
2003-02-20 08:41:59 +00:00
|
|
|
struct netif *netif = (struct netif *)arg;
|
|
|
|
struct dhcp *dhcp = netif->dhcp;
|
2002-11-08 15:57:23 +00:00
|
|
|
struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
|
2003-02-20 08:41:59 +00:00
|
|
|
u8_t *options_ptr;
|
|
|
|
u8_t msg_type;
|
|
|
|
u8_t i;
|
2007-03-30 08:47:04 +00:00
|
|
|
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,
|
2005-11-25 12:03:38 +00:00
|
|
|
(u16_t)(ntohl(addr->addr) >> 24 & 0xff), (u16_t)(ntohl(addr->addr) >> 16 & 0xff),
|
|
|
|
(u16_t)(ntohl(addr->addr) >> 8 & 0xff), (u16_t)(ntohl(addr->addr) & 0xff), port));
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
|
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
|
2003-05-18 22:29:10 +00:00
|
|
|
/* prevent warnings about unused arguments */
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
|
|
|
LWIP_UNUSED_ARG(addr);
|
|
|
|
LWIP_UNUSED_ARG(port);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->p = p;
|
2003-05-18 22:29:10 +00:00
|
|
|
/* TODO: check packet length before reading them */
|
2003-02-20 08:41:59 +00:00
|
|
|
if (reply_msg->op != DHCP_BOOTREPLY) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
|
2007-12-02 14:40:32 +00:00
|
|
|
goto free_pbuf_and_return;
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
/* iterate through hardware address and match against DHCP message */
|
|
|
|
for (i = 0; i < netif->hwaddr_len; i++) {
|
2003-06-11 22:34:51 +00:00
|
|
|
if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
|
2005-11-25 12:03:38 +00:00
|
|
|
(u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
|
2007-12-02 14:40:32 +00:00
|
|
|
goto free_pbuf_and_return;
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* match transaction ID against what we expected */
|
|
|
|
if (ntohl(reply_msg->xid) != dhcp->xid) {
|
2007-03-30 08:47:04 +00:00
|
|
|
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));
|
2007-12-02 14:40:32 +00:00
|
|
|
goto free_pbuf_and_return;
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
/* option fields could be unfold? */
|
|
|
|
if (dhcp_unfold_reply(dhcp) != ERR_OK) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("problem unfolding DHCP message - too short on memory?\n"));
|
2007-12-02 14:40:32 +00:00
|
|
|
goto free_pbuf_and_return;
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
2003-06-11 22:34:51 +00:00
|
|
|
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
|
2003-06-11 22:34:51 +00:00
|
|
|
/* obtain pointer to DHCP message type */
|
2003-02-20 08:41:59 +00:00
|
|
|
options_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_MESSAGE_TYPE);
|
|
|
|
if (options_ptr == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
|
2007-12-02 14:40:32 +00:00
|
|
|
goto free_pbuf_and_return;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
|
|
|
|
/* read DHCP message type */
|
|
|
|
msg_type = dhcp_get_option_byte(options_ptr + 2);
|
|
|
|
/* message type is DHCP ACK? */
|
|
|
|
if (msg_type == DHCP_ACK) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_ACK received\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* in requesting state? */
|
|
|
|
if (dhcp->state == DHCP_REQUESTING) {
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_handle_ack(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->request_timeout = 0;
|
2002-11-08 15:57:23 +00:00
|
|
|
#if DHCP_DOES_ARP_CHECK
|
2003-02-20 08:41:59 +00:00
|
|
|
/* check if the acknowledged lease address is already in use */
|
|
|
|
dhcp_check(netif);
|
2002-11-08 15:57:23 +00:00
|
|
|
#else
|
2003-02-20 08:41:59 +00:00
|
|
|
/* bind interface to the acknowledged lease address */
|
|
|
|
dhcp_bind(netif);
|
2002-11-08 15:57:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
/* already bound to the given lease address? */
|
|
|
|
else if ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING)) {
|
|
|
|
dhcp->request_timeout = 0;
|
|
|
|
dhcp_bind(netif);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
/* received a DHCP_NAK in appropriate state? */
|
|
|
|
else if ((msg_type == DHCP_NAK) &&
|
2003-06-11 22:34:51 +00:00
|
|
|
((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REQUESTING) ||
|
2003-02-20 08:41:59 +00:00
|
|
|
(dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING ))) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_NAK received\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->request_timeout = 0;
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_handle_nak(netif);
|
2003-02-20 08:41:59 +00:00
|
|
|
}
|
|
|
|
/* received a DHCP_OFFER in DHCP_SELECTING state? */
|
|
|
|
else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_SELECTING)) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("DHCP_OFFER received in DHCP_SELECTING state\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->request_timeout = 0;
|
|
|
|
/* remember offered lease */
|
2003-02-20 13:13:51 +00:00
|
|
|
dhcp_handle_offer(netif);
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
2007-12-02 14:40:32 +00:00
|
|
|
free_pbuf_and_return:
|
2009-04-15 19:53:59 +00:00
|
|
|
dhcp_free_reply(dhcp);
|
2002-11-08 15:57:23 +00:00
|
|
|
pbuf_free(p);
|
2003-05-20 09:49:04 +00:00
|
|
|
dhcp->p = NULL;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2007-11-25 14:23:59 +00:00
|
|
|
/**
|
|
|
|
* Create a DHCP request, fill in common headers
|
|
|
|
*
|
|
|
|
* @param netif the netif under DHCP control
|
|
|
|
*/
|
|
|
|
static err_t
|
|
|
|
dhcp_create_request(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-06-17 14:39:50 +00:00
|
|
|
struct dhcp *dhcp;
|
2002-11-08 15:57:23 +00:00
|
|
|
u16_t i;
|
2009-04-15 18:48:10 +00:00
|
|
|
#ifndef DHCP_GLOBAL_XID
|
|
|
|
/** default global transaction identifier starting value (easy to match
|
|
|
|
* with a packet analyser). We simply increment for each new request.
|
|
|
|
* Predefine DHCP_GLOBAL_XID to a better value or a function call to generate one
|
|
|
|
* at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */
|
|
|
|
static u32_t xid = 0xABCD0000;
|
|
|
|
#else
|
|
|
|
static u32_t xid;
|
|
|
|
static u8_t xid_initialised = 0;
|
|
|
|
if (!xid_initialised) {
|
|
|
|
xid = DHCP_GLOBAL_XID;
|
|
|
|
xid_initialised = !xid_initialised;
|
|
|
|
}
|
|
|
|
#endif
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("dhcp_create_request: netif != NULL", (netif != NULL), return ERR_ARG;);
|
2007-06-17 14:39:50 +00:00
|
|
|
dhcp = netif->dhcp;
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("dhcp_create_request: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
|
2003-02-20 08:41:59 +00:00
|
|
|
LWIP_ASSERT("dhcp_create_request: dhcp->p_out == NULL", dhcp->p_out == NULL);
|
|
|
|
LWIP_ASSERT("dhcp_create_request: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
|
|
|
|
dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
|
|
|
|
if (dhcp->p_out == NULL) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("dhcp_create_request(): could not allocate pbuf\n"));
|
2002-11-08 15:57:23 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2007-11-25 11:51:44 +00:00
|
|
|
LWIP_ASSERT("dhcp_create_request: check that first pbuf can hold struct dhcp_msg",
|
2007-07-03 20:28:35 +00:00
|
|
|
(dhcp->p_out->len >= sizeof(struct dhcp_msg)));
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* give unique transaction identifier to this request */
|
2003-06-11 22:34:51 +00:00
|
|
|
dhcp->xid = xid++;
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("transaction id xid++(%"X32_F") dhcp->xid(%"U32_F")\n",xid,dhcp->xid));
|
2003-02-20 08:41:59 +00:00
|
|
|
|
|
|
|
dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
|
|
|
|
|
|
|
|
dhcp->msg_out->op = DHCP_BOOTREQUEST;
|
2003-06-11 22:34:51 +00:00
|
|
|
/* TODO: make link layer independent */
|
|
|
|
dhcp->msg_out->htype = DHCP_HTYPE_ETH;
|
|
|
|
/* TODO: make link layer independent */
|
|
|
|
dhcp->msg_out->hlen = DHCP_HLEN_ETH;
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->msg_out->hops = 0;
|
2003-06-11 22:34:51 +00:00
|
|
|
dhcp->msg_out->xid = htonl(dhcp->xid);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->msg_out->secs = 0;
|
|
|
|
dhcp->msg_out->flags = 0;
|
2009-04-28 19:29:46 +00:00
|
|
|
dhcp->msg_out->ciaddr.addr = 0;
|
|
|
|
if (dhcp->state==DHCP_BOUND || dhcp->state==DHCP_RENEWING || dhcp->state==DHCP_REBINDING) {
|
|
|
|
dhcp->msg_out->ciaddr.addr = netif->ip_addr.addr;
|
|
|
|
}
|
2004-04-30 01:00:21 +00:00
|
|
|
dhcp->msg_out->yiaddr.addr = 0;
|
|
|
|
dhcp->msg_out->siaddr.addr = 0;
|
|
|
|
dhcp->msg_out->giaddr.addr = 0;
|
2003-02-20 08:41:59 +00:00
|
|
|
for (i = 0; i < DHCP_CHADDR_LEN; i++) {
|
|
|
|
/* copy netif hardware address, pad with zeroes */
|
|
|
|
dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len) ? netif->hwaddr[i] : 0/* pad byte*/;
|
|
|
|
}
|
2007-11-25 14:23:59 +00:00
|
|
|
for (i = 0; i < DHCP_SNAME_LEN; i++) {
|
|
|
|
dhcp->msg_out->sname[i] = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < DHCP_FILE_LEN; i++) {
|
|
|
|
dhcp->msg_out->file[i] = 0;
|
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->msg_out->cookie = htonl(0x63825363UL);
|
|
|
|
dhcp->options_out_len = 0;
|
|
|
|
/* fill options field with an incrementing array (for debugging purposes) */
|
2007-11-25 14:23:59 +00:00
|
|
|
for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
|
2007-03-26 17:15:23 +00:00
|
|
|
dhcp->msg_out->options[i] = (u8_t)i; /* for debugging only, no matter if truncated */
|
2007-11-25 14:23:59 +00:00
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2007-11-25 14:23:59 +00:00
|
|
|
/**
|
|
|
|
* Free previously allocated memory used to send a DHCP request.
|
|
|
|
*
|
|
|
|
* @param netif the netif under DHCP control
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dhcp_delete_request(struct netif *netif)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-06-17 14:39:50 +00:00
|
|
|
struct dhcp *dhcp;
|
2007-11-25 11:51:44 +00:00
|
|
|
LWIP_ERROR("dhcp_delete_request: netif != NULL", (netif != NULL), return;);
|
2007-06-17 14:39:50 +00:00
|
|
|
dhcp = netif->dhcp;
|
2007-11-25 11:51:44 +00:00
|
|
|
LWIP_ERROR("dhcp_delete_request: dhcp != NULL", (dhcp != NULL), return;);
|
|
|
|
LWIP_ASSERT("dhcp_delete_request: dhcp->p_out != NULL", dhcp->p_out != NULL);
|
|
|
|
LWIP_ASSERT("dhcp_delete_request: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
|
2007-06-17 15:20:21 +00:00
|
|
|
if (dhcp->p_out != NULL) {
|
|
|
|
pbuf_free(dhcp->p_out);
|
|
|
|
}
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->p_out = NULL;
|
|
|
|
dhcp->msg_out = NULL;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a DHCP message trailer
|
|
|
|
*
|
2003-02-20 08:41:59 +00:00
|
|
|
* Adds the END option to the DHCP message, and if
|
|
|
|
* necessary, up to three padding bytes.
|
2007-11-25 14:23:59 +00:00
|
|
|
*
|
|
|
|
* @param dhcp DHCP state structure
|
2002-11-08 15:57:23 +00:00
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static void
|
|
|
|
dhcp_option_trailer(struct dhcp *dhcp)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2007-11-25 11:51:44 +00:00
|
|
|
LWIP_ERROR("dhcp_option_trailer: dhcp != NULL", (dhcp != NULL), return;);
|
2003-03-31 10:32:35 +00:00
|
|
|
LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
|
|
|
|
LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
|
2003-02-20 08:41:59 +00:00
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
|
|
|
|
/* packet is too small, or not 4 byte aligned? */
|
|
|
|
while ((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) {
|
2005-11-25 12:03:38 +00:00
|
|
|
/* 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)); */
|
2003-03-31 10:32:35 +00:00
|
|
|
LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
|
2003-02-20 08:41:59 +00:00
|
|
|
/* add a fill/padding byte */
|
|
|
|
dhcp->msg_out->options[dhcp->options_out_len++] = 0;
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Find the offset of a DHCP option inside the DHCP message.
|
|
|
|
*
|
2007-11-25 14:23:59 +00:00
|
|
|
* @param dhcp DHCP client
|
2002-12-01 19:49:45 +00:00
|
|
|
* @param option_type
|
|
|
|
*
|
|
|
|
* @return a byte offset into the UDP message where the option was found, or
|
|
|
|
* zero if the given option was not found.
|
|
|
|
*/
|
2003-02-20 08:41:59 +00:00
|
|
|
static u8_t *dhcp_get_option_ptr(struct dhcp *dhcp, u8_t option_type)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
|
|
|
u8_t overload = DHCP_OVERLOAD_NONE;
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* options available? */
|
|
|
|
if ((dhcp->options_in != NULL) && (dhcp->options_in_len > 0)) {
|
|
|
|
/* start with options field */
|
|
|
|
u8_t *options = (u8_t *)dhcp->options_in;
|
|
|
|
u16_t offset = 0;
|
|
|
|
/* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
|
|
|
|
while ((offset < dhcp->options_in_len) && (options[offset] != DHCP_OPTION_END)) {
|
2005-11-25 12:03:38 +00:00
|
|
|
/* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
|
2003-02-20 08:41:59 +00:00
|
|
|
/* are the sname and/or file field overloaded with options? */
|
|
|
|
if (options[offset] == DHCP_OPTION_OVERLOAD) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 2, ("overloaded message detected\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* skip option type and length */
|
|
|
|
offset += 2;
|
|
|
|
overload = options[offset++];
|
|
|
|
}
|
|
|
|
/* requested option found */
|
|
|
|
else if (options[offset] == option_type) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("option found at offset %"U16_F" in options\n", offset));
|
2003-02-20 08:41:59 +00:00
|
|
|
return &options[offset];
|
|
|
|
/* skip option */
|
|
|
|
} else {
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", options[offset]));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* skip option type */
|
|
|
|
offset++;
|
|
|
|
/* skip option length, and then length bytes */
|
|
|
|
offset += 1 + options[offset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* is this an overloaded message? */
|
|
|
|
if (overload != DHCP_OVERLOAD_NONE) {
|
|
|
|
u16_t field_len;
|
|
|
|
if (overload == DHCP_OVERLOAD_FILE) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("overloaded file field\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
options = (u8_t *)&dhcp->msg_in->file;
|
|
|
|
field_len = DHCP_FILE_LEN;
|
|
|
|
} else if (overload == DHCP_OVERLOAD_SNAME) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("overloaded sname field\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
options = (u8_t *)&dhcp->msg_in->sname;
|
|
|
|
field_len = DHCP_SNAME_LEN;
|
|
|
|
/* TODO: check if else if () is necessary */
|
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | 1, ("overloaded sname and file field\n"));
|
2003-02-20 08:41:59 +00:00
|
|
|
options = (u8_t *)&dhcp->msg_in->sname;
|
|
|
|
field_len = DHCP_FILE_LEN + DHCP_SNAME_LEN;
|
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
offset = 0;
|
|
|
|
|
2003-02-20 08:41:59 +00:00
|
|
|
/* at least 1 byte to read and no end marker */
|
|
|
|
while ((offset < field_len) && (options[offset] != DHCP_OPTION_END)) {
|
|
|
|
if (options[offset] == option_type) {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("option found at offset=%"U16_F"\n", offset));
|
2003-02-20 08:41:59 +00:00
|
|
|
return &options[offset];
|
|
|
|
/* skip option */
|
|
|
|
} else {
|
2007-03-30 08:47:04 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("skipping option %"U16_F"\n", options[offset]));
|
2003-02-20 08:41:59 +00:00
|
|
|
/* skip option type */
|
|
|
|
offset++;
|
|
|
|
offset += 1 + options[offset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-08 00:16:56 +00:00
|
|
|
return NULL;
|
2002-11-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Return the byte of DHCP option data.
|
|
|
|
*
|
|
|
|
* @param client DHCP client.
|
|
|
|
* @param ptr pointer obtained by dhcp_get_option_ptr().
|
|
|
|
*
|
|
|
|
* @return byte value at the given address.
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static u8_t
|
|
|
|
dhcp_get_option_byte(u8_t *ptr)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG, ("option byte value=%"U16_F"\n", (u16_t)(*ptr)));
|
2003-02-20 08:41:59 +00:00
|
|
|
return *ptr;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2007-11-25 14:23:59 +00:00
|
|
|
#if 0 /* currently unused */
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Return the 16-bit value of DHCP option data.
|
|
|
|
*
|
|
|
|
* @param client DHCP client.
|
|
|
|
* @param ptr pointer obtained by dhcp_get_option_ptr().
|
|
|
|
*
|
|
|
|
* @return byte value at the given address.
|
|
|
|
*/
|
2007-11-25 14:23:59 +00:00
|
|
|
static u16_t
|
|
|
|
dhcp_get_option_short(u8_t *ptr)
|
2002-11-08 15:57:23 +00:00
|
|
|
{
|
|
|
|
u16_t value;
|
2003-02-20 08:41:59 +00:00
|
|
|
value = *ptr++ << 8;
|
|
|
|
value |= *ptr;
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG, ("option short value=%"U16_F"\n", value));
|
2002-11-08 15:57:23 +00:00
|
|
|
return value;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2006-06-08 00:16:56 +00:00
|
|
|
#endif
|
2002-11-08 15:57:23 +00:00
|
|
|
|
2002-12-01 19:49:45 +00:00
|
|
|
/**
|
|
|
|
* Return the 32-bit value of DHCP option data.
|
|
|
|
*
|
|
|
|
* @param client DHCP client.
|
|
|
|
* @param ptr pointer obtained by dhcp_get_option_ptr().
|
|
|
|
*
|
|
|
|
* @return byte value at the given address.
|
|
|
|
*/
|
2002-11-08 15:57:23 +00:00
|
|
|
static u32_t dhcp_get_option_long(u8_t *ptr)
|
|
|
|
{
|
|
|
|
u32_t value;
|
2003-02-20 08:41:59 +00:00
|
|
|
value = (u32_t)(*ptr++) << 24;
|
|
|
|
value |= (u32_t)(*ptr++) << 16;
|
|
|
|
value |= (u32_t)(*ptr++) << 8;
|
|
|
|
value |= (u32_t)(*ptr++);
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(DHCP_DEBUG, ("option long value=%"U32_F"\n", value));
|
2003-02-20 08:41:59 +00:00
|
|
|
return value;
|
2003-06-11 22:34:51 +00:00
|
|
|
}
|
2004-02-16 21:33:42 +00:00
|
|
|
|
|
|
|
#endif /* LWIP_DHCP */
|