Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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