Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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