HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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