ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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