EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
5:dd63a1e02b1b

        

Who changed what in which revision?

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