Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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