Microchip® Advanced Software Framework

dhcp.c File Reference

Dynamic Host Configuration Protocol client.

#include "lwip/opt.h"
#include "lwip/stats.h"
#include "lwip/mem.h"
#include "lwip/udp.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/def.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
#include "lwip/dns.h"
#include "netif/etharp.h"
#include <string.h>

Macros

#define dhcp_clear_all_options(dhcp)   (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given)))
 
#define dhcp_clear_option(dhcp, idx)   (dhcp_rx_options_given[idx] = 0)
 
#define DHCP_CREATE_RAND_XID   1
 DHCP_CREATE_RAND_XID: if this is set to 1, the xid is created using LWIP_RAND() (this overrides DHCP_GLOBAL_XID) More...
 
#define dhcp_get_option_value(dhcp, idx)   (dhcp_rx_options_val[idx])
 
#define dhcp_got_option(dhcp, idx)   (dhcp_rx_options_given[idx] = 1)
 
#define DHCP_MAX_MSG_LEN(netif)   (netif->mtu)
 Default for DHCP_GLOBAL_XID is 0xABCD0000 This can be changed by defining DHCP_GLOBAL_XID and DHCP_GLOBAL_XID_HEADER, e.g. More...
 
#define DHCP_MAX_MSG_LEN_MIN_REQUIRED   576
 
#define DHCP_MIN_REPLY_LEN   44
 Minimum length for reply before packet is parsed. More...
 
#define dhcp_option_given(dhcp, idx)   (dhcp_rx_options_given[idx] != 0)
 
#define DHCP_OPTION_IDX_DNS_SERVER   8
 
#define DHCP_OPTION_IDX_LEASE_TIME   3
 
#define DHCP_OPTION_IDX_MAX   (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS)
 
#define DHCP_OPTION_IDX_MSG_TYPE   1
 
#define DHCP_OPTION_IDX_OVERLOAD   0
 Option handling: options are parsed in dhcp_parse_reply and saved in an array where other functions can load them from. More...
 
#define DHCP_OPTION_IDX_ROUTER   7
 
#define DHCP_OPTION_IDX_SERVER_ID   2
 
#define DHCP_OPTION_IDX_SUBNET_MASK   6
 
#define DHCP_OPTION_IDX_T1   4
 
#define DHCP_OPTION_IDX_T2   5
 
#define dhcp_set_option_value(dhcp, idx, val)   (dhcp_rx_options_val[idx] = (val))
 
#define REBOOT_TRIES   2
 

Functions

void dhcp_arp_reply (struct netif *netif, ip_addr_t *addr)
 Match an ARP reply with the offered IP address. More...
 
static void dhcp_bind (struct netif *netif)
 Bind the interface to the offered IP address. More...
 
static void dhcp_check (struct netif *netif)
 Checks if the offered IP address is already in use. More...
 
void dhcp_cleanup (struct netif *netif)
 Removes a struct dhcp from a netif. More...
 
void dhcp_coarse_tmr ()
 The DHCP timer that checks for lease renewal/rebind timeouts. More...
 
static err_t dhcp_create_msg (struct netif *netif, struct dhcp *dhcp, u8_t message_type)
 Create a DHCP request, fill in common headers. More...
 
static err_t dhcp_decline (struct netif *netif)
 Decline an offered lease. More...
 
static void dhcp_delete_msg (struct dhcp *dhcp)
 Free previously allocated memory used to send a DHCP request. More...
 
static err_t dhcp_discover (struct netif *netif)
 Start the DHCP process, discover a DHCP server. More...
 
void dhcp_fine_tmr ()
 DHCP transaction timeout handling. More...
 
static void dhcp_handle_ack (struct netif *netif)
 Handle a DHCP ACK packet. More...
 
static void dhcp_handle_nak (struct netif *netif)
 Back-off the DHCP client (because of a received NAK response). More...
 
static void dhcp_handle_offer (struct netif *netif)
 Remember the configuration offered by a DHCP server. More...
 
void dhcp_inform (struct netif *netif)
 Inform a DHCP server of our manual configuration. More...
 
void dhcp_network_changed (struct netif *netif)
 Handle a possible change in the network configuration. More...
 
static void dhcp_option (struct dhcp *dhcp, u8_t option_type, u8_t option_len)
 
static void dhcp_option_byte (struct dhcp *dhcp, u8_t value)
 
static void dhcp_option_long (struct dhcp *dhcp, u32_t value)
 
