Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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