Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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