SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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