Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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