Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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