Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

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