static void dhcp_option_short (struct dhcp *dhcp, u16_t value)
 
static void dhcp_option_trailer (struct dhcp *dhcp)
 Add a DHCP message trailer. More...
 
static err_t dhcp_parse_reply (struct dhcp *dhcp, struct pbuf *p)
 Extract the DHCP message and the DHCP options. More...
 
static err_t dhcp_rebind (struct netif *netif)
 Rebind with a DHCP server for an existing DHCP lease. More...
 
static err_t dhcp_reboot (struct netif *netif)
 Enter REBOOTING state to verify an existing lease. More...
 
static void dhcp_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
 If an incoming DHCP message is in response to us, then trigger the state machine. More...
 
err_t dhcp_release (struct netif *netif)
 Release a DHCP lease. More...
 
err_t dhcp_renew (struct netif *netif)
 Renew an existing DHCP lease at the involved DHCP server. More...
 
static err_t dhcp_select (struct netif *netif)
 Select a DHCP server offer out of all offers. More...
 
static void dhcp_set_state (struct dhcp *dhcp, u8_t new_state)
 
void dhcp_set_struct (struct netif *netif, struct dhcp *dhcp)
 Set a statically allocated struct dhcp to work with. More...
 
err_t dhcp_start (struct netif *netif)
 Start DHCP negotiation for a network interface. More...
 
void dhcp_stop (struct netif *netif)
 Remove the DHCP client from the interface. More...
 
static void dhcp_t1_timeout (struct netif *netif)
 The renewal period has timed out. More...
 
static void dhcp_t2_timeout (struct netif *netif)
 The rebind period has timed out. More...
 
static void dhcp_timeout (struct netif *netif)
 A DHCP negotiation transaction, or ARP request, has timed out. More...
 

Variables

u8_t dhcp_rx_options_given [DHCP_OPTION_IDX_MAX]
 Holds a flag which option was received and is contained in dhcp_rx_options_val, only valid while in dhcp_recv. More...
 
u32_t dhcp_rx_options_val [DHCP_OPTION_IDX_MAX]
 Holds the decoded option values, only valid while in dhcp_recv. More...
 

#define dhcp_clear_all_options (   dhcp)    (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given)))

Referenced by dhcp_parse_reply().

#define dhcp_clear_option (   dhcp,
  idx 
)    (dhcp_rx_options_given[idx] = 0)

Referenced by dhcp_parse_reply().

#define DHCP_CREATE_RAND_XID   1

DHCP_CREATE_RAND_XID: if this is set to 1, the xid is created using LWIP_RAND() (this overrides DHCP_GLOBAL_XID)

#define dhcp_get_option_value (   dhcp,
  idx 
)    (dhcp_rx_options_val[idx])
#define dhcp_got_option (   dhcp,
  idx 
)    (dhcp_rx_options_given[idx] = 1)

Referenced by dhcp_parse_reply().

#define DHCP_MAX_MSG_LEN (   netif)    (netif->mtu)

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() DHCP_OPTION_MAX_MSG_SIZE is set to the MTU MTU is checked to be big enough in dhcp_start

Referenced by dhcp_discover(), dhcp_inform(), dhcp_rebind(), dhcp_renew(), and dhcp_select().

#define DHCP_MAX_MSG_LEN_MIN_REQUIRED   576

Referenced by dhcp_start().

#define DHCP_MIN_REPLY_LEN   44

Minimum length for reply before packet is parsed.

Referenced by dhcp_recv().

#define dhcp_option_given (   dhcp,
  idx 
)    (dhcp_rx_options_given[idx] != 0)
#define DHCP_OPTION_IDX_DNS_SERVER   8
#define DHCP_OPTION_IDX_LEASE_TIME   3
#define DHCP_OPTION_IDX_MAX   (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS)

Referenced by dhcp_parse_reply().

#define DHCP_OPTION_IDX_MSG_TYPE   1

Referenced by dhcp_parse_reply(), and dhcp_recv().

#define DHCP_OPTION_IDX_OVERLOAD   0

Option handling: options are parsed in dhcp_parse_reply and saved in an array where other functions can load them from.

This might be moved into the struct dhcp (not necessarily since lwIP is single-threaded and the array is only used while in recv callback).

Referenced by dhcp_parse_reply().

#define DHCP_OPTION_IDX_ROUTER   7
#define DHCP_OPTION_IDX_SERVER_ID   2
#define DHCP_OPTION_IDX_SUBNET_MASK   6
#define DHCP_OPTION_IDX_T1   4
#define DHCP_OPTION_IDX_T2   5
#define dhcp_set_option_value (   dhcp,
  idx,
  val 
)    (dhcp_rx_options_val[idx] = (val))

Referenced by dhcp_parse_reply().

#define REBOOT_TRIES   2

Referenced by dhcp_timeout().

void dhcp_arp_reply ( struct netif netif,
ip_addr_t addr 
)

Match an ARP reply with the offered IP address.

if enabled, check whether the offered IP address is not in use, using ARP

Parameters
netifthe network interface on which the reply was received
addrThe IP address we received a reply from

References netif::dhcp, DHCP_CHECKING, DHCP_DEBUG, dhcp_decline(), ip4_addr_get_u32, ip_addr_cmp, LWIP_DBG_LEVEL_WARNING, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), NULL, dhcp::offered_ip_addr, dhcp::state, and X32_F.

Referenced by etharp_arp_input().

static void dhcp_check ( struct netif netif)
static

Checks if the offered IP address is already in use.

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.

Parameters
netifthe netif under DHCP control

References netif::dhcp, DHCP_CHECKING, DHCP_DEBUG, DHCP_FINE_TIMER_MSECS, dhcp_set_state(), ERR_OK, etharp_query(), LWIP_DBG_LEVEL_WARNING, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, netif::name, NULL, dhcp::offered_ip_addr, dhcp::request_timeout, dhcp::tries, and U16_F.

Referenced by dhcp_recv(), and dhcp_timeout().

void dhcp_cleanup ( struct netif netif)

Removes a struct dhcp from a netif.

ATTENTION: Only use this when not using dhcp_set_struct() to allocate the struct dhcp since the memory is passed back to the heap.

Parameters
netifthe netif from which to remove the struct dhcp

References netif::dhcp, LWIP_ASSERT, mem_free(), and NULL.

void dhcp_coarse_tmr ( void  )

The DHCP timer that checks for lease renewal/rebind timeouts.

to be called every minute

References netif::dhcp, DHCP_DEBUG, dhcp_t1_timeout(), dhcp_t2_timeout(), LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, netif_list, netif::next, NULL, dhcp::t1_timeout, and dhcp::t2_timeout.

Referenced by dhcp_timer_coarse().

static err_t dhcp_create_msg ( struct netif netif,
struct dhcp dhcp,
u8_t  message_type 
)
static

Create a DHCP request, fill in common headers.

Parameters
netifthe netif under DHCP control
dhcpdhcp control struct
message_typemessage type of the request

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

References DHCP_BOOTREQUEST, DHCP_CHADDR_LEN, DHCP_DEBUG, DHCP_DECLINE, DHCP_FILE_LEN, DHCP_HTYPE_ETH, DHCP_INFORM, DHCP_MAGIC_COOKIE, dhcp_option(), dhcp_option_byte(), DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN, DHCP_OPTIONS_LEN, DHCP_REBINDING, DHCP_RENEWING, DHCP_REQUEST, DHCP_SNAME_LEN, ERR_ARG, ERR_MEM, ERR_OK, ERR_VAL, htonl, netif::hwaddr, netif::hwaddr_len, netif::ip_addr, ip_addr_copy, ip_addr_set_zero, pbuf::len, LWIP_ASSERT, LWIP_DBG_LEVEL_SERIOUS, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), dhcp::msg_out, NULL, dhcp::options_out_len, dhcp::p_out, pbuf::payload, pbuf_alloc(), PBUF_RAM, PBUF_TRANSPORT, PP_HTONL, dhcp::state, dhcp::tries, X32_F, and dhcp::xid.

Referenced by dhcp_decline(), dhcp_discover(), dhcp_inform(), dhcp_rebind(), dhcp_reboot(), dhcp_release(), dhcp_renew(), and dhcp_select().

static err_t dhcp_decline ( struct netif netif)
static

Decline an offered lease.

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).

Parameters
netifthe netif under DHCP control

References netif::dhcp, DHCP_BACKING_OFF, dhcp_create_msg(), DHCP_DEBUG, DHCP_DECLINE, dhcp_delete_msg(), DHCP_FINE_TIMER_MSECS, dhcp_option(), dhcp_option_long(), DHCP_OPTION_REQUESTED_IP, dhcp_option_trailer(), DHCP_OPTIONS_LEN, DHCP_SERVER_PORT, dhcp_set_state(), ERR_OK, ip4_addr_get_u32, IP_ADDR_BROADCAST, LWIP_DBG_LEVEL_SERIOUS, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, ntohl, dhcp::offered_ip_addr, dhcp::options_out_len, dhcp::p_out, pbuf_realloc(), dhcp::pcb, dhcp::request_timeout, dhcp::tries, U16_F, and udp_sendto_if().

Referenced by dhcp_arp_reply().

static void dhcp_delete_msg ( struct dhcp dhcp)
static

Free previously allocated memory used to send a DHCP request.

Parameters
dhcpthe dhcp struct to free the request from

References LWIP_ASSERT, LWIP_ERROR(), dhcp::msg_out, NULL, dhcp::p_out, and pbuf_free().

Referenced by dhcp_decline(), dhcp_discover(), dhcp_inform(), dhcp_rebind(), dhcp_reboot(), dhcp_release(), dhcp_renew(), and dhcp_select().

void dhcp_fine_tmr ( void  )

DHCP transaction timeout handling.

to be called every half second

A DHCP server is expected to respond within a short period of time. This timer checks whether an outstanding DHCP request is timed out.

References netif::dhcp, DHCP_DEBUG, dhcp_timeout(), LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, netif_list, netif::next, NULL, and dhcp::request_timeout.

Referenced by dhcp_timer_fine().

static void dhcp_handle_nak ( struct netif netif)
static

Back-off the DHCP client (because of a received NAK response).

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.

We clear any existing set IP address and restart DHCP negotiation afresh (as per RFC2131 3.2.3).

Parameters
netifthe netif under DHCP control

References netif::dhcp, DHCP_BACKING_OFF, DHCP_DEBUG, dhcp_discover(), dhcp_set_state(), IP_ADDR_ANY, LWIP_DBG_TRACE, LWIP_DEBUGF, netif::name, netif_set_down(), netif_set_gw(), netif_set_ipaddr(), netif_set_netmask(), netif::num, and U16_F.

Referenced by dhcp_recv().

static void dhcp_handle_offer ( struct netif netif)
static
void dhcp_inform ( struct netif netif)

Inform a DHCP server of our manual configuration.

inform server of our manual IP address

This informs DHCP servers of our fixed IP address configuration by sending an INFORM message. It does not involve DHCP address configuration, it is just here to be nice to the network.

Parameters
netifThe lwIP network interface

References netif::dhcp, DHCP_CLIENT_PORT, dhcp_create_msg(), DHCP_DEBUG, dhcp_delete_msg(), DHCP_INFORM, DHCP_MAX_MSG_LEN, dhcp_option(), DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN, dhcp_option_short(), dhcp_option_trailer(), DHCP_OPTIONS_LEN, DHCP_SERVER_PORT, dhcp_set_state(), ERR_OK, IP_ADDR_ANY, IP_ADDR_BROADCAST, ip_set_option, LWIP_DBG_LEVEL_SERIOUS, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), NULL, dhcp::options_out_len, dhcp::p_out, pbuf_realloc(), dhcp::pcb, SOF_BROADCAST, udp_bind(), udp_new(), udp_remove(), and udp_sendto_if().

void dhcp_network_changed ( struct netif netif)

Handle a possible change in the network configuration.

This enters the REBOOTING state to verify that the currently bound address is still valid.

References netif::dhcp, DHCP_AUTOIP_COOP_STATE_OFF, DHCP_AUTOIP_COOP_STATE_ON, DHCP_BOUND, dhcp_discover(), DHCP_OFF, DHCP_REBINDING, dhcp_reboot(), DHCP_REBOOTING, DHCP_RENEWING, netif_set_down(), dhcp::state, and dhcp::tries.

Referenced by netif_set_link_up().

static void dhcp_option ( struct dhcp dhcp,
u8_t  option_type,
u8_t  option_len 
)
static
static void dhcp_option_byte ( struct dhcp dhcp,
u8_t  value 
)
static
static void dhcp_option_long ( struct dhcp dhcp,
u32_t  value 
)
static
static void dhcp_option_short ( struct dhcp dhcp,
u16_t  value 
)
static
static void dhcp_option_trailer ( struct dhcp dhcp)
static

Add a DHCP message trailer.

Adds the END option to the DHCP message, and if necessary, up to three padding bytes.

Parameters
dhcpDHCP state structure

References DHCP_MIN_OPTIONS_LEN, DHCP_OPTION_END, DHCP_OPTIONS_LEN, LWIP_ASSERT, LWIP_ERROR(), dhcp::msg_out, NULL, and dhcp::options_out_len.

Referenced by dhcp_decline(), dhcp_discover(), dhcp_inform(), dhcp_rebind(), dhcp_reboot(), dhcp_release(), dhcp_renew(), and dhcp_select().

void dhcp_set_struct ( struct netif netif,
struct dhcp dhcp 
)

Set a statically allocated struct dhcp to work with.

Using this prevents dhcp_start to allocate it using mem_malloc.

Parameters
netifthe netif for which to set the struct dhcp
dhcp(uninitialised) dhcp struct allocated by the application

References netif::dhcp, LWIP_ASSERT, and NULL.

err_t dhcp_start ( struct netif netif)

Start DHCP negotiation for a network interface.

start DHCP configuration

If no DHCP client instance was attached to this interface, a new client is created first. If a DHCP client instance was already present, it restarts negotiation.

Parameters
netifThe lwIP network interface
Returns
lwIP error code
  • ERR_OK - No error
  • ERR_MEM - Out of memory

References netif::dhcp, DHCP_CLIENT_PORT, DHCP_DEBUG, dhcp_discover(), DHCP_MAX_MSG_LEN_MIN_REQUIRED, dhcp_recv(), DHCP_SERVER_PORT, dhcp_stop(), ERR_ARG, ERR_MEM, ERR_OK, netif::flags, IP_ADDR_ANY, ip_set_option, LWIP_ASSERT, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), mem_malloc(), netif::mtu, netif::name, NETIF_FLAG_DHCP, NETIF_FLAG_ETHARP, NULL, netif::num, SOF_BROADCAST, U16_F, udp_bind(), udp_connect(), udp_new(), udp_recv(), and udp_remove().

Referenced by ethernet_configure_interface().

void dhcp_stop ( struct netif netif)

Remove the DHCP client from the interface.

stop DHCP configuration

Parameters
netifThe network interface to stop DHCP on

References netif::dhcp, DHCP_AUTOIP_COOP_STATE_OFF, DHCP_AUTOIP_COOP_STATE_ON, DHCP_DEBUG, DHCP_OFF, dhcp_set_state(), netif::flags, LWIP_ASSERT, LWIP_DBG_TRACE, LWIP_DEBUGF, LWIP_ERROR(), NETIF_FLAG_DHCP, NULL, and udp_remove().

Referenced by dhcp_start().

static void dhcp_t1_timeout ( struct netif netif)
static

The renewal period has timed out.

Parameters
netifthe netif under DHCP control

References netif::dhcp, DHCP_BOUND, DHCP_DEBUG, dhcp_renew(), DHCP_RENEWING, DHCP_REQUESTING, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, and dhcp::state.

Referenced by dhcp_coarse_tmr().

static void dhcp_t2_timeout ( struct netif netif)
static

The rebind period has timed out.

Parameters
netifthe netif under DHCP control

References netif::dhcp, DHCP_BOUND, DHCP_DEBUG, dhcp_rebind(), DHCP_RENEWING, DHCP_REQUESTING, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, and dhcp::state.

Referenced by dhcp_coarse_tmr().

static void dhcp_timeout ( struct netif netif)
static

A DHCP negotiation transaction, or ARP request, has timed out.

The timer that was started with the DHCP or ARP request has timed out, indicating no response was received in time.

Parameters
netifthe netif under DHCP control

References netif::dhcp, DHCP_BACKING_OFF, dhcp_bind(), dhcp_check(), DHCP_CHECKING, DHCP_DEBUG, dhcp_discover(), dhcp_rebind(), DHCP_REBINDING, dhcp_reboot(), DHCP_REBOOTING, dhcp_release(), dhcp_renew(), DHCP_RENEWING, DHCP_REQUESTING, dhcp_select(), DHCP_SELECTING, LWIP_DBG_STATE, LWIP_DBG_TRACE, LWIP_DEBUGF, REBOOT_TRIES, dhcp::state, and dhcp::tries.

Referenced by dhcp_fine_tmr().

u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX]

Holds a flag which option was received and is contained in dhcp_rx_options_val, only valid while in dhcp_recv.

Todo:
: move this into struct dhcp?
u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX]

Holds the decoded option values, only valid while in dhcp_recv.

Todo:
: move this into struct dhcp?