blob: 93eff872468418a436343c2e6b40578ef1350527 [file] [log] [blame]
wdenk3861aa52002-09-27 23:19:37 +00001/*
2 * Based on LiMon - BOOTP.
3 *
4 * Copyright 1994, 1995, 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
wdenk232c1502004-03-12 00:14:09 +00008 * Copyright 2000-2004 Wolfgang Denk, wd@denx.de
wdenk3861aa52002-09-27 23:19:37 +00009 */
10
wdenk3861aa52002-09-27 23:19:37 +000011#include <common.h>
12#include <command.h>
13#include <net.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020014#include <net/tftp.h>
wdenk3861aa52002-09-27 23:19:37 +000015#include "bootp.h"
wdenk232c1502004-03-12 00:14:09 +000016#include "nfs.h"
wdenk3861aa52002-09-27 23:19:37 +000017#ifdef CONFIG_STATUS_LED
18#include <status_led.h>
19#endif
Kim Phillipsdb7720b2012-07-05 13:19:32 +000020#ifdef CONFIG_BOOTP_RANDOM_DELAY
21#include "net_rand.h"
22#endif
wdenk3861aa52002-09-27 23:19:37 +000023
Joe Hershberger3090b7e32012-05-15 08:59:06 +000024#define BOOTP_VENDOR_MAGIC 0x63825363 /* RFC1048 Magic Cookie */
wdenk3861aa52002-09-27 23:19:37 +000025
Stephen Warrenf59be6e2014-07-25 17:30:48 -060026/*
27 * The timeout for the initial BOOTP/DHCP request used to be described by a
28 * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
29 * that counter
30 *
31 * Now that the timeout periods are variable (exponential backoff and retry)
32 * we convert the timeout count to the absolute time it would have take to
33 * execute that many retries, and keep sending retry packets until that time
34 * is reached.
35 */
wdenk232c1502004-03-12 00:14:09 +000036#ifndef CONFIG_NET_RETRY_COUNT
Joe Hershberger3090b7e32012-05-15 08:59:06 +000037# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
wdenk3861aa52002-09-27 23:19:37 +000038#else
wdenk232c1502004-03-12 00:14:09 +000039# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
wdenk3861aa52002-09-27 23:19:37 +000040#endif
Stephen Warrenf59be6e2014-07-25 17:30:48 -060041#define TIMEOUT_MS ((3 + (TIMEOUT_COUNT * 5)) * 1000)
wdenk3861aa52002-09-27 23:19:37 +000042
Joe Hershberger3090b7e32012-05-15 08:59:06 +000043#define PORT_BOOTPS 67 /* BOOTP server UDP port */
44#define PORT_BOOTPC 68 /* BOOTP client UDP port */
wdenk3861aa52002-09-27 23:19:37 +000045
Joe Hershberger3090b7e32012-05-15 08:59:06 +000046#ifndef CONFIG_DHCP_MIN_EXT_LEN /* minimal length of extension list */
wdenk232c1502004-03-12 00:14:09 +000047#define CONFIG_DHCP_MIN_EXT_LEN 64
wdenk3861aa52002-09-27 23:19:37 +000048#endif
49
Thierry Reding92ac8ac2014-08-19 10:21:24 +020050#ifndef CONFIG_BOOTP_ID_CACHE_SIZE
51#define CONFIG_BOOTP_ID_CACHE_SIZE 4
52#endif
53
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -050054u32 bootp_ids[CONFIG_BOOTP_ID_CACHE_SIZE];
Thierry Reding92ac8ac2014-08-19 10:21:24 +020055unsigned int bootp_num_ids;
Joe Hershberger7044c6b2015-04-08 01:41:09 -050056int bootp_try;
Stephen Warrenf59be6e2014-07-25 17:30:48 -060057ulong bootp_start;
58ulong bootp_timeout;
Joe Hershberger586cbe52015-04-08 01:41:03 -050059char net_nis_domain[32] = {0,}; /* Our NIS domain */
60char net_hostname[32] = {0,}; /* Our hostname */
61char net_root_path[64] = {0,}; /* Our bootpath */
wdenk3861aa52002-09-27 23:19:37 +000062
Jon Loeliger643d1ab2007-07-09 17:45:14 -050063#if defined(CONFIG_CMD_DHCP)
Kim Phillips06370592012-10-29 13:34:33 +000064static dhcp_state_t dhcp_state = INIT;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -050065static u32 dhcp_leasetime;
Joe Hershberger049a95a2015-04-08 01:41:01 -050066static struct in_addr dhcp_server_ip;
67static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
68 unsigned src, unsigned len);
wdenk3861aa52002-09-27 23:19:37 +000069
70/* For Debug */
wdenk3e386912003-04-05 00:53:31 +000071#if 0
72static char *dhcpmsg2str(int type)
wdenk3861aa52002-09-27 23:19:37 +000073{
74 switch (type) {
wdenk232c1502004-03-12 00:14:09 +000075 case 1: return "DHCPDISCOVER"; break;
76 case 2: return "DHCPOFFER"; break;
77 case 3: return "DHCPREQUEST"; break;
78 case 4: return "DHCPDECLINE"; break;
79 case 5: return "DHCPACK"; break;
80 case 6: return "DHCPNACK"; break;
81 case 7: return "DHCPRELEASE"; break;
wdenk3861aa52002-09-27 23:19:37 +000082 default: return "UNKNOWN/INVALID MSG TYPE"; break;
83 }
84}
wdenk3e386912003-04-05 00:53:31 +000085#endif
Jon Loeliger610f2e92007-07-10 11:05:02 -050086#endif
wdenk3861aa52002-09-27 23:19:37 +000087
Thierry Reding92ac8ac2014-08-19 10:21:24 +020088static void bootp_add_id(ulong id)
89{
90 if (bootp_num_ids >= ARRAY_SIZE(bootp_ids)) {
91 size_t size = sizeof(bootp_ids) - sizeof(id);
92
93 memmove(bootp_ids, &bootp_ids[1], size);
94 bootp_ids[bootp_num_ids - 1] = id;
95 } else {
96 bootp_ids[bootp_num_ids] = id;
97 bootp_num_ids++;
98 }
99}
100
101static bool bootp_match_id(ulong id)
102{
103 unsigned int i;
104
105 for (i = 0; i < bootp_num_ids; i++)
106 if (bootp_ids[i] == id)
107 return true;
108
109 return false;
110}
111
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200112static int check_reply_packet(uchar *pkt, unsigned dest, unsigned src,
113 unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000114{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500115 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000116 int retval = 0;
117
118 if (dest != PORT_BOOTPC || src != PORT_BOOTPS)
119 retval = -1;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500120 else if (len < sizeof(struct bootp_hdr) - OPT_FIELD_SIZE)
wdenk3861aa52002-09-27 23:19:37 +0000121 retval = -2;
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200122 else if (bp->bp_op != OP_BOOTREPLY)
wdenk3861aa52002-09-27 23:19:37 +0000123 retval = -3;
wdenk3861aa52002-09-27 23:19:37 +0000124 else if (bp->bp_htype != HWT_ETHER)
125 retval = -4;
126 else if (bp->bp_hlen != HWL_ETHER)
127 retval = -5;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500128 else if (!bootp_match_id(net_read_u32(&bp->bp_id)))
wdenk3861aa52002-09-27 23:19:37 +0000129 retval = -6;
wdenk3861aa52002-09-27 23:19:37 +0000130
Robin Getz0ebf04c2009-07-23 03:01:03 -0400131 debug("Filtering pkt = %d\n", retval);
wdenk3861aa52002-09-27 23:19:37 +0000132
133 return retval;
134}
135
136/*
137 * Copy parameters of interest from BOOTP_REPLY/DHCP_OFFER packet
138 */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500139static void store_net_params(struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000140{
Wilson Callan5d110f02007-07-28 10:56:13 -0400141#if !defined(CONFIG_BOOTP_SERVERIP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500142 struct in_addr tmp_ip;
Joe Hershberger1752f0f2012-05-23 07:59:18 +0000143
Joe Hershberger049a95a2015-04-08 01:41:01 -0500144 net_copy_ip(&tmp_ip, &bp->bp_siaddr);
145 if (tmp_ip.s_addr != 0)
146 net_copy_ip(&net_server_ip, &bp->bp_siaddr);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500147 memcpy(net_server_ethaddr,
148 ((struct ethernet_hdr *)net_rx_packet)->et_src, 6);
wdenk3d3befa2004-03-14 15:06:13 +0000149 if (strlen(bp->bp_file) > 0)
Joe Hershberger14111572015-04-08 01:41:02 -0500150 copy_filename(net_boot_file_name, bp->bp_file,
151 sizeof(net_boot_file_name));
wdenk3861aa52002-09-27 23:19:37 +0000152
Joe Hershberger14111572015-04-08 01:41:02 -0500153 debug("net_boot_file_name: %s\n", net_boot_file_name);
wdenk3861aa52002-09-27 23:19:37 +0000154
155 /* Propagate to environment:
wdenk8bde7f72003-06-27 21:31:46 +0000156 * don't delete exising entry when BOOTP / DHCP reply does
wdenk3861aa52002-09-27 23:19:37 +0000157 * not contain a new value
158 */
Joe Hershberger14111572015-04-08 01:41:02 -0500159 if (*net_boot_file_name)
160 setenv("bootfile", net_boot_file_name);
Wu, Joshecec4e92014-11-18 13:07:08 +0800161#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500162 net_copy_ip(&net_ip, &bp->bp_yiaddr);
wdenk3861aa52002-09-27 23:19:37 +0000163}
164
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000165static int truncate_sz(const char *name, int maxlen, int curlen)
wdenk3861aa52002-09-27 23:19:37 +0000166{
167 if (curlen >= maxlen) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000168 printf("*** WARNING: %s is too long (%d - max: %d)"
169 " - truncated\n", name, curlen, maxlen);
wdenk3861aa52002-09-27 23:19:37 +0000170 curlen = maxlen - 1;
171 }
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000172 return curlen;
wdenk3861aa52002-09-27 23:19:37 +0000173}
174
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500175#if !defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000176
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500177static void bootp_process_vendor_field(u8 *ext)
wdenk3861aa52002-09-27 23:19:37 +0000178{
wdenk232c1502004-03-12 00:14:09 +0000179 int size = *(ext + 1);
wdenk3861aa52002-09-27 23:19:37 +0000180
Robin Getz0ebf04c2009-07-23 03:01:03 -0400181 debug("[BOOTP] Processing extension %d... (%d bytes)\n", *ext,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500182 *(ext + 1));
wdenk3861aa52002-09-27 23:19:37 +0000183
Joe Hershberger14111572015-04-08 01:41:02 -0500184 net_boot_file_expected_size_in_blocks = 0;
wdenk3861aa52002-09-27 23:19:37 +0000185
wdenk232c1502004-03-12 00:14:09 +0000186 switch (*ext) {
187 /* Fixed length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000188 case 1: /* Subnet mask */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500189 if (net_netmask.s_addr == 0)
190 net_copy_ip(&net_netmask, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000191 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000192 case 2: /* Time offset - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000193 break;
wdenk232c1502004-03-12 00:14:09 +0000194 /* Variable length fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000195 case 3: /* Gateways list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500196 if (net_gateway.s_addr == 0)
197 net_copy_ip(&net_gateway, (struct in_addr *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000198 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000199 case 4: /* Time server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000200 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000201 case 5: /* IEN-116 name server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000202 break;
203 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500204 if (net_dns_server.s_addr == 0)
205 net_copy_ip(&net_dns_server,
206 (struct in_addr *)(ext + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500207#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500208 if ((net_dns_server2.s_addr == 0) && (size > 4))
209 net_copy_ip(&net_dns_server2,
210 (struct in_addr *)(ext + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000211#endif
wdenk3861aa52002-09-27 23:19:37 +0000212 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000213 case 7: /* Log server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000214 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000215 case 8: /* Cookie/Quote server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000216 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000217 case 9: /* LPR server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000218 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000219 case 10: /* Impress server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000220 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000221 case 11: /* RPL server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000222 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000223 case 12: /* Host name */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500224 if (net_hostname[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000225 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500226 sizeof(net_hostname), size);
227 memcpy(&net_hostname, ext + 2, size);
228 net_hostname[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000229 }
230 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000231 case 13: /* Boot file size */
wdenk3861aa52002-09-27 23:19:37 +0000232 if (size == 2)
Joe Hershberger14111572015-04-08 01:41:02 -0500233 net_boot_file_expected_size_in_blocks =
234 ntohs(*(ushort *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000235 else if (size == 4)
Joe Hershberger14111572015-04-08 01:41:02 -0500236 net_boot_file_expected_size_in_blocks =
237 ntohl(*(ulong *)(ext + 2));
wdenk3861aa52002-09-27 23:19:37 +0000238 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000239 case 14: /* Merit dump file - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000240 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000241 case 15: /* Domain name - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000242 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000243 case 16: /* Swap server - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000244 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000245 case 17: /* Root path */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500246 if (net_root_path[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000247 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500248 sizeof(net_root_path), size);
249 memcpy(&net_root_path, ext + 2, size);
250 net_root_path[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000251 }
252 break;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000253 case 18: /* Extension path - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000254 /*
wdenk8bde7f72003-06-27 21:31:46 +0000255 * This can be used to send the information of the
256 * vendor area in another file that the client can
257 * access via TFTP.
wdenk3861aa52002-09-27 23:19:37 +0000258 */
259 break;
wdenk232c1502004-03-12 00:14:09 +0000260 /* IP host layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000261 case 40: /* NIS Domain name */
Joe Hershberger586cbe52015-04-08 01:41:03 -0500262 if (net_nis_domain[0] == 0) {
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000263 size = truncate_sz("NIS Domain Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500264 sizeof(net_nis_domain), size);
265 memcpy(&net_nis_domain, ext + 2, size);
266 net_nis_domain[size] = 0;
wdenk3861aa52002-09-27 23:19:37 +0000267 }
268 break;
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000269#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
270 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500271 net_copy_ip(&net_ntp_server, (struct in_addr *)(ext + 2));
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000272 break;
273#endif
wdenk232c1502004-03-12 00:14:09 +0000274 /* Application layer fields */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000275 case 43: /* Vendor specific info - Not yet supported */
wdenk3861aa52002-09-27 23:19:37 +0000276 /*
wdenk8bde7f72003-06-27 21:31:46 +0000277 * Binary information to exchange specific
278 * product information.
wdenk3861aa52002-09-27 23:19:37 +0000279 */
280 break;
wdenk232c1502004-03-12 00:14:09 +0000281 /* Reserved (custom) fields (128..254) */
282 }
wdenk3861aa52002-09-27 23:19:37 +0000283}
284
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500285static void bootp_process_vendor(u8 *ext, int size)
wdenk3861aa52002-09-27 23:19:37 +0000286{
wdenk232c1502004-03-12 00:14:09 +0000287 u8 *end = ext + size;
wdenk3861aa52002-09-27 23:19:37 +0000288
Robin Getz0ebf04c2009-07-23 03:01:03 -0400289 debug("[BOOTP] Checking extension (%d bytes)...\n", size);
wdenk3861aa52002-09-27 23:19:37 +0000290
wdenk232c1502004-03-12 00:14:09 +0000291 while ((ext < end) && (*ext != 0xff)) {
292 if (*ext == 0) {
293 ext++;
294 } else {
295 u8 *opt = ext;
296
297 ext += ext[1] + 2;
298 if (ext <= end)
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500299 bootp_process_vendor_field(opt);
wdenk232c1502004-03-12 00:14:09 +0000300 }
wdenk3861aa52002-09-27 23:19:37 +0000301 }
wdenk3861aa52002-09-27 23:19:37 +0000302
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000303 debug("[BOOTP] Received fields:\n");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500304 if (net_netmask.s_addr)
305 debug("net_netmask : %pI4\n", &net_netmask);
wdenk3861aa52002-09-27 23:19:37 +0000306
Joe Hershberger049a95a2015-04-08 01:41:01 -0500307 if (net_gateway.s_addr)
308 debug("net_gateway : %pI4", &net_gateway);
wdenk3861aa52002-09-27 23:19:37 +0000309
Joe Hershberger14111572015-04-08 01:41:02 -0500310 if (net_boot_file_expected_size_in_blocks)
311 debug("net_boot_file_expected_size_in_blocks : %d\n",
312 net_boot_file_expected_size_in_blocks);
wdenk3861aa52002-09-27 23:19:37 +0000313
Joe Hershberger586cbe52015-04-08 01:41:03 -0500314 if (net_hostname[0])
315 debug("net_hostname : %s\n", net_hostname);
wdenk3861aa52002-09-27 23:19:37 +0000316
Joe Hershberger586cbe52015-04-08 01:41:03 -0500317 if (net_root_path[0])
318 debug("net_root_path : %s\n", net_root_path);
wdenk3861aa52002-09-27 23:19:37 +0000319
Joe Hershberger586cbe52015-04-08 01:41:03 -0500320 if (net_nis_domain[0])
321 debug("net_nis_domain : %s\n", net_nis_domain);
wdenk3861aa52002-09-27 23:19:37 +0000322
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000323#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500324 if (net_ntp_server)
325 debug("net_ntp_server : %pI4\n", &net_ntp_server);
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000326#endif
wdenk3861aa52002-09-27 23:19:37 +0000327}
Simon Glass09349862011-06-13 16:13:12 -0700328
wdenk3861aa52002-09-27 23:19:37 +0000329/*
330 * Handle a BOOTP received packet.
331 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500332static void bootp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
333 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000334{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500335 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000336
Robin Getz0ebf04c2009-07-23 03:01:03 -0400337 debug("got BOOTP packet (src=%d, dst=%d, len=%d want_len=%zu)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500338 src, dest, len, sizeof(struct bootp_hdr));
wdenk3861aa52002-09-27 23:19:37 +0000339
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500340 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000341
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000342 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200343 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000344 return;
345
346 /*
wdenk232c1502004-03-12 00:14:09 +0000347 * Got a good BOOTP reply. Copy the data into our variables.
wdenk3861aa52002-09-27 23:19:37 +0000348 */
Thomas Chou82953322015-08-25 20:54:24 +0800349#if defined(CONFIG_STATUS_LED) && defined(STATUS_LED_BOOT)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000350 status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
wdenk3861aa52002-09-27 23:19:37 +0000351#endif
352
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500353 store_net_params(bp); /* Store net parameters from reply */
wdenk3861aa52002-09-27 23:19:37 +0000354
355 /* Retrieve extended information (we must parse the vendor area) */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500356 if (net_read_u32((u32 *)&bp->bp_vend[0]) == htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500357 bootp_process_vendor((uchar *)&bp->bp_vend[4], len);
wdenk3861aa52002-09-27 23:19:37 +0000358
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500359 net_set_timeout_handler(0, (thand_f *)0);
Simon Glass573f14f2011-12-10 11:08:06 +0000360 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP, "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +0000361
Robin Getz0ebf04c2009-07-23 03:01:03 -0400362 debug("Got good BOOTP\n");
wdenk3861aa52002-09-27 23:19:37 +0000363
Simon Glasse4a3d572011-10-27 06:24:32 +0000364 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +0000365}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500366#endif
wdenk3861aa52002-09-27 23:19:37 +0000367
368/*
369 * Timeout on BOOTP/DHCP request.
370 */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500371static void bootp_timeout_handler(void)
wdenk3861aa52002-09-27 23:19:37 +0000372{
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600373 ulong time_taken = get_timer(bootp_start);
374
375 if (time_taken >= TIMEOUT_MS) {
Joe Hershberger2c00e092012-05-23 07:59:19 +0000376#ifdef CONFIG_BOOTP_MAY_FAIL
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600377 puts("\nRetry time exceeded\n");
benoit.thebaudeau@advansb977aa82012-07-19 01:23:21 +0000378 net_set_state(NETLOOP_FAIL);
Joe Hershberger2c00e092012-05-23 07:59:19 +0000379#else
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600380 puts("\nRetry time exceeded; starting again\n");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500381 net_start_again();
Joe Hershberger2c00e092012-05-23 07:59:19 +0000382#endif
wdenk3861aa52002-09-27 23:19:37 +0000383 } else {
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600384 bootp_timeout *= 2;
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200385 if (bootp_timeout > 2000)
386 bootp_timeout = 2000;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500387 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500388 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +0000389 }
390}
391
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000392#define put_vci(e, str) \
393 do { \
394 size_t vci_strlen = strlen(str); \
395 *e++ = 60; /* Vendor Class Identifier */ \
396 *e++ = vci_strlen; \
397 memcpy(e, str, vci_strlen); \
398 e += vci_strlen; \
399 } while (0)
400
wdenk3861aa52002-09-27 23:19:37 +0000401/*
402 * Initialize BOOTP extension fields in the request.
403 */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500404#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500405static int dhcp_extended(u8 *e, int message_type, struct in_addr server_ip,
406 struct in_addr requested_ip)
wdenk3861aa52002-09-27 23:19:37 +0000407{
wdenk232c1502004-03-12 00:14:09 +0000408 u8 *start = e;
409 u8 *cnt;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000410#if defined(CONFIG_BOOTP_PXE)
411 char *uuid;
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000412 u16 clientarch;
413#endif
wdenk232c1502004-03-12 00:14:09 +0000414
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500415#if defined(CONFIG_BOOTP_VENDOREX)
wdenk232c1502004-03-12 00:14:09 +0000416 u8 *x;
wdenk3861aa52002-09-27 23:19:37 +0000417#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500418#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200419 char *hostname;
stroesefe389a82003-08-28 14:17:32 +0000420#endif
wdenk3861aa52002-09-27 23:19:37 +0000421
wdenk232c1502004-03-12 00:14:09 +0000422 *e++ = 99; /* RFC1048 Magic Cookie */
423 *e++ = 130;
424 *e++ = 83;
425 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000426
wdenk232c1502004-03-12 00:14:09 +0000427 *e++ = 53; /* DHCP Message Type */
428 *e++ = 1;
429 *e++ = message_type;
wdenk3861aa52002-09-27 23:19:37 +0000430
wdenk232c1502004-03-12 00:14:09 +0000431 *e++ = 57; /* Maximum DHCP Message Size */
432 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000433 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 8;
434 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
wdenk3861aa52002-09-27 23:19:37 +0000435
Joe Hershberger049a95a2015-04-08 01:41:01 -0500436 if (server_ip.s_addr) {
437 int tmp = ntohl(server_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000438
wdenk232c1502004-03-12 00:14:09 +0000439 *e++ = 54; /* ServerID */
440 *e++ = 4;
441 *e++ = tmp >> 24;
442 *e++ = tmp >> 16;
443 *e++ = tmp >> 8;
444 *e++ = tmp & 0xff;
445 }
wdenk3861aa52002-09-27 23:19:37 +0000446
Joe Hershberger049a95a2015-04-08 01:41:01 -0500447 if (requested_ip.s_addr) {
448 int tmp = ntohl(requested_ip.s_addr);
wdenk3861aa52002-09-27 23:19:37 +0000449
wdenk232c1502004-03-12 00:14:09 +0000450 *e++ = 50; /* Requested IP */
451 *e++ = 4;
452 *e++ = tmp >> 24;
453 *e++ = tmp >> 16;
454 *e++ = tmp >> 8;
455 *e++ = tmp & 0xff;
456 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500457#if defined(CONFIG_BOOTP_SEND_HOSTNAME)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000458 hostname = getenv("hostname");
459 if (hostname) {
460 int hostnamelen = strlen(hostname);
wdenk232c1502004-03-12 00:14:09 +0000461
462 *e++ = 12; /* Hostname */
463 *e++ = hostnamelen;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000464 memcpy(e, hostname, hostnamelen);
wdenk232c1502004-03-12 00:14:09 +0000465 e += hostnamelen;
466 }
stroesefe389a82003-08-28 14:17:32 +0000467#endif
468
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000469#if defined(CONFIG_BOOTP_PXE)
470 clientarch = CONFIG_BOOTP_PXE_CLIENTARCH;
471 *e++ = 93; /* Client System Architecture */
472 *e++ = 2;
473 *e++ = (clientarch >> 8) & 0xff;
474 *e++ = clientarch & 0xff;
475
476 *e++ = 94; /* Client Network Interface Identifier */
477 *e++ = 3;
478 *e++ = 1; /* type field for UNDI */
479 *e++ = 0; /* major revision */
480 *e++ = 0; /* minor revision */
481
482 uuid = getenv("pxeuuid");
483
484 if (uuid) {
485 if (uuid_str_valid(uuid)) {
486 *e++ = 97; /* Client Machine Identifier */
487 *e++ = 17;
488 *e++ = 0; /* type 0 - UUID */
489
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200490 uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000491 e += 16;
492 } else {
493 printf("Invalid pxeuuid: %s\n", uuid);
494 }
495 }
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000496#endif
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000497
Hannes Petermaier214dc1d2015-08-25 12:17:59 +0200498#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING)
499 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
500#elif defined(CONFIG_BOOTP_VCI_STRING)
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000501 put_vci(e, CONFIG_BOOTP_VCI_STRING);
Jason Hobbsd2b5d5c2011-08-31 05:37:31 +0000502#endif
503
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500504#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000505 x = dhcp_vendorex_prep(e);
506 if (x)
wdenk232c1502004-03-12 00:14:09 +0000507 return x - start;
wdenk3861aa52002-09-27 23:19:37 +0000508#endif
509
wdenk232c1502004-03-12 00:14:09 +0000510 *e++ = 55; /* Parameter Request List */
511 cnt = e++; /* Pointer to count of requested items */
512 *cnt = 0;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500513#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000514 *e++ = 1; /* Subnet Mask */
515 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000516#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500517#if defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000518 *e++ = 2;
519 *cnt += 1;
520#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500521#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000522 *e++ = 3; /* Router Option */
523 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000524#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500525#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000526 *e++ = 6; /* DNS Server(s) */
527 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000528#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500529#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000530 *e++ = 12; /* Hostname */
531 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000532#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500533#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000534 *e++ = 13; /* Boot File Size */
535 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000536#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500537#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000538 *e++ = 17; /* Boot path */
539 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000540#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500541#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000542 *e++ = 40; /* NIS Domain name request */
543 *cnt += 1;
wdenk3861aa52002-09-27 23:19:37 +0000544#endif
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500545#if defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000546 *e++ = 42;
547 *cnt += 1;
548#endif
Jason Liu258ccd62010-11-14 12:23:09 +0800549 /* no options, so back up to avoid sending an empty request list */
550 if (*cnt == 0)
551 e -= 2;
552
wdenk232c1502004-03-12 00:14:09 +0000553 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000554
wdenk232c1502004-03-12 00:14:09 +0000555 /* Pad to minimal length */
wdenk3861aa52002-09-27 23:19:37 +0000556#ifdef CONFIG_DHCP_MIN_EXT_LEN
Simon Glass21076f62011-02-02 15:03:28 -0800557 while ((e - start) < CONFIG_DHCP_MIN_EXT_LEN)
wdenk232c1502004-03-12 00:14:09 +0000558 *e++ = 0;
wdenk3861aa52002-09-27 23:19:37 +0000559#endif
560
wdenk232c1502004-03-12 00:14:09 +0000561 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000562}
563
Jon Loeliger610f2e92007-07-10 11:05:02 -0500564#else
wdenk3861aa52002-09-27 23:19:37 +0000565/*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000566 * Warning: no field size check - change CONFIG_BOOTP_* at your own risk!
wdenk3861aa52002-09-27 23:19:37 +0000567 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500568static int bootp_extended(u8 *e)
wdenk3861aa52002-09-27 23:19:37 +0000569{
wdenk232c1502004-03-12 00:14:09 +0000570 u8 *start = e;
wdenk3861aa52002-09-27 23:19:37 +0000571
wdenk232c1502004-03-12 00:14:09 +0000572 *e++ = 99; /* RFC1048 Magic Cookie */
573 *e++ = 130;
574 *e++ = 83;
575 *e++ = 99;
wdenk3861aa52002-09-27 23:19:37 +0000576
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500577#if defined(CONFIG_CMD_DHCP)
wdenk232c1502004-03-12 00:14:09 +0000578 *e++ = 53; /* DHCP Message Type */
579 *e++ = 1;
580 *e++ = DHCP_DISCOVER;
wdenk3861aa52002-09-27 23:19:37 +0000581
wdenk232c1502004-03-12 00:14:09 +0000582 *e++ = 57; /* Maximum DHCP Message Size */
583 *e++ = 2;
Joe Hershbergerf8315732012-05-23 07:58:14 +0000584 *e++ = (576 - 312 + OPT_FIELD_SIZE) >> 16;
585 *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500586#endif
wdenk3861aa52002-09-27 23:19:37 +0000587
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000588#if defined(CONFIG_BOOTP_VCI_STRING) || \
589 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
590#ifdef CONFIG_SPL_BUILD
591 put_vci(e, CONFIG_SPL_NET_VCI_STRING);
592#else
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000593 put_vci(e, CONFIG_BOOTP_VCI_STRING);
594#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000595#endif
Ilya Yanok9ace17c2012-09-17 10:26:25 +0000596
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500597#if defined(CONFIG_BOOTP_SUBNETMASK)
wdenk232c1502004-03-12 00:14:09 +0000598 *e++ = 1; /* Subnet mask request */
599 *e++ = 4;
600 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000601#endif
602
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500603#if defined(CONFIG_BOOTP_GATEWAY)
wdenk232c1502004-03-12 00:14:09 +0000604 *e++ = 3; /* Default gateway request */
605 *e++ = 4;
606 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000607#endif
608
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500609#if defined(CONFIG_BOOTP_DNS)
wdenk232c1502004-03-12 00:14:09 +0000610 *e++ = 6; /* Domain Name Server */
611 *e++ = 4;
612 e += 4;
wdenk3861aa52002-09-27 23:19:37 +0000613#endif
614
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500615#if defined(CONFIG_BOOTP_HOSTNAME)
wdenk232c1502004-03-12 00:14:09 +0000616 *e++ = 12; /* Host name request */
617 *e++ = 32;
618 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000619#endif
620
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500621#if defined(CONFIG_BOOTP_BOOTFILESIZE)
wdenk232c1502004-03-12 00:14:09 +0000622 *e++ = 13; /* Boot file size */
623 *e++ = 2;
624 e += 2;
wdenk3861aa52002-09-27 23:19:37 +0000625#endif
626
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500627#if defined(CONFIG_BOOTP_BOOTPATH)
wdenk232c1502004-03-12 00:14:09 +0000628 *e++ = 17; /* Boot path */
629 *e++ = 32;
630 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000631#endif
632
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500633#if defined(CONFIG_BOOTP_NISDOMAIN)
wdenk232c1502004-03-12 00:14:09 +0000634 *e++ = 40; /* NIS Domain name request */
635 *e++ = 32;
636 e += 32;
wdenk3861aa52002-09-27 23:19:37 +0000637#endif
Luuk Paulussen09e3a672011-05-16 18:29:19 +0000638#if defined(CONFIG_BOOTP_NTPSERVER)
639 *e++ = 42;
640 *e++ = 4;
641 e += 4;
642#endif
wdenk3861aa52002-09-27 23:19:37 +0000643
wdenk232c1502004-03-12 00:14:09 +0000644 *e++ = 255; /* End of the list */
wdenk3861aa52002-09-27 23:19:37 +0000645
wdenk232c1502004-03-12 00:14:09 +0000646 return e - start;
wdenk3861aa52002-09-27 23:19:37 +0000647}
Jon Loeliger610f2e92007-07-10 11:05:02 -0500648#endif
wdenk3861aa52002-09-27 23:19:37 +0000649
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500650void bootp_reset(void)
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600651{
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200652 bootp_num_ids = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500653 bootp_try = 0;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600654 bootp_start = get_timer(0);
Thierry Reding92ac8ac2014-08-19 10:21:24 +0200655 bootp_timeout = 250;
Stephen Warrenf59be6e2014-07-25 17:30:48 -0600656}
657
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500658void bootp_request(void)
wdenk3861aa52002-09-27 23:19:37 +0000659{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000660 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500661 struct bootp_hdr *bp;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000662 int extlen, pktlen, iplen;
663 int eth_hdr_size;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000664#ifdef CONFIG_BOOTP_RANDOM_DELAY
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200665 ulong rand_ms;
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000666#endif
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500667 u32 bootp_id;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500668 struct in_addr zero_ip;
669 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000670
Simon Glass573f14f2011-12-10 11:08:06 +0000671 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500672#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000673 dhcp_state = INIT;
674#endif
675
676#ifdef CONFIG_BOOTP_RANDOM_DELAY /* Random BOOTP delay */
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500677 if (bootp_try == 0)
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000678 srand_mac();
wdenk3861aa52002-09-27 23:19:37 +0000679
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500680 if (bootp_try <= 2) /* Start with max 1024 * 1ms */
681 rand_ms = rand() >> (22 - bootp_try);
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000682 else /* After 3rd BOOTP request max 8192 * 1ms */
683 rand_ms = rand() >> 19;
wdenk3861aa52002-09-27 23:19:37 +0000684
Joe Hershbergereafc8db2012-05-23 07:57:58 +0000685 printf("Random delay: %ld ms...\n", rand_ms);
Pavel Machek8e8d73b2014-07-11 11:39:37 +0200686 mdelay(rand_ms);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000687
wdenk3861aa52002-09-27 23:19:37 +0000688#endif /* CONFIG_BOOTP_RANDOM_DELAY */
689
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500690 printf("BOOTP broadcast %d\n", ++bootp_try);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500691 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000692 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000693
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500694 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000695 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000696
697 /*
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000698 * Next line results in incorrect packet size being transmitted,
699 * resulting in errors in some DHCP servers, reporting missing bytes.
700 * Size must be set in packet header after extension length has been
701 * determined.
wdenk3861aa52002-09-27 23:19:37 +0000702 * C. Hallinan, DS4.COM, Inc.
703 */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000704 /* net_set_udp_header(pkt, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC,
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500705 sizeof (struct bootp_hdr)); */
Joe Hershberger4b11c912012-05-23 07:59:07 +0000706 iphdr = pkt; /* We need this later for net_set_udp_header() */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000707 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000708
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500709 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000710 bp->bp_op = OP_BOOTREQUEST;
711 bp->bp_htype = HWT_ETHER;
712 bp->bp_hlen = HWL_ETHER;
713 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200714 /*
715 * according to RFC1542, should be 0 on first request, secs since
716 * first request otherwise
717 */
718 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500719 zero_ip.s_addr = 0;
720 net_write_ip(&bp->bp_ciaddr, zero_ip);
721 net_write_ip(&bp->bp_yiaddr, zero_ip);
722 net_write_ip(&bp->bp_siaddr, zero_ip);
723 net_write_ip(&bp->bp_giaddr, zero_ip);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500724 memcpy(bp->bp_chaddr, net_ethaddr, 6);
Joe Hershberger14111572015-04-08 01:41:02 -0500725 copy_filename(bp->bp_file, net_boot_file_name, sizeof(bp->bp_file));
wdenk3861aa52002-09-27 23:19:37 +0000726
727 /* Request additional information from the BOOTP/DHCP server */
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500728#if defined(CONFIG_CMD_DHCP)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500729 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_DISCOVER, zero_ip,
730 zero_ip);
wdenk3861aa52002-09-27 23:19:37 +0000731#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500732 extlen = bootp_extended((u8 *)bp->bp_vend);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500733#endif
wdenk3861aa52002-09-27 23:19:37 +0000734
735 /*
736 * Bootp ID is the lower 4 bytes of our ethernet address
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +0200737 * plus the current time in ms.
wdenk3861aa52002-09-27 23:19:37 +0000738 */
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500739 bootp_id = ((u32)net_ethaddr[2] << 24)
740 | ((u32)net_ethaddr[3] << 16)
741 | ((u32)net_ethaddr[4] << 8)
742 | (u32)net_ethaddr[5];
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500743 bootp_id += get_timer(0);
744 bootp_id = htonl(bootp_id);
745 bootp_add_id(bootp_id);
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500746 net_copy_u32(&bp->bp_id, &bootp_id);
wdenk3861aa52002-09-27 23:19:37 +0000747
748 /*
749 * Calculate proper packet lengths taking into account the
750 * variable size of the options field
751 */
Joe Hershbergerae446f52012-05-23 07:59:10 +0000752 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
753 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500754 bcast_ip.s_addr = 0xFFFFFFFFL;
755 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500756 net_set_timeout_handler(bootp_timeout, bootp_timeout_handler);
wdenk3861aa52002-09-27 23:19:37 +0000757
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500758#if defined(CONFIG_CMD_DHCP)
wdenk3861aa52002-09-27 23:19:37 +0000759 dhcp_state = SELECTING;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500760 net_set_udp_handler(dhcp_handler);
wdenk3861aa52002-09-27 23:19:37 +0000761#else
Joe Hershberger049a95a2015-04-08 01:41:01 -0500762 net_set_udp_handler(bootp_handler);
Jon Loeliger610f2e92007-07-10 11:05:02 -0500763#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500764 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000765}
766
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500767#if defined(CONFIG_CMD_DHCP)
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500768static void dhcp_process_options(uchar *popt, struct bootp_hdr *bp)
wdenk3861aa52002-09-27 23:19:37 +0000769{
wdenk3e386912003-04-05 00:53:31 +0000770 uchar *end = popt + BOOTP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000771 int oplen, size;
Wolfgang Denkd8d87242009-09-11 09:05:32 +0200772#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
773 int *to_ptr;
774#endif
wdenk3861aa52002-09-27 23:19:37 +0000775
wdenk232c1502004-03-12 00:14:09 +0000776 while (popt < end && *popt != 0xff) {
wdenk3861aa52002-09-27 23:19:37 +0000777 oplen = *(popt + 1);
wdenk232c1502004-03-12 00:14:09 +0000778 switch (*popt) {
779 case 1:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500780 net_copy_ip(&net_netmask, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000781 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500782#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000783 case 2: /* Time offset */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500784 to_ptr = &net_ntp_time_offset;
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500785 net_copy_u32((u32 *)to_ptr, (u32 *)(popt + 2));
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500786 net_ntp_time_offset = ntohl(net_ntp_time_offset);
wdenkea287de2005-04-01 00:25:43 +0000787 break;
788#endif
wdenk232c1502004-03-12 00:14:09 +0000789 case 3:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500790 net_copy_ip(&net_gateway, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000791 break;
792 case 6:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500793 net_copy_ip(&net_dns_server, (popt + 2));
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500794#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000795 if (*(popt + 1) > 4)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500796 net_copy_ip(&net_dns_server2, (popt + 2 + 4));
stroesefe389a82003-08-28 14:17:32 +0000797#endif
wdenk232c1502004-03-12 00:14:09 +0000798 break;
799 case 12:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000800 size = truncate_sz("Host Name",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500801 sizeof(net_hostname), oplen);
802 memcpy(&net_hostname, popt + 2, size);
803 net_hostname[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000804 break;
805 case 15: /* Ignore Domain Name Option */
806 break;
807 case 17:
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000808 size = truncate_sz("Root Path",
Joe Hershberger586cbe52015-04-08 01:41:03 -0500809 sizeof(net_root_path), oplen);
810 memcpy(&net_root_path, popt + 2, size);
811 net_root_path[size] = 0;
wdenk232c1502004-03-12 00:14:09 +0000812 break;
Brian Rzyckiee0f60d2012-09-11 09:22:53 +0000813 case 28: /* Ignore Broadcast Address Option */
814 break;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500815#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000816 case 42: /* NTP server IP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500817 net_copy_ip(&net_ntp_server, (popt + 2));
wdenkea287de2005-04-01 00:25:43 +0000818 break;
819#endif
wdenk232c1502004-03-12 00:14:09 +0000820 case 51:
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500821 net_copy_u32(&dhcp_leasetime, (u32 *)(popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000822 break;
823 case 53: /* Ignore Message Type Option */
824 break;
825 case 54:
Joe Hershberger049a95a2015-04-08 01:41:01 -0500826 net_copy_ip(&dhcp_server_ip, (popt + 2));
wdenk232c1502004-03-12 00:14:09 +0000827 break;
828 case 58: /* Ignore Renewal Time Option */
829 break;
830 case 59: /* Ignore Rebinding Time Option */
831 break;
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100832 case 66: /* Ignore TFTP server name */
833 break;
834 case 67: /* vendor opt bootfile */
835 /*
836 * I can't use dhcp_vendorex_proc here because I need
837 * to write into the bootp packet - even then I had to
838 * pass the bootp packet pointer into here as the
839 * second arg
840 */
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000841 size = truncate_sz("Opt Boot File",
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100842 sizeof(bp->bp_file),
843 oplen);
844 if (bp->bp_file[0] == '\0' && size > 0) {
845 /*
846 * only use vendor boot file if we didn't
847 * receive a boot file in the main non-vendor
848 * part of the packet - god only knows why
849 * some vendors chose not to use this perfectly
850 * good spot to store the boot file (join on
851 * Tru64 Unix) it seems mind bogglingly crazy
852 * to me
853 */
854 printf("*** WARNING: using vendor "
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500855 "optional boot file\n");
Wolfgang Denk3b2e4fd2006-03-12 18:26:46 +0100856 memcpy(bp->bp_file, popt + 2, size);
857 bp->bp_file[size] = '\0';
858 }
859 break;
wdenk232c1502004-03-12 00:14:09 +0000860 default:
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500861#if defined(CONFIG_BOOTP_VENDOREX)
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000862 if (dhcp_vendorex_proc(popt))
wdenk8bde7f72003-06-27 21:31:46 +0000863 break;
wdenk3861aa52002-09-27 23:19:37 +0000864#endif
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000865 printf("*** Unhandled DHCP Option in OFFER/ACK:"
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500866 " %d\n", *popt);
wdenk232c1502004-03-12 00:14:09 +0000867 break;
wdenk3861aa52002-09-27 23:19:37 +0000868 }
869 popt += oplen + 2; /* Process next option */
870 }
871}
872
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500873static int dhcp_message_type(unsigned char *popt)
wdenk3861aa52002-09-27 23:19:37 +0000874{
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500875 if (net_read_u32((u32 *)popt) != htonl(BOOTP_VENDOR_MAGIC))
wdenk3861aa52002-09-27 23:19:37 +0000876 return -1;
877
878 popt += 4;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000879 while (*popt != 0xff) {
880 if (*popt == 53) /* DHCP Message Type */
wdenk3861aa52002-09-27 23:19:37 +0000881 return *(popt + 2);
882 popt += *(popt + 1) + 2; /* Scan through all options */
883 }
884 return -1;
885}
886
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500887static void dhcp_send_request_packet(struct bootp_hdr *bp_offer)
wdenk3861aa52002-09-27 23:19:37 +0000888{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000889 uchar *pkt, *iphdr;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500890 struct bootp_hdr *bp;
wdenk3861aa52002-09-27 23:19:37 +0000891 int pktlen, iplen, extlen;
Joe Hershbergerae446f52012-05-23 07:59:10 +0000892 int eth_hdr_size;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500893 struct in_addr offered_ip;
894 struct in_addr zero_ip;
895 struct in_addr bcast_ip;
wdenk3861aa52002-09-27 23:19:37 +0000896
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500897 debug("dhcp_send_request_packet: Sending DHCPREQUEST\n");
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500898 pkt = net_tx_packet;
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000899 memset((void *)pkt, 0, PKTSIZE);
wdenk3861aa52002-09-27 23:19:37 +0000900
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500901 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_IP);
Joe Hershbergerae446f52012-05-23 07:59:10 +0000902 pkt += eth_hdr_size;
wdenk3861aa52002-09-27 23:19:37 +0000903
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000904 iphdr = pkt; /* We'll need this later to set proper pkt size */
Joe Hershberger594c26f2012-05-23 07:58:04 +0000905 pkt += IP_UDP_HDR_SIZE;
wdenk3861aa52002-09-27 23:19:37 +0000906
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500907 bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000908 bp->bp_op = OP_BOOTREQUEST;
909 bp->bp_htype = HWT_ETHER;
910 bp->bp_hlen = HWL_ETHER;
911 bp->bp_hops = 0;
Stefan Brüns454d9d32015-08-27 23:57:18 +0200912 bp->bp_secs = htons(get_timer(bootp_start) / 1000);
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000913 /* Do not set the client IP, your IP, or server IP yet, since it
914 * hasn't been ACK'ed by the server yet */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400915
Wolfgang Denkc6686702006-10-12 00:01:08 +0200916 /*
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200917 * RFC3046 requires Relay Agents to discard packets with
918 * nonzero and offered giaddr
919 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500920 zero_ip.s_addr = 0;
921 net_write_ip(&bp->bp_giaddr, zero_ip);
Wolfgang Denkd82718f2006-10-09 01:26:14 +0200922
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500923 memcpy(bp->bp_chaddr, net_ethaddr, 6);
wdenk3861aa52002-09-27 23:19:37 +0000924
925 /*
926 * ID is the id of the OFFER packet
927 */
928
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500929 net_copy_u32(&bp->bp_id, &bp_offer->bp_id);
wdenk3861aa52002-09-27 23:19:37 +0000930
931 /*
932 * Copy options from OFFER packet if present
933 */
Justin Flammiae5c794e2007-10-29 17:40:35 -0400934
935 /* Copy offered IP into the parameters request list */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500936 net_copy_ip(&offered_ip, &bp_offer->bp_yiaddr);
937 extlen = dhcp_extended((u8 *)bp->bp_vend, DHCP_REQUEST,
938 dhcp_server_ip, offered_ip);
wdenk3861aa52002-09-27 23:19:37 +0000939
Joe Hershbergerae446f52012-05-23 07:59:10 +0000940 iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
941 pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500942 bcast_ip.s_addr = 0xFFFFFFFFL;
943 net_set_udp_header(iphdr, bcast_ip, PORT_BOOTPS, PORT_BOOTPC, iplen);
wdenk3861aa52002-09-27 23:19:37 +0000944
Aras Vaichasd9a2f412008-03-26 09:43:57 +1100945#ifdef CONFIG_BOOTP_DHCP_REQUEST_DELAY
946 udelay(CONFIG_BOOTP_DHCP_REQUEST_DELAY);
947#endif /* CONFIG_BOOTP_DHCP_REQUEST_DELAY */
Joe Hershbergerf9623222012-05-23 07:59:11 +0000948 debug("Transmitting DHCPREQUEST packet: len = %d\n", pktlen);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500949 net_send_packet(net_tx_packet, pktlen);
wdenk3861aa52002-09-27 23:19:37 +0000950}
951
952/*
953 * Handle DHCP received packets.
954 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500955static void dhcp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
956 unsigned src, unsigned len)
wdenk3861aa52002-09-27 23:19:37 +0000957{
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500958 struct bootp_hdr *bp = (struct bootp_hdr *)pkt;
wdenk3861aa52002-09-27 23:19:37 +0000959
Robin Getz0ebf04c2009-07-23 03:01:03 -0400960 debug("DHCPHandler: got packet: (src=%d, dst=%d, len=%d) state: %d\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500961 src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000962
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000963 /* Filter out pkts we don't want */
Stefan Brüns867d6ae2015-08-27 23:53:26 +0200964 if (check_reply_packet(pkt, dest, src, len))
wdenk3861aa52002-09-27 23:19:37 +0000965 return;
966
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500967 debug("DHCPHandler: got DHCP packet: (src=%d, dst=%d, len=%d) state: "
968 "%d\n", src, dest, len, dhcp_state);
wdenk3861aa52002-09-27 23:19:37 +0000969
970 switch (dhcp_state) {
971 case SELECTING:
972 /*
973 * Wait an appropriate time for any potential DHCPOFFER packets
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000974 * to arrive. Then select one, and generate DHCPREQUEST
975 * response. If filename is in format we recognize, assume it
976 * is a valid OFFER from a server we want.
wdenk3861aa52002-09-27 23:19:37 +0000977 */
Robin Getz0ebf04c2009-07-23 03:01:03 -0400978 debug("DHCP: state=SELECTING bp_file: \"%s\"\n", bp->bp_file);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200979#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000980 if (strncmp(bp->bp_file,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200981 CONFIG_SYS_BOOTFILE_PREFIX,
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000982 strlen(CONFIG_SYS_BOOTFILE_PREFIX)) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200983#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000984
Robin Getz0ebf04c2009-07-23 03:01:03 -0400985 debug("TRANSITIONING TO REQUESTING STATE\n");
wdenk3861aa52002-09-27 23:19:37 +0000986 dhcp_state = REQUESTING;
stroese759a51b2003-04-10 13:26:44 +0000987
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -0500988 if (net_read_u32((u32 *)&bp->bp_vend[0]) ==
Joe Hershberger3090b7e32012-05-15 08:59:06 +0000989 htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500990 dhcp_process_options((u8 *)&bp->bp_vend[4], bp);
wdenk3861aa52002-09-27 23:19:37 +0000991
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500992 net_set_timeout_handler(5000, bootp_timeout_handler);
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500993 dhcp_send_request_packet(bp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200994#ifdef CONFIG_SYS_BOOTFILE_PREFIX
wdenk3861aa52002-09-27 23:19:37 +0000995 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200996#endif /* CONFIG_SYS_BOOTFILE_PREFIX */
wdenk3861aa52002-09-27 23:19:37 +0000997
998 return;
999 break;
1000 case REQUESTING:
Robin Getz0ebf04c2009-07-23 03:01:03 -04001001 debug("DHCP State: REQUESTING\n");
wdenk3861aa52002-09-27 23:19:37 +00001002
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001003 if (dhcp_message_type((u8 *)bp->bp_vend) == DHCP_ACK) {
Sergey Temerkhanov5917e7d2015-04-08 01:41:22 -05001004 if (net_read_u32((u32 *)&bp->bp_vend[0]) ==
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001005 htonl(BOOTP_VENDOR_MAGIC))
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001006 dhcp_process_options((u8 *)&bp->bp_vend[4], bp);
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001007 /* Store net params from reply */
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001008 store_net_params(bp);
wdenk3861aa52002-09-27 23:19:37 +00001009 dhcp_state = BOUND;
Thierry Reding92ac8ac2014-08-19 10:21:24 +02001010 printf("DHCP client bound to address %pI4 (%lu ms)\n",
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001011 &net_ip, get_timer(bootp_start));
Simon Glass573f14f2011-12-10 11:08:06 +00001012 bootstage_mark_name(BOOTSTAGE_ID_BOOTP_STOP,
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001013 "bootp_stop");
wdenk3861aa52002-09-27 23:19:37 +00001014
Simon Glasse4a3d572011-10-27 06:24:32 +00001015 net_auto_load();
wdenk3861aa52002-09-27 23:19:37 +00001016 return;
1017 }
1018 break;
Remy Bohmer51dfe132008-08-20 11:30:28 +02001019 case BOUND:
1020 /* DHCP client bound to address */
1021 break;
wdenk3861aa52002-09-27 23:19:37 +00001022 default:
Joe Hershberger3090b7e32012-05-15 08:59:06 +00001023 puts("DHCP: INVALID STATE\n");
wdenk3861aa52002-09-27 23:19:37 +00001024 break;
1025 }
wdenk3861aa52002-09-27 23:19:37 +00001026}
1027
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001028void dhcp_request(void)
wdenk3861aa52002-09-27 23:19:37 +00001029{
Joe Hershberger7044c6b2015-04-08 01:41:09 -05001030 bootp_request();
wdenk3861aa52002-09-27 23:19:37 +00001031}
Wolfgang Denk992742a2007-11-03 23:09:27 +01001032#endif /* CONFIG_CMD_DHCP */