Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